jon-wei commented on code in PR #14662: URL: https://github.com/apache/druid/pull/14662#discussion_r1279979827
########## indexing-service/src/main/java/org/apache/druid/indexing/common/task/KillUnusedSegmentsByIdTask.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.druid.indexing.common.task; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; +import com.google.errorprone.annotations.concurrent.GuardedBy; +import org.apache.druid.client.indexing.ClientKillUnusedSegmentsByIdTaskQuery; +import org.apache.druid.indexer.TaskStatus; +import org.apache.druid.indexing.common.TaskLock; +import org.apache.druid.indexing.common.TaskLockType; +import org.apache.druid.indexing.common.TaskToolbox; +import org.apache.druid.indexing.common.actions.RetrieveUnusedSegmentsByIdAction; +import org.apache.druid.indexing.common.actions.SegmentNukeAction; +import org.apache.druid.indexing.common.actions.TaskActionClient; +import org.apache.druid.indexing.common.actions.TaskLocks; +import org.apache.druid.indexing.common.actions.TimeChunkLockTryAcquireAction; +import org.apache.druid.indexing.common.config.TaskConfig; +import org.apache.druid.java.util.common.ISE; +import org.apache.druid.java.util.common.StringUtils; +import org.apache.druid.server.security.ResourceAction; +import org.apache.druid.timeline.DataSegment; +import org.apache.druid.timeline.SegmentId; +import org.joda.time.DateTime; + +import javax.annotation.Nonnull; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.NavigableMap; +import java.util.Set; +import java.util.TreeMap; +import java.util.stream.Collectors; + +/** + * Kill a list of segments by {@link SegmentId#toString()}. The segments provided must belong to the datasource given, + * and be marked unused, or they will be skipped from deletion. The client representation of this task is + * {@link ClientKillUnusedSegmentsByIdTaskQuery}. JSON serialization fields of this class must correspond to those of + * {@link ClientKillUnusedSegmentsByIdTaskQuery}, except for "id" and "context" fields. + */ +public class KillUnusedSegmentsByIdTask extends AbstractTask +{ + public static final String TYPE = "kill_by_segment_id"; + private final List<String> segmentIds; + + @GuardedBy("this") + private List<DataSegment> unusedSegmentsTokill; + + @JsonCreator + public KillUnusedSegmentsByIdTask( + @JsonProperty("id") String id, + @JsonProperty("dataSource") String dataSource, + @JsonProperty("context") Map<String, Object> context, + @JsonProperty("segmentIds") List<String> segmentIds + ) + { + this( + getOrMakeId(id, TYPE, dataSource, null), + dataSource, + segmentIds, + context + ); + } + + protected KillUnusedSegmentsByIdTask( + String id, + String dataSource, + List<String> segmentIds, + Map<String, Object> context + ) + { + this(id, id, new TaskResource(id, 1), dataSource, segmentIds, context); + } + + protected KillUnusedSegmentsByIdTask( + String id, + String groupId, + TaskResource taskResource, + String dataSource, + List<String> segmentIds, + Map<String, Object> context + ) + { + super(id, groupId, taskResource, dataSource, context); + this.segmentIds = Preconditions.checkNotNull(segmentIds, "segmentIds"); + } + + @JsonProperty + public List<String> getSegmentIds() + { + return segmentIds; + } + + @Override + public String getType() + { + return TYPE; + } + + @Nonnull + @JsonIgnore + @Override + public Set<ResourceAction> getInputSourceResources() + { + return ImmutableSet.of(); + } + + @Override + public TaskStatus runTask(TaskToolbox toolbox) throws Exception + { + final NavigableMap<DateTime, List<TaskLock>> taskLockMap = getTaskLockMap(toolbox.getTaskActionClient()); + + // List unused segments + List<DataSegment> unusedSegments; + synchronized (this) { + unusedSegments = ImmutableList.copyOf(unusedSegmentsTokill); + } + + if (!TaskLocks.isLockCoversSegments(taskLockMap, unusedSegments)) { + throw new ISE( + "Locks[%s] for task[%s] can't cover segments[%s]", + taskLockMap.values().stream().flatMap(List::stream).collect(Collectors.toList()), + getId(), + unusedSegments + ); + } + + // Kill segments + toolbox.getTaskActionClient().submit(new SegmentNukeAction(new HashSet<>(unusedSegments))); + for (DataSegment segment : unusedSegments) { + toolbox.getDataSegmentKiller().kill(segment); + } + + return TaskStatus.success(getId()); + } + + @Override + public boolean isReady(TaskActionClient taskActionClient) throws Exception + { + List<DataSegment> unusedSegments = + taskActionClient.submit(new RetrieveUnusedSegmentsByIdAction(getDataSource(), getSegmentIds())); + for (DataSegment unusedSegment : unusedSegments) { + final TaskLock lock = taskActionClient.submit( + new TimeChunkLockTryAcquireAction( + TaskLockType.EXCLUSIVE, + unusedSegment.getInterval() + ) + ); + if (lock == null) { + return false; + } + if (lock.isRevoked()) { + throw new ISE(StringUtils.format("Lock for interval [%s] was revoked.", unusedSegment.getInterval())); Review Comment: or be blocked on waiting for a lock to be released -- 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]
