Redis consistent hash

Source: Internet
Author: User

Use zookeeper to implement consistent hash.

When the redis service starts, it writes its route information to ZK through a temporary node, and the client reads available route information through ZK client.

Server

Daemon written with Python script: https://github.com/LittlePeng/redis-manager

The script is deployed on the redis-server host and regularly ping the redis-Server

Node failure:

1. The session expired is lost between the server and the zk server, which is caused by network fluctuations. You need to set the appropriate zookeeper timeout time based on network conditions to avoid this situation.

2. When the server goes down and zookeeper server finds that zkclient Ping times out, it notifies the node to go offline.

3. When the redis-server is down, the redis-manager Ping times out and actively disconnects from the zookeeper Server

 

Client

Zkclient is required to monitor node changes and update routing policies in a timely manner

The following is the consistent version hash of C #.Algorithm:

 
1:ClassKetamanodelocator
 
2:{
3:PrivateDictionary <Long, Rediscluster> ketamanodes;
 
4:PrivateHashalgorithm hashalg;
 
5:Private IntNumreps = 160;
 
6:Private Long[] Keys;
 
7: 
 
8:PublicKetamanodelocator (list <rediscluster> nodes)
 
9:{
10:Ketamanodes =NewDictionary <Long, Rediscluster> ();
 
11: 
 
12:// Generate ncopies virtual nodes for all nodes
 
13:For(IntJ = 0; j <nodes. Count; j ++ ){
 
14:Rediscluster node = nodes [J];
 
15:IntNumreps = node. weight;
 
16: 
 
17:// Each four virtual nodes is a group
18:For(IntI = 0; I <numreps/4; I ++ ){
 
19:Byte[] Digest = computemd5 (
 
20:String. Format ("{0 }_{ 1 }_{ 2 }", Node. rolename, node. routevalue, I ));
 
21: 
 
22:/** MD5 is a 16-byte array, which groups 16-byte arrays every four bytes,
 
23:* Corresponds to one virtual node, which is why four virtual nodes are divided into one group */
24:For(IntH = 0; H <4; H ++ ){
 
25: 
 
26:LongRv = ((Long) (Digest [3 + H * 4] & 0xff) <24)
 
27:| ((Long) (Digest [2 + H * 4] & 0xff) <16)
 
28:| ((Long) (Digest [1 + H * 4] & 0xff) <8)
 
29:| ((Long) Digest [0 + H * 4] & 0xff );
30: 
 
31:Rv = RV & 0 xffffffffl;/* Truncate to 32-bits */
 
32:Ketamanodes [RV] = node;
 
33:}
 
34:}
 
35:}
 
36: 
 
37:Keys = ketamanodes. Keys. orderby (P => P). toarray ();
 
38:}
41:PublicRediscluster getworkernode (StringK)
 
42:{
 
43:Byte[] Digest = computemd5 (k );
 
44:ReturnGetnodeinner (Hash (Digest, 0 ));
 
45:}
 
46: 
 
47:Rediscluster getnodeinner (LongHash)
 
48:{
 
49:If(Ketamanodes. Count = 0)
50:Return Null;
 
51:LongKey = hash;
 
52:IntNear = 0;
 
53:IntIndex = array. binarysearch (keys, hash );
 
54:If(Index <0 ){
 
55:Near = (~ Index );
 
56:If(Near = keys. length)
 
57:Near = 0;
58:}
 
59:Else{
 
60:Near = index;
 
61:}
 
62: 
 
63:ReturnKetamanodes [Keys [near];
 
64:}
 
65: 
 
66:Public Static LongHash (Byte[] Digest,IntNtime)
67:{
 
68:LongRv = ((Long) (Digest [3 + ntime * 4] & 0xff) <24)
 
69:| ((Long) (Digest [2 + ntime * 4] & 0xff) <16)
 
70:| ((Long) (Digest [1 + ntime * 4] & 0xff) <8)
 
71:| ((Long) Digest [0 + ntime * 4] & 0xff );
 
72: 
 
73:ReturnRV & 0 xffffffffl;/* Truncate to 32-bits */
74:}
 
 
 
79:Public Static Byte[] Computemd5 (StringK)
 
80:{
 
81:MD5 MD5 =NewMd5cryptoserviceprovider ();
 
82: 
 
83:Byte[] Keybytes = md5.computehash (encoding. utf8.getbytes (k ));
 
84:Md5.clear ();
 
85:ReturnKeybytes;
86:}
 
87:}
Related Article

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.