Skip to content

Exits: reconcile NetBird route drift in bounded pages, on the worker's schedule - #543

Merged
teddashh merged 13 commits into
mainfrom
w5-netbird-recon
Nov 15, 2026
Merged

teddashh merged 13 commits into
mainfrom
w5-netbird-recon

Conversation

@teddashh

Copy link
Copy Markdown
Contributor

ReconcileNetBird compares each exit's publication intent with the route NetBird really has, and records the difference as drift on the node and on the exit's binding. Before this change nothing ran it: its only caller was exits.Worker, which no binary registers. It was also not fit to run against a fleet. It read every exit in one query and asked the adapter once per exit, and each of those calls reads the whole NetBird state. It stopped at the first exit whose evidence could not be read. It replaced the node's whole drift array with its own elements, which erased the elements of other writers (the restore observation hold, for one), and it raised the node's resource_version on every run, changed or not.

What changes.

  • Bounded pages, one NetBird read per deployment (internal/exits/reconcile.go, internal/exits/exits.go). The reconcile walks the exits in pages of 200, ordered by exit id. It groups each page by deployment and calls the new adapter method ReadExitPage once per deployment, so one read serves all of that deployment's exits in the page. A deployment the adapter does not serve returns ErrDeploymentNotServed before any request, and its exits are skipped. Any other error from a page read stops the run, and nothing of that page is written.
  • One exit's unreadable evidence no longer stops the run. A per-exit PublicationEvidenceError becomes the drift element netbird_route_evidence with the classified cause. The exit's binding gets sync_state = 'error' and the cause in last_error, and the run goes on to the next exit.
  • The node's drift is merged, not replaced. One UPDATE rebuilds nodes.drift from the row it updates: the elements of other writers stay, in their order, and only the three route fields (netbird_route, netbird_route_config, netbird_route_evidence) are replaced. resource_version rises only when the array changes, so a run that finds nothing new leaves the node's ETag as it was.
  • Each page is written in one transaction, in the fleet's lock order. After the fleet lock, the transaction locks the page's nodes in node id order with one single-table statement. Freeze and recovery lock nodes in that order too. Only then does a second statement read publication intent, the deployment and every binding value again, so that this read sees everything committed before the locks were granted. An exit whose values differ from the page read is skipped in this run. Publishers write bindings without a node lock, so both binding updates repeat every binding value of the page read in their WHERE; when such an update matches no row, the exit's drift is not written either, and the next run takes the exit up again. The reconcile never changes exits.publication_intent.
  • The adapter (internal/nodes/httpwire/publication_adapter.go). ReadExitPage takes one ReadObservedState snapshot and matches each exit with the function ReadExitState uses. In that function, a paid: group binding key that is not a UUID is now the per-exit evidence group_evidence_unavailable (it was a raw parse error). A database failure in the peer or group lookup now fails the read (it was recorded as that exit's evidence); a missing peer row stays route_peer_mismatch.
  • The worker runs it (apps/worker/jobs_nodes.go). The new job node.netbird_routes.reconcile, registered with a pool peak of one connection, runs the reconcile and then enqueues its successor one reconcile period later (the configured ReconcileSeconds, at most 60 s). The node schedule seeds it in the same fleet-locked transaction as the other three scheduled node events. internal/exits/worker.go, the unregistered exits.Worker, is removed.

Tests (integration, against PostgreSQL, unless noted):

  • Behaviour (internal/exits/reconcile_integration_test.go):
    • TestReconcileNetBird_KeepsForeignDrift: two foreign elements stay first and unchanged, and the route element follows them.
    • TestReconcileNetBird_ETagStableWithoutChange and TestReconcileNetBird_BumpsVersionOnceOnChange: a second run changes neither the version nor the drift, SetPublication with the ETag read before that run still succeeds, and a change moves the version once.
    • TestReconcileNetBird_EvidenceErrorContinues: the exit with the evidence error gets the evidence element, and its binding gets error and the cause while its observed hash and verification time stay. A decoy binding of the same exit in another deployment is untouched, and the other exit is reconciled.
    • TestReconcileNetBird_PageErrorWritesNothing: a failed page read returns an error and leaves versions and drift as they were.
    • TestReconcileNetBird_PagesOncePerDeployment and TestReconcileNetBird_GroupsExitsByDeployment, with page sizes of 2 and 3: every exit is reconciled once, and each adapter call carries one deployment's exits of one page, in order.
    • TestReconcileNetBird_SkipsUnservedDeployment: no read and no write for the unserved deployment, and its binding row is unchanged in every column.
  • Concurrency (internal/exits/reconcile_lock_integration_test.go, internal/exits/reconcile_guard_integration_test.go), each with a second transaction and bounded waits:
    • TestReconcileNetBird_ConcurrentDriftAppend and TestReconcileNetBird_ConcurrentDriftRemoval: another transaction appends or removes a foreign drift element and holds the node row. The reconcile is seen waiting for it. After its commit, the appended element is still there (or the removed one stays removed) next to the route drift.
    • TestReconcileNetBird_LockOrderWithFreeze: two nodes whose exit order is the reverse of their node order, a held binding row, and the real FreezeIngest. The reconcile never waits for the freeze, both finish without error, and the database's deadlock counter does not move.
    • TestReconcileNetBird_SkipsStaleIntent, TestReconcileNetBird_SkipsIntentChangedDuringNodeLock, TestReconcileNetBird_SkipsDeploymentChangedDuringNodeLock and TestReconcileNetBird_SkipsBindingCreatedDuringNodeLock: the intent, the deployment or the binding changes after the page read, committed either before the write transaction or while it waits for the node lock. The exit is skipped: it is not in the results, its version is unchanged and it gets no route element.
    • TestReconcileNetBird_SkipsBindingChangedDuringWrite, TestReconcileNetBird_SkipsBindingValueChangedDuringWrite (one subtest per binding value, and a re-publish), TestReconcileNetBird_SkipsBindingRepublishedBeforeWrite, TestReconcileNetBird_EvidenceSkipsBindingRepublishedDuringWrite and TestReconcileNetBird_EvidenceSkipsBindingValueChangedDuringWrite (one subtest per value): a publisher changes the binding while the reconcile's binding update waits, or just before the write transaction. The reconcile writes neither the binding nor the drift, and the binding row is what the publisher left.
  • Adapter (internal/nodes/httpwire):
    • TestReadExitPage_MatchesReadExitState: an unserved deployment makes no request, a page makes exactly the requests of one ReadObservedState, and each result equals ReadExitState for that exit, for a present, an absent and an ambiguous route.
    • TestReconcileNetBird_MalformedPaidBinding: the malformed key is per-exit evidence in both read paths, and a reconcile through the real adapter records it and still reconciles the other exit.
  • Worker (apps/worker): TestNetBirdRoutesReconcile_ScheduleCoalescesAndReconciles. Two schedule cycles leave one undelivered event. The job writes the netbird_route drift, leaves the intent alone, publishes and withdraws nothing, returns its receipt and enqueues one successor. The job also has its case in the pool budget test, and the composition test counts it.
  • Unit: TestFakeNetBirdAdapter_ReadExitPage. TestDisabledNode_NeverAutoReEnabledByHealthOrUpdate now calls ReconcileNetBird directly, because the worker type it used is removed.

Deliberate breakages. Each was run on the branch as it stood when its test was written, and reverted afterwards:

  • the earlier drift merge, which read the array through a second scan of nodes, without the node locks: TestReconcileNetBird_ConcurrentDriftAppend lost the appended element and TestReconcileNetBird_ConcurrentDriftRemoval brought the removed one back;
  • no node locks and no node id order: TestReconcileNetBird_LockOrderWithFreeze failed, because the reconcile waited for the freeze;
  • the locks kept, but changed exits not skipped: TestReconcileNetBird_SkipsStaleIntent failed;
  • the raw parse error for a malformed paid: key: TestReconcileNetBird_MalformedPaidBinding failed, because the whole page read failed;
  • one adapter call per exit: TestReconcileNetBird_GroupsExitsByDeployment failed with five calls instead of four;
  • locking the nodes and reading the values in one joined statement: TestReconcileNetBird_SkipsIntentChangedDuringNodeLock failed with the intent from before the wait;
  • the binding updates without their conditions, and then the drift written before the binding update: TestReconcileNetBird_SkipsBindingChangedDuringWrite failed both times;
  • the binding update without its last_error condition: only the last_error subtest of TestReconcileNetBird_SkipsBindingValueChangedDuringWrite failed;
  • the re-check without the deployment comparison: TestReconcileNetBird_SkipsDeploymentChangedDuringNodeLock failed;
  • the re-check without the binding value comparisons: TestReconcileNetBird_SkipsBindingCreatedDuringNodeLock failed;
  • the evidence update without any binding value condition: TestReconcileNetBird_EvidenceSkipsBindingRepublishedDuringWrite failed;
  • the evidence update with one condition replaced at a time: each time, only the subtest of that value in TestReconcileNetBird_EvidenceSkipsBindingValueChangedDuringWrite failed.

Validation: on the exact source a2e73684:

  • the change's shape: exactly the 15 owned paths in 13 commits, scanned, with clean whitespace;
  • go vet of internal/exits, internal/nodes/httpwire and apps/worker, plain and with the integration tag, for linux/amd64 and linux/arm64;
  • by name against PostgreSQL, each passed three times, none skipped: the 21 TestReconcileNetBird_* tests of internal/exits (the 20 new ones and TestReconcileNetBird_DetectsDrift), each of the five subtests of TestReconcileNetBird_EvidenceSkipsBindingValueChangedDuringWrite, TestReconcileNetBird_MalformedPaidBinding and TestReadExitPage_MatchesReadExitState (internal/nodes/httpwire), and TestNetBirdRoutesReconcile_ScheduleCoalescesAndReconciles (apps/worker); the unit test TestFakeNetBirdAdapter_ReadExitPage 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 616 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);
  • the integration tests of internal/fleetupdate against PostgreSQL (1 package ok); the other packages of CI's fleet update job depend on none of the changed packages;
  • the DNS steps that need no Docker: lint, build, unit tests and the publish integration (12 packages ok);
  • none of the 495 test databases the runs named was left behind;
  • CI's Go job: go-lint, go test ./... (95 packages ok, none 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;
  • the at-members and at-usage pull-request jobs (lint, test, build) and e2e's pull-request lint job (its 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 2 test databases the runs named was left behind.

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

  • go vet of internal/exits, internal/nodes/httpwire and apps/worker, plain and with the integration tag, for linux/amd64 and linux/arm64;
  • by name against PostgreSQL, each passed three times, none skipped: the 21 TestReconcileNetBird_* tests of internal/exits (the 20 new ones and TestReconcileNetBird_DetectsDrift), each of the five subtests of TestReconcileNetBird_EvidenceSkipsBindingValueChangedDuringWrite, TestReconcileNetBird_MalformedPaidBinding and TestReadExitPage_MatchesReadExitState (internal/nodes/httpwire), and TestNetBirdRoutesReconcile_ScheduleCoalescesAndReconciles (apps/worker); the unit test TestFakeNetBirdAdapter_ReadExitPage 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 619 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);
  • the integration tests of internal/fleetupdate against PostgreSQL (1 package ok); the other packages of CI's fleet update job depend on none of the changed packages;
  • the DNS steps that need no Docker: lint, build, unit tests and the publish integration (12 packages ok);
  • none of the 495 test databases the runs named was left behind;
  • CI's Go job: go-lint, go test ./... (96 packages ok, none 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;
  • the at-members and at-usage pull-request jobs (lint, test, build) and e2e's pull-request lint job (its 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 2 test databases the runs named was left behind.

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

