Repository navigation
Exits: reconcile NetBird route drift in bounded pages, on the worker's schedule - #543
Merged
Merged
Conversation
Agent: grok/grok-4.7
Agent: grok/grok-4.7
Agent: grok/grok-4.7
Agent: grok/grok-4.7
Agent: grok/grok-4.7
Agent: grok/grok-4.7
Agent: grok/grok-4.7
Agent: grok/grok-4.7
Agent: grok/grok-4.7
Agent: grok/grok-4.7
Agent: grok/grok-4.7
Agent: grok/grok-4.7
Agent: grok/grok-4.7
teddashh
added a commit
that referenced
this pull request
Nov 15, 2026
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
ReconcileNetBirdcompares 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 wasexits.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 wholedriftarray with its own elements, which erased the elements of other writers (the restore observation hold, for one), and it raised the node'sresource_versionon every run, changed or not.What changes.
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 methodReadExitPageonce per deployment, so one read serves all of that deployment's exits in the page. A deployment the adapter does not serve returnsErrDeploymentNotServedbefore any request, and its exits are skipped. Any other error from a page read stops the run, and nothing of that page is written.PublicationEvidenceErrorbecomes the drift elementnetbird_route_evidencewith the classified cause. The exit's binding getssync_state = 'error'and the cause inlast_error, and the run goes on to the next exit.UPDATErebuildsnodes.driftfrom 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_versionrises only when the array changes, so a run that finds nothing new leaves the node's ETag as it was.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 changesexits.publication_intent.internal/nodes/httpwire/publication_adapter.go).ReadExitPagetakes oneReadObservedStatesnapshot and matches each exit with the functionReadExitStateuses. In that function, apaid:group binding key that is not a UUID is now the per-exit evidencegroup_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 staysroute_peer_mismatch.apps/worker/jobs_nodes.go). The new jobnode.netbird_routes.reconcile, registered with a pool peak of one connection, runs the reconcile and then enqueues its successor one reconcile period later (the configuredReconcileSeconds, 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 unregisteredexits.Worker, is removed.Tests (integration, against PostgreSQL, unless noted):
internal/exits/reconcile_integration_test.go):TestReconcileNetBird_KeepsForeignDrift: two foreign elements stay first and unchanged, and the route element follows them.TestReconcileNetBird_ETagStableWithoutChangeandTestReconcileNetBird_BumpsVersionOnceOnChange: a second run changes neither the version nor the drift,SetPublicationwith 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 getserrorand 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_PagesOncePerDeploymentandTestReconcileNetBird_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.internal/exits/reconcile_lock_integration_test.go,internal/exits/reconcile_guard_integration_test.go), each with a second transaction and bounded waits:TestReconcileNetBird_ConcurrentDriftAppendandTestReconcileNetBird_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 realFreezeIngest. 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_SkipsDeploymentChangedDuringNodeLockandTestReconcileNetBird_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_EvidenceSkipsBindingRepublishedDuringWriteandTestReconcileNetBird_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.internal/nodes/httpwire):TestReadExitPage_MatchesReadExitState: an unserved deployment makes no request, a page makes exactly the requests of oneReadObservedState, and each result equalsReadExitStatefor 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.apps/worker):TestNetBirdRoutesReconcile_ScheduleCoalescesAndReconciles. Two schedule cycles leave one undelivered event. The job writes thenetbird_routedrift, 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.TestFakeNetBirdAdapter_ReadExitPage.TestDisabledNode_NeverAutoReEnabledByHealthOrUpdatenow callsReconcileNetBirddirectly, 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:
nodes, without the node locks:TestReconcileNetBird_ConcurrentDriftAppendlost the appended element andTestReconcileNetBird_ConcurrentDriftRemovalbrought the removed one back;TestReconcileNetBird_LockOrderWithFreezefailed, because the reconcile waited for the freeze;TestReconcileNetBird_SkipsStaleIntentfailed;paid:key:TestReconcileNetBird_MalformedPaidBindingfailed, because the whole page read failed;TestReconcileNetBird_GroupsExitsByDeploymentfailed with five calls instead of four;TestReconcileNetBird_SkipsIntentChangedDuringNodeLockfailed with the intent from before the wait;TestReconcileNetBird_SkipsBindingChangedDuringWritefailed both times;last_errorcondition: only thelast_errorsubtest ofTestReconcileNetBird_SkipsBindingValueChangedDuringWritefailed;TestReconcileNetBird_SkipsDeploymentChangedDuringNodeLockfailed;TestReconcileNetBird_SkipsBindingCreatedDuringNodeLockfailed;TestReconcileNetBird_EvidenceSkipsBindingRepublishedDuringWritefailed;TestReconcileNetBird_EvidenceSkipsBindingValueChangedDuringWritefailed.Validation: on the exact source
a2e73684:go vetofinternal/exits,internal/nodes/httpwireandapps/worker, plain and with the integration tag, for linux/amd64 and linux/arm64;TestReconcileNetBird_*tests ofinternal/exits(the 20 new ones andTestReconcileNetBird_DetectsDrift), each of the five subtests ofTestReconcileNetBird_EvidenceSkipsBindingValueChangedDuringWrite,TestReconcileNetBird_MalformedPaidBindingandTestReadExitPage_MatchesReadExitState(internal/nodes/httpwire), andTestNetBirdRoutesReconcile_ScheduleCoalescesAndReconciles(apps/worker); the unit testTestFakeNetBirdAdapter_ReadExitPageonce;internal/fleetupdateagainst PostgreSQL (1 package ok); the other packages of CI's fleet update job depend on none of the changed packages;go-lint,go test ./...(95 packages ok, none cached), the build of every command for amd64 and arm64, and the tidy check;TestD096_RealNetBird), build;e2e-configstep not run);fvpn-controlandfvpn-worker(12 tests passed, none failed, flaky or skipped);Then the same checks on a temporary merge of this source into main
e7120c38, which merged without conflicts:go vetofinternal/exits,internal/nodes/httpwireandapps/worker, plain and with the integration tag, for linux/amd64 and linux/arm64;TestReconcileNetBird_*tests ofinternal/exits(the 20 new ones andTestReconcileNetBird_DetectsDrift), each of the five subtests ofTestReconcileNetBird_EvidenceSkipsBindingValueChangedDuringWrite,TestReconcileNetBird_MalformedPaidBindingandTestReadExitPage_MatchesReadExitState(internal/nodes/httpwire), andTestNetBirdRoutesReconcile_ScheduleCoalescesAndReconciles(apps/worker); the unit testTestFakeNetBirdAdapter_ReadExitPageonce;internal/fleetupdateagainst PostgreSQL (1 package ok); the other packages of CI's fleet update job depend on none of the changed packages;go-lint,go test ./...(96 packages ok, none cached), the build of every command for amd64 and arm64, and the tidy check;TestD096_RealNetBird), build;e2e-configstep not run);fvpn-controlandfvpn-worker(12 tests passed, none failed, flaky or skipped);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 anifthat 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