最近在研究"一致性HASH算法"(Consistent Hashing),用于解决memcached集群中当服务器出现增减变动时对散列值的影响。后来 在JAVAEYE上的一篇文章中,找到了其中的 KetamaHash 算法的JAVA实现(一种基于虚拟结点的HASH算法),于是为了加深理解,对照 JAVA版本,用C#重写了一个。放到这里,如果大家感兴趣的话, 可以下载测试一下,如果发现写法有问题请及时告之我,以便我及时修正。 
 
      下面是对Ketama的介绍: 

一致性哈希算法(c#版)
 Ketama is an implementation of a consistent hashing algorithm, meaning you can add or remove servers from the memcached pool without causing a complete remap of all keys. 
 Here’s how it works: 
 * Take your list of servers (eg: 1.2.3.4:11211, 5.6.7.8:11211, 9.8.7.6:11211) 
 * Hash each server string to several (100-200) unsigned ints 
 * Conceptually, these numbers are placed on a circle called the continuum. (imagine a clock face that goes from 0 to 2^32) 
 * Each number links to the server it was hashed from, so servers appear at several points on the continuum, by each of the numbers they hashed to. 
 * To map a key->server, hash your key to a single unsigned int, and find the next biggest number on the continuum. The server linked to that number is the correct server for that key. 
 * If you hash your key to a value near 2^32 and there are no points on the continuum greater than your hash, return the first server in the continuum. 
 If you then add or remove a server from the list, only a small proportion of keys end up mapping to different servers.
一致性哈希算法(c#版)

相关文章:

  • 2021-12-29
猜你喜欢
  • 2021-05-20
  • 2021-06-21
  • 2021-11-20
相关资源
相似解决方案