寫了一個一致性hash的java實現代碼,算法是用別人的,據說很好,然後自己做了一個測試,用線程池起了1000個線程,每個線程hash10000次,模擬一萬次數據hash,並將測試結果上傳。
/**
* 一致性hash代碼
*
* @author shiguiming
*
* @param <T>
*/
public class Shared<T> {
// 真實節點對應的虛擬節點數量
private int length = 100;
// 虛擬節點信息
private TreeMap<Long, T> virtualNodes;
// 真實節點信息
private List<T> realNodes;
public Shared(List<T> realNodes) {
this.realNodes = realNodes;
init();
}
public List<T> getReal() {
return realNodes;
}
/**
* 初始化虛擬節點
*/
private void init() {
virtualNodes = new TreeMap<Long, T>();
for (int i = 0; i < realNodes.size(); i++) {
for (int j = 0; j < length; j++) {
virtualNodes.put(hash("aa" + i + j), realNodes.get(i));
}
}
}
/**
* 獲取一個結點
*
* @param key
* @return
*/
@SuppressWarnings("unchecked")
public T getNode(String key) {
Long hashedKey = hash(key);
// TODO judge null
Entry en = virtualNodes.ceilingEntry(hashedKey);
if (en == null) {
return (T) virtualNodes.firstEntry().getValue();
}
return (T) en.getValue();
}
/**
* MurMurHash算法,是非加密HASH算法,性能很高,
* 比傳統的CRC32,MD5,SHA-1(這兩個算法都是加密HASH算法,複雜度本身就很高,帶來的性能上的損害也不可避免)
* 等HASH算法要快很多,而且據說這個算法的碰撞率很低. http://murmurhash.googlepages.com/
*/
private Long hash(String key) {
ByteBuffer buf = ByteBuffer.wrap(key.getBytes());
int seed = 0x1234ABCD;
ByteOrder byteOrder = buf.order();
buf.order(ByteOrder.LITTLE_ENDIAN);
long m = 0xc6a4a7935bd1e995L;
int r = 47;
long h = seed ^ (buf.remaining() * m);
long k;
while (buf.remaining() >= 8) {
k = buf.getLong();
k *= m;
k ^= k >>> r;
k *= m;
h ^= k;
h *= m;
}
if (buf.remaining() > 0) {
ByteBuffer finish = ByteBuffer.allocate(8).order(ByteOrder.LITTLE_ENDIAN);
// for big-endian version, do this first:
// finish.position(8-buf.remaining());
finish.put(buf).rewind();
h ^= finish.getLong();
h *= m;
}
h ^= h >>> r;
h *= m;
h ^= h >>> r;
buf.order(byteOrder);
return h;
}
/**
* 測試內部類
*
* @author shiguiming
*
*/
static public class Node {
private int name;
private int count = 0;
public Node() {
}
public Node(int i) {
this.name = i;
}
public int getName() {
return name;
}
public void setName(int name) {
this.name = name;
}
public int getCount() {
return count;
}
// 同步方法,防止併發
synchronized public void inc() {
count++;
}
}
/**
* 測試方法
*
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
List<Node> ndList = new ArrayList<Node>();
int i = 0;
while (true) {
ndList.add(new Node(i));
if (i++ == 9)
break;
}
final Shared<Node> sh = new Shared<Node>(ndList);
ExecutorService es = Executors.newCachedThreadPool();
final CountDownLatch cdl = new CountDownLatch(1000);
// 1000個線程
for (int j = 0; j < 1000; j++) {
es.execute(new Runnable() {
@Override
public void run() {
// Random rd = new Random(1100);
for (int k = 0; k < 10000; k++) {
sh.getNode(String.valueOf(Math.random())).inc();
}
cdl.countDown();
}
});
}
// 等待所有線程結束
cdl.await();
List<Node> nodeList = sh.getReal();
for (Node node : nodeList) {
System.out.println("node" + node.getName() + ":" + node.getCount());
}
}
}
測試結果:
一共10,000,000次 hash,基本算是較均勻投遞到10個節點,有什麼問題大家交流~