You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pinot.apache.org by "deepthi912 (via GitHub)" <gi...@apache.org> on 2026/09/01 22:28:38 UTC
[PR] Star-tree offheap build: read dims from sort buffer instead of re-fetching from segment [pinot]
deepthi912 opened a new pull request, #19430:
URL: https://github.com/apache/pinot/pull/19430
## Summary
`OffHeapSingleTreeBuilder.sortAndAggregateSegmentRecords` fills a direct/mmap buffer with every row's dim dict-ids in a sequential pass, sorts docIds by dim tuple, then — today — **closes the buffer before returning the aggregation iterator, and the iterator re-reads the same dim values from the segment forward-index** via `getSegmentRecord(sortedDocIds[i])`. After the sort, those docIds are random with respect to Parquet page layout, so every re-read triggers a full page-context miss: Snappy decompress + RLE decode + fresh `String[]` / `int[]` materialize, for one value consumed.
For external-table segments backed by `parquet_page_index.bin` + on-demand parquet page decode, this dominates preprocess wall clock. On a real cluster today, a 3.3M-row segment spends **~24 minutes** in this method; a JFR taken during a live build shows 77% of samples in `RunLengthBitPackingHybridDecoder.readInt` under the following stack:
```
OffHeapSingleTreeBuilder$1.next
-> BaseSingleTreeBuilder.getSegmentRecord
-> BaseSingleTreeBuilder.getSegmentRecordDimensions
-> PinotSegmentColumnReader.getDictId
-> ParquetForwardIndexReaderV6.getString
-> ensureContextForDocId (page-context miss)
-> DeserializedParquetPageData.deserializeData
-> decodeDictStringPage
-> RunLengthBitPackingHybridDecoder.readInt
```
## Fix
Extend the buffer's lifetime to iterator exhaustion, and add a `BaseSingleTreeBuilder#getSegmentRecordWithBufferDims(int, PinotDataBuffer)` helper that reads dim dict-ids back from the buffer (already in memory) instead of re-fetching from the segment. Metrics still read from the segment.
For a star-tree with N dims and M non-COUNT metrics, random column reads per aggregated row drop from N+M to M — ~67% fewer random reads for a common (N=4, M=2) config.
Buffer ownership transfers to the iterator: it calls `closeSegmentRecordBuffer` on the terminal `next()`. If an exception is thrown before the iterator is returned (e.g. during fill or sort), the outer `finally` still cleans up via the same helper, so no leak.
## What is NOT changed
- `OnHeapSingleTreeBuilder` — its own scan is already sequential, no change.
- `BaseSingleTreeBuilder#getSegmentRecord(int)` — kept intact for other callers (tests, subclasses).
- Public/SPI API, config, on-disk formats — nothing changes.
- Star-tree records emitted by the iterator — same content, same order, same aggregation semantics.
## Test plan
- [ ] Existing `pinot-segment-local` module tests (`./mvnw -pl pinot-segment-local test`) pass.
- [ ] New targeted unit test asserting `getSegmentRecordWithBufferDims(docId, buffer)` produces a `Record` equal to `getSegmentRecord(docId)` for the same docId, over a hand-built segment covering all dim dict-id combinations.
- [ ] Integration test that exercises `MultipleTreesBuilder.build()` end-to-end on the OffHeap path and asserts the resulting star-tree matches a snapshot from before this change.
- [ ] Manual benchmark: rebuild a 3.3M-row external-table segment on a StarTree Cloud dev cluster and confirm preprocess wall-clock drops from ~24 min to ~8–14 min per segment.
## Measurement context
The 24-minute-per-segment baseline is measured on a StarTree Cloud dev cluster with `uc_tvi_insights_v3_OFFLINE` (external table, parquet-backed, one row group per file, 30 leaf columns, ~1500 pages per 6-col Sub-phase-C walk). JFR + `DeserCacheMiss=482M` counter attached to the ticket confirm this is CPU-bound decode, not S3/IO. Full JFR + call-stack analysis available on request.
--
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: commits-unsubscribe@pinot.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org
Re: [PR] Star-tree offheap build: read dims from sort buffer instead of re-fetching from segment [pinot]
Posted by "Jackie-Jiang (via GitHub)" <gi...@apache.org>.
Jackie-Jiang merged PR #19430:
URL: https://github.com/apache/pinot/pull/19430
--
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: commits-unsubscribe@pinot.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org
Re: [PR] Star-tree offheap build: read dims from sort buffer instead of re-fetching from segment [pinot]
Posted by "codecov-commenter (via GitHub)" <gi...@apache.org>.
codecov-commenter commented on PR #19430:
URL: https://github.com/apache/pinot/pull/19430#issuecomment-5501745923
## [Codecov](https://app.codecov.io/gh/apache/pinot/pull/19430?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
:x: Patch coverage is `82.05128%` with `7 lines` in your changes missing coverage. Please review.
:white_check_mark: Project coverage is 57.69%. Comparing base ([`9fd510f`](https://app.codecov.io/gh/apache/pinot/commit/9fd510fe81f00ffdd8c3bceb07dfebcae3fd5aa9?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)) to head ([`6304daf`](https://app.codecov.io/gh/apache/pinot/commit/6304daf1ec6ffbcfc04f3dd9735d0b1d503d8636?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)).
| [Files with missing lines](https://app.codecov.io/gh/apache/pinot/pull/19430?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Patch % | Lines |
|---|---|---|
| [.../startree/v2/builder/OffHeapSingleTreeBuilder.java](https://app.codecov.io/gh/apache/pinot/pull/19430?src=pr&el=tree&filepath=pinot-segment-local%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Fpinot%2Fsegment%2Flocal%2Fstartree%2Fv2%2Fbuilder%2FOffHeapSingleTreeBuilder.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-cGlub3Qtc2VnbWVudC1sb2NhbC9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcGlub3Qvc2VnbWVudC9sb2NhbC9zdGFydHJlZS92Mi9idWlsZGVyL09mZkhlYXBTaW5nbGVUcmVlQnVpbGRlci5qYXZh) | 75.86% | [5 Missing and 2 partials :warning: ](https://app.codecov.io/gh/apache/pinot/pull/19430?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
> :exclamation: There is a different number of reports uploaded between BASE (9fd510f) and HEAD (6304daf). Click for more details.
>
> <details><summary>HEAD has 24 uploads less than BASE</summary>
>
>| Flag | BASE (9fd510f) | HEAD (6304daf) |
>|------|------|------|
>|lane-a|2|0|
>|integration|4|0|
>|java-25|6|1|
>|integration1|2|0|
>|temurin|6|1|
>|lane-b|2|0|
>|unittests|2|1|
>|integration2|2|0|
>|unittests2|1|0|
></details>
<details><summary>Additional details and impacted files</summary>
```diff
@@ Coverage Diff @@
## master #19430 +/- ##
============================================
- Coverage 67.59% 57.69% -9.91%
+ Complexity 1430 1 -1429
============================================
Files 3487 2684 -803
Lines 224304 164112 -60192
Branches 35411 26652 -8759
============================================
- Hits 151620 94678 -56942
- Misses 60659 61430 +771
+ Partials 12025 8004 -4021
```
| [Flag](https://app.codecov.io/gh/apache/pinot/pull/19430/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Coverage Δ | |
|---|---|---|
| [integration](https://app.codecov.io/gh/apache/pinot/pull/19430/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `?` | |
| [integration1](https://app.codecov.io/gh/apache/pinot/pull/19430/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `?` | |
| [integration2](https://app.codecov.io/gh/apache/pinot/pull/19430/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `?` | |
| [java-25](https://app.codecov.io/gh/apache/pinot/pull/19430/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `57.69% <82.05%> (-9.91%)` | :arrow_down: |
| [lane-a](https://app.codecov.io/gh/apache/pinot/pull/19430/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `?` | |
| [lane-b](https://app.codecov.io/gh/apache/pinot/pull/19430/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `?` | |
| [temurin](https://app.codecov.io/gh/apache/pinot/pull/19430/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `57.69% <82.05%> (-9.91%)` | :arrow_down: |
| [unittests](https://app.codecov.io/gh/apache/pinot/pull/19430/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `57.69% <82.05%> (-9.91%)` | :arrow_down: |
| [unittests1](https://app.codecov.io/gh/apache/pinot/pull/19430/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `57.69% <82.05%> (+<0.01%)` | :arrow_up: |
| [unittests2](https://app.codecov.io/gh/apache/pinot/pull/19430/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `?` | |
Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#carryforward-flags-in-the-pull-request-comment) to find out more.
</details>
[:umbrella: View full report in Codecov by Harness](https://app.codecov.io/gh/apache/pinot/pull/19430?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
<details><summary> :rocket: New features to boost your workflow: </summary>
- :package: [JS Bundle Analysis](https://docs.codecov.com/docs/javascript-bundle-analysis): Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
</details>
--
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: commits-unsubscribe@pinot.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org
Re: [PR] Star-tree offheap build: read dims from sort buffer instead of re-fetching from segment [pinot]
Posted by "Jackie-Jiang (via GitHub)" <gi...@apache.org>.
Jackie-Jiang commented on code in PR #19430:
URL: https://github.com/apache/pinot/pull/19430#discussion_r3984992107
##########
pinot-segment-local/src/test/java/org/apache/pinot/segment/local/startree/v2/builder/OffHeapSingleTreeBuilderTest.java:
##########
@@ -53,4 +96,207 @@ public void testVariableSizeRecordOffsets() {
assertEquals(offsets.getStartOffset(3), Integer.MAX_VALUE + 456L);
assertEquals(offsets.getEndOffset(), Integer.MAX_VALUE + 456L + 789L);
}
+
+ /// Builds a star-tree with the off-heap builder and asserts the intermediate segment record file
+ /// is cleaned up after build. Exercises the full sortAndAggregateSegmentRecords → iterator → close
+ /// lifecycle, including buffer allocation, sequential fill in Sub-phase A, sort in Sub-phase B,
+ /// and dim-from-buffer reads in Sub-phase C.
+ @Test
+ public void testBuildCleansUpSegmentRecordFile()
+ throws Exception {
+ buildTestSegment();
+
+ List<StarTreeV2BuilderConfig> builderConfigs = createBuilderConfigs();
+ File segmentDir = INDEX_DIR.listFiles()[0];
+
+ try (MultipleTreesBuilder builder = new MultipleTreesBuilder(builderConfigs, segmentDir,
+ MultipleTreesBuilder.BuildMode.OFF_HEAP)) {
+ builder.build();
+ }
+
+ // OffHeapSingleTreeBuilder writes an intermediate segment.record file during Sub-phase A/B/C.
+ // The dim-buffer optimization keeps that file alive through iterator exhaustion; close() must
+ // release the buffer and delete the file.
+ File segmentRecordFile = findSegmentRecordFile(segmentDir);
+ assertFalse(segmentRecordFile != null && segmentRecordFile.exists(),
+ "segment.record file should be deleted after build: " + segmentRecordFile);
Review Comment:
[P2] Verify buffer release directly
The fixture allocates a 40-byte DIRECT buffer, so `segment.record` is never created and this assertion cannot detect a leak. I confirmed that all five tests still pass with `releaseSegmentRecordBuffer()` completely disabled. The close-without-build test also never constructs `OffHeapSingleTreeBuilder`.
Please add direct builder tests that assert buffer count/usage returns to baseline after iterator exhaustion and after closing an undrained iterator.
##########
pinot-segment-local/src/test/java/org/apache/pinot/segment/local/startree/v2/builder/OffHeapSingleTreeBuilderTest.java:
##########
@@ -53,4 +96,207 @@ public void testVariableSizeRecordOffsets() {
assertEquals(offsets.getStartOffset(3), Integer.MAX_VALUE + 456L);
assertEquals(offsets.getEndOffset(), Integer.MAX_VALUE + 456L + 789L);
}
+
+ /// Builds a star-tree with the off-heap builder and asserts the intermediate segment record file
+ /// is cleaned up after build. Exercises the full sortAndAggregateSegmentRecords → iterator → close
+ /// lifecycle, including buffer allocation, sequential fill in Sub-phase A, sort in Sub-phase B,
+ /// and dim-from-buffer reads in Sub-phase C.
+ @Test
+ public void testBuildCleansUpSegmentRecordFile()
+ throws Exception {
+ buildTestSegment();
+
+ List<StarTreeV2BuilderConfig> builderConfigs = createBuilderConfigs();
+ File segmentDir = INDEX_DIR.listFiles()[0];
+
+ try (MultipleTreesBuilder builder = new MultipleTreesBuilder(builderConfigs, segmentDir,
+ MultipleTreesBuilder.BuildMode.OFF_HEAP)) {
+ builder.build();
+ }
+
+ // OffHeapSingleTreeBuilder writes an intermediate segment.record file during Sub-phase A/B/C.
+ // The dim-buffer optimization keeps that file alive through iterator exhaustion; close() must
+ // release the buffer and delete the file.
+ File segmentRecordFile = findSegmentRecordFile(segmentDir);
+ assertFalse(segmentRecordFile != null && segmentRecordFile.exists(),
+ "segment.record file should be deleted after build: " + segmentRecordFile);
+ }
+
+ /// close() before build() must not throw and must leave no leftover segment record file.
+ @Test
+ public void testCloseWithoutBuildDoesNotThrow()
+ throws Exception {
+ buildTestSegment();
+
+ List<StarTreeV2BuilderConfig> builderConfigs = createBuilderConfigs();
+ File segmentDir = INDEX_DIR.listFiles()[0];
+
+ // Construct and immediately close — no build().
+ MultipleTreesBuilder builder = new MultipleTreesBuilder(builderConfigs, segmentDir,
+ MultipleTreesBuilder.BuildMode.OFF_HEAP);
+ builder.close();
+
+ File segmentRecordFile = findSegmentRecordFile(segmentDir);
+ assertFalse(segmentRecordFile != null && segmentRecordFile.exists(),
+ "segment.record file should not exist when build() was never called: " + segmentRecordFile);
+ }
+
+ /// Builds the same segment twice — once with OFF_HEAP and once with ON_HEAP — then compares the
+ /// resulting star-trees by grouping star-tree records by dim tuple and summing the aggregate
+ /// column. Directly regressions-tests dim-read parity: if the buffer read returned wrong bytes,
+ /// OFF_HEAP would aggregate metrics under the wrong dim tuples and the maps would diverge.
+ @Test
+ public void testOffHeapProducesSameStarTreeAsOnHeap()
+ throws Exception {
+ buildTestSegment();
+ File sourceSegmentDir = INDEX_DIR.listFiles()[0];
+ List<StarTreeV2BuilderConfig> builderConfigs = createBuilderConfigs();
+
+ File offHeapDir = new File(TEMP_DIR, "offHeapCopy");
+ File onHeapDir = new File(TEMP_DIR, "onHeapCopy");
+ FileUtils.copyDirectory(sourceSegmentDir, offHeapDir);
+ FileUtils.copyDirectory(sourceSegmentDir, onHeapDir);
+
+ try (MultipleTreesBuilder builder = new MultipleTreesBuilder(builderConfigs, offHeapDir,
+ MultipleTreesBuilder.BuildMode.OFF_HEAP)) {
+ builder.build();
+ }
+ try (MultipleTreesBuilder builder = new MultipleTreesBuilder(builderConfigs, onHeapDir,
+ MultipleTreesBuilder.BuildMode.ON_HEAP)) {
+ builder.build();
+ }
+
+ Map<String, Long> offHeapByDim = readStarTreeAggregatedByDim(offHeapDir);
+ Map<String, Long> onHeapByDim = readStarTreeAggregatedByDim(onHeapDir);
+ assertEquals(offHeapByDim, onHeapByDim,
+ "OFF_HEAP and ON_HEAP star-trees disagree on aggregate-by-dim: "
+ + "off=" + offHeapByDim + ", on=" + onHeapByDim);
+ }
+
+ /// Iterates every star-tree record, resolves dim dict-ids to values, and returns a
+ /// `(stringCol|intCol) -> sum(longCol)` map. Star nodes (dim value = STAR) fold into their own
+ /// bucket, so the map is a canonical view of the tree independent of internal doc order.
+ private Map<String, Long> readStarTreeAggregatedByDim(File segmentDir)
+ throws Exception {
+ ImmutableSegment segment = ImmutableSegmentLoader.load(segmentDir, ReadMode.mmap);
+ try {
+ StarTreeV2 tree = segment.getStarTrees().get(0);
+ int numDocs = tree.getMetadata().getNumDocs();
+
+ ForwardIndexReader<ForwardIndexReaderContext> stringReader =
+ (ForwardIndexReader<ForwardIndexReaderContext>) tree.getDataSource("stringCol").getForwardIndex();
+ Dictionary stringDict = tree.getDataSource("stringCol").getDictionary();
+ ForwardIndexReader<ForwardIndexReaderContext> intReader =
+ (ForwardIndexReader<ForwardIndexReaderContext>) tree.getDataSource("intCol").getForwardIndex();
+ Dictionary intDict = tree.getDataSource("intCol").getDictionary();
+ // Aggregate columns are keyed by AggregationFunctionColumnPair.toColumnName() ("sum__longCol").
+ ForwardIndexReader<ForwardIndexReaderContext> aggReader =
+ (ForwardIndexReader<ForwardIndexReaderContext>) tree.getDataSource("sum__longCol").getForwardIndex();
+
+ Map<String, Long> byDim = new TreeMap<>();
+ try (ForwardIndexReaderContext stringCtx = stringReader.createContext();
+ ForwardIndexReaderContext intCtx = intReader.createContext();
+ ForwardIndexReaderContext aggCtx = aggReader.createContext()) {
+ for (int docId = 0; docId < numDocs; docId++) {
+ int stringDictId = stringReader.getDictId(docId, stringCtx);
+ int intDictId = intReader.getDictId(docId, intCtx);
+ String stringVal = stringDictId == -1 ? "*" : stringDict.getStringValue(stringDictId);
+ String intVal = intDictId == -1 ? "*" : String.valueOf(intDict.getIntValue(intDictId));
+ byDim.merge(stringVal + "|" + intVal, aggReader.getLong(docId, aggCtx), Long::sum);
+ }
+ }
+ return byDim;
+ } finally {
+ segment.destroy();
+ }
+ }
+
+ private void buildTestSegment()
+ throws Exception {
+ Schema schema = new Schema.SchemaBuilder()
+ .addSingleValueDimension("stringCol", FieldSpec.DataType.STRING)
+ .addSingleValueDimension("intCol", FieldSpec.DataType.INT)
+ .addMetric("longCol", FieldSpec.DataType.LONG)
Review Comment:
[P3] Import `DataType` directly
Please import `FieldSpec.DataType` and use `DataType.STRING`, `DataType.INT`, and `DataType.LONG`, following the repository convention.
--
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: commits-unsubscribe@pinot.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org