tillrohrmann commented on a change in pull request #11353: [FLINK-16438][yarn] Make YarnResourceManager starts workers using WorkerResourceSpec requested by SlotManager URL: https://github.com/apache/flink/pull/11353#discussion_r409581744
########## File path: flink-yarn/src/main/java/org/apache/flink/yarn/WorkerSpecContainerResourceAdapter.java ########## @@ -0,0 +1,156 @@ +/* + * 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.yarn; + +import org.apache.flink.annotation.VisibleForTesting; +import org.apache.flink.configuration.Configuration; +import org.apache.flink.runtime.clusterframework.TaskExecutorProcessSpec; +import org.apache.flink.runtime.clusterframework.TaskExecutorProcessUtils; +import org.apache.flink.runtime.resourcemanager.WorkerResourceSpec; +import org.apache.flink.util.Preconditions; + +import org.apache.hadoop.yarn.api.records.Resource; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.annotation.Nullable; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; + +/** + * Utility class for converting between Flink {@link WorkerResourceSpec} and Yarn {@link Resource}. + */ +public class WorkerSpecContainerResourceAdapter { + private final Logger log = LoggerFactory.getLogger(WorkerSpecContainerResourceAdapter.class); + + private final Configuration flinkConfig; + private final int minMemMB; + private final int minVcore; + private final int maxMemMB; + private final int maxVcore; + private final WorkerSpecContainerResourceAdapter.MatchingStrategy matchingStrategy; + private final Map<WorkerResourceSpec, Resource> workerSpecToContainerResource; + private final Map<Resource, List<WorkerResourceSpec>> containerResourceToWorkerSpecs; + private final Map<Integer, Set<Resource>> containerMemoryToContainerResource; + + @VisibleForTesting + WorkerSpecContainerResourceAdapter( + final Configuration flinkConfig, + final int minMemMB, + final int minVcore, + final int maxMemMB, + final int maxVcore, + final WorkerSpecContainerResourceAdapter.MatchingStrategy matchingStrategy) { + this.flinkConfig = Preconditions.checkNotNull(flinkConfig); + this.minMemMB = minMemMB; + this.minVcore = minVcore; + this.maxMemMB = maxMemMB; + this.maxVcore = maxVcore; + this.matchingStrategy = matchingStrategy; + workerSpecToContainerResource = new HashMap<>(); + containerResourceToWorkerSpecs = new HashMap<>(); + containerMemoryToContainerResource = new HashMap<>(); + } + + @VisibleForTesting + Optional<Resource> getContainerResource(final WorkerResourceSpec workerResourceSpec) { + return Optional.ofNullable(workerSpecToContainerResource.computeIfAbsent( + Preconditions.checkNotNull(workerResourceSpec), + this::createAndMapContainerResource)); + } + + @VisibleForTesting + Set<WorkerResourceSpec> getWorkerSpecs(final Resource containerResource) { + return getEquivalentContainerResource(containerResource).stream() + .flatMap(resource -> containerResourceToWorkerSpecs.getOrDefault(resource, Collections.emptyList()).stream()) + .collect(Collectors.toSet()); + } + + @VisibleForTesting + Set<Resource> getEquivalentContainerResource(final Resource containerResource) { + // Yarn might ignore the requested vcores, depending on its configurations. + // In such cases, we should also not matching vcores. + final Set<Resource> equivalentContainerResources; + switch (matchingStrategy) { + case MATCH_VCORE: + equivalentContainerResources = Collections.singleton(containerResource); + break; + case IGNORE_VCORE: + default: + equivalentContainerResources = containerMemoryToContainerResource + .getOrDefault(containerResource.getMemory(), Collections.emptySet()); + break; + } + return equivalentContainerResources; + } + + @Nullable + private Resource createAndMapContainerResource(final WorkerResourceSpec workerResourceSpec) { + final TaskExecutorProcessSpec taskExecutorProcessSpec = + TaskExecutorProcessUtils.processSpecFromWorkerResourceSpec(flinkConfig, workerResourceSpec); + final Resource containerResource = Resource.newInstance( + normalize(taskExecutorProcessSpec.getTotalProcessMemorySize().getMebiBytes(), minMemMB), + normalize(taskExecutorProcessSpec.getCpuCores().getValue().intValue(), minVcore)); + + if (resourceWithinMaxAllocation(containerResource)) { + containerResourceToWorkerSpecs.computeIfAbsent(containerResource, ignored -> new ArrayList<>()) + .add(workerResourceSpec); + containerMemoryToContainerResource.computeIfAbsent(containerResource.getMemory(), ignored -> new HashSet<>()) + .add(containerResource); + return containerResource; + } else { + log.warn("Requested container resource {} exceeds yarn max allocation {}. Will not allocate resource.", + containerResource, + Resource.newInstance(maxMemMB, maxVcore)); + return null; + } + } + + /** + * Normalize to the minimum integer that is greater or equal to 'value' and is integer multiple of 'unitValue'. + */ + private int normalize(final int value, final int unitValue) { + if (value < unitValue) { + return unitValue; + } + + if (value % unitValue == 0) { + return value; + } + + return (value / unitValue + 1) * unitValue; Review comment: ```suggestion return MathUtils.divideRoundUp(value, unitValue) * unitValue; ``` ---------------------------------------------------------------- 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] With regards, Apache Git Services
