Skip to content

Worker: while the usage ledger is frozen or reconciling, run only recovery work and exit withdrawals - #546

Merged
teddashh merged 5 commits into
mainfrom
w5-recovery-worker-mode
Nov 21, 2026
Merged

teddashh merged 5 commits into
mainfrom
w5-recovery-worker-mode

Conversation

@teddashh

Copy link
Copy Markdown
Contributor

After a database restore, the operator freezes the usage ledger before control and the worker start, and the ledger stays frozen or reconciling until recovery completes. In that time the worker should do only work that makes the restored state safe or converges it. Before this change, Dispatcher.Once claimed every due outbox event whatever the ledger's mode, so device discovery, fleet updates, provisioning, policy evaluation and exit publication were dispatched on restored data. Separately, exits.ExecuteOperation never compared a publication operation with the exit's current intent. An operation that ran after a newer command, for example a retry after backoff, could publish a route that had since been withdrawn, or withdraw one that had since been published.

What changes.

  • The claim reads the ledger's mode (internal/db/outbox.go, apps/worker/worker.go). db.ClaimOutboxByLedgerMode is the candidate query of ClaimOutbox with one more condition: usage_ledger_state.mode is normal, or the event's type is in the allowed list, or the type is node.publication_changed and the operation that the payload's operation_id names has a withdrawal kind. Dispatcher.Once now claims through it. A missing ledger row counts as restricted. ClaimOutbox itself is unchanged.
  • Waiting events are left alone. The condition is part of the candidate query's WHERE, so an event that must wait is not locked, not leased and not counted: its attempt does not rise and it is not dead-lettered for waiting. Older waiting events do not hold up an allowed one. When the mode is normal again, the next claim takes the oldest due event, as before.
  • No lock on the ledger or on operations. The mode and the operation's kind are read in sub-selects without a row lock, and FOR UPDATE SKIP LOCKED still applies to outbox_events only. A claim does not wait for a transaction that holds the ledger row or an operation row.
  • The lists (apps/worker/recovery_mode.go).
    • Allowed while the ledger is not normal: member.entitlement_changed, member.grant_revoked, member.suspended, device.authorized, device.revoked, netbird.device_sync, members.reconcile, billing.reconcile, billing.inbox_received, node.staleness.sweep, node.generation_hold.check, node.peer.observe, node.resolver_documents.reconcile, node.netbird_routes.reconcile, dns.publish and dns.feed.refresh.
    • Withdrawal kinds for node.publication_changed: node.unpublish, node.disable, node.revoke and node.retire. A publish waits.
    • Listed as waiting: devices.discover, node.reconcile, fleet.update_created, fleet.first_sync_created, fleet.update_retry, fleet.release_support_changed, node.updated, node.provision.requested, node.peer.discover, release.verify, policy.activated, fup.evaluate, fup.member_generation_changed, usage.projection_rebuild and usage.retention_purge. A type that is in no list waits as well.
  • Malformed publication payloads. The operation_id is cast to uuid only when it is a JSON string that pg_input_is_valid accepts. That function needs PostgreSQL 16 or later; CI and the deployment image run 17. While the mode is not normal, an event without an id, with an id that is not a string or not a UUID, or with an id that names no operation waits. It cannot make the claim fail.
  • The members schedule (apps/worker/jobs_members.go). scheduleMemberWork enqueues a waiting type only when the mode is normal. Of the three types it schedules, that is devices.discover: its row id changes with every interval, so the rows would otherwise pile up during a long recovery.
  • Publication intent guard (internal/exits/exits.go). ExecuteOperation already holds the per-node publication lock and has re-read the operation. It now supersedes a node.publish operation when the exit's publication_intent is not published, and a node.unpublish or node.disable operation when it is published. Nothing is sent to NetBird for a superseded operation, and the worker's handler already treats superseded as finished. The status trigger lets failed change only to running, so a failed operation that contradicts the intent is moved to running and then superseded in the same transaction. node.revoke and node.retire always run, a partially_succeeded operation is kept, and a node without an exit row is not affected. The guard applies in every mode.

