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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
|
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */
// +---------------------------------------------------------------------------
// | SWAN [ $_SWANBR_SLOGAN_$ ]
// +---------------------------------------------------------------------------
// | Copyright $_SWANBR_COPYRIGHT_$
// +---------------------------------------------------------------------------
// | Version $_SWANBR_VERSION_$
// +---------------------------------------------------------------------------
// | Licensed ( $_SWANBR_LICENSED_URL_$ )
// +---------------------------------------------------------------------------
// | $_SWANBR_WEB_DOMAIN_$
// +---------------------------------------------------------------------------
namespace Kafka;
/**
+------------------------------------------------------------------------------
* Kafka protocol since Kafka v0.8
+------------------------------------------------------------------------------
*
* @package
* @version $_SWANBR_VERSION_$
* @copyright Copyleft
* @author $_SWANBR_AUTHOR_$
+------------------------------------------------------------------------------
*/
class ZooKeeper implements \Kafka\ClusterMetaData
{
// {{{ consts
/**
* get all broker
*/
const BROKER_PATH = '/brokers/ids';
/**
* get broker detail
*/
const BROKER_DETAIL_PATH = '/brokers/ids/%d';
/**
* get topic detail
*/
const TOPIC_PATCH = '/brokers/topics/%s';
/**
* get partition state
*/
const PARTITION_STATE = '/brokers/topics/%s/partitions/%d/state';
/**
* register consumer
*/
const REG_CONSUMER = '/consumers/%s/ids/%s';
/**
* list consumer
*/
const LIST_CONSUMER = '/consumers/%s/ids';
/**
* partition owner
*/
const PARTITION_OWNER = '/consumers/%s/owners/%s/%d';
// }}}
// {{{ members
/**
* zookeeper
*
* @var mixed
* @access private
*/
private $zookeeper = null;
// }}}
// {{{ functions
// {{{ public function __construct()
/**
* __construct
*
* @access public
* @return void
*/
public function __construct($hostList, $timeout = null)
{
if (!is_null($timeout) && is_numeric($timeout)) {
$this->zookeeper = new \ZooKeeper($hostList, null, $timeout);
} else {
$this->zookeeper = new \ZooKeeper($hostList);
}
}
// }}}
// {{{ public function listBrokers()
/**
* get broker list using zookeeper
*
* @access public
* @return array
*/
public function listBrokers()
{
$result = array();
$lists = $this->zookeeper->getChildren(self::BROKER_PATH);
if (!empty($lists)) {
foreach ($lists as $brokerId) {
$brokerDetail = $this->getBrokerDetail($brokerId);
if (!$brokerDetail) {
continue;
}
$result[$brokerId] = $brokerDetail;
}
}
return $result;
}
// }}}
// {{{ public function getBrokerDetail()
/**
* get broker detail
*
* @param integer $brokerId
* @access public
* @return void
*/
public function getBrokerDetail($brokerId)
{
$result = array();
$path = sprintf(self::BROKER_DETAIL_PATH, (int) $brokerId);
if ($this->zookeeper->exists($path)) {
$result = $this->zookeeper->get($path);
if (!$result) {
return false;
}
$result = json_decode($result, true);
}
return $result;
}
// }}}
// {{{ public function getTopicDetail()
/**
* get topic detail
*
* @param string $topicName
* @access public
* @return void
*/
public function getTopicDetail($topicName)
{
$result = array();
$path = sprintf(self::TOPIC_PATCH, (string) $topicName);
if ($this->zookeeper->exists($path)) {
$result = $this->zookeeper->get($path);
if (!$result) {
return false;
}
$result = json_decode($result, true);
}
return $result;
}
// }}}
// {{{ public function getPartitionState()
/**
* get partition state
*
* @param string $topicName
* @param integer $partitionId
* @access public
* @return void
*/
public function getPartitionState($topicName, $partitionId = 0)
{
$result = array();
$path = sprintf(self::PARTITION_STATE, (string) $topicName, (int) $partitionId);
if ($this->zookeeper->exists($path)) {
$result = $this->zookeeper->get($path);
if (!$result) {
return false;
}
$result = json_decode($result, true);
}
return $result;
}
// }}}
// {{{ public function registerConsumer()
/**
* register consumer
*
* @param string $topicName
* @param integer $partitionId
* @access public
* @return void
*/
public function registerConsumer($groupId, $consumerId, $topics = array())
{
if (empty($topics)) {
return true;
}
$path = sprintf(self::REG_CONSUMER, (string) $groupId, (string) $consumerId);
$subData = array();
foreach ($topics as $topic) {
$subData[$topic] = 1;
}
$data = array(
'version' => '1',
'pattern' => 'white_list',
'subscription' => $subData,
);
if (!$this->zookeeper->exists($path)) {
$this->makeZkPath($path);
$this->makeZkNode($path, json_encode($data));
} else {
$this->zookeeper->set($path, json_encode($data));
}
}
// }}}
// {{{ public function listConsumer()
/**
* list consumer
*
* @param string $groupId
* @access public
* @return void
*/
public function listConsumer($groupId)
{
$path = sprintf(self::LIST_CONSUMER, (string) $groupId);
if (!$this->zookeeper->exists($path)) {
return array();
} else {
return $this->zookeeper->getChildren($path);
}
}
// }}}
// {{{ public function getConsumersPerTopic()
/**
* get consumer per topic
*
* @param string $groupId
* @access public
* @return array
*/
public function getConsumersPerTopic($groupId)
{
$consumers = $this->listConsumer($groupId);
if (empty($consumers)) {
return array();
}
$topics = array();
foreach ($consumers as $consumerId) {
$path = sprintf(self::REG_CONSUMER, (string) $groupId, (string) $consumerId);
if (!$this->zookeeper->exists($path)) {
continue;
}
$info = $this->zookeeper->get($path);
$info = json_decode($info, true);
$subTopic = isset($info['subscription']) ? $info['subscription'] : array();
foreach ($subTopic as $topic => $num) {
$topics[$topic] = $consumerId;
}
}
return $topics;
}
// }}}
// {{{ public function addPartitionOwner()
/**
* add partition owner
*
* @param string $groupId
* @param string $topicName
* @param integer $partitionId
* @param string $consumerId
* @access public
* @return void
*/
public function addPartitionOwner($groupId, $topicName, $partitionId, $consumerId)
{
$path = sprintf(self::PARTITION_OWNER, (string) $groupId, $topicName, (string) $partitionId);
if (!$this->zookeeper->exists($path)) {
$this->makeZkPath($path);
$this->makeZkNode($path, $consumerId);
} else {
$this->zookeeper->set($path, $consumerId);
}
}
// }}}
// {{{ protected function makeZkPath()
/**
* Equivalent of "mkdir -p" on ZooKeeper
*
* @param string $path The path to the node
* @param mixed $value The value to assign to each new node along the path
*
* @return bool
*/
protected function makeZkPath($path, $value = 0)
{
$parts = explode('/', $path);
$parts = array_filter($parts);
$subpath = '';
while (count($parts) > 1) {
$subpath .= '/' . array_shift($parts);
if (!$this->zookeeper->exists($subpath)) {
$this->makeZkNode($subpath, $value);
}
}
}
// }}}
// {{{ protected function makeZkNode()
/**
* Create a node on ZooKeeper at the given path
*
* @param string $path The path to the node
* @param mixed $value The value to assign to the new node
*
* @return bool
*/
protected function makeZkNode($path, $value)
{
$params = array(
array(
'perms' => \Zookeeper::PERM_ALL,
'scheme' => 'world',
'id' => 'anyone',
)
);
return $this->zookeeper->create($path, $value, $params);
}
// }}}
// }}}
}
|