zentol commented on a change in pull request #13464: URL: https://github.com/apache/flink/pull/13464#discussion_r499373230
########## File path: flink-runtime/src/main/java/org/apache/flink/runtime/resourcemanager/slotmanager/JobScopedResourceTracker.java ########## @@ -0,0 +1,225 @@ +/* + * 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.api.common.JobID; +import org.apache.flink.runtime.clusterframework.types.ResourceProfile; +import org.apache.flink.runtime.slots.ResourceCounter; +import org.apache.flink.runtime.slots.ResourceRequirement; +import org.apache.flink.util.Preconditions; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; + +/** + * Tracks resource for a single job. + */ +class JobScopedResourceTracker { + + private static final Logger LOG = LoggerFactory.getLogger(JobScopedResourceTracker.class); + + // only for logging purposes + private final JobID jobId; + + private final ResourceCounter resourceRequirements = new ResourceCounter(); + private final BiDirectionalResourceToRequirementMapping resourceToRequirementMapping = new BiDirectionalResourceToRequirementMapping(); + private final ResourceCounter excessResources = new ResourceCounter(); + + JobScopedResourceTracker(JobID jobId) { + this.jobId = Preconditions.checkNotNull(jobId); + } + + public void notifyResourceRequirements(Collection<ResourceRequirement> newResourceRequirements) { + Preconditions.checkNotNull(newResourceRequirements); + + resourceRequirements.clear(); + for (ResourceRequirement newResourceRequirement : newResourceRequirements) { + resourceRequirements.incrementCount(newResourceRequirement.getResourceProfile(), newResourceRequirement.getNumberOfRequiredSlots()); + } + findExcessSlots(); + tryAssigningExcessSlots(); + } + + public void notifyAcquiredResource(ResourceProfile resourceProfile) { + Preconditions.checkNotNull(resourceProfile); + final Optional<ResourceProfile> matchingRequirement = findMatchingRequirement(resourceProfile); + if (matchingRequirement.isPresent()) { + resourceToRequirementMapping.incrementCount(matchingRequirement.get(), resourceProfile, 1); + } else { + LOG.debug("Job {} acquired excess resource {}.", resourceProfile, jobId); + excessResources.incrementCount(resourceProfile, 1); + } + } + + private Optional<ResourceProfile> findMatchingRequirement(ResourceProfile resourceProfile) { + for (Map.Entry<ResourceProfile, Integer> requirementCandidate : resourceRequirements.getResourceProfilesWithCount().entrySet()) { + ResourceProfile requirementProfile = requirementCandidate.getKey(); + + // beware the order when matching resources to requirements, because ResourceProfile.UNKNOWN (which only + // occurs as a requirement) does not match any resource! + if (resourceProfile.isMatching(requirementProfile) && requirementCandidate.getValue() > resourceToRequirementMapping.getNumFulfillingResources(requirementProfile)) { + return Optional.of(requirementProfile); + } + } + return Optional.empty(); + } + + public void notifyLostResource(ResourceProfile resourceProfile) { + Preconditions.checkNotNull(resourceProfile); + if (excessResources.getResourceCount(resourceProfile) > 0) { + LOG.trace("Job {} lost excess resource {}.", jobId, resourceProfile); + excessResources.decrementCount(resourceProfile, 1); + return; + } + + Set<ResourceProfile> fulfilledRequirements = resourceToRequirementMapping.getRequirementsFulfilledBy(resourceProfile).keySet(); + + if (!fulfilledRequirements.isEmpty()) { + // determine for which of the requirements, that the resource could be used for, the resource count should be reduced for + ResourceProfile assignedRequirement = null; + + for (ResourceProfile requirementProfile : fulfilledRequirements) { + assignedRequirement = requirementProfile; + + // try finding a requirement that has too many resources; if non are exceeding the requirements we deduct + // the resource from any requirement having such a resource + if (resourceRequirements.getResourceCount(requirementProfile) < resourceToRequirementMapping.getNumFulfillingResources(requirementProfile)) { + break; + } Review comment: hmm...that is true. ---------------------------------------------------------------- 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: us...@infra.apache.org