Tests (integration, against PostgreSQL, unless marked):

  • TestRecoveryModeT1_ClaimByMode (apps/worker): one due event for every registered type, for three unregistered types, and one node.publication_changed event for each of the five operation kinds. In normal every event is claimed. In frozen, in reconciling and without a ledger row, exactly the allowed types and the four withdrawal events are claimed; the test carries its own expected list and does not read the worker's. Two more subtests hold FOR UPDATE on the ledger row and on the operation row and require the claim to return the withdrawal event within two seconds, one in reconciling and one in normal.
  • TestRecoveryModeT2_WaitingRowsUntouched: in reconciling, with an event of every waiting type (one of them with an expired lease and two attempts), three unregistered types, a publish event, an event with an invalid operation id and a dead-lettered row, twenty dispatch rounds do no work and every outbox_events row is unchanged, compared as JSON. Back in normal, the first claim is the oldest event, with attempt 1.
  • TestRecoveryModeT3_MalformedPublicationPayloads: with a withdrawal operation present, events with no id, a string that is not a UUID, a number and an unknown UUID are not claimed and do not change, and the claim returns no error.
  • TestRecoveryModeT4_NoHeadOfLineBlocking: fifty older waiting events do not keep the dispatcher from an allowed one.
  • TestRecoveryModeT6_MemberSchedule: in frozen, in reconciling and without a ledger row, one schedule cycle enqueues billing.reconcile and members.reconcile and no devices.discover; in normal it enqueues all three.
  • TestRecoveryModeT7_RegistrationCoverage (unit): every type in the production registration is in exactly one of the lists.
  • TestRecoveryModeT5_PublicationIntentGuard (internal/exits): an obsolete publish after a newer unpublish, and an obsolete unpublish or disable after a newer publish, end superseded with no call to the adapter and no change to the observed route. The same holds for an obsolete operation that had failed once. A partially_succeeded withdrawal stays as it is, revoke and retire run although the intent is published, a current publish runs, and an operation of a node without an exit is not superseded.
  • TestRecoveryModeT8_RevocationWithdrawal (internal/nodes/httpwire): a published exit, then a freeze and the start of recovery through the usage service, then a revocation command. The production dispatcher works until its claim is empty; the revoke operation ends succeeded or partially_succeeded, the route is observed as withdrawn, and the mode is still reconciling. It runs with and without a node observation after the freeze.

Deliberate breakages, each reverted afterwards. On the first three commits:

  • the claim treats every mode as normal: the three restricted cases of TestRecoveryModeT1_ClaimByMode and TestRecoveryModeT2_WaitingRowsUntouched failed;
  • every node.publication_changed event is allowed whatever its operation's kind: the same three cases and TestRecoveryModeT3_MalformedPublicationPayloads failed;
  • the operation_id cast without its check: TestRecoveryModeT3_MalformedPublicationPayloads failed with SQLSTATE 22P02;
  • the intent guard removed: publish_then_unpublish, unpublished_then_publish and disabled_then_publish of TestRecoveryModeT5_PublicationIntentGuard failed;
  • the schedule enqueues devices.discover in every mode: the reconciling and missing cases of TestRecoveryModeT6_MemberSchedule failed (the frozen case did not exist yet);
  • members.reconcile removed from the allowed list: TestRecoveryModeT7_RegistrationCoverage and the three restricted cases of TestRecoveryModeT1_ClaimByMode failed.

On the final commit:

  • FOR SHARE on the ledger read of the claim: only does_not_lock_ledger_in_normal_mode failed, at its two-second deadline;
  • FOR SHARE on the operation read: only does_not_lock_ledger_or_operation failed, at its deadline;
  • the schedule read treats frozen as normal: only the frozen case of TestRecoveryModeT6_MemberSchedule failed;
  • the supersede statement without its status condition: only partially_succeeded_withdrawal_is_kept failed, with invalid operation transition from the status trigger;
  • the statement that moves failed to running removed: only failed_publish_then_unpublish and failed_unpublish_then_publish failed.

