Skip to content

[lake] Keep a bucket's lake splits in write order for union read - #5288

Open
jaimelopez89 wants to merge 2 commits into
apache:mainfrom
jaimelopez89:jaimelopez89/iceberg-ordering
Open

jaimelopez89 wants to merge 2 commits into
apache:mainfrom
jaimelopez89:jaimelopez89/iceberg-ordering

Conversation

@jaimelopez89

Copy link
Copy Markdown
  • No generative AI tools used
  • Yes (please specify the tool below)

Created with: Claude Code (Claude Opus 5.5) following your repo guidelines

Purpose

Linked issue: close #5077

A Flink union read of a log table tiered to Iceberg returns a bucket's lake data in arbitrary order. Iceberg's planFiles() has no defined order (manifests are kept in a hash set and planned in parallel), and Fluss passed that order straight to the reader. Separately, Flink restores a reader's splits in hash order (SourceReaderBase keeps them in a HashMap), so a bucket's pending lake splits can be reordered after a restore. That affects Paimon as well as Iceberg.

Brief change log

  • IcebergSplitPlanner#plan: sort splits by the data file's data sequence number with a stable sort. Each tiering round is one Iceberg commit, so this is write order. Files of one commit share a sequence number and keep the order Iceberg appended them in. No extra I/O.
  • FlinkSourceReader#addSplits: put LakeSnapshotSplits back in planning order (partition, then split index) before handing them to the split reader. Restored state and enumerator assignments both arrive through addSplits in one batch, so this covers restore and reassignment after a failure. Other split types keep their positions.
  • Docs: a new "Read Order" section in streaming-lakehouse/union-read.md. It states that for Iceberg, per-bucket order in the lake part is best effort, and describes the effect of compaction, tables without a bucket key, rescaling during the lake phase, and the watermark caveat for event-time jobs.

Known limits, as discussed in the issue: compaction rewrites files without keeping row order, so rows rewritten by a single compaction run can still come out of order. Order across buckets is not addressed. Event-time-aware split ordering and order-preserving compaction are left as follow-ups.

Tests

Unit tests:

  • IcebergSplitPlannerTest#testLogTablePlanKeepsWriteOrderWithinBucket (partitioned and non-partitioned): five interleaved commits into two buckets, then one commit with three files into one bucket. It plans 20 times and asserts that each bucket's splits come out in write order.
  • FlinkSourceReaderTest#testLakeSnapshotSplitsKeepPlanningOrderAcrossRestore: one reader with two buckets of three lake splits each. It reads part of them, checkpoints, reverses the checkpointed splits and restores while lake splits are still pending, then asserts that each bucket's records come out in planning order.
  • FlinkSourceReaderTest: three smaller tests of the sort. They check ordering by partition then split index, that other split types keep their positions, and the case with fewer than two lake splits.
  • LakeSplitGeneratorTest#testLogTableLakeSplitIndexFollowsPlanningOrderWithinBucket: checks that the split index follows planning order within a bucket, which is what the reader sort relies on.

Integration tests, FlinkUnionReadOrderITCase, using a real Fluss cluster and tiering service, a 3-bucket table, five tiering rounds and then log-only data:

  • testBatchUnionReadKeepsWriteOrderWithinBucket: batch union read, asserting each key comes back in write order across the lake and log parts.
  • testStreamingUnionReadKeepsWriteOrderAcrossRestoreDuringLakePhase: a streaming union read that fails once, right after a completed checkpoint, while lake splits are still pending. It asserts order before and after the restore, and that the job actually resumed in the middle of the lake data. Test hooks, not fixed sleeps, coordinate the failure.

Red/green: the new planner, reader and integration tests fail without their fixes and pass with them. Without the planner sort, the batch IT read one key as 10-19, 0-9, 30-39, 20-29, 40-49, 50-59.

