Jackie-Jiang commented on a change in pull request #6094: URL: https://github.com/apache/incubator-pinot/pull/6094#discussion_r508110795
########## File path: pinot-common/src/main/java/org/apache/pinot/common/utils/FileUploadDownloadClient.java ########## @@ -287,11 +309,26 @@ private static HttpUriRequest getUploadSegmentMetadataFilesRequest(URI uri, Map< return requestBuilder.build(); } + private static HttpUriRequest getStartReplaceSegmentsRequest(URI uri, String jsonRequestBody, int socketTimeoutMs) { + RequestBuilder requestBuilder = RequestBuilder.post(uri).setVersion(HttpVersion.HTTP_1_1) + .setHeader(HttpHeaders.CONTENT_TYPE, JSON_CONTENT_TYPE) + .setEntity(new StringEntity(jsonRequestBody, ContentType.APPLICATION_JSON)); + setTimeout(requestBuilder, socketTimeoutMs); + return requestBuilder.build(); + } + + private static HttpUriRequest endStartReplaceSegmentsRequest(URI uri, int socketTimeoutMs) { Review comment: ```suggestion private static HttpUriRequest getEndReplaceSegmentsRequest(URI uri, int socketTimeoutMs) { ``` ########## File path: pinot-controller/src/main/java/org/apache/pinot/controller/helix/core/minion/generator/SegmentMergeRollupTaskGenerator.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.controller.helix.core.minion.generator; + +import com.google.common.base.Preconditions; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; +import org.apache.pinot.common.lineage.LineageEntry; +import org.apache.pinot.common.lineage.LineageEntryState; +import org.apache.pinot.common.lineage.SegmentLineage; +import org.apache.pinot.common.metadata.segment.OfflineSegmentZKMetadata; +import org.apache.pinot.common.metadata.segment.SegmentZKMetadata; +import org.apache.pinot.controller.helix.core.minion.ClusterInfoProvider; +import org.apache.pinot.controller.helix.core.minion.mergestrategy.MergeStrategyFactory; +import org.apache.pinot.core.common.MinionConstants; +import org.apache.pinot.core.minion.PinotTaskConfig; +import org.apache.pinot.core.segment.processing.collector.CollectorFactory; +import org.apache.pinot.spi.config.table.TableConfig; +import org.apache.pinot.spi.config.table.TableTaskConfig; +import org.apache.pinot.spi.config.table.TableType; +import org.apache.pinot.spi.utils.TimeUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +public class SegmentMergeRollupTaskGenerator implements PinotTaskGenerator { Review comment: Please add some javadoc describing the algorithm ########## File path: pinot-controller/src/main/java/org/apache/pinot/controller/helix/core/minion/generator/TaskGeneratorUtils.java ########## @@ -68,4 +73,30 @@ } return runningSegments; } + + public static Map<String, List<String>> getScheduledSegmentsMap(@Nonnull String taskType, + @Nonnull ClusterInfoProvider clusterInfoProvider) { + Map<String, List<String>> scheduledSegments = new HashMap<>(); + Map<String, TaskState> taskStates = clusterInfoProvider.getTaskStates(taskType); + for (Map.Entry<String, TaskState> entry : taskStates.entrySet()) { + // Skip COMPLETED tasks + if (entry.getValue() == TaskState.COMPLETED) { + continue; + } + for (PinotTaskConfig pinotTaskConfig : clusterInfoProvider.getTaskConfigs(entry.getKey())) { + Map<String, String> configs = pinotTaskConfig.getConfigs(); + List<String> scheduledSegmentsForTable = + scheduledSegments.computeIfAbsent(configs.get(MinionConstants.TABLE_NAME_KEY), v -> new ArrayList<>()); + String segmentName = configs.get(MinionConstants.SEGMENT_NAME_KEY); + if (taskType.equals(MinionConstants.MergeRollupTask.TASK_TYPE)) { Review comment: Why treating them differently? ########## File path: pinot-controller/src/main/java/org/apache/pinot/controller/helix/core/minion/generator/TaskGeneratorUtils.java ########## @@ -68,4 +73,30 @@ } return runningSegments; } + + public static Map<String, List<String>> getScheduledSegmentsMap(@Nonnull String taskType, Review comment: Return `Map<String, Set<String>>` for fast lookup in the generator? ########## File path: pinot-controller/src/main/java/org/apache/pinot/controller/helix/core/minion/generator/TaskGeneratorUtils.java ########## @@ -68,4 +73,30 @@ } return runningSegments; } + + public static Map<String, List<String>> getScheduledSegmentsMap(@Nonnull String taskType, + @Nonnull ClusterInfoProvider clusterInfoProvider) { + Map<String, List<String>> scheduledSegments = new HashMap<>(); + Map<String, TaskState> taskStates = clusterInfoProvider.getTaskStates(taskType); + for (Map.Entry<String, TaskState> entry : taskStates.entrySet()) { + // Skip COMPLETED tasks + if (entry.getValue() == TaskState.COMPLETED) { + continue; + } Review comment: Skip tasks that are older than one day? ########## File path: pinot-controller/src/main/java/org/apache/pinot/controller/helix/core/minion/generator/TaskGeneratorUtils.java ########## @@ -68,4 +73,30 @@ } return runningSegments; } + + public static Map<String, List<String>> getScheduledSegmentsMap(@Nonnull String taskType, Review comment: (nit) Don't use the non-null annotation (you can also remove them from other places), we only use nullable for readability. ########## File path: pinot-controller/src/main/java/org/apache/pinot/controller/helix/core/minion/generator/SegmentMergeRollupTaskGenerator.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.controller.helix.core.minion.generator; + +import com.google.common.base.Preconditions; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; +import org.apache.pinot.common.lineage.LineageEntry; +import org.apache.pinot.common.lineage.LineageEntryState; +import org.apache.pinot.common.lineage.SegmentLineage; +import org.apache.pinot.common.metadata.segment.OfflineSegmentZKMetadata; +import org.apache.pinot.common.metadata.segment.SegmentZKMetadata; +import org.apache.pinot.controller.helix.core.minion.ClusterInfoProvider; +import org.apache.pinot.controller.helix.core.minion.mergestrategy.MergeStrategyFactory; +import org.apache.pinot.core.common.MinionConstants; +import org.apache.pinot.core.minion.PinotTaskConfig; +import org.apache.pinot.core.segment.processing.collector.CollectorFactory; +import org.apache.pinot.spi.config.table.TableConfig; +import org.apache.pinot.spi.config.table.TableTaskConfig; +import org.apache.pinot.spi.config.table.TableType; +import org.apache.pinot.spi.utils.TimeUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +public class SegmentMergeRollupTaskGenerator implements PinotTaskGenerator { + private static final Logger LOGGER = LoggerFactory.getLogger(SegmentMergeRollupTaskGenerator.class); + + private static final int DEFAULT_MAX_NUM_SEGMENTS_PER_TASK = 20; // 20 segments + private static final int DEFAULT_MAX_NUM_RECORDS_PER_SEGMENT = 5_000_000; // 5 million rows + private static final int DEFAULT_MAX_NUM_TASKS = 200; // schedule at most 200 tasks at a time + private static final String DEFAULT_BUFFER_TIME_PERIOD = "14d"; // 2 weeks + + private final ClusterInfoProvider _clusterInfoProvider; + + public SegmentMergeRollupTaskGenerator(ClusterInfoProvider clusterInfoProvider) { + _clusterInfoProvider = clusterInfoProvider; + } + + @Override + public String getTaskType() { + return MinionConstants.MergeRollupTask.TASK_TYPE; + } + + @Override + public List<PinotTaskConfig> generateTasks(List<TableConfig> tableConfigs) { + List<PinotTaskConfig> pinotTaskConfigs = new ArrayList<>(); + + // Get the segments that are being converted so that we don't submit them again + Map<String, List<String>> scheduledSegmentsMap = + TaskGeneratorUtils.getScheduledSegmentsMap(MinionConstants.MergeRollupTask.TASK_TYPE, _clusterInfoProvider); + + for (TableConfig tableConfig : tableConfigs) { + // Only generate tasks for OFFLINE tables + String offlineTableName = tableConfig.getTableName(); + if (tableConfig.getTableType() != TableType.OFFLINE) { + LOGGER.warn("Skip generating MergeRollupTask for non-OFFLINE table: {}", offlineTableName); + continue; + } + + TableTaskConfig tableTaskConfig = tableConfig.getTaskConfig(); + Preconditions.checkNotNull(tableTaskConfig); + Map<String, String> taskConfigs = + tableTaskConfig.getConfigsForTaskType(MinionConstants.MergeRollupTask.TASK_TYPE); + Preconditions.checkNotNull(taskConfigs, "Task config shouldn't be null for Table: {}", offlineTableName); + + int tableMaxNumTasks = Review comment: This should be the global tasks limit instead of the per-table tasks limit ########## File path: pinot-controller/src/main/java/org/apache/pinot/controller/helix/core/minion/generator/SegmentMergeRollupTaskGenerator.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.controller.helix.core.minion.generator; + +import com.google.common.base.Preconditions; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; +import org.apache.pinot.common.lineage.LineageEntry; +import org.apache.pinot.common.lineage.LineageEntryState; +import org.apache.pinot.common.lineage.SegmentLineage; +import org.apache.pinot.common.metadata.segment.OfflineSegmentZKMetadata; +import org.apache.pinot.common.metadata.segment.SegmentZKMetadata; +import org.apache.pinot.controller.helix.core.minion.ClusterInfoProvider; +import org.apache.pinot.controller.helix.core.minion.mergestrategy.MergeStrategyFactory; +import org.apache.pinot.core.common.MinionConstants; +import org.apache.pinot.core.minion.PinotTaskConfig; +import org.apache.pinot.core.segment.processing.collector.CollectorFactory; +import org.apache.pinot.spi.config.table.TableConfig; +import org.apache.pinot.spi.config.table.TableTaskConfig; +import org.apache.pinot.spi.config.table.TableType; +import org.apache.pinot.spi.utils.TimeUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +public class SegmentMergeRollupTaskGenerator implements PinotTaskGenerator { + private static final Logger LOGGER = LoggerFactory.getLogger(SegmentMergeRollupTaskGenerator.class); + + private static final int DEFAULT_MAX_NUM_SEGMENTS_PER_TASK = 20; // 20 segments + private static final int DEFAULT_MAX_NUM_RECORDS_PER_SEGMENT = 5_000_000; // 5 million rows + private static final int DEFAULT_MAX_NUM_TASKS = 200; // schedule at most 200 tasks at a time + private static final String DEFAULT_BUFFER_TIME_PERIOD = "14d"; // 2 weeks + + private final ClusterInfoProvider _clusterInfoProvider; + + public SegmentMergeRollupTaskGenerator(ClusterInfoProvider clusterInfoProvider) { + _clusterInfoProvider = clusterInfoProvider; + } + + @Override + public String getTaskType() { + return MinionConstants.MergeRollupTask.TASK_TYPE; + } + + @Override + public List<PinotTaskConfig> generateTasks(List<TableConfig> tableConfigs) { + List<PinotTaskConfig> pinotTaskConfigs = new ArrayList<>(); + + // Get the segments that are being converted so that we don't submit them again + Map<String, List<String>> scheduledSegmentsMap = + TaskGeneratorUtils.getScheduledSegmentsMap(MinionConstants.MergeRollupTask.TASK_TYPE, _clusterInfoProvider); + + for (TableConfig tableConfig : tableConfigs) { + // Only generate tasks for OFFLINE tables + String offlineTableName = tableConfig.getTableName(); + if (tableConfig.getTableType() != TableType.OFFLINE) { + LOGGER.warn("Skip generating MergeRollupTask for non-OFFLINE table: {}", offlineTableName); + continue; + } + + TableTaskConfig tableTaskConfig = tableConfig.getTaskConfig(); + Preconditions.checkNotNull(tableTaskConfig); + Map<String, String> taskConfigs = + tableTaskConfig.getConfigsForTaskType(MinionConstants.MergeRollupTask.TASK_TYPE); + Preconditions.checkNotNull(taskConfigs, "Task config shouldn't be null for Table: {}", offlineTableName); + + int tableMaxNumTasks = + readIntConfigWithDefaultValue(taskConfigs, MinionConstants.TABLE_MAX_NUM_TASKS_KEY, DEFAULT_MAX_NUM_TASKS); + + int maxNumSegmentsPerTask = + readIntConfigWithDefaultValue(taskConfigs, MinionConstants.MergeRollupTask.MAX_NUM_SEGMENTS_PER_TASK_KEY, + DEFAULT_MAX_NUM_SEGMENTS_PER_TASK); + + int maxNumRecordsPerSegment = + readIntConfigWithDefaultValue(taskConfigs, MinionConstants.MergeRollupTask.MAX_NUM_RECORDS_PER_SEGMENT_KEY, + DEFAULT_MAX_NUM_RECORDS_PER_SEGMENT); + + String bufferTimePeriod = + taskConfigs.getOrDefault(MinionConstants.MergeRollupTask.BUFFER_TIME_PERIOD_KEY, DEFAULT_BUFFER_TIME_PERIOD); + long bufferTimePeriodMs; + try { + bufferTimePeriodMs= TimeUtils.convertPeriodToMillis(bufferTimePeriod); + } catch (IllegalArgumentException e) { + LOGGER.error("Buffer time period ('{}') for table '{}' is not configured correctly.", bufferTimePeriod, + offlineTableName, e); + throw e; + } + + // Generate tasks + int tableNumTasks = 0; + + List<OfflineSegmentZKMetadata> segmentsForOfflineTable = + _clusterInfoProvider.getOfflineSegmentsMetadata(offlineTableName); + + // Fetch the segment lineage for the table and compute the segments that should not be scheduled for merge + // based on the segment lineage. + SegmentLineage segmentLineageForTable = _clusterInfoProvider.getSegmentLineage(offlineTableName); + Set<String> segmentsNotToMerge = new HashSet<>(); + if (segmentLineageForTable != null) { + for (String segmentLineageEntryId : segmentLineageForTable.getLineageEntryIds()) { + LineageEntry lineageEntry = segmentLineageForTable.getLineageEntry(segmentLineageEntryId); + // Segments shows up on "segmentFrom" field in the lineage entry should not be scheduled again. + segmentsNotToMerge.addAll(lineageEntry.getSegmentsFrom()); + + // Segments shows up on "segmentsTo" field in the lineage entry with "IN_PROGRESS" state cannot be merged. + if (lineageEntry.getState() == LineageEntryState.IN_PROGRESS) { + segmentsNotToMerge.addAll(lineageEntry.getSegmentsTo()); + } + } + } + + // Filter out the segments that cannot be merged + List<SegmentZKMetadata> segmentsToMergeForTable = new ArrayList<>(); + List<String> scheduledSegments = scheduledSegmentsMap.getOrDefault(offlineTableName, Collections.emptyList()); + for (OfflineSegmentZKMetadata offlineSegmentZKMetadata : segmentsForOfflineTable) { + String segmentName = offlineSegmentZKMetadata.getSegmentName(); + + // The segment should not be merged if it's already scheduled or in progress + if (scheduledSegments.contains(segmentName) || segmentsNotToMerge.contains(segmentName)) { + continue; + } + + // The segments that are newer than the buffer time period should not be be merged + if (System.currentTimeMillis() - offlineSegmentZKMetadata.getEndTime() < bufferTimePeriodMs) { + continue; + } + segmentsToMergeForTable.add(offlineSegmentZKMetadata); + } + + // Compute Merge Strategy + List<List<SegmentZKMetadata>> segmentsToSchedule = MergeStrategyFactory.getMergeStrategy(taskConfigs) + .generateMergeTaskCandidates(segmentsToMergeForTable, maxNumSegmentsPerTask); + + // Generate tasks + for (List<SegmentZKMetadata> segments : segmentsToSchedule) { + if (tableNumTasks == tableMaxNumTasks) { + break; + } + if (segments.size() >= 1) { + Map<String, String> configs = new HashMap<>(); + configs.put(MinionConstants.TABLE_NAME_KEY, offlineTableName); + configs.put(MinionConstants.SEGMENT_NAME_KEY, + segments.stream().map(s -> s.getSegmentName()).collect(Collectors.joining(","))); + configs.put(MinionConstants.DOWNLOAD_URL_KEY, + segments.stream().map(s -> ((OfflineSegmentZKMetadata) s).getDownloadUrl()) + .collect(Collectors.joining(","))); + configs.put(MinionConstants.VIP_URL_KEY, _clusterInfoProvider.getVipUrl()); Review comment: Should this be `uploadURL`? ########## File path: pinot-controller/src/main/java/org/apache/pinot/controller/helix/core/minion/generator/SegmentMergeRollupTaskGenerator.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.controller.helix.core.minion.generator; + +import com.google.common.base.Preconditions; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; +import org.apache.pinot.common.lineage.LineageEntry; +import org.apache.pinot.common.lineage.LineageEntryState; +import org.apache.pinot.common.lineage.SegmentLineage; +import org.apache.pinot.common.metadata.segment.OfflineSegmentZKMetadata; +import org.apache.pinot.common.metadata.segment.SegmentZKMetadata; +import org.apache.pinot.controller.helix.core.minion.ClusterInfoProvider; +import org.apache.pinot.controller.helix.core.minion.mergestrategy.MergeStrategyFactory; +import org.apache.pinot.core.common.MinionConstants; +import org.apache.pinot.core.minion.PinotTaskConfig; +import org.apache.pinot.core.segment.processing.collector.CollectorFactory; +import org.apache.pinot.spi.config.table.TableConfig; +import org.apache.pinot.spi.config.table.TableTaskConfig; +import org.apache.pinot.spi.config.table.TableType; +import org.apache.pinot.spi.utils.TimeUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +public class SegmentMergeRollupTaskGenerator implements PinotTaskGenerator { + private static final Logger LOGGER = LoggerFactory.getLogger(SegmentMergeRollupTaskGenerator.class); + + private static final int DEFAULT_MAX_NUM_SEGMENTS_PER_TASK = 20; // 20 segments + private static final int DEFAULT_MAX_NUM_RECORDS_PER_SEGMENT = 5_000_000; // 5 million rows + private static final int DEFAULT_MAX_NUM_TASKS = 200; // schedule at most 200 tasks at a time + private static final String DEFAULT_BUFFER_TIME_PERIOD = "14d"; // 2 weeks + + private final ClusterInfoProvider _clusterInfoProvider; + + public SegmentMergeRollupTaskGenerator(ClusterInfoProvider clusterInfoProvider) { + _clusterInfoProvider = clusterInfoProvider; + } + + @Override + public String getTaskType() { + return MinionConstants.MergeRollupTask.TASK_TYPE; + } + + @Override + public List<PinotTaskConfig> generateTasks(List<TableConfig> tableConfigs) { + List<PinotTaskConfig> pinotTaskConfigs = new ArrayList<>(); + + // Get the segments that are being converted so that we don't submit them again + Map<String, List<String>> scheduledSegmentsMap = + TaskGeneratorUtils.getScheduledSegmentsMap(MinionConstants.MergeRollupTask.TASK_TYPE, _clusterInfoProvider); + + for (TableConfig tableConfig : tableConfigs) { + // Only generate tasks for OFFLINE tables + String offlineTableName = tableConfig.getTableName(); + if (tableConfig.getTableType() != TableType.OFFLINE) { + LOGGER.warn("Skip generating MergeRollupTask for non-OFFLINE table: {}", offlineTableName); + continue; + } + + TableTaskConfig tableTaskConfig = tableConfig.getTaskConfig(); + Preconditions.checkNotNull(tableTaskConfig); + Map<String, String> taskConfigs = + tableTaskConfig.getConfigsForTaskType(MinionConstants.MergeRollupTask.TASK_TYPE); + Preconditions.checkNotNull(taskConfigs, "Task config shouldn't be null for Table: {}", offlineTableName); + + int tableMaxNumTasks = + readIntConfigWithDefaultValue(taskConfigs, MinionConstants.TABLE_MAX_NUM_TASKS_KEY, DEFAULT_MAX_NUM_TASKS); + + int maxNumSegmentsPerTask = + readIntConfigWithDefaultValue(taskConfigs, MinionConstants.MergeRollupTask.MAX_NUM_SEGMENTS_PER_TASK_KEY, + DEFAULT_MAX_NUM_SEGMENTS_PER_TASK); + + int maxNumRecordsPerSegment = + readIntConfigWithDefaultValue(taskConfigs, MinionConstants.MergeRollupTask.MAX_NUM_RECORDS_PER_SEGMENT_KEY, + DEFAULT_MAX_NUM_RECORDS_PER_SEGMENT); + + String bufferTimePeriod = + taskConfigs.getOrDefault(MinionConstants.MergeRollupTask.BUFFER_TIME_PERIOD_KEY, DEFAULT_BUFFER_TIME_PERIOD); + long bufferTimePeriodMs; + try { + bufferTimePeriodMs= TimeUtils.convertPeriodToMillis(bufferTimePeriod); + } catch (IllegalArgumentException e) { + LOGGER.error("Buffer time period ('{}') for table '{}' is not configured correctly.", bufferTimePeriod, + offlineTableName, e); + throw e; + } + + // Generate tasks + int tableNumTasks = 0; + + List<OfflineSegmentZKMetadata> segmentsForOfflineTable = + _clusterInfoProvider.getOfflineSegmentsMetadata(offlineTableName); + + // Fetch the segment lineage for the table and compute the segments that should not be scheduled for merge + // based on the segment lineage. + SegmentLineage segmentLineageForTable = _clusterInfoProvider.getSegmentLineage(offlineTableName); + Set<String> segmentsNotToMerge = new HashSet<>(); + if (segmentLineageForTable != null) { + for (String segmentLineageEntryId : segmentLineageForTable.getLineageEntryIds()) { + LineageEntry lineageEntry = segmentLineageForTable.getLineageEntry(segmentLineageEntryId); + // Segments shows up on "segmentFrom" field in the lineage entry should not be scheduled again. + segmentsNotToMerge.addAll(lineageEntry.getSegmentsFrom()); + + // Segments shows up on "segmentsTo" field in the lineage entry with "IN_PROGRESS" state cannot be merged. + if (lineageEntry.getState() == LineageEntryState.IN_PROGRESS) { + segmentsNotToMerge.addAll(lineageEntry.getSegmentsTo()); + } + } + } + + // Filter out the segments that cannot be merged + List<SegmentZKMetadata> segmentsToMergeForTable = new ArrayList<>(); + List<String> scheduledSegments = scheduledSegmentsMap.getOrDefault(offlineTableName, Collections.emptyList()); + for (OfflineSegmentZKMetadata offlineSegmentZKMetadata : segmentsForOfflineTable) { + String segmentName = offlineSegmentZKMetadata.getSegmentName(); + + // The segment should not be merged if it's already scheduled or in progress + if (scheduledSegments.contains(segmentName) || segmentsNotToMerge.contains(segmentName)) { + continue; + } + + // The segments that are newer than the buffer time period should not be be merged + if (System.currentTimeMillis() - offlineSegmentZKMetadata.getEndTime() < bufferTimePeriodMs) { + continue; + } + segmentsToMergeForTable.add(offlineSegmentZKMetadata); + } + + // Compute Merge Strategy + List<List<SegmentZKMetadata>> segmentsToSchedule = MergeStrategyFactory.getMergeStrategy(taskConfigs) + .generateMergeTaskCandidates(segmentsToMergeForTable, maxNumSegmentsPerTask); + + // Generate tasks + for (List<SegmentZKMetadata> segments : segmentsToSchedule) { + if (tableNumTasks == tableMaxNumTasks) { + break; + } + if (segments.size() >= 1) { + Map<String, String> configs = new HashMap<>(); + configs.put(MinionConstants.TABLE_NAME_KEY, offlineTableName); + configs.put(MinionConstants.SEGMENT_NAME_KEY, + segments.stream().map(s -> s.getSegmentName()).collect(Collectors.joining(","))); + configs.put(MinionConstants.DOWNLOAD_URL_KEY, + segments.stream().map(s -> ((OfflineSegmentZKMetadata) s).getDownloadUrl()) + .collect(Collectors.joining(","))); + configs.put(MinionConstants.VIP_URL_KEY, _clusterInfoProvider.getVipUrl()); + configs.put(MinionConstants.REPLACE_SEGMENTS_KEY, Boolean.TRUE.toString()); Review comment: What is this config for? ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org For additional commands, e-mail: commits-h...@pinot.apache.org