Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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.resourcemanager.slotmanager;
import org.apache.flink.annotation.VisibleForTesting;
import org.apache.flink.api.common.time.Time;
import org.apache.flink.runtime.akka.AkkaUtils;
import org.apache.flink.runtime.clusterframework.types.AllocationID;
import org.apache.flink.runtime.clusterframework.types.ResourceID;
import org.apache.flink.runtime.clusterframework.types.SlotID;
import org.apache.flink.runtime.clusterframework.types.TaskManagerSlot;
import org.apache.flink.runtime.concurrent.ScheduledExecutor;
import org.apache.flink.runtime.resourcemanager.SlotRequest;
import org.apache.flink.runtime.resourcemanager.placementconstraint.SlotTag;
import org.apache.flink.util.Preconditions;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/**
* A slot manager that answers request with the slot which exactly the same resource with the request.
*/
public class StrictlyMatchingSlotManager extends SlotManager {
private Map> tmTags = new HashMap<>();
@VisibleForTesting
public StrictlyMatchingSlotManager(
ScheduledExecutor scheduledExecutor,
Time taskManagerRequestTimeout,
Time slotRequestTimeout,
Time taskManagerTimeout,
Time taskManagerCheckerInitialDelay) {
this(scheduledExecutor, taskManagerRequestTimeout, slotRequestTimeout, taskManagerTimeout, Time.milliseconds(0), taskManagerCheckerInitialDelay);
}
public StrictlyMatchingSlotManager(
ScheduledExecutor scheduledExecutor,
Time taskManagerRequestTimeout,
Time slotRequestTimeout,
Time taskManagerTimeout,
Time taskManagerFastTimeout,
Time taskManagerCheckerInitialDelay) {
super(scheduledExecutor, taskManagerRequestTimeout, slotRequestTimeout, taskManagerTimeout, taskManagerFastTimeout, taskManagerCheckerInitialDelay, Time.seconds(AkkaUtils.INF_TIMEOUT().toSeconds()));
}
@Override
protected TaskManagerSlot findMatchingSlot(SlotRequest slotRequest) {
Map numTmFreeSlots = new HashMap<>();
for (SlotID slotID : freeSlots.keySet()) {
ResourceID resourceID = slotID.getResourceID();
numTmFreeSlots.put(resourceID, numTmFreeSlots.getOrDefault(resourceID, 0) + 1);
}
Iterator> iterator = freeSlots.entrySet().stream()
.sorted(Comparator.comparingInt(
(Map.Entry entry) -> numTmFreeSlots.get(entry.getKey().getResourceID())))
.iterator();
while (iterator.hasNext()) {
TaskManagerSlot taskManagerSlot = iterator.next().getValue();
// sanity check
Preconditions.checkState(taskManagerSlot.getState() == TaskManagerSlot.State.FREE,
String.format("Slot %s is in state %s", taskManagerSlot.getSlotId(), taskManagerSlot.getState()));
Set tags = tmTags.get(taskManagerSlot.getSlotId().getResourceID());
if (taskManagerSlot.getResourceProfile().equals(slotRequest.getResourceProfile()) &&
(tags == null || tags.equals(new HashSet<>(slotRequest.getTags()))) &&
placementConstraintManager.check(
slotRequest.getJobId(),
allocationIdTags.get(slotRequest.getAllocationId()),
getTaskExecutorSlotTags(taskManagerSlot.getSlotId()))) {
freeSlots.remove(taskManagerSlot.getSlotId());
return taskManagerSlot;
}
}
return null;
}
@Override
protected PendingSlotRequest findMatchingRequest(TaskManagerSlot taskManagerSlot) {
for (PendingSlotRequest pendingSlotRequest : pendingSlotRequests.values()) {
Set tags = tmTags.get(taskManagerSlot.getSlotId().getResourceID());
if (taskManagerSlot.getResourceProfile().equals(pendingSlotRequest.getResourceProfile()) &&
!pendingSlotRequest.isAssigned() &&
(tags == null || tags.equals(new HashSet<>(pendingSlotRequest.getTags()))) &&
placementConstraintManager.check(
pendingSlotRequest.getJobId(),
allocationIdTags.get(pendingSlotRequest.getAllocationId()),
getTaskExecutorSlotTags(taskManagerSlot.getSlotId()))) {
return pendingSlotRequest;
}
}
return null;
}
@Override
public boolean unregisterSlotRequest(AllocationID allocationId) {
checkInit();
PendingSlotRequest pendingSlotRequest = pendingSlotRequests.remove(allocationId);
if (null != pendingSlotRequest) {
LOG.debug("Cancel slot request {}.", allocationId);
if (null != slotListener) {
slotListener.notifySlotRequestRemoved(pendingSlotRequest);
}
if (pendingSlotRequest.isAssigned()) {
cancelPendingSlotRequest(pendingSlotRequest);
} else {
resourceActions.cancelResourceAllocation(pendingSlotRequest.getResourceProfile(),
getSlotTagsIfHasPlacementConstraints(pendingSlotRequest));
}
return true;
} else {
LOG.debug("No pending slot request with allocation id {} found. Ignoring unregistration request.", allocationId);
return false;
}
}
public void setTmTags(ResourceID resourceId, Set tags) {
if (tags != null && !tags.isEmpty()) {
tmTags.put(resourceId, tags);
}
}
}