|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.flink.runtime.scheduler.declarative; |
| 20 | + |
| 21 | +import org.apache.flink.runtime.jobmanager.scheduler.Locality; |
| 22 | +import org.apache.flink.runtime.jobmaster.LogicalSlot; |
| 23 | +import org.apache.flink.runtime.jobmaster.SlotOwner; |
| 24 | +import org.apache.flink.runtime.jobmaster.SlotRequestId; |
| 25 | +import org.apache.flink.runtime.jobmaster.slotpool.PhysicalSlot; |
| 26 | +import org.apache.flink.runtime.jobmaster.slotpool.SingleLogicalSlot; |
| 27 | +import org.apache.flink.util.Preconditions; |
| 28 | + |
| 29 | +import org.slf4j.Logger; |
| 30 | +import org.slf4j.LoggerFactory; |
| 31 | + |
| 32 | +import java.util.ArrayList; |
| 33 | +import java.util.HashMap; |
| 34 | +import java.util.List; |
| 35 | +import java.util.Map; |
| 36 | + |
| 37 | +/** Shared slot implementation for the {@link DeclarativeScheduler}. */ |
| 38 | +public class SharedSlot implements SlotOwner, PhysicalSlot.Payload { |
| 39 | + private static final Logger LOG = LoggerFactory.getLogger(SharedSlot.class); |
| 40 | + |
| 41 | + private final SlotRequestId physicalSlotRequestId; |
| 42 | + |
| 43 | + private final PhysicalSlot physicalSlot; |
| 44 | + |
| 45 | + private final Runnable externalReleaseCallback; |
| 46 | + |
| 47 | + private final Map<SlotRequestId, LogicalSlot> allocatedLogicalSlots; |
| 48 | + |
| 49 | + private final boolean slotWillBeOccupiedIndefinitely; |
| 50 | + |
| 51 | + private State state; |
| 52 | + |
| 53 | + public SharedSlot( |
| 54 | + SlotRequestId physicalSlotRequestId, |
| 55 | + PhysicalSlot physicalSlot, |
| 56 | + boolean slotWillBeOccupiedIndefinitely, |
| 57 | + Runnable externalReleaseCallback) { |
| 58 | + this.physicalSlotRequestId = physicalSlotRequestId; |
| 59 | + this.physicalSlot = physicalSlot; |
| 60 | + this.slotWillBeOccupiedIndefinitely = slotWillBeOccupiedIndefinitely; |
| 61 | + this.externalReleaseCallback = externalReleaseCallback; |
| 62 | + this.state = State.ALLOCATED; |
| 63 | + this.allocatedLogicalSlots = new HashMap<>(); |
| 64 | + physicalSlot.tryAssignPayload(this); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Registers an allocation request for a logical slot. |
| 69 | + * |
| 70 | + * <p>The logical slot request is complete once the underlying physical slot request is |
| 71 | + * complete. |
| 72 | + * |
| 73 | + * @return the logical slot |
| 74 | + */ |
| 75 | + public LogicalSlot allocateLogicalSlot() { |
| 76 | + LOG.debug("Allocating logical slot from shared slot ({})", physicalSlotRequestId); |
| 77 | + return new SingleLogicalSlot( |
| 78 | + new SlotRequestId(), |
| 79 | + physicalSlot, |
| 80 | + null, |
| 81 | + Locality.UNKNOWN, |
| 82 | + this, |
| 83 | + slotWillBeOccupiedIndefinitely); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public void returnLogicalSlot(LogicalSlot logicalSlot) { |
| 88 | + LOG.debug("Returning logical slot to shared slot ({})", physicalSlotRequestId); |
| 89 | + Preconditions.checkState( |
| 90 | + allocatedLogicalSlots.remove(logicalSlot.getSlotRequestId()) != null, |
| 91 | + "Trying to remove a logical slot request which has been either already removed or never created."); |
| 92 | + tryReleaseExternally(); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public void release(Throwable cause) { |
| 97 | + LOG.debug("Release shared slot ({})", physicalSlotRequestId); |
| 98 | + |
| 99 | + // copy the logical slot collection to avoid ConcurrentModificationException |
| 100 | + // if logical slot releases cause cancellation of other executions |
| 101 | + // which will try to call returnLogicalSlot and modify requestedLogicalSlots collection |
| 102 | + final List<LogicalSlot> collect = new ArrayList<>(allocatedLogicalSlots.values()); |
| 103 | + for (LogicalSlot allocatedLogicalSlot : collect) { |
| 104 | + allocatedLogicalSlot.releaseSlot(cause); |
| 105 | + } |
| 106 | + allocatedLogicalSlots.clear(); |
| 107 | + tryReleaseExternally(); |
| 108 | + } |
| 109 | + |
| 110 | + private void tryReleaseExternally() { |
| 111 | + if (state != State.RELEASED && allocatedLogicalSlots.isEmpty()) { |
| 112 | + state = State.RELEASED; |
| 113 | + LOG.debug("Release shared slot externally ({})", physicalSlotRequestId); |
| 114 | + externalReleaseCallback.run(); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public boolean willOccupySlotIndefinitely() { |
| 120 | + return slotWillBeOccupiedIndefinitely; |
| 121 | + } |
| 122 | + |
| 123 | + private enum State { |
| 124 | + ALLOCATED, |
| 125 | + RELEASED |
| 126 | + } |
| 127 | +} |
0 commit comments