lvyanquan opened a new issue, #11404:
URL: https://github.com/apache/paimon/issues/11404
### Paimon version
Built from `master` at commit `88792ed0d15b6aec47e4bdef997fb3971914cecb`
(`2.2-SNAPSHOT`).
The affected feature (Flink batch `postpone.merge-on-read`) was introduced
by #8849, merge commit `930fdfaafc5bc4c2d6f3ae4adc56bcafb679a9f4`. The code
involved (`BinaryMergeIterator`, `PostponeMergeOperator`) is unchanged since
then.
### Compute Engine
Flink 1.20.1, batch mode, Flink SQL (`paimon-flink-common`). The bug is in
Paimon Core / Flink module only, no engine-specific behavior is required.
### Minimal reproduce step
Any batch read of a postpone bucket table with `postpone.merge-on-read =
'true'` fails with NPE once the operator-side carrier sort buffer spills.
**1. Create a postpone bucket table and write postpone data**
```sql
CREATE TABLE t (
k INT,
v STRING,
seq BIGINT,
PRIMARY KEY (k) NOT ENFORCED
) WITH (
'bucket' = '-2',
'sequence.field' = 'seq',
'postpone.default-bucket-num' = '4',
-- small target split size so that many small files are produced
'source.split.target-size' = '1 B'
);
-- seed data, then insert with per-bucket postpone writes
INSERT INTO t VALUES (1, 'a', 1), (2, 'b', 1);
SET 'execution.runtime-mode' = 'batch';
INSERT INTO t /*+ OPTIONS('postpone.batch-write-fixed-bucket' = 'false') */
VALUES (1, 'c', 2), (2, 'd', 2), (3, 'e', 2);
```
**2. Read with merge-on-read enabled and a small carrier sort buffer**
```sql
SET 'execution.runtime-mode' = 'batch';
SELECT * FROM t /*+ OPTIONS(
'postpone.merge-on-read' = 'true',
-- force the carrier sort buffer to spill: minimum is 3 * 64kb pages
'sort-spill-buffer-size' = '192 kb'
) */;
```
**3. Result**
```
java.lang.NullPointerException
at
org.apache.paimon.data.serializer.BinaryRowSerializer.deserialize(BinaryRowSerializer.java:86)
at
org.apache.paimon.disk.ChannelReaderInputViewIterator.next(ChannelReaderInputViewIterator.java:50)
at
org.apache.paimon.disk.ChannelReaderInputViewIterator.next(ChannelReaderInputViewIterator.java:31)
at
org.apache.paimon.sort.BinaryMergeIterator$HeadStream.noMoreHead(BinaryMergeIterator.java:93)
at
org.apache.paimon.sort.BinaryMergeIterator$HeadStream.access$100(BinaryMergeIterator.java:75)
at
org.apache.paimon.sort.BinaryMergeIterator.next(BinaryMergeIterator.java:60)
at
org.apache.paimon.sort.BinaryExternalSortBuffer$1.next(BinaryExternalSortBuffer.java:241)
at
org.apache.paimon.sort.BinaryExternalSortBuffer$1.next(BinaryExternalSortBuffer.java:236)
at
org.apache.paimon.sort.BinaryExternalSortBuffer$1.next(BinaryExternalSortBuffer.java:232)
at
org.apache.paimon.flink.source.PostponeMergeOperator$SortedCarriers.hasNext(PostponeMergeOperator.java:258)
at
org.apache.paimon.flink.source.PostponeMergeOperator$SortedCarriers.access$100(PostponeMergeOperator.java:245)
at
org.apache.paimon.flink.source.PostponeMergeOperator.mergeBucket(PostponeMergeOperator.java:190)
at
org.apache.paimon.flink.source.PostponeMergeOperator.endInput(PostponeMergeOperator.java:147)
...
```
In a streaming job / job with restarts this surfaces as
`java.lang.RuntimeException: Failed to read sorted postpone carriers.` wrapping
the NPE.
Deterministic repros added locally:
- IT: `PostponeBucketTableITCase#testMergeOnReadWithSpilledCarriers` (table
options above, query hint `'sort-spill-buffer-size' = '192 kb'`).
- Core unit test: poll a spilled `BinaryExternalSortBuffer#sortedIterator()`
again after it has returned `null` once, e.g. 20,000 rows into a 192kb buffer;
the second post-EOF `next()` throws NPE. With a non-spilling buffer the same
sequence is fine.
### What doesn't meet your expectations?
The query should return the merged view. Instead it fails
non-deterministically with NPE, and the failure depends on data volume:
- With no spill (carrier rows fit in `sort-spill-buffer-size`, default `64
mb`), `BinaryExternalSortBuffer#sortedIterator()` returns the in-memory
iterator and everything works — this is why existing tests with tiny data do
not catch it.
- Once spilled, the iterator is backed by `BinaryMergeIterator`, which
**violates the `MutableObjectIterator` contract** (`@return The filled object
or null if the iterator is exhausted`): after returning `null` once, the next
call throws NPE instead of returning `null` again.
**Root cause** (open source `master`):
1. `MergeFileSplitRead#createPostponeMergeReader` eagerly drains the
postpone records into a sort buffer while constructing the bucket merge reader,
so the `postponeRecords` iterator passed to `PostponeMergeOperator` is usually
exhausted inside `read.createBucketMergeReader(...)`.
2. To detect leftover carriers of the same bucket,
`PostponeMergeOperator#mergeBucket` then probes `carriers.hasNext()` once more
(line ~190). For the last bucket in the sorted stream this is a second poll
after EOF.
3. `SortedCarriers#hasNext()` keeps no `exhausted` flag, so it calls
`iterator.next(reuse)` again after `null` was already returned.
4. With spill, `BinaryMergeIterator#next()` does:
```java
if (currHead != null) {
if (currHead.noMoreHead()) {
this.heap.poll();
// currHead is NOT reset: it still references the evicted
HeadStream
} else {
this.heap.adjustTop();
}
}
```
After `heap.poll()`, `currHead` still points at the evicted `HeadStream`,
whose `head` field is `null`. The next `next()` call therefore calls
`currHead.noMoreHead()` again, i.e. `iterator.next(null)`, and
`BinaryRowSerializer#deserialize` starts with `reuse.getSegments()` → NPE.
Trigger conditions, all required: carrier data exceeds
`sort-spill-buffer-size` **and** the last bucket of the sorted stream is polled
twice after EOF (which is exactly what `mergeBucket` does). This explains why
the failure is intermittent in production and dependent on the amount of
postpone data per subtask.
### Anything else?
**Workaround (mitigation, not a fix):** increase `sort-spill-buffer-size` so
the carriers stay in memory, e.g.
```sql
SET 'table.optimizer.dynamic-options' = ...; -- or a query hint:
SELECT * FROM t /*+ OPTIONS(
'postpone.merge-on-read' = 'true',
'sort-spill-buffer-size' = '512 mb'
) */;
```
With no spill the defective `BinaryMergeIterator` path is not used. This is
data-size dependent and only avoids the symptom; large-volume subtasks can
still spill.
**Suggested fix:**
- `BinaryMergeIterator#next()`: reset `currHead = null;` right after
`this.heap.poll();`.
- `PostponeMergeOperator.SortedCarriers#hasNext()`: add an `exhausted` flag
so the underlying iterator is never polled after it returned `null`.
- Add a contract test for `MutableObjectIterator` implementations (polling
several times after EOF must keep returning `null`); existing sort-buffer tests
stop exactly at the last row and never poll afterwards.
I can submit a PR with the fix and the two tests above.
### Are you willing to submit a PR?
- [x] I'm willing to submit a PR!
--
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]