Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add header support for kafka #955

Merged
merged 6 commits into from
Sep 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions pkg/rdkafka/RdKafkaContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,18 @@ public function purgeQueue(Queue $queue): void
throw PurgeQueueNotSupportedException::providerDoestNotSupportIt();
}

public static function getLibrdKafkaVersion(): string
{
if (!defined('RD_KAFKA_VERSION')) {
throw new \RuntimeException('RD_KAFKA_VERSION constant is not defined. Phprdkafka is probably not installed');
}
$major = (RD_KAFKA_VERSION & 0xFF000000) >> 24;
$minor = (RD_KAFKA_VERSION & 0x00FF0000) >> 16;
$patch = (RD_KAFKA_VERSION & 0x0000FF00) >> 8;

return "$major.$minor.$patch";
}

private function getProducer(): VendorProducer
{
if (null === $this->producer) {
Expand Down
19 changes: 19 additions & 0 deletions pkg/rdkafka/RdKafkaProducer.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,25 @@ public function send(Destination $destination, Message $message): void
$key = $message->getKey() ?: $destination->getKey() ?: null;

$topic = $this->producer->newTopic($destination->getTopicName(), $destination->getConf());

// Note: Topic::producev method exists in phprdkafka > 3.1.0
// Headers in payload are maintained for backwards compatibility with apps that might run on lower phprdkafka version
if (method_exists($topic, 'producev')) {
// Phprdkafka <= 3.1.0 will fail calling `producev` on librdkafka >= 1.0.0 causing segfault
if (version_compare(RdKafkaContext::getLibrdKafkaVersion(), '1.0.0', '>=')
&& version_compare(phpversion('rdkafka'), '3.1.0', '<=')) {
trigger_error(
'Phprdkafka <= 3.1.0 is incompatible with librdkafka 1.0.0 when calling `producev`. '.
'Falling back to `produce` (without message headers) instead.',
E_USER_WARNING
);
} else {
$topic->producev($partition, 0 /* must be 0 */, $payload, $key, $message->getHeaders());

return;
}
}

$topic->produce($partition, 0 /* must be 0 */, $payload, $key);
}

Expand Down
17 changes: 10 additions & 7 deletions pkg/rdkafka/Tests/RdKafkaProducerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,20 @@ public function testThrowIfMessageInvalid()

public function testShouldUseSerializerToEncodeMessageAndPutToExpectedTube()
{
$message = new RdKafkaMessage('theBody', ['foo' => 'fooVal'], ['bar' => 'barVal']);
$messageHeaders = ['bar' => 'barVal'];
$message = new RdKafkaMessage('theBody', ['foo' => 'fooVal'], $messageHeaders);
$message->setKey('key');

$kafkaTopic = $this->createKafkaTopicMock();
$kafkaTopic
->expects($this->once())
->method('produce')
->method('producev')
->with(
RD_KAFKA_PARTITION_UA,
0,
'theSerializedMessage',
'key'
'key',
$messageHeaders
)
;

Expand Down Expand Up @@ -87,7 +89,7 @@ public function testShouldPassNullAsTopicConfigIfNotSetOnTopic()
$kafkaTopic = $this->createKafkaTopicMock();
$kafkaTopic
->expects($this->once())
->method('produce')
->method('producev')
;

$kafkaProducer = $this->createKafkaProducerMock();
Expand Down Expand Up @@ -123,7 +125,7 @@ public function testShouldPassCustomConfAsTopicConfigIfSetOnTopic()
$kafkaTopic = $this->createKafkaTopicMock();
$kafkaTopic
->expects($this->once())
->method('produce')
->method('producev')
;

$kafkaProducer = $this->createKafkaProducerMock();
Expand Down Expand Up @@ -165,13 +167,14 @@ public function testShouldAllowGetPreviouslySetSerializer()

public function testShouldAllowSerializersToSerializeKeys()
{
$message = new RdKafkaMessage('theBody', ['foo' => 'fooVal'], ['bar' => 'barVal']);
$messageHeaders = ['bar' => 'barVal'];
$message = new RdKafkaMessage('theBody', ['foo' => 'fooVal'], $messageHeaders);
$message->setKey('key');

$kafkaTopic = $this->createKafkaTopicMock();
$kafkaTopic
->expects($this->once())
->method('produce')
->method('producev')
->with(
RD_KAFKA_PARTITION_UA,
0,
Expand Down