`
catastiger
  • 浏览: 136237 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

一致性HASH算法

    博客分类:
  • JAVA
 
阅读更多
package com.test;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.SortedMap;
import java.util.TreeMap;

public class ConsistentHash<T> {

	private final HashFunction hashFunction;/** Hash计算对象,用于自定义hash算法 */
	private final int numberOfReplicas;/** 复制的节点个数 */
	private final SortedMap<Integer, T> circle = new TreeMap<Integer, T>();/** 一致性Hash环 */

	public ConsistentHash(HashFunction hashFunction, int numberOfReplicas,Collection<T> nodes) {
		this.hashFunction = hashFunction;
		this.numberOfReplicas = numberOfReplicas;

		for (T node : nodes) {
			add(node);
		}
	}
	
	/**
	* 增加节点<br>
	* 每增加一个节点,就会在闭环上增加给定复制节点数<br>
	* 例如复制节点数是2,则每调用此方法一次,增加两个虚拟节点,这两个节点指向同一Node
	* 由于hash算法会调用node的toString方法,故按照toString去重
	* @param node 节点对象
	*/
	public void add(T node) {
		for (int i = 0; i < numberOfReplicas; i++) {
			circle.put(hashFunction.hash(node.toString() + i), node);
		}
	}
	/**
	* 移除节点的同时移除相应的虚拟节点
	* @param node 节点对象
	*/
	public void remove(T node) {
		for (int i = 0; i < numberOfReplicas; i++) {
			circle.remove(hashFunction.hash(node.toString() + i));
		}
	}
	/**
	* 获得一个最近的顺时针节点
	* @param key 为给定键取Hash,取得顺时针方向上最近的一个虚拟节点对应的实际节点
	* @return 节点对象
	*/
	public T get(Object key) {
		if (circle.isEmpty()) {
			return null;
		}
		int hash = hashFunction.hash(key);
		// System.out.println("hash---: " + hash);
		if (!circle.containsKey(hash)) {
			//返回此映射的部分视图,其键大于等于 hash
			SortedMap<Integer, T> tailMap = circle.tailMap(hash);
			hash = tailMap.isEmpty() ? circle.firstKey() : tailMap.firstKey();
		}
		// System.out.println("hash---: " + hash);
		return circle.get(hash);
	}
	/***
	 * 自定义HASH算法
	 *
	 */
	static class HashFunction {
		int hash(Object key) {
			// md5加密后,hashcode
			return MD5Encrypt.encode(key.toString()).hashCode();
		}
	}

	public static void main(String[] args) {
		HashSet<String> set = new HashSet<String>();
		set.add("A");
		set.add("B");
		set.add("C");
		set.add("D");

		Map<String, Integer> map = new HashMap<String, Integer>();

		ConsistentHash<String> consistentHash = new ConsistentHash<String>(new HashFunction(), 1000, set);

		int count = 10000;

		for (int i = 0; i < count; i++) {
			String key = consistentHash.get(i);
			if (map.containsKey(key)) {
				map.put(consistentHash.get(i), map.get(key) + 1);
			} else {
				map.put(consistentHash.get(i), 1);
			}
			// System.out.println(key);
		}

		showServer(map);
		map.clear();
		consistentHash.remove("A");

		System.out.println("------- remove A");

		for (int i = 0; i < count; i++) {
			String key = consistentHash.get(i);
			if (map.containsKey(key)) {
				map.put(consistentHash.get(i), map.get(key) + 1);
			} else {
				map.put(consistentHash.get(i), 1);
			}
			// System.out.println(key);
		}

		showServer(map);
		map.clear();
		consistentHash.add("E");
		System.out.println("------- add E");

		for (int i = 0; i < count; i++) {
			String key = consistentHash.get(i);
			if (map.containsKey(key)) {
				map.put(consistentHash.get(i), map.get(key) + 1);
			} else {
				map.put(consistentHash.get(i), 1);
			}
			// System.out.println(key);
		}

		showServer(map);
		map.clear();

		consistentHash.add("F");
		System.out.println("------- add F服务器  业务量加倍");
		count = count * 2;
		for (int i = 0; i < count; i++) {
			String key = consistentHash.get(i);
			if (map.containsKey(key)) {
				map.put(consistentHash.get(i), map.get(key) + 1);
			} else {
				map.put(consistentHash.get(i), 1);
			}
			// System.out.println(key);
		}

		showServer(map);

	}

	public static void showServer(Map<String, Integer> map) {
		for (Entry<String, Integer> m : map.entrySet()) {
			System.out.println("服务器 " + m.getKey() + "----" + m.getValue()
					+ "个");
		}
	}

}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics