summaryrefslogtreecommitdiff
path: root/vendor/nmred/kafka-php/src/Kafka/Produce.php
blob: 2b9e6cd3f5889a8585d0f55d0ceb278a81f1bf6d (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
<?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 Produce
{
    // {{{ consts
    // }}}
    // {{{ members

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

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

    /**
     * default the server will not send any response
     *
     * @var float
     * @access private
     */
    private $requiredAck = 0;

    /**
     * default timeout is 100ms
     *
     * @var float
     * @access private
     */
    private $timeout = 100;

    /**
     * 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();

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

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

        return self::$instance;
    }

    // }}}
    // {{{ public function __construct()

    /**
     * __construct
     *
     * @access public
     * @return void
     */
    public function __construct($hostList, $timeout = null, $kafkaHostList = null)
    {
        if ($hostList instanceof \Kafka\ClusterMetaData) {
            $metadata = $hostList;
        } elseif ( $kafkaHostList !== null ) {
            $metadata = new \Kafka\MetaDataFromKafka($kafkaHostList);
        } else {
            $metadata = new \Kafka\ZooKeeper($hostList, $timeout);
        }
        $this->client = new \Kafka\Client($metadata);
    }

    // }}}
    // {{{ public function setMessages()

    /**
     * set send messages
     *
     * @access public
     * @return void
     */
    public function setMessages($topicName, $partitionId = 0, $messages = array())
    {
        if (isset($this->payload[$topicName][$partitionId])) {
            $this->payload[$topicName][$partitionId] =
                    array_merge($this->payload[$topicName][$partitionId], $messages);
        } else {
            $this->payload[$topicName][$partitionId] = $messages;
        }

        return $this;
    }

    // }}}
    // {{{ public function setRequireAck()

    /**
     * set request mode
     * This field indicates how many acknowledgements the servers should receive
     * before responding to the request. If it is 0 the server will not send any
     * response (this is the only case where the server will not reply to a
     * request). If it is 1, the server will wait the data is written to the
     * local log before sending a response. If it is -1 the server will block
     * until the message is committed by all in sync replicas before sending a
     * response. For any number > 1 the server will block waiting for this
     * number of acknowledgements to occur (but the server will never wait for
     * more acknowledgements than there are in-sync replicas).
     *
     * @param int $ack
     * @access public
     * @return void
     */
    public function setRequireAck($ack = 0)
    {
        if ($ack >= -1) {
            $this->requiredAck = (int) $ack;
        }

        return $this;
    }

    // }}}
    // {{{ public function setTimeOut()

    /**
     * set request timeout
     *
     * @param int $timeout
     * @access public
     * @return void
     */
    public function setTimeOut($timeout = 100)
    {
        if ((int) $timeout) {
            $this->timeout = (int) $timeout;
        }
        return $this;
    }

    // }}}
    // {{{ public function send()

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

        $responseData = array();
        foreach ($data as $host => $requestData) {
            $stream = $this->client->getStream($host);
            $conn   = $stream['stream'];
            $encoder = new \Kafka\Protocol\Encoder($conn);
            $encoder->produceRequest($requestData);
            if ((int) $this->requiredAck !== 0) { // get broker response
                $decoder = new \Kafka\Protocol\Decoder($conn);
                $response = $decoder->produceResponse();
                foreach ($response as $topicName => $info) {
                    if (!isset($responseData[$topicName])) {
                        $responseData[$topicName] = $info;
                    } else {
                        $responseData[$topicName] = array_merge($info, $responseData[$topicName]);
                    }
                }
            }

            $this->client->freeStream($stream['key']);
        }

        $this->payload = array();
        return $responseData;
    }

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

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

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

    // }}}
    // {{{ public function getAvailablePartitions()

    /**
     * get available partition
     *
     * @access public
     * @return array
     */
    public function getAvailablePartitions($topicName)
    {
        $topicDetail = $this->client->getTopicDetail($topicName);
        if (is_array($topicDetail) && isset($topicDetail['partitions'])) {
            $topicPartitiions = array_keys($topicDetail['partitions']);
        } else {
            $topicPartitiions = array();
        }

        return $topicPartitiions;
    }

    // }}}
    // {{{ 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 => $messages) {
                $host = $this->client->getHostByPartition($topicName, $partitionId);
                $data[$host][$topicName][$partitionId] = $messages;
            }
        }

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

            $requestData[$host] = array(
                'required_ack' => $this->requiredAck,
                'timeout'      => $this->timeout,
                'data' => $topicData,
            );
        }

       return $requestData;
    }

    // }}}
    // }}}
}