Skip to content

fix(streams): resolve the shard against its stream inside the GetRecords read - #543

Merged
LeeroyHannigan merged 8 commits into
mainfrom
fix/438-get-records-round-trips
Oct 29, 2026
Merged

LeeroyHannigan merged 8 commits into
mainfrom
fix/438-get-records-round-trips

Conversation

@LeeroyHannigan

@LeeroyHannigan LeeroyHannigan commented Oct 25, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Since #419, GetRecords called validate_shard before get_stream_records so an iterator could not pair an allowed stream ARN with another stream's shard. That added two catalog queries to every poll (#438), and stream consumers poll idle shards in a loop.The stream not-found sentences are set per operation; see "Stream not-found sentences" below.

Storage statements per GetRecords call, counted from each backend's statement log (SQLite sqlx debug log, PostgreSQL log_statement = 'all', MongoDB profiler level 2, Cassandra full query log), idle shard and shard with records:

Backend main this branch
SQLite 4 1 (one stream_shards JOIN tables LEFT JOIN stream_records)
PostgreSQL 5 (2 catalog, 3 data) 2 (1 catalog, 1 data; the two can be different databases)
MongoDB 5 2 (1 catalog find, 1 data aggregate)
Cassandra 4 sequential 3 statements in 2 round trips (catalog tables, then the shard row and the records issued together)

Cassandra idle GetRecords over loopback, 200 calls alternating binaries in the same minute: p50 2.11 to 2.14 ms on main, 1.55 ms on this branch; p99 3.74 to 5.59 against 2.33 to 2.52. SQLite and MongoDB p50 fell 0.15 ms and 1 ms with p99 not worse.

The ownership predicate

Account and table name from the iterator's stream ARN select the table (SQLite: in the join; PostgreSQL: SELECT table_id, stream_label FROM tables WHERE account_id = $1 AND table_name = $2; MongoDB: find_one on _id; Cassandra: a primary-key read on (account_id), table_name, where main filtered on the label with ALLOW FILTERING). The data read keys the shard on table_id. Whether the shard belongs to the stream the ARN's label names is decided in one Rust function, extenddb_storage::util::shard_in_stream(table_id, shard_id, table_label, arn_label), today table_label == Some(arn_label); a table with no stream owns no shard. A shard of another table, another account, or an earlier label on the same table answers the stream not-found sentence validate_shard gave. Zero rows from the data read means the shard is not that table's; one row without a record is the idle shard; the refusal no longer depends on the row count (the shard id is selected alongside the records).

The predicate is pinned at storage level on every backend, each test asserting both directions so a lookup that dropped the name or the account cannot pass by returning whichever table row comes first: SQLite (stream_tests.rs, 7), PostgreSQL (tests/stream_shard_ownership.rs, 3), Cassandra (tests/direct/stream_ownership.rs, 3), MongoDB (stream_records_live_tests.rs, 4). Dropping the account, the table name or the label from the predicate fails the named test on each backend.

Limit bounds

GetRecords now refuses Limit under 1 with 1 validation error detected: Value at 'limit' failed to satisfy constraint: Member must have value greater than or equal to 1 and over 1000 with One or more parameter values were invalid: Limit should be less than or equal to 1000, both measured on the service with SDK parameter validation off (main let Limit: 0 reach storage, where it answered a 500 on MongoDB, and clamped 1001 to 1000). The check runs in the engine before any storage call; the constraint(member, rule) helper is written in the same shape and place as #515's so the two merge.

MongoDB page size

An earlier form of this change on MongoDB put the page in a $lookup array on the shard document, which made a whole page one BSON document under the 16 MB cap: 1000 records of about 16 KB (an 8 KB item under NEW_AND_OLD_IMAGES, the default Limit) answered 500 on every call and the consumer could not advance. The aggregation now follows the $lookup with $unwind and $project, which MongoDB coalesces so the array is never materialized and each record is its own document again, bounded by Limit. Pinned by a test that reads 40 records of about 450 KB from one shard (inserted in descending order so the $sort is what the assertion sees); without the $unwind it fails with BSONObjectTooLarge. The MongoDB storage tests run in the mongodb-rust CI job against the job's container (devtools/run-mongodb-tests).

Streams after a disable and re-enable

Measured on the service (GetShardIterator on S1, disable, re-enable, write, GetRecords with the S1 iterator): the re-enable mints a new stream ARN; the old stream is DISABLED with every shard closed; an iterator issued before the disable reads its own records to the end (NextShardIterator null) and none of the writes after the re-enable; GetShardIterator on the old ARN still answers 200. Today no backend clears stream_label on disable and the re-enable keeps it, so an old iterator keeps reading the one stream and LatestStreamArn does not change; PR #515 changes that lifecycle.The re-enable test runs one state; see "Re-enable test" below.

Tests

  • tests/python/test_stream_shard_ownership.py (3): an iterator naming an allowed stream with another stream's shard reads nothing; the disable and re-enable case above; Limit 0 and 1001 refused with the sentences above (raw client). On this branch's release binaries: 3 passed on SQLite (dev mode), PostgreSQL, MongoDB and Cassandra, with test_streams.py 15, tests/test_authz_resource_arn.py 43 and tests/test_stream_table_reuse.py 4 (65 per backend).
  • Unit: engine 180 (get_records_limit_bounds_carry_the_measured_sentences), storage 128 (3 on shard_in_stream), SQLite 148 (7 in stream_tests), MongoDB 122 plus 4 live, Cassandra direct stream_ownership 3 and streams:: 14.
  • cargo fmt, clippy 1.97.0 with -D warnings across the workspace, 1.88.0 build: clean. No dependency change.rs1024 to 1030 andcrates/storage-cassandra/tests/direct/streams.

Merge notes

Not in this PR

  • No backend clears stream_label when UpdateTable disables the stream, and the re-enable keeps it when shards exist (SQLite update_table.rs, PostgreSQL update_table.rs, MongoDB table_engine.rs), so LatestStreamArn does not change across a disable and re-enable where the service mints a new stream. That is Disabling a stream does not close its shards or change its status #482, fixed by fix(streams): disabling a stream closes its shards; enabling makes a new one #515.
    GetShardIterator on a shard that is not the named stream's keeps the text main had, Requested resource not found: Stream: <arn> not found.; the service answers Shard does not exist (measured 2026-10-24), and that difference is outside this PR.
  • In a dev-mode build a token whose ARN does not parse answers Invalid stream ARN format: <arn> where a non-dev build answers Invalid ShardIterator from the authorization layer; unchanged by this branch.
  • extenddb version reports the commit of the first build in a target directory (crates/bin/build.rs is not rerun on a worktree HEAD change).

