GJL commented on a change in pull request #12256: URL: https://github.com/apache/flink/pull/12256#discussion_r429217557
########## File path: flink-runtime/src/main/java/org/apache/flink/runtime/jobmaster/slotpool/PhysicalSlotRequest.java ########## @@ -0,0 +1,79 @@ +/* + * 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.runtime.clusterframework.types.SlotProfile; +import org.apache.flink.runtime.jobmaster.SlotRequestId; + +/** + * Represents a request for a physical slot. + */ +public class PhysicalSlotRequest { + + private SlotRequestId slotRequestId; + + private SlotProfile slotProfile; + + private boolean slotWillBeOccupiedIndefinitely; + + public PhysicalSlotRequest( + final SlotRequestId slotRequestId, + final SlotProfile slotProfile, + final boolean slotWillBeOccupiedIndefinitely) { + + this.slotRequestId = slotRequestId; + this.slotProfile = slotProfile; + this.slotWillBeOccupiedIndefinitely = slotWillBeOccupiedIndefinitely; + } + + public SlotRequestId getSlotRequestId() { + return slotRequestId; + } + + public SlotProfile getSlotProfile() { + return slotProfile; + } + + boolean willSlotBeOccupiedIndefinitely() { + return slotWillBeOccupiedIndefinitely; + } + + /** + * Result of a {@link PhysicalSlotRequest}. + */ + public static class Result { + + private SlotRequestId slotRequestId; Review comment: Should be `final`. ########## File path: flink-runtime/src/main/java/org/apache/flink/runtime/jobmaster/slotpool/PhysicalSlotRequest.java ########## @@ -0,0 +1,79 @@ +/* + * 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.runtime.clusterframework.types.SlotProfile; +import org.apache.flink.runtime.jobmaster.SlotRequestId; + +/** + * Represents a request for a physical slot. + */ +public class PhysicalSlotRequest { + + private SlotRequestId slotRequestId; Review comment: Should be `final`. ########## File path: flink-runtime/src/main/java/org/apache/flink/runtime/scheduler/DefaultExecutionSlotAllocator.java ########## @@ -67,23 +76,211 @@ private final SlotProviderStrategy slotProviderStrategy; + private final SlotOwner slotOwner; + private final InputsLocationsRetriever inputsLocationsRetriever; + // temporary hack. can be removed along with the individual slot allocation code path + // once bulk slot allocation is fully functional + private final boolean enableBulkSlotAllocation; Review comment: Why not use Javadoc style comments? Furthermore, I would add more details: 1. What does this flag do? 1. Why is it a temporary hack? ########## File path: flink-runtime/src/main/java/org/apache/flink/runtime/scheduler/DefaultExecutionSlotAllocator.java ########## @@ -67,23 +76,211 @@ private final SlotProviderStrategy slotProviderStrategy; + private final SlotOwner slotOwner; + private final InputsLocationsRetriever inputsLocationsRetriever; + // temporary hack. can be removed along with the individual slot allocation code path + // once bulk slot allocation is fully functional + private final boolean enableBulkSlotAllocation; Review comment: Would it be possible to move the new logic to a new class (e.g., `OneSlotPerExecutionSlotAllocator`) that subclasses from `DefaultExecutionSlotAllocator`? ########## File path: flink-runtime/src/main/java/org/apache/flink/runtime/jobmaster/slotpool/SingleLogicalSlot.java ########## @@ -168,6 +185,11 @@ public void release(Throwable cause) { releaseFuture.complete(null); } + @Override + public boolean willOccupySlotIndefinitely() { Review comment: Method name doesn't match the field name `willOccupySlotIndefinitely` vs `willBeOccupiedIndefinitely`. Is it intentional? ########## File path: flink-runtime/src/main/java/org/apache/flink/runtime/jobmaster/slotpool/PhysicalSlotRequestBulkTracker.java ########## @@ -0,0 +1,86 @@ +/* + * 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.annotation.VisibleForTesting; +import org.apache.flink.util.clock.Clock; + +import java.util.IdentityHashMap; +import java.util.Map; +import java.util.Set; + +import static org.apache.flink.util.Preconditions.checkState; + +/** + * Tracks physical slot request bulks. + */ +class PhysicalSlotRequestBulkTracker { + + private final Clock clock; + + /** Timestamps indicate when bulks become unfulfillable. */ + private final Map<PhysicalSlotRequestBulk, Long> bulkUnfulfillableTimestamps; + + PhysicalSlotRequestBulkTracker(final Clock clock) { + this.clock = clock; + this.bulkUnfulfillableTimestamps = new IdentityHashMap<>(); + } + + void track(final PhysicalSlotRequestBulk bulk) { + // a bulk is initially unfulfillable + bulkUnfulfillableTimestamps.put(bulk, clock.relativeTimeMillis()); + } + + void untrack(final PhysicalSlotRequestBulk bulk) { + bulkUnfulfillableTimestamps.remove(bulk); + } + + boolean isTracked(final PhysicalSlotRequestBulk bulk) { + return bulkUnfulfillableTimestamps.containsKey(bulk); + } + + void markBulkFulfillable(final PhysicalSlotRequestBulk bulk) { + checkState(isTracked(bulk)); + + bulkUnfulfillableTimestamps.put(bulk, Long.MAX_VALUE); + } + + void markBulkUnfulfillable(final PhysicalSlotRequestBulk bulk, final long currentTimestamp) { Review comment: What is the benefit of passing an external timestamp instead of calling `clock.relativeTimeMillis()` inside this method? ########## File path: flink-runtime/src/main/java/org/apache/flink/runtime/scheduler/DefaultExecutionSlotAllocator.java ########## @@ -67,23 +76,211 @@ private final SlotProviderStrategy slotProviderStrategy; + private final SlotOwner slotOwner; + private final InputsLocationsRetriever inputsLocationsRetriever; + // temporary hack. can be removed along with the individual slot allocation code path + // once bulk slot allocation is fully functional + private final boolean enableBulkSlotAllocation; + public DefaultExecutionSlotAllocator( SlotProviderStrategy slotProviderStrategy, - InputsLocationsRetriever inputsLocationsRetriever) { + InputsLocationsRetriever inputsLocationsRetriever, + boolean enableBulkSlotAllocation) { this.slotProviderStrategy = checkNotNull(slotProviderStrategy); this.inputsLocationsRetriever = checkNotNull(inputsLocationsRetriever); + this.enableBulkSlotAllocation = enableBulkSlotAllocation; + + this.slotOwner = new DefaultExecutionSlotAllocatorSlotOwner(); pendingSlotAssignments = new HashMap<>(); } + /** + * Allocate slots in bulks. It has some limitations at the moment and is for pipelined region scheduling only. + * Slot sharing will be ignored. + * Co-location constraints are not allowed as a result. + * Intra-bulk input location preferences will be ignored. + */ + private List<SlotExecutionVertexAssignment> allocateSlotsInBulks( + final List<ExecutionVertexSchedulingRequirements> executionVertexSchedulingRequirements) { + + validateNoCoLocationConstraint(executionVertexSchedulingRequirements); + + final Set<ExecutionVertexID> allExecutionVertexIds = executionVertexSchedulingRequirements.stream() + .map(ExecutionVertexSchedulingRequirements::getExecutionVertexId) + .collect(Collectors.toSet()); + + final Map<ExecutionVertexID, SlotRequestId> executionVertexSlotRequestIds = + generateExecutionVertexSlotRequestIds(allExecutionVertexIds); + + final List<SlotExecutionVertexAssignment> slotExecutionVertexAssignments = + createSlotExecutionVertexAssignments(executionVertexSchedulingRequirements, executionVertexSlotRequestIds); + + final List<CompletableFuture<PhysicalSlotRequest>> physicalSlotRequestFutures = + createPhysicalSlotRequestFutures( + executionVertexSchedulingRequirements, + allExecutionVertexIds, + executionVertexSlotRequestIds); + + allocateSlotsForAssignments( + physicalSlotRequestFutures, + slotExecutionVertexAssignments, + executionVertexSlotRequestIds); + + return slotExecutionVertexAssignments; + } + + private static void validateNoCoLocationConstraint( + final Collection<ExecutionVertexSchedulingRequirements> schedulingRequirements) { + + final boolean hasCoLocationConstraint = schedulingRequirements.stream() + .anyMatch(r -> r.getCoLocationConstraint() != null); + checkState( + !hasCoLocationConstraint, + "Jobs with co-location constraints are not allowed to run with pipelined region scheduling strategy."); + } + + private Map<ExecutionVertexID, SlotRequestId> generateExecutionVertexSlotRequestIds( + final Set<ExecutionVertexID> allExecutionVertexIds) { + + final Map<ExecutionVertexID, SlotRequestId> executionVertexSlotRequestIds = new HashMap<>(); + for (ExecutionVertexID executionVertexId : allExecutionVertexIds) { + executionVertexSlotRequestIds.put(executionVertexId, new SlotRequestId()); + } + return executionVertexSlotRequestIds; + } + + private List<SlotExecutionVertexAssignment> createSlotExecutionVertexAssignments( + final List<ExecutionVertexSchedulingRequirements> executionVertexSchedulingRequirements, + final Map<ExecutionVertexID, SlotRequestId> executionVertexSlotRequestIds) { + + final List<SlotExecutionVertexAssignment> slotExecutionVertexAssignments = + new ArrayList<>(executionVertexSchedulingRequirements.size()); + for (ExecutionVertexSchedulingRequirements schedulingRequirements : executionVertexSchedulingRequirements) { + final ExecutionVertexID executionVertexId = schedulingRequirements.getExecutionVertexId(); + final SlotRequestId slotRequestId = executionVertexSlotRequestIds.get(executionVertexId); + + final SlotExecutionVertexAssignment slotExecutionVertexAssignment = + createAndRegisterSlotExecutionVertexAssignment( + executionVertexId, + new CompletableFuture<>(), + slotRequestId, + null); + slotExecutionVertexAssignments.add(slotExecutionVertexAssignment); + } + + return slotExecutionVertexAssignments; + } + + private List<CompletableFuture<PhysicalSlotRequest>> createPhysicalSlotRequestFutures( + final List<ExecutionVertexSchedulingRequirements> executionVertexSchedulingRequirements, + final Set<ExecutionVertexID> allExecutionVertexIds, + final Map<ExecutionVertexID, SlotRequestId> executionVertexSlotRequestIds) { + + final Set<AllocationID> allPreviousAllocationIds = + computeAllPriorAllocationIds(executionVertexSchedulingRequirements); + + final List<CompletableFuture<PhysicalSlotRequest>> physicalSlotRequestFutures = + new ArrayList<>(executionVertexSchedulingRequirements.size()); + for (ExecutionVertexSchedulingRequirements schedulingRequirements : executionVertexSchedulingRequirements) { + final ExecutionVertexID executionVertexId = schedulingRequirements.getExecutionVertexId(); + final SlotRequestId slotRequestId = executionVertexSlotRequestIds.get(executionVertexId); + + LOG.debug("Allocate slot with id {} for execution {}", slotRequestId, executionVertexId); + + // use the task resource profile as the physical slot resource requirement since slot sharing is ignored here + final ResourceProfile physicalSlotResourceProfile = schedulingRequirements.getTaskResourceProfile(); + + final CompletableFuture<SlotProfile> slotProfileFuture = createSlotProfile( + schedulingRequirements, + allExecutionVertexIds, + physicalSlotResourceProfile, + allPreviousAllocationIds); + final CompletableFuture<PhysicalSlotRequest> physicalSlotRequestFuture = + slotProfileFuture.thenApply( + slotProfile -> createPhysicalSlotRequest(slotRequestId, slotProfile)); + physicalSlotRequestFutures.add(physicalSlotRequestFuture); + } + + return physicalSlotRequestFutures; + } + + private PhysicalSlotRequest createPhysicalSlotRequest( + final SlotRequestId slotRequestId, + final SlotProfile slotProfile) { + return new PhysicalSlotRequest(slotRequestId, slotProfile, slotProviderStrategy.willSlotBeOccupiedIndefinitely()); + } + + private void allocateSlotsForAssignments( + final List<CompletableFuture<PhysicalSlotRequest>> physicalSlotRequestFutures, + final List<SlotExecutionVertexAssignment> slotExecutionVertexAssignments, + final Map<ExecutionVertexID, SlotRequestId> executionVertexSlotRequestIds) { + + final Map<ExecutionVertexID, SlotExecutionVertexAssignment> vertexToAssignmentMapping = + slotExecutionVertexAssignments.stream() + .collect(Collectors.toMap(SlotExecutionVertexAssignment::getExecutionVertexId, Function.identity())); + + final Map<SlotRequestId, ExecutionVertexID> slotToVertexMapping = new HashMap<>(); + for (Map.Entry<ExecutionVertexID, SlotRequestId> entry : executionVertexSlotRequestIds.entrySet()) { + slotToVertexMapping.put(entry.getValue(), entry.getKey()); + } + + FutureUtils.combineAll(physicalSlotRequestFutures) + .thenCompose(physicalSlotRequests -> slotProviderStrategy.allocatePhysicalSlots(physicalSlotRequests)) + .thenAccept(physicalSlotRequestResults -> { + for (PhysicalSlotRequest.Result result : physicalSlotRequestResults) { + final SlotRequestId slotRequestId = result.getSlotRequestId(); + final ExecutionVertexID executionVertexId = slotToVertexMapping.get(slotRequestId); + try { + final LogicalSlot logicalSlot = allocateSingleLogicalSlotFromPhysicalSlot( + slotRequestId, + result.getPhysicalSlot()); + vertexToAssignmentMapping.get(executionVertexId).getLogicalSlotFuture().complete(logicalSlot); + } catch (Exception ex) { + throw new CompletionException(ex); + } + } + }) + .exceptionally(ex -> { + executionVertexSlotRequestIds.values().stream() + .forEach(slotRequestId -> { + final ExecutionVertexID executionVertexId = slotToVertexMapping.get(slotRequestId); + vertexToAssignmentMapping.get(executionVertexId).getLogicalSlotFuture().completeExceptionally(ex); + }); + return null; + }); + } + + private LogicalSlot allocateSingleLogicalSlotFromPhysicalSlot( + final SlotRequestId slotRequestId, + final PhysicalSlot physicalSlot) throws FlinkException { + + final SingleLogicalSlot singleTaskSlot = new SingleLogicalSlot( + slotRequestId, + physicalSlot, + null, + Locality.UNKNOWN, + slotOwner); + + if (physicalSlot.tryAssignPayload(singleTaskSlot)) { + return singleTaskSlot; + } else { + throw new FlinkException("Could not assign payload to allocated slot " + physicalSlot.getAllocationId() + '.'); + } + } + @Override public List<SlotExecutionVertexAssignment> allocateSlotsFor( Review comment: I would prefer this method (`allocateSlotsFor()`) to be the first in the class. ---------------------------------------------------------------- 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]
