@Override
public Mapping map(Task task, MappingWorksheet ws) {
List<ConsistentHash<ExecutorChunk>> hashes = new ArrayList<ConsistentHash<ExecutorChunk>>(ws.works.size());
for (int i=0; i<ws.works.size(); i++) {
ConsistentHash<ExecutorChunk> hash = new ConsistentHash<ExecutorChunk>(new Hash<ExecutorChunk>() {
public String hash(ExecutorChunk node) {
return node.getName();
}
});
for (ExecutorChunk ec : ws.works(i).applicableExecutorChunks())
hash.add(ec,ec.size()*100);
hashes.add(hash);
}
Mapping m = ws.new Mapping();
assert m.size()==ws.works.size();
if (assignGreedily(m,task,hashes,0)) {
assert m.isCompletelyValid();
return m;
} else
return null;
}
private boolean assignGreedily(Mapping m, Task task, List<ConsistentHash<ExecutorChunk>> hashes, int i) {
if (i==hashes.size()) return true;
String key = task.getFullDisplayName() + (i>0 ? String.valueOf(i) : "");
for (ExecutorChunk ec : hashes.get(i).list(key)) {
m.assign(i,ec);
if (m.isPartiallyValid() && assignGreedily(m,task,hashes,i+1))
return true;
}
m.assign(i,null);
return false;
}
I've run into this issue where we got 10 nodes with 10 executors each and the same one gets full before it gets executed elsewhere. somehow the contents of the job can kill the agent (OOM), which means it is very bothersome to greedily fill the same node.
my coworker looked up the code:
https://github.com/jenkinsci/jenkins/blob/master/core/src/main/java/hudson/model/LoadBalancer.java