A simple example of consistent hashing is very easy to understand.
First, there is a device class that defines the machine name and IP Address:
public class Cache{public String name;public String ipAddress;}
Then there is the basic implementation:
Public class shard <t> {// The hash algorithm does not guarantee an absolute balance. If the cache is small, objects cannot be evenly mapped to the cache, // Add the virtual node private treemap <long, T> nodes; private list <t> shards; // The Node fragment private final int node_num = 10; // The number of virtual nodes associated with each machine node public shard (list <t> shards) {This. shards = shards; Init ();} private void Init () {nodes = new treemap <long, T> (); For (INT I = 0; I <shards. size (); I ++) {// traverse the real node final t shardinfo = shards. get (I); For (INT n = 0; n <node_num; n ++) {// The Real node is associated with the virtual node. The real node is value; nodes. put (long) Hash ("shard-" + I + "-node-" + n), shardinfo);} system. out. println (shardinfo) ;}} public t getshardinfo (string key) {sortedmap <long, T> tail = nodes. tailmap (long) Hash (key); If (tail. size () = 0) {return nodes. get (nodes. firstkey ();} // locate the recent virtual node return tail. get (tail. firstkey ();}/*** improved 32-bit FNV algorithm, highly discrete ** @ Param string * @ return int value */public static int Hash (string Str) {final int P = 16777619; int hash = (INT) 2166136261l; for (byte B: Str. getbytes () hash = (hash ^ B) * P; hash + = hash <13; hash ^ = hash> 7; hash + = hash <3; hash ^ = hash> 17; hash + = hash <5; return hash ;}}
It's all done here. Isn't it very easy? Let's try it below:
Public class test {/*** @ Param ARGs */public static void main (string [] ARGs) {list <cache> mycaches = new arraylist <cache> (); cache cache1 = new cache (); cache1.name = "computer1"; cache cache2 = new cache (); cache2.name = "computer2"; mycaches. add (cache1); mycaches. add (cache2); shard <cache> myshard = new shard <cache> (mycaches); cache currentcache = myshard. getshardinfo ("info1"); system. out. println (currentcache. name); // For (INT I = 0; I <20; I ++) // {// String object = getrandomstring (20 ); // generate a random string of 20 characters // cache currentcache = myshard. getshardinfo (object); // system. out. println (currentcache. name); //} public static string getrandomstring (INT length) {// Length indicates the length of the generated string base = "abcdefghijklmnopqrstuvwxyz0123456789"; random = new random (); stringbuffer sb = new stringbuffer (); For (INT I = 0; I <length; I ++) {int number = random. nextint (base. length (); sb. append (base. charat (number);} return sb. tostring ();}}
We have two devices, computer1 and computer2. We need to build a 32-power ring for the first initialization and put the device on it. This ring is implemented by the improved FNV algorithm. The location is also determined by the hash algorithm.
However, we only have two devices, which are obviously unevenly distributed on the ring (this is not an explanation, and there are a lot of information on the Internet ). So we add 10 Virtual Devices to each device.
The final distribution is as follows:
[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected]
-Is the ratio between 2147483648 and 2147483647 uniform? This is Java. Assume that C # is 0 ~ The power of 2. Hash calculates the key value as 2049553054, and finds the nearest position clockwise, that is
[email protected]
As a result, computer1 is located.
It is best to see how the balance is: cancel the above staring code and loop for 20 times. The result is as follows:
Computer1
Computer2
Computer1
Computer2
Computer1
Computer2
Computer1
Computer1
Computer1
Computer2
Computer2
Computer2
Computer1
Computer2
Computer1
Computer1
Computer1
Computer2
Computer1
Computer2
You can try it on your own,
The FNV hash algorithm is a highly discrete hash algorithm, which is especially suitable for hash similar strings, such as URLs, IP addresses, host names, and file names.
The following service uses FNV:
1. Calc
2. DNS
3. mdbm key/value query function
4. Database Index HASH
5. Mainstream web query/index Engines
6. High-Performance email service
7. Message ID-based Query Functions
8. Auti-spam anti-spam filter
9. NFS implementation (for example, FreeBSD 4.3, Linux NFS V4)
10. cohesia mass Project
11. spellchecker of Ada 95
12. Open-source x86 Assembler: flatassembler User-Defined symbol hashtree
13. powerbasic
14. Text resources on pS2 and Xbox
15. Non-encrypted image file fingerprints
16. fret
17. Symbian dasm
18. hash_map Implementation of VC ++ 2005
19. libketama in memcache
20. php 5.x
21. Improve cache fragments in Twitter
22. BSD ide Project
23. deliantra game server
24. leprechaun
25. IPv6 Flow tag
Consistent hashing sample + example.