PHP一致性哈希

什么是一致性哈希参考这里:

http://zh.wikipedia.org/zh/%E4%B8%80%E8%87%B4%E5%93%88%E5%B8%8C#.E5.BA.94.E7.94.A8.E5.AE.9E.E4.BE.8B

和这里:

http://baike.baidu.com/view/1588037.htm

目录:

|____classes
| |____Consistenthashing
| | |____Exception.php
| |
|____Hasher
| | | |____Crc32.php
| | | |____Md5.php
| | |
|____Sha1.php
| | |____Hasher.php
|
|____Consistenthashing.php
|____config
| |____consistenthashing.php

classes/Consistenthashing.php


  1 <?php
2 /**
3 * 一致性哈希
4 * @author Sundj
5 *
6 * demo:
7 * $servers = array(
8 * ‘rm11950.eos.com.cn‘,
9 * ‘rm11951.eos.com.cn‘,
10 * ‘rm11952.eos.com.cn‘,
11 * ‘rm11953.eos.com.cn‘,
12 * ‘rs11950.hebe.com.cn‘,
13 * ‘rs11951.hebe.com.cn‘,
14 * ‘rs11952.hebe.com.cn‘,
15 * ‘rs11953.hebe.com.cn‘,
16 * ‘rs11950.atlas.com.cn‘,
17 * ‘rs11951.atlas.com.cn‘,
18 * ‘rs11952.atlas.com.cn‘,
19 * ‘rs11953.atlas.com.cn‘
20 * );
21 *
22 * $consistentHashing = Consistenthashing::instance()->addNodes($servers);
23 *
24 * for($i = 0; $i < 10; $i++) {
25 * echo $consistentHashing->getNode(chr(mt_rand(33, 126)))."\r\n";
26 * }
27 */
28 final class Consistenthashing {
29
30 static protected $_instance = NULL;
31
32 static public function instance($name = ‘‘) {
33
34 if(self::$_instance === NULL) {
35 if($name == ‘‘) {
36 $name = ‘md5‘;
37 }
38
39 $hasher = Consistenthashing_Hasher::factory($name);
40 $config = Kohana::$config->load(‘consistenthashing.md5‘);
41
42 self::$_instance = new self($hasher, $config);
43 }
44
45 return self::$_instance;
46 }
47
48 protected $_replicas = 1;
49
50 protected $_hasher = NULL;
51
52 protected $_nodes = array();
53
54 protected $_virtualNodes = array();
55
56 public function __construct(Consistenthashing_Hasher $hasher, array $config) {
57
58 if(!$config) {
59 throw new Consistenthashing_Exception(‘$config is needed.‘);
60 }
61
62 $this->_hasher = $hasher;
63
64 if(isset($config[‘replicas‘])) {
65 $this->_replicas = $config[‘replicas‘];
66 }
67 }
68
69 /**
70 * 增加节点
71 * @param string $node
72 * @return Consistenthashing
73 */
74 public function addNode($node = NULL) {
75 if(!$node) {
76 throw new Consistenthashing_Exception(‘node: $node can\‘t be NULL‘);
77 }
78
79 $hash = $this->_hasher->hash($node);
80 if(isset($this->_nodes[$hash])) {
81 return $this;
82 }
83 $this->_nodes[$hash] = $node;
84
85 for($i = 0; $i < $this->_replicas; $i++) {
86 $virtualNode = $node .‘#‘. $i;
87 $hash = $this->_hasher->hash($virtualNode);
88
89 $this->_virtualNodes[$hash] = $node;
90 }
91
92 ksort($this->_virtualNodes);
93
94 return $this;
95 }
96
97 /**
98 * 批量增加节点
99 * @param array $nodes
100 * @return Consistenthashing
101 */
102 public function addNodes(array $nodes = array()) {
103 foreach($nodes as $node) {
104 $this->addNode($node);
105 }
106 return $this;
107 }
108
109 /**
110 * 删除一个节点
111 * @param object $node
112 */
113 public function removeNode($node = NULL) {
114 $hash = $this->_hasher->hash($node);
115 if(!isset($this->_nodes[$hash])) {
116 return $this;
117 }
118 unset($this->_nodes[$hash]);
119
120 for($i = 0; $i < $this->_replicas; $i++) {
121 $virtualNode = $node .‘#‘. $i;
122
123 $hash = $this->_hasher->hash($virtualNode);
124 unset($this->_virtualNodes[$hash]);
125 }
126 return $this;
127 }
128
129 /**
130 * 批量删除节点
131 * @param array $nodes
132 * @return Consistenthashing
133 */
134 public function removeNodes(array $nodes = array()) {
135 foreach($nodes as $node) {
136 $this->removeNode($node);
137 }
138 return $this;
139 }
140
141 /**
142 * 全部节点(虚拟)
143 * @return array
144 */
145 public function all() {
146 return $this->_virtualNodes;
147 }
148
149 /**
150 * 获得节点
151 * @param string $key
152 * @return string
153 */
154 public function getNode($key) {
155 $hashKey = $this->_hasher->hash($key);
156
157 $nodeFound = NULL;
158 $counter = 0;
159 foreach($this->_virtualNodes as $hash => $node) {
160 if($counter == 0) {
161 $nodeFound = $node;
162 }
163 if($hash > $hashKey) {
164 $nodeFound = $node;
165 break;
166 }
167 }
168
169 return $nodeFound;
170 }
171 }

