Skip to content

Feature heartbeat module - #151

Merged
alowrydi merged 2 commits into
mainfrom
feature-heartbeat-module
Nov 4, 2026
Merged

alowrydi merged 2 commits into
mainfrom
feature-heartbeat-module

Conversation

@alowrydi

@alowrydi alowrydi commented Nov 2, 2026

Copy link
Copy Markdown
Collaborator

di.torq.heartbeat

Port of TorQ's code/common/heartbeat.q as a KDB-X module, wired into every di.torq process.

Every process publishes a heartbeat row on a timer. A process configured as a subscriber keeps the latest heartbeat per process, and flags any process whose heartbeats stop arriving in time: first a warning, then an error. Both flags clear when heartbeats resume. This is the liveness signal a monitor process will consume. The monitor itself is out of scope.

Commits

  1. di.torq.heartbeat: port TorQ's heartbeat.q: the module only (di/torq/heartbeat/): heartbeat.q, init.q, VERSION (0.1.0), deps.toml, heartbeat.md, test.csv, test.q.
  2. di.torq: wire di.torq.heartbeat into every process:
    • torq.q: new initheartbeat[config;deps], guarded so it wires once, like initclients. init calls it after the app code and before .ps.initialise.
    • settings/default.q: the flat heartbeat* keys with TorQ's defaults (table below).
    • settings/tickerplant.q, segmentedtp.q, chainedtp.q: heartbeatenabled:0b, so tickerplants publish no heartbeats, as in TorQ.
    • test.csv: one row confirming di.torq wires the module and .ps.initialise registers the heartbeat table.

Settings

Key Default Meaning
heartbeatenabled 1b publish and check heartbeats
heartbeatsubscribeenabled 0b subscribe to other processes' heartbeats
heartbeatdebug 1b log warnings and errors
heartbeatpublishinterval 0D00:00:30 how often heartbeats are published
heartbeatcheckinterval 0D00:00:10 how often received heartbeats are checked
heartbeatwarningtolerance 1.5 warn after this many publish intervals without a heartbeat
heartbeaterrortolerance 2f error after this many publish intervals without a heartbeat
heartbeatsubscribetypes `symbol$() proctypes to subscribe to; ALL for all

Optional processwarning and processerror callbacks can be passed in config. Each receives the flagged rows, and both default to no-ops.

Behaviour

  • Publishing. heartbeatenabled adds two di.timer jobs: publishheartbeat, which publishes through .ps.publish once pubsub is initialised, and checkheartbeat.
  • Subscribing. heartbeatsubscribeenabled independently turns on the subscriber side:
    • root upd is wrapped to store heartbeat rows and pass everything else through;
    • a .z.pc observer forgets closed handles;
    • a 1-minute job subscribes to any new processes of the wanted types;
    • .servers.connectcustom is wrapped so a newly connected process is subscribed immediately.
  • Connections. A subscriber also needs to connect to the types it subscribes to, via servers CONNECTIONS and STARTUP.
  • State. Received heartbeats live in .m.di.0torq.0heartbeat.hb, keyed on sym (proctype) and procname, with warning and error flags.
  • Pubsub everywhere. init loads di.pubsub, so di.torq's .ps.initialise[] now runs on every process.

Deviations from TorQ

  1. Startup code moves into init[config;deps]: module state replaces .hb, calls use bracket indexing, root names are written with @[`.;..] or set.
  2. Flat heartbeat* settings keys. .hb.CONNECTIONS becomes heartbeatsubscribetypes.
  3. processwarning and processerror come from config and default to {[x]}. The .html.pub calls are dropped.
  4. warningperiod and errorperiod can't be overridden from outside the module.
  5. .proc.cp[] becomes .z.p. .proc.proctype and .proc.procname come from config.
  6. The .timer.enabled and .ps checks are dropped. Timer jobs have ids, and the modes map 2h→3h and 0h→1h.
  7. .dotz.set becomes the handlers dep, which also registers .stpps.closesub on .z.pc.
  8. totimespan on the intervals. Forced by TOML, which has no timespan type.
  9. heartbeatsubscribetypes is converted from strings to symbols. Forced by TOML.
  10. init errors if an interval doesn't convert. The only addition that isn't forced.

Dependencies

  • Injected: log, timer, handlers; init errors if any is missing.
  • Declared in deps.toml: di.pubsub 0.4.0 and di.torq.servers 0.7.0. servers is not injected: the subscriber side calls the root .servers.getservers and wraps .servers.connectcustom, which di.torq.servers defines, and depcheck reports it if they're missing.

