summaryrefslogtreecommitdiff
path: root/vendor/nmred/kafka-php/src/Kafka/MetaDataFromKafka.php
blob: 9d2c613e7ad585b49d1286128e7cfd6ab7310086 (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
<?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;

/**
+------------------------------------------------------------------------------
* Cluster metadata provided by kafka
+------------------------------------------------------------------------------
*
* @package
* @version $_SWANBR_VERSION_$
* @copyright Copyleft
* @author ebernhardson@wikimedia.org
+------------------------------------------------------------------------------
*/

class MetaDataFromKafka implements ClusterMetaData
{
    // {{{ consts
    // }}}
    // {{{ members

    /**
     * client
     *
     * @var \Kafka\Client
     * @access private
     */
    private $client;

    /**
     * list of kafka brokers to get metadata from
     *
     * @var array
     * @access private
     */
    private $hostList;

    /**
     * List of all kafka brokers
     *
     * @var array
     * @access private
     */
    private $brokers = array();

    /**
     * List of all loaded topic metadata
     *
     * @var array
     * @access private
     */
    private $topics = array();

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

    /**
     * @var string|array $hostList List of kafka brokers to get metadata from
     * @access public
     */
    public function __construct($hostList)
    {
        if (is_string($hostList)) { // support host list 127.0.0.1:9092,192.168.2.11:9092 form
            $this->hostList = explode(',', $hostList);
        } else {
            $this->hostList = (array)$hostList;
        }
        // randomize the order of servers we collect metadata from
        shuffle($this->hostList);
    }

    // }}}
    // {{{ public function setClient()

    /**
     * @var \Kafka\Client $client
     * @access public
     * @return void
     */
    public function setClient(\Kafka\Client $client)
    {
        $this->client = $client;
    }

    // }}}
    // {{{ public function listBrokers()

    /**
     * get broker list from kafka metadata
     *
     * @access public
     * @return array
     */
    public function listBrokers()
    {
        if ($this->brokers === null) {
            $this->loadBrokers();
        }
        return $this->brokers;
    }

    // }}}
    // {{{ public function getPartitionState()

    public function getPartitionState($topicName, $partitionId = 0)
    {
        if (!isset( $this->topics[$topicName] ) ) {
            $this->loadTopicDetail(array($topicName));
        }
        if ( isset( $this->topics[$topicName]['partitions'][$partitionId] ) ) {
            return $this->topics[$topicName]['partitions'][$partitionId];
        } else {
            return null;
        }
    }

    // }}}
    // {{{ public function getTopicDetail()

    /**
     *
     * @param string $topicName
     * @access public
     * @return array
     */
    public function getTopicDetail($topicName)
    {
        if (!isset( $this->topics[$topicName] ) ) {
            $this->loadTopicDetail(array($topicName));
        }
        if (isset( $this->topics[$topicName] ) ) {
            return $this->topics[$topicName];
        } else {
            return array();
        }
    }

    // }}}
    // {{{ private function loadBrokers()

    private function loadBrokers()
    {
        $this->brokers = array();
        // not sure how to ask for only the brokers without a topic...
        // just ask for a topic we don't care about
        $this->loadTopicDetail(array('test'));
    }

    // }}}
    // {{{ private function loadTopicDetail()

    private function loadTopicDetail(array $topics)
    {
        if ($this->client === null) {
            throw new \Kafka\Exception('client was not provided');
        }
        $response = null;
        foreach ($this->hostList as $host) {
            try {
                $response = null;
                $stream = $this->client->getStream($host);
                $conn = $stream['stream'];
                $encoder = new \Kafka\Protocol\Encoder($conn);
                $encoder->metadataRequest($topics);
                $decoder = new \Kafka\Protocol\Decoder($conn);
                $response = $decoder->metadataResponse();
                $this->client->freeStream($stream['key']);
                break;
            } catch (\Kafka\Exception $e) {
                // keep trying
            }
        }
        if ($response) {
            // Merge arrays using "+" operator to preserve key (which are broker IDs)
            // instead of array_merge (which reindex numeric keys)
            $this->brokers = $response['brokers'] + $this->brokers;
            $this->topics = array_merge($response['topics'], $this->topics);
        } else {
            throw new \Kafka\Exception('Could not connect to any kafka brokers');
        }
    }

    // }}}
    // }}}
}