Skip to content

YARN-10302. Make FairScheduler node comparator configurable #2044

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.google.common.util.concurrent.SettableFuture;
import java.io.Serializable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.hadoop.classification.InterfaceAudience.LimitedPrivate;
Expand Down Expand Up @@ -103,6 +104,8 @@
import org.apache.hadoop.yarn.util.resource.Resources;

import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -188,8 +191,7 @@ public class FairScheduler extends
@Deprecated
protected volatile int continuousSchedulingSleepMs;
// Node available resource comparator
private Comparator<FSSchedulerNode> nodeAvailableResourceComparator =
new NodeAvailableResourceComparator();
private Comparator<FSSchedulerNode> nodeAvailableResourceComparator;
protected double nodeLocalityThreshold; // Cluster threshold for node locality
protected double rackLocalityThreshold; // Cluster threshold for rack locality
@Deprecated
Expand Down Expand Up @@ -295,6 +297,33 @@ private void validateConf(FairSchedulerConfiguration config) {
}
}

private Comparator<FSSchedulerNode> initNodeComparator(
FairSchedulerConfiguration config) {
Class<?> nodeComparatorClass = config.getNodeComparatorClass();
if (!Comparator.class.isAssignableFrom(nodeComparatorClass)) {
throw new YarnRuntimeException("Class: " +
nodeComparatorClass.getCanonicalName() + " not instance of " +
Comparator.class.getCanonicalName() + "<FSSchedulerNode>");
}

try {
try {
Constructor constructor =
nodeComparatorClass.getDeclaredConstructor(FairScheduler.class);
constructor.setAccessible(true);
return (Comparator<FSSchedulerNode>) constructor.newInstance(this);
} catch (NoSuchMethodException e) {
// constructor doesn't exist, use default
return (Comparator<FSSchedulerNode>) nodeComparatorClass.newInstance();
}
} catch (ClassCastException | InstantiationException |
IllegalAccessException | InvocationTargetException e) {
throw new YarnRuntimeException(
"Could not create instance of class: " +
nodeComparatorClass.getCanonicalName(), e);
}
}

public FairSchedulerConfiguration getConf() {
return conf;
}
Expand Down Expand Up @@ -1054,12 +1083,13 @@ void continuousSchedulingAttempt() throws InterruptedException {
}

/** Sort nodes by available resource */
private class NodeAvailableResourceComparator
implements Comparator<FSSchedulerNode> {
protected static class NodeAvailableResourceComparator
implements Comparator<FSSchedulerNode>, Serializable {

@Override
public int compare(FSSchedulerNode n1, FSSchedulerNode n2) {
return RESOURCE_CALCULATOR.compare(getClusterResource(),
// clusterResource unused by DefaultResourceCalculator
return RESOURCE_CALCULATOR.compare(null,
n2.getUnallocatedResource(),
n1.getUnallocatedResource());
}
Expand Down Expand Up @@ -1432,6 +1462,7 @@ private void initScheduler(Configuration conf) throws IOException {
sizeBasedWeight = this.conf.getSizeBasedWeight();
usePortForNodeName = this.conf.getUsePortForNodeName();
reservableNodesRatio = this.conf.getReservableNodes();
nodeAvailableResourceComparator = initNodeComparator(this.conf);

updateInterval = this.conf.getUpdateInterval();
if (updateInterval < 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.regex.Pattern;

import com.google.common.collect.Lists;
import org.apache.hadoop.yarn.exceptions.YarnRuntimeException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.hadoop.classification.InterfaceAudience.Private;
Expand Down Expand Up @@ -251,6 +252,9 @@ public class FairSchedulerConfiguration extends Configuration {
private static final String RESOURCES_WITH_SPACES_PATTERN =
"-?\\d+(?:\\.\\d*)?\\s*[a-z]+\\s*";

public static final String NODE_COMPARATOR_CLASS =
CONF_PREFIX + "node.comparator.class";

public FairSchedulerConfiguration() {
super();
}
Expand Down Expand Up @@ -757,4 +761,14 @@ private static String findResourceFromValues(String[] resourceValues,
}
throw new AllocationConfigurationException("Missing resource: " + resource);
}

protected Class<?> getNodeComparatorClass() {
try {
return getClassByName(get(NODE_COMPARATOR_CLASS,
FairScheduler.NodeAvailableResourceComparator.class.getName()));
} catch (ClassNotFoundException e) {
throw new YarnRuntimeException(
"Could not find class for " + NODE_COMPARATOR_CLASS);
}
}
}