Consistent hashing algorithm and Java implementation

Source: Internet
Author: User

The consistent hashing algorithm is a common algorithm in distributed systems. For example, a distributed storage system, to store data on a specific node, if the use of ordinary hash method, the data mapped to a specific node, such as Key%n,key is the data key,n is the number of machine nodes, if a machine joins or exits the cluster, then all the data map is invalid , if you are persisting the storage to do the data migration, if it is distributed cache, then the other cache will be invalidated.

Therefore, a consistent hashing algorithm is introduced:

The data is mapped into a large space using a hash function (such as MD5). When the data is stored, a hash value is obtained, corresponding to each position in the ring, such as the K1 corresponds to the position shown in the figure, then a machine node B is found clockwise, and the K1 is stored in the Node B.

If the b node goes down, the data on B falls to the C node, as shown in:

In this way, only the C node is affected and the data of other nodes a,d is not affected. However, this will create an "avalanche" situation, the C node due to bear the B-node data, so the C node load will be high, C node is easy to go down, so in turn, so that the entire cluster is hung.

To this end, the concept of "virtual node" is introduced: that is, there are many "virtual nodes" in this ring, the storage of data is to find a virtual node in the clockwise direction of the ring, each virtual node will be associated to a real node, as used:


The figure of A1, A2, B1, B2, C1, C2, D1, D2 are virtual nodes, machine a load storage A1, A2 data, machine B load Storage B1, B2 data, machine C load Storage C1, C2 data. Because these virtual nodes are large in number and evenly distributed, they do not cause "avalanche" phenomena.

Java implementations:

[Java]View PlainCopyPrint?
  1. Public class Shard<s> { //S class encapsulates information about machine nodes, such as name, password, IP, port, etc.
  2. private Treemap<long, s> nodes; //Virtual node
  3. private list<s> shards; //Real machine node
  4. private Final int node_num = 100; //number of virtual nodes associated with each machine node
  5. Public Shard (list<s> shards) {
  6. super ();
  7. this.shards = shards;
  8. Init ();
  9. }
  10. private void init () { //Initialize consistency hash ring
  11. nodes = new Treemap<long, s> ();
  12. For (int i = 0; I! = Shards.size (); ++i) { //each real machine node requires an associated virtual node
  13. final S shardinfo = Shards.get (i);
  14. For (int n = 0; n < node_num; n++)
  15. //A Real Machine node association node_num virtual Nodes
  16. Nodes.put (Hash ("shard-" + i + "-node-" + N), shardinfo);
  17. }
  18. }
  19. Public S Getshardinfo (String key) {
  20. Sortedmap<long, s> tail = nodes.tailmap (hash (key)); //Find a virtual node clockwise along the ring
  21. if (tail.size () = = 0) {
  22. return Nodes.get (Nodes.firstkey ());
  23. }
  24. return Tail.get (Tail.firstkey ()); //Returns the information of the Real machine node corresponding to the virtual node
  25. }
  26. /** 
  27. * MurmurHash algorithm, non-cryptographic hash algorithm, high performance,
  28. * Compared to traditional crc32,md5,sha-1 (both algorithms are cryptographic hash algorithms, the complexity itself is very high, resulting in the performance of the damage is inevitable)
  29. * Equal hash algorithm is much faster, and it is said that the collision rate of this algorithm is very low.
  30. * http://murmurhash.googlepages.com/
  31. */
  32. Private Long hash (String key) {
  33. Bytebuffer buf = Bytebuffer.wrap (Key.getbytes ());
  34. int seed = 0X1234ABCD;
  35. Byteorder Byteorder = Buf.order ();
  36. Buf.order (Byteorder.little_endian);
  37. long m = 0xc6a4a7935bd1e995l;
  38. int r = 47;
  39. long h = seed ^ (buf.remaining () * m);
  40. long K;
  41. While (buf.remaining () >= 8) {
  42. K = Buf.getlong ();
  43. K *= m;
  44. K ^= k >>> R;
  45. K *= m;
  46. H ^= K;
  47. H *= m;
  48. }
  49. if (buf.remaining () > 0) {
  50. Bytebuffer finish = bytebuffer.allocate (8). Order (
  51. Byteorder.little_endian);
  52. //For Big-endian version, does this first:
  53. //Finish.position (8-buf.remaining ());
  54. Finish.put (BUF). Rewind ();
  55. H ^= Finish.getlong ();
  56. H *= m;
  57. }
  58. H ^= h >>> R;
  59. H *= m;
  60. H ^= h >>> R;
  61. Buf.order (Byteorder);
  62. return h;
  63. }
  64. }

Consistent hashing algorithm and Java implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.