Locally, mvn verify passes for fluss-flink-common, fluss-lake-iceberg-common and fluss-lake-paimon, and spotless and checkstyle pass. Before rebasing, the integration tests also passed against the Flink 2.2 and 2.3 connectors. fluss-lake-iceberg-1.12 (JDK 17) was not built locally.

API and Format

No API or storage format change. No change to checkpointed state.

Documentation

Adds a "Read Order" section to the union read page.

Found by @dgnester while testing union read on Fluss 1.0. The diagnosis and this change were made with AI tooling (Claude Code).

Iceberg's planFiles() returns data files in no defined order, and the
Flink source read a bucket's lake splits in that order. Sort the planned
splits by data sequence number, which follows the tiering commit order.

Flink also restores a reader's splits in hash order, which reorders a
bucket's pending lake splits for both Iceberg and Paimon. Put lake
snapshot splits back in planning order in FlinkSourceReader#addSplits.

Document the read order of union read, including that Iceberg order is
best effort once the table is compacted.

Found by Dustin Nest (@dgnester). Diagnosed and implemented with
Claude Code.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jaimelopez89

Copy link
Copy Markdown
Author

Where this was tested

All runs were local, on macOS with OpenJDK 11.0.29 and Maven 3.9.11, on this branch rebased onto current main. They used the repo's default dependency versions:

Component Version
Flink 1.20.3 (default connector)
Iceberg 1.10.1
Paimon 2.1.0

Full mvn verify, all passing after the rebase:

Module Unit tests Integration tests
fluss-flink-common 850 57
fluss-lake-paimon 331 109
fluss-lake-iceberg-common 188 (3 skipped, in existing tests) 30

The 30 integration tests include both FlinkUnionReadOrderITCase tests.

Red/green checks:

  • The new planner test, reader restore test and both integration tests fail without their fix and pass with it.
  • The restore check failed 3 of 3 runs without the reader change and passed 3 of 3 with it.
  • Without the change, restored splits came back in plain HashMap order.

Flink 2.x, checked before the rebase:

  • FlinkUnionReadOrderITCase passes against the Flink 2.2.0 and 2.3.0 connectors.
  • For those runs, the Iceberg Flink test dependency was pinned to iceberg-flink-1.20, because iceberg-flink-2.2 isn't published for Iceberg 1.10.1. That pin isn't part of this PR.
  • The fluss-flink-2.3 suite passes: 72 unit and 237 integration tests. 180 are skipped by design, because that module reruns only @MultiVersionTest tests.
  • The restore fix applies to 2.x as well. SourceReaderBase still keeps splits in a HashMap in 2.2 and 2.3, and restore still goes through addSplits.

Not tested locally:

  • fluss-lake-iceberg-1.12 (Iceberg 1.12.0), which needs a JDK 17 toolchain.
  • The Flink 1.18 and 1.19 connectors. They share the changed reader code from fluss-flink-common.

I'm relying on CI for these.

// with more than one manifest, planned concurrently on a worker pool. The Flink source
// reads a bucket's lake splits one after another, so put them back in write order. Each
// tiering round is one Iceberg commit, so the data sequence number follows write order.
// The sort is stable, which keeps the files of one commit in the order Iceberg appended

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could we qualify the same-commit ordering claim here? Iceberg can roll one commit's files into multiple manifests, while planFiles() does not guarantee manifest order. Files with the same data sequence number remain tied after this sort, so a stable sort may still return them out of write order. Could we test a commit spanning multiple manifests, or describe this case as best effort?

@jaimelopez89 jaimelopez89 Nov 26, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks @Jackeyzhe, good catch. You're right: my previous comment overclaimed. A commit's files can span several manifests, and planFiles() reads those in parallel, so files tied on data sequence number can come back in any order. In my earlier PR, a stable sort couldn't recover an order that the input never had.

