Skip to content

Compatibility with Phprdkafka 4.0 #959

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 15, 2020
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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ script:
- if [ "$PHPSTAN" = true ] && [ ! -z "${PKG_PHP_CHANGED_FILES}" ]; then docker run --workdir="/mqdev" -v "`pwd`:/mqdev" --rm enqueue/dev:latest php -d memory_limit=1024M bin/phpstan analyse -l 1 -c phpstan.neon -- ${PKG_PHP_CHANGED_FILES[@]} ; fi
- if [ "$UNIT_TESTS" = true ]; then bin/phpunit --exclude-group=functional; fi
- if [ "$FUNCTIONAL_TESTS" = true ]; then bin/test.sh --exclude-group=rdkafka; fi
- if [ "RDKAFKA_TESTS" = true ]; then bin/test.sh --group=rdkafka; fi
- if [ "$RDKAFKA_TESTS" = true ]; then bin/test.sh --group=rdkafka; fi

notifications:
webhooks:
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"doctrine/doctrine-bundle": "~1.2|^2",
"doctrine/mongodb-odm-bundle": "^3.5|^4",
"alcaeus/mongo-php-adapter": "^1.0",
"kwn/php-rdkafka-stubs": "^1.0.2",
"kwn/php-rdkafka-stubs": "^1.0.2 | ^2.0",
"friendsofphp/php-cs-fixer": "^2"
},
"autoload": {
Expand Down
6 changes: 3 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,13 @@ services:
- '2181:2181'

kafka:
image: 'wurstmeister/kafka:0.10.2.1'
image: 'wurstmeister/kafka'
ports:
- '9092:9092'
environment:
KAFKA_ZOOKEEPER_CONNECT: 'zookeeper:2181'
volumes:
- '/var/run/docker.sock:/var/run/docker.sock'
KAFKA_ADVERTISED_HOST_NAME: kafka
KAFKA_ADVERTISED_PORT: 9092

google-pubsub:
image: 'google/cloud-sdk:latest'
Expand Down
1 change: 1 addition & 0 deletions pkg/rdkafka/RdKafkaConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class RdKafkaConnectionFactory implements ConnectionFactory
* 'partitioner' => null, // https://arnaud-lb.github.io/php-rdkafka/phpdoc/rdkafka-topicconf.setpartitioner.html
* 'log_level' => null,
* 'commit_async' => false,
* 'shutdown_timeout' => -1, // https://github.com/arnaud-lb/php-rdkafka#proper-shutdown
* ]
*
* or
Expand Down
41 changes: 23 additions & 18 deletions pkg/rdkafka/RdKafkaContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class RdKafkaContext implements Context
private $conf;

/**
* @var Producer
* @var RdKafkaProducer
*/
private $producer;

Expand All @@ -50,9 +50,6 @@ class RdKafkaContext implements Context
*/
private $rdKafkaConsumers;

/**
* @param array $config
*/
public function __construct(array $config)
{
$this->config = $config;
Expand Down Expand Up @@ -96,7 +93,23 @@ public function createTemporaryQueue(): Queue
*/
public function createProducer(): Producer
{
return new RdKafkaProducer($this->getProducer(), $this->getSerializer());
if (!isset($this->producer)) {
$producer = new VendorProducer($this->getConf());

if (isset($this->config['log_level'])) {
$producer->setLogLevel($this->config['log_level']);
Copy link
Contributor

@nick-zh nick-zh Sep 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is setLogLevel of RdKafka\Producer be aware that it has been deprecated. Just having log_level in RdKafka\Conf is fine ✌️
@Steveb-p i do tend to forget which producer is which in enqueue, forgive me if my assumption is not correct

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nick-zh you're correct, this is directly calling setLogLevel on Kafka Producer instance.

At this point I'm not really going to remove this deprecation, since in general PHP frameworks and applications should only log the deprecation (similarly to the default topic case). I'll address this in a separate PR.

}

$this->producer = new RdKafkaProducer($producer, $this->getSerializer());

// Once created RdKafkaProducer can store messages internally that need to be delivered before PHP shuts
// down. Otherwise, we are bound to lose messages in transit.
// Note that it is generally preferable to call "close" method explicitly before shutdown starts, since
// otherwise we might not have access to some objects, like database connections.
register_shutdown_function([$this->producer, 'flush'], $this->config['shutdown_timeout'] ?? -1);
}

return $this->producer;
}

/**
Expand Down Expand Up @@ -139,6 +152,11 @@ public function close(): void
foreach ($kafkaConsumers as $kafkaConsumer) {
$kafkaConsumer->unsubscribe();
}

// Compatibility with phprdkafka 4.0.
if (isset($this->producer)) {
$this->producer->flush($this->config['shutdown_timeout'] ?? -1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe throw an error or log something if the return value is not RD_KAFKA_RESP_ERR_NO_ERROR, sry not sure what the enqueue convention is for something like that

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As what we discussed: throwing an exception here would change the behavior and make it necessary for wrapping libraries (like enqueue-bundle) to catch it, so I've left it out.

However, I do agree that it is something that this package eventually should do, since otherwise potential delivery errors will be hidden from end user.

}
}

public function createSubscriptionConsumer(): SubscriptionConsumer
Expand All @@ -163,19 +181,6 @@ public static function getLibrdKafkaVersion(): string
return "$major.$minor.$patch";
}

private function getProducer(): VendorProducer
{
if (null === $this->producer) {
$this->producer = new VendorProducer($this->getConf());

if (isset($this->config['log_level'])) {
$this->producer->setLogLevel($this->config['log_level']);
}
}

return $this->producer;
}

private function getConf(): Conf
{
if (null === $this->conf) {
Expand Down
8 changes: 8 additions & 0 deletions pkg/rdkafka/RdKafkaProducer.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,12 @@ public function getTimeToLive(): ?int
{
return null;
}

public function flush(int $timeout): void
{
// Flush method is exposed in phprdkafka 4.0
if (method_exists($this->producer, 'flush')) {
$this->producer->flush($timeout);
}
}
}
11 changes: 7 additions & 4 deletions pkg/rdkafka/Tests/Spec/RdKafkaSendToAndReceiveFromTopicTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@ public function test()

$topic = $this->createTopic($context, uniqid('', true));

$consumer = $context->createConsumer($topic);

$expectedBody = __CLASS__.time();
$producer = $context->createProducer();
$producer->send($topic, $context->createMessage($expectedBody));

// Calling close causes Producer to flush (wait for messages to be delivered to Kafka)
$context->close();

$consumer = $context->createConsumer($topic);

$context->createProducer()->send($topic, $context->createMessage($expectedBody));

Expand All @@ -48,8 +53,6 @@ protected function createContext()

$context = (new RdKafkaConnectionFactory($config))->createContext();

sleep(3);

return $context;
}
}
4 changes: 2 additions & 2 deletions pkg/rdkafka/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
"license": "MIT",
"require": {
"php": "^7.1.3",
"ext-rdkafka": "^3.0.3",
"ext-rdkafka": "^3.0.3|^4.0",
"queue-interop/queue-interop": "^0.8"
},
"require-dev": {
"phpunit/phpunit": "~7.5",
"enqueue/test": "0.10.x-dev",
"enqueue/null": "0.10.x-dev",
"queue-interop/queue-spec": "^0.6",
"kwn/php-rdkafka-stubs": "^1.0.2"
"kwn/php-rdkafka-stubs": "^1.0.2 | ^2.0"
},
"support": {
"email": "[email protected]",
Expand Down