-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathKafkaProducer.php
288 lines (261 loc) · 8.25 KB
/
KafkaProducer.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
<?php
declare(strict_types=1);
namespace PhpKafka\Producer;
use PhpKafka\Exception\KafkaProducerTransactionAbortException;
use PhpKafka\Exception\KafkaProducerTransactionFatalException;
use PhpKafka\Exception\KafkaProducerTransactionRetryException;
use PhpKafka\Message\KafkaProducerMessageInterface;
use PhpKafka\Message\Encoder\EncoderInterface;
use PhpKafka\Configuration\KafkaConfiguration;
use SimpleKafkaClient\Producer as SkcProducer;
use SimpleKafkaClient\ProducerTopic as SkcProducerTopic;
use SimpleKafkaClient\Metadata\Topic as SkcMetadataTopic;
use SimpleKafkaClient\Exception as SkcException;
use SimpleKafkaClient\KafkaErrorException as SkcErrorException;
final class KafkaProducer implements KafkaProducerInterface
{
/**
* @var SkcProducer
*/
protected $producer;
/**
* @var KafkaConfiguration
*/
protected $kafkaConfiguration;
/**
* @var array|SkcProducerTopic[]
*/
protected $producerTopics = [];
/**
* @var EncoderInterface
*/
protected $encoder;
/**
* @var bool
*/
private $transactionInitialized = false;
/**
* KafkaProducer constructor.
* @param SkcProducer $producer
* @param KafkaConfiguration $kafkaConfiguration
* @param EncoderInterface $encoder
*/
public function __construct(
SkcProducer $producer,
KafkaConfiguration $kafkaConfiguration,
EncoderInterface $encoder
) {
$this->producer = $producer;
$this->kafkaConfiguration = $kafkaConfiguration;
$this->encoder = $encoder;
}
/**
* Produces a message to the topic and partition defined in the message
* If a schema name was given, the message body will be avro serialized.
*
* @param KafkaProducerMessageInterface $message
* @param boolean $autoPoll
* @param integer $pollTimeoutMs
* @return void
*/
public function produce(KafkaProducerMessageInterface $message, bool $autoPoll = true, int $pollTimeoutMs = 0): void
{
$message = $this->encoder->encode($message);
$topicProducer = $this->getProducerTopicForTopic($message->getTopicName());
$topicProducer->producev(
$message->getPartition(),
RD_KAFKA_MSG_F_BLOCK,
$message->getBody(),
$message->getKey(),
$message->getHeaders()
);
if (true === $autoPoll) {
$this->producer->poll($pollTimeoutMs);
}
}
/**
* Produces a message to the topic and partition defined in the message
* If a schema name was given, the message body will be avro serialized.
* Wait for an event to arrive before continuing (blocking)
*
* @param KafkaProducerMessageInterface $message
* @return void
*/
public function syncProduce(KafkaProducerMessageInterface $message): void
{
$this->produce($message, true, -1);
}
/**
* Poll for producer event, pass 0 for non-blocking, pass -1 to block until an event arrives
*
* @param integer $timeoutMs
* @return void
*/
public function poll(int $timeoutMs = 0): void
{
$this->producer->poll($timeoutMs);
}
/**
* Poll for producer events until the number of $queueSize events remain
*
* @param integer $timeoutMs
* @param integer $queueSize
* @return void
*/
public function pollUntilQueueSizeReached(int $timeoutMs = 0, int $queueSize = 0): void
{
while ($this->producer->getOutQLen() > $queueSize) {
$this->producer->poll($timeoutMs);
}
}
/**
* Purge producer messages that are in flight
*
* @param integer $purgeFlags
* @return integer
*/
public function purge(int $purgeFlags): int
{
return $this->producer->purge($purgeFlags);
}
/**
* Wait until all outstanding produce requests are completed
*
* @param integer $timeoutMs
* @return integer
*/
public function flush(int $timeoutMs): int
{
return $this->producer->flush($timeoutMs);
}
/**
* Queries the broker for metadata on a certain topic
*
* @param string $topicName
* @param integer $timeoutMs
* @return SkcMetadataTopic
* @throws SkcException
*/
public function getMetadataForTopic(string $topicName, int $timeoutMs = 10000): SkcMetadataTopic
{
$topic = $this->producer->getTopicHandle($topicName);
return $this->producer
->getMetadata(
false,
$timeoutMs,
$topic
)
->getTopics()
->current();
}
/**
* Start a producer transaction
*
* @param int $timeoutMs
* @return void
*
* @throws KafkaProducerTransactionAbortException
* @throws KafkaProducerTransactionFatalException
* @throws KafkaProducerTransactionRetryException
*/
public function beginTransaction(int $timeoutMs): void
{
try {
if (false === $this->transactionInitialized) {
$this->producer->initTransactions($timeoutMs);
$this->transactionInitialized = true;
}
$this->producer->beginTransaction();
} catch (SkcErrorException $e) {
$this->handleTransactionError($e);
}
}
/**
* Commit the current producer transaction
*
* @param int $timeoutMs
* @return void
*
* @throws KafkaProducerTransactionAbortException
* @throws KafkaProducerTransactionFatalException
* @throws KafkaProducerTransactionRetryException
*/
public function commitTransaction(int $timeoutMs): void
{
try {
$this->producer->commitTransaction($timeoutMs);
} catch (SkcErrorException $e) {
$this->handleTransactionError($e);
}
}
/**
* Abort the current producer transaction
*
* @param int $timeoutMs
* @return void
*
* @throws KafkaProducerTransactionAbortException
* @throws KafkaProducerTransactionFatalException
* @throws KafkaProducerTransactionRetryException
*/
public function abortTransaction(int $timeoutMs): void
{
try {
$this->producer->abortTransaction($timeoutMs);
} catch (SkcErrorException $e) {
$this->handleTransactionError($e);
}
}
/**
* @param string $topic
* @return SkcProducerTopic
*/
private function getProducerTopicForTopic(string $topic): SkcProducerTopic
{
if (!isset($this->producerTopics[$topic])) {
$this->producerTopics[$topic] = $this->producer->getTopicHandle($topic);
}
return $this->producerTopics[$topic];
}
/**
* @param SkcErrorException $e
*
* @throws KafkaProducerTransactionAbortException
* @throws KafkaProducerTransactionFatalException
* @throws KafkaProducerTransactionRetryException
*/
private function handleTransactionError(SkcErrorException $e): void
{
if (true === $e->isRetriable()) {
throw new KafkaProducerTransactionRetryException(
sprintf(
KafkaProducerTransactionRetryException::RETRIABLE_TRANSACTION_EXCEPTION_MESSAGE,
$e->getMessage()
),
$e->getCode(),
$e
);
} elseif (true === $e->transactionRequiresAbort()) {
throw new KafkaProducerTransactionAbortException(
sprintf(
KafkaProducerTransactionAbortException::TRANSACTION_REQUIRES_ABORT_EXCEPTION_MESSAGE,
$e->getMessage()
),
$e->getCode(),
$e
);
} else {
$this->transactionInitialized = false;
// according to librdkafka documentation, everything that is not retriable, abortable or fatal is fatal
// fatal errors (so stated), need the producer to be destroyed
throw new KafkaProducerTransactionFatalException(
sprintf(
KafkaProducerTransactionFatalException::FATAL_TRANSACTION_EXCEPTION_MESSAGE,
$e->getMessage()
),
$e->getCode(),
$e
);
}
}
}