-
Notifications
You must be signed in to change notification settings - Fork 2
/
kafkaHighLevelConsumer.php
291 lines (243 loc) · 8.93 KB
/
kafkaHighLevelConsumer.php
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
<?php
use App\DataExchange\Exceptions\StreamingServiceException;
require __DIR__ . '/vendor/autoload.php';
$argments = [];
$options = [];
foreach ($argv as $idx => $arg) {
if ($idx == 0) {
continue;
}
if (substr($arg, 0, 2) == '--') {
$name = substr($arg, 2);
$value = true;
if (preg_match('/=/', $name)) {
[$name, $value] = explode('=', $name);
}
$options[$name] = $value;
continue;
}
$arguments[] = $arg;
}
$topics = isset($options['topic']) ? explode(',', $options['topic']) : [];
$offset = (int)(isset($options['offset']) ? $options['offset'] : -1);
$onlyResetOffset = (int)(isset($options['offset-only']) ? true : false);
$limit = isset($options['limit']) ? (int)$options['limit'] : false;
$writeToDisk = isset($options['write-to-disk']) ? (int)$options['write-to-disk'] : false;
$singleFile = isset($options['singleFile']) ? (int)$options['singleFile'] : false;
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
function rSortByKeys($array)
{
$obj = false;
if (is_object($array)) {
$array = (array)$array;
$obj = true;
}
foreach ($array as $key => $value) {
if (is_array($value)) {
$array[$key] = rSortByKeys($value);
}
}
ksort($array);
if ($obj) {
return (object)$array;
}
return $array;
}
function commitOffset($consumer, $topicPartition, $offset, $attempt = 0)
{
global $onlyResetOffset;
dump('Commit offset '.$offset);
if ($offset >= 0) {
echo "Committing offset set to $offset for topic ".$topicPartition->getTopic()." on partition ".$topicPartition->getPartition()."...\n";
} else {
echo "Don't update offset.\n";
return;
}
$topicPartition->setOffset($offset);
dump('tp offset: '.$topicPartition->getOffset());
$consumer->commit([$topicPartition]);
if ($onlyResetOffset) {
dd('Offset reset to '.$offset.'!');
}
}
function configure($offset)
{
$conf = new RdKafka\Conf();
// Configure the group.id. All consumer with the same group.id will consume
// different partitions.
$conf->setErrorCb(function ($kafka, $err, $reason) {
throw new StreamingServiceException("Kafka producer error: ".rd_kafka_err2str($err)." (reason: ".$reason.')');
});
$conf->setStatsCb(function ($kafka, $json, $json_len) {
Log::info('Kafka Stats ', json_decode($json));
});
$conf->setDrMsgCb(function ($kafka, $message) {
if ($message->err) {
throw new StreamingServiceException('DrMsg: '.rd_kafka_err2str($message->err));
}
});
// Set where to start consuming messages when there is no initial offset in
// offset store or the desired offset is out of range.
// 'smallest': start from the beginning
// $topicConf = new RdKafka\TopicConf();
// $topicConf->set('auto.offset.reset', 'beginning');
// Set the configuration to use for subscribed/assigned topics
// $conf->setDefaultTopicConf($topicConf);
$conf->set('auto.offset.reset', 'beginning');
// Set a rebalance callback to log partition assignments (optional)
$conf->setRebalanceCb(function (RdKafka\KafkaConsumer $consumer, $err, array $topicPartitions = null) use ($offset) {
switch ($err) {
case RD_KAFKA_RESP_ERR__ASSIGN_PARTITIONS:
echo "Assign partions...";
$consumer->assign($topicPartitions);
foreach ($topicPartitions as $tp) {
commitOffset($consumer, $tp, $offset);
}
break;
case RD_KAFKA_RESP_ERR__REVOKE_PARTITIONS:
$assignments = $consumer->getAssignment();
$consumer->assign(null);
break;
default:
throw new \Exception($err);
}
});
return $conf;
}
function configureForConfluent($offset)
{
$conf = configure($offset);
echo "setting group to ".env('DX_GROUP', 'unc_staging')." for ".env('DX_BROKER')."...\n";
$conf->set('group.id', env('DX_GROUP', 'unc_staging'));
$conf->set('security.protocol', 'sasl_ssl');
$conf->set('metadata.broker.list', env('DX_BROKER'));
$conf->set('sasl.mechanism', 'PLAIN');
echo "Authenticating with DX_USERNAME (".env('DX_USERNAME').") and DX_PASSWORD (see .env)";
$conf->set('sasl.username', env('DX_USERNAME'));
$conf->set('sasl.password', env('DX_PASSWORD'));
return $conf;
}
$messageDir = __DIR__.'/consumed_messages';
if (!file_exists($messageDir)) {
mkdir($messageDir);
echo "made ".$messageDir.' directory';
}
$conf = configureForConfluent($offset);
$consumer = new RdKafka\KafkaConsumer($conf);
if (count($topics) == 0 || array_key_exists('list-topics', $options)) {
$availableTopics = $consumer->getMetadata(true, null, 60e3)->getTopics();
echo "Available Topics: \n";
foreach ($availableTopics as $idx => $avlTopic) {
echo $idx.': '.$avlTopic->getTopic()."\n";
}
echo "Enter the number of the topic(s) do you want to join? (comma-delimit for > 1):\n";
$stdin = fopen('php://stdin', 'r');
$line = fgets($stdin);
$topicIndexes = explode(',', trim($line));
$topics = [];
foreach ($availableTopics as $idx => $avlTopic) {
if (in_array($idx, $topicIndexes)) {
$topics[] = $avlTopic->getTopic();
}
}
fclose($stdin);
}
if (array_key_exists('dont-listen', $options)) {
exit;
}
// Subscribe to topic 'test'
echo "**Subscribing to the following topics:\n".implode("\n ", $topics)."...\n";
$consumer->subscribe($topics);
// var_dump($consumer->getAssignment());
echo "\nWaiting for partition assignment...\n";
$count = 0;
$keys = [];
if ($writeToDisk && $singleFile) {
$singleFile = fopen(__DIR__.'/consumed_message/'.date('Y-m-d').'.json', 'w+');
fwrite($singlFile, '[');
}
while (true) {
$message = $consumer->consume(10000);
switch ($message->err) {
case RD_KAFKA_RESP_ERR_NO_ERROR:
$payload = rSortByKeys(json_decode($message->payload));
$a = json_decode($message->payload, true);
$filename = $message->key ?? 'null-key-'.$message->offset;
$filePath = $messageDir.'/'.$filename.'.json';
if ($writeToDisk) {
if ($singleFile) {
fwrite($singleFile, $message->payload);
} else {
file_put_contents($filePath, $message->payload);
}
}
echo(json_encode([
'len' => $message->len,
'topic_name' => $message->topic_name,
'timestamp' => $message->timestamp,
'partition' => $message->partition,
'payload' => $payload,
'key' => $message->key,
'offset' => $message->offset,
], JSON_PRETTY_PRINT));
if (!isset($keys[$message->key])) {
$keys[$message->key] = [];
}
// $keys[$message->key][] = json_encode($payload, JSON_PRETTY_PRINT);
$count++;
if ($limit && $count > $limit) {
echo "Reached limit $limit";
break 2;
}
break;
case RD_KAFKA_RESP_ERR__PARTITION_EOF:
echo "\n\n**No more messages; will wait for more...\n\n";
break;
// echo "\n\nFound all messages. Closing for now.\n\n";
// break 2;
case RD_KAFKA_RESP_ERR__TIMED_OUT:
echo "**Timed out\n";
// echo "Timed out\n";
break;
case RD_KAFKA_RESP_ERR__FAIL:
echo "**Failed to communicate with broker\n";
break;
case RD_KAFKA_RESP_ERR__BAD_MSG:
echo "**Bad message format\n";
break;
case RD_KAFKA_RESP_ERR__RESOLVE:
echo "**Host resolution failure";
break;
case RD_KAFKA_RESP_ERR__UNKNOWN_TOPIC:
echo "**unknown topic\n";
break;
case RD_KAFKA_RESP_ERR_INVALID_GROUP_ID:
echo "**invalid group id\n";
break;
case RD_KAFKA_RESP_ERR_GROUP_AUTHORIZATION_FAILED:
echo "**group auth failed\n";
break;
default:
echo "**Unknown Error: ".$message->err."\n";
break;
}
}
if ($writeToDisk && $singleFile) {
$singleFile = fopen(__DIR__.'/consumed_message/'.date('Y-m-d').'.json', 'w+');
fwrite($singlFile, ']');
}
echo count($keys)." keys that have the multple messages\n";
foreach ($keys as $key => $payloads) {
if (count($payloads) > 1) {
echo $key." has ".count($payloads)." messages.\n";
for ($i=0; $i < count($payloads); $i++) {
if ($i == 0) {
continue;
}
$diff = xdiff_string_diff($payloads[($i-1)], $payloads[$i]);
echo(($diff) ? $diff : 'NO DIFFERENCE')."\n";
}
echo "-------\n";
}
}