I'm also proposing some fixes in 3aff801:

  • Tie-break on file name. A tiering round writes each bucket with a single rolling writer. Iceberg's OutputFileFactory names those files <partitionId>-<taskId>-<operationId>-<counter>, with a zero-padded counter that grows as the writer rolls, so the file name restores write order regardless of manifest layout. Only the name is compared, not the full location, so a hashing location provider doesn't affect it.
  • Legacy tables (with __offset) are now ordered by the lower bound of __offset, as you suggested in the other thread. That order is exact per bucket, including after compaction, since IcebergRewriteDataFiles packs runs of files already sorted by __offset. The planner requests __offset stats for this, and falls back to sequence-number order if any file lacks them.
  • Tests: IcebergSplitPlannerTest now shuffles the planned splits to stand in for arbitrary planFiles() order, and checks both one commit of rolled files and a legacy table whose files come from separate writers. Both tests fail without the fix. The existing same-commit case also uses one rolling writer now, which matches what tiering actually does.

For **log tables** with a bucket key, Flink Union Read returns each bucket's lake data before its Fluss log data, and the hand-off between them has no gap and no overlap. Rows from different buckets are not ordered relative to each other. Whether rows inside the lake data stay in write order depends on the lake format:

- **Paimon**: the lake data of a bucket is read in write order.
- **Iceberg**: write order is best effort. A bucket's data files are read in commit order, which is write order until the table is compacted. Compaction rewrites files without keeping row order, so rows rewritten by a single compaction run can come out of order. Data compacted in earlier runs is read before data compacted in later runs, and data written since the last compaction is read in write order.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think the compaction limitation is broader than disorder within a single rewrite. IcebergLakeCommitter commits newly written files before committing the rewrite; rewritten older files can therefore receive a later data sequence number and be read after newer rows. Could we clarify this in the docs, or use __offset ordering for legacy tables as discussed in #5077?

@jaimelopez89 jaimelopez89 Nov 26, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks again @Jackeyzhe, this one is a real bug in my proposed solution IMO and you described really well. commitRewrite didn't (then) set a data sequence number, so the rewrite, which is committed after the round's appends, gave compacted older rows a newer sequence number than the rows appended in that same round. These rows were read after them, so it contradicted the doc as written.

Took the liberty to propose some fixes, based on your diagnosis, in 3aff801:

  • Committer: IcebergLakeCommitter now commits the rewrite with rewriteFiles.dataSequenceNumber(...) set to the sequence number of the snapshot the rewrite was planned from, the same thing Spark's rewrite does by default (use-starting-sequence-number). Compacted rows now sort before anything tiered after the compaction was planned. Equality deletes committed since then still apply to the rewritten files, because they carry a higher sequence number.
  • Legacy tables: these are ordered by __offset (see the other thread), as you suggested, so read order on them is exact even after compaction.
  • Docs: union-read.md now says that legacy tables keep write order after compaction. For clean tables it says that rows rewritten by a compaction run can be out of order among themselves and relative to files the run didn't rewrite, and that compacted data is still read before later-tiered data. Making compaction itself preserve order on clean tables is a separate change, and I'd rather track it as a follow-up.
  • Test: IcebergLakeCommitterTest#testRewriteKeepsSequenceNumberOfStartingSnapshot commits a round with new data plus a rewrite, and checks that the rewritten files keep the starting snapshot's sequence number, below the new data's. It fails without the fix.

…d compaction

Address review feedback on the Iceberg read order:

- Files of one commit share a data sequence number, and planFiles() may
  list them in any order once a commit spans several manifests. Break the
  tie on the file name: a tiering round writes each bucket with a single
  rolling writer, whose file names differ only in a zero-padded counter.
- Legacy tables (created before FIP-27) carry __offset. Order their files
  by the lower bound of __offset, which is exact per bucket, also after
  compaction.
- IcebergLakeCommitter committed a rewrite after the round's data, so
  compacted older rows got a newer sequence number and were read after
  newer rows. Commit the rewrite with the sequence number of the snapshot
  it was planned from, as Spark's rewrite does.

Update the union read docs to match.

Reported by @Jackeyzhe in review.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[lake/iceberg] Union read returns a bucket's lake data out of write order

2 participants