Testing

Suite Result
di.torq.heartbeat 70/70 (new)
di.torq 94/94
di.pubsub 133/133
di.timer 179/179

Run on the branch tip 5f8d547. The new suite covers:

  • init: required deps and settings, bad intervals, string subscribe types, and the typed default;
  • jobs and handlers: the timer jobs and handlers it registers;
  • wraps: the upd and connectcustom wraps;
  • stored heartbeats: storing heartbeats and seeding processes;
  • checks: the ok → warning → error → recovery cycle;
  • publishing: publishing heartbeats;
  • tickerplants: they have no heartbeat jobs;
  • di.timer: a run against the real di.timer;
  • two processes: a spawned publisher's heartbeat reaches a subscriber.

TorqX-POC. Booted the full stack on this branch, with permissions on:

  • Every non-tickerplant process publishes heartbeats, with both jobs active. stp1, sctp1, tickerplant1 and chainedtp1 have no heartbeat jobs.
  • No errors in any process log.
  • A housekeeping1 subscriber stored rdb1's heartbeats. Stopping rdb1 produced the warning about 7.9s and the error about 11.0s after its last heartbeat (thresholds 7.5s and 10s with a 5s publish interval; the overshoot is the check job's timer). Restarting rdb1 cleared both flags within about 30s.

The TorqX-POC feature-heartbeat branch depends on this module, so merge this PR first.

di.torq.config. It shows 68/76 on current main (489726d) and on the previous base, so it's not caused by this branch. The 8 failures are kx.log rows, and kx.log isn't installed in my KDB-X.

Comment thread di/torq/heartbeat/heartbeat.q Resolved
Comment thread di/torq/heartbeat/heartbeat.q Resolved
Comment thread di/torq/heartbeat/heartbeat.q Resolved
Comment thread di/torq/heartbeat/heartbeat.q Resolved
Comment thread di/torq/heartbeat/heartbeat.q Resolved
Comment thread di/torq/heartbeat/heartbeat.q Resolved
Comment thread di/torq/heartbeat/heartbeat.q Resolved
@DI-Software-Engineering

Copy link
Copy Markdown

DIReview Summary

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

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

Suggestions

  • di/torq/heartbeat/heartbeat.q:113 — The timer addjob calls pass the bare function names publishheartbeat and checkheartbeat (as symbols `publishheartbeat / `checkheartbeat). Whether a timer resolves these names against root or the module namespace at call time depends on the timer implementation, which is not visible here. If the timer calls the function by name lookup at root, the bare symbol will fail because publishheartbeat and checkheartbeat live in the module namespace. The functions should be passed as values (the function objects themselves, not symbols) — or stored at root explicitly before being passed.

@alowrydi
alowrydi requested a review from DI-Dexter November 2, 2026 15:46

@DI-Dexter DI-Dexter 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.

Test suites looks good, just a couple points for consideration

Requires fixing
heartbeat.q:112 registers the segmented TP's cleanup, not pubsub's.

