summaryrefslogtreecommitdiff
path: root/vendor/nmred/kafka-php/src/Kafka/Consumer.php
blob: 5ff2d43f2b55c440a8a67f6aafc3994fa9c2cbf1 (plain)
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
<?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 Consumer
{
    // {{{ consts
    // }}}
    // {{{ members

    /**
     * client
     *
     * @var mixed
     * @access private
     */
    private $client = null;

    /**
     * send message options cache
     *
     * @var array
     * @access private
     */
    private $payload = array();

    /**
     * consumer group
     *
     * @var string
     * @access private
     */
    private $group = '';

    /**
     * from offset
     *
     * @var mixed
     * @access private
     */
    private $fromOffset = true;

    /**
     * produce instance
     *
     * @var \Kafka\Produce
     * @access private
     */
    private static $instance = null;

    /**
     * broker host list
     *
     * @var array
     * @access private
     */
    private $hostList = array();

    /**
     * save broker connection
     *
     * @var array
     * @access private
     */
    private $stream = array();

    /**
     * maxSize
     *
     * @var integer
     */
    private $maxSize = 1048576;

    /**
     * offsetStrategy
     * @var integer
     */
    private $offsetStrategy = \Kafka\Offset::DEFAULT_EARLY;

    // }}}
    // {{{ functions
    // {{{ public function static getInstance()

    /**
     * set send messages
     *
     * @access public
     * @return void
     */
    public static function getInstance($hostList, $timeout = null)
    {
        if (is_null(self::$instance)) {
            self::$instance = new self($hostList, $timeout);
        }

        return self::$instance;
    }

    // }}}
    // {{{ private function __construct()

    /**
     * __construct
     *
     * @access public
     * @return void
     */
    private function __construct($hostList, $timeout = null)
    {
        $zookeeper = new \Kafka\ZooKeeper($hostList, $timeout);
        $this->client = new \Kafka\Client($zookeeper);
    }

    // }}}
    // {{{ public function clearPayload()

    /**
     * clearPayload
     *
     * @access public
     * @return void
     */
    public function clearPayload()
    {
        $this->payload = array();
    }

    // }}}
    // {{{ public function setTopic()

    /**
     * set topic name
     *
     * @access public
     * @return void
     */
    public function setTopic($topicName, $defaultOffset = null)
    {
        $parts = $this->client->getTopicDetail($topicName);
        if (!isset($parts['partitions']) || empty($parts['partitions'])) {
            // set topic fail.
            return $this;
        }

        foreach ($parts['partitions'] as $partId => $info) {
            $this->setPartition($topicName, $partId, $defaultOffset);
        }

        return $this;
    }

    // }}}
    // {{{ public function setPartition()

    /**
     * set topic partition
     *
     * @access public
     * @return void
     */
    public function setPartition($topicName, $partitionId = 0, $offset = null)
    {
        if (is_null($offset)) {
            if ($this->fromOffset) {
                $offsetObject = new \Kafka\Offset($this->client, $this->group, $topicName, $partitionId);
                $offset = $offsetObject->getOffset($this->offsetStrategy);
                \Kafka\Log::log('topic name:' . $topicName . ', part:' . $partitionId . 'get offset from kafka server, offet:' . $offset, LOG_DEBUG);
            } else {
                $offset = 0;
            }
        }
        $this->payload[$topicName][$partitionId] = $offset;

        return $this;
    }

    // }}}
    // {{{ public function setFromOffset()

    /**
     * set whether starting offset fetch
     *
     * @param boolean $fromOffset
     * @access public
     * @return void
     */
    public function setFromOffset($fromOffset)
    {
        $this->fromOffset = (boolean) $fromOffset;
    }

    // }}}
    // {{{ public function setMaxBytes()

    /**
     * set fetch message max bytes
     *
     * @param int $maxSize
     * @access public
     * @return void
     */
    public function setMaxBytes($maxSize)
    {
        $this->maxSize = $maxSize;
    }

    // }}}
    // {{{ public function setGroup()

    /**
     * set consumer group
     *
     * @param string $group
     * @access public
     * @return void
     */
    public function setGroup($group)
    {
        $this->group = (string) $group;
        return $this;
    }

    // }}}
    // {{{ public function fetch()

    /**
     * fetch message to broker
     *
     * @access public
     * @return void
     */
    public function fetch()
    {
        $data = $this->_formatPayload();
        if (empty($data)) {
            return false;
        }

        $responseData = array();
        $streams = array();
        foreach ($data as $host => $requestData) {
            $connArr = $this->client->getStream($host);
            $conn    = $connArr['stream'];
            $encoder = new \Kafka\Protocol\Encoder($conn);
            $encoder->fetchRequest($requestData);
            $streams[$connArr['key']] = $conn;
        }

        $fetch = new \Kafka\Protocol\Fetch\Topic($streams, $data);

        // register fetch helper
        $freeStream = new \Kafka\Protocol\Fetch\Helper\FreeStream($this->client);
        $freeStream->setStreams($streams);
        \Kafka\Protocol\Fetch\Helper\Helper::registerHelper('freeStream', $freeStream);

        // register partition commit offset
        $commitOffset = new \Kafka\Protocol\Fetch\Helper\CommitOffset($this->client);
        $commitOffset->setGroup($this->group);
        \Kafka\Protocol\Fetch\Helper\Helper::registerHelper('commitOffset', $commitOffset);

        $updateConsumer = new \Kafka\Protocol\Fetch\Helper\Consumer($this);
        \Kafka\Protocol\Fetch\Helper\Helper::registerHelper('updateConsumer', $updateConsumer);

        return $fetch;
    }

    // }}}
    // {{{ public function getClient()

    /**
     * get client object
     *
     * @access public
     * @return void
     */
    public function getClient()
    {
        return $this->client;
    }

    /**
     * passthru method to client for setting stream options
     *
     * @param array $options
     */
    public function setStreamOptions($options = array())
    {
        $this->client->setStreamOptions($options);
    }

    // }}}
    // {{{ private function _formatPayload()

    /**
     * format payload array
     *
     * @access private
     * @return array
     */
    private function _formatPayload()
    {
        if (empty($this->payload)) {
            return array();
        }

        $data = array();
        foreach ($this->payload as $topicName => $partitions) {
            foreach ($partitions as $partitionId => $offset) {
                $host = $this->client->getHostByPartition($topicName, $partitionId);
                $data[$host][$topicName][$partitionId] = $offset;
            }
        }

        $requestData = array();
        foreach ($data as $host => $info) {
            $topicData = array();
            foreach ($info as $topicName => $partitions) {
                $partitionData = array();
                foreach ($partitions as $partitionId => $offset) {
                    $partitionData[] = array(
                        'partition_id' => $partitionId,
                        'offset'       => $offset,
                        'max_bytes'    => $this->maxSize,
                    );
                }
                $topicData[] = array(
                    'topic_name' => $topicName,
                    'partitions' => $partitionData,
                );
            }

            $requestData[$host] = array(
                'data' => $topicData,
            );
        }

       return $requestData;
    }

    /**
     * const LAST_OFFSET = -1;
     * const EARLIEST_OFFSET = -2;
     * const DEFAULT_LAST  = -2;
     * const DEFAULT_EARLY = -1;
     * @param type $offsetStrategy
     */
    public function setOffsetStrategy($offsetStrategy)
    {
        $this->offsetStrategy = $offsetStrategy;
    }

    // }}}
    // }}}
}