Closes #438

PostgreSQL read bound

The PostgreSQL statement that reads a page of records joined the shard row to its records and applied ORDER BY r.sequence_number LIMIT n after the join. The outer side of the join is one row, but the planner does not use that to treat the join output as ordered by the record's sequence number. It read every record of the shard after the sequence bound, sorted them, and applied the LIMIT to the sorted rows. A call cost time in proportion to the unread backlog of the shard instead of the page size, and held a connection of the data pool for that time.

The records now come from a LEFT JOIN LATERAL subquery that carries its own ORDER BY sequence_number LIMIT n. The planner reads the primary key (shard_id, sequence_number) in order and stops after n rows. The outer query keeps the result shape: an idle shard of the named table returns one row with a NULL record, and a shard of another table returns no row. The statement text is in crates/storage-postgres/src/stream_records_sql.rs, which the engine and the plan test both use.

The measurements ran on PostgreSQL 16.15 in a container, with the shipped schema and one shard of 100,000 records of about 1 KB in a table of 299,000 stream records. The statements were run through psql with the text copied from the code, not through the server binary. The plan rows are EXPLAIN (ANALYZE) runs, each repeated once, and the second run is quoted. The drain row is three runs of a loop inside one database session, so it leaves out network and pool acquisition time.

Case Join form in the first commit of this PR LATERAL form
First page of 1000 102.7 ms, 22,580 buffers, 100,000 records fetched 0.56 ms, 158 buffers, 1000 records fetched
Page of 1000 after sequence 50000 53.0 ms, 50,000 records fetched 0.85 ms
Generic plan, first page 81.0 ms 1.49 ms
Generic plan, page after sequence 50000 51.5 ms 1.10 ms
Drain of the shard in 101 calls of 1000 records 5,184 to 5,230 ms 142.6 to 143.6 ms

The read on main, which looked the shard up in separate statements before reading its records, drained the same shard in 108.5 to 111.3 ms, measured the same way (three runs). The join form and the LATERAL form returned the same pages in the same order for six sequence bounds; an md5 of the page bytes matched for each.

Four PostgreSQL storage tests are added, three in crates/storage-postgres/tests/stream_shard_ownership.rs and one beside it:

  • five_pages_read_every_record_once_in_sequence_order reads five pages in turn and expects every record once, in sequence order.
  • an_idle_shard_reads_an_empty_page_with_and_without_a_bound expects an empty page, not the not-found error, from a shard with no record to read.
  • a_page_is_in_sequence_order_when_the_records_were_written_in_reverse (its own file, stream_records_page_order.rs) writes ten records with the highest sequence number first and expects pages of 1, 2, 3 and 3, 4, 5; with the subquery's ORDER BY removed the first page is 8, 9, 10.
  • a_page_visits_at_most_the_page_size_of_the_shards_records runs EXPLAIN (ANALYZE) on both statements, in the plan chosen for the parameters and in the generic plan, from the start of a shard of five pages and after its second page, and expects the scans of stream_records to visit no more rows than the page holds.

Stream not-found sentences

The sentence for a stream that does not exist is set per operation. On main, every stream not-found sentence on every backend was Requested resource not found: Stream: <arn> not found., with a trailing period. With this PR the sentences are:

Operation and case Answer Basis
DescribeStream, stream ARN of an existing table with a label the table never had Requested resource not found: Stream: <arn> not found (no period) Measured on DynamoDB in us-east-1 on 2026-10-24, two runs with the same answer
GetShardIterator, unknown stream ARN, existing shard id Requested resource not found: Stream does not exist Measured on DynamoDB in us-east-1 on 2026-10-24, two runs with the same answer
GetRecords, iterator that names an unknown stream, or a shard that is not the named stream's Requested resource not found: Stream does not exist Not measured. The service's iterator tokens are opaque, so a token of that kind cannot be built against it. GetRecords uses the GetShardIterator sentence
GetShardIterator, shard that is not the named stream's Requested resource not found: Stream: <arn> not found. (trailing period) Same text as on main. The service answers Requested resource not found: Shard does not exist (measured 2026-10-24); that difference is outside this PR

Three operations change their text compared with main: DescribeStream loses the trailing period, and GetShardIterator and GetRecords answer Stream does not exist for an unknown stream. tests/python/test_streams.py (TestStreamNotFoundSentences) sends DescribeStream, GetShardIterator and GetRecords to an unknown stream ARN of an existing table and compares the code and the message of each. test_streams.py and test_stream_shard_ownership.py pass on SQLite, PostgreSQL and MongoDB.

File sizes

crates/engine/src/streams.rs goes from 463 to 597 lines. The PostgreSQL stream engine goes from 617 to 609 lines and the MongoDB stream engine from 810 to 814. Both engines were over 600 lines before this PR and are not split here. Three other files this PR grows were over 600 lines already: crates/storage/src/lib.rs (1,024 to 1,030), crates/storage-postgres/src/lib.rs (877 to 879) and crates/storage-cassandra/tests/direct/streams.rs (637 to 655). Every file this PR adds is under 600 lines. The largest is crates/storage-postgres/tests/stream_shard_ownership.rs at 587.

Re-enable test

test_iterator_reads_only_while_its_arn_is_the_tables_stream runs one state. After a disable and an enable, every backend keeps the table's stream ARN, so the state with a new ARN cannot be reached. The test asserts that precondition, so a backend that starts issuing a new ARN fails with that message, and a comment names #515 as the change that makes the other state reachable.

