zhuzhurk commented on a change in pull request #12423: URL: https://github.com/apache/flink/pull/12423#discussion_r436028808
########## File path: flink-runtime/src/main/java/org/apache/flink/runtime/scheduler/AbstractExecutionSlotAllocator.java ########## @@ -0,0 +1,134 @@ +/* + * 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.flink.runtime.scheduler; + +import org.apache.flink.annotation.VisibleForTesting; +import org.apache.flink.runtime.clusterframework.types.AllocationID; +import org.apache.flink.runtime.jobmaster.LogicalSlot; +import org.apache.flink.runtime.scheduler.strategy.ExecutionVertexID; +import org.apache.flink.runtime.taskmanager.TaskManagerLocation; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.concurrent.CompletableFuture; +import java.util.function.Consumer; +import java.util.stream.Collectors; + +import static org.apache.flink.util.Preconditions.checkNotNull; +import static org.apache.flink.util.Preconditions.checkState; + +/** + * Base class for all {@link ExecutionSlotAllocator}. It is responsible to allocate slots for tasks and + * keep the unfulfilled slot requests for further cancellation. + */ +public abstract class AbstractExecutionSlotAllocator implements ExecutionSlotAllocator { + + /** + * Store the uncompleted slot assignments. + */ + protected final Map<ExecutionVertexID, SlotExecutionVertexAssignment> pendingSlotAssignments; + + protected final PreferredLocationsRetriever preferredLocationsRetriever; + + public AbstractExecutionSlotAllocator(final PreferredLocationsRetriever preferredLocationsRetriever) { + this.preferredLocationsRetriever = checkNotNull(preferredLocationsRetriever); + this.pendingSlotAssignments = new HashMap<>(); + } + + @Override + public void cancel(final ExecutionVertexID executionVertexId) { + final SlotExecutionVertexAssignment slotExecutionVertexAssignment = pendingSlotAssignments.get(executionVertexId); + if (slotExecutionVertexAssignment != null) { + slotExecutionVertexAssignment.getLogicalSlotFuture().cancel(false); + } + } + + @Override + public CompletableFuture<Void> stop() { + final List<ExecutionVertexID> executionVertexIds = new ArrayList<>(pendingSlotAssignments.keySet()); + executionVertexIds.forEach(this::cancel); + Review comment: I will make the change "getNumberOfPendingSlotAssignments -> getPendingSlotAssignments" in #12256 when introducing `AbstractExecutionSlotAllocator`. It would be good for verifying the existence of a given assignment. ---------------------------------------------------------------- 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: [email protected]
