Distributed server layout needs to use the hash algorithm, set the node has N, the common hashing algorithm: key%n, in the case of node increase and decrease, for the simple logical calculation hash is no problem, but if the node cluster provides storage function, The effect is not ideal. The idea at this point is that the previous nodes are not affected when the nodes are added, and only those that are associated with reducing the node are affected when the node is reduced. Then we can not simply use the method of%, there is a solution is to abstract the object key and server node key, spaced out on a ring above, install the clockwise way object key to fetch the nearest server node. This method is Consistenthash, and the Java version of the algorithm is posted below:
Import Java.util.collection;import Java.util.sortedmap;import Java.util.treemap;public class ConsistentHash<T > {Private final hashfunction hashfunction;//hash algorithm private final int numberofreplicas;//virtual node number private final Sorted Map<integer, t> circle = new Treemap<integer, t> (); Public Consistenthash (hashfunction hashfunction, int numberofreplicas, collection<t> nodes) {//Physical node This.hash Function = hashfunction; This.numberofreplicas = Numberofreplicas; for (T node:nodes) {Add (node); }} public void Add (T-node) {for (int i = 0; i < Numberofreplicas; i++) {Circle.put (Hashfunction.hash (node.tost Ring () + i), node); }} public void remove (T-node) {for (int i = 0; i < Numberofreplicas; i++) {Circle.remove (Hashfunction.hash (nod E.tostring () + i)); }} public T get (Object key) {//Key algorithm if (Circle.isempty ()) {return null; }//Calculate hash value int hash = Hashfunction.hash (key); If this hash value is not included if (!circle.containskey (haSH) {sortedmap<integer, t> tailmap = Circle.tailmap (hash); hash = Tailmap.isempty ()? Circle.firstkey (): Tailmap.firstkey (); } return Circle.get (hash); }}
An implementation scenario:
Using the Tree Management Server node, each server node holds a list of hash keys that can be indexed to it (the actual application is mostly distributed IP hashes), and when accessed, calculates the hash value of key and compares it to the tree-managed list to find the corresponding mapping server. Tree managed server nodes are not very good decisions can only be based on the keyhash value and the number of servers for Virtual node expansion.
Consistenthash consistent Hash