GJL commented on a change in pull request #8486: [FLINK-12372] [runtime] implement ExecutionSlotAllocator URL: https://github.com/apache/flink/pull/8486#discussion_r289773084
########## File path: flink-runtime/src/main/java/org/apache/flink/runtime/scheduler/DefaultExecutionSlotAllocator.java ########## @@ -0,0 +1,294 @@ +/* + * 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.api.common.time.Time; +import org.apache.flink.runtime.clusterframework.types.AllocationID; +import org.apache.flink.runtime.clusterframework.types.SlotProfile; +import org.apache.flink.runtime.concurrent.FutureUtils; +import org.apache.flink.runtime.instance.SlotSharingGroupId; +import org.apache.flink.runtime.jobmanager.scheduler.ScheduledUnit; +import org.apache.flink.runtime.jobmaster.LogicalSlot; +import org.apache.flink.runtime.jobmaster.SlotRequestId; +import org.apache.flink.runtime.jobmaster.slotpool.Scheduler; +import org.apache.flink.runtime.jobmaster.slotpool.SlotProvider; +import org.apache.flink.runtime.scheduler.strategy.ExecutionVertexID; +import org.apache.flink.runtime.taskmanager.TaskManagerLocation; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.concurrent.CompletableFuture; + +import static org.apache.flink.runtime.executiongraph.ExecutionVertex.MAX_DISTINCT_LOCATIONS_TO_CONSIDER; +import static org.apache.flink.util.Preconditions.checkNotNull; + +/** + * Default {@link ExecutionSlotAllocator} which will use {@link SlotProvider} to allocate slots and + * keep the unfulfilled requests for further cancellation. + */ +public class DefaultExecutionSlotAllocator implements ExecutionSlotAllocator { + + static final Logger LOG = LoggerFactory.getLogger(DefaultExecutionSlotAllocator.class); + + /** + * Store the uncompleted slot assignments. + */ + private final Map<ExecutionVertexID, SlotExecutionVertexAssignment> pendingSlotAssignments; + + private final SlotProvider slotProvider; + + private final InputsLocationsRetriever inputsLocationsRetriever; + + private final Time allocationTimeout; + + private final Boolean isQueuedSchedulingAllowed; + + public DefaultExecutionSlotAllocator( + SlotProvider slotProvider, + InputsLocationsRetriever inputsLocationsRetriever, + Time allocationTimeout, + boolean isQueuedSchedulingAllowed) { + this.slotProvider = checkNotNull(slotProvider); + this.inputsLocationsRetriever = checkNotNull(inputsLocationsRetriever); + this.allocationTimeout = checkNotNull(allocationTimeout); + this.isQueuedSchedulingAllowed = isQueuedSchedulingAllowed; + + pendingSlotAssignments = new HashMap<>(); + } + + @Override + public Collection<SlotExecutionVertexAssignment> allocateSlotsFor( + Collection<ExecutionVertexSchedulingRequirements> executionVertexSchedulingRequirements) { + + List<SlotExecutionVertexAssignment> slotExecutionVertexAssignments = + new ArrayList<>(executionVertexSchedulingRequirements.size()); + + Set<AllocationID> allPreviousAllocationIds = + computeAllPriorAllocationIdsIfRequiredByScheduling(executionVertexSchedulingRequirements); + + for (ExecutionVertexSchedulingRequirements schedulingRequirements : executionVertexSchedulingRequirements) { + final ExecutionVertexID executionVertexId = schedulingRequirements.getExecutionVertexId(); + final SlotRequestId slotRequestId = new SlotRequestId(); + final SlotSharingGroupId slotSharingGroupId = schedulingRequirements.getSlotSharingGroupId(); + + if (LOG.isDebugEnabled()) { + LOG.debug("Allocate slot with id {} for execution {}", slotRequestId, executionVertexId); Review comment: `isDebugEnabled()` is not needed. https://www.slf4j.org/faq.html#logging_performance > There exists a very convenient alternative based on message formats. Assuming entry is an object, you can write: ``` Object entry = new SomeObject(); logger.debug("The entry is {}.", entry); ``` > After evaluating whether to log or not, and only if the decision is affirmative, will the logger implementation format the message and replace the '{}' pair with the string value of entry. In other words, this form does not incur the cost of parameter construction in case the log statement is disabled. > A two argument variant is also available. For example, you can write: ``` logger.debug("The new entry is {}. It replaces {}.", entry, oldEntry); ``` > If three or more arguments need to be passed, you can make use of the Object... variant of the printing methods. For example, you can write: ``` logger.debug("Value {} was inserted between {} and {}.", newVal, below, above); ``` > This form incurs the hidden cost of construction of an Object[] (object array) which is usually very small. The one and two argument variants do not incur this hidden cost and exist solely for this reason (efficiency). The slf4j-api would be smaller/cleaner with only the Object... variant. ---------------------------------------------------------------- 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] With regards, Apache Git Services