classes/Consistenthashing/Hasher.php


 1 <?php
2 abstract class Consistenthashing_Hasher {
3
4 static public function factory($name) {
5 if($name == ‘md5‘) {
6 return new Consistenthashing_Hasher_Md5();
7 }
8 if($name == ‘sha1‘) {
9 return new Consistenthashing_Hasher_Sha1();
10 }
11 if($name == ‘crc32‘) {
12 return new Consistenthashing_Hasher_Crc32();
13 }
14 throw new Consistenthashing_Exception("Undefined hashing method: {$name}. md5, sha1 and crc32 are supported.");
15 }
16
17 abstract public function hash($object);
18
19 }

classes/Consistenthashing/Hasher/Md5.php


<?php
class Consistenthashing_Hasher_Md5 extends Consistenthashing_Hasher {

public function hash($object) {
return md5($object);
}

}

config/consistenthashing.php


 1 <?php
2 return array(
3 ‘md5‘ => array(
4 ‘replicas‘ => 64,
5 ),
6
7 ‘crc32‘ => array(
8 ‘replicas‘ => 64,
9 ),
10
11 ‘sha1‘ => array(
12 ‘replicas‘ => 64,
13 ),
14 );

PHP一致性哈希

时间: 2024-08-12 04:56:52

PHP一致性哈希的相关文章

转(一致性哈希算法(consistent hashing))

转自:http://blog.csdn.net/cywosp/article/details/23397179 一致性哈希算法在1997年由麻省理工学院提出的一种分布式哈希(DHT)实现算法,设计目标是为了解决因特网中的热点(Hot spot)问题,初衷和CARP十分类似.一致性哈希修正了CARP使用的简 单哈希算法带来的问题,使得分布式哈希(DHT)可以在P2P环境中真正得到应用. 一致性hash算法提出了在动态变化的Cache环境中,判定哈希算法好坏的四个定义: 1.平衡性(Balance)

一致性哈希算法原理

一致性Hash算法背景 一致性哈希算法在1997年由麻省理工学院的Karger等人在解决分布式Cache中提出的,设计目标是为了解决因特网中的热点(Hot spot)问题,初衷和CARP十分类似.一致性哈希修正了CARP使用的简单哈希算法带来的问题,使得DHT可以在P2P环境中真正得到应用. 但现在一致性hash算法在分布式系统中也得到了广泛应用,研究过memcached缓存数据库的人都知道,memcached服务器端本身不提供分布式cache的一致性,而是由客户端来提供,具体在计算一致性has

一致性哈希算法(consistent hashing)(转)

原文链接:每天进步一点点——五分钟理解一致性哈希算法(consistent hashing) 一致性哈希算法在1997年由麻省理工学院提出的一种分布式哈希(DHT)实现算法,设计目标是为了解决因特网中的热点(Hot spot)问题,初衷和CARP十分类似.一致性哈希修正了CARP使用的简 单哈希算法带来的问题,使得分布式哈希(DHT)可以在P2P环境中真正得到应用. 一致性hash算法提出了在动态变化的Cache环境中,判定哈希算法好坏的四个定义: 1.平衡性(Balance):平衡性是指哈希的

