forked from colinmollenhour/credis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cluster.php
193 lines (176 loc) · 4.36 KB
/
Cluster.php
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
<?php
/**
* Credis, a Redis interface for the modest
*
* @author Justin Poliey <[email protected]>
* @copyright 2009 Justin Poliey <[email protected]>
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
* @package Credis
*/
#require_once 'Credis/Client.php';
/**
* A generalized Credis_Client interface for a cluster of Redis servers
*/
class Credis_Cluster
{
/**
* Collection of Credis_Client objects attached to Redis servers
* @var Credis_Client[]
*/
protected $clients;
/**
* Aliases of Credis_Client objects attached to Redis servers, used to route commands to specific servers
* @see Credis_Cluster::to
* @var array
*/
protected $aliases;
/**
* Hash ring of Redis server nodes
* @var array
*/
protected $ring;
/**
* Individual nodes of pointers to Redis servers on the hash ring
* @var array
*/
protected $nodes;
/**
* The commands that are not subject to hashing
* @var array
* @access protected
*/
protected $dont_hash;
/**
* Creates an interface to a cluster of Redis servers
* Each server should be in the format:
* array(
* 'host' => hostname,
* 'port' => port,
* 'timeout' => timeout,
* 'alias' => alias
* )
*
* @param array $servers The Redis servers in the cluster.
* @param int $replicas
*/
public function __construct($servers, $replicas = 128)
{
$this->clients = array();
$this->aliases = array();
$this->ring = array();
$clientNum = 0;
foreach ($servers as $server)
{
$client = new Credis_Client($server['host'], $server['port'], isset($server['timeout']) ? $server['timeout'] : 2.5);
$this->clients[] = $client;
if (isset($server['alias'])) {
$this->aliases[$server['alias']] = $client;
}
for ($replica = 0; $replica <= $replicas; $replica++) {
$this->ring[crc32($server['host'].':'.$server['port'].'-'.$replica)] = $clientNum;
}
$clientNum++;
}
ksort($this->ring, SORT_NUMERIC);
$this->nodes = array_keys($this->ring);
$this->dont_hash = array_flip(array(
'RANDOMKEY', 'DBSIZE',
'SELECT', 'MOVE', 'FLUSHDB', 'FLUSHALL',
'SAVE', 'BGSAVE', 'LASTSAVE', 'SHUTDOWN',
'INFO', 'MONITOR', 'SLAVEOF'
));
}
/**
* Get a client by index or alias.
*
* @param string|int $alias
* @return Credis_Client
*/
public function client($alias)
{
if (is_int($alias) && isset($this->clients[$alias])) {
return $this->clients[$alias];
}
else if (isset($this->aliases[$alias])) {
return $this->aliases[$alias];
}
throw new CredisException("Client $alias does not exist.");
}
/**
* Get an array of all clients
*
* @return array|Credis_Client[]
*/
public function clients()
{
return $this->clients;
}
/**
* Execute a command on all clients
*
* @return array
*/
public function all()
{
$args = func_get_args();
$name = array_shift($args);
$results = array();
foreach($this->clients as $client) {
$results[] = $client->__call($name, $args);
}
return $results;
}
/**
* Get the client that the key would hash to.
*
* @param string $key
* @return \Credis_Client
*/
public function byHash($key)
{
return $this->clients[$this->hash($key)];
}
/**
* Execute a Redis command on the cluster with automatic consistent hashing
*
* @param string $name
* @param array $args
* @return mixed
*/
public function __call($name, $args)
{
if (isset($this->dont_hash[strtoupper($name)])) {
$client = $this->clients[0];
}
else {
$client = $this->byHash($args[0]);
}
return $client->__call($name, $args);
}
/**
* Get client index for a key by searching ring with binary search
*
* @param string $key The key to hash
* @return int The index of the client object associated with the hash of the key
*/
public function hash($key)
{
$needle = crc32($key);
$min = 0;
$max = count($this->nodes) - 1;
while ($max >= $min) {
$position = (int) (($min + $max) / 2);
$server = $this->nodes[$position];
if ($needle < $server) {
$max = $position - 1;
}
else if ($needle > $server) {
$min = $position + 1;
}
else {
break;
}
}
return $this->ring[$server];
}
}