Other changes in this update

  • MongoDB: a record document without record_data answers Internal (Missing record_data), as it does on main. The new aggregation had dropped such a document as an idle page. The aggregation now marks every joined record, and two live tests pin both halves: the document without record_data, and the idle shard.
  • A shard iterator token with fewer than four fields answers Invalid ShardIterator in the engine, as the authorization layer does. A build without the layer (developer mode) answered such a token as expired.
  • The doc comment on StreamEngine::get_stream_records in crates/storage/src/lib.rs and the comment in handle_get_records in crates/engine/src/streams.rs now say that only SQLite resolves the shard in the same round trip as the records, and that PostgreSQL, MongoDB and Cassandra read the table first. The comments in crates/server/src/request_helpers.rs and tests/test_authz_resource_arn.py now say that the storage read resolves the shard against the stream, not that the engine checks it before reading.
  • docs/dynamodb-limits.md lists the GetRecords limit as enforced and gives its two sentences.
  • The SQLite stream tests and the Python shard ownership test carry the copyright line.

…rds read

GetRecords called validate_shard before get_stream_records, which added two
catalog queries to every poll on top of the account ownership guard inside
the read. get_stream_records now takes the iterator's stream ARN and resolves
the shard against the table that ARN names, scoped to the caller's account,
in the same query as the records read (SQLite), or in one catalog lookup plus
one data query (PostgreSQL, MongoDB), or one catalog lookup plus the shard row
and records read issued together (Cassandra). Whether the shard belongs to
the stream the ARN's label names is decided in one function,
extenddb_storage::util::shard_in_stream, from the table's current label. A
shard of another table, another account, or an earlier stream label on the
same table gets the stream not-found answer validate_shard gave. The
authorization decision is untouched: crates/server does not change.

Storage statements per GetRecords call, measured on an idle shard and on a
shard with records: SQLite 4 to 1, PostgreSQL 5 to 2, MongoDB 5 to 2,
Cassandra 4 to 3 (two sequential round trips instead of four; idle p50
2.1 ms to 1.6 ms over loopback).

GetRecords refuses a Limit under 1 with "1 validation error detected: Value
at 'limit' failed to satisfy constraint: Member must have value greater than
or equal to 1" and over 1000 with "One or more parameter values were invalid:
Limit should be less than or equal to 1000" before the read (both measured on
the service 2026-10-25; before, 0 reached storage and 1001 was clamped).
MongoDB streams the page one record per document, so a page over 16 MB reads
instead of answering InternalServerError. Every streams path answers one
stream not-found sentence, without a trailing period, as measured on
DescribeStream.

validate_shard stays for GetShardIterator. A GetRecords token with no stream
ARN is answered as expired by the engine, as the authorization layer already
does; in a dev-mode build, where authorization does not classify the token,
such a token with a real shard id read before and is expired now.