Validation: on the exact source befaf969:

  • the change's shape: exactly the 9 owned paths in 5 commits, scanned, with clean whitespace;
  • go vet of internal/db, internal/exits, internal/nodes/httpwire and apps/worker, plain and with the integration tag, for linux/amd64 and linux/arm64;
  • by name, with the integration tag and against PostgreSQL, each passed three times, none skipped: the eight TestRecoveryMode* tests (six in apps/worker, one in internal/exits, one in internal/nodes/httpwire), and of TestRecoveryModeT1_ClaimByMode the subtests does_not_lock_ledger_or_operation, does_not_lock_ledger_in_normal_mode and missing_ledger_state; TestRecoveryModeT7_RegistrationCoverage also once without the integration tag;
  • the lint step of CI's database job: gofmt, the down-migration lint test, vet and staticcheck;
  • the tests of CI's database job against PostgreSQL (2 packages ok, no test failed or skipped), run without the job's sweep of old test databases; in that run Test15_1_SchemaGuards, Test15_1_Uint64Bounds, Test15_5_Migrations, Test15_5_MigrationsRoundTrip, Test15_5_MigrationDownLint and Test14_1_OpenScansTimestamptzUTC each passed exactly once;
  • CI's nodes job against PostgreSQL: lint, unit tests, integration, build (12 packages ok);
  • CI's pki job against PostgreSQL: verify (lint, unit tests, build) and integration (26 packages ok);
  • CI's control job against PostgreSQL: verify (drift, lint, unit tests, build) and integration (18 packages ok, none from the test cache);
  • none of the 638 test databases the runs named was left behind;
  • CI's billing job against PostgreSQL: verify (lint, unit tests, integration, build; 18 packages ok; the composition test passed once) and the arm64 build of every package;
  • CI's policies job against PostgreSQL: verify (lint with the integration tag, build, unit tests) and integration (4 packages ok);
  • CI's usage job against PostgreSQL: verify (lint, build, unit and integration tests; 7 packages ok);
  • CI's fleet update job against PostgreSQL: lint, unit tests, build, and its integration packages (3 packages ok) except internal/db, which the database job above ran;
  • the DNS steps that need no Docker: lint, build, unit tests and the publish integration (12 packages ok);
  • none of the 505 test databases the runs named was left behind;
  • CI's Go job: go-lint, go test ./... (95 packages ok; 2 of them came from Go's test cache (internal/fleetupdate and internal/releases: their tests had passed a few minutes earlier in the same tree, in the fleet update job's unit test step above; no changed package was cached)), the build of every command for amd64 and arm64, and the tidy check;
  • CI's Node job: lint, unit tests (10 packages ok), integration against PostgreSQL (2 packages ok; skipped: TestD096_RealNetBird), build;
  • CI's updater integration job against PostgreSQL (3 packages ok; none skipped);
  • the pull-request jobs of at-members, at-usage and at-secops (lint, test, build; the at-secops component suite ran 47 tests) and the lint jobs of at-enrollment, at-updates and e2e (e2e's e2e-config step not run);
  • CI's Portal job: lockfile install, typecheck, lint, format check, unit tests with coverage (367 tests passed), and the live Playwright suite against fvpn-control and fvpn-worker (12 tests passed, none failed, flaky or skipped);
  • none of the 9 test databases the runs named was left behind.

Then the same checks on a temporary merge of this source into main 2d04cffc, which merged without conflicts:

  • go vet of internal/db, internal/exits, internal/nodes/httpwire and apps/worker, plain and with the integration tag, for linux/amd64 and linux/arm64;
  • by name, with the integration tag and against PostgreSQL, each passed three times, none skipped: the eight TestRecoveryMode* tests (six in apps/worker, one in internal/exits, one in internal/nodes/httpwire), and of TestRecoveryModeT1_ClaimByMode the subtests does_not_lock_ledger_or_operation, does_not_lock_ledger_in_normal_mode and missing_ledger_state; TestRecoveryModeT7_RegistrationCoverage also once without the integration tag;
  • the lint step of CI's database job: gofmt, the down-migration lint test, vet and staticcheck;
  • the tests of CI's database job against PostgreSQL (2 packages ok, no test failed or skipped), run without the job's sweep of old test databases; in that run Test15_1_SchemaGuards, Test15_1_Uint64Bounds, Test15_5_Migrations, Test15_5_MigrationsRoundTrip, Test15_5_MigrationDownLint and Test14_1_OpenScansTimestamptzUTC each passed exactly once;
  • CI's nodes job against PostgreSQL: lint, unit tests, integration, build (12 packages ok);
  • CI's pki job against PostgreSQL: verify (lint, unit tests, build) and integration (26 packages ok);
  • CI's control job against PostgreSQL: verify (drift, lint, unit tests, build) and integration (18 packages ok, none from the test cache);
  • none of the 649 test databases the runs named was left behind;
  • CI's billing job against PostgreSQL: verify (lint, unit tests, integration, build; 18 packages ok; the composition test passed once) and the arm64 build of every package;
  • CI's policies job against PostgreSQL: verify (lint with the integration tag, build, unit tests) and integration (4 packages ok);
  • CI's usage job against PostgreSQL: verify (lint, build, unit and integration tests; 7 packages ok);
  • CI's fleet update job against PostgreSQL: lint, unit tests, build, and its integration packages (3 packages ok) except internal/db, which the database job above ran;
  • the DNS steps that need no Docker: lint, build, unit tests and the publish integration (12 packages ok);
  • none of the 508 test databases the runs named was left behind;
  • CI's Go job: go-lint, go test ./... (96 packages ok; 2 of them came from Go's test cache (internal/fleetupdate and internal/releases: their tests passed earlier on the same merge tree in the fleet update job's unit test step; no changed package was cached)), the build of every command for amd64 and arm64, and the tidy check;
  • CI's Node job: lint, unit tests (10 packages ok), integration against PostgreSQL (2 packages ok; skipped: TestD096_RealNetBird), build;
  • CI's updater integration job against PostgreSQL (3 packages ok; none skipped);
  • the pull-request jobs of at-members, at-usage and at-secops (lint, test, build; the at-secops component suite ran 47 tests) and the lint jobs of at-enrollment, at-updates and e2e (e2e's e2e-config step not run);
  • CI's Portal job: lockfile install, typecheck, lint, format check, unit tests with coverage (367 tests passed), and the live Playwright suite against fvpn-control and fvpn-worker (12 tests passed, none failed, flaky or skipped);
  • none of the 9 test databases the runs named was left behind.

These are local runs; no CI result is claimed here.

Review: An independent review of the first three commits asked for changes. Two points were about tests: the lock subtest ran only in reconciling, where the ledger read returns no row, so it could not see a row lock on that read, and the schedule test had no frozen case. It also noted that the guard skipped an operation left failed. The worker's handler moves such an operation back to running while attempts remain, but a worker that stops between the two writes leaves it failed for the next delivery. The guard's status condition was untested as well. The last two commits add the normal lock subtest and the frozen case, supersede a contradicting failed operation, and test the status condition. The review's last point is left for a later change: an allowed handler whose compile waits for a node observation after the freeze can use up its retry budget during recovery, as it can before this change.

A second review, of the last two commits, found those points resolved and rated one interleaving a blocker: an intent change that commits between the guard's two statements leaves a failed operation unsuperseded, and it is executed. The project lead's analysis was that this ends in the same state as an intent change that commits just after the guard, which a check made before the NetBird call cannot exclude. The command that changes the intent also creates the operation for the new intent, and that operation runs afterwards under the same per-node lock, so the last operation executed for the node matches the intent. Because the reviewer had rated it a blocker, a third model was asked to adjudicate from the code without seeing that analysis. It reached the same conclusion, and no code changed for it. The second review also suggested a subtest for a failed operation whose intent still matches; that was not required.

Agent: codex/gpt-6.1-sol

Prepared with Codex; implementation and review attribution are recorded above.

@teddashh
teddashh merged commit 7f43ab4 into main Nov 21, 2026
49 checks passed
@teddashh
teddashh deleted the w5-recovery-worker-mode branch November 21, 2026 05:05
teddashh added a commit that referenced this pull request Nov 21, 2026
Co-Authored-By: Codex <noreply@openai.com>
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.

1 participant