tillrohrmann commented on a change in pull request #7662: [FLINK-10431] 
Extraction of scheduling-related code from SlotPool into preliminary Scheduler 
(rebased)
URL: https://github.com/apache/flink/pull/7662#discussion_r255103880
 
 

 ##########
 File path: 
flink-runtime/src/main/java/org/apache/flink/runtime/jobmaster/slotpool/Scheduler.java
 ##########
 @@ -0,0 +1,519 @@
+/*
+ * 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.jobmaster.slotpool;
+
+import org.apache.flink.api.common.time.Time;
+import org.apache.flink.runtime.clusterframework.types.SlotProfile;
+import org.apache.flink.runtime.concurrent.ComponentMainThreadExecutor;
+import org.apache.flink.runtime.concurrent.FutureUtils;
+import org.apache.flink.runtime.instance.SlotSharingGroupId;
+import org.apache.flink.runtime.jobmanager.scheduler.CoLocationConstraint;
+import org.apache.flink.runtime.jobmanager.scheduler.Locality;
+import 
org.apache.flink.runtime.jobmanager.scheduler.NoResourceAvailableException;
+import org.apache.flink.runtime.jobmanager.scheduler.ScheduledUnit;
+import org.apache.flink.runtime.jobmaster.LogicalSlot;
+import org.apache.flink.runtime.jobmaster.SlotContext;
+import org.apache.flink.runtime.jobmaster.SlotInfo;
+import org.apache.flink.runtime.jobmaster.SlotOwner;
+import org.apache.flink.runtime.jobmaster.SlotRequestId;
+import org.apache.flink.util.AbstractID;
+import org.apache.flink.util.FlinkException;
+import org.apache.flink.util.Preconditions;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CompletionException;
+
+/**
+ * Scheduler that assigns tasks to slots. This class is currently work in 
progress, comments will be updated as we
+ * move forward.
+ */
+public class Scheduler implements SlotProvider, SlotOwner {
+
+       /** Logger */
+       private final Logger log = LoggerFactory.getLogger(getClass());
+
+       /** Strategy that selects the best slot for a given slot allocation 
request. */
+       @Nonnull
+       private final SlotSelectionStrategy slotSelectionStrategy;
+
+       /** The slot pool from which slots are allocated. */
+       @Nonnull
+       private final SlotPool slotPool;
+
+       /** Executor for running tasks in the job master's main thread. */
+       @Nonnull
+       private ComponentMainThreadExecutor componentMainThreadExecutor;
+
+       /** Managers for the different slot sharing groups. */
+       @Nonnull
+       private final Map<SlotSharingGroupId, SlotSharingManager> 
slotSharingManagersMap;
+
+       public Scheduler(
+               @Nonnull Map<SlotSharingGroupId, SlotSharingManager> 
slotSharingManagersMap,
+               @Nonnull SlotSelectionStrategy slotSelectionStrategy,
+               @Nonnull SlotPool slotPool) {
+
+               this.slotSelectionStrategy = slotSelectionStrategy;
+               this.slotSharingManagersMap = slotSharingManagersMap;
+               this.slotPool = slotPool;
+               this.componentMainThreadExecutor = new 
ComponentMainThreadExecutor.DummyComponentMainThreadExecutor(
+                       "Scheduler is not initialized with proper main thread 
executor. " +
+                               "Call to Scheduler.start(...) required.");
+       }
+
+       public void start(@Nonnull ComponentMainThreadExecutor 
mainThreadExecutor) {
+               this.componentMainThreadExecutor = mainThreadExecutor;
+       }
+
+       //---------------------------
+
+       @Override
+       public CompletableFuture<LogicalSlot> allocateSlot(
+               SlotRequestId slotRequestId,
+               ScheduledUnit scheduledUnit,
+               SlotProfile slotProfile,
+               boolean allowQueuedScheduling,
+               Time allocationTimeout) {
+               log.debug("Received slot request [{}] for task: {}", 
slotRequestId, scheduledUnit.getTaskToExecute());
+
+               componentMainThreadExecutor.assertRunningInMainThread();
+
+               final CompletableFuture<LogicalSlot> allocationResultFuture = 
new CompletableFuture<>();
+
+               CompletableFuture<LogicalSlot> allocationFuture = 
scheduledUnit.getSlotSharingGroupId() == null ?
+                       allocateSingleSlot(slotRequestId, slotProfile, 
allowQueuedScheduling, allocationTimeout) :
+                       allocateSharedSlot(slotRequestId, scheduledUnit, 
slotProfile, allowQueuedScheduling, allocationTimeout);
+
+               allocationFuture.whenComplete((LogicalSlot slot, Throwable 
failure) -> {
+                       if (failure != null) {
+                               cancelSlotRequest(
+                                       slotRequestId,
+                                       scheduledUnit.getSlotSharingGroupId(),
+                                       failure);
+                               
allocationResultFuture.completeExceptionally(failure);
+                       } else {
+                               allocationResultFuture.complete(slot);
+                       }
+               });
+
+               return allocationResultFuture;
+       }
+
+       @Override
+       public void cancelSlotRequest(
+               SlotRequestId slotRequestId,
+               @Nullable SlotSharingGroupId slotSharingGroupId,
+               Throwable cause) {
+
+               componentMainThreadExecutor.assertRunningInMainThread();
+
+               if (slotSharingGroupId != null) {
+                       releaseSharedSlot(slotRequestId, slotSharingGroupId, 
cause);
+               } else {
+                       slotPool.releaseSlot(slotRequestId, cause);
+               }
+       }
+
+       @Override
+       public void returnAllocatedSlot(LogicalSlot logicalSlot) {
+               SlotRequestId slotRequestId = logicalSlot.getSlotRequestId();
+               SlotSharingGroupId slotSharingGroupId = 
logicalSlot.getSlotSharingGroupId();
+               FlinkException cause = new FlinkException("Slot is being 
returned to the SlotPool.");
+               cancelSlotRequest(slotRequestId, slotSharingGroupId, cause);
+       }
+
+       //---------------------------
+
+       private CompletableFuture<LogicalSlot> allocateSingleSlot(
+               SlotRequestId slotRequestId,
+               SlotProfile slotProfile,
+               boolean allowQueuedScheduling,
+               Time allocationTimeout) {
+
+               Optional<SlotAndLocality> slotAndLocality =
+                       tryAllocateFromAvailable(slotRequestId, slotProfile);
+
+               if (slotAndLocality.isPresent()) {
+                       // already successful from available
+                       try {
+                               return CompletableFuture.completedFuture(
+                                       
completeAllocationByAssigningPayload(slotRequestId, slotAndLocality.get()));
+                       } catch (FlinkException e) {
+                               return FutureUtils.completedExceptionally(e);
+                       }
+               } else if (allowQueuedScheduling) {
+                       // we allocate by requesting a new slot
+                       return slotPool
+                               .requestNewAllocatedSlot(slotRequestId, 
slotProfile.getResourceProfile(), allocationTimeout)
+                               .thenApply((AllocatedSlotContext allocatedSlot) 
-> {
+                                       try {
+                                               return 
completeAllocationByAssigningPayload(slotRequestId, new 
SlotAndLocality(allocatedSlot, Locality.UNKNOWN));
+                                       } catch (FlinkException e) {
+                                               throw new 
CompletionException(e);
+                                       }
+                               });
+               } else {
+                       // failed to allocate
+                       return FutureUtils.completedExceptionally(
+                               new NoResourceAvailableException("Could not 
allocate a simple slot for " + slotRequestId + '.'));
+               }
+       }
+
+       @Nonnull
+       private LogicalSlot completeAllocationByAssigningPayload(
+               @Nonnull SlotRequestId slotRequestId,
+               @Nonnull SlotAndLocality slotAndLocality) throws FlinkException 
{
+
+               final AllocatedSlotContext allocatedSlot = 
slotAndLocality.getSlot();
+
+               final SingleLogicalSlot singleTaskSlot = new SingleLogicalSlot(
+                       slotRequestId,
+                       allocatedSlot,
+                       null,
+                       slotAndLocality.getLocality(),
+                       this);
+
+               if (allocatedSlot.tryAssignPayload(singleTaskSlot)) {
+                       return singleTaskSlot;
+               } else {
+                       final FlinkException flinkException =
+                               new FlinkException("Could not assign payload to 
allocated slot " + allocatedSlot.getAllocationId() + '.');
+                       slotPool.releaseSlot(slotRequestId, flinkException);
+                       throw flinkException;
+               }
+       }
+
+       private Optional<SlotAndLocality> tryAllocateFromAvailable(
+               @Nonnull SlotRequestId slotRequestId,
+               @Nonnull SlotProfile slotProfile) {
+
+               Collection<SlotInfo> slotInfoList = 
slotPool.getAvailableSlotsInformation();
+
+               Optional<SlotSelectionStrategy.SlotInfoAndLocality> 
selectedAvailableSlot =
+                       
slotSelectionStrategy.selectBestSlotForProfile(slotInfoList, slotProfile);
+
+
 
 Review comment:
   two line breaks

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on 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

Reply via email to