Tests: seven SQLite storage tests, three unit tests on shard_in_stream, one
engine test on the Limit bounds, storage-level ownership tests on PostgreSQL,
Cassandra and MongoDB (same-label other table, same-named other account, an
earlier label on the same shards; a 40 by 450 KB page on MongoDB, inserted
out of order so the read's sort is what the assertion sees), and three wire
tests under tests/python. The MongoDB storage tests run in the backend's CI
job against its container.

Closes #438
The data read joined the shard row to its records and put the ORDER BY and
LIMIT after the join. The planner does not treat the join output as ordered by
the record sequence number, so it fetched every record of the shard after the
bound and sorted them before the LIMIT applied. A call cost time in proportion
to the unread backlog of the shard instead of the page size, and held a data
pool connection for that time.

The records now come from a LEFT JOIN LATERAL subquery that carries its own
ORDER BY and LIMIT. The planner reads the primary key (shard_id,
sequence_number) in order and stops after LIMIT rows. The outer query keeps
the result shape: a shard of the named table with no record to read returns
one row with a NULL record, and a shard of another table returns no row. The
statement text moves to stream_records_sql.rs so the engine and the plan test
run the same text.

Measured on PostgreSQL 16.15 with the shipped schema and one shard of 100,000
records of about 1 KB, in a table of 299,000 stream records (EXPLAIN ANALYZE,
the second of two runs):

  first page of 1000          join 102.7 ms, 22,580 buffers   lateral 0.56 ms, 158 buffers
  page after sequence 50000   join 53.0 ms                    lateral 0.85 ms
  generic plan, first page    join 81.0 ms                    lateral 1.49 ms
  drain in 101 pages          join 5,183 to 5,230 ms          lateral 142.6 to 143.6 ms

The drain figures are three runs each. The read before the shard check
existed took 108.5 to 111.3 ms for the same drain.

Three tests join stream_shard_ownership.rs. The paging test reads five pages in
turn and expects every record once, in sequence order. An idle shard reads as an
empty page, not the not-found error, with and without a sequence bound. The
plan test runs EXPLAIN (ANALYZE) on both statements, in the plan chosen for the
parameters and in the generic plan, from the start of a shard of five pages and
after its second page, and expects the scans of stream_records to visit no more
rows than the page holds.
DescribeStream and GetShardIterator answer a stream ARN that names no stream of
the caller's account with different sentences. Measured against DynamoDB in
us-east-1 on 2026-10-24, with the last three digits of the stream label of an
existing table changed, twice with identical answers:

  DescribeStream     Requested resource not found: Stream: <arn> not found
  GetShardIterator   Requested resource not found: Stream does not exist

The first has no trailing period. On main every backend answers all three
operations with "Stream: <arn> not found." and the period. The shared helper
added earlier in this series moved all three to the DescribeStream form, which
also changed GetShardIterator and GetRecords. The sentences are now set per
operation:

  DescribeStream, unknown stream ARN: "Stream: <arn> not found", no period.
  GetShardIterator, unknown stream ARN: "Stream does not exist".
  GetRecords, unknown stream ARN, or a shard that is not the stream's:
  "Stream does not exist".
  GetShardIterator, a shard that is not the stream's: unchanged, "Stream:
  <arn> not found." with the period. The service answers it with "Shard does
  not exist" (measured 2026-10-24); that sentence is not part of this change.

The GetRecords sentence is not measured. The service's iterator tokens are
opaque, so a token that names an unknown stream, or pairs a stream with another
stream's shard, cannot be built against it. GetRecords uses the GetShardIterator
sentence for both.

storage::util gets stream_does_not_exist and shard_not_in_stream beside
stream_not_found, each with a unit test on its text. The four backend ownership
tests compare a GetRecords answer with the sentence itself, where they compared
it with the helper's own output. The engine test maps the new error to a
ResourceNotFoundException. tests/python/test_streams.py sends DescribeStream,
GetShardIterator and GetRecords to an unknown stream ARN of an existing table
and compares the code and the message of each.
The doc on StreamEngine::get_stream_records and the comment in
handle_get_records said the shard is resolved in the same round trip as the
records. Only SQLite does that. PostgreSQL, MongoDB and Cassandra read the
table first. Both texts now say so.

A token with no stream ARN is answered by the authorization layer as expired
when it carries a timestamp and as an invalid iterator when it does not. The
engine answered expired for both, so a developer-mode build, which skips the
layer, answered a token with fewer than four fields differently from the other
builds. no_stream_arn_error gives the engine the layer's answer. A unit test
runs a two-field token, a three-field token, a four-field token and a
four-field token with an unparseable timestamp through both the engine and
classify_shard_iterator and requires the same class.

The comments in request_helpers.rs and tests/test_authz_resource_arn.py said
the engine confirms the shard belongs to the stream before reading. They now
say the storage read resolves the shard against the stream.

docs/dynamodb-limits.md lists the GetRecords limit as enforced and gives the
two sentences (measured 2026-10-25): a Limit under 1 answers the constraint
sentence for 'limit', and a Limit over 1000 answers "Limit should be less than
or equal to 1000". The summary counts, the list of unenforced limits and the
GetRecords row of docs/technical-debt.md no longer list it as a gap.
The test that reads an old shard iterator after a disable and an enable had a
second branch for a re-enabled stream with a new ARN. No backend gives a
re-enabled stream a new ARN yet, so that branch could not run anywhere. It
also could not pass: the shard is resolved against the stream the iterator
names, so the old iterator answers "stream does not exist" where the branch
expected a 200 with none of the new keys.

The test now covers the state that exists. The table keeps its ARN, and the old
iterators read across the four shards exactly what fresh iterators read. An
assertion states that precondition, so a backend that starts issuing a new ARN
fails with that message, and a comment names the change that makes the other
state reachable. The module docstring says the storage-level tests cover an
iterator whose ARN is no longer the table's current stream.
The GetRecords aggregation starts from the shard document, joins the shard's
records, and keeps the shard document when no record follows the bound. The
read dropped every output document without record_data to skip that
shard-only document, so a record document that lacked record_data was dropped
too and read as an idle page. Before the records were joined in the
aggregation, the read answered Internal ("Missing record_data") for it.

The final $project now emits has_record: true when a record was joined and
false for the shard-only document. records_from_docs (stream_record_docs.rs)
skips the documents with has_record false and answers Internal when a
document with has_record true carries no record_data. The conversion moves
into that file, which takes stream_engine.rs from 817 to 814 lines.

Three unit tests cover the shard-only document, joined records in order and
the missing field, and need no server. Two live tests run against MongoDB
7.0.45. One writes a good record and a record document without record_data,
reads the first page, which returns the good record, and expects the next page
to answer Internal. The other reads a shard that never had a record and a
shard past its last record, and expects an empty page for both.
crates/storage-sqlite/src/stream_tests.rs and
tests/python/test_stream_shard_ownership.py started with the SPDX line only.
The files beside them put "Copyright 2026 ExtendDB contributors" above it,
and both files now do the same.
No test failed when the ORDER BY inside the records subquery was removed: a
shard written in sequence order returns the right page in physical order too.
The new test writes ten records with the highest sequence number first and
reads a page of three from the start and after sequence 2, expecting 1, 2, 3
and 3, 4, 5. With the clause removed the first page is 8, 9, 10. The test goes
in its own file because stream_shard_ownership.rs is at 587 lines, and the
PostgreSQL CI step lists it.

Two comments and one list follow the code: a token without a timestamp is
invalid (earlier builds did issue such tokens), the shard iterator lifetime and
tag rows of the unenforced-limits list are numbered 7 to 9 after the GetRecords
row left it.

@robinnsc robinnsc left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving: nothing here blocks. The ownership check holds up. Forged iterators on the SQLite, PostgreSQL and MongoDB release binaries (another table's shard, another account, an older or near-miss label, a deleted and recreated table, a table with no stream, swapped shards on every iterator type) gave the same answers as main apart from the intended sentence change. Dropping the account, the table name, the shard's table id or the label from the predicate failed a test on each of the four backends. The statement counts also matched what we logged: PostgreSQL 5 to 2 and MongoDB 5 to 2, plus SQLite 4 to 1 from reading the code. A few things in the description:

  • The #515 merge notes look out of date. git merge-tree --write-tree against #515's head (5f22dc4) gives 9 conflicted files and 19 hunks. The extra file is crates/storage-postgres/src/lib.rs, from the LATERAL commit (pub mod stream_records_sql next to #515's mod stream_lifecycle). One of the four Cassandra hunks is in describe_stream, not validate_shard. After resolving the textual conflicts it still won't compile: both sides add a stream_table_row method on CassandraEngine with different return types (stream_engine.rs:68 here, stream_lifecycle.rs:387 there), so one needs renaming. The merged tree would also hold two copies of each not-found sentence (util::* and stream_lineage::*).
  • On main's MongoDB, Limit: 0 didn't answer a 500. limit(0) means no limit, so the base binary returned the whole shard (3,753 records in our run). The fix is the same, but main's behaviour was worse than described. Similarly, in dev mode main read a token with no stream ARN with only the account check, which this PR now refuses; that might be worth a line.
  • Main's records statement is still a bit faster per full page in database time than the LATERAL form (see the inline comment), which the plan table doesn't show because it compares against the first commit's join form. Over the network, the saving is the three removed round trips.
  • A few counts have moved on: engine 182, storage 134, SQLite 151, MongoDB 125 plus 6 live, and test_streams.py 16. There are also a couple of pasted fragments ("No dependency change.rs` 1024 to 1030 ...", "loop.The stream", "lifecycle.The re-enable") and a bullet that lost its marker in "Not in this PR".
  • Refusing Limit over 1000 matches the service, but the botocore model has no maximum, so a client written against ExtendDB alone that sends 1001 or more now gets a ValidationException where main clamped. The Kinesis adapter clamps to 1000 itself. Probably worth a release note.

Separately from this PR: on PostgreSQL a consumer can skip records written by concurrent writers, because the sequence comes from nextval('stream_seq') inside the write transaction (data/tx_helpers.rs:340-344), so a later sequence can commit first and the reader moves past the earlier one. With 8 BatchWriteItem threads and a LATEST reader per shard, 0 to 11 of 8,000 records were skipped on the base binary as well as this branch. The records are in stream_records, so a fresh TRIM_HORIZON read finds them. It's outside #438, so it probably wants its own issue.

ORDER BY sequence_number LIMIT $3 \
) r ON true \
WHERE s.shard_id = $1 AND s.table_id = $2 \
ORDER BY r.sequence_number";

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This outer ORDER BY adds a Sort over the page the LATERAL subquery already returns in key order. On a 100k-record shard, a page of 1000 took 2.2 ms in EXPLAIN against 1.2 ms without it and about 1.1 ms for main's records statement, or roughly 10.3 ms against 8.7 ms client side. It's invisible behind the ~85 ms the server spends per page, and an idle or Limit-1 poll sorts one row, so this is optional. The ordering is needed for a guarantee, since NextShardIterator comes from the last record, but nothing pins it: removing it from both statements still passes every PostgreSQL test, because the nested loop happens to emit in order. If you want the database time back, one option would be to drop it and sort records by dynamodb.sequence_number in Rust (the values are fixed width), with a test that would fail on an unordered page. Keeping it and adding such a test would also be fine.

};

