rollback: arm lock for the router pair (one merge armed both) #165

Merged
ginjiruu merged 1 commit from rollback-peer-lock into master 2026-11-20 01:15:29 +00:00
Owner

Fixes the race seen on #163: one merge applied rb5009-a and rb5009-b at the same moment, and both routers armed their rollback fuse at 09:20:01. The rule is never both at once.

Cause

arm.sh checked the peer's schedulers, then spent seconds to a minute on the probe wait and the backup before creating its own scheduler. Both runners started together, both read an unarmed peer, and both armed.

Fix (modules/rollback/scripts/arm.sh)

  • Arm lock. Before the peer check, each runner creates /system/script r74-arm-lock on the pair's lower-sorting device, which is rb5009-a for the pair and r1 in the sim. RouterOS refuses a second script with the same name, so the create is an atomic test-and-set. The holder keeps the lock until its own scheduler exists, so the next holder's peer check sees it. A runner that finds the lock taken polls every 3 s, for up to R74_LOCK_WAIT (900 s).
  • Wait instead of fail. Under the lock, an armed peer is waited out for up to R74_PEER_WAIT (900 s). Its early confirm commits about 30 s after its apply, and its judge runs at N. Only then does the arm refuse. One merge therefore serializes the pair: the second router arms after the first commits, and neither apply fails.
  • Stale locks. The comment holds <owner> <epoch>, refreshed every 10 s while the holder waits. A lock with no refresh for R74_LOCK_TTL (120 s) belonged to a crashed runner and is broken with a warning.
  • An unreachable lock device only warns, the same as an unreachable peer, so a broken rb5009-a can still be fixed from rb5009-b.
  • The name has no rollback- prefix, because arm's own leftover cleanup deletes those.
  • New credential use: rb5009-b's runner already holds rb5009-a's password (ROLLBACK_PEER_PASSWORD). It now also creates and deletes the lock script there, which the terraform group allows.

A tofu-controller dependsOn between the two Terraform objects would not close this. Both objects see the new revision together, and the dependency still reads Ready from the previous revision.

Sim results, 2026-11-19

sim/rollback/run.sh on CHR 7.25.1, N=180 s.

Scenario Result
E, A, A2, B, C, F PASS, unchanged behaviour (run /tmp/opencode/r74-lock-all.txt)
D peer guard PASS. r2 now waits; with R74_PEER_WAIT=20 it refuses while r1 stays armed. After r1's judge, r2 applies
G dead recursor PASS after a fix to the scenario: a prep apply adds the dns probe first. A probe votes from the apply after the one that adds it, because the early arm carries the previous probe set. Same as live on #163
P both at once (new) PASS. Both applies start together. r1 takes the lock, arms and commits early. r2 waits 9 s for the lock, then 20 s for r1's confirm, then arms and commits. 45 one-second samples, never both armed, both rc 0, no lock left
K stale lock (new) PASS. A lock with a 10-minute-old heartbeat is broken with a warning, and the arm and confirm go through
S no secrets PASS, 0 hits in state, show -json and the saved plan

One run was invalid. I paused the sandbox for about 2 h 40 min during P. On resume the CHR clocks caught up and pending judges fired mid-scenario. Even so, the lock broke the stale holder correctly. P, K and S were rerun on fresh routers and passed. P's sampler now counts a router that doesn't answer for a second as unarmed instead of aborting the run.

shellcheck passes on arm.sh and run.sh.

Order

Merge this before #164: it changes all three router roots and would race otherwise. This PR changes modules/rollback, so its own merge re-arms all three roots, and the new script already runs in that apply.

Not proven

The production runner image (the sim ran the host toolset, not RUNNER=1), and HTTPS: the lock calls use the same --cacert path as the peer check.

