YARN-6680. Avoid locking overhead for NO_LABEL lookups. Contributed by Daryn Sharp

(cherry picked from commit ee89ac84e6)
This commit is contained in:
Naganarasimha 2017-06-19 22:42:45 +05:30
parent 4250250c7b
commit 363ef7b8ad
4 changed files with 26 additions and 12 deletions

View File

@ -99,6 +99,8 @@ public class CommonNodeLabelsManager extends AbstractService {
protected ConcurrentMap<String, Host> nodeCollections = protected ConcurrentMap<String, Host> nodeCollections =
new ConcurrentHashMap<String, Host>(); new ConcurrentHashMap<String, Host>();
protected RMNodeLabel noNodeLabel;
protected final ReadLock readLock; protected final ReadLock readLock;
protected final WriteLock writeLock; protected final WriteLock writeLock;
@ -225,7 +227,8 @@ public class CommonNodeLabelsManager extends AbstractService {
isCentralizedNodeLabelConfiguration = isCentralizedNodeLabelConfiguration =
YarnConfiguration.isCentralizedNodeLabelConfiguration(conf); YarnConfiguration.isCentralizedNodeLabelConfiguration(conf);
labelCollections.put(NO_LABEL, new RMNodeLabel(NO_LABEL)); noNodeLabel = new RMNodeLabel(NO_LABEL);
labelCollections.put(NO_LABEL, noNodeLabel);
} }
/** /**
@ -925,6 +928,9 @@ public class CommonNodeLabelsManager extends AbstractService {
} }
public boolean isExclusiveNodeLabel(String nodeLabel) throws IOException { public boolean isExclusiveNodeLabel(String nodeLabel) throws IOException {
if (nodeLabel.equals(NO_LABEL)) {
return noNodeLabel.getIsExclusive();
}
try { try {
readLock.lock(); readLock.lock();
RMNodeLabel label = labelCollections.get(nodeLabel); RMNodeLabel label = labelCollections.get(nodeLabel);

View File

@ -503,12 +503,16 @@ public class RMNodeLabelsManager extends CommonNodeLabelsManager {
public Resource getResourceByLabel(String label, Resource clusterResource) { public Resource getResourceByLabel(String label, Resource clusterResource) {
label = normalizeLabel(label); label = normalizeLabel(label);
if (label.equals(NO_LABEL)) {
return noNodeLabel.getResource();
}
try { try {
readLock.lock(); readLock.lock();
if (null == labelCollections.get(label)) { RMNodeLabel nodeLabel = labelCollections.get(label);
if (nodeLabel == null) {
return Resources.none(); return Resources.none();
} }
return labelCollections.get(label).getResource(); return nodeLabel.getResource();
} finally { } finally {
readLock.unlock(); readLock.unlock();
} }

View File

@ -28,7 +28,6 @@ import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;
import org.apache.hadoop.yarn.api.records.Resource; import org.apache.hadoop.yarn.api.records.Resource;
import org.apache.hadoop.yarn.nodelabels.CommonNodeLabelsManager; import org.apache.hadoop.yarn.nodelabels.CommonNodeLabelsManager;
import org.apache.hadoop.yarn.server.resourcemanager.nodelabels.RMNodeLabelsManager;
import org.apache.hadoop.yarn.util.resource.Resources; import org.apache.hadoop.yarn.util.resource.Resources;
/** /**
@ -46,6 +45,7 @@ public class ResourceUsage {
private Map<String, UsageByLabel> usages; private Map<String, UsageByLabel> usages;
// short for no-label :) // short for no-label :)
private static final String NL = CommonNodeLabelsManager.NO_LABEL; private static final String NL = CommonNodeLabelsManager.NO_LABEL;
private final UsageByLabel usageNoLabel;
public ResourceUsage() { public ResourceUsage() {
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
@ -53,7 +53,8 @@ public class ResourceUsage {
writeLock = lock.writeLock(); writeLock = lock.writeLock();
usages = new HashMap<String, UsageByLabel>(); usages = new HashMap<String, UsageByLabel>();
usages.put(NL, new UsageByLabel(NL)); usageNoLabel = new UsageByLabel(NL);
usages.put(NL, usageNoLabel);
} }
// Usage enum here to make implement cleaner // Usage enum here to make implement cleaner
@ -323,10 +324,9 @@ public class ResourceUsage {
} }
private Resource _get(String label, ResourceType type) { private Resource _get(String label, ResourceType type) {
if (label == null) { if (label == null || label.equals(NL)) {
label = RMNodeLabelsManager.NO_LABEL; return normalize(usageNoLabel.resArr[type.idx]);
} }
try { try {
readLock.lock(); readLock.lock();
UsageByLabel usage = usages.get(label); UsageByLabel usage = usages.get(label);
@ -362,8 +362,8 @@ public class ResourceUsage {
} }
private UsageByLabel getAndAddIfMissing(String label) { private UsageByLabel getAndAddIfMissing(String label) {
if (label == null) { if (label == null || label.equals(NL)) {
label = RMNodeLabelsManager.NO_LABEL; return usageNoLabel;
} }
if (!usages.containsKey(label)) { if (!usages.containsKey(label)) {
UsageByLabel u = new UsageByLabel(label); UsageByLabel u = new UsageByLabel(label);

View File

@ -52,6 +52,7 @@ import org.apache.hadoop.yarn.server.resourcemanager.RMContextImpl;
import org.apache.hadoop.yarn.server.resourcemanager.ResourceManager; import org.apache.hadoop.yarn.server.resourcemanager.ResourceManager;
import org.apache.hadoop.yarn.server.resourcemanager.applicationsmanager.MockAsm; import org.apache.hadoop.yarn.server.resourcemanager.applicationsmanager.MockAsm;
import org.apache.hadoop.yarn.server.resourcemanager.nodelabels.NullRMNodeLabelsManager; import org.apache.hadoop.yarn.server.resourcemanager.nodelabels.NullRMNodeLabelsManager;
import org.apache.hadoop.yarn.server.resourcemanager.nodelabels.RMNodeLabelsManager;
import org.apache.hadoop.yarn.server.resourcemanager.rmapp.RMApp; import org.apache.hadoop.yarn.server.resourcemanager.rmapp.RMApp;
import org.apache.hadoop.yarn.server.resourcemanager.rmnode.RMNode; import org.apache.hadoop.yarn.server.resourcemanager.rmnode.RMNode;
import org.apache.hadoop.yarn.server.resourcemanager.scheduler.ResourceScheduler; import org.apache.hadoop.yarn.server.resourcemanager.scheduler.ResourceScheduler;
@ -228,12 +229,15 @@ public class TestRMWebApp {
setupQueueConfiguration(conf); setupQueueConfiguration(conf);
CapacityScheduler cs = new CapacityScheduler(); CapacityScheduler cs = new CapacityScheduler();
cs.setConf(new YarnConfiguration()); YarnConfiguration yarnConf = new YarnConfiguration();
cs.setConf(yarnConf);
RMContext rmContext = new RMContextImpl(null, null, null, null, null, RMContext rmContext = new RMContextImpl(null, null, null, null, null,
null, new RMContainerTokenSecretManager(conf), null, new RMContainerTokenSecretManager(conf),
new NMTokenSecretManagerInRM(conf), new NMTokenSecretManagerInRM(conf),
new ClientToAMTokenSecretManagerInRM(), null); new ClientToAMTokenSecretManagerInRM(), null);
rmContext.setNodeLabelManager(new NullRMNodeLabelsManager()); RMNodeLabelsManager labelManager = new NullRMNodeLabelsManager();
labelManager.init(yarnConf);
rmContext.setNodeLabelManager(labelManager);
cs.setRMContext(rmContext); cs.setRMContext(rmContext);
cs.init(conf); cs.init(conf);
return cs; return cs;