Skip to content

Feat: add support for concurrency manager #22

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

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,6 @@ jobs:
docker compose up -d
sleep 10

# When re-running locally, always do 'docker compose down -v' beforehand
- name: Run Tests
run: docker compose exec tests vendor/bin/phpunit --configuration phpunit.xml
34 changes: 17 additions & 17 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/Queue/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public function __construct(string $queue, Connection $connection, string $names

public function enqueue(array $payload): bool
{
if (!isset($payload['concurrencyKey'])) {
$payload['concurrencyKey'] = \uniqid();
}

$payload = [
'pid' => \uniqid(more_entropy: true),
'queue' => $this->queue,
Expand Down
38 changes: 38 additions & 0 deletions src/Queue/Concurrency/Manager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Utopia\Queue\Concurrency;

use Utopia\Queue\Connection;
use Utopia\Queue\Message;

abstract class Manager
{
public function __construct(protected Connection $adapter, protected int $limit = 1)
{
}

public function canProcessJob(Message $message): bool
{
$key = $this->getConcurrencyKey($message);
$value = $this->adapter->get($key);
if ($value === null) {
$this->adapter->set($key, "0");
$value = 0;
}
return \intval($value) < $this->limit;
}

public function startJob(Message $message): void
{
$key = $this->getConcurrencyKey($message);
$this->adapter->increment($key);
}

public function finishJob(Message $message): void
{
$key = $this->getConcurrencyKey($message);
$this->adapter->decrement($key);
}

abstract public function getConcurrencyKey(Message $message): string;
}
47 changes: 47 additions & 0 deletions src/Queue/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Utopia\CLI\Console;
use Utopia\DI\Container;
use Utopia\DI\Dependency;
use Utopia\Queue\Concurrency\Manager;
use Utopia\Servers\Base;

class Worker extends Base
Expand All @@ -25,6 +26,13 @@ class Worker extends Base
*/
protected static Job $job;

/**
* Concurrency Manager
*
* @var ?Manager
*/
protected ?Manager $concurrencyManager = null;

/**
* Creates an instance of a Queue server.
* @param Adapter $adapter
Expand All @@ -43,6 +51,27 @@ public static function job(): Job
return self::$job;
}

/**
* Register a concurrency manager
*
* @param Manager $manager
*/
public function setConcurrencyManager(Manager $manager): self
{
$this->concurrencyManager = $manager;
return $this;
}

/**
* Get a concurrency manager
*
* @return ?Manager
*/
public function getConcurrencyManager(): ?Manager
{
return $this->concurrencyManager;
}

/**
* Stops the Queue server.
* @return self
Expand Down Expand Up @@ -150,10 +179,23 @@ protected function lifecycle(Job $job, Message $message, array $nextMessage, Con
{
Console::info("[Job] Received Job ({$message->getPid()}).");

$concurrencyManager = $this->getConcurrencyManager();

$groups = $job->getGroups();

$connection->getConnection();

if ($concurrencyManager && !$concurrencyManager->canProcessJob($message)) {
$this->adapter->connection->leftPushArray("{$this->adapter->namespace}.queue.{$this->adapter->queue}", $nextMessage);
Console::info("[Job] Re-queued Job ({$message->getPid()}) due to concurrency limit.");
return $this;
}

/** Increment the concurrency counter */
if ($concurrencyManager) {
$concurrencyManager->startJob($message);
}

/**
* Move Job to Jobs and it's PID to the processing list.
*/
Expand Down Expand Up @@ -257,6 +299,11 @@ protected function lifecycle(Job $job, Message $message, array $nextMessage, Con
}
}
} finally {
/** Decrement the counter so another job can be picked up again */
if ($concurrencyManager) {
$concurrencyManager->finishJob($message);
}

/**
* Remove Job from Processing.
*/
Expand Down
24 changes: 24 additions & 0 deletions tests/Queue/E2E/Adapter/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,28 @@ public function testRetry(): void
$this->assertEquals(2, $client->countFailedJobs());
$this->assertEquals(0, $client->countSuccessfulJobs());
}

public function testConcurrencyManager(): void
{
run(function () {
$client = $this->getClient();
go(function () use ($client) {
$client->resetStats();

for ($i = 0; $i < 10; $i++) {
$this->assertTrue($client->enqueue([
'type' => 'test_sleep',
'id' => $i
]));
}

sleep(1);

$this->assertEquals(10, $client->countTotalJobs());
$this->assertEquals(10, $client->countProcessingJobs());
$this->assertEquals(0, $client->countFailedJobs());
$this->assertEquals(0, $client->countSuccessfulJobs());
});
});
}
}
4 changes: 4 additions & 0 deletions tests/Queue/servers/Swoole/worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use Utopia\DI\Container;
use Utopia\Queue;
use Utopia\Queue\Concurrency\Manager;
use Utopia\Queue\Message;
use Utopia\Queue\Worker;
use Utopia\Servers\Validator;
Expand Down Expand Up @@ -61,10 +62,13 @@ public function isValid(mixed $value): bool
}
}



$container = new Container();
$connection = new Queue\Adapter\Swoole\Redis('redis');
$adapter = new Queue\Adapter\Swoole\Server($connection, 1, 'swoole');
$server = new Queue\Worker($adapter);
$server->setConcurrencyManager(new TestConcurrencyManager($connection, 5));
$server->setContainer($container);

// Server::init()
Expand Down
13 changes: 13 additions & 0 deletions tests/Queue/servers/tests.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

use Utopia\Queue;
use Utopia\Queue\Concurrency\Manager;
use Utopia\Queue\Message;

function handleRequest(Queue\Message $job): void
{
Expand Down Expand Up @@ -48,6 +50,17 @@ function handleRequest(Queue\Message $job): void
case 'test_exception':
assert(false);

// no break
case 'test_sleep':
sleep(5);
break;
}
}

class TestConcurrencyManager extends Manager
{
public function getConcurrencyKey(Message $message): string
{
return $message->getPayload()['concurrencyKey'];
}
}
Loading