zentol commented on a change in pull request #13561: URL: https://github.com/apache/flink/pull/13561#discussion_r503769706
########## File path: flink-runtime/src/main/java/org/apache/flink/runtime/jobmaster/slotpool/DefaultAllocatedSlotPool.java ########## @@ -0,0 +1,218 @@ +/* + * 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.AllocationID; +import org.apache.flink.runtime.clusterframework.types.ResourceID; +import org.apache.flink.runtime.jobmaster.SlotInfo; +import org.apache.flink.util.Preconditions; + +import javax.annotation.Nullable; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Optional; +import java.util.Set; + +/** + * Default {@link AllocatedSlotPool} implementation. + */ +public class DefaultAllocatedSlotPool implements AllocatedSlotPool { + + private final Map<AllocationID, AllocatedSlot> registeredSlots; + + /** + * Map containing all free slots and since when they are free. + */ + private final Map<AllocationID, Long> freeSlotsSince; + + /** + * Index containing a mapping between TaskExecutors and their slots. + */ + private final Map<ResourceID, Set<AllocationID>> slotsPerTaskExecutor; + + public DefaultAllocatedSlotPool() { + this.registeredSlots = new HashMap<>(); + this.slotsPerTaskExecutor = new HashMap<>(); + this.freeSlotsSince = new HashMap<>(); + } + + @Override + public void addSlots(Collection<AllocatedSlot> slots, long currentTime) { + for (AllocatedSlot slot : slots) { + addSlot(slot, currentTime); + } + } + + private void addSlot(AllocatedSlot slot, long currentTime) { + Preconditions.checkState(!registeredSlots.containsKey(slot.getAllocationId()), "The slot pool already contains a slot with id %s", slot.getAllocationId()); + addSlotInternal(slot, currentTime); + + slotsPerTaskExecutor + .computeIfAbsent(slot.getTaskManagerId(), resourceID -> new HashSet<>()) + .add(slot.getAllocationId()); Review comment: argh, I will remove the copy in `addSlotInternal`. Leftover from an experiment where all 3 data-structure updates were done in `addSlotInternal`, but then you'd lose symmetry with `removeSlot(Internal)`, which you could only avoid by moving _most_ of `removeSlot`, but then there was little benefit... ---------------------------------------------------------------- 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]
