Skip to content

Fix partition's choice for the cases when partition number is zero #1196

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

Merged
merged 2 commits into from
Sep 13, 2021
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
25 changes: 21 additions & 4 deletions pkg/rdkafka/RdKafkaProducer.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function send(Destination $destination, Message $message): void
InvalidDestinationException::assertDestinationInstanceOf($destination, RdKafkaTopic::class);
InvalidMessageException::assertMessageInstanceOf($message, RdKafkaMessage::class);

$partition = $message->getPartition() ?: $destination->getPartition() ?: RD_KAFKA_PARTITION_UA;
$partition = $this->getPartition($destination, $message);
$payload = $this->serializer->toString($message);
$key = $message->getKey() ?: $destination->getKey() ?: null;

Expand All @@ -53,17 +53,17 @@ public function send(Destination $destination, Message $message): void
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
\E_USER_WARNING
);
} else {
$topic->producev($partition, 0 /* must be 0 */, $payload, $key, $message->getHeaders());
$topic->producev($partition, 0 /* must be 0 */ , $payload, $key, $message->getHeaders());
$this->producer->poll(0);

return;
}
}

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

Expand Down Expand Up @@ -122,4 +122,21 @@ public function flush(int $timeout): void
$this->producer->flush($timeout);
}
}

/**
* @param RdKafkaTopic $destination
* @param RdKafkaMessage $message
*/
private function getPartition(Destination $destination, Message $message): int
{
if (null !== $message->getPartition()) {
return $message->getPartition();
}

if (null !== $destination->getPartition()) {
return $destination->getPartition();
}

return \RD_KAFKA_PARTITION_UA;
}
}
98 changes: 98 additions & 0 deletions pkg/rdkafka/Tests/RdKafkaProducerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,104 @@ public function testShouldAllowSerializersToSerializeKeys()
$producer->send(new RdKafkaTopic('theQueueName'), $message);
}

public function testShouldGetPartitionFromMessage(): void
{
$partition = 1;

$kafkaTopic = $this->createKafkaTopicMock();
$kafkaTopic
->expects($this->once())
->method('producev')
->with(
$partition,
0,
'theSerializedMessage',
'theSerializedKey'
)
;

$kafkaProducer = $this->createKafkaProducerMock();
$kafkaProducer
->expects($this->once())
->method('newTopic')
->willReturn($kafkaTopic)
;
$kafkaProducer
->expects($this->once())
->method('poll')
->with(0)
;
$messageHeaders = ['bar' => 'barVal'];
$message = new RdKafkaMessage('theBody', ['foo' => 'fooVal'], $messageHeaders);
$message->setKey('key');
$message->setPartition($partition);

$serializer = $this->createSerializerMock();
$serializer
->expects($this->once())
->method('toString')
->willReturnCallback(function () use ($message) {
$message->setKey('theSerializedKey');

return 'theSerializedMessage';
})
;

$destination = new RdKafkaTopic('theQueueName');

$producer = new RdKafkaProducer($kafkaProducer, $serializer);
$producer->send($destination, $message);
}

public function testShouldGetPartitionFromDestination(): void
{
$partition = 2;

$kafkaTopic = $this->createKafkaTopicMock();
$kafkaTopic
->expects($this->once())
->method('producev')
->with(
$partition,
0,
'theSerializedMessage',
'theSerializedKey'
)
;

$kafkaProducer = $this->createKafkaProducerMock();
$kafkaProducer
->expects($this->once())
->method('newTopic')
->willReturn($kafkaTopic)
;
$kafkaProducer
->expects($this->once())
->method('poll')
->with(0)
;
$messageHeaders = ['bar' => 'barVal'];
$message = new RdKafkaMessage('theBody', ['foo' => 'fooVal'], $messageHeaders);
$message->setKey('key');

$serializer = $this->createSerializerMock();
$serializer
->expects($this->once())
->method('toString')
->willReturnCallback(function () use ($message) {
$message->setKey('theSerializedKey');

return 'theSerializedMessage';
})
;

$destination = new RdKafkaTopic('theQueueName');
$destination->setPartition($partition);

$producer = new RdKafkaProducer($kafkaProducer, $serializer);
$producer->send($destination, $message);
}

/**
* @return \PHPUnit\Framework\MockObject\MockObject|ProducerTopic
*/
Expand Down