Preface
When reading the community code recently, a creative program algorithm-conformance Hashi was discovered. That is, the implementation of a consistent hashing algorithm, by a Microsoft engineer in the Community Code, the author review, feel the code to achieve rigorous simplicity, and the consistency of the characteristics of Hashi all considered, is a very good algorithm program. In this paper, it is simply analyzed and explained. Consistent hashing algorithm here is not much to introduce, you can click the author previously wrote the article Consistency hashing algorithm. Consistent hashing algorithm has many application scenarios in distributed system, mainly to solve the problem of "hot spot" in data. Currently this algorithm is used to select the target cluster location for the data to be written, the target cluster will have many, and the written file data can only select 1 of the clusters. implementation of conformance Hashi algorithm
/** * Consistent hash ring to distribute items across nodes (locations).
If we add * or remove nodes, it minimizes the item migration.
* Consistency Hashi, the selection of node locations for decentralized entity items, reducing the migration of the entity items to which they belong because of a change in the node.
*/public class Consistenthashring {private static final String seperator = "/";
private static final String Virtual_node_format = "%s" + seperator + "%d"; /** Hash ring Hashi.
*/Private sortedmap<string, string> ring = new treemap<string, string> (); /** virtual node information, node number mapping information *//** Entry, num virtual nodes on ring.
*/Private map<string, integer> entrytovirtualnodes = new hashmap<string, integer> (); /** synchronization lock Synchronization.
*/private final Readwritelock Readwritelock = new Reentrantreadwritelock ();
Private final Lock Readlock = Readwritelock.readlock ();
Private final Lock Writelock = Readwritelock.writelock ();
Public consistenthashring (set<string> locations) {for (String location:locations) {//Add location information inside the ring Addlocation (location);
}}/** * Add entry to consistent hash ring.
* Add entity to the hash ring * @param location Node to add to the ring.
*/public void Addlocation (String location) {//virtual out of 100 nodes inserted into addlocation (location, 100);
}/** * ADD entry to consistent hash ring.
* Add specific items to the hash ring within * @param location needs to add nodes.
* @param numvirtualnodes The number of virtual nodes that need to be added.
*/public void Addlocation (String location, int numvirtualnodes) {writelock.lock ();
try {//Update virtual node list information Entrytovirtualnodes.put (location, numvirtualnodes); for (int i = 0; i < numvirtualnodes; i++) {//Get virtual node name String key = String.Format (Virtual_node_format,
location, i);
Take its hash value String hash = Gethash (key);
Add to the hash ring within the ring.put (hash, key);
}} finally {Writelock.unlock ();
}}/** * Remove specified entry from hash ring.
* Remove entity items from the hash ring * @param location Node to remove from the ring. */public void RemoveLocation (String location) {writelock.lock ();
try {//Remove the given node position and get its corresponding number of virtual nodes Integer Numvirtualnodes = entrytovirtualnodes.remove (location); for (int i = 0; i < numvirtualnodes; i++) {//Gets the virtual node key and removes String key = String.Format from the hash ring (virtual_no
De_format, location, i);
String hash = Gethash (key);
Ring.remove (hash);
}} finally {Writelock.unlock (); }}/** * Return location (owner) of specified item.
Owner is the next * entry in the hash ring (with a hash value > hash value of Item).
* From the hash ring to get its nearest node location * @param item item to the look for.
* @return The location of the item.
*/public string getLocation (string item) {Readlock.lock ();
try {if (Ring.isempty ()) {return null;
}//Computes the hash value of the input path String hash = Gethash (item); If the hash ring does not exactly contain this node if (!ring.containskey (hash)) {//Hashi is positioned to the first position greater than this key sortedmap<string, STRING&G T Tailmap = Ring.tailmap (hash); And get the key of the first item larger than this key, which is the nearest key hash = Tailmap.isempty ()?
Ring.firstkey (): Tailmap.firstkey ();
}//Based on this key to obtain the corresponding virtual node information String Virtualnode = Ring.get (hash);
Then get the actual location information from the virtual node information int index = VIRTUALNODE.LASTINDEXOF (seperator);
if (index >= 0) {return virtualnode.substring (0, index);
} else {return virtualnode;
}} finally {Readlock.unlock ();
}} public String Gethash (string key) {return md5hash.digest (key). ToString ();
}/** * Get the locations in the ring.
* @return Set of locations in the ring.
*/Public set<string> getlocations () {return entrytovirtualnodes.keyset (); }
}