-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathShardedClient.php
More file actions
248 lines (221 loc) · 6.48 KB
/
ShardedClient.php
File metadata and controls
248 lines (221 loc) · 6.48 KB
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php
namespace Aternos\Etcd;
use Aternos\Etcd\Exception\InvalidClientException;
use Etcdserverpb\Compare;
use Etcdserverpb\RequestOp;
use Etcdserverpb\TxnResponse;
use Flexihash\Exception;
use Flexihash\Flexihash;
use Generator;
/**
* Class ShardedClient
*
* @package Aternos\Etcd
*/
class ShardedClient implements ClientInterface
{
/**
* @var ClientInterface[]
*/
protected $clients = [];
/**
* @var ClientInterface[]
*/
protected $keyCache = [];
/**
* @var Flexihash
*/
protected $hash = null;
/**
* ShardedClient constructor.
*
* @param ClientInterface[] $clients
* @throws InvalidClientException
*/
public function __construct(array $clients)
{
foreach ($clients as $client) {
if (!$client instanceof ClientInterface) {
throw new InvalidClientException("Invalid client in client list.");
}
$this->clients[$client->getHostname()] = $client;
}
}
/**
* Get the correct client object for that key through consistent hashing
*
* @param string $key
* @return ClientInterface
* @throws Exception
*/
protected function getClientFromKey(string $key): ClientInterface
{
if (isset($this->keyCache[$key])) {
return $this->keyCache[$key];
}
if ($this->hash === null) {
$this->hash = new Flexihash();
foreach ($this->clients as $client) {
$this->hash->addTarget($client->getHostname());
}
}
$clientHostname = $this->hash->lookup($key);
$this->keyCache[$key] = $this->clients[$clientHostname];
return $this->keyCache[$key];
}
/**
* Get random client
*
* @return ClientInterface
*/
protected function getRandomClient(): ClientInterface
{
$rndIndex = array_rand($this->clients);
return $this->clients[$rndIndex];
}
/**
* @inheritDoc
* @throws Exception
*/
public function getHostname(?string $key = null): string
{
if ($key) {
return $this->getClientFromKey($key)->getHostname($key);
}
return implode("-", array_keys($this->clients));
}
/**
* @inheritDoc
* @throws Exception
*/
public function put(string $key, $value, bool $prevKv = false, int $lease = 0, bool $ignoreLease = false, bool $ignoreValue = false)
{
return $this->getClientFromKey($key)->put($key, $value, $prevKv, $lease, $ignoreLease, $ignoreValue);
}
/**
* @inheritDoc
* @throws Exception
*/
public function get(string $key)
{
return $this->getClientFromKey($key)->get($key);
}
/**
* @param string $prefix
* @param int $limit
* @inheritDoc
* @throws Exception
*/
public function getWithPrefix(string $prefix, int $limit = 100): Generator
{
return $this->getClientFromKey($prefix)->getWithPrefix($prefix, $limit);
}
/**
* @inheritDoc
* @throws Exception
*/
public function delete(string $key)
{
return $this->getClientFromKey($key)->delete($key);
}
/**
* @inheritDoc
* @throws Exception
*/
public function putIf(string $key, string $value, $compareValue, bool $returnNewValueOnFail = false)
{
return $this->getClientFromKey($key)->putIf($key, $value, $compareValue, $returnNewValueOnFail);
}
/**
* @inheritDoc
* @throws Exception
*/
public function deleteIf(string $key, $compareValue, bool $returnNewValueOnFail = false)
{
return $this->getClientFromKey($key)->deleteIf($key, $compareValue, $returnNewValueOnFail);
}
/**
* @inheritDoc
* @throws Exception
*/
public function txnRequest(array $requestOperations, ?array $failureOperations, array $compare): TxnResponse
{
return $this->getRandomClient()->txnRequest($requestOperations, $failureOperations, $compare);
}
/**
* @inheritDoc
* @throws Exception
*/
public function getCompare(string $key, string $value, int $result, int $target): Compare
{
return $this->getClientFromKey($key)->getCompare($key, $value, $result, $target);
}
/**
* @inheritDoc
* @throws Exception
*/
public function getGetOperation(string $key): RequestOp
{
return $this->getClientFromKey($key)->getGetOperation($key);
}
/**
* @inheritDoc
* @throws Exception
*/
public function getPutOperation(string $key, string $value, int $leaseId = 0): RequestOp
{
return $this->getClientFromKey($key)->getPutOperation($key, $value, $leaseId);
}
/**
* @inheritDoc
* @throws Exception
*/
public function getDeleteOperation(string $key): RequestOp
{
return $this->getClientFromKey($key)->getDeleteOperation($key);
}
/**
* @inheritDoc
*/
public function getLeaseID(int $ttl)
{
return $this->getRandomClient()->getLeaseID($ttl);
}
/**
* @inheritDoc
*/
public function revokeLeaseID(int $leaseID)
{
return $this->getRandomClient()->revokeLeaseID($leaseID);
}
/**
* @inheritDoc
*/
public function refreshLease(int $leaseID)
{
return $this->getRandomClient()->refreshLease($leaseID);
}
/**
* Transform TxnResponse into more friendly array
*
* @param TxnResponse $txnResponse return value of txnRequest method
* @param string|null $type returns only chosen type if defined,
* can be one of those: response_range, response_put, response_delete_range, response_txn
* @param bool $simpleArray return just simple array containing values,
* example: ['value1', 'value2']
* @return array example: [
* [
* 'type' => 'response_range',
* 'values' => [
* 'key' => 'key',
* 'value' => '2',
* 'version' => 3
* ]
* ]
* ]
*/
public function getResponses(TxnResponse $txnResponse, ?string $type = null, bool $simpleArray = false): array
{
return $this->getRandomClient()->getResponses($txnResponse, $type, $simpleArray);
}
}