register[`.z.pc;`;`hbpubsub;0j;.stpps.closesub];
.stpps.closesub clears .stpps.t, but heartbeats go out through .ps.publish, whose subscribers are in reqalldict, which this leaves alone. A disconnected heartbeat subscriber is never removed, in every process this PR turns into a publisher. Line 108's use di.pubsub; drops the handle, which is why pubsub's own closesub isn't reachable:

.z.m.ps:use`di.pubsub;
register[`.z.pc;`;`hbpubsub;0j;.z.m.ps`closesub];

Smaller points that aren't blockers

  • Only init / version are exported, with callers pointed at .m.di.0torq.0heartbeat.*. TorQ's apidetails.q makes six of these public and monitor.q uses two, addprocs has no other route. Worth exporting them.
  • init throws on a second call (duplicate timer job). Worth a registered guard, as di.subscriptions and di.permissions have.
  • heartbeat.md: note that processwarning / processerror default to no-ops where TorQ called .html.pub.

alowrydi and others added 2 commits November 3, 2026 10:54
Publishes heartbeats and, on a subscriber, flags processes whose heartbeats stop
arriving in time. Faithful to TorQ; changes only where the module system or TOML
forces them, plus an init error on an interval that does not convert.

Exports addprocs, storeheartbeat, warningperiod and errorperiod alongside init and
version, as TorQ's apidetails.q makes them public. A registered guard means a
second init applies the settings but adds no timer jobs, wraps or .z.pc handlers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
initheartbeat always calls init, after the app code and before .ps.initialise;
init gates publishing and subscribing itself. Settings are the flat heartbeat*
keys in di/torq/settings/default.q; tickerplant, segmentedtp and chainedtp set
heartbeatenabled:0b, as TorQ does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@alowrydi
alowrydi force-pushed the feature-heartbeat-module branch from 5f8d547 to 794697b Compare November 3, 2026 10:55
Comment thread di/torq/heartbeat/heartbeat.q Resolved
Comment thread di/torq/heartbeat/heartbeat.q Resolved
Comment thread di/torq/heartbeat/heartbeat.q Resolved
Comment thread di/torq/heartbeat/heartbeat.q Resolved
Comment thread di/torq/heartbeat/heartbeat.q Resolved
@DI-Software-Engineering

Copy link
Copy Markdown

DIReview Summary

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

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

Suggestions

  • di/torq/heartbeat/heartbeat.q:34 — addprocs writes .z.m.hb but reads the initial hb value via .z.m.hb on the right-hand side. On the very first call after init, .z.m.hb should correctly refer to the module's hb. However, the expression (2!([]sym:proctypes,();...)),.z.m.hb joins the new rows in front of the existing keyed table. Joining a plain table with a keyed table via , may not produce a correctly keyed result — the new rows should be upserted into the keyed table (e.g. .z.m.hb upsert 2!([]sym:proctypes,();...)) rather than using , with the keyed table on the right.

@alowrydi

alowrydi commented Nov 3, 2026

Copy link
Copy Markdown
Collaborator Author

Test suites looks good, just a couple points for consideration

Requires fixing heartbeat.q:112 registers the segmented TP's cleanup, not pubsub's.

register[`.z.pc;`;`hbpubsub;0j;.stpps.closesub]; .stpps.closesub clears .stpps.t, but heartbeats go out through .ps.publish, whose subscribers are in reqalldict, which this leaves alone. A disconnected heartbeat subscriber is never removed, in every process this PR turns into a publisher. Line 108's use di.pubsub; drops the handle, which is why pubsub's own closesub isn't reachable:

.z.m.ps:use`di.pubsub;
register[`.z.pc;`;`hbpubsub;0j;.z.m.ps`closesub];

Smaller points that aren't blockers

  • Only init / version are exported, with callers pointed at .m.di.0torq.0heartbeat.*. TorQ's apidetails.q makes six of these public and monitor.q uses two, addprocs has no other route. Worth exporting them.
  • init throws on a second call (duplicate timer job). Worth a registered guard, as di.subscriptions and di.permissions have.
  • heartbeat.md: note that processwarning / processerror default to no-ops where TorQ called .html.pub.

Thanks Dexter, I've pushed 794697b (tests: di.torq.heartbeat 81/81, di.torq 94/94, di.pubsub 133/133, di.timer 179/179).

.z.pc registration (heartbeat.q:112): left as .stpps.closesub. .ps.publish is .stpps.pub and .ps.subscribe is .u.sub, so heartbeat subscribers are held in .stpps.subrequestall, which .stpps.closesub clears. The exported closesub clears reqalldict, which the classic subscribe/publish use. Verified with a real publisher and a second process subscribed to heartbeat: with the current registration the handle is removed from .stpps.subrequestall[heartbeat]on disconnect, and with the exportedclosesubswapped in (scratch copy) it stays there. I've added a regression test for this, using the real di.torq.handlers so.z.pc` fires.

Smaller points, all done:

  • Exports: addprocs, storeheartbeat, warningperiod and errorperiod are now exported with init and version. processwarning and processerror aren't, because they now come from config, so exporting them would suggest they can be overridden.
  • Second-call guard: registered flag, as in di.subscriptions. A second init applies settings but adds no job, wrap or .z.pc registration. A later call that turns heartbeatsubscribeenabled on doesn't add the subscriber side, which heartbeat.md now says.
  • heartbeat.md: notes that processwarning and processerror default to no-ops where TorQ published to the html layer, and the exports section is split into exported and module-private names.

Ready for another look when you have time.

@DI-Dexter DI-Dexter 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.

Thanks Aaron, you're right on .z.pc, my mistake. I'd checked reqalldict directly, but heartbeat subscribers come in through root .ps.subscribe (.u.sub) and sit in .stpps.subrequestall, which .stpps.closesub clears. Good to have the regression test with real handlers. The other three look good, and the suite passes here too. Approving.

@alowrydi
alowrydi merged commit b603445 into main Nov 4, 2026
@alowrydi
alowrydi deleted the feature-heartbeat-module branch November 4, 2026 09:36
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