Review: The first review, of the first three commits, found three major problems. The drift merge could lose or bring back another writer's element. A malformed paid: binding key stopped the whole page. The paging test could not tell whether exits were grouped by deployment. The same review suspected the lock order against a freeze and the use of page values that had changed since they were read; both were confirmed by reading the code. Three commits answered that: node locks in id order with a re-check under them, the merge from the updated row, per-exit evidence for the malformed key, and the grouping test. Checking those commits, the project lead found two ways a change could still slip past the re-check: a locking statement with a join does not read the joined rows again after it waits, and the binding write had no guard. Two more commits read the values in a second statement after the locks and guard the binding write. A second review, of the whole source, was stopped before its report, because the reviewing session grew too large. Its notes were checked against the code and held one blocker and two untested conditions: a publisher that writes the binding again with the same external id and desired hash could be overwritten with an older NetBird read, and neither the evidence update's guard nor the re-check's deployment and binding comparisons had a test. Three commits made both binding updates and the re-check compare every binding value, with a test for each case. One of those tests first reached its state by turning off a trigger; it was rewritten to use the statement the worker itself runs. A third review, of those three commits, approved the production code and found one minor gap: the evidence update's conditions were tested only together. A separate run of deliberate breakages on the same commit confirmed that gap. One commit added a subtest per value, and a fourth review approved it with no findings. The last commit is a one-line simplification that the lint step of the local validation asked for (return !errors.As(...) in place of an if that returned constants); a fifth review compared both forms for every kind of input and approved it with no findings.

Agent: grok/grok-4.7

🤖 Generated with Claude Code

@teddashh
teddashh merged commit 60ec9f9 into main Nov 15, 2026
45 checks passed
@teddashh
teddashh deleted the w5-netbird-recon branch November 15, 2026 04:11
teddashh added a commit that referenced this pull request Nov 15, 2026
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.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