let limit = input.limit.unwrap_or(1000).min(1000);
let limit = get_records_limit(input.limit)?;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small test gap: replacing this call with main's unwrap_or(1000).min(1000) still passes the engine tests, because get_records_limit_bounds_carry_the_measured_sentences calls the function directly. Only tests/python/test_stream_shard_ownership.py covers the wiring. A unit test through handle_get_records with Limit: 0 would pin it on the Rust side.

@LeeroyHannigan
LeeroyHannigan added this pull request to the merge queue Oct 29, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to no response for status checks Oct 29, 2026
@LeeroyHannigan
LeeroyHannigan added this pull request to the merge queue Oct 29, 2026
Merged via the queue into main with commit 877ef39 Oct 29, 2026
34 checks passed
@LeeroyHannigan
LeeroyHannigan deleted the fix/438-get-records-round-trips branch October 29, 2026 16:36
LeeroyHannigan added a commit that referenced this pull request Oct 31, 2026
…ames

util::shard_in_stream decided ownership by the table's current label
alone, which was right while a table had one stream. With a stream per
enable, that rule made a closed stream unreadable as soon as the table
was enabled again (its ARN answered "Stream does not exist") and let an
iterator pairing the new ARN with an old shard id read the old stream.

The rule now reads the shard id with stream_lineage::parse_shard_id: a
labelled id belongs to the label it carries, and an id from before
streams were named in shard ids belongs to the table's current stream
while the table has no labelled shard. From a table's first enable under
this release every later stream is labelled, so legacy rows beside a
labelled row are the earlier, closed stream, which is how DescribeStream
and GetShardIterator already group them. All four backends call it from
get_stream_records, so a closed stream stays readable under its own ARN
after the table has a newer one, and a token pairing the newer ARN with
an older stream's shard id, labelled or legacy, is refused.

Whether the table has a labelled shard is read only for a legacy id, so
a labelled id keeps #543's statements: an EXISTS on stream_shards over
the byte-order range of the table's labelled ids on SQLite and
PostgreSQL (stream_lineage::labelled_shard_id_range; COLLATE "C" on
PostgreSQL), a find_one on the {table_id, shard_id} index with the
labelled id pattern on MongoDB, whose legacy ids have 12-digit suffixes,
and the table's shard rows on Cassandra.

