Skip to content

di.torq.proc.gateway: register backends that connect after startup - #145

Merged
alowrydi merged 2 commits into
DataIntellectTech:mainfrom
DI-Dexter:fix-gateway-late-backend
Oct 16, 2026
Merged

alowrydi merged 2 commits into
DataIntellectTech:mainfrom
DI-Dexter:fix-gateway-late-backend

Conversation

@DI-Dexter

Copy link
Copy Markdown
Collaborator

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

# ensure you're using main branch
cd path/to/kdbx-modules && git checkout main
cd path/to/TorqX-POC && source ./setenv.sh

# stop anything already running, so the stack picks up the checked-out branch
torqx.sh stop

# start all processes
torqx.sh start

# bring down gateway
torqx.sh stop gateway1

# bring down rdb
torqx.sh stop rdb1

# bring up gateway and wait 10s
torqx.sh start gateway1

# bring up rdb and wait 20s
torqx.sh start rdb1

# show what the gw is connected to (rdb is connected)
echo 'h:hopen`::5306; show h"select proctype,procname,w,live:.dotz.liveh0 w from .servers.SERVERS"; exit 0' | q

proctype  procname   w  live
----------------------------
discovery discovery1 6  1
rdb       rdb1       10 1
hdb       hdb        7  1
idb       idb1       8  1

# show what the gateway can route to (rdb missing)
echo 'h:hopen`::5306; show h"select servertype,handle,active from .m.di.0serverselect.servers"; exit 0' | q
servertype handle active
------------------------
hdb        7      1
idb        8      1

# try to send a query to rdb
echo 'h:hopen`::5306; neg[h](`.gw.asyncexecjpt;"select cnt:count i from trade";`rdb;raze;();0D00:00:05); show h[]; exit 0' | q
"error: query timed out"

# send the same query to the hdb
echo 'h:hopen`::5306; neg[h](`.gw.asyncexecjpt;"select cnt:count i from trade";`hdb;raze;();0D00:00:05); show h[]; exit 0' | q
cnt
---
6

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:

set[`.servers.connectcustom;{[rows] registerbackends[]}];

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:

# cd path/to/kdbx-modules && git checkout <this branch>
# cd path/to/TorqX-POC && source ./setenv.sh

torqx.sh stop gateway1
torqx.sh stop rdb1
torqx.sh start gateway1   # wait ~10s
torqx.sh start rdb1       # wait ~20s

Re-run the same four checks. 
di.serverselect now lists the rdb, and the rdb query returns a count instead of timing out.

# connected
echo 'h:hopen`::5306; show h"select proctype,procname,w,live:.dotz.liveh0 w from .servers.SERVERS"; exit 0' | q

# routable - the rdb should now appear here
echo 'h:hopen`::5306; show h"select servertype,handle,active from .m.di.0serverselect.servers"; exit 0' | q

# query the rdb - should return a count, not a timeout
echo 'h:hopen`::5306; neg[h](`.gw.asyncexecjpt;"select cnt:count i from trade";`rdb;raze;();0D00:00:05); show h[]; exit 0' | q

# control
echo 'h:hopen`::5306; neg[h](`.gw.asyncexecjpt;"select cnt:count i from trade";`hdb;raze;();0D00:00:05); show h[]; exit 0' | q

DI-Dexter and others added 2 commits October 15, 2026 15:12
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>
@DI-Dexter
DI-Dexter requested a review from alowrydi October 16, 2026 09:03
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,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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[]}]`.

@DI-Software-Engineering

Copy link
Copy Markdown

DIReview Summary

0 critical | 1 warning(s) | 0 suggestion(s)

⚠️ Spec check skipped — tracker lookup failed (NO_REF_FOUND). Standards axis only.

@alowrydi alowrydi left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 current main)
  • 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

@alowrydi
alowrydi merged commit 0901741 into DataIntellectTech:main Oct 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants