tomerr90 opened a new pull request, #19334: URL: https://github.com/apache/iceberg/pull/19334
## Summary `BaseEqualityDeltaWriter` keeps an in-memory map from every equality key written in the current commit to the data file position of its latest row, so that a later write or delete of the same key in the same commit becomes a position delete (an equality delete can't remove a row with the same data sequence number). For upsert workloads this map is the dominant heap cost of the writer and is bounded only by the number of distinct keys per commit, which has been reported as an OOM risk several times (#5955, #10030, #15859). Earlier attempts to spill it to RocksDB (#2680, #4298) stalled, partly over adding a native dependency to core. This PR keeps the map in memory but makes it compact. `InsertedRowMap` is a package-private open-addressing table that stores, per entry, the Avro binary encoding of the key (one `byte[]`, produced by Iceberg's `InternalWriter`), its hash, and the file ordinal and row position packed into a `long`. Data file paths are stored once per file. No new dependencies; the change is internal to `BaseTaskWriter`. ## Memory (primary goal) Retained heap per entry at 10M keys, measured both with a post-GC heap delta and with JOL (the two agree within ~2 B). JMH can't measure retained memory, so the harness, run instructions and raw output are in [this gist](https://gist.github.com/tomerr90/0c47ffae1a55d046dd8e098d88fa620c): | Equality key | `StructLikeMap` (before) | `InsertedRowMap` (after) | Reduction | |---|---|---|---| | UUID string | 232 B | 88 B | 2.6x | | string (20 chars) + timestamp | 240 B | 80 B | 3.0x | | random 64-bit long | 176 B | 64 B | 2.8x | Where the 240 B per entry goes today (JOL, string + timestamp key): `HashMap$Node` 32, `StructLikeWrapper` 32, boxed `Integer` hash 16, `StructCopy` 16, `Object[]` 24, `String` 24 + its `byte[]` 40, boxed `Long` 24, `PathOffset` 24, plus ~8 for the table slot, i.e. 9 objects for a ~28-byte key and an 8-byte position. After this change each entry is one ~48 B `byte[]` plus ~33 B of primitive table arrays. With `HadoopFileIO`, `OutputFile.location()` returns a new `String` per call, so before this change every entry also retained its own copy of the data file path; that cost is gone as well. ## Speed Not a goal of the change, but it shouldn't regress. JMH (`InsertedRowMapBenchmark`, included) runs 4M upserts, each a `remove` then a `put` as the upsert path does, with rows that return a new `String` per `get()` like Flink's `RowDataWrapper`. 3 forks x 10 iterations, JDK 21, Apple M5 Pro: | Key, distinct keys | `StructLikeMap` (before) | `InsertedRowMap` (after) | |---|---|---| | string + timestamp, 4M (every key new) | 1177 ± 26 ms (294 ns/upsert) | 1114 ± 12 ms (278 ns/upsert) | | string + timestamp, 1M (keys repeat) | 1904 ± 27 ms (476 ns/upsert) | 974 ± 20 ms (244 ns/upsert) | | long, 4M (every key new) | 702 ± 23 ms (175 ns/upsert) | 673 ± 6 ms (168 ns/upsert) | | long, 1M (keys repeat) | 1021 ± 87 ms (255 ns/upsert) | 765 ± 15 ms (191 ns/upsert) | When every key is new the two are within a few percent; when keys repeat, `StructLikeMap` compares keys field by field through its struct comparator while `InsertedRowMap` compares two byte arrays, which is where the 1.3-2x comes from. The benchmark can also be run with the JMH Benchmarks GitHub action; I'm happy to move it to a follow-up if reviewers prefer a smaller diff. <details> <summary>Raw JMH output</summary> ``` Benchmark (distinctKeys) (keyShape) Mode Cnt Score Error Units InsertedRowMapBenchmark.insertedRowMap 4000000 string_timestamp ss 30 1113.533 ± 11.807 ms/op InsertedRowMapBenchmark.insertedRowMap 4000000 long ss 30 673.305 ± 6.478 ms/op InsertedRowMapBenchmark.insertedRowMap 1000000 string_timestamp ss 30 974.389 ± 19.987 ms/op InsertedRowMapBenchmark.insertedRowMap 1000000 long ss 30 764.598 ± 15.197 ms/op InsertedRowMapBenchmark.structLikeMap 4000000 string_timestamp ss 30 1176.518 ± 25.540 ms/op InsertedRowMapBenchmark.structLikeMap 4000000 long ss 30 701.935 ± 23.170 ms/op InsertedRowMapBenchmark.structLikeMap 1000000 string_timestamp ss 30 1903.863 ± 26.967 ms/op InsertedRowMapBenchmark.structLikeMap 1000000 long ss 30 1021.317 ± 87.313 ms/op ``` </details> <details> <summary>Raw memory measurement output (10M keys)</summary> ``` before uuid-string n=10,000,000 retained=2,216 MiB 232.5 B/entry before string20+timestamp n=10,000,000 retained=2,294 MiB 240.6 B/entry before long n=10,000,000 retained=1,682 MiB 176.4 B/entry after uuid-string n=10,000,000 retained=839 MiB 88.0 B/entry after string20+timestamp n=10,000,000 retained=762 MiB 79.9 B/entry after long n=10,000,000 retained=608 MiB 63.8 B/entry ``` </details> ## Behavior changes Keys are now equal when their Avro encodings are equal, which requires equality field values in Iceberg's internal representation. This is documented on `asStructLike` / `asStructLikeKey`. Flink's `RowDataWrapper` already provides internal values; the generic test writer now wraps records with `InternalRecordWrapper`. Compared to `StructLikeMap`: - Strings must be `String` or `Utf8`; dates, times, timestamps and fixed values must be in their internal form (`Integer`, `Long`, `ByteBuffer`) rather than `LocalDate`, `LocalDateTime` or `byte[]`. - Decimals must already have the column's scale (the data writers require this too). - Strings with unpaired surrogates are compared after UTF-8 encoding, where they become `?`, which is also how the data files store them. - `NaN` values with different bit patterns are distinct keys (floating point columns can't be identifier fields). ### New limits The file ordinal and row position are packed into one `long` (23 + 40 bits, sign bit unused, so no handle can equal the "absent" marker). This makes each writer support up to 2^23 (~8.4M) data files per commit and 2^40 - 1 (~1.1 trillion) rows per data file. The previous map stored the path and a full `long` position, so it had neither limit. Both are checked: exceeding them makes `put` throw instead of producing a wrong position delete. Neither limit is reachable in practice. Writers roll files at the target file size, so even at an extreme 1 bit per row, 2^40 rows would be a ~137 GB data file; and 8.4M rolled files from one partition writer in a single commit would be petabytes per partition per checkpoint at normal file sizes. Keeping the two values in separate arrays would remove the limits at a cost of ~7 B per entry (about a tenth of the saving), which didn't seem worth it. ## Test plan - `TestInsertedRowMap#matchesStructLikeMap` replays 100k random puts, removes and remove-then-put pairs against `StructLikeMap` for every primitive type, composite and nested keys and null values, and checks every returned handle and the size. It exercises resizing and slot deletion; breaking the deletion shift fails 17 of its cases. - `binaryKeysCompareByContent` checks that buffer position and offset do not affect equality. - `handlesSurviveFileRolls` checks that a handle still resolves to the right path after the writer rolls to a new file. - Existing `TestTaskEqualityDeltaWriter` (42 cases) passes unchanged apart from the internal-representation wrapper. ## Related work #18675 (fix for #18262) turns this map into a shareable `InsertedRowTracker` backed by `StructLikeMap`. The two changes compose: if #18675 lands first, I'm happy to rebase this so the tracker uses `InsertedRowMap` internally. ## AI disclosure This change was developed with AI assistance (Claude Code): design exploration, the implementation, tests and benchmarks were drafted with the tool, then reviewed, measured and verified by me. Areas that deserve the closest look: the backward-shift deletion in `InsertedRowMap#deleteSlot`, and the equality semantics listed under "Behavior changes". cc @mxm @pvary 🤖 Generated with [Claude Code](https://claude.com/claude-code) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