Unit tests on the rule (a legacy shard under a table that has a labelled
stream for the current label is not its stream; a labelled shard is
decided without reading the table's shards) and on the labelled range,
and SQLite engine tests reading a re-enabled table: the closed stream
under its own ARN, the new stream under its, the two cross pairings
refused, and the new ARN with a legacy shard of a closed stream refused.
@navareamol navareamol added the v1 Needed for the 1.0 release label Nov 3, 2026
@navareamol navareamol added this to the 1.0 milestone Nov 3, 2026
LeeroyHannigan added a commit that referenced this pull request Nov 3, 2026
Merged with main, the storage-mongodb test build failed with E0063: the
MongoEngine literals in stream_records_live_tests.rs (from #543) and in
snapshot_engine.rs (from #395) do not set contention_deadline. Each test
that needed an engine listed every field, so each field added to
MongoEngine broke every such literal.

MongoEngine::new now builds the engine in from_parts, and a test-only
MongoEngine::for_test calls the same function with the driver's default
pool of 10 connections. The test literals in backup_live_tests.rs,
stream_records_live_tests.rs, snapshot_engine.rs and the restore backfill
test now call for_test and set only the fields they change with struct
update syntax. A new field is set in from_parts and nowhere else.

The four contention storage tests move from the data_engine test module
into data_engine_live_tests_contention.rs, next to the create-race tests
#431 moved. They build the engine through the live test module's
scratch_engine, are #[ignore]d like the other live tests and run under
devtools/run-mongodb-tests --rust-live, and set the deadline with struct
update syntax. The with_scratch_engine helpers and the second URI gate go
with them. What each test checks is unchanged.
@navareamol navareamol added the release-blocker Must ship for its milestone's release label Nov 3, 2026
parsnips pushed a commit to parsnips/extenddb that referenced this pull request Nov 11, 2026
…new one (ExtendDB#515)

* fix(streams): disabling a stream closes its shards; enabling creates a new stream

On UpdateTable StreamEnabled=false the stream is closed: DescribeStream
reports DISABLED with the stream's own view type, every shard carries an
EndingSequenceNumber (its last record's sequence number, or its starting
number when it holds none), DescribeTable omits StreamSpecification, and
LatestStreamArn keeps naming the closed stream. Writes record only to
open shards.

UpdateTable StreamEnabled=true afterwards creates a new stream with a
later label and four new shard ids; the closed stream stays describable
and listed. ListStreams lists every stream of a table, newest first.
Enabling a table whose stream is enabled is refused with "Table already
has an enabled stream: TableName: <name>", and disabling a table with
no enabled stream with "Table has no stream to disable: TableName:
<name>". GetShardIterator on a stream ARN with a shard of the table's
other stream answers "Requested resource not found: Shard does not
exist".

A stream change travels alone: an UpdateTable carrying
StreamSpecification beside BillingMode or ProvisionedThroughput is
refused with "You cannot modify stream status while updating table
IOPS", beside any GlobalSecondaryIndexUpdates entry with "You cannot
create or delete index while changing stream status", beside
DeletionProtectionEnabled with "DeletionProtection modification must be
the only operation in the request", and nothing is applied. A disable
beside a StreamViewType is refused ("If stream is being disabled, then
UpdateViewType must not be specified"). CreateTable with a stream
enabled and no view type is refused ("Table is being created with a
stream enabled, UpdateViewType is required"); a StreamSpecification
without StreamEnabled on CreateTable or UpdateTable gets the model's
null-member sentence; a table created with StreamSpecification
{StreamEnabled: false} stores nothing of it.

ListStreams carries LastEvaluatedStreamArn whenever Limit cut the page,
refuses an ExclusiveStartStreamArn of another table ("ExclusiveStartStreamArn
provided is invalid: <arn>"), Limit under 1 and a start ARN under 37
characters. GetShardIterator refuses a ShardId under 28 or over 65
characters and a SequenceNumber under 21 with the service's constraint
sentences, answers AT_SEQUENCE_NUMBER without a number with
"SequenceNumber cannot be null", and an unknown stream ARN with
"Requested resource not found: Stream does not exist". DescribeStream
refuses a short ExclusiveStartShardId and answers an unknown stream of an
existing table with "Stream: <arn> not found".

Shard ids written before v0.1.14 (shardId-{table_name}-{index}) are the
table's shards: that stream stays describable, listed and readable after
the upgrade, and keeps its view type when disabled (the disable stores
{StreamEnabled: false} with the view type and responses omit the
member). A disable closes only the stream it decided on. On MongoDB and
Cassandra the catalog write of a stream change is conditioned on the
specification and label it read, so of two overlapping changes exactly
one applies and the other is refused; an enable re-reads the row after
inserting its shards and closes them when a disable got in between, so a
DISABLED stream has every shard closed. A write racing a disable
lands before the ending number on PostgreSQL (the open shards are read
FOR SHARE and locked FOR UPDATE by the close) and in no stream on
Cassandra (a new stream starts at a clock value drawn at enable time);
MongoDB keeps a one-transaction window.

Each stream's identity is carried in its shard ids
(shardId-{table_id}-{label_millis}{view_type}{index}), so no backend
needs a schema change; ids written before this scheme belong to the
table's current stream. The rules shared by the four backends live in
extenddb_storage::stream_lineage. On Cassandra, whose write paths
compute a record's partition without reading the shard rows, a stream's
shard is a range of its index's partition.

Re-cut on main after 877ef39 (resolving the shard against its stream
inside the GetRecords read). The not-found sentences use the one set of
helpers in extenddb_storage::util; GetShardIterator with a shard of the
table's other stream answers "Requested resource not found: Shard does
not exist" (util::SHARD_DOES_NOT_EXIST, measured 2026-10-24), replacing
the "Stream: <arn> not found." sentence main kept there. The Cassandra
get_stream_records keeps main's ownership check (the shard row's table
id and util::shard_in_stream) and its two round trips, and reads the
records from the shard's physical partition in parallel with the shard
row, applying the shard's sequence range to the rows it gets back. The
engine keeps main's constraint helper beside the shard id length
constants.

Closes ExtendDB#482

* fix(streams): GetRecords resolves a shard against the stream its id names

util::shard_in_stream decided ownership by the table's current label
alone, which was right while a table had one stream. With a stream per
enable, that rule made a closed stream unreadable as soon as the table
was enabled again (its ARN answered "Stream does not exist") and let an
iterator pairing the new ARN with an old shard id read the old stream.

The rule now reads the shard id with stream_lineage::parse_shard_id: a
labelled id belongs to the label it carries, and an id from before
streams were named in shard ids belongs to the table's current stream
while the table has no labelled shard. From a table's first enable under
this release every later stream is labelled, so legacy rows beside a
labelled row are the earlier, closed stream, which is how DescribeStream
and GetShardIterator already group them. All four backends call it from
get_stream_records, so a closed stream stays readable under its own ARN
after the table has a newer one, and a token pairing the newer ARN with
an older stream's shard id, labelled or legacy, is refused.

Whether the table has a labelled shard is read only for a legacy id, so
a labelled id keeps ExtendDB#543's statements: an EXISTS on stream_shards over
the byte-order range of the table's labelled ids on SQLite and
PostgreSQL (stream_lineage::labelled_shard_id_range; COLLATE "C" on
PostgreSQL), a find_one on the {table_id, shard_id} index with the
labelled id pattern on MongoDB, whose legacy ids have 12-digit suffixes,
and the table's shard rows on Cassandra.

Unit tests on the rule (a legacy shard under a table that has a labelled
stream for the current label is not its stream; a labelled shard is
decided without reading the table's shards) and on the labelled range,
and SQLite engine tests reading a re-enabled table: the closed stream
under its own ARN, the new stream under its, the two cross pairings
refused, and the new ARN with a legacy shard of a closed stream refused.

* test(streams): read re-enabled and relabelled streams by the shard ids they carry

The wire test that pinned a re-enable to the same stream ARN moves to the
new ARN the service answers: the iterators issued before the disable read
the closed stream (the same records a fresh iterator on each of its
shards reads) and none of the keys written after the enable, which are
in the new stream's shards alone.

The Cassandra direct tests read a stream's records by the stream's own
shard id for the partition a key lands in, not by the partition id,
since get_stream_records resolves the shard row the id names. The
ownership tests' relabel moves the table's shard rows to the ids the new
label gives them, as a shard id carries its stream's label, and the
disable test checks that a token pairing one stream's ARN with the other
stream's shard id is refused.

* fix(engine): a StreamSpecification that is not an object stays a SerializationException

The StreamEnabled member check ran on any non-null StreamSpecification,
so a string, number or array there answered the null-member
ValidationException where main answered SerializationException from
deserialisation. The check now applies to objects only. The doc block
that had landed inside deserialize_error's comment documents the member
check it describes.

* fix(streams): apply the ShardId maximum only when the shard is not found

GetShardIterator refused a ShardId over 65 characters before looking it
up. The shards a release before v0.1.14 wrote are named after the table
(shardId-{table_name}-{index:016}), so a table whose name is over 40
characters had its stream refused with a ValidationException after the
upgrade, where the previous release read it. botocore enforces only the
minimum, so SDK clients reach this check.

The minimum and the SequenceNumber length are still checked first; the
maximum is applied when the lookup finds no such shard, which is the
answer the service gives since it never has a shard that long. A found
shard over the maximum gets its iterator.

* fix(streams): a refused disable of a disabled stream closes the shards left open

A release that stored {StreamEnabled: false} without closing the shards,
or a close that failed after its catalog write, leaves a stream that
DescribeStream reports as DISABLED with four open shards, and writes
keep recording into it until the key info cache expires. A disable on
that table was refused with "Table has no stream to disable" and changed
nothing, so only an enable closed the shards.

The decide step on each backend now closes the current label's open
shards when the stored specification is already disabled, then answers
the same refusal. SQLite closes in the request's transaction and commits
it before answering; PostgreSQL, MongoDB and Cassandra close in the
shard store, as the first disable would have, with no catalog write.

Tests on SQLite and PostgreSQL seed that state: the refusal stands, the
ending numbers are the last record or the starting number, a write after
it records nothing, a second refusal has nothing left to close, and on
SQLite an enable met with the same state closes the open shards too.

* perf(postgres): lock only the record's shard row on a streamed write

Every write to a streamed table locked all four open shard rows FOR
SHARE, so concurrent writers on one table kept extending the same
MultiXacts for a record that goes into one shard. The open set is now
read without a lock, and the chosen row alone is locked FOR KEY SHARE
with its ending_sequence_number re-checked. The close still locks the
open rows FOR UPDATE before taking the ending numbers, which conflicts
with this lock, so a write either commits before the ending number is
taken or finds its row closed and records nothing.

A storage test runs eight writers against a disable and checks that no
record is past its shard's ending number.

* perf(mongodb): index stream_shards by table_id and shard_id

stream_shards had the unique shard_id index alone, so the open-shard
read on every write to a streamed table, and the shard reads behind
DescribeStream, GetShardIterator and ListStreams, scanned the whole
collection across all accounts: with about 20k shard documents a write
examined 20,008 to return 4, and PutItem p50 went from 7.9 ms to 40 ms
(measured in review). Each enable now adds four documents that stay, so
the collection grows with every disable and enable cycle.

A {table_id: 1, shard_id: 1} index covers the filter and the sort of
those reads. extenddb init creates it in create_data_db. MongoDB's data
migration, which extenddb migrate --yes runs on every upgrade, creates
it too, so an existing deployment gets it; creating it again is a no-op,
and the migration reports it pending while it is missing. A live test
drops the index, checks it is reported pending, runs the migration and
finds it.

* fix(cassandra): start a new stream above the closed streams' ending numbers

A new stream's shards started at a value drawn from the enabling
server's clock alone. With several servers, a record of the closed
stream written by a server whose clock is ahead could sit above that
start, and so read in both the closed stream and the new one.

The start is now one above the larger of this server's next clock value
and the last ending number among the table's shards, taken after the
close. A server whose clock is behind the start can still write a
new-stream record below it, which no stream reads, and on several
servers a raced record from a server whose clock is ahead can still land
above the start; the module and the design document note both cases
beside the deferred cross-instance item. The design document also said
the new shard starts where the closed one ended, which neither the code
nor its later paragraph did.

A close writes a shard's ending number only when the shard has none
(UPDATE ... IF ending_sequence_number = null). An open shard's range has
no upper bound, so a disable whose close read the old shards as open and
took their ending numbers after an overlapping enable had closed them
and started the next stream would otherwise set them to a record of the
new stream from the shared partition, above the new stream's start.

Cassandra direct tests: one moves a closed stream's ending numbers an
hour ahead of the clock and checks that the new stream starts above
them; one runs a close with the old stream's rows as read before the
disable, after the enable and eight writes to the new stream, and checks
that the old shards keep their ending numbers.

* docs(streams): upgrade order for stream changes, and what a disable stores

The upgrade manual gets a behaviour change entry: a server on the earlier
release writes records past a closed shard's ending number and re-enables
a stream under its old ARN, so every server is upgraded before any
UpdateTable changes a StreamSpecification, and rollback is a restore of
the pre-upgrade database. No catalog version gate stops the earlier
release, since the change needs no catalog migration. The entry also
says an existing MongoDB deployment gets the new stream_shards index
from extenddb migrate --yes.

The PostgreSQL and MongoDB lifecycle comments and the design document
said a disable clears the specification; it stores {StreamEnabled: false}
with the stream's view type, which DescribeTable omits. DescribeBackup
for a backup taken after a disable keeps showing StreamDescription as
{StreamEnabled: false}, as before the view type was kept, since the
service's answer there is not measured.

* refactor(streams): move the engine stream tests and the ListStreams builder to their own files

Pure moves, no behaviour change: crates/engine/src/streams.rs had grown
to 711 lines with the GetShardIterator bounds, so its unit tests move to
streams_tests.rs; crates/storage/src/stream_lineage.rs had reached 602,
so the ListStreams page builder moves to stream_lineage_list.rs and is
re-exported from the parent module.

* test(postgres): pin stream labels through the shard ids the stream carries

The GetRecords ownership and page-order tests pinned a table's stream
label by updating the catalog row alone. A shard id now carries its
stream's label, so the ownership fixture's relabel moves each shard row
and its records to the ids the new label gives them, and the page-order
test reads under the label the table was created with. The relabel test
also checks that the old shard id under the new ARN is refused.

* fix(streams): an enable closes the open shards of every earlier stream

On PostgreSQL, MongoDB and Cassandra a stream change writes the catalog
row first and changes the shards after it. An enable closed only the
open shards of the label it read. When a disable's close failed after
its catalog write and the client then enabled the stream, and that
enable's close failed too, the table was enabled under the new label
with no shards of its own while the earlier stream's four shards stayed
open: writes recorded into a DISABLED stream, a later disable closed
only the new label's rows, and every later enable closed only the label
it read, so those shards were never closed.

An enable now closes the open shards of every stream of the table older
than the one it creates, legacy rows included
(stream_lineage::open_shard_ids_before). A disable still closes only
the stream it read, so a disable that applies after an overlapping
enable leaves the new stream open. SQLite closes every open shard of
the table in the transaction that writes the catalog row, so it is
unchanged.

PostgreSQL storage tests: an_enable_closes_an_earlier_stream_left_open
seeds an enabled label with no shards beside an earlier stream's open
shards, and the next disable and enable close them;
a_disable_applying_after_an_overlapping_enable_leaves_the_new_stream_open
applies a disable's shard changes for the earlier label after the
enable created the new stream, and the new stream's four shards stay
open.

* refactor(postgres): split the stream disable storage tests into two files

tests/stream_disable.rs had grown to 877 lines. The scratch database and
request helpers move to tests/common/stream_disable_fixture.rs, which
both test files include; the lifecycle and race tests stay in
stream_disable.rs, and the two tests of states an earlier release left
(the name-based shards from before v0.1.14, and a disabled stream whose
shards stayed open) move to stream_disable_legacy.rs. The test bodies
are unchanged; the helpers gain `pub`. The PostgreSQL storage step in
the integration workflow runs the new --test stream_disable_legacy.

* refactor(streams): move the labelled shard checks and the Cassandra shard reads to their own files

Three files this change adds had grown past 600 lines. Pure moves, by
concern:

- stream_lineage.rs (603 lines): labelled_shard_id_range,
  labelled_shard_id_pattern and has_labelled_shard move to
  stream_lineage_labelled.rs, re-exported from stream_lineage.
- storage-cassandra stream_lifecycle.rs (635): the table and shard row
  reads (stream_table_row, stream_table_rows, stream_shard_rows and their
  row helper) move to stream_lifecycle_rows.rs, a child module.
- storage-cassandra tests/direct/stream_disable.rs (634): the two tests
  of a closed stream's ending numbers against the next stream's start
  move to tests/direct/stream_endings.rs; the request helpers they share
  with stream_disable.rs become pub(crate).

* fix(postgres): lock stream shards in id order and number the backlog first

A disable on PostgreSQL closes the shards of its stream in one READ
COMMITTED transaction. The transaction locks the open shard rows FOR
UPDATE, numbers the records waiting in stream_pending, and then takes the
ending numbers.

- crates/storage-postgres/src/stream_lifecycle.rs: before that
  transaction, the close numbers each shard's backlog with number_shard,
  the numbering worker's path. Its batches are separate transactions,
  bounded by NUMBER_BATCH_BYTES. Each locks the shard row FOR NO KEY UPDATE
  SKIP LOCKED, and writers do not wait for that lock. The locked numbering
  then moves only what those batches left, normally the records committed
  after them. Before this change, a write that still carried the enabled
  stream specification waited while the close numbered the whole backlog.
  The close functions take the engine instead of the data pool, so they
  can call number_shard.
- Same file: the locking select has ORDER BY shard_id COLLATE "C". The
  close then locks the rows in the byte order of their ids, whatever the
  database collation.
- crates/storage-postgres/src/data/transactions.rs and data/tx_helpers.rs:
  TransactWriteItems first chooses the shard of each stream record. It then
  stores the records in the same byte order of shard ids. Two records on one
  shard keep their request order. Before this change, a transaction with
  items on shard 2 and then shard 0 held shard 2 and waited for shard 0. A
  close that held shard 0 waited for shard 2, and PostgreSQL cancelled one
  of the two after deadlock_timeout.
- crates/storage-postgres/tests/stream_disable_lock_order.rs: two tests
  hold a transaction after its first stream record and start a disable.
  Each checks that both calls succeed and that the close numbered both
  records. One test puts its items in descending shard order. The other
  writes a new version of the lowest shard row after the rest. It also
  disables index scans, so a close without ORDER BY reads that row last.
  Without its half of the change, each test fails with "deadlock detected".
- crates/storage-postgres/tests/stream_disable.rs: a new test checks that
  the closing transaction numbers none of six pending records.
- SQLite, MongoDB and Cassandra have no such cycle. SQLite runs the close
  and every write under one engine write lock. The MongoDB close writes
  only shard documents, which write transactions only read. Cassandra
  writes never read or lock shard rows.
- docs/manuals/07-upgrade-manual.md: the streams section points to the rule
  that migrations 1 to 7 are breaking, instead of advice for servers of two
  releases. It drops the claim that no reader finds a record written past
  an EndingSequenceNumber. It names the first UpdateTable that sets a
  StreamSpecification as the call that closes shards the earlier release
  left open.
- docs/design/07-component-streams.md: the PostgreSQL paragraph describes
  the numbering before the lock and the shard lock order.

Signed-off-by: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com>

* fix(cassandra): condition the stream change catalog write on table_id

A table created without a stream has both stream columns null. Its
first enable binds null for the stream specification and the stream
label that it read. Cassandra meets a condition that compares a column
with null on a row that does not exist, and an UPDATE then writes the
row. A DeleteTable can remove the row between the read and the
lightweight transaction. An enable that ran in that window wrote a
catalog row that held only the columns that the write sets.

The write of the tables row is now conditional on table_id when it
carries a WarmThroughput or a stream change. A missing row fails the
condition. A table created again under the same name has a different
table_id and fails the condition too. For a request with a stream
change, the read after a failed write and
refuse_overlapping_stream_change answer TableNotFound for such a row.
They give the same answer for a missing row. A WarmThroughput without a
stream change keeps main's retry loop. A missing row ends that loop. On
a table created again under the same name, the loop tries again until
MERGE_ATTEMPTS and then answers Internal, as on main.
refuse_overlapping_stream_change reads the row at LOCAL_SERIAL through
the same query as the retry loop.

Tests:
- A unit test pins the condition and the order of its bound values. It
  covers four cases: no part, a WarmThroughput only, a stream change
  only, and both.
- A direct test runs the stream enable write with null stream values
  against a key that has no row. The write does not apply and no row
  appears. The same write applies to a table created without a stream.
  On a local Cassandra 4.1.12 the test fails without the table_id part
  and passes with it.

---------

Signed-off-by: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-blocker Must ship for its milestone's release v1 Needed for the 1.0 release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GetRecords makes three storage round trips per call after the resource ARN authorization change

3 participants