forked from xdebug/xdebug
-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
670-ConsistentHashing.inc
220 lines (178 loc) · 6.66 KB
/
670-ConsistentHashing.inc
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
216
217
218
219
220
<?php
/**
* @author Kijin Sung <kijinbear@gmail.com>
* @package Rediska
* @subpackage Key distributor
* @link http://github.com/kijin/distrib
* @version 0.1.1
* @link http://rediska.geometria-lab.net
* @license http://www.opensource.org/licenses/bsd-license.php
*/
class Rediska_KeyDistributor_ConsistentHashing
{
protected $_backends = array();
protected $_backendsCount = 0;
protected $_hashring = array();
protected $_hashringCount = 0;
protected $_replicas = 256;
protected $_slicesCount = 0;
protected $_slicesHalf = 0;
protected $_slicesDiv = 0;
protected $_cache = array();
protected $_cacheCount = 0;
protected $_cacheMax = 256;
protected $_hashringIsInitialized = false;
/**
* (non-PHPdoc)
* @see Rediska_KeyDistributor_Interface#addConnection
*/
public function addConnection($connectionString, $weight = Rediska_Connection::DEFAULT_WEIGHT)
{
if (isset($this->_backends[$connectionString])) {
throw new Rediska_KeyDistributor_Exception("Connection '$connectionString' already exists.");
}
$this->_backends[$connectionString] = $weight;
$this->_backendsCount++;
$this->_hashringIsInitialized = false;
return $this;
}
/**
* (non-PHPdoc)
* @see Rediska_KeyDistributor_Interface#removeConnection
*/
public function removeConnection($connectionString)
{
if (!isset($this->_backends[$connectionString])) {
throw new Rediska_KeyDistributor_Exception("Connection '$connectionString' not exist.");
}
unset($this->_backends[$connectionString]);
$this->_backendsCount--;
$this->_hashringIsInitialized = false;
return $this;
}
/**
* (non-PHPdoc)
* @see Rediska_KeyDistributor_Interface#getConnectionByKeyName
*/
public function getConnectionByKeyName($name)
{
$connections = $this->getConnectionsByKeyName($name, 1);
if (empty($connections)) {
throw new Rediska_KeyDistributor_Exception('No connections exist');
}
return $connections[0];
}
/**
* Get a list of connections by key name
*
* @param string $name Key name
* @param int $requestedCount The length of the list to return
* @return array List of connections
*/
public function getConnectionsByKeyName($name, $count)
{
// If we have only one backend, return it.
if ($this->_backendsCount == 1) {
return array_keys($this->_backends);
}
if (!$this->_hashringIsInitialized) {
$this->_initializeHashring();
$this->_hashringIsInitialized = true;
}
// If the key has already been mapped, return the cached entry.
if ($this->_cacheMax > 0 && isset($this->_cache[$name])) {
return $this->_cache[$name];
}
// If $count is greater than or equal to the number of available backends, return all.
if ($count >= $this->_backendsCount) return array_keys($this->_backends);
// Initialize the return array.
$return = array();
$crc32 = crc32($name);
// Select the slice to begin with.
$slice = floor($crc32 / $this->_slicesDiv) + $this->_slicesHalf;
// This counter prevents going through more than 1 loop.
$looped = false;
while (true) {
// Go through the hashring, one slice at a time.
foreach ($this->_hashring[$slice] as $position => $backend) {
// If we have a usable backend, add to the return array.
if ($position >= $crc32) {
// If $count = 1, no more checks are necessary.
if ($count === 1) {
$return = array($backend);
break 2;
} elseif (!in_array($backend, $return)) {
$return[] = $backend;
if (count($return) >= $count) break 2;
}
$return = array($backend);
break 1;
}
}
// Continue to the next slice.
$slice++;
// If at the end of the hashring.
if ($slice >= $this->_slicesCount) {
// If already looped once, something is wrong.
if ($looped) {
break 2;
}
// Otherwise, loop back to the beginning.
$crc32 = -2147483648;
$slice = 0;
$looped = true;
}
}
// Cache the result for quick retrieval in the future.
if ($this->_cacheMax > 0) {
// Add to internal cache.
$this->_cache[$name] = $return;
$this->_cacheCount++;
// If the cache is getting too big, clear it.
if ($this->_cacheCount > $this->_cacheMax) {
$this->_cleanCache();
}
}
// Return the result.
return $return;
}
protected function _initializeHashring()
{
if ($this->_backendsCount < 2) {
$this->_hashring = array();
$this->_hashringCount = 0;
$this->_slicesCount = 0;
$this->_slicesHalf = 0;
$this->_slicesDiv = 0;
} else {
$this->_slicesCount = ($this->_replicas * $this->_backendsCount) / 8;
$this->_slicesHalf = $this->_slicesCount / 2;
$this->_slicesDiv = (2147483648 / $this->_slicesHalf);
// Initialize the hashring.
$this->_hashring = array_fill(0, $this->_slicesCount, array());
// Calculate the average weight.
$avg = round(array_sum($this->_backends) / $this->_backendsCount, 2);
// Interate over the backends.
foreach ($this->_backends as $backend => $weight) {
// Adjust the weight.
$weight = round(($weight / $avg) * $this->_replicas);
// Create as many replicas as $weight.
for ($i = 0; $i < $weight; $i++) {
$position = crc32($backend . ':' . $i);
$slice = floor($position / $this->_slicesDiv) + $this->_slicesHalf;
$this->_hashring[$slice][$position] = $backend;
}
}
// Sort each slice of the hashring.
for ($i = 0; $i < $this->_slicesCount; $i++) {
ksort($this->_hashring[$i], SORT_NUMERIC);
}
}
$this->_cleanCache();
}
protected function _cleanCache()
{
$this->_cache = array();
$this->_cacheCount = 0;
}
}