cecemei commented on code in PR #19059: URL: https://github.com/apache/druid/pull/19059#discussion_r2866796588
########## indexing-service/src/main/java/org/apache/druid/indexing/common/actions/SegmentUpgradeAction.java: ########## @@ -0,0 +1,103 @@ +/* + * 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.druid.indexing.common.actions; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.core.type.TypeReference; +import org.apache.druid.indexing.common.task.Task; +import org.apache.druid.java.util.common.IAE; +import org.apache.druid.metadata.ReplaceTaskLock; +import org.apache.druid.timeline.DataSegment; + +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * Task action that records segments as being upgraded in the metadata store. + * <p> + * This action is used during compaction to track which segments are being replaced. + * It validates that all segments to be upgraded are covered by + * {@link ReplaceTaskLock}s before inserting them into the upgrade segments table. + * <p> + * The action will fail if any of the upgrade segments do not have a corresponding + * replace lock, ensuring that only properly locked segments can be marked for upgrade. + * + * @return the number of segments successfully inserted into the upgrade segments table + */ +public class SegmentUpgradeAction implements TaskAction<Integer> +{ + private final String dataSource; + private final List<DataSegment> upgradeSegments; + + /** + * @param dataSource the datasource containing the segments to upgrade + * @param upgradeSegments the list of segments to be recorded as upgraded + */ + @JsonCreator + public SegmentUpgradeAction( + @JsonProperty("dataSource") String dataSource, + @JsonProperty("upgradeSegments") List<DataSegment> upgradeSegments + ) + { + this.dataSource = dataSource; + this.upgradeSegments = upgradeSegments; + } + + @JsonProperty + public String getDataSource() + { + return dataSource; + } + + @JsonProperty + public List<DataSegment> getUpgradeSegments() + { + return upgradeSegments; + } + + @Override + public TypeReference<Integer> getReturnTypeReference() + { + return new TypeReference<>() + { + }; + } + + @Override + public Integer perform(Task task, TaskActionToolbox toolbox) + { + final String datasource = task.getDataSource(); + final Map<DataSegment, ReplaceTaskLock> segmentToReplaceLock + = TaskLocks.findReplaceLocksCoveringSegments(datasource, toolbox.getTaskLockbox(), Set.copyOf(upgradeSegments)); + + if (segmentToReplaceLock.size() < upgradeSegments.size()) { + throw new IAE( Review Comment: done ########## indexing-service/src/main/java/org/apache/druid/indexing/common/task/CompactionIntervalSpec.java: ########## @@ -42,18 +44,52 @@ public class CompactionIntervalSpec implements CompactionInputSpec private final Interval interval; @Nullable + private final List<SegmentDescriptor> uncompactedSegments; + /** + * Optional hash of all segment IDs for validation. When set, this is used in {@link #validateSegments} to verify + * that the segments haven't changed since this spec was created. + * <p> + * Note: This hash is computed and validated against ALL segments overlapping the interval, not just the + * uncompactedSegments. This is because compaction operates on all segments within the interval - compacted + * segments may need to be rewritten alongside uncompacted ones to maintain proper partitioning and sort order. + * Therefore, the validation check must apply to all segments to ensure correctness. + */ + @Nullable private final String sha256OfSortedSegmentIds; + public CompactionIntervalSpec(Interval interval, String sha256OfSortedSegmentIds) + { + this(interval, null, sha256OfSortedSegmentIds); + } + @JsonCreator public CompactionIntervalSpec( @JsonProperty("interval") Interval interval, + @JsonProperty("uncompactedSegments") @Nullable Review Comment: updated ########## server/src/main/java/org/apache/druid/server/compaction/MostFragmentedIntervalFirstPolicy.java: ########## @@ -106,6 +118,17 @@ public HumanReadableBytes getMaxAverageUncompactedBytesPerSegment() return maxAverageUncompactedBytesPerSegment; } + /** + * Threshold ratio of uncompacted bytes to compacted bytes below which Review Comment: done ########## server/src/main/java/org/apache/druid/server/compaction/CompactionMode.java: ########## @@ -27,5 +27,9 @@ public enum CompactionMode /** * Indicates that all existing segments of the interval will be picked for compaction. */ - FULL_COMPACTION; + FULL_COMPACTION, + /** + * Indicates that only uncompacted segments of the interval will be picked for compaction. + */ + INCREMENTAL_COMPACTION; Review Comment: done ########## server/src/main/java/org/apache/druid/server/coordinator/duty/CompactSegments.java: ########## @@ -463,14 +470,30 @@ private static ClientCompactionTaskQuery compactSegments( context.put("priority", compactionTaskPriority); final String taskId = IdUtils.newTaskId(TASK_ID_PREFIX, ClientCompactionTaskQuery.TYPE, dataSource, null); + final ClientCompactionIntervalSpec clientCompactionIntervalSpec; + switch (compactionMode) { + case FULL_COMPACTION: + clientCompactionIntervalSpec = new ClientCompactionIntervalSpec(entry.getCompactionInterval(), null, null); + break; + case INCREMENTAL_COMPACTION: Review Comment: this method is used by both coordinator and overlord, coordinator would always use ALL_SEGMENTS compaction mode in line 266. ########## server/src/main/java/org/apache/druid/server/compaction/MostFragmentedIntervalFirstPolicy.java: ########## @@ -47,13 +47,16 @@ public class MostFragmentedIntervalFirstPolicy extends BaseCandidateSearchPolicy private final int minUncompactedCount; private final HumanReadableBytes minUncompactedBytes; private final HumanReadableBytes maxAverageUncompactedBytesPerSegment; + private final double incrementalCompactionUncompactedRatioThreshold; @JsonCreator public MostFragmentedIntervalFirstPolicy( @JsonProperty("minUncompactedCount") @Nullable Integer minUncompactedCount, @JsonProperty("minUncompactedBytes") @Nullable HumanReadableBytes minUncompactedBytes, @JsonProperty("maxAverageUncompactedBytesPerSegment") @Nullable HumanReadableBytes maxAverageUncompactedBytesPerSegment, + @JsonProperty("incrementalCompactionUncompactedRatioThreshold") @Nullable Review Comment: done ########## server/src/main/java/org/apache/druid/server/compaction/CompactionMode.java: ########## @@ -27,5 +27,6 @@ public enum CompactionMode /** * Indicates that all existing segments of the interval will be picked for compaction. */ - FULL_COMPACTION; + FULL_COMPACTION, + INCREMENTAL_COMPACTION; Review Comment: done ########## server/src/main/java/org/apache/druid/indexing/overlord/IndexerMetadataStorageCoordinator.java: ########## @@ -387,6 +387,29 @@ SegmentPublishResult commitAppendSegmentsAndMetadata( @Nullable SegmentSchemaMapping segmentSchemaMapping ); + /** + * Inserts entries into the upgrade_segments table for segments that need to be upgraded + * to a new version without recompaction. This is used during incremental compaction when + * some segments in the interval don't require compaction but should be upgraded to match + * the version of newly compacted segments. + * <p> + * In incremental compaction scenarios: + * <ul> + * <li>Some segments may already meet compaction criteria (e.g., already compacted, correct partitioning) + * and don't need to be rewritten.</li> + * <li>These segments are tracked in the upgrade_segments table via this method, mapped to their + * corresponding REPLACE lock.</li> + * <li>When the compaction task finishes via {@link #commitReplaceSegments}, these segments are + * upgraded directly to the new version to maintain version consistency across the interval.</li> + * <li>After the task completes, entries are cleaned up via {@link #deleteUpgradeSegmentsForTask}.</li> + * </ul> Review Comment: done -- 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]
