Repository navigation
fix(durable): key_scope review findings, and the engine bugs behind CI's flaky fuzz - #54
Merged
Merged
Conversation
…scope - A run in a scope's line that comes for the key found the run before it by start time, read before the key's lock and on the node's own clock. When that disagreed with the line, two runs waited behind each other for good. It now follows runs_after_id (Keys.ahead/2). - A run being rolled back keeps its key by the scope it was started under, not its workflow's now, and starts wait for its undo by that scope too (without_key/2, Keys.undoing?/2). - The demo app gets the key_scope migration. - :cancel_existing giving its run in line the newer input is recorded as cancel_existing, and the Key view says "Gave it the newer input". - list_keys returns holder_workflow_module; the dashboard tells another module's workflow of the same name apart. - The dashboard doesn't block Rerun when another workflow's run holds the key (the rerun waits in line), only when its own workflow has a run waiting; a cancelled run's blocked Rerun now says why. - The guide says a run from before a scope change stays in the scope it was started under.
Under four suites at once the retried lane's 30 ms ran out before its step ran, and the run failed again. The deadline is still bounded by the retry; a lane given twice its time still fails the test.
Ecto's cast took any whitespace-only key ("\t", " ") for nil: a
keyed start went on with no key and raised comparing it (since 2.1.0),
and a schedule saved it as none, so its fires overlapped. Starts and
schedules now share one key check (WorkflowExecution.cast_key/2): a
string of 1 to 255 bytes, kept as given, no NUL or invalid UTF-8. A
schedule key a start would refuse is refused when saved, not on every
fire. The fuzz's fixed strings now include " " and "\t".
A run's key_scope went through Ecto's default cast too, so a workflow naming key_scope: " " stored none and held its key beside the scope's other workflows. It's kept as given, like a key. Durable.Testing's started-run helpers raised on key: nil; it now matches runs started without a key. A schedule's key: "" used to be saved as no key; since a1f4155 it's refused, as a start's is (@schedule key: "" logs a failed register).
…nder load SlowSaga's 50 ms ran out during :reserve when the file ran alone on a loaded machine, which left nothing to undo, so the run failed rather than compensated; the retry's 40..60 window needed the retry itself to take under 10 ms. The deadline is bounded by clock reads around the retry now, and both tests still fail if the deadline stops counting.
A step that fails for good (:fail, :rollback, a return no step may give, its last attempt) closes its row failed before the run is written failed. A node stopped in between (grace 0, or lost) handed the run back still running, and the next claim counted the failed row as one attempt of several and ran the step again: a declined card was charged twice. give_up/2 now writes a __gave_up__ marker naming the row, in one transaction with the row's close, and the next claim fails the run with that row's error (StepRunner.gave_up?/3). A marker from before a Durable.retry/2 names none of the attempts that count. The restart fuzz's coverage check counted restarts that found a worker, which is timing (CI boots slower, so more restarts land before the first claim). It now asserts a restart cut an attempt off (a row closed node_stopped), which the old check didn't catch when nothing was.
…n't strand it A cancel's heartbeat kills the step's task and closes what the run has open. A task killed mid-write of its attempt's row (an INSERT on its way through PgBouncer, or already committing) landed the row after that close, and it stayed running until the stale sweep; killed between the start event and the write, the attempt had a start and no end. The row and the start event are now written in one transaction that holds the run's row FOR SHARE while it's still the claim's (StepRunner.begin_attempt/4): a cancel that lands first finds nothing begun and the step never starts, one that lands after finds the row to close and sends its end. Since 90f99d6. :start telemetry handlers now run inside that transaction.
A crash op only lands with a second node, and second_node? was drawn apart from the ops: a seed whose crashes all fell in one-node cases (287534) failed "crash_node never landed" every time. A case with a crash op starts the second node until a crash has landed, so as many cases as before still run on one node.
A key is checked for text Postgres can't hold (NUL, invalid UTF-8); a scope wasn't, so key_scope: "a\0b" reached the key's lock and failed with a raw Postgrex error, and in start_many failed every start of the batch, not only its own. Keys.scope/1 refuses it as any other invalid key_scope.
…e written, isn't run again; :start goes out after the attempt's row From an independent review of 0ce2f39 and 73d9f10: - A step returning {:cancel, _} closed its row cancelled before the run was cancelled. A node stopped in between handed the run back running, and the next claim found no attempt used and ran the step again (a refund refunded twice, a cancelled run completed). It now writes the same marker as a step that gave up, with its scope on the row, and the next claim cancels the same run (StepRunner.ended_for_good/3). - A wait that couldn't be written closed its row failed in a statement of its own: a node stop before the run failed gave the step another attempt. The row's close and the marker are one transaction now. - 73d9f10 sent [:durable, :step, :start] inside the transaction that writes the attempt's row. A handler querying through the same repo ran in it, and a statement the database refused aborted it: the run failed with a MatchError and the step never ran. The row is still written only under the claim, FOR SHARE; the start event goes out after the commit, and a slow handler no longer holds up a cancel.
…ransient error entering a wait sends the run back From a second review round on the engine changes: - 38ec2e6 sent [:durable, :step, :start] after the commit. Postgres commits once the COMMIT reaches it, so a task killed while its COMMIT was on the wire (a node stop at grace 0, a crash, a lost reply) left a row whose closer sent an end with no start. The start goes out inside the transaction again, after the insert. A :start handler whose statement the database refuses aborts it: the row is written again then, without the event, and with the claim gone the attempt sends its own end. - A wait the database gave up on writing (a deadlock, a timeout, a dropped connection) failed the run, and with the marker for good. It says nothing about the step (rule 32): the wait's row is closed as a lost worker's and the run goes back on its queue.
…e the row running Executor.attempt_died/3 closed the run's open attempts before touching the run. A task killed with its begin_attempt COMMIT already sent landed the row after that close; the next attempt's close_left_open would have closed it, but a run that failed at the step boundary (its deadline) never began one, and the row stayed running. attempt_died first waits on the run's row (FOR NO KEY UPDATE), so a commit in flight lands before anything is closed, the order release_runs and a cancel already take.
…re batches The coverage gates (undo_charge reached, every operator landed, enough runs completed) failed on their own when one 25-case batch missed a rare path: main's CI failed "never reached undo_charge", 7 of 54 local iterations failed one. A gate unmet after a batch runs another (seed derived from the ExUnit seed, so --seed replays every batch), at most four in all, and only then flunks. Coverage adds up across batches. A broken path still fails: with lane retries or rollbacks always refused, "after N batches: retry_lane / rollback never landed".
…wait, not 600 ms Through PgBouncer under load the run hadn't reached its wait 600 ms in. It's awaited now, then checked to stay waiting with its body run once: a run that took the data key for the event never waits, and still fails.
…nded before it acts From a third review round, with a proxy that loses one COMMIT or only its reply: - wait_given_up/3 closed the waiting attempt and put the run back with the run as it was in memory. When the wait's COMMIT had landed and only the reply was lost, the run was really waiting: its attempt got a false worker_lost (three in a row fail the run), and once the wait was answered and another node took the run, that node's running attempt was closed and the run stuck until the stale sweep. It now takes the run's row FOR UPDATE first and acts only on a run still running under its claim. - A connection dropped after :start went out and before the COMMIT left a start with no end though nothing was killed. begin_attempt now waits out that transaction on the run's row and sends the end itself when the row never landed; one that landed is closed, with its end, by whoever finds it.
…ts COMMIT, sends its end
From a fourth review round:
- A lost connection put the run back from inside the step's task: it
closed the attempt, requeued the run (dropping the claim) and only then
sent the attempt's end. A heartbeat in between found the claim gone and
killed the task, and the end never went out (CI's flaky "one connection
dying while a finished step's result is written"; older than this
branch). The task now hands {:connection_lost, error} back and the
worker puts the run back itself, its heartbeat stopped, as it does for a
task that died.
- With :start sent before the COMMIT, a COMMIT the database refused for
good (a deferred check or trigger) left a start with no end: the row
isn't in and begin_attempt is outside the span. It sends the :exception
itself.
… claimed, and has time to Two gaps left runs running under a stopped node's id until the stale sweep (the restart fuzz's "never finished", seeds 917020 and 918543; the same in 2.1.1): - A poller busy in a slow query answered the drain after it had timed out, unpaused, and claimed the runs just handed back; their workers died with the node. The Drainer now kills the pollers before the workers: a claim killed on its way is cancelled, one that landed is running and the hand-back takes it. - The Drainer had a second past the grace period for the hand-back; a late poller used half and a busy database the rest, and its supervisor killed it with nothing handed back. It has 5 s now. The production guide and the shutdown_grace_period docs say to leave a platform the few seconds more. The restart fuzz's coverage gate counts stops whose grace ran out with steps still running (the drain stop event), not rows closed node_stopped, which went to 0 on a loaded machine when stops found runs between steps.
…ll answers its poller 2623a84 moved the put-back into the worker's own handle_info, so the worker couldn't answer Worker.adopt/1: a poller restarted meanwhile waited the 5 s call timeout in init for each such worker (a blip that drops connections hits them all at once), then left it out of its count. The put-back is back in the task, which first asks its worker to stop its heartbeat (:stop_heartbeat): a heartbeat under way answers first, and none after can kill the task before it has sent the ends of the attempts it closed. The restart fuzz gate's message says what it counts: stops whose grace ran out with work still claimed.
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.
Follow-up to #53 (#52). Started as #53's review findings. CI's fuzz then failed on this PR and on main, so independent audit and review agents went through each failure. Each engine fix has a test that fails without it.
key_scope review findings (10270b2)
The line no longer deadlocks:
Keys.ahead/2followsruns_after_id. A rollback keeps its key by the stored scope.cancel_existingis recorded behind another workflow. Rerun under a scope,holder_workflow_module, the demo migration, and guide fixes.Engine bugs behind the flaky fuzz
"\t"for nil. Blank keys and scopes are now kept as given. A schedule key is checked as a start's is, and a key or scope Postgres can't store is refused (a1f4155, 48ca91c, a92b2ce).__gave_up__marker naming the attempt is written with the attempt's close, and the next claim fails or cancels from it (0ce2f39, 38ec2e6).FOR SHARE.:startgoes out before the COMMIT. A dropped or refused COMMIT ends its own attempt.attempt_diedwaits out an in-flight commit. A lost connection's put-back sends the ends before the heartbeat can kill the task (73d9f10, 3ae6b90, 64e62f1, 39a2266, 2623a84, eaa6650).Test-only
Timeout tests get room under load. Chaos fuzz coverage gates get up to four batches before failing. A crash case always has a node to crash. The restart gate counts what it means. The event-name test waits for the wait (2412fc4, 30977ef/84c3bed, f98a3a0, 2d8b90d).
Behaviour changes
@schedule key: ""is refused instead of meaning no key.[:durable, :step, :start]handlers run inside the attempt's transaction. A slow one holds up a cancel of that run.CI is green on eaa6650: fast, integration and fuzz suites, direct and through PgBouncer.