Fixes the race seen on #163: one merge applied rb5009-a and rb5009-b at the same moment, and both routers armed their rollback fuse at 09:20:01. The rule is never both at once. ## Cause `arm.sh` checked the peer's schedulers, then spent seconds to a minute on the probe wait and the backup before creating its own scheduler. Both runners started together, both read an unarmed peer, and both armed. ## Fix (`modules/rollback/scripts/arm.sh`) - **Arm lock.** Before the peer check, each runner creates `/system/script` `r74-arm-lock` on the pair's lower-sorting device, which is rb5009-a for the pair and r1 in the sim. RouterOS refuses a second script with the same name, so the create is an atomic test-and-set. The holder keeps the lock until its own scheduler exists, so the next holder's peer check sees it. A runner that finds the lock taken polls every 3 s, for up to `R74_LOCK_WAIT` (900 s). - **Wait instead of fail.** Under the lock, an armed peer is waited out for up to `R74_PEER_WAIT` (900 s). Its early confirm commits about 30 s after its apply, and its judge runs at N. Only then does the arm refuse. One merge therefore serializes the pair: the second router arms after the first commits, and neither apply fails. - **Stale locks.** The comment holds `<owner> <epoch>`, refreshed every 10 s while the holder waits. A lock with no refresh for `R74_LOCK_TTL` (120 s) belonged to a crashed runner and is broken with a warning. - An unreachable lock device only warns, the same as an unreachable peer, so a broken rb5009-a can still be fixed from rb5009-b. - The name has no `rollback-` prefix, because arm's own leftover cleanup deletes those. - New credential use: rb5009-b's runner already holds rb5009-a's password (`ROLLBACK_PEER_PASSWORD`). It now also creates and deletes the lock script there, which the terraform group allows. A tofu-controller `dependsOn` between the two Terraform objects would not close this. Both objects see the new revision together, and the dependency still reads Ready from the previous revision. ## Sim results, 2026-11-19 `sim/rollback/run.sh` on CHR 7.25.1, N=180 s. | Scenario | Result | |---|---| | E, A, A2, B, C, F | PASS, unchanged behaviour (run `/tmp/opencode/r74-lock-all.txt`) | | D peer guard | PASS. r2 now waits; with `R74_PEER_WAIT=20` it refuses while r1 stays armed. After r1's judge, r2 applies | | G dead recursor | PASS after a fix to the scenario: a prep apply adds the dns probe first. A probe votes from the apply after the one that adds it, because the early arm carries the previous probe set. Same as live on #163 | | **P both at once (new)** | PASS. Both applies start together. r1 takes the lock, arms and commits early. r2 waits 9 s for the lock, then 20 s for r1's confirm, then arms and commits. 45 one-second samples, never both armed, both rc 0, no lock left | | **K stale lock (new)** | PASS. A lock with a 10-minute-old heartbeat is broken with a warning, and the arm and confirm go through | | S no secrets | PASS, 0 hits in state, show -json and the saved plan | One run was invalid. I paused the sandbox for about 2 h 40 min during P. On resume the CHR clocks caught up and pending judges fired mid-scenario. Even so, the lock broke the stale holder correctly. P, K and S were rerun on fresh routers and passed. P's sampler now counts a router that doesn't answer for a second as unarmed instead of aborting the run. shellcheck passes on `arm.sh` and `run.sh`. ## Order Merge this before #164: it changes all three router roots and would race otherwise. This PR changes `modules/rollback`, so its own merge re-arms all three roots, and the new script already runs in that apply. ## Not proven The production runner image (the sim ran the host toolset, not `RUNNER=1`), and HTTPS: the lock calls use the same `--cacert` path as the peer check.
arm.sh checked the peer, then spent seconds on probes and the backup
before creating its scheduler, so one merge armed both routers (#163,
09:20:01). Both runners now take /system/script r74-arm-lock on the
pair's lower-sorting device (atomic: RouterOS refuses a duplicate name),
hold it until their scheduler exists, and wait out an armed peer
(R74_PEER_WAIT) instead of failing. Heartbeat + TTL break a crashed
holder's lock. Sim: new scenarios P (both at once) and K (stale lock);
D waits then refuses; G adds its probe in a prep apply.
ginjiruu merged commit 456bf5491f into master 2026-11-20 01:15:29 +00:00
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
lab/iac!165
No description provided.