[Bug] Fix concurrent LocalTableQuery lookups returning null across files - #9500
Open
zhang-arvin wants to merge 1 commit into
Open
[Bug] Fix concurrent LocalTableQuery lookups returning null across files#9500zhang-arvin wants to merge 1 commit into
zhang-arvin wants to merge 1 commit into
Conversation
…eadLocal for SliceComparator readers
There was a problem hiding this comment.
Pull request overview
This PR fixes a concurrency race in RowCompactedSerializer.SliceComparator that could cause incorrect key comparisons during concurrent LocalTableQuery lookups, leading to intermittent false misses (null results) across different files/partitions.
Changes:
- Replaced shared mutable
RowReaderinstances inSliceComparatorwith per-threadThreadLocal<RowReader>instances. - Updated
compare()to use thread-local readers to avoid cross-threadpointTo()interleaving.
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+738
to
+746
| private final int headerSizeInBytes; | ||
| private final ThreadLocal<RowReader> reader1; | ||
| private final ThreadLocal<RowReader> reader2; | ||
| private final FieldReader[] fieldReaders; | ||
|
|
||
| public SliceComparator(RowType rowType) { | ||
| int bitSetInBytes = calculateBitSetInBytes(rowType.getFieldCount()); | ||
| this.reader1 = new RowReader(bitSetInBytes); | ||
| this.reader2 = new RowReader(bitSetInBytes); | ||
| this.headerSizeInBytes = calculateBitSetInBytes(rowType.getFieldCount()); | ||
| this.reader1 = ThreadLocal.withInitial(() -> new RowReader(headerSizeInBytes)); | ||
| this.reader2 = ThreadLocal.withInitial(() -> new RowReader(headerSizeInBytes)); |
Comment on lines
754
to
+758
| public int compare(MemorySlice slice1, MemorySlice slice2) { | ||
| reader1.pointTo(slice1.segment(), slice1.offset()); | ||
| reader2.pointTo(slice2.segment(), slice2.offset()); | ||
| RowReader r1 = reader1.get(); | ||
| RowReader r2 = reader2.get(); | ||
| r1.pointTo(slice1.segment(), slice1.offset()); | ||
| r2.pointTo(slice2.segment(), slice2.offset()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changes were proposed in this pull request?
Fix #9483: Concurrent
LocalTableQuerylookups across files can return null because theSliceComparatorinRowCompactedSerializeruses shared mutablereader1/reader2fields across all threads. When two threads concurrently callcompare(), thepointTo()calls interleave, producing incorrect comparison results and causing binary search to return false negatives (null).Root Cause
LocalTableQuerycreates a singleLookupStoreFactorywith aRowCompactedSerializer.createSliceComparator()that is shared across allLookupLevelsinstances (all partitions and buckets). TheSliceComparatorhas mutablereader1andreader2fields that are repositioned viapointTo()on eachcompare()call, creating a race condition.Fix
Convert
reader1andreader2inSliceComparatortoThreadLocal<RowReader>instances, giving each thread its own independent reader pair.How was this patch tested?
paimon-commonmodule compiles successfullyLookupLevelsTest19 tests passPrimaryKeySimpleTableTest133 tests pass