From 1d9323d8ad7e851f1a7c042ee13eebc46f373cca Mon Sep 17 00:00:00 2001 From: Maksim Kotlyar Date: Thu, 30 Nov 2017 18:08:52 +0200 Subject: [PATCH 1/5] [dbal] Store UUIDs as binary data. Improves performance --- pkg/dbal/DbalContext.php | 9 ++++++--- pkg/dbal/DbalProducer.php | 11 ++++------- pkg/dbal/composer.json | 3 ++- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/pkg/dbal/DbalContext.php b/pkg/dbal/DbalContext.php index 0570a4463..d472e2ff1 100644 --- a/pkg/dbal/DbalContext.php +++ b/pkg/dbal/DbalContext.php @@ -170,12 +170,15 @@ public function createDataBaseTable() return; } + $table = new Table($this->getTableName()); + if ($this->getDbalConnection()->getDatabasePlatform()->hasNativeGuidType()) { - throw new \LogicException('The platform does not support UUIDs natively'); + $table->addColumn('id', 'guid'); + } else { + $table->addColumn('id', 'binary', ['length' => 16]); } - $table = new Table($this->getTableName()); - $table->addColumn('id', 'guid'); + $table->addColumn('human_id', 'string', ['length' => 36]); $table->addColumn('published_at', 'bigint'); $table->addColumn('body', 'text', ['notnull' => false]); $table->addColumn('headers', 'text', ['notnull' => false]); diff --git a/pkg/dbal/DbalProducer.php b/pkg/dbal/DbalProducer.php index c8f58bc20..6da886d13 100644 --- a/pkg/dbal/DbalProducer.php +++ b/pkg/dbal/DbalProducer.php @@ -10,6 +10,7 @@ use Interop\Queue\PsrDestination; use Interop\Queue\PsrMessage; use Interop\Queue\PsrProducer; +use Ramsey\Uuid\Uuid; class DbalProducer implements PsrProducer { @@ -74,15 +75,11 @@ public function send(PsrDestination $destination, PsrMessage $message) )); } - $sql = 'SELECT '.$this->context->getDbalConnection()->getDatabasePlatform()->getGuidExpression(); - $uuid = $this->context->getDbalConnection()->query($sql)->fetchColumn(0); - - if (empty($uuid)) { - throw new \LogicException('The generated uuid is empty'); - } + $uuid = Uuid::uuid4(); $dbalMessage = [ - 'id' => $uuid, + 'id' => $uuid->getBytes(), + 'human_id' => $uuid->toString(), 'published_at' => (int) microtime(true) * 10000, 'body' => $body, 'headers' => JSON::encode($message->getHeaders()), diff --git a/pkg/dbal/composer.json b/pkg/dbal/composer.json index 15197f7f4..a37e646a2 100644 --- a/pkg/dbal/composer.json +++ b/pkg/dbal/composer.json @@ -8,7 +8,8 @@ "require": { "php": ">=5.6", "queue-interop/queue-interop": "^0.6@dev", - "doctrine/dbal": "~2.5" + "doctrine/dbal": "~2.5", + "ramsey/uuid": "^3" }, "require-dev": { "phpunit/phpunit": "~5.4.0", From 3a80860dd54c9e69e0aa84c2457955dff2c189a1 Mon Sep 17 00:00:00 2001 From: Maksim Kotlyar Date: Thu, 30 Nov 2017 18:14:14 +0200 Subject: [PATCH 2/5] [dbal] use Type::XXX consts. --- pkg/dbal/DbalConsumer.php | 2 +- pkg/dbal/DbalContext.php | 23 ++++++++++++----------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/pkg/dbal/DbalConsumer.php b/pkg/dbal/DbalConsumer.php index f898e567f..6721b53b3 100644 --- a/pkg/dbal/DbalConsumer.php +++ b/pkg/dbal/DbalConsumer.php @@ -178,7 +178,7 @@ protected function receiveMessage() // remove message $affectedRows = $this->dbal->delete($this->context->getTableName(), ['id' => $dbalMessage['id']], [ - 'id' => Type::INTEGER, + 'id' => Type::BINARY, ]); if (1 !== $affectedRows) { diff --git a/pkg/dbal/DbalContext.php b/pkg/dbal/DbalContext.php index d472e2ff1..41e35f86f 100644 --- a/pkg/dbal/DbalContext.php +++ b/pkg/dbal/DbalContext.php @@ -4,6 +4,7 @@ use Doctrine\DBAL\Connection; use Doctrine\DBAL\Schema\Table; +use Doctrine\DBAL\Types\Type; use Interop\Queue\InvalidDestinationException; use Interop\Queue\PsrContext; use Interop\Queue\PsrDestination; @@ -175,19 +176,19 @@ public function createDataBaseTable() if ($this->getDbalConnection()->getDatabasePlatform()->hasNativeGuidType()) { $table->addColumn('id', 'guid'); } else { - $table->addColumn('id', 'binary', ['length' => 16]); + $table->addColumn('id', Type::BINARY, ['length' => 16]); } - $table->addColumn('human_id', 'string', ['length' => 36]); - $table->addColumn('published_at', 'bigint'); - $table->addColumn('body', 'text', ['notnull' => false]); - $table->addColumn('headers', 'text', ['notnull' => false]); - $table->addColumn('properties', 'text', ['notnull' => false]); - $table->addColumn('redelivered', 'boolean', ['notnull' => false]); - $table->addColumn('queue', 'string'); - $table->addColumn('priority', 'smallint'); - $table->addColumn('delayed_until', 'integer', ['notnull' => false]); - $table->addColumn('time_to_live', 'integer', ['notnull' => false]); + $table->addColumn('human_id', Type::STRING, ['length' => 36]); + $table->addColumn('published_at', Type::BIGINT); + $table->addColumn('body', Type::TEXT, ['notnull' => false]); + $table->addColumn('headers', Type::TEXT, ['notnull' => false]); + $table->addColumn('properties', Type::TEXT, ['notnull' => false]); + $table->addColumn('redelivered', Type::BOOLEAN, ['notnull' => false]); + $table->addColumn('queue', Type::STRING); + $table->addColumn('priority', Type::SMALLINT); + $table->addColumn('delayed_until', Type::INTEGER, ['notnull' => false]); + $table->addColumn('time_to_live', Type::INTEGER, ['notnull' => false]); $table->setPrimaryKey(['id']); $table->addIndex(['published_at']); From 7802b20151683d9c29c49dca79e1f4912d64081d Mon Sep 17 00:00:00 2001 From: Maksim Kotlyar Date: Fri, 1 Dec 2017 09:50:33 +0200 Subject: [PATCH 3/5] [dbal] store string if dbal supports guid natively. --- pkg/dbal/DbalProducer.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/dbal/DbalProducer.php b/pkg/dbal/DbalProducer.php index 6da886d13..e9929a722 100644 --- a/pkg/dbal/DbalProducer.php +++ b/pkg/dbal/DbalProducer.php @@ -75,10 +75,11 @@ public function send(PsrDestination $destination, PsrMessage $message) )); } + $hasNativeGuid = $this->context->getDbalConnection()->getDatabasePlatform()->hasNativeGuidType(); $uuid = Uuid::uuid4(); $dbalMessage = [ - 'id' => $uuid->getBytes(), + 'id' => $hasNativeGuid ? $uuid->toString() : $uuid->getBytes(), 'human_id' => $uuid->toString(), 'published_at' => (int) microtime(true) * 10000, 'body' => $body, From c01955c010d2101c6337b1b2bdb8b3d40064a12f Mon Sep 17 00:00:00 2001 From: Maksim Kotlyar Date: Mon, 4 Dec 2017 11:38:22 +0200 Subject: [PATCH 4/5] use uuid1 plus time ordered codec. --- pkg/dbal/DbalContext.php | 7 +------ pkg/dbal/DbalProducer.php | 13 ++++++++++--- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/pkg/dbal/DbalContext.php b/pkg/dbal/DbalContext.php index 41e35f86f..a81ef2272 100644 --- a/pkg/dbal/DbalContext.php +++ b/pkg/dbal/DbalContext.php @@ -173,12 +173,7 @@ public function createDataBaseTable() $table = new Table($this->getTableName()); - if ($this->getDbalConnection()->getDatabasePlatform()->hasNativeGuidType()) { - $table->addColumn('id', 'guid'); - } else { - $table->addColumn('id', Type::BINARY, ['length' => 16]); - } - + $table->addColumn('id', Type::BINARY, ['length' => 16, 'fixed' => true]); $table->addColumn('human_id', Type::STRING, ['length' => 36]); $table->addColumn('published_at', Type::BIGINT); $table->addColumn('body', Type::TEXT, ['notnull' => false]); diff --git a/pkg/dbal/DbalProducer.php b/pkg/dbal/DbalProducer.php index e9929a722..89612775b 100644 --- a/pkg/dbal/DbalProducer.php +++ b/pkg/dbal/DbalProducer.php @@ -10,7 +10,9 @@ use Interop\Queue\PsrDestination; use Interop\Queue\PsrMessage; use Interop\Queue\PsrProducer; +use Ramsey\Uuid\Codec\OrderedTimeCodec; use Ramsey\Uuid\Uuid; +use Ramsey\Uuid\UuidFactory; class DbalProducer implements PsrProducer { @@ -34,12 +36,18 @@ class DbalProducer implements PsrProducer */ private $context; + /** + * @var OrderedTimeCodec + */ + private $uuidCodec; + /** * @param DbalContext $context */ public function __construct(DbalContext $context) { $this->context = $context; + $this->uuidCodec = new OrderedTimeCodec((new UuidFactory())->getUuidBuilder()); } /** @@ -75,11 +83,10 @@ public function send(PsrDestination $destination, PsrMessage $message) )); } - $hasNativeGuid = $this->context->getDbalConnection()->getDatabasePlatform()->hasNativeGuidType(); - $uuid = Uuid::uuid4(); + $uuid = Uuid::uuid1(); $dbalMessage = [ - 'id' => $hasNativeGuid ? $uuid->toString() : $uuid->getBytes(), + 'id' => $this->uuidCodec->encodeBinary($uuid), 'human_id' => $uuid->toString(), 'published_at' => (int) microtime(true) * 10000, 'body' => $body, From bf6095dd3d917fe494bfd29c18f266d91612e9bf Mon Sep 17 00:00:00 2001 From: Maksim Kotlyar Date: Mon, 4 Dec 2017 15:02:38 +0200 Subject: [PATCH 5/5] fix --- pkg/dbal/DbalConnectionFactory.php | 7 ++++++- pkg/dbal/DbalContext.php | 12 ++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/pkg/dbal/DbalConnectionFactory.php b/pkg/dbal/DbalConnectionFactory.php index 55164213c..75b86929e 100644 --- a/pkg/dbal/DbalConnectionFactory.php +++ b/pkg/dbal/DbalConnectionFactory.php @@ -45,7 +45,12 @@ public function __construct($config = 'mysql:') throw new \LogicException('The config must be either an array of options, a DSN string or null'); } - $this->config = $config; + $this->config = array_replace_recursive([ + 'connection' => [], + 'table_name' => 'enqueue', + 'polling_interval' => 1000, + 'lazy' => true, + ], $config); } /** diff --git a/pkg/dbal/DbalContext.php b/pkg/dbal/DbalContext.php index a81ef2272..682d11c26 100644 --- a/pkg/dbal/DbalContext.php +++ b/pkg/dbal/DbalContext.php @@ -193,4 +193,16 @@ public function createDataBaseTable() $sm->createTable($table); } + + /** + * @param DbalDestination $queue + */ + public function purgeQueue(DbalDestination $queue) + { + $this->getDbalConnection()->delete( + $this->getTableName(), + ['queue' => $queue->getQueueName()], + ['queue' => Type::STRING] + ); + } }