Repository navigation
fix(storage-postgres): unlock a refused maintenance claim before closing it - #1194
Conversation
…ing it maintenance_claim takes the maintenance advisory lock on a dedicated session, finds the previous holder's row still younger than the stale window, and refuses with AlreadyExists. It closed the session without unlocking. The server frees a closed session's lock when its backend exits, which can be after close returns. Until then check_maintenance_claim, which runs in every CreateBackup and RestoreTableFromBackup request, found the lock held and answered that a claim was live. The refused path now runs pg_advisory_unlock on the session before closing it, as the release path does. A failed unlock is logged and the close still frees the lock, only later. A loop of a refused take followed at once by the check, against a row older than the check's window, read the closing session's lock as a live claim 3 times and 2 times in two runs of 2,000 rounds. With the unlock it did so 0 times in 3 runs of 2,000 rounds. Closes #1094 Signed-off-by: Hitesh Sharma <hts@amazon.com>
LeeroyHannigan
left a comment
There was a problem hiding this comment.
Thanks, the diagnosis holds. ClaimSession::close returns before the server's backend exits, and until then check_maintenance_claim reads the lock as held. I ran your loop against PostgreSQL 16, 2,000 rounds per run. Each round writes a young row, waits 20 ms, makes a refused take, then checks with a 1 ms window. Without the unlock, 70 to 84 rounds read the lock as held in each of three runs. With it, 0 in each of four runs. That's about 4 percent of rounds on this loaded host, against the 0.1 percent in the description.
Before it merges:
- Please add a test. A deterministic one takes about 2 s. In it, a second engine reaches the server through a proxy that holds each chunk from client to server for 100 ms on its way. The refused take's Terminate then reaches the server 100 ms after
closereturns. Acheck_maintenance_claimon the direct engine right after the take still finds the lock. That test failed 10 of 10 runs without the unlock and passed 50 of 50 with it.crates/storage-postgres/tests/migrations_support/proxy.rsalready has aProxythat can pause and cut a connection. Please give it a client-to-server delay, as #1181 does for MongoDB'sPausingProxy, rather than adding a second proxy. - The new unlock is a second copy of the one in
release(backup_catalog.rs:1697-1702), and its comment repeatsrelease's doc. Please put the statement in one method next toClaimSession::close, for exampleasync fn unlock(&mut self, key: &str) -> Result<(), sqlx::Error>, and call it from both places.
You could also unlock on the two error returns. The young-row read (backup_catalog.rs:1331) and the INSERT (:1365) return on an error with the lock held, and the dropped session frees it only when its backend exits. If you add the method, both returns can call it first.
catalog_index::the_maintenance_claim_excludes_a_second_holder_and_expires_when_stale passed 100 of 100 runs. --test backup_restore passed apart from 3 store_identity tests, which fail on this host because of the owner of an ancestor directory and pass in CI. #1166 (sqlx 0.9.1) leaves release's unlock as it is, so whichever of the two merges second still builds.
LeeroyHannigan
left a comment
There was a problem hiding this comment.
Thanks, both are in, and so are the unlocks on the two error returns. With the refused path closing without the unlock, the new test failed 10 of 10 runs. With the unlock it passed 50 of 50, at 4.0 to 5.6 s per run against PostgreSQL 16 on a loaded host. pg_advisory_unlock now appears only in ClaimSession::unlock. --test migrations_guards, the other user of the proxy, passed 7 of 7. --test backup_restore passed apart from the 3 store_identity tests that fail on this host, also with main at ca768ed merged in. fmt and both of CI's clippy commands are clean.
Before it merges, please fix three sentences:
- The description says that neither the young-row read nor the INSERT can be made to fail on its own. Both can fail on their own. In psql against PostgreSQL 16, one session took the maintenance lock and ran the take's two statements. A BEFORE INSERT trigger that raises made the INSERT fail after the young-row read returned no row, and
pg_advisory_unlockon that session then returned true. Withheartbeat_atrenamed, the young-row read failed on its own. So a test can install the trigger in the scratch catalog and make the take through the proxy, as the refused-take test does. Please add that test, or say that the error-path unlocks have no test. - The new commit message says "so no path leaves the lock to the close". A failed unlock still leaves it to the close, as the doc of
end_unkept_maintenance_takeand the description say. "so no path skips the unlock" would be true. The commit message goes into main's history when the PR merges. - The doc of
ClaimSession::unlocksays that a caller that ends the session runs it beforeclose.backup_catalog.rsends aClaimSessionwithclosealone at lines 916, 995, 1133 and 1199. Please limit the sentence to a caller whose lock another connection can check right after the close.
The new commit also has no Signed-off-by line, where 9b7ae17 has one.
Move the advisory unlock into ClaimSession::unlock, called by release and by end_unkept_maintenance_take. A take now unlocks before it closes its session when it is refused for a young row and when the young-row read or the INSERT fails, so no path skips the unlock. A failed unlock is logged, and the close then frees the lock once the server's backend exits. Add a test that makes the refused take through a proxy holding each chunk to the server for 100 ms, so the close's Terminate arrives after the take returns, and checks the lock is free right after the take. The proxy gains a client-to-server delay for it. Signed-off-by: Hitesh Sharma <hts@amazon.com>
… take Add tests for a take stopped by a failing INSERT (a BEFORE INSERT trigger that raises) and by a failing young-row read (heartbeat_at renamed for the take). Each makes the take through the 100 ms proxy and checks the lock is free right after it returns. Without the unlock in end_unkept_maintenance_take all three maintenance-take tests fail. Limit the doc of ClaimSession::unlock to a caller whose lock another connection can check right after the close. Signed-off-by: Hitesh Sharma <hts@amazon.com>
a2d6663 to
40d60b5
Compare
|
Thanks, all three sentences are fixed, and the error-path unlocks now have tests.
Both rewritten commits and the new one carry a Signed-off-by line. fmt and |
LeeroyHannigan
left a comment
There was a problem hiding this comment.
Thanks, all three are in. With end_unkept_maintenance_take reduced to a close, each of the three take tests failed at its lock assertion in 3 of 3 runs. With the unlock, each passed 20 of 20. Each test's catalog is its own database, so the trigger and the rename stay out of the other tests. --test backup_restore passed apart from the 3 store_identity tests that fail on this host. --test migrations_guards passed 7 of 7, including a_role_that_does_not_own_the_tables_is_told_what_to_run. The take tests also passed with main at f0170bd merged in.
The two new tests accept any error from the take. A take that fails before it holds the lock, for example on the proxy's connection, would also pass them. Matching the error they provoke, a StorageError::Internal with "insert refused" or "heartbeat_at" in it, would rule that out. The description's --test backup_restore row still says 76 of 76, and the binary has 78 tests now.
This merge brings in 13 commits of main through 44cd436. One of them, #1194, changes two files of this branch: it adds ClaimSession::unlock to crates/storage-postgres/src/backup_engine.rs, and it adds the migrations_support module and a test to crates/storage-postgres/tests/backup_restore.rs. Both files merge without a conflict.
Moves the workspace version from 1.0.0 to 1.1.0 on every surface that carries it: the root Cargo.toml version and its 13 exact pins, the 15 workspace entries in Cargo.lock (no third-party entry changes), packaging/npm/package.json (the version and the five platform pins), the four license-notice files (regenerated with cargo-about 0.9.0), and the sample `extenddb version` and `serve` output in docs/getting-started.md and docs/manuals/04-quickstart-setup-guide.md. This is a minor release. Since v1.0.0, main has two features (#468, #1009) and changes that make ExtendDB answer as DynamoDB does, which docs/versioning.md ships in a minor release. It adds no catalog migration (RELEASED_THROUGH stays 12, the last id), no configuration key and no backup format version. Upgrade manual, Behavior Changes by Release: v1.1.0 entries for #468 (AT_TIMESTAMP, and the GetShardIterator answers it changes), #503, #544, #567, #765 and #774, #588, #596, #690, #1144, #1173 and #1194, next to the existing entries for #398 and #526. The #526 entry now gives the old TransactWriteItems mismatch message, and the entries are in PR order. Current Status, the General Upgrade Procedure and the Rollback Procedure cover 1.0.0 to 1.1.0: no migrate step, and a rollback runs the 1.0.0 binary on the same catalog, with the two limits the manual names (#526 tokens, and 1.0.0's catalog-check --fix). The rollback note of the KEK-file switch says that 1.0.0 serves a switched catalog. docs/manuals/11-deployment-guide.md: a sentence about the release before the one that carries #398 is gone. Signed-off-by: Scott Robinson <robinnsc@amazon.com>
Moves the workspace version from 1.0.0 to 1.1.0 on every surface that carries it: the root Cargo.toml version and its 13 exact pins, the 15 workspace entries in Cargo.lock (no third-party entry changes), packaging/npm/package.json (the version and the five platform pins), the four license-notice files (regenerated with cargo-about 0.9.0), and the sample `extenddb version` and `serve` output in docs/getting-started.md and docs/manuals/04-quickstart-setup-guide.md. This is a minor release. Since v1.0.0, main has two features (#468, #1009) and changes that make ExtendDB answer as DynamoDB does, which docs/versioning.md ships in a minor release. It adds no catalog migration (RELEASED_THROUGH stays 12, the last id), no configuration key and no backup format version. Upgrade manual, Behavior Changes by Release: v1.1.0 entries for #468 (AT_TIMESTAMP, and the GetShardIterator answers it changes), #486, #503, #544, #567, #765 and #774, #588, #596, #690, #963, #1144, #1173, #1194 and #1375, next to the existing entries for #398, #526 and #1166. The #526 entry now gives the old TransactWriteItems mismatch message, and the entries are in PR order. Current Status, the General Upgrade Procedure and the Rollback Procedure cover 1.0.0 to 1.1.0: no migrate step, the #1166 checks before a PostgreSQL upgrade, and a rollback runs the 1.0.0 binary on the same catalog, with the two limits the manual names (#526 tokens, and 1.0.0's catalog-check --fix). The rollback note of the KEK-file switch says that 1.0.0 serves a switched catalog. docs/manuals/11-deployment-guide.md: a sentence about the release before the one that carries #398 is gone.
Moves the workspace version from 1.0.0 to 1.1.0 on every surface that carries it: the root Cargo.toml version and its 13 exact pins, the 15 workspace entries in Cargo.lock (no third-party entry changes), packaging/npm/package.json (the version and the five platform pins), the four license-notice files (regenerated with cargo-about 0.9.0), and the sample `extenddb version` and `serve` output in docs/getting-started.md and docs/manuals/04-quickstart-setup-guide.md. This is a minor release. Since v1.0.0, main has three features (#468, #1009, #1449) and changes that make ExtendDB answer as DynamoDB does, which docs/versioning.md ships in a minor release. It adds no catalog migration (RELEASED_THROUGH stays 12, the last id), no configuration key and no backup format version. Upgrade manual, Behavior Changes by Release: v1.1.0 entries for #468 (AT_TIMESTAMP, and the GetShardIterator answers it changes), #486, #503, #544, #567, #765 and #774, #588, #596, #690, #963, #1144, #1173, #1194, #1375 and #1522, next to the existing entries for #398, #526, #1166, #1449 and #1523. The #526 entry now gives the old TransactWriteItems mismatch message, the #1523 heading takes the v1.1.0: prefix, and the entries are in PR order. Current Status, the General Upgrade Procedure and the Rollback Procedure cover 1.0.0 to 1.1.0: no migrate step, the #1166 checks before a PostgreSQL upgrade, and a rollback runs the 1.0.0 binary on the same catalog, with the two limits the manual names (#526 tokens, and 1.0.0's catalog-check --fix). The rollback note of the KEK-file switch says that 1.0.0 serves a switched catalog. docs/manuals/11-deployment-guide.md: a sentence about the release before the one that carries #398 is gone.
Moves the workspace version from 1.0.0 to 1.1.0 on every surface that carries it: the root Cargo.toml version and its 13 exact pins, the 15 workspace entries in Cargo.lock (no third-party entry changes), packaging/npm/package.json (the version and the five platform pins), the four license-notice files (regenerated with cargo-about 0.9.0), and the sample `extenddb version` and `serve` output in docs/getting-started.md and docs/manuals/04-quickstart-setup-guide.md. This is a minor release. Since v1.0.0, main has three features (#468, #1009, #1449) and changes that make ExtendDB answer as DynamoDB does, which docs/versioning.md ships in a minor release. It adds no catalog migration (RELEASED_THROUGH stays 12, the last id), no configuration key and no backup format version. Upgrade manual, Behavior Changes by Release: v1.1.0 entries for #468 (AT_TIMESTAMP, and the GetShardIterator answers it changes), #486, #503, #544, #567, #765 and #774, #588, #596, #690, #963, #1144, #1173, #1194, #1375 and #1522, next to the existing entries for #398, #526, #1166, #1449 and #1523. The #526 entry now gives the old TransactWriteItems mismatch message, the #1523 heading takes the v1.1.0: prefix, and the entries are in PR order. Current Status, the General Upgrade Procedure and the Rollback Procedure cover 1.0.0 to 1.1.0: no migrate step, the #1166 checks before a PostgreSQL upgrade, and a rollback runs the 1.0.0 binary on the same catalog, with the two limits the manual names (#526 tokens, and 1.0.0's catalog-check --fix). The rollback note of the KEK-file switch says that 1.0.0 serves a switched catalog. docs/manuals/11-deployment-guide.md: a sentence about the release before the one that carries #398 is gone. docs/troubleshooting.md: the bus error entry names 1.0.0 as the release that has the bug, so its line carries a version-literal-ok marker.
* chore(release): bump version to 1.1.0 Moves the workspace version from 1.0.0 to 1.1.0 on every surface that carries it: the root Cargo.toml version and its 13 exact pins, the 15 workspace entries in Cargo.lock (no third-party entry changes), packaging/npm/package.json (the version and the five platform pins), the four license-notice files (regenerated with cargo-about 0.9.0), and the sample `extenddb version` and `serve` output in docs/getting-started.md and docs/manuals/04-quickstart-setup-guide.md. This is a minor release. Since v1.0.0, main has three features (#468, #1009, #1449) and changes that make ExtendDB answer as DynamoDB does, which docs/versioning.md ships in a minor release. It adds no catalog migration (RELEASED_THROUGH stays 12, the last id), no configuration key and no backup format version. Upgrade manual, Behavior Changes by Release: v1.1.0 entries for #468 (AT_TIMESTAMP, and the GetShardIterator answers it changes), #486, #503, #544, #567, #765 and #774, #588, #596, #690, #963, #1144, #1173, #1194, #1375 and #1522, next to the existing entries for #398, #526, #1166, #1449 and #1523. The #526 entry now gives the old TransactWriteItems mismatch message, the #1523 heading takes the v1.1.0: prefix, and the entries are in PR order. Current Status, the General Upgrade Procedure and the Rollback Procedure cover 1.0.0 to 1.1.0: no migrate step, the #1166 checks before a PostgreSQL upgrade, and a rollback runs the 1.0.0 binary on the same catalog, with the two limits the manual names (#526 tokens, and 1.0.0's catalog-check --fix). The rollback note of the KEK-file switch says that 1.0.0 serves a switched catalog. docs/manuals/11-deployment-guide.md: a sentence about the release before the one that carries #398 is gone. docs/troubleshooting.md: the bus error entry names 1.0.0 as the release that has the bug, so its line carries a version-literal-ok marker. * docs(upgrade): add the v1.1.0 entry of #1290, put #1349's in order #1290 merged after this branch was rebased. It changes the answer to a request with no Authorization header, so it gets a v1.1.0 entry. The merge of main put the #1349 entry after the #1523 entry. It moves to the place of its number, beside the new #1290 entry. --------- Co-authored-by: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com>
What
On PostgreSQL, a maintenance take that is not kept now releases the maintenance advisory lock on its claim session before it closes the session. That covers a take refused because the previous holder's row is still younger than the stale window, and a take stopped by an error in the young-row read or the INSERT. The unlock statement is now one method,
ClaimSession::unlock, called byreleaseand byend_unkept_maintenance_take.Why
maintenance_claimtakes the maintenance advisory lock on a dedicated session. When the row it then finds is still young, it refuses withAlreadyExistsand closes the session. The close did not unlock first. The server frees a closed session's lock when its backend exits, and that can happen afterclosereturns. Until it does,check_maintenance_claimfinds the lock held and answers that a claim is live. That check runs in every CreateBackup and RestoreTableFromBackup request, so those requests could see a claim that is not there in the moment after a refused maintenance command. The two error returns dropped the session with the lock held, which leaves the same window.The same window made
catalog_index::the_maintenance_claim_excludes_a_second_holder_and_expires_when_stalefail once in CI atcrates/storage-postgres/tests/backup_restore.rs(a stale claim is not held). The release path already unlocked before it closed; every path that ends a take now does the same. A failed unlock on a take that is not kept is logged, and the close still frees the lock, only later.Closes #1094
Testing done
catalog_index::a_refused_maintenance_take_leaves_no_lock_behindis new. It writes a young maintenance row, then makes the take through a second engine whose connections pass through the test proxy with a 100 ms delay on every chunk from client to server. The close's Terminate then reaches the server 100 ms after the take returns. Right after the take,check_maintenance_claimon the direct engine, with a 1 ms window so the row alone cannot answer held, must find the lock free. For it, the PostgreSQL test proxy (tests/migrations_support/proxy.rs) gainsto_server_delay_ms.Against a local PostgreSQL 17 with
EXTENDDB_TEST_PG_REQUIRED=1:the refused take's lock is still held after it returned--test backup_restore--test migrations_guards, the other user of the proxyTwo more tests cover the error-path unlocks.
a_maintenance_take_whose_insert_fails_leaves_no_lock_behindinstalls a BEFORE INSERT trigger that raises in the scratch catalog, so the INSERT fails after the young-row read finds no row.a_maintenance_take_whose_young_row_read_fails_leaves_no_lock_behindrenamesheartbeat_atfor the take, so the young-row read fails on its own, and renames it back before the check. Both make the take through the same 100 ms proxy and check the lock right after it returns. With the unlock inend_unkept_maintenance_takedisabled, all three take tests fail in 3 of 3 runs; with it, all three pass, and the sixmaintenancetests passed in 3 of 3 runs.--test migrations_guardspasses apart froma_role_that_does_not_own_the_tables_is_told_what_to_run, which fails on my host because the catalog tables are owned by my local role.All clean.
Checklist
cargo test --workspace)cargo fmt --check)cargo clippy -- -W clippy::pedantic)Storagetrait, auth model, on-diskformat, or public CLI surface, an RFC has been accepted or is linked
below. Otherwise, an ADR captures the decision (link below).
ADR / RFC: n/a
Breaking changes
None.
By submitting this pull request, I confirm that my contribution is made under
the terms of the Apache License 2.0 and I agree to the Developer Certificate of
Origin (DCO). See CONTRIBUTING.md for details.