Repository navigation
Fix stale aligned field merge in PriorityMergeReader look-ahead #19399
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
ac1c904
a268d3f
df69421
6084fd7
e4bcc9a
304bf2c
0e55257
9f21baf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,13 +18,22 @@ | |
| */ | ||
| package org.apache.iotdb.db.storageengine.dataregion.read.reader.common; | ||
|
|
||
| import org.apache.tsfile.enums.TSDataType; | ||
| import org.apache.tsfile.read.TimeValuePair; | ||
| import org.apache.tsfile.read.common.BatchData; | ||
| import org.apache.tsfile.utils.Binary; | ||
| import org.apache.tsfile.utils.TsPrimitiveType; | ||
| import org.apache.tsfile.utils.TsPrimitiveType.TsBinary; | ||
| import org.apache.tsfile.utils.TsPrimitiveType.TsInt; | ||
| import org.apache.tsfile.utils.TsPrimitiveType.TsLong; | ||
| import org.apache.tsfile.utils.TsPrimitiveType.TsVector; | ||
| import org.junit.Test; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertFalse; | ||
| import static org.junit.Assert.assertTrue; | ||
|
|
||
| public class AlignedPriorityMergeReaderTest { | ||
|
|
||
|
|
@@ -87,4 +96,115 @@ public void test3() { | |
| assertEquals(20, v.getValue().getVector()[1].getInt()); | ||
| assertEquals(30, v.getValue().getVector()[2].getInt()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testAscScanMergesMiddlePriorityReaderAtNextTimestamp() throws IOException { | ||
| assertMiddlePriorityMerge(new AlignedPriorityMergeReader(), 10L, 20L); | ||
| } | ||
|
|
||
| @Test | ||
| public void testDescScanMergesMiddlePriorityReaderAtNextTimestamp() throws IOException { | ||
| assertMiddlePriorityMerge(new AlignedDescPriorityMergeReader(), 20L, 10L); | ||
| } | ||
|
|
||
| private void assertMiddlePriorityMerge(PriorityMergeReader reader, long t1, long t2) | ||
| throws IOException { | ||
| addBatch(reader, 1, t2, batch(t1, vec(null, 100L), t2, vec(null, 200L))); | ||
| addBatch(reader, 2, t2, batch(t2, vec(null, 220L))); | ||
| addBatch(reader, 3, t2, batch(t1, vec(110L, null), t2, vec(120L, null))); | ||
|
|
||
| assertLongRow(reader.nextTimeValuePair(), t1, 110L, 100L); | ||
| assertLongRow(reader.nextTimeValuePair(), t2, 120L, 220L); | ||
| assertFalse(reader.hasNextTimeValuePair()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testAscScanPreservesOlderColumnsWhileMiddleOverridesOverlappingColumn() | ||
| throws IOException { | ||
| AlignedPriorityMergeReader reader = new AlignedPriorityMergeReader(); | ||
| addBatch(reader, 1, 20L, batch(10L, vec(null, 100L, 1000L), 20L, vec(null, 200L, 2000L))); | ||
| addBatch(reader, 2, 20L, batch(20L, vec(null, 220L, null))); | ||
| addBatch(reader, 3, 20L, batch(10L, vec(110L, null, null), 20L, vec(120L, null, null))); | ||
|
|
||
| assertLongRow(reader.nextTimeValuePair(), 10L, 110L, 100L, 1000L); | ||
| assertLongRow(reader.nextTimeValuePair(), 20L, 120L, 220L, 2000L); | ||
| assertFalse(reader.hasNextTimeValuePair()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSkipOlderReaderOnlyWhenNextAlignedRowIsComplete() throws IOException { | ||
| assertFalse( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we also add direct unit tests covering |
||
| AlignedPriorityMergeReader.fillNullValueInAligned( | ||
| new TimeValuePair(1, new TsVector(vec(10L, null, null))), | ||
| new TimeValuePair(1, new TsVector(vec(null, 20L, null))))); | ||
| assertTrue( | ||
| AlignedPriorityMergeReader.fillNullValueInAligned( | ||
| new TimeValuePair(1, new TsVector(vec(10L, null))), | ||
| new TimeValuePair(1, new TsVector(vec(null, 20L))))); | ||
|
|
||
| assertCompleteRowSkip(new AlignedPriorityMergeReader(), 10L, 20L, 30L); | ||
| assertCompleteRowSkip(new AlignedDescPriorityMergeReader(), 30L, 20L, 10L); | ||
| } | ||
|
|
||
| private void assertCompleteRowSkip(PriorityMergeReader reader, long t1, long t2, long t3) | ||
| throws IOException { | ||
| TsBinary oldVal = new TsBinary(new Binary(new byte[] {1})); | ||
| TsBinary emptyVal = new TsBinary(Binary.EMPTY_VALUE); | ||
|
|
||
| BatchData older = new BatchData(TSDataType.VECTOR); | ||
| for (long t : new long[] {t1, t2, t3}) { | ||
| older.putVector(t, new TsPrimitiveType[] {null, oldVal}); | ||
| } | ||
| BatchData newer = new BatchData(TSDataType.VECTOR); | ||
| newer.putVector(t1, new TsPrimitiveType[] {new TsLong(10L), emptyVal}); | ||
| newer.putVector(t2, new TsPrimitiveType[] {new TsLong(20L), new TsBinary()}); | ||
| newer.putVector(t3, new TsPrimitiveType[] {new TsLong(30L), emptyVal}); | ||
|
|
||
| addBatch(reader, 1, t3, older); | ||
| addBatch(reader, 2, t3, newer); | ||
|
|
||
| // topNext (t2) has a null-valued TsBinary, so older stays in the heap; at t2, topNext (t3) is | ||
| // complete, so older is skipped and closed immediately. | ||
| assertEquals(emptyVal, reader.nextTimeValuePair().getValue().getVector()[1]); | ||
| assertEquals(2, reader.heap.size()); | ||
| assertEquals(oldVal, reader.nextTimeValuePair().getValue().getVector()[1]); | ||
| assertEquals(1, reader.heap.size()); | ||
| assertEquals(emptyVal, reader.nextTimeValuePair().getValue().getVector()[1]); | ||
| assertFalse(reader.hasNextTimeValuePair()); | ||
| } | ||
|
|
||
| private static void addBatch(PriorityMergeReader reader, int prio, long endTime, BatchData data) | ||
| throws IOException { | ||
| reader.addReader( | ||
| data.getBatchDataIterator(), new MergeReaderPriority(prio, 1, 0, false), endTime); | ||
| } | ||
|
|
||
| private static BatchData batch(long t1, TsPrimitiveType[] v1) { | ||
| BatchData b = new BatchData(TSDataType.VECTOR); | ||
| b.putVector(t1, v1); | ||
| return b; | ||
| } | ||
|
|
||
| private static BatchData batch(long t1, TsPrimitiveType[] v1, long t2, TsPrimitiveType[] v2) { | ||
| BatchData b = batch(t1, v1); | ||
| b.putVector(t2, v2); | ||
| return b; | ||
| } | ||
|
|
||
| private static TsPrimitiveType[] vec(Long... values) { | ||
| TsPrimitiveType[] v = new TsPrimitiveType[values.length]; | ||
| for (int i = 0; i < values.length; i++) { | ||
| if (values[i] != null) { | ||
| v[i] = new TsLong(values[i]); | ||
| } | ||
| } | ||
| return v; | ||
| } | ||
|
|
||
| private static void assertLongRow(TimeValuePair row, long expectedTime, long... expectedValues) { | ||
| assertEquals(expectedTime, row.getTimestamp()); | ||
| for (int i = 0; i < expectedValues.length; i++) { | ||
| assertEquals(expectedValues[i], row.getValue().getVector()[i].getLong()); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a performance concern here for wide rows. If only the last field is null, hasNoNullFields() scans the entire vector just to determine that the lower-priority point must stay in the heap. fillNullValueInAligned() then scans the row again when the points are merged. In an in-memory microbenchmark with 256 fields and two readers whose timestamps fully overlapped, this case took about 210 ns per output row, compared with 130 ns with look-ahead skipping disabled. Could we track whether the row is complete while building the vector and check that flag here? That would preserve the fast path for complete rows while avoiding the extra scan.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes total sense, thanks for benchmarking that! I removed hasNoNullFields() in 304bf2c and instead tracked completeness while building each row via a small AlignedPointReader interface across the TsBlock and AlignedTVList iterators (since TsVector is in tsfile). PriorityMergeReader now just checks that cached boolean in O(1), and all unit tests + the table IT pass locally. Ready for another look whenever you get a chance!