Repository navigation
di.torq.proc.gateway: register backends that connect after startup - #145
Conversation
registerbackends ran only at init and at EOD reloadend, so a backend that was down when the gateway started was never copied into di.serverselect - the registry di.asyncdispatch routes on. di.torq.servers' retry does reconnect it, leaving the gateway holding a live handle it could not route to until a restart or the next EOD. Hook registerbackends onto .servers.connectcustom, which retryrows fires on every successful new connection. registerbackends is already idempotent and re-reads the whole registry, so it ignores the rows it is handed. The .z.pc side already deactivated a backend on disconnect - this completes the pair. Verified against the TorqX-POC stack: starting gateway1 with rdb1 down and then starting rdb1 leaves the rdb connected-but-unroutable on main, and routable here within one retry interval, with no gateway restart. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
The suite could not reach init before - it only checked the export shape and that init rejects missing deps - so nothing covered the registry the gateway actually routes on. Mock the four injected deps (log, timer, handlers, servers) and let di.serverselect, di.asyncdispatch and di.dataaccess be the real modules, so init completes and the tests can assert against di.serverselect's table. The servers mock reports connections from a BACKENDS table a test can edit, which is what makes the late-connect case expressible at all. Covers: the hdb registers at init, an rdb that was down does not, init installs the .servers.connectcustom hook, and firing that hook the way retryrows does makes the late rdb routable without a restart. Removing the hook from gateway.q fails exactly these three and leaves the other nine passing. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
| sconfig:config,(enlist`connections)!enlist .z.m.backendtypes; | ||
| (.z.m.svc`startup)[sconfig]; | ||
| registerbackends[]; | ||
| / a backend that was down at startup connects later through di.torq.servers' retry/discovery, |
There was a problem hiding this comment.
set[.servers.connectcustom;{[rows] registerbackends[]}]unconditionally overwrites whatever was previously at.servers.connectcustom. If initis called more than once (the consistency guide requires injected-moduleinitto be idempotent) the hook is re-installed harmlessly, but more importantly any third-party hook already registered at.servers.connectcustomby another module is silently discarded. The gateway should compose with the existing hook rather than replace it — e.g. capture the previous value and chain it:prev:@[value;.servers.connectcustom;{{[rows]}}]; set[.servers.connectcustom;{[rows] prev[rows]; registerbackends[]}]`.
DIReview Summary0 critical | 1 warning(s) | 0 suggestion(s)
|
alowrydi
left a comment
There was a problem hiding this comment.
Ran this against the POC and it seems to work well, once the changes from main are also included.
Verified against TorqX-POC
Same sequence on both builds (start the stack, stop gateway1 and rdb1, wait until both are down, start gateway1, then start rdb1 and wait ~20s):
main |
this PR | |
|---|---|---|
rdb in .servers.SERVERS |
live | live |
rdb in .m.di.0serverselect.servers |
missing | present, active |
rdb query via .gw.asyncexecjpt |
"error: query timed out" |
returns a count |
| hdb control query | OK | OK |
The gateway log shows registered 2 backend server(s): hdb, idb at init, then registered 3 backend server(s): rdb, hdb, idb about 5s after the rdb started, with no gateway restart.
- k4unit
di.torq.proc.gateway: passed (12 tests, run on the branch merged with currentmain) - Reconnect after a drop: with the gateway left running, stopping the rdb set its row to
active=0, and restarting it added an active row within ~30s; the rdb query returned a count
registerbackends ran only at init and at EOD reloadend, so a backend that was down when the gateway started was never copied into di.serverselect - the registry di.asyncdispatch routes on. di.torq.servers' retry does reconnect it, leaving the gateway holding a live handle it could not route to until a restart or the next EOD.
Hook registerbackends onto .servers.connectcustom, which retryrows fires on every successful new connection. registerbackends is already idempotent and re-reads the whole registry, so it ignores the rows it is handed. The .z.pc side already deactivated a backend on disconnect - this completes the pair.
Verified against the TorqX-POC stack: starting gateway1 with rdb1 down and then starting rdb1 leaves the rdb connected-but-unroutable on main, and routable here within one retry interval, with no gateway restart.
To test
Underlying issue
The gateway keeps two registries of its backends:
.servers.SERVERS (di.torq.servers) — the connection registry. A serversretry timer job keeps dialling peers that are down, so it reconnects a late backend on its own.
di.serverselect — the routing table, and the one di.asyncdispatch actually dispatches on (gateway.q:99).
registerbackends (gateway.q:44) is the only thing that copies the first into the second.
It ran at exactly two moments:
init — gateway.q:110
EOD reloadend — gateway.q:73
Nothing called it when a backend connected later.
Result: a backend that was down at gateway startup gets reconnected by di.torq.servers, so the gateway holds a live handle it cannot route to.
It stays broken until the gateway is restarted or the next EOD reloadend — which is why a manual restart "fixes" it: init re-runs, and by then the backend is up.
The fix
di.torq.servers already provides the hook: retryrows fires .servers.connectcustom[connectedrows] after every successful new connection (servers.q:159).
It ships as a no-op (servers.q:356), and nothing else in a gateway process installs it.
So the gateway hooks registerbackends onto it:
registerbackends is already idempotent and re-reads the whole connection registry — it ignores the rows it's handed.
Both ways a connection can be established funnel through retryrows, so one hook covers both:
the plain serversretry timer
the discovery-driven path
A hook rather than a periodic registerbackends timer job: it fires exactly when the state changes, costs nothing in between, and is the mechanism already present in the framework.
Verifying the fix
Only gateway.q changes, so the rest of the stack can stay up — just restart the gateway and rdb: