Skip to content
This repository was archived by the owner on Nov 24, 2020. It is now read-only.

Commit 05e8dd8

Browse files
authored
Update to rdkafka 4.0.0 (#40)
* add ext-rdkafka 4.0 capabilities
1 parent 0e0f7fa commit 05e8dd8

21 files changed

+301
-77
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ To read more about the functions used in this lib, check out the [documentation]
1414

1515
## Requirements
1616
- php: ^7.1
17-
- ext-rdkafka: ^3.1.2
17+
- ext-rdkafka: ^4.0.0
1818

1919
## Installation
2020
```composer require jobcloud/messaging-lib "~4.0"```

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
],
1818
"require": {
1919
"php": "^7.3",
20-
"ext-rdkafka": "^3.1.2",
20+
"ext-rdkafka": "^4.0.0",
2121
"ext-json": "*",
2222
"flix-tech/avro-serde-php": "^1.3"
2323
},
2424
"require-dev": {
25-
"phpunit/phpunit": "^8.2.3|^7.5.13",
25+
"phpunit/phpunit": "~8.3.5",
2626
"squizlabs/php_codesniffer": "^3.4.2",
2727
"phpstan/phpstan": "^0.11.12",
2828
"php-mock/php-mock-phpunit": "^2.4",

docker/dev/php/Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ ARG HOST_USER_ID
66
ARG HOST_USER
77

88
# PHP: Copy configuration files & remove dist files
9-
COPY dev/php/files/php/ /phpIni
9+
RUN mkdir /phpIni
1010
COPY dev/php/files/bin/ /usr/local/bin/
1111

1212
# SYS: Install required packages
@@ -35,7 +35,7 @@ RUN composer global require hirak/prestissimo
3535

3636
# PHP: Install php extensions
3737
RUN pecl channel-update pecl.php.net && \
38-
pecl install rdkafka xdebug && \
38+
pecl install rdkafka && \
3939
docker-php-ext-install pcntl && \
4040
php-ext-enable rdkafka pcntl
4141

docker/dev/php/files/php/20-xdebug.ini

Lines changed: 0 additions & 1 deletion
This file was deleted.

phpstan.neon

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
parameters:
22
level: 7
33
paths: [ src ]
4-
ignoreErrors:
5-
- '#Class RdKafka\\TopicPartition constructor invoked with 3 parameters, 0 required#'

src/Kafka/Consumer/AbstractKafkaConsumer.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use RdKafka\KafkaConsumer as RdKafkaHighLevelConsumer;
1919
use RdKafka\Metadata\Topic as RdKafkaMetadataTopic;
2020
use RdKafka\Message as RdKafkaMessage;
21+
use RdKafka\TopicPartition as RdKafkaTopicPartition;
2122

2223
abstract class AbstractKafkaConsumer implements KafkaConsumerInterface
2324
{
@@ -136,6 +137,54 @@ public function getMetadataForTopic(RdKafkaConsumerTopic $topic): RdKafkaMetadat
136137
->current();
137138
}
138139

140+
/**
141+
* Get the earliest offset for a certain timestamp for topic partitions
142+
*
143+
* @param array|RdKafkaTopicPartition[] $topicPartitions
144+
* @param integer $timeout
145+
* @return array
146+
*/
147+
public function offsetsForTimes(array $topicPartitions, int $timeout): array
148+
{
149+
return $this->consumer->offsetsForTimes($topicPartitions, $timeout);
150+
}
151+
152+
/**
153+
* Queries the broker for the first offset of a given topic and partition
154+
*
155+
* @param string $topic
156+
* @param integer $partition
157+
* @param integer $timeout
158+
* @return integer
159+
*/
160+
public function getFirstOffsetForTopicPartition(string $topic, int $partition, int $timeout): int
161+
{
162+
$lowOffset = 0;
163+
$highOffset = 0;
164+
165+
$this->consumer->queryWatermarkOffsets($topic, $partition, $lowOffset, $highOffset, $timeout);
166+
167+
return $lowOffset;
168+
}
169+
170+
/**
171+
* Queries the broker for the last offset of a given topic and partition
172+
*
173+
* @param string $topic
174+
* @param integer $partition
175+
* @param integer $timeout
176+
* @return integer
177+
*/
178+
public function getLastOffsetForTopicPartition(string $topic, int $partition, int $timeout): int
179+
{
180+
$lowOffset = 0;
181+
$highOffset = 0;
182+
183+
$this->consumer->queryWatermarkOffsets($topic, $partition, $lowOffset, $highOffset, $timeout);
184+
185+
return $highOffset;
186+
}
187+
139188
/**
140189
* @param RdKafkaMessage $message
141190
* @return KafkaConsumerMessageInterface

src/Kafka/Consumer/KafkaConsumerBuilder.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ final class KafkaConsumerBuilder implements KafkaConsumerBuilderInterface
6363
*/
6464
private $consumeCallback;
6565

66+
/**
67+
* @var callable
68+
*/
69+
private $logCallback;
70+
6671
/**
6772
* @var callable
6873
*/
@@ -247,6 +252,20 @@ public function withConsumeCallback(callable $consumeCallback): KafkaConsumerBui
247252
return $that;
248253
}
249254

