Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions di/torq/proc/gateway/gateway.q
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ init:{[config;deps]
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[]}]`.

/ which fires .servers.connectcustom - hook it so serverselect learns about the late arrival.
/ Without this the gateway holds a live handle it can never route to until a restart or EOD.
set[`.servers.connectcustom;{[rows] registerbackends[]}];

/ di.dataaccess - the getdata query-normalisation layer, on top of the SAME asyncdispatch
/ instance (idempotent use) so it dispatches via the callbacks/registry set up above. Partition
Expand Down
13 changes: 13 additions & 0 deletions di/torq/proc/gateway/test.csv
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,16 @@ fail,0,0,q,gw.init[()!();(enlist`log)!enlist mlog],1,,init with log but no timer

true,0,0,q,`init`asyncexec`asyncexecjpt`reload`version~key gw,1,,module exports init/asyncexec/asyncexecjpt/reload/version
true,0,0,q,0<count gw`version,1,,version string is present (e.g. 0.1.0)

before,0,0,q,system "l di/torq/proc/gateway/test.q",1,,mock log/timer/handlers/servers deps
before,0,0,q,hdbonly[],1,,only the hdb is connected when the gateway starts

run,0,0,q,gw[`init][cfg[];mockdeps[]],1,,init with the hdb up and the rdb down
true,0,0,q,`hdb in routable[],1,,the connected hdb is registered into di.serverselect at init
true,0,0,q,not `rdb in routable[],1,,the rdb was down at init so it is not routable
true,0,0,q,hookinstalled[],1,,init installs the .servers.connectcustom hook di.torq.servers fires on a new connection

run,0,0,q,hdbandrdb[],1,,the rdb connects after startup - di.torq.servers' retry has reconnected it
run,0,0,q,fireconnectcustom[],1,,di.torq.servers' retryrows fires the hook with the connected rows
true,0,0,q,`rdb in routable[],1,,REGRESSION: a backend that connected after startup becomes routable without a restart
true,0,0,q,`hdb in routable[],1,,the already-registered hdb is undisturbed
46 changes: 46 additions & 0 deletions di/torq/proc/gateway/test.q
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/ shared mocks for di.torq.proc.gateway's tests. di.serverselect, di.asyncdispatch and
/ di.dataaccess are the REAL modules - only the four injected deps are mocked, so the tests
/ exercise the registry the gateway actually routes on.

/ --- log ---
calls:([]lvl:`symbol$();ctx:`symbol$();msg:())
resetcalls:{[] `calls set ([]lvl:`symbol$();ctx:`symbol$();msg:()); }
mocklogfn:{[lvl;ctx;msg] `calls insert (lvl;ctx;msg); }
mocklog:{[] `info`warn`error!(mocklogfn[`info;;];mocklogfn[`warn;;];mocklogfn[`error;;])}

/ --- timer / handlers: record the registration and do nothing ---
jobs:`symbol$()
mockaddjob:{[i;f;p;pe;m;o] `jobs set jobs,i; }
mocktimer:{[] `addjob`deletejobs`enablejobs`disablejobs`getactivejobs!(mockaddjob;{[x]};{[x]};{[x]};{[]()})}
mockhandlers:{[] `register`remove`list!({[e;n;nm;p;f]};{[e;n]};{[e]()})}

/ --- servers ---
/ BACKENDS is what di.torq.servers would report as connected. A test edits it to bring a
/ backend "up" mid-session, which is the whole point: the gateway must notice.
BACKENDS:([]w:`int$();proctype:`symbol$();procname:`symbol$();hpup:`symbol$())
setbackends:{[t] `BACKENDS set t; }

/ only the hdb is up - the state the gateway starts in
hdbonly:{[] setbackends ([]w:enlist 7i;proctype:enlist`hdb;procname:enlist`hdb;hpup:enlist`:localhost:5302); }

/ the rdb has since connected - what di.torq.servers' retry leaves behind
hdbandrdb:{[] setbackends ([]w:7 10i;proctype:`hdb`rdb;procname:`hdb`rdb1;hpup:`:localhost:5302`:localhost:5304); }

mockgetservers:{[pt] select from BACKENDS where proctype=pt}
mockservers:{[] `startup`getservers`gethandlebytype`waitfortype!({[c]};mockgetservers;{[pt;sel] 0Ni};{[pt;t;p] 1b})}

mockdeps:{[] `log`timer`handlers`servers!(mocklog[];mocktimer[];mockhandlers[];mockservers[])}

cfg:{[] (enlist`backendtypes)!enlist `rdb`hdb}

/ what the gateway can actually ROUTE to: di.serverselect's table, which di.asyncdispatch
/ dispatches on. Neither module exposes it, so the tests read it directly rather than have
/ one of them grow an accessor for their benefit (same call as di.torq.proc.idb's tests).
routable:{[] exec distinct servertype from .m.di.0serverselect.servers where active}

/ di.torq.servers fires .servers.connectcustom (from retryrows) after every successful new
/ connection, and ships it as a no-op. 1b only once the gateway has replaced it with its own.
hookinstalled:{[] @[{100h=type value x};`.servers.connectcustom;{[e] 0b}]}

/ fire the hook exactly as di.torq.servers' retryrows does - with the connected rows
fireconnectcustom:{[] (value `.servers.connectcustom) BACKENDS; }