Go语言实现一致性哈希(Consistent Hashing)算法

一致性哈希可用于解决服务器均衡问题. 用Golang简单实现了下,并加入了权重.可采用合适的权重配合算法使用. package main //一致性哈希(Consistent Hashing) //author: Xiong Chuan Liang //date: 2015-2-20 import ( "fmt" "hash/crc32" "sort" "strconv" "sync" ) const DE

一致性哈希算法

tencent2012笔试题附加题    问题描述: 例如手机朋友网有n个服务器,为了方便用户的访问会在服务器上缓存数据,因此用户每次访问的时候最好能保持同一台服务器.已有的做法是根据ServerIPIndex[QQNUM%n]得到请求的服务器,这种方法很方便将用户分到不同的服务器上去.但是如果一台服务器死掉了,那么n就变为了n-1,那么ServerIPIndex[QQNUM%n]与ServerIPIndex[QQNUM%(n-1)]基本上都不一样了,所以大多数用户的请求都会转到其他服务器,这样

一致性哈希算法及其在分布式系统中的应用(转)

原文:http://blog.codinglabs.org/articles/consistent-hashing.html 本文将会从实际应用场景出发,介绍一致性哈希算法(Consistent Hashing)及其在分布式系统中的应用.首先本文会描述一个在日常开发中经常会遇到的问题场景,借此介绍一致性哈希算法以及这个算法如何解决此问题:接下来会对这个算法进行相对详细的描述,并讨论一些如虚拟节点等与此算法应用相关的话题. 分布式缓存问题 假设我们有一个网站,最近发现随着流量增加,服务器压力越来越

一致性哈希

http://blog.csdn.net/sparkliang/article/details/5279393 比如你有 N 个 cache 服务器(后面简称 cache ),那么如何将一个对象 object 映射到 N 个 cache 上呢,你很可能会采用类似下面的通用方法计算 object 的 hash 值,然后均匀的映射到到 N 个 cache : hash(object)%N 一切都运行正常,再考虑如下的两种情况: 1 一个 cache 服务器 m down 掉了(在实际应用中必须要考虑

五分钟理解一致性哈希算法(consistent hashing)

转载请说明出处:http://blog.csdn.net/cywosp/article/details/23397179 一致性哈希算法在1997年由麻省理工学院提出的一种分布式哈希(DHT)实现算法,设计目标是为了解决因特网中的热点(Hot spot)问题,初衷和CARP十分类似.一致性哈希修正了CARP使用的简 单哈希算法带来的问题,使得分布式哈希(DHT)可以在P2P环境中真正得到应用. 一致性hash算法提出了在动态变化的Cache环境中,判定哈希算法好坏的四个定义: 1.平衡性(Bal

一致性哈希(consistent hashing)算法

文章同步发表在博主的网站朗度云,传输门:http://www.wolfbe.com/detail/201608/341.html 1.背景 我们都知道memcached服务器是不提供分布式功能的,memcached的分布式完全是由客户端来实现的.在部署memcached服务器集群时,我们需要把缓存请求尽可能分散到不同的缓存服务器中,这样可以使得所有的缓存空间都得到利用,而且可以降低单独一台缓存服务器的压力.     最简单的一种实现是,缓存请求时通过计算key的哈希值,取模后映射到不同的memc

一致性哈希算法及其在分布式系统中的应用 作者 张洋

转 http://blog.codinglabs.org/articles/consistent-hashing.html 摘要 本文将会从实际应用场景出发,介绍一致性哈希算法(Consistent Hashing)及其在分布式系统中的应用.首先本文会描述一个在日常开发中经常会遇到的问题场景,借此介绍一致性哈希算法以及这个算法如何解决此问题:接下来会对这个算法进行相对详细的描述,并讨论一些如虚拟节点等与此算法应用相关的话题. 分布式缓存问题 假设我们有一个网站,最近发现随着流量增加,服务器压力越