Skip to content

Commit 5316a97

Browse files
committed
make delivery_id field binary, review indexes.
1 parent 287b0b6 commit 5316a97

4 files changed

Lines changed: 27 additions & 28 deletions

File tree

‎pkg/dbal/DbalConsumer.php‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Interop\Queue\Impl\ConsumerPollingTrait;
1212
use Interop\Queue\Message;
1313
use Interop\Queue\Queue;
14+
use Ramsey\Uuid\Uuid;
1415

1516
class DbalConsumer implements Consumer
1617
{
@@ -138,8 +139,8 @@ private function deleteMessage(string $deliveryId): void
138139

139140
$this->getConnection()->delete(
140141
$this->getContext()->getTableName(),
141-
['delivery_id' => $deliveryId],
142-
['delivery_id' => Type::STRING]
142+
['delivery_id' => Uuid::fromString($deliveryId)->getBytes()],
143+
['delivery_id' => Type::BINARY]
143144
);
144145
}
145146
}

‎pkg/dbal/DbalConsumerHelperTrait.php‎

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@ abstract protected function getConnection(): Connection;
2222
protected function fetchMessage(array $queues, int $redeliveryDelay): ?array
2323
{
2424
$now = time();
25-
$deliveryId = (string) Uuid::uuid1();
25+
$deliveryId = Uuid::uuid1();
2626

2727
$endAt = microtime(true) + 0.2; // add 200ms
2828

2929
$select = $this->getConnection()->createQueryBuilder()
3030
->select('id')
3131
->from($this->getContext()->getTableName())
32-
->andWhere('delivery_id IS NULL')
33-
->andWhere('delayed_until IS NULL OR delayed_until <= :delayedUntil')
3432
->andWhere('queue IN (:queues)')
33+
->andWhere('delayed_until IS NULL OR delayed_until <= :delayedUntil')
34+
->andWhere('delivery_id IS NULL')
3535
->addOrderBy('priority', 'asc')
3636
->addOrderBy('published_at', 'asc')
3737
->setParameter('delayedUntil', $now, ParameterType::INTEGER)
@@ -44,11 +44,11 @@ protected function fetchMessage(array $queues, int $redeliveryDelay): ?array
4444
->set('redeliver_after', ':redeliverAfter')
4545
->andWhere('id = :messageId')
4646
->andWhere('delivery_id IS NULL')
47-
->setParameter('deliveryId', $deliveryId, Type::STRING)
47+
->setParameter('deliveryId', $deliveryId->getBytes(), Type::BINARY)
4848
->setParameter('redeliverAfter', $now + $redeliveryDelay, Type::BIGINT)
4949
;
5050

51-
while (microtime(true) < $endAt) {
51+
while (microtime(true) >= $endAt) {
5252
$result = $select->execute()->fetch();
5353
if (empty($result)) {
5454
return null;
@@ -63,7 +63,7 @@ protected function fetchMessage(array $queues, int $redeliveryDelay): ?array
6363
->select('*')
6464
->from($this->getContext()->getTableName())
6565
->andWhere('delivery_id = :deliveryId')
66-
->setParameter('deliveryId', $deliveryId, Type::STRING)
66+
->setParameter('deliveryId', $deliveryId->getBytes(), Type::BINARY)
6767
->setMaxResults(1)
6868
->execute()
6969
->fetch()
@@ -88,18 +88,19 @@ protected function redeliverMessages(): void
8888
return;
8989
}
9090

91-
$this->getConnection()->createQueryBuilder()
91+
$update = $this->getConnection()->createQueryBuilder()
9292
->update($this->getContext()->getTableName())
9393
->set('delivery_id', ':deliveryId')
9494
->set('redelivered', ':redelivered')
95-
->andWhere('delivery_id IS NOT NULL')
9695
->andWhere('redeliver_after < :now')
97-
->setParameter(':now', (int) time(), Type::BIGINT)
98-
->setParameter('deliveryId', null, Type::STRING)
96+
->andWhere('delivery_id IS NOT NULL')
97+
->setParameter(':now', time(), Type::BIGINT)
98+
->setParameter('deliveryId', null, Type::BINARY)
9999
->setParameter('redelivered', true, Type::BOOLEAN)
100-
->execute()
101100
;
102101

102+
$update->execute();
103+
103104
$this->redeliverMessagesLastExecutedAt = microtime(true);
104105
}
105106

@@ -111,14 +112,17 @@ protected function removeExpiredMessages(): void
111112
return;
112113
}
113114

114-
$this->getConnection()->createQueryBuilder()
115+
$update = $this->getConnection()->createQueryBuilder()
115116
->delete($this->getContext()->getTableName())
116117
->andWhere('(time_to_live IS NOT NULL) AND (time_to_live < :now)')
117-
->andWhere('(redelivered = false OR delivery_id IS NULL)')
118+
->andWhere('delivery_id IS NULL')
119+
->andWhere('redelivered = false')
120+
118121
->setParameter(':now', (int) time(), Type::BIGINT)
119-
->execute()
120122
;
121123

124+
$update->execute();
125+
122126
$this->removeExpiredMessagesLastExecutedAt = microtime(true);
123127
}
124128
}

‎pkg/dbal/DbalContext.php‎

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Interop\Queue\Queue;
1818
use Interop\Queue\SubscriptionConsumer;
1919
use Interop\Queue\Topic;
20+
use Ramsey\Uuid\Uuid;
2021

2122
class DbalContext implements Context
2223
{
@@ -160,7 +161,7 @@ public function convertMessage(array $dbalMessage): DbalMessage
160161
$dbalMessageObj->setPublishedAt((int) $dbalMessage['published_at']);
161162
}
162163
if (isset($dbalMessage['delivery_id'])) {
163-
$dbalMessageObj->setDeliveryId((string) $dbalMessage['delivery_id']);
164+
$dbalMessageObj->setDeliveryId(Uuid::fromBytes($dbalMessage['delivery_id'])->toString());
164165
}
165166
if (isset($dbalMessage['redeliver_after'])) {
166167
$dbalMessageObj->setRedeliverAfter((int) $dbalMessage['redeliver_after']);
@@ -229,20 +230,13 @@ public function createDataBaseTable(): void
229230
$table->addColumn('priority', Type::SMALLINT, ['notnull' => false]);
230231
$table->addColumn('delayed_until', Type::BIGINT, ['notnull' => false]);
231232
$table->addColumn('time_to_live', Type::BIGINT, ['notnull' => false]);
232-
$table->addColumn('delivery_id', Type::STRING, ['notnull' => false]);
233+
$table->addColumn('delivery_id', Type::BINARY, ['length' => 16, 'fixed' => true, 'notnull' => false]);
233234
$table->addColumn('redeliver_after', Type::BIGINT, ['notnull' => false]);
234235

235236
$table->setPrimaryKey(['id']);
236-
$table->addUniqueIndex(['delivery_id']);
237+
$table->addIndex(['priority', 'published_at', 'queue', 'delivery_id', 'delayed_until', 'id']);
237238

238-
// try to select a message index
239-
$table->addIndex(['delivery_id, delayed_until, queue']);
240-
$table->addIndex(['priority', 'published_at']);
241-
242-
// redeliver failed messages
243-
$table->addIndex(['delivery_id', 'redeliver_after']);
244-
245-
// remove expired messages
239+
$table->addIndex(['redeliver_after', 'delivery_id']);
246240
$table->addIndex(['time_to_live', 'delivery_id']);
247241

248242
$sm->createTable($table);

‎pkg/dbal/DbalSubscriptionConsumer.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ public function consume(int $timeout = 0): void
7575
}
7676

7777
$timeout /= 1000;
78+
$now = time();
7879
$redeliveryDelay = $this->getRedeliveryDelay() / 1000; // milliseconds to seconds
7980

8081
$currentQueueNames = [];
@@ -83,7 +84,6 @@ public function consume(int $timeout = 0): void
8384
$currentQueueNames = $queueNames;
8485
}
8586

86-
$now = time();
8787
$this->removeExpiredMessages();
8888
$this->redeliverMessages();
8989

0 commit comments

Comments
 (0)