tibrewalpratik17 commented on code in PR #14477:
URL: https://github.com/apache/pinot/pull/14477#discussion_r1879628123


##########
pinot-plugins/pinot-minion-tasks/pinot-minion-builtin-tasks/src/main/java/org/apache/pinot/plugin/minion/tasks/upsertcompactmerge/UpsertCompactMergeTaskExecutor.java:
##########
@@ -0,0 +1,199 @@
+/**
+ * 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.upsertcompactmerge;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.Set;
+import java.util.TreeMap;
+import java.util.stream.Collectors;
+import 
org.apache.pinot.common.metadata.segment.SegmentZKMetadataCustomMapModifier;
+import org.apache.pinot.common.restlet.resources.ValidDocIdsType;
+import org.apache.pinot.common.utils.SegmentUtils;
+import org.apache.pinot.core.common.MinionConstants;
+import org.apache.pinot.core.minion.PinotTaskConfig;
+import 
org.apache.pinot.core.segment.processing.framework.SegmentProcessorConfig;
+import 
org.apache.pinot.core.segment.processing.framework.SegmentProcessorFramework;
+import org.apache.pinot.minion.MinionConf;
+import 
org.apache.pinot.plugin.minion.tasks.BaseMultipleSegmentsConversionExecutor;
+import org.apache.pinot.plugin.minion.tasks.MinionTaskUtils;
+import org.apache.pinot.plugin.minion.tasks.SegmentConversionResult;
+import 
org.apache.pinot.segment.local.segment.readers.CompactedPinotSegmentRecordReader;
+import 
org.apache.pinot.segment.spi.creator.name.UploadedRealtimeSegmentNameGenerator;
+import org.apache.pinot.segment.spi.index.metadata.SegmentMetadataImpl;
+import org.apache.pinot.spi.config.table.TableConfig;
+import org.apache.pinot.spi.data.Schema;
+import org.apache.pinot.spi.data.readers.RecordReader;
+import org.apache.pinot.spi.utils.builder.TableNameBuilder;
+import org.roaringbitmap.RoaringBitmap;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * Minion task that compacts and merges multiple segments of an upsert table 
and uploads it back as one single
+ * segment. This helps in keeping the segment count in check and also prevents 
a lot of small segments created over
+ * time.
+ */
+public class UpsertCompactMergeTaskExecutor extends 
BaseMultipleSegmentsConversionExecutor {
+
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(UpsertCompactMergeTaskExecutor.class);
+
+  public UpsertCompactMergeTaskExecutor(MinionConf minionConf) {
+    super(minionConf);
+  }
+
+  @Override
+  protected List<SegmentConversionResult> convert(PinotTaskConfig 
pinotTaskConfig, List<File> segmentDirs,
+      File workingDir)
+      throws Exception {
+    int numInputSegments = segmentDirs.size();
+    _eventObserver.notifyProgress(pinotTaskConfig, "Converting segments: " + 
numInputSegments);
+    String taskType = pinotTaskConfig.getTaskType();
+    Map<String, String> configs = pinotTaskConfig.getConfigs();
+    LOGGER.info("Starting task: {} with configs: {}", taskType, configs);
+    long startMillis = System.currentTimeMillis();
+
+    String tableNameWithType = configs.get(MinionConstants.TABLE_NAME_KEY);
+    TableConfig tableConfig = getTableConfig(tableNameWithType);
+    Schema schema = getSchema(tableNameWithType);
+
+    SegmentProcessorConfig.Builder segmentProcessorConfigBuilder =
+        new 
SegmentProcessorConfig.Builder().setTableConfig(tableConfig).setSchema(schema);
+
+    // Progress observer
+    segmentProcessorConfigBuilder.setProgressObserver(p -> 
_eventObserver.notifyProgress(_pinotTaskConfig, p));
+
+    // get list of segment metadata
+    List<SegmentMetadataImpl> segmentMetadataList = segmentDirs.stream().map(x 
-> {
+      try {
+        return new SegmentMetadataImpl(x);
+      } catch (Exception e) {
+        throw new RuntimeException(String.format("Error fetching 
segment-metadata for segmentDir: %s", x), e);
+      }
+    }).collect(Collectors.toList());
+
+    // validate if partitionID is same for all small segments. Get partition 
id value for new segment.
+    int partitionID = getCommonPartitionIDForSegments(segmentMetadataList);
+
+    // get the max creation time of the small segments. This will be the index 
creation time for the new segment.
+    Optional<Long> maxCreationTimeOfMergingSegments = 
segmentMetadataList.stream().map(
+        SegmentMetadataImpl::getIndexCreationTime).reduce(Long::max);
+    if (maxCreationTimeOfMergingSegments.isEmpty()) {

Review Comment:
   Yes we have added the check in generator code to ensure `numInputSegments > 
1` 



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