brumi1024 commented on code in PR #8845: URL: https://github.com/apache/hadoop/pull/8845#discussion_r4388588865
########## hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/resolver/QueueConfigResolver.java: ########## @@ -0,0 +1,1315 @@ +/** + * 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.hadoop.yarn.server.resourcemanager.scheduler.capacity.resolver; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.function.Function; + +import org.apache.hadoop.classification.InterfaceAudience; +import org.apache.hadoop.classification.InterfaceStability; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.yarn.api.records.QueueState; +import org.apache.hadoop.yarn.api.records.Resource; +import org.apache.hadoop.yarn.api.records.ResourceInformation; +import org.apache.hadoop.yarn.conf.YarnConfiguration; +import org.apache.hadoop.yarn.nodelabels.CommonNodeLabelsManager; +import org.apache.hadoop.yarn.server.resourcemanager.nodelabels.RMNodeLabelsManager; +import org.apache.hadoop.yarn.server.resourcemanager.reservation.ReservationConstants; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.AutoCreatedQueueTemplate; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.AbstractCSQueue.CapacityConfigType; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacitySchedulerConfiguration; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueueApplicationLimits; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueueCapacityChecks; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueueCapacityVector; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueueCapacityVector.ResourceUnitCapacityType; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueuePath; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueuePrefixes; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.resolver.QueueProperty.Kind; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.resolver.ResolutionInputs.DynamicQueue; +import org.apache.hadoop.yarn.util.resource.Resources; + +import static org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacitySchedulerConfiguration.AUTO_CREATED_LEAF_QUEUE_TEMPLATE_PREFIX; +import static org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacitySchedulerConfiguration.DOT; +import static org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacitySchedulerConfiguration.PREFIX; +import static org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.resolver.QueueProperties.*; + +/** + * Resolves the configuration of every queue from a {@link ConfigSnapshot}, + * top-down, the way queue construction reads it today, without building + * queues. + * <p> + * Values are read through the {@link QueueProperty} readers that the + * {@code CapacitySchedulerConfiguration} getters use. For dynamic queues the + * snapshot is overlaid with the entries trunk writes into the live + * configuration before the queue reads it: + * <ul> + * <li>v2 dynamic leaves first get {@code user-limit-factor=-1} and + * {@code maximum-am-resource-percent=1.0}, unconditionally;</li> + * <li>v2 templates of the parent then write the type specific template + * ({@code leaf-template} or {@code parent-template}) and the generic + * {@code template}, skipping keys that are explicitly set for the queue in + * the snapshot, so a type specific entry from a wildcard path beats a generic + * entry from the exact path;</li> + * <li>a v1 auto-created leaf gets every {@code leaf-queue-template} entry of + * its managed parent, overwriting explicit keys.</li> + * </ul> + * Static queues never receive templates. Inheritance from the parent + * (PARENT steps) uses the parent's resolved values, and derived values are + * computed from resolved values only; effective resources, which depend on + * the cluster resource, are not resolved. + */ [email protected] [email protected] +public final class QueueConfigResolver { + static final QueuePath ROOT = + new QueuePath(CapacitySchedulerConfiguration.ROOT); + + private static final String LABELS_PREFIX = + CapacitySchedulerConfiguration.ACCESSIBLE_NODE_LABELS; + private static final String DYNAMIC_LEAF_DEFAULT = "dynamic leaf default"; + private static final String DYNAMIC_QUEUE_DEFAULT = "dynamic queue default"; + private static final String LEAF_TEMPLATE_PSEUDO_QUEUE = + DOT + AUTO_CREATED_LEAF_QUEUE_TEMPLATE_PREFIX; + private static final String V2_TEMPLATE_PSEUDO_QUEUE = DOT + + AutoCreatedQueueTemplate.AUTO_QUEUE_TEMPLATE_PREFIX.substring(0, + AutoCreatedQueueTemplate.AUTO_QUEUE_TEMPLATE_PREFIX.length() - 1); + + /** Properties resolved by dedicated code instead of a plain read. */ + private static final Set<QueueProperty<?>> SPECIAL = new HashSet<>( + java.util.Arrays.<QueueProperty<?>>asList(LABELED_CAPACITY, + LABELED_MAXIMUM_CAPACITY, USER_WEIGHTS, STATE, + ACCESSIBLE_NODE_LABELS, DEFAULT_NODE_LABEL_EXPRESSION, + ACL_APPLICATION_MAX_PRIORITY, QUEUE_ORDERING_POLICY, + ORDERING_POLICY_PARAMETERS, MAXIMUM_ALLOCATION, PREEMPTION_DISABLED, + INTRA_QUEUE_PREEMPTION_DISABLED, MAXIMUM_APPLICATION_LIFETIME, + DEFAULT_APPLICATION_LIFETIME, LEAF_QUEUE_TEMPLATE, AQC_V2_TEMPLATE, + AQC_V2_LEAF_TEMPLATE, AQC_V2_PARENT_TEMPLATE)); + + private QueueConfigResolver() { + } + + /** + * Resolves every configured queue and every existing dynamic queue. + * @param snapshot the configuration + * @param inputs what the scheduler holds outside the configuration + * @return the resolved tree + */ + public static ResolvedQueueTree resolve(ConfigSnapshot snapshot, + ResolutionInputs inputs) { + Map<String, Set<String>> labels; + RuntimeException labelFailure = null; + try { + labels = configuredNodeLabelsByQueue(snapshot); + } catch (RuntimeException e) { + labels = Collections.emptyMap(); + labelFailure = e; + } + ResolvedQueueTree tree = new ResolvedQueueTree(snapshot, inputs, labels); + + Set<QueuePath> dynamicParents = new HashSet<>(); + for (DynamicQueue queue : inputs.getDynamicQueues()) { + if (!queue.isLeaf()) { + dynamicParents.add(queue.getPath()); + } + } + resolveConfigured(tree, null, ROOT, dynamicParents, labelFailure); + + // Existing dynamic queues that are not configured keep their dynamic + // nature; configured ones were turned static above + List<DynamicQueue> dynamicQueues = + new ArrayList<>(inputs.getDynamicQueues()); + Collections.sort(dynamicQueues, new Comparator<DynamicQueue>() { + @Override + public int compare(DynamicQueue a, DynamicQueue b) { + return Integer.compare(a.getPath().getPathComponents().length, + b.getPath().getPathComponents().length); + } + }); + for (DynamicQueue queue : dynamicQueues) { + QueuePath path = queue.getPath(); + ResolvedQueueConfig parent = path.isRoot() ? null + : tree.get(path.getParentObject()); + if (tree.get(path) == null && parent != null) { + tree.add(resolveDynamic(tree, parent, path, + queue.isLeaf() ? Kind.LEAF : Kind.PARENT, + queue.isLegacyAutoCreated(), labelFailure)); + } + } + + // The default ReservationQueue of every PlanQueue + for (ResolvedQueueConfig queue : new ArrayList<>(tree.getQueues())) { + if (queue.getKind() == Kind.PLAN) { + QueuePath path = QueuePath.createFromQueues( + queue.getQueuePath().getFullPath(), queue.getQueuePath() + .getLeafName() + ReservationConstants.DEFAULT_QUEUE_SUFFIX); + tree.add(resolveDynamic(tree, queue, path, Kind.RESERVATION, false, + labelFailure)); + } + } + + boolean legacyMode = isLegacyQueueMode(snapshot); + Map<QueuePath, Resolved<Map<String, Float>>> weightSums = new HashMap<>(); + for (ResolvedQueueConfig queue : tree.getQueues()) { + QueuePath path = queue.getQueuePath(); + Resolved<Map<String, Float>> sums = null; + if (!path.isRoot()) { + sums = weightSums.get(path.getParentObject()); + if (sums == null) { + sums = weightSums(tree.getChildren(path.getParentObject())); + weightSums.put(path.getParentObject(), sums); + } + } + derive(tree, queue, legacyMode, sums); + } + return tree; + } + + /** + * Resolves a dynamic queue that does not exist yet, as it would be set up + * if it were created now under its parent in the tree. A leaf under a + * managed parent is resolved as a v1 auto-created leaf. + * @param tree the resolved tree + * @param path the path of the new queue + * @param leaf whether the new queue is a leaf + * @return the resolved configuration of the queue, not added to the tree + * @throws IllegalArgumentException if the parent is not in the tree + */ + public static ResolvedQueueConfig resolveDynamicQueue(ResolvedQueueTree tree, + QueuePath path, boolean leaf) { + ResolvedQueueConfig parent = tree.get(path.getParentObject()); + if (parent == null) { + throw new IllegalArgumentException("Parent of " + path + + " is not in the resolved tree"); + } + boolean legacy = leaf && isManagedParent(parent); + Resolved<Set<String>> rootLabels = + tree.getRoot().get(CONFIGURED_NODE_LABELS); + ResolvedQueueConfig queue = resolveDynamic(tree, parent, path, + leaf ? Kind.LEAF : Kind.PARENT, legacy, + rootLabels.isFailed() ? failureOf(rootLabels) : null); + List<ResolvedQueueConfig> siblings = + new ArrayList<>(tree.getChildren(parent.getQueuePath())); + siblings.add(queue); + derive(tree, queue, isLegacyQueueMode(tree.getSnapshot()), + weightSums(siblings)); + return queue; + } + + /** + * Indexes the labels of every queue path that has + * {@code <path>.accessible-node-labels.<label>.<property>} keys, including + * template pseudo-queues; every indexed path also gets the empty label. + * @param snapshot the configuration + * @return queue path to its configured labels + * @throws StringIndexOutOfBoundsException for a key that ends right after + * the label + */ + public static Map<String, Set<String>> configuredNodeLabelsByQueue( + ConfigSnapshot snapshot) { + Map<String, Set<String>> labelsByQueue = new HashMap<>(); + Map<String, String> schedulerEntries = + snapshot.getRawPropertiesWithPrefix(PREFIX, false); + for (String key : schedulerEntries.keySet()) { + // Consider all keys that have the accessible-node-labels prefix, + // excluding <queue-path>.accessible-node-labels itself + if (key.contains(LABELS_PREFIX + DOT)) { + int labelStartIdx = key.indexOf(LABELS_PREFIX) + + LABELS_PREFIX.length() + 1; + int labelEndIndx = key.indexOf('.', labelStartIdx); + String labelName = key.substring(labelStartIdx, labelEndIndx); + String queuePath = + key.substring(0, key.indexOf(LABELS_PREFIX) - 1); + Set<String> labels = labelsByQueue.get(queuePath); + if (labels == null) { + labels = new HashSet<>(); + labels.add(RMNodeLabelsManager.NO_LABEL); + labelsByQueue.put(queuePath, labels); + } + labels.add(labelName); + } + } + return labelsByQueue; + } + + private static void resolveConfigured(ResolvedQueueTree tree, + ResolvedQueueConfig parent, QueuePath path, Set<QueuePath> dynamicParents, + RuntimeException labelFailure) { + Function<String, String> conf = snapshotLookup(tree.getSnapshot()); + // The queue kind is decided as CapacitySchedulerQueueManager.parseQueue + // does; a value that fails to read counts as unset, its failure is kept + // in the resolved property + List<String> children = Collections.emptyList(); + boolean parentLike = dynamicParents.contains(path); + boolean reservable = false; + try { + children = QUEUES.read(conf, path); + parentLike = parentLike + || AUTO_QUEUE_CREATION_V2_ENABLED.read(conf, path) + || AUTO_CREATE_CHILD_QUEUE_ENABLED.read(conf, path); + reservable = RESERVABLE.read(conf, path); + } catch (RuntimeException e) { + // Recorded when the queue's properties are read + } + Kind kind; + if (path.isRoot()) { + kind = Kind.ROOT; + } else if (children.isEmpty() && !parentLike) { + kind = reservable ? Kind.PLAN : Kind.LEAF; + } else { + kind = Kind.PARENT; + } + + Resolved<Set<String>> labels = labelFailure != null Review Comment: Done. ########## hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/resolver/QueueConfigResolver.java: ########## @@ -0,0 +1,1315 @@ +/** + * 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.hadoop.yarn.server.resourcemanager.scheduler.capacity.resolver; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.function.Function; + +import org.apache.hadoop.classification.InterfaceAudience; +import org.apache.hadoop.classification.InterfaceStability; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.yarn.api.records.QueueState; +import org.apache.hadoop.yarn.api.records.Resource; +import org.apache.hadoop.yarn.api.records.ResourceInformation; +import org.apache.hadoop.yarn.conf.YarnConfiguration; +import org.apache.hadoop.yarn.nodelabels.CommonNodeLabelsManager; +import org.apache.hadoop.yarn.server.resourcemanager.nodelabels.RMNodeLabelsManager; +import org.apache.hadoop.yarn.server.resourcemanager.reservation.ReservationConstants; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.AutoCreatedQueueTemplate; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.AbstractCSQueue.CapacityConfigType; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacitySchedulerConfiguration; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueueApplicationLimits; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueueCapacityChecks; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueueCapacityVector; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueueCapacityVector.ResourceUnitCapacityType; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueuePath; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueuePrefixes; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.resolver.QueueProperty.Kind; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.resolver.ResolutionInputs.DynamicQueue; +import org.apache.hadoop.yarn.util.resource.Resources; + +import static org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacitySchedulerConfiguration.AUTO_CREATED_LEAF_QUEUE_TEMPLATE_PREFIX; +import static org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacitySchedulerConfiguration.DOT; +import static org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacitySchedulerConfiguration.PREFIX; +import static org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.resolver.QueueProperties.*; + +/** + * Resolves the configuration of every queue from a {@link ConfigSnapshot}, + * top-down, the way queue construction reads it today, without building + * queues. + * <p> + * Values are read through the {@link QueueProperty} readers that the + * {@code CapacitySchedulerConfiguration} getters use. For dynamic queues the + * snapshot is overlaid with the entries trunk writes into the live + * configuration before the queue reads it: + * <ul> + * <li>v2 dynamic leaves first get {@code user-limit-factor=-1} and + * {@code maximum-am-resource-percent=1.0}, unconditionally;</li> + * <li>v2 templates of the parent then write the type specific template + * ({@code leaf-template} or {@code parent-template}) and the generic + * {@code template}, skipping keys that are explicitly set for the queue in + * the snapshot, so a type specific entry from a wildcard path beats a generic + * entry from the exact path;</li> + * <li>a v1 auto-created leaf gets every {@code leaf-queue-template} entry of + * its managed parent, overwriting explicit keys.</li> + * </ul> + * Static queues never receive templates. Inheritance from the parent + * (PARENT steps) uses the parent's resolved values, and derived values are + * computed from resolved values only; effective resources, which depend on + * the cluster resource, are not resolved. + */ [email protected] [email protected] +public final class QueueConfigResolver { + static final QueuePath ROOT = + new QueuePath(CapacitySchedulerConfiguration.ROOT); + + private static final String LABELS_PREFIX = + CapacitySchedulerConfiguration.ACCESSIBLE_NODE_LABELS; + private static final String DYNAMIC_LEAF_DEFAULT = "dynamic leaf default"; + private static final String DYNAMIC_QUEUE_DEFAULT = "dynamic queue default"; + private static final String LEAF_TEMPLATE_PSEUDO_QUEUE = + DOT + AUTO_CREATED_LEAF_QUEUE_TEMPLATE_PREFIX; + private static final String V2_TEMPLATE_PSEUDO_QUEUE = DOT + + AutoCreatedQueueTemplate.AUTO_QUEUE_TEMPLATE_PREFIX.substring(0, + AutoCreatedQueueTemplate.AUTO_QUEUE_TEMPLATE_PREFIX.length() - 1); + + /** Properties resolved by dedicated code instead of a plain read. */ + private static final Set<QueueProperty<?>> SPECIAL = new HashSet<>( + java.util.Arrays.<QueueProperty<?>>asList(LABELED_CAPACITY, + LABELED_MAXIMUM_CAPACITY, USER_WEIGHTS, STATE, + ACCESSIBLE_NODE_LABELS, DEFAULT_NODE_LABEL_EXPRESSION, + ACL_APPLICATION_MAX_PRIORITY, QUEUE_ORDERING_POLICY, + ORDERING_POLICY_PARAMETERS, MAXIMUM_ALLOCATION, PREEMPTION_DISABLED, + INTRA_QUEUE_PREEMPTION_DISABLED, MAXIMUM_APPLICATION_LIFETIME, + DEFAULT_APPLICATION_LIFETIME, LEAF_QUEUE_TEMPLATE, AQC_V2_TEMPLATE, + AQC_V2_LEAF_TEMPLATE, AQC_V2_PARENT_TEMPLATE)); + + private QueueConfigResolver() { + } + + /** + * Resolves every configured queue and every existing dynamic queue. + * @param snapshot the configuration + * @param inputs what the scheduler holds outside the configuration + * @return the resolved tree + */ + public static ResolvedQueueTree resolve(ConfigSnapshot snapshot, + ResolutionInputs inputs) { + Map<String, Set<String>> labels; + RuntimeException labelFailure = null; + try { + labels = configuredNodeLabelsByQueue(snapshot); + } catch (RuntimeException e) { + labels = Collections.emptyMap(); + labelFailure = e; + } + ResolvedQueueTree tree = new ResolvedQueueTree(snapshot, inputs, labels); + + Set<QueuePath> dynamicParents = new HashSet<>(); + for (DynamicQueue queue : inputs.getDynamicQueues()) { + if (!queue.isLeaf()) { + dynamicParents.add(queue.getPath()); + } + } + resolveConfigured(tree, null, ROOT, dynamicParents, labelFailure); + + // Existing dynamic queues that are not configured keep their dynamic + // nature; configured ones were turned static above + List<DynamicQueue> dynamicQueues = + new ArrayList<>(inputs.getDynamicQueues()); + Collections.sort(dynamicQueues, new Comparator<DynamicQueue>() { + @Override + public int compare(DynamicQueue a, DynamicQueue b) { + return Integer.compare(a.getPath().getPathComponents().length, + b.getPath().getPathComponents().length); + } + }); + for (DynamicQueue queue : dynamicQueues) { + QueuePath path = queue.getPath(); + ResolvedQueueConfig parent = path.isRoot() ? null + : tree.get(path.getParentObject()); + if (tree.get(path) == null && parent != null) { + tree.add(resolveDynamic(tree, parent, path, + queue.isLeaf() ? Kind.LEAF : Kind.PARENT, + queue.isLegacyAutoCreated(), labelFailure)); + } + } + + // The default ReservationQueue of every PlanQueue + for (ResolvedQueueConfig queue : new ArrayList<>(tree.getQueues())) { + if (queue.getKind() == Kind.PLAN) { + QueuePath path = QueuePath.createFromQueues( + queue.getQueuePath().getFullPath(), queue.getQueuePath() + .getLeafName() + ReservationConstants.DEFAULT_QUEUE_SUFFIX); + tree.add(resolveDynamic(tree, queue, path, Kind.RESERVATION, false, + labelFailure)); + } + } + + boolean legacyMode = isLegacyQueueMode(snapshot); + Map<QueuePath, Resolved<Map<String, Float>>> weightSums = new HashMap<>(); + for (ResolvedQueueConfig queue : tree.getQueues()) { + QueuePath path = queue.getQueuePath(); + Resolved<Map<String, Float>> sums = null; + if (!path.isRoot()) { + sums = weightSums.get(path.getParentObject()); + if (sums == null) { + sums = weightSums(tree.getChildren(path.getParentObject())); + weightSums.put(path.getParentObject(), sums); + } + } + derive(tree, queue, legacyMode, sums); + } + return tree; + } + + /** + * Resolves a dynamic queue that does not exist yet, as it would be set up + * if it were created now under its parent in the tree. A leaf under a + * managed parent is resolved as a v1 auto-created leaf. + * @param tree the resolved tree + * @param path the path of the new queue + * @param leaf whether the new queue is a leaf + * @return the resolved configuration of the queue, not added to the tree + * @throws IllegalArgumentException if the parent is not in the tree + */ + public static ResolvedQueueConfig resolveDynamicQueue(ResolvedQueueTree tree, + QueuePath path, boolean leaf) { + ResolvedQueueConfig parent = tree.get(path.getParentObject()); + if (parent == null) { + throw new IllegalArgumentException("Parent of " + path + + " is not in the resolved tree"); + } + boolean legacy = leaf && isManagedParent(parent); + Resolved<Set<String>> rootLabels = + tree.getRoot().get(CONFIGURED_NODE_LABELS); + ResolvedQueueConfig queue = resolveDynamic(tree, parent, path, + leaf ? Kind.LEAF : Kind.PARENT, legacy, + rootLabels.isFailed() ? failureOf(rootLabels) : null); + List<ResolvedQueueConfig> siblings = + new ArrayList<>(tree.getChildren(parent.getQueuePath())); + siblings.add(queue); + derive(tree, queue, isLegacyQueueMode(tree.getSnapshot()), + weightSums(siblings)); + return queue; + } + + /** + * Indexes the labels of every queue path that has + * {@code <path>.accessible-node-labels.<label>.<property>} keys, including + * template pseudo-queues; every indexed path also gets the empty label. + * @param snapshot the configuration + * @return queue path to its configured labels + * @throws StringIndexOutOfBoundsException for a key that ends right after + * the label + */ + public static Map<String, Set<String>> configuredNodeLabelsByQueue( + ConfigSnapshot snapshot) { + Map<String, Set<String>> labelsByQueue = new HashMap<>(); + Map<String, String> schedulerEntries = + snapshot.getRawPropertiesWithPrefix(PREFIX, false); + for (String key : schedulerEntries.keySet()) { + // Consider all keys that have the accessible-node-labels prefix, + // excluding <queue-path>.accessible-node-labels itself + if (key.contains(LABELS_PREFIX + DOT)) { + int labelStartIdx = key.indexOf(LABELS_PREFIX) + + LABELS_PREFIX.length() + 1; + int labelEndIndx = key.indexOf('.', labelStartIdx); + String labelName = key.substring(labelStartIdx, labelEndIndx); + String queuePath = + key.substring(0, key.indexOf(LABELS_PREFIX) - 1); + Set<String> labels = labelsByQueue.get(queuePath); + if (labels == null) { + labels = new HashSet<>(); + labels.add(RMNodeLabelsManager.NO_LABEL); + labelsByQueue.put(queuePath, labels); + } + labels.add(labelName); + } + } + return labelsByQueue; + } + + private static void resolveConfigured(ResolvedQueueTree tree, + ResolvedQueueConfig parent, QueuePath path, Set<QueuePath> dynamicParents, + RuntimeException labelFailure) { + Function<String, String> conf = snapshotLookup(tree.getSnapshot()); + // The queue kind is decided as CapacitySchedulerQueueManager.parseQueue + // does; a value that fails to read counts as unset, its failure is kept + // in the resolved property + List<String> children = Collections.emptyList(); + boolean parentLike = dynamicParents.contains(path); + boolean reservable = false; + try { + children = QUEUES.read(conf, path); + parentLike = parentLike + || AUTO_QUEUE_CREATION_V2_ENABLED.read(conf, path) + || AUTO_CREATE_CHILD_QUEUE_ENABLED.read(conf, path); + reservable = RESERVABLE.read(conf, path); + } catch (RuntimeException e) { + // Recorded when the queue's properties are read Review Comment: Done. ########## hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/resolver/QueueConfigResolver.java: ########## @@ -0,0 +1,1315 @@ +/** + * 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.hadoop.yarn.server.resourcemanager.scheduler.capacity.resolver; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.function.Function; + +import org.apache.hadoop.classification.InterfaceAudience; +import org.apache.hadoop.classification.InterfaceStability; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.yarn.api.records.QueueState; +import org.apache.hadoop.yarn.api.records.Resource; +import org.apache.hadoop.yarn.api.records.ResourceInformation; +import org.apache.hadoop.yarn.conf.YarnConfiguration; +import org.apache.hadoop.yarn.nodelabels.CommonNodeLabelsManager; +import org.apache.hadoop.yarn.server.resourcemanager.nodelabels.RMNodeLabelsManager; +import org.apache.hadoop.yarn.server.resourcemanager.reservation.ReservationConstants; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.AutoCreatedQueueTemplate; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.AbstractCSQueue.CapacityConfigType; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacitySchedulerConfiguration; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueueApplicationLimits; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueueCapacityChecks; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueueCapacityVector; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueueCapacityVector.ResourceUnitCapacityType; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueuePath; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueuePrefixes; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.resolver.QueueProperty.Kind; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.resolver.ResolutionInputs.DynamicQueue; +import org.apache.hadoop.yarn.util.resource.Resources; + +import static org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacitySchedulerConfiguration.AUTO_CREATED_LEAF_QUEUE_TEMPLATE_PREFIX; +import static org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacitySchedulerConfiguration.DOT; +import static org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacitySchedulerConfiguration.PREFIX; +import static org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.resolver.QueueProperties.*; + +/** + * Resolves the configuration of every queue from a {@link ConfigSnapshot}, + * top-down, the way queue construction reads it today, without building + * queues. + * <p> + * Values are read through the {@link QueueProperty} readers that the + * {@code CapacitySchedulerConfiguration} getters use. For dynamic queues the + * snapshot is overlaid with the entries trunk writes into the live + * configuration before the queue reads it: + * <ul> + * <li>v2 dynamic leaves first get {@code user-limit-factor=-1} and + * {@code maximum-am-resource-percent=1.0}, unconditionally;</li> + * <li>v2 templates of the parent then write the type specific template + * ({@code leaf-template} or {@code parent-template}) and the generic + * {@code template}, skipping keys that are explicitly set for the queue in + * the snapshot, so a type specific entry from a wildcard path beats a generic + * entry from the exact path;</li> + * <li>a v1 auto-created leaf gets every {@code leaf-queue-template} entry of + * its managed parent, overwriting explicit keys.</li> + * </ul> + * Static queues never receive templates. Inheritance from the parent + * (PARENT steps) uses the parent's resolved values, and derived values are + * computed from resolved values only; effective resources, which depend on + * the cluster resource, are not resolved. + */ [email protected] [email protected] +public final class QueueConfigResolver { + static final QueuePath ROOT = + new QueuePath(CapacitySchedulerConfiguration.ROOT); + + private static final String LABELS_PREFIX = + CapacitySchedulerConfiguration.ACCESSIBLE_NODE_LABELS; + private static final String DYNAMIC_LEAF_DEFAULT = "dynamic leaf default"; + private static final String DYNAMIC_QUEUE_DEFAULT = "dynamic queue default"; + private static final String LEAF_TEMPLATE_PSEUDO_QUEUE = + DOT + AUTO_CREATED_LEAF_QUEUE_TEMPLATE_PREFIX; + private static final String V2_TEMPLATE_PSEUDO_QUEUE = DOT + + AutoCreatedQueueTemplate.AUTO_QUEUE_TEMPLATE_PREFIX.substring(0, + AutoCreatedQueueTemplate.AUTO_QUEUE_TEMPLATE_PREFIX.length() - 1); + + /** Properties resolved by dedicated code instead of a plain read. */ + private static final Set<QueueProperty<?>> SPECIAL = new HashSet<>( + java.util.Arrays.<QueueProperty<?>>asList(LABELED_CAPACITY, + LABELED_MAXIMUM_CAPACITY, USER_WEIGHTS, STATE, + ACCESSIBLE_NODE_LABELS, DEFAULT_NODE_LABEL_EXPRESSION, + ACL_APPLICATION_MAX_PRIORITY, QUEUE_ORDERING_POLICY, + ORDERING_POLICY_PARAMETERS, MAXIMUM_ALLOCATION, PREEMPTION_DISABLED, + INTRA_QUEUE_PREEMPTION_DISABLED, MAXIMUM_APPLICATION_LIFETIME, + DEFAULT_APPLICATION_LIFETIME, LEAF_QUEUE_TEMPLATE, AQC_V2_TEMPLATE, + AQC_V2_LEAF_TEMPLATE, AQC_V2_PARENT_TEMPLATE)); + + private QueueConfigResolver() { + } + + /** + * Resolves every configured queue and every existing dynamic queue. + * @param snapshot the configuration + * @param inputs what the scheduler holds outside the configuration + * @return the resolved tree + */ + public static ResolvedQueueTree resolve(ConfigSnapshot snapshot, + ResolutionInputs inputs) { + Map<String, Set<String>> labels; + RuntimeException labelFailure = null; + try { + labels = configuredNodeLabelsByQueue(snapshot); + } catch (RuntimeException e) { + labels = Collections.emptyMap(); + labelFailure = e; + } + ResolvedQueueTree tree = new ResolvedQueueTree(snapshot, inputs, labels); + + Set<QueuePath> dynamicParents = new HashSet<>(); + for (DynamicQueue queue : inputs.getDynamicQueues()) { + if (!queue.isLeaf()) { + dynamicParents.add(queue.getPath()); + } + } + resolveConfigured(tree, null, ROOT, dynamicParents, labelFailure); + + // Existing dynamic queues that are not configured keep their dynamic + // nature; configured ones were turned static above + List<DynamicQueue> dynamicQueues = + new ArrayList<>(inputs.getDynamicQueues()); + Collections.sort(dynamicQueues, new Comparator<DynamicQueue>() { + @Override + public int compare(DynamicQueue a, DynamicQueue b) { + return Integer.compare(a.getPath().getPathComponents().length, + b.getPath().getPathComponents().length); + } + }); + for (DynamicQueue queue : dynamicQueues) { + QueuePath path = queue.getPath(); + ResolvedQueueConfig parent = path.isRoot() ? null + : tree.get(path.getParentObject()); + if (tree.get(path) == null && parent != null) { + tree.add(resolveDynamic(tree, parent, path, + queue.isLeaf() ? Kind.LEAF : Kind.PARENT, + queue.isLegacyAutoCreated(), labelFailure)); + } + } + + // The default ReservationQueue of every PlanQueue + for (ResolvedQueueConfig queue : new ArrayList<>(tree.getQueues())) { + if (queue.getKind() == Kind.PLAN) { + QueuePath path = QueuePath.createFromQueues( + queue.getQueuePath().getFullPath(), queue.getQueuePath() + .getLeafName() + ReservationConstants.DEFAULT_QUEUE_SUFFIX); + tree.add(resolveDynamic(tree, queue, path, Kind.RESERVATION, false, + labelFailure)); + } + } + + boolean legacyMode = isLegacyQueueMode(snapshot); + Map<QueuePath, Resolved<Map<String, Float>>> weightSums = new HashMap<>(); + for (ResolvedQueueConfig queue : tree.getQueues()) { + QueuePath path = queue.getQueuePath(); + Resolved<Map<String, Float>> sums = null; + if (!path.isRoot()) { + sums = weightSums.get(path.getParentObject()); + if (sums == null) { + sums = weightSums(tree.getChildren(path.getParentObject())); + weightSums.put(path.getParentObject(), sums); + } + } + derive(tree, queue, legacyMode, sums); + } + return tree; + } + + /** + * Resolves a dynamic queue that does not exist yet, as it would be set up + * if it were created now under its parent in the tree. A leaf under a + * managed parent is resolved as a v1 auto-created leaf. + * @param tree the resolved tree + * @param path the path of the new queue + * @param leaf whether the new queue is a leaf + * @return the resolved configuration of the queue, not added to the tree + * @throws IllegalArgumentException if the parent is not in the tree + */ + public static ResolvedQueueConfig resolveDynamicQueue(ResolvedQueueTree tree, + QueuePath path, boolean leaf) { + ResolvedQueueConfig parent = tree.get(path.getParentObject()); + if (parent == null) { + throw new IllegalArgumentException("Parent of " + path + + " is not in the resolved tree"); + } + boolean legacy = leaf && isManagedParent(parent); + Resolved<Set<String>> rootLabels = + tree.getRoot().get(CONFIGURED_NODE_LABELS); + ResolvedQueueConfig queue = resolveDynamic(tree, parent, path, + leaf ? Kind.LEAF : Kind.PARENT, legacy, + rootLabels.isFailed() ? failureOf(rootLabels) : null); + List<ResolvedQueueConfig> siblings = + new ArrayList<>(tree.getChildren(parent.getQueuePath())); + siblings.add(queue); + derive(tree, queue, isLegacyQueueMode(tree.getSnapshot()), + weightSums(siblings)); + return queue; + } + + /** + * Indexes the labels of every queue path that has + * {@code <path>.accessible-node-labels.<label>.<property>} keys, including + * template pseudo-queues; every indexed path also gets the empty label. + * @param snapshot the configuration + * @return queue path to its configured labels + * @throws StringIndexOutOfBoundsException for a key that ends right after + * the label + */ + public static Map<String, Set<String>> configuredNodeLabelsByQueue( + ConfigSnapshot snapshot) { + Map<String, Set<String>> labelsByQueue = new HashMap<>(); + Map<String, String> schedulerEntries = + snapshot.getRawPropertiesWithPrefix(PREFIX, false); + for (String key : schedulerEntries.keySet()) { + // Consider all keys that have the accessible-node-labels prefix, + // excluding <queue-path>.accessible-node-labels itself + if (key.contains(LABELS_PREFIX + DOT)) { + int labelStartIdx = key.indexOf(LABELS_PREFIX) + + LABELS_PREFIX.length() + 1; + int labelEndIndx = key.indexOf('.', labelStartIdx); + String labelName = key.substring(labelStartIdx, labelEndIndx); + String queuePath = + key.substring(0, key.indexOf(LABELS_PREFIX) - 1); + Set<String> labels = labelsByQueue.get(queuePath); Review Comment: Done. -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
