1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
/**
  * 一致性hahs实现类
 
  */
class  FlexiHash{
/**
  * var int
  * 虚拟节点
  */
private  $_replicas  = 200;
/**
  * 使用hash方法
  */
private  $_hasher  = null;
/**
  * 真实节点计数器
 
  */
private  $_targetCount  = 0;
/**
  * 位置对应节点,用户lookup中根据位置确定要访问的节点
  */
private  $_positionToTarget  array ();
/**
  * 节点对应位置,用于删除节点
  */
private  $_targetToPositions  array ();
/**
  * 是否已排序
  */
private  $_positionToTargetSorted  = false;
/**
  * @ $hasher hash方法
  * @ $replicas 虚拟节点的个数
 
  * 确定要使用的hash方法和虚拟的节点数,虚拟节点越多,分布越均匀,但程序的分布式运算越慢
  */
public  function  __construct(FlexiHash_Hasher  $hasher =null,  $replicas  = null){
// hash方法
$this ->_hasher =  $hasher ? $hasher new  FlexiHash_Crc32Hasher();
// 虚拟节点的个数
if  (! empty ( $replicas )){
$this ->_replicas =  $replicas ;
}
}
/**
  * 增加节点,根据虚拟节点数,把节点分布到更多的虚拟位置上
  */
public  function  addTarget( $target ){
if  (isset( $this ->_targetToPositions[ $target ])) {
throw  new  FlexiHash_Exception( "Target $target already exists." );
}
$this ->_targetToPositions[ $target ] =  array ();
for  ( $i  = 0;  $i  $this ->_replicas;  $i ++) {
// 根据规定的方法hash
$position  $this ->_hasher->hash( $target . $i );
// 虚拟节点对应的真实的节点
$this ->_positionToTarget[ $position ] =  $target ;
// 真实节点包含的虚拟节点
$this ->_targetToPositions[ $target ][] =  $position ;
}
$this ->_positionToTargetSorted = false;
// 真实节点个数
$this ->_targetCount++;
return  $this ;
}
/**
  * 添加多个节点
 
  */
public  function  addTargets( $targets ){
foreach  ( $targets  as  $target ){
$this ->addTarget( $target );
}
return  $this ;
}
/**
  * 移除某个节点
 
  */
public  function  removeTarget( $target ){
if  (!isset( $this ->_targetToPositions[ $target ])){
throw  new  FlexiHash_Exception( "target $target does not exist\n" );
}
foreach ( $this ->_targetToPositions[ $target as  $position ){
unset( $this ->_positionToTarget[ $position ]);
}
unset( $this ->_targetToPositions[ $target ]);
$this ->_targetCount--;
return  $this ;
}
/**
  * 获取所有节点
 
  */
public  function  getAllTargets(){
return  array_keys ( $this ->_targetToPositions);
}
/**
  * 根据key查找hash到的真实节点
 
  */
public  function  lookup( $resource ){
$targets  $this ->lookupList( $resource , 1);
if  ( empty ( $targets )){
throw  new  FlexiHash_Exception( "no targets exist" );
}
return  $targets [0];
}
/**
  * 查找资源存在的节点
 
  * 描述:根据要求的数量,返回与$resource哈希后数值相等或比其大并且是最小的数值对应的节点,若不存在或数量不够,则从虚拟节点排序后的前一个或多个
  */
public  function  lookupList( $resource $requestedCount ){
if  (! $requestedCount ) {
throw  new  FlexiHash_Exception( 'Invalid count requested' );
}
if  ( empty ( $this ->_positionToTarget)) {
return  array ();
}
// 直接节点只有一个的时候
if  ( $this ->_targetCount == 1 ){
return  array_unique ( array_values ( $this ->_positionToTarget));
}
// 获取当前key进行hash后的值
$resourcePosition  $this ->_hasher->hash( $resource );
$results  array ();
$collect  = false;
$this ->_sortPositionTargets();
// 查找与$resourcePosition 相等或比其大并且是最小的数
foreach ( $this ->_positionToTarget  as  $key  =>  $value ){
if  (! $collect  &&  $key  $resourcePosition ){
$collect  = true;
}
if  ( $collect  && !in_array( $value $results )){
$results [] =  $value ;
}
// 找到$requestedCount 或个数与真实节点数量相同
if  ( count ( $results ) ==  $requestedCount  ||  count ( $results ) ==  $this ->_targetCount){
return  $results ;
}
}
// 如数量不够或者未查到,则从第一个开始,将$results中不存在前$requestedCount-count($results),设置为需要的节点
foreach  ( $this ->_positionToTarget  as  $key  =>  $value ){
if  (!in_array( $value $results )){
$results [] =  $value ;
}
if  ( count ( $results ) ==  $requestedCount  ||  count ( $results ) ==  $this ->_targetCount){
return  $results ;
}
}
return  $results ;
}
/**
  * 根据虚拟节点进行排序
  */
private  function  _sortPositionTargets(){
if  (! $this ->_positionToTargetSorted){
ksort( $this ->_positionToTarget, SORT_REGULAR);
$this ->_positionToTargetSorted = true;
}
}
} // end class
/**
  * hash方式
  */
interface  FlexiHash_Hasher{
public  function  hash( $string );
}
class  FlexiHash_Crc32Hasher  implements  FlexiHash_Hasher{
public  function  hash( $string ){
return  sprintf( "%u" ,crc32( $string ));
}
}
class  FlexiHash_Md5Hasher  implements  FlexiHash_Hasher{
public  function  hash( $string ){
return  substr (md5( $string ), 0, 8);
}
}
class  FlexiHash_Exception  extends  Exception{
}
$runData [ 'BEGIN_TIME' ] = microtime(true);
$key = "lihuibin" ;
for ( $i =0; $i <10; $i ++) {
$targetsArray  array (
"127.0.0.1:11211" ,
"127.0.0.1:11212" ,
"127.0.0.1:11213" ,
"127.0.0.1:11214" ,
# "127.0.0.1:11218"
);
$flexiHashObj  new  FlexiHash( new  FlexiHash_Crc32Hasher(),1);
$result  $flexiHashObj ->addTargets( $targetsArray );
$key = $key . "$i" ;
$targets  $flexiHashObj ->lookup( $key );
var_dump( $targets );
# $key  = md5(mt_rand());
# $targets  $flexiHashObj ->lookup( $key );
#var_dump( $targets );
}
echo  "一致性hash:" ;
var_dump(number_format(microtime(true) -  $runData [ 'BEGIN_TIME' ],6));
exit ;
$runData [ 'BEGIN_TIME' ] = microtime(true); 
$m new  Memcache;
$m ->connect( '127.0.0.1' , 11211); 
for ( $i =0; $i <10000; $i ++) {
$key  = md5(mt_rand());
$b  $m ->set( $key , time(), 0, 10);
}
echo  "单台机器:" ;
var_dump(number_format(microtime(true) -  $runData [ 'BEGIN_TIME' ],6));
?>