提交 6a2b8792 编写于 作者: K Kohsuke Kawaguchi

Revisiting 9b7b1364 a bit.

Since ConsistentHash is written as a mutable class, I think a better way to fit the existing design is to provide a bulk add operation.
It already has several methods to that end, so I just added one more that's needed for our purpose.
上级 9650ec5e
......@@ -1074,17 +1074,19 @@ public class Queue extends ResourceController implements Saveable {
private void makeBuildable(BuildableItem p) {
if(Jenkins.FLYWEIGHT_SUPPORT && p.task instanceof FlyweightTask && !ifBlockedByHudsonShutdown(p.task)) {
ConsistentHash.Builder<Node> builder = new ConsistentHash.Builder<Node>(NODE_HASH);
Jenkins h = Jenkins.getInstance();
Map<Node,Integer> hashSource = new HashMap<Node, Integer>(h.getNodes().size());
// Even if master is configured with zero executors, we may need to run a flyweight task like MatrixProject on it.
builder.add(h, Math.max(h.getNumExecutors() * 100, 1));
hashSource.put(h, Math.max(h.getNumExecutors() * 100, 1));
for (Node n : h.getNodes()) {
builder.add(n, n.getNumExecutors() * 100);
hashSource.put(n, n.getNumExecutors() * 100);
}
ConsistentHash<Node> hash = builder.build();
ConsistentHash<Node> hash = new ConsistentHash<Node>(NODE_HASH);
hash.addAll(hashSource);
Label lbl = p.getAssignedLabel();
for (Node n : hash.list(p.task.getFullDisplayName())) {
......
......@@ -186,7 +186,7 @@ public class ConsistentHash<T> {
String hash(T t);
}
private static final Hash DEFAULT_HASH = new Hash() {
static final Hash DEFAULT_HASH = new Hash() {
public String hash(Object o) {
return o.toString();
}
......@@ -229,7 +229,8 @@ public class ConsistentHash<T> {
*/
public void addAll(T... nodes) {
for (T node : nodes)
add(node);
addInternal(node,defaultReplication);
refreshTable();
}
/**
......@@ -237,7 +238,17 @@ public class ConsistentHash<T> {
*/
public void addAll(Collection<? extends T> nodes) {
for (T node : nodes)
add(node);
addInternal(node,defaultReplication);
refreshTable();
}
/**
* Calls {@link #add(Object,int)} with all the arguments.
*/
public void addAll(Map<? extends T,Integer> nodes) {
for (Map.Entry<? extends T,Integer> node : nodes.entrySet())
addInternal(node.getKey(),node.getValue());
refreshTable();
}
/**
......@@ -344,24 +355,4 @@ public class ConsistentHash<T> {
public Iterable<T> list(String queryPoint) {
return list(md5(queryPoint));
}
public static class Builder<T> {
final ConsistentHash<T> c;
public Builder(Hash hash) {
c = new ConsistentHash<T>(hash);
}
public Builder add(T node, int replica) {
c.addInternal(node, replica);
return this;
}
public ConsistentHash<T> build() {
c.refreshTable();
return c;
}
}
}
......@@ -23,6 +23,8 @@
*/
package hudson.util;
import com.google.common.collect.Iterables;
import hudson.util.CopyOnWriteMap.Hash;
import junit.framework.TestCase;
import java.util.Random;
......@@ -119,4 +121,23 @@ public class ConsistentHashTest extends TestCase {
assertNull(hash.lookup(0));
assertNull(hash.lookup(999));
}
/**
* This test doesn't fail but it's written to measure the performance of the consistent hash function with large data set.
*/
public void testSpeed() {
Map<String,Integer> data = new Hash<String, Integer>();
for (int i = 0; i < 1000; i++)
data.put("node" + i,100);
data.put("tail",100);
long start = System.currentTimeMillis();
for (int j=0; j<10; j++) {
ConsistentHash<String> b = new ConsistentHash<String>();
b.addAll(data);
// System.out.println(Iterables.toString(b.list("x")));
}
System.out.println(System.currentTimeMillis()-start);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册