|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.hadoop.yarn.server.resourcemanager.scheduler.placement; |
| 20 | + |
| 21 | +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.SchedulerNode; |
| 22 | + |
| 23 | +import java.util.Comparator; |
| 24 | +import java.util.Collection; |
| 25 | +import java.util.Collections; |
| 26 | +import java.util.Iterator; |
| 27 | +import java.util.Map; |
| 28 | +import java.util.Set; |
| 29 | +import java.util.List; |
| 30 | +import java.util.ArrayList; |
| 31 | +import java.util.concurrent.ConcurrentHashMap; |
| 32 | +import java.util.concurrent.ConcurrentSkipListSet; |
| 33 | + |
| 34 | +/** |
| 35 | + * <p> |
| 36 | + * This class has the following functionality: |
| 37 | + * |
| 38 | + * <p> |
| 39 | + * ResourceUsageWithPartialShuffleMultiNodeLookupPolicy |
| 40 | + * holds sorted nodes list based on the |
| 41 | + * resource usage of nodes at given time. |
| 42 | + * Also inorder to prevent hot accessing node with multi-thread scheduling, |
| 43 | + * we add the partial shuffle with SHUFFLE_INTERVAL default is 10. |
| 44 | + * Se details YARN-10738. |
| 45 | + * </p> |
| 46 | + */ |
| 47 | +public class ResourceUsageWithPartialShuffleMultiNodeLookupPolicy |
| 48 | + <N extends SchedulerNode> implements MultiNodeLookupPolicy<N> { |
| 49 | + |
| 50 | + private Map<String, Set<N>> nodesPerPartition = new ConcurrentHashMap<>(); |
| 51 | + private Comparator<N> comparator; |
| 52 | + // Shuffle interval(the shuffle size of every shuffle). |
| 53 | + private static final int SHUFFLE_INTERVAL = 10; |
| 54 | + |
| 55 | + public ResourceUsageWithPartialShuffleMultiNodeLookupPolicy() { |
| 56 | + this.comparator = new Comparator<N>() { |
| 57 | + @Override |
| 58 | + public int compare(N o1, N o2) { |
| 59 | + int allocatedDiff = o1.getAllocatedResource() |
| 60 | + .compareTo(o2.getAllocatedResource()); |
| 61 | + if (allocatedDiff == 0) { |
| 62 | + return o1.getNodeID().compareTo(o2.getNodeID()); |
| 63 | + } |
| 64 | + return allocatedDiff; |
| 65 | + } |
| 66 | + }; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public Iterator<N> getPreferredNodeIterator(Collection<N> nodes, |
| 71 | + String partition) { |
| 72 | + Iterator<N> beforePartialShuffle = |
| 73 | + getNodesPerPartition(partition).iterator(); |
| 74 | + int counter = 0; |
| 75 | + List<N> list = new ArrayList<N>(); |
| 76 | + while(beforePartialShuffle.hasNext()) { |
| 77 | + list.add(beforePartialShuffle.next()); |
| 78 | + // Every shuffle interval(the shuffle size of every shuffle), |
| 79 | + // we should shuffle to prevent |
| 80 | + // hot accessing node when multi scheduling. |
| 81 | + // It's very important for big clusters. |
| 82 | + if (counter > 0 && counter % SHUFFLE_INTERVAL == 0) { |
| 83 | + Collections. |
| 84 | + shuffle(list.subList(counter -10, counter)); |
| 85 | + } |
| 86 | + ++counter; |
| 87 | + } |
| 88 | + return list.iterator(); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public void addAndRefreshNodesSet(Collection<N> nodes, |
| 93 | + String partition) { |
| 94 | + Set<N> nodeList = new ConcurrentSkipListSet<N>(comparator); |
| 95 | + nodeList.addAll(nodes); |
| 96 | + nodesPerPartition.put(partition, Collections.unmodifiableSet(nodeList)); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public Set<N> getNodesPerPartition(String partition) { |
| 101 | + return nodesPerPartition.getOrDefault(partition, Collections.emptySet()); |
| 102 | + } |
| 103 | + |
| 104 | + public Map<String, Set<N>> getNodesPerPartition() { |
| 105 | + return nodesPerPartition; |
| 106 | + } |
| 107 | + |
| 108 | + public Comparator<N> getComparator() { |
| 109 | + return comparator; |
| 110 | + } |
| 111 | +} |
0 commit comments