xiangfu0 commented on code in PR #20441:
URL: https://github.com/apache/pinot/pull/20441#discussion_r4763670280


##########
pinot-plugins/pinot-minion-tasks/pinot-minion-builtin-tasks/src/main/java/org/apache/pinot/plugin/minion/tasks/BaseTaskExecutor.java:
##########
@@ -102,6 +105,46 @@ protected long getSegmentCrc(String tableNameWithType, 
String segmentName) {
     return segmentZKMetadata == null ? -1 : segmentZKMetadata.getCrc();
   }
 
+  /// Returns the stream start/end offsets to keep when the given segment is 
rebuilt under the same name, or `null` if
+  /// the segment has none.
+  ///
+  /// The offsets are not derived from the data, so they are lost unless 
carried over to the rebuilt segment, and a
+  /// segment with LLC segment name needs them in order to be uploaded as a 
new segment (e.g. to another table). The
+  /// ones recorded in the segment are preferred. A segment rebuilt before the 
offsets were carried over no longer has
+  /// them, in which case the ones kept in the segment ZK metadata are used.
+  @Nullable
+  protected SegmentZKPropsConfig getStreamOffsets(String tableNameWithType, 
String segmentName,
+      SegmentMetadata segmentMetadata) {
+    SegmentZKPropsConfig streamOffsets =
+        toStreamOffsets(segmentMetadata.getStartOffset(), 
segmentMetadata.getEndOffset());
+    return streamOffsets != null ? streamOffsets : 
getStreamOffsetsFromZK(tableNameWithType, segmentName);

Review Comment:
   Confirmed, `p0,100` came back as `p0`. Fixed in dcd67cb47b on both sides. 
The segment creator now escapes the commas so the two offsets are stored as 
single values, and that form also reads back in full through a plain 
`getString()`, so a controller on an older version gets the whole offset from a 
segment written by this one. `SegmentMetadataImpl` puts an offset that is 
already split into a list back together, which covers the segments committed 
before this change.
   
   The tests rewrite a segment with `p0,100` and `p0,200` through refresh, 
compaction and purge, and the refresh and purge ones also start from a segment 
in the old list form. One limit to know about: the old writer dropped 
whitespace next to a comma, so for a segment written before this change that 
whitespace can't be restored from the file.
   



##########
pinot-plugins/pinot-minion-tasks/pinot-minion-builtin-tasks/src/main/java/org/apache/pinot/plugin/minion/tasks/purge/PurgeTaskExecutor.java:
##########
@@ -60,8 +61,11 @@ protected SegmentConversionResult convert(PinotTaskConfig 
pinotTaskConfig, File
         recordModifierFactory != null ? 
recordModifierFactory.getRecordModifier(rawTableName) : null;
 
     _eventObserver.notifyProgress(pinotTaskConfig, "Purging segment: " + 
indexDir);
+    // The purger keeps the stream start/end offsets recorded in the segment. 
A segment rebuilt before the offsets were
+    // carried over no longer has them, so also give it the ones kept in the 
segment ZK metadata to fall back to.
     SegmentPurger segmentPurger =
-        new SegmentPurger(indexDir, workingDir, tableConfig, schema, 
recordPurger, recordModifier, null);
+        new SegmentPurger(indexDir, workingDir, tableConfig, schema, 
recordPurger, recordModifier, null,
+            getStreamOffsetsFromZK(tableNameWithType, segmentName));

Review Comment:
   Deferred it in dcd67cb47b. `SegmentPurger` now takes a supplier and only 
asks for the ZK offsets once it has rows to rewrite, keeps the segment name and 
finds an offset missing, so a scan that purges nothing does no extra read.
   
   I went with deferring rather than reusing the record from the CRC check 
because `executeTask()` gets the CRC through the overridable `getSegmentCrc()`, 
and caching the record there would change that hook for subclasses. 
`PurgeTaskExecutorTest` asserts no read for a purge with nothing to purge and 
one read for a rebuild.
   



##########
pinot-plugins/pinot-minion-tasks/pinot-minion-builtin-tasks/src/test/java/org/apache/pinot/plugin/minion/tasks/refreshsegment/RefreshSegmentTaskExecutorTest.java:
##########
@@ -0,0 +1,200 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.plugin.minion.tasks.refreshsegment;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import javax.annotation.Nullable;
+import org.apache.commons.io.FileUtils;
+import org.apache.helix.AccessOption;
+import org.apache.helix.store.zk.ZkHelixPropertyStore;
+import org.apache.helix.zookeeper.datamodel.ZNRecord;
+import org.apache.pinot.common.metadata.ZKMetadataProvider;
+import org.apache.pinot.common.metadata.segment.SegmentZKMetadata;
+import org.apache.pinot.common.utils.LLCSegmentName;
+import org.apache.pinot.common.utils.config.SchemaSerDeUtils;
+import org.apache.pinot.common.utils.config.TableConfigSerDeUtils;
+import org.apache.pinot.core.common.MinionConstants;
+import org.apache.pinot.core.minion.PinotTaskConfig;
+import org.apache.pinot.minion.MinionContext;
+import org.apache.pinot.plugin.minion.tasks.MinionTaskTestUtils;
+import 
org.apache.pinot.segment.local.segment.creator.impl.SegmentIndexCreationDriverImpl;
+import org.apache.pinot.segment.local.segment.readers.GenericRowRecordReader;
+import org.apache.pinot.segment.spi.creator.SegmentGeneratorConfig;
+import org.apache.pinot.segment.spi.index.metadata.SegmentMetadataImpl;
+import org.apache.pinot.spi.config.table.SegmentZKPropsConfig;
+import org.apache.pinot.spi.config.table.TableConfig;
+import org.apache.pinot.spi.config.table.TableType;
+import org.apache.pinot.spi.data.FieldSpec;
+import org.apache.pinot.spi.data.Schema;
+import org.apache.pinot.spi.data.readers.GenericRow;
+import org.apache.pinot.spi.utils.builder.TableConfigBuilder;
+import org.apache.pinot.spi.utils.builder.TableNameBuilder;
+import org.mockito.Mockito;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotNull;
+
+
+/// Tests the segment metadata that 
[RefreshSegmentTaskExecutor#convert(PinotTaskConfig, File, File)] must carry 
over
+/// from the original segment when it rebuilds a segment.
+public class RefreshSegmentTaskExecutorTest {
+  private static final File TEMP_DIR = new File(FileUtils.getTempDirectory(), 
"RefreshSegmentTaskExecutorTest");
+
+  private static final int NUM_ROWS = 5;
+  private static final String RAW_TABLE_NAME = "testTable";
+  private static final String REALTIME_TABLE_NAME = 
TableNameBuilder.REALTIME.tableNameWithType(RAW_TABLE_NAME);
+  private static final String SEGMENT_NAME = new 
LLCSegmentName(RAW_TABLE_NAME, 0, 5, 0L).getSegmentName();
+  private static final String SEGMENT_ZK_METADATA_PATH =
+      
ZKMetadataProvider.constructPropertyStorePathForSegment(REALTIME_TABLE_NAME, 
SEGMENT_NAME);
+  private static final String D1 = "d1";
+  private static final String D2 = "d2";
+
+  private TableConfig _tableConfig;
+  private Schema _segmentSchema;
+  private Schema _tableSchema;
+
+  @BeforeClass
+  public void setUp()
+      throws Exception {
+    FileUtils.deleteDirectory(TEMP_DIR);
+
+    _tableConfig = new 
TableConfigBuilder(TableType.REALTIME).setTableName(RAW_TABLE_NAME).build();
+    _segmentSchema = new Schema.SchemaBuilder().setSchemaName(RAW_TABLE_NAME)
+        .addSingleValueDimension(D1, FieldSpec.DataType.INT)
+        .build();
+    // The table schema has a column that the segments do not have, so that 
the segments need to be refreshed
+    _tableSchema = new Schema.SchemaBuilder().setSchemaName(RAW_TABLE_NAME)
+        .addSingleValueDimension(D1, FieldSpec.DataType.INT)
+        .addSingleValueDimension(D2, FieldSpec.DataType.INT)
+        .build();
+  }
+
+  @DataProvider(name = "streamOffsets")
+  public Object[][] streamOffsets() {
+    return new Object[][]{
+        // testCase, segment start/end offset, segment ZK metadata start/end 
offset, expected start/end offset
+        // The stream offsets recorded in the original segment are kept
+        {"segmentOffsets", "100", "200", null, null, "100", "200"},
+        // They are preferred over the ones in the segment ZK metadata
+        {"segmentOffsetsPreferred", "100", "200", "300", "400", "100", "200"},
+        // A segment that lost its stream offsets in an earlier rewrite gets 
back the ones in the segment ZK metadata
+        {"zkOffsets", null, null, "300", "400", "300", "400"},
+        // Half of the offsets are not usable
+        {"zkOffsetsIncomplete", null, null, "300", null, null, null},
+        // A segment without stream offsets stays without them
+        {"noOffsets", null, null, null, null, null, null}
+    };
+  }
+
+  /// The refreshed segment keeps the segment name, so it must keep the stream 
start/end offsets of the original
+  /// segment. Without them the deep store copy of an LLC segment can no 
longer be uploaded as a new segment.
+  @Test(dataProvider = "streamOffsets")
+  public void testConvertKeepsStreamOffsets(String testCase, @Nullable String 
segmentStartOffset,
+      @Nullable String segmentEndOffset, @Nullable String zkStartOffset, 
@Nullable String zkEndOffset,
+      @Nullable String expectedStartOffset, @Nullable String expectedEndOffset)
+      throws Exception {
+    File originalIndexDir =
+        buildSegment(new File(TEMP_DIR, "originalSegment-" + testCase), 
segmentStartOffset, segmentEndOffset);
+    SegmentMetadataImpl originalSegmentMetadata = new 
SegmentMetadataImpl(originalIndexDir);
+    assertEquals(originalSegmentMetadata.getStartOffset(), segmentStartOffset);
+    assertEquals(originalSegmentMetadata.getEndOffset(), segmentEndOffset);
+    ZkHelixPropertyStore<ZNRecord> helixPropertyStore = 
mockHelixPropertyStore(zkStartOffset, zkEndOffset);
+
+    RefreshSegmentTaskExecutor executor = new RefreshSegmentTaskExecutor();
+    
executor.setMinionEventObserver(MinionTaskTestUtils.getMinionProgressObserver());
+    PinotTaskConfig pinotTaskConfig = new 
PinotTaskConfig(MinionConstants.RefreshSegmentTask.TASK_TYPE,
+        Map.of(MinionConstants.TABLE_NAME_KEY, REALTIME_TABLE_NAME, 
MinionConstants.SEGMENT_NAME_KEY, SEGMENT_NAME));
+    File refreshedIndexDir =
+        executor.convert(pinotTaskConfig, originalIndexDir, new File(TEMP_DIR, 
"refreshedSegment-" + testCase))
+            .getFile();
+
+    // The segment got the new column, so it was rebuilt
+    SegmentMetadataImpl refreshedSegmentMetadata = new 
SegmentMetadataImpl(refreshedIndexDir);
+    assertEquals(refreshedSegmentMetadata.getName(), SEGMENT_NAME);
+    assertEquals(refreshedSegmentMetadata.getTotalDocs(), NUM_ROWS);
+    assertNotNull(refreshedSegmentMetadata.getColumnMetadataFor(D2));
+
+    assertEquals(refreshedSegmentMetadata.getStartOffset(), 
expectedStartOffset);
+    assertEquals(refreshedSegmentMetadata.getEndOffset(), expectedEndOffset);
+
+    // The segment ZK metadata is read only when the segment does not record 
the stream offsets itself
+    Mockito.verify(helixPropertyStore, Mockito.times(segmentStartOffset != 
null ? 0 : 1))
+        .get(SEGMENT_ZK_METADATA_PATH, null, AccessOption.PERSISTENT);
+  }
+
+  /// Builds a segment with LLC segment name and only column [#D1]. The stream 
start/end offsets are recorded in the
+  /// segment metadata when they are given, the way a committed realtime 
segment looks like.
+  private File buildSegment(File outputDir, @Nullable String startOffset, 
@Nullable String endOffset)
+      throws Exception {
+    List<GenericRow> rows = new ArrayList<>(NUM_ROWS);
+    for (int i = 0; i < NUM_ROWS; i++) {
+      GenericRow row = new GenericRow();
+      row.putValue(D1, i);
+      rows.add(row);
+    }
+
+    SegmentGeneratorConfig config = new SegmentGeneratorConfig(_tableConfig, 
_segmentSchema);
+    config.setOutDir(outputDir.getPath());
+    config.setSegmentName(SEGMENT_NAME);
+    if (startOffset != null && endOffset != null) {

Review Comment:
   Added in dcd67cb47b. The fixture can now build a segment with only one of 
the offsets, and there are start-only and end-only rows with a complete ZK pair 
that expect `300` and `400` after one lookup, plus a row where the segment and 
ZK each have only half and nothing is carried over. The read-count assertion 
now checks both source offsets. The compaction and purger tests got the same 
rows.
   



-- 
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]

Reply via email to