255+
/**
256+
* Callback for log related events
257+
*
258+
* @param callable $logCallback
259+
* @return KafkaConsumerBuilderInterface
260+
*/
261+
public function withLogCallback(callable $logCallback): KafkaConsumerBuilderInterface
262+
{
263+
$that = clone $this;
264+
$that->logCallback = $logCallback;
265+
266+
return $that;
267+
}
268+
250269
/**
251270
* Set callback that is being called on offset commits
252271
*
@@ -293,7 +312,6 @@ public function build(): KafkaConsumerInterface
293312

294313
//set additional config
295314
$this->config['group.id'] = $this->consumerGroup;
296-
$this->config['enable.auto.offset.store'] = false;
297315

298316
//create config
299317
$kafkaConfig = new KafkaConfiguration(
@@ -309,6 +327,7 @@ public function build(): KafkaConsumerInterface
309327
//create RdConsumer
310328

311329
if (self::CONSUMER_TYPE_LOW_LEVEL === $this->consumerType) {
330+
$this->config['enable.auto.offset.store'] = false;
312331
if (null !== $this->consumeCallback) {
313332
throw new KafkaConsumerBuilderException(
314333
sprintf(
@@ -328,6 +347,8 @@ public function build(): KafkaConsumerInterface
328347
);
329348
}
330349

350+
$this->config['enable.auto.commit'] = false;
351+
331352
$rdKafkaConsumer = new RdKafkaHighLevelConsumer($kafkaConfig);
332353

333354
return new KafkaHighLevelConsumer($rdKafkaConsumer, $kafkaConfig, $this->decoder);
@@ -349,6 +370,10 @@ private function registerCallbacks(KafkaConfiguration $conf): void
349370
$conf->setConsumeCb($this->consumeCallback);
350371
}
351372

373+
if (null !== $this->logCallback) {
374+
$conf->setLogCb($this->logCallback);
375+
}
376+
352377
if (null !== $this->offsetCommitCallback) {
353378
$conf->setOffsetCommitCb($this->rebalanceCallback);
354379
}

src/Kafka/Consumer/KafkaConsumerBuilderInterface.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,14 @@ public function withOffsetCommitCallback(callable $offsetCommitCallback): self;
124124
*/
125125
public function withDecoder(DecoderInterface $decoder): self;
126126

127+
/**
128+
* Callback for log related events
129+
*
130+
* @param callable $consumeCallback
131+
* @return KafkaConsumerBuilderInterface
132+
*/
133+
public function withLogCallback(callable $consumeCallback): KafkaConsumerBuilderInterface;
134+
127135
/**
128136
* Returns your consumer instance
129137
*

src/Kafka/Consumer/KafkaConsumerInterface.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Jobcloud\Messaging\Kafka\Message\KafkaConsumerMessageInterface;
1010
use RdKafka\Metadata\Topic as RdKafkaMetadataTopic;
1111
use RdKafka\ConsumerTopic as RdKafkaConsumerTopic;
12+
use RdKafka\TopicPartition as RdKafkaTopicPartition;
1213

1314
interface KafkaConsumerInterface extends ConsumerInterface
1415
{
@@ -65,4 +66,33 @@ public function getConfiguration(): array;
6566
* @return RdKafkaMetadataTopic
6667
*/
6768
public function getMetadataForTopic(RdKafkaConsumerTopic $topic): RdKafkaMetadataTopic;
69+
70+
/**
71+
* Get the earliest offset for a certain timestamp for topic partitions
72+
*
73+
* @param array|RdKafkaTopicPartition[] $topicPartitions
74+
* @param integer $timeout
75+
* @return array
76+
*/
77+
public function offsetsForTimes(array $topicPartitions, int $timeout): array;
78+
79+
/**
80+
* Queries the broker for the first offset of a given topic and partition
81+
*
82+
* @param string $topic
83+
* @param integer $partition
84+
* @param integer $timeout
85+
* @return integer
86+
*/
87+
public function getFirstOffsetForTopicPartition(string $topic, int $partition, int $timeout): int;
88+
89+
/**
90+
* Queries the broker for the last offset of a given topic and partition
91+
*
92+
* @param string $topic
93+
* @param integer $partition
94+
* @param integer $timeout
95+
* @return integer
96+
*/
97+
public function getLastOffsetForTopicPartition(string $topic, int $partition, int $timeout): int;
6898
}

src/Kafka/Consumer/KafkaHighLevelConsumer.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,27 @@ public function getCommittedOffsets(array $topicPartitions, int $timeout): array
156156
}
157157
}
158158

159+
/**
160+
* Get current offset positions of the consumer
161+
*
162+
* @param array|RdKafkaTopicPartition[] $topicPartitions
163+
* @return array
164+
*/
165+
public function getOffsetPositions(array $topicPartitions): array
166+
{
167+
return $this->consumer->getOffsetPositions($topicPartitions);
168+
}
169+
170+
/**
171+
* Close the consumer connection
172+
*
173+
* @return void;
174+
*/
175+
public function close(): void
176+
{
177+
$this->consumer->close();
178+
}
179+
159180
/**
160181
* @param integer $timeout
161182
* @return RdKafkaMessage|null

0 commit comments

Comments
 (0)