Repository navigation
gateway: an offline mark is written only while the record names the stream it ends - #262
Conversation
…n a newer stream A stream ends on one replica and its agent redials through another, whose stream reaches SYNCED before the first replica's offline mark is written. Nothing orders the two replicas' writes, so the test holds the mark until the newer stream's state is written and releases it then: the workload must still read SYNCED and a directed reconnect must be accepted and delivered. A second test ends a stream with no newer one and expects the mark written and the workload offline. The first test fails on this commit: the offline mark is unconditional (issue #109). The fix follows. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Claude <noreply@anthropic.com>
|
No ADR contradictions found (full-corpus audit — the diff touches Scope and what was checkedThe PR fixes a cross-replica race (#109) where a sync stream's offline mark, written by the replica that served it after the stream had gone, could land after a newer stream of the same workload had already registered PENDING/SYNCED elsewhere (another replica, or the incoming process of a deploy). The fix adds Checked against:
The ADR-0018 amendment itselfThe amendment to
Document consistency
Full-corpus checkRead every ADR in |
… it ends A sync stream's offline mark is written by the replica that served it, after the stream has gone. An agent directed to reconnect redials at once, and its next stream can open on another replica, or on the incoming process of a deploy while the outgoing one writes the marks of the streams it ended. Nothing ordered the two processes' writes, so the old stream's mark could land after the new stream had recorded PENDING and SYNCED: the workload read offline while its stream was live, and a directed reconnect was refused, until the next acknowledgement. A replica now gives each stream it registers a random id. The stream's open is recorded as PENDING with that id in one statement (RecordWorkloadStreamOpen, migration 00019), directly after the stream registers and while the registry holds the workload, the hold #107 took for the offline mark, so on one replica the record names streams in the order they registered. The offline mark (MarkWorkloadStreamOffline) is written only while the record still names the ending stream, or names none, as a row does that no stream has opened since the migration; when it names another, the mark writes nothing and the gateway logs that a newer stream has opened, at info level. The id says which stream registered last, never whether one is alive, and nothing outside the gateway reads it. The writes a live stream makes (pushed deltas, acknowledgements, heartbeats, inventory, the snapshot stamp) run under the stream's own context and are unchanged. The shutdown path writes its marks through the same exit path, and the control plane writes no sync state at start. ADR-0018 takes a dated amendment and decision 3 is corrected in place; ARCHITECTURE.md, the troubleshooting entry, and the changelog follow. TestPresentedCredential now migrates to the latest schema after enrolling, since the store's queries read the new column. Fixes #109. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Claude <noreply@anthropic.com>
Keeps both Unreleased changelog entries. Signed-off-by: Claude <noreply@anthropic.com>
The race
A sync stream's offline mark is written by the replica that served the stream, after the stream has gone. #107 made the registry hold the workload while that mark is written, so a newer stream of the same workload on the same replica registers only after it. Nothing orders one replica's writes against another's:
RequestReconnect(Resend snapshot) refuses it, until the next APPLIED ack, which on a quiet workload can be a long time away.The same happens with one replica whenever two control-plane processes overlap: during a deploy, the outgoing process's
GracefulStopends its streams and writes their offline marks after the agents may already be on the incoming process.The fix: the offline mark names the stream it ends
uuid.UUID(uuid.New()inServer.Sync), kept on the in-processsessionbeside the other per-stream state. It is not a timestamp, because two replicas' clocks are not ordered.RecordWorkloadStreamOpen(new,internal/store/queries/workloads.sql) writes the stream's open, sync state PENDING, andsync_stream_idin one statement, so a stream registering replaces the token atomically. Nothing else writes the column. This is the PENDING write a stream already made at open; it moves to directly aftersessions.addand runs while the registry holds the workload, the same hold gateway: log every lost reconnect directive; never mark a live stream offline #107 introduced for the offline mark, now also taken for the open record. On one replica, then, the record names streams in the order they registered, and a second stream of the workload registers only once the first one's open is recorded.RecordAgent.RecordAgentruns at Hello, before the stream is registered. Two streams of one workload on one replica (an agent's deploy turnover) can reachRecordAgentin one order and the registry in the other, or a stream can record its Hello and then fail to register at all. A token written there would then name a stream that is not the registered one, its eventual mark would be skipped, and a workload with no stream would never read offline. Writing the token where the registered stream records its open rules that out.MarkWorkloadStreamOffline(new) writes OFFLINE onlyWHERE sync_stream_id IS NULL OR sync_stream_id = <the ending stream's id>. When the record names another stream, a newer one has registered since, on this replica or another, and the mark writes nothing:registry.Store.MarkStreamOfflinereportsfalsewith no error, and the gateway logsoffline mark not written: a newer sync stream of the workload has opened since this oneonce at info level, after the write returned. An unknown workload is stillErrWorkloadUnknown.sync_stream_id. A NULL names no stream, so nothing newer is known, and the mark is written exactly as before. Because every stream records its open, and its token, before it can end, a stream served by this release only meets a NULL when its own open write failed (logged asrecording stream open).RequestReconnectdo not see it; the refusal still mirrors the stored sync state. A replica remembers only the ids of the streams it serves (ADR-0017).Every writer examined
internal/gateway/sync.goSync, aftersessions.addRecordStreamOpen: PENDING plus the stream's id, in one write, under the registry hold. FormerlySetSyncState(PENDING)after the snapshot was queued.Sync's deferred cleanupMarkStreamOffline, conditioned on the id; a skipped mark is logged at info.cmd/innerwall/serve.gocmd/innerwall/serve.goworkload_policiesonly. There is no start-up reconciliation that marks workloads offline, so a rolling deploy does not reset the state of streams served elsewhere.Server.Run/reconcilepushLatest, below.RecordWorkloadFlowReport(ingest)last_flow_report_atand stored gaps, never the sync state.RetireWorkloadretired_at; it ends the stream, whose exit path is the conditioned mark.pushLatesthandleAck->RecordApplied,RecordApplyFailedRecordHeartbeat,RecordFacts,RecordListeningServices,RecordAgentrecordSnapshotSentWhy the live-stream writes are left as they are: each runs under its stream's own context, so it can land after a newer stream opened elsewhere only if it was already in flight when its stream ended, a window of one statement. The offline mark was the one write that runs after the stream has gone, by design, and so lands late routinely. Conditioning the ack writes also means telling a skipped write apart from a retired workload in
RecordApplied/RecordApplyFailed, which is more than this change needs. The residual windows, and the snapshot-sent stamp (written after its stream may have ended, by design), are recorded in a follow-up issue.Migration
00019_sync_stream_id.sql:ALTER TABLE workloads ADD COLUMN sync_stream_id uuid, nullable, existing rows NULL; the down migration drops it. No applied migration changes.storetest.Reset's table list is unchanged (a column, not a table), soTestResetTablesAreTheMigratedSchemais unaffected.TestPresentedCredentialread the store at migration 00018 after enrolling at 00017; the store's queries are compiled against the latest schema, which now has a column 00018 lacks, so the test now migrates to the latest after enrolling, which still covers a row written before 00018.The outgoing process of the deploy that brings this release runs the old code, so its marks are still unconditional during that one deploy.
Tests
internal/store/sync_stream_test.goTestOfflineMarkNamesItsStream: a row written before 00019 (NULL id) takes the mark as before; a stream open records PENDING and its id; after a newer stream opens and is acked SYNCED, the older stream's mark writes nothing, reportsfalse, and leaves SYNCED andlast_seen_atas they were; the newer stream's mark is written; an unknown workload isErrWorkloadUnknownfor both writes.internal/gateway/cross_replica_integration_test.go:TestOfflineMarkFromAnotherReplicaNeverOverwritesNewerStream: two gateways over one database. The stream opens and is acked on A; it ends, and A's mark is held at the registry; the same identity opens on B and reaches PENDING, then SYNCED; A's mark is released. The mark reports not written, the workload reads SYNCED, andRequestReconnectthrough the fleet domain is accepted and its directive reaches B's stream.TestOfflineMarkWithNoNewerStream: A's stream ends with no newer stream; the mark reports written, the workload reads OFFLINE, andRequestReconnectis refused as offline.internal/gateway/dispatch_test.goTestNewerStreamWaitsForStreamOpenRecord: a stream that has just registered holds its workload until its open is recorded; a second stream registers only after that, and supersedes it.TestOfflineMarkNeverOverwritesNewerStreamandTestNewerStreamWaitsForOfflineMarkkeep their assertions; the first one's delaying store now wrapsMarkStreamOffline, which is where the offline mark is written.The cross-replica test was pushed first, on its own, against the unconditional mark, and CI failed on it; the fix followed in the next commit.
Docs
No change to the proto, the wire contract, the agent, the operator API, or the workload read.
Verified by the project's CI only; nothing was built or run locally.
Fixes #109.
Generated by Claude Code