mirror of
https://github.com/appwrite/appwrite.git
synced 2026-11-18 20:36:03 +00:00
Add coroutine concurrency to queue workers
Upgrade utopia-php/queue to 0.19.0 and utopia-php/platform to 1.0.0-rc4, enabling workers to consume multiple jobs concurrently via Swoole coroutines within a single worker process. - Workers pass a maxCoroutines option to the queue adapter, configured via the new _APP_WORKER_MAX_COROUTINES env var (default 1) - Per-message DI resources now live in a coroutine-scoped context container (Server::context()) instead of the shared container - Consumer Redis connections are wrapped in the Locking decorator so concurrent coroutines can safely share one connection - Add a functions worker load test: 30 async 1s executions drain in 31.1s at 1 coroutine, 10.4s at 5, 7.3s at 10 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
3927d52be1
commit
2191a332ef
8 files changed
+163
-21
No files matched your search
@@ -150,3 +150,5 @@ _APP_POOL_ADAPTER=stack
|
||||
_APP_WORKER_SCREENSHOTS_ROUTER=http://appwrite
|
||||
_TESTS_OAUTH2_GITHUB_CLIENT_ID=
|
||||
_TESTS_OAUTH2_GITHUB_CLIENT_SECRET=
|
||||
|
||||
_APP_WORKER_MAX_COROUTINES=3
|
||||
@@ -364,11 +364,17 @@ $register->set('pools', function () {
|
||||
default => null
|
||||
};
|
||||
case 'publisher':
|
||||
case 'consumer':
|
||||
return match ($dsn->getScheme()) {
|
||||
'redis' => new Queue\Broker\Redis(new Queue\Connection\Redis($dsn->getHost(), $dsn->getPort())),
|
||||
default => null
|
||||
};
|
||||
case 'consumer':
|
||||
// Consumers are shared by multiple coroutines within a worker
|
||||
// process, so serialize commands on the single connection.
|
||||
return match ($dsn->getScheme()) {
|
||||
'redis' => new Queue\Broker\Redis(new Queue\Connection\Locking(new Queue\Connection\Redis($dsn->getHost(), $dsn->getPort()))),
|
||||
default => null
|
||||
};
|
||||
case 'cache':
|
||||
$adapter = match ($dsn->getScheme()) {
|
||||
'redis' => new RedisCache($resource()),
|
||||
|
||||
+6
-4
@@ -86,14 +86,16 @@ $pools = $container->get('pools');
|
||||
$adapter = new Swoole(
|
||||
$pools->get('consumer')->pop()->getResource(),
|
||||
System::getEnv('_APP_WORKERS_NUM', 1),
|
||||
$queueName
|
||||
$queueName,
|
||||
maxCoroutines: (int) System::getEnv('_APP_WORKER_MAX_COROUTINES', 1),
|
||||
resources: $container,
|
||||
);
|
||||
|
||||
$worker = new Server($adapter, $container);
|
||||
$worker = new Server($adapter);
|
||||
|
||||
try {
|
||||
$worker->init()->action(function () use ($worker, $registerWorkerMessageResources, $queueName) {
|
||||
$registerWorkerMessageResources($worker->getContainer());
|
||||
$registerWorkerMessageResources($worker->context());
|
||||
Span::init("worker.{$queueName}");
|
||||
});
|
||||
|
||||
@@ -103,7 +105,7 @@ try {
|
||||
|
||||
$container->set('bus', function ($register) use ($worker) {
|
||||
return $register->get('bus')->setResolver(
|
||||
fn (string $name) => $worker->getContainer()->get($name)
|
||||
fn (string $name) => $worker->context()->get($name)
|
||||
);
|
||||
}, ['register']);
|
||||
|
||||
|
||||
+2
-2
@@ -76,11 +76,11 @@
|
||||
"utopia-php/logger": "0.8.*",
|
||||
"utopia-php/messaging": "1.0.*",
|
||||
"utopia-php/migration": "1.*",
|
||||
"utopia-php/platform": "1.0.0-rc3",
|
||||
"utopia-php/platform": "1.0.0-rc4",
|
||||
"utopia-php/pools": "1.*",
|
||||
"utopia-php/span": "1.1.*",
|
||||
"utopia-php/preloader": "0.2.*",
|
||||
"utopia-php/queue": "0.18.*",
|
||||
"utopia-php/queue": "0.19.*",
|
||||
"utopia-php/servers": "0.4.*",
|
||||
"utopia-php/registry": "0.5.*",
|
||||
"utopia-php/storage": "2.*",
|
||||
|
||||
Generated
+14
-14
@@ -4,7 +4,7 @@
|
||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "551924e300a646e6f433ea0a0f3d6c9f",
|
||||
"content-hash": "0c23c30bba86ad84377d4d1abce0992a",
|
||||
"packages": [
|
||||
{
|
||||
"name": "adhocore/jwt",
|
||||
@@ -4773,16 +4773,16 @@
|
||||
},
|
||||
{
|
||||
"name": "utopia-php/platform",
|
||||
"version": "1.0.0-rc3",
|
||||
"version": "1.0.0-rc4",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/utopia-php/platform.git",
|
||||
"reference": "674fd3cea6d1f8db660b01c5be89e8333dfb6de9"
|
||||
"reference": "455c84f8d1cdaec6fc2c39587a78aa5ef263ddd0"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/utopia-php/platform/zipball/674fd3cea6d1f8db660b01c5be89e8333dfb6de9",
|
||||
"reference": "674fd3cea6d1f8db660b01c5be89e8333dfb6de9",
|
||||
"url": "https://api.github.com/repos/utopia-php/platform/zipball/455c84f8d1cdaec6fc2c39587a78aa5ef263ddd0",
|
||||
"reference": "455c84f8d1cdaec6fc2c39587a78aa5ef263ddd0",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
@@ -4791,7 +4791,7 @@
|
||||
"php": ">=8.3",
|
||||
"utopia-php/cli": "0.23.*",
|
||||
"utopia-php/http": "^2.0@RC",
|
||||
"utopia-php/queue": "0.18.*",
|
||||
"utopia-php/queue": "^0.19",
|
||||
"utopia-php/servers": "0.4.*"
|
||||
},
|
||||
"require-dev": {
|
||||
@@ -4818,9 +4818,9 @@
|
||||
],
|
||||
"support": {
|
||||
"issues": "https://github.com/utopia-php/platform/issues",
|
||||
"source": "https://github.com/utopia-php/platform/tree/1.0.0-rc3"
|
||||
"source": "https://github.com/utopia-php/platform/tree/1.0.0-rc4"
|
||||
},
|
||||
"time": "2026-05-25T11:48:18+00:00"
|
||||
"time": "2026-06-02T20:19:26+00:00"
|
||||
},
|
||||
{
|
||||
"name": "utopia-php/pools",
|
||||
@@ -4976,16 +4976,16 @@
|
||||
},
|
||||
{
|
||||
"name": "utopia-php/queue",
|
||||
"version": "0.18.6",
|
||||
"version": "0.19.0",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/utopia-php/queue.git",
|
||||
"reference": "954cd3788b37697b83fd2757adcd93422f21a846"
|
||||
"reference": "cd4248af69006b2568c3486a675f27cb21191859"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/utopia-php/queue/zipball/954cd3788b37697b83fd2757adcd93422f21a846",
|
||||
"reference": "954cd3788b37697b83fd2757adcd93422f21a846",
|
||||
"url": "https://api.github.com/repos/utopia-php/queue/zipball/cd4248af69006b2568c3486a675f27cb21191859",
|
||||
"reference": "cd4248af69006b2568c3486a675f27cb21191859",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
@@ -5037,9 +5037,9 @@
|
||||
],
|
||||
"support": {
|
||||
"issues": "https://github.com/utopia-php/queue/issues",
|
||||
"source": "https://github.com/utopia-php/queue/tree/0.18.6"
|
||||
"source": "https://github.com/utopia-php/queue/tree/0.19.0"
|
||||
},
|
||||
"time": "2026-06-01T19:47:57+00:00"
|
||||
"time": "2026-06-02T20:12:18+00:00"
|
||||
},
|
||||
{
|
||||
"name": "utopia-php/registry",
|
||||
|
||||
@@ -350,6 +350,7 @@ services:
|
||||
environment:
|
||||
- _APP_ENV
|
||||
- _APP_WORKER_PER_CORE
|
||||
- _APP_WORKER_MAX_COROUTINES
|
||||
- _APP_POOL_ADAPTER
|
||||
- _APP_OPENSSL_KEY_V1
|
||||
- _APP_REDIS_HOST
|
||||
@@ -737,6 +738,7 @@ services:
|
||||
environment:
|
||||
- _APP_ENV
|
||||
- _APP_WORKER_PER_CORE
|
||||
- _APP_WORKER_MAX_COROUTINES
|
||||
- _APP_POOL_ADAPTER
|
||||
- _APP_OPENSSL_KEY_V1
|
||||
- _APP_DOMAIN
|
||||
|
||||
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\E2E\Services\Functions;
|
||||
|
||||
use Tests\E2E\Scopes\ProjectCustom;
|
||||
use Tests\E2E\Scopes\Scope;
|
||||
use Tests\E2E\Scopes\SideServer;
|
||||
use Utopia\Database\Helpers\ID;
|
||||
use Utopia\Database\Query;
|
||||
|
||||
/**
|
||||
* Load test for the functions worker (PoC, not part of CI).
|
||||
*
|
||||
* Deploys a function that sleeps ~1s, fires N async executions and measures
|
||||
* the wall time until the queue drains. Run with different
|
||||
* _APP_WORKER_MAX_COROUTINES values on appwrite-worker-functions to compare
|
||||
* coroutine concurrency throughput.
|
||||
*/
|
||||
class FunctionsLoadTest extends Scope
|
||||
{
|
||||
use FunctionsBase;
|
||||
use ProjectCustom;
|
||||
use SideServer;
|
||||
|
||||
private const EXECUTIONS = 30;
|
||||
private const DRAIN_TIMEOUT_SECONDS = 180;
|
||||
|
||||
public function testAsyncExecutionThroughput(): void
|
||||
{
|
||||
$functionId = $this->setupFunction([
|
||||
'functionId' => ID::unique(),
|
||||
'name' => 'Load Test Sleep',
|
||||
'runtime' => 'node-22',
|
||||
'entrypoint' => 'index.js',
|
||||
// Must exceed worst-case queue drain time: executions waiting in the
|
||||
// queue longer than the function timeout are reported as failed.
|
||||
'timeout' => 300,
|
||||
]);
|
||||
|
||||
$this->setupDeployment($functionId, [
|
||||
'code' => $this->packageFunction('sleep'),
|
||||
'activate' => true,
|
||||
]);
|
||||
|
||||
// Warm up the runtime so cold start does not skew the measurement.
|
||||
$execution = $this->createExecution($functionId, ['async' => false]);
|
||||
$this->assertEquals(201, $execution['headers']['status-code']);
|
||||
$this->assertEquals('completed', $execution['body']['status']);
|
||||
|
||||
// Fire N async executions.
|
||||
$start = \microtime(true);
|
||||
|
||||
for ($i = 0; $i < self::EXECUTIONS; $i++) {
|
||||
$execution = $this->createExecution($functionId, ['async' => true]);
|
||||
$this->assertEquals(202, $execution['headers']['status-code']);
|
||||
}
|
||||
|
||||
$enqueued = \microtime(true) - $start;
|
||||
|
||||
// Poll until all executions reach a terminal state.
|
||||
// The warmup execution is already completed, so expect N + 1.
|
||||
$expected = self::EXECUTIONS + 1;
|
||||
$completed = 0;
|
||||
$failed = 0;
|
||||
$drained = null;
|
||||
|
||||
while (\microtime(true) - $start < self::DRAIN_TIMEOUT_SECONDS) {
|
||||
$completed = $this->countExecutions($functionId, 'completed');
|
||||
$failed = $this->countExecutions($functionId, 'failed');
|
||||
|
||||
if ($completed + $failed >= $expected) {
|
||||
$drained = \microtime(true) - $start;
|
||||
break;
|
||||
}
|
||||
|
||||
\usleep(250_000);
|
||||
}
|
||||
|
||||
$this->assertNotNull($drained, "Queue did not drain in " . self::DRAIN_TIMEOUT_SECONDS . "s ({$completed} completed, {$failed} failed)");
|
||||
|
||||
if ($failed > 0) {
|
||||
$samples = $this->listExecutions($functionId, [
|
||||
'queries' => [
|
||||
Query::equal('status', ['failed'])->toString(),
|
||||
Query::limit(3)->toString(),
|
||||
],
|
||||
]);
|
||||
|
||||
$details = \array_map(fn ($e) => [
|
||||
'responseStatusCode' => $e['responseStatusCode'] ?? null,
|
||||
'errors' => $e['errors'] ?? null,
|
||||
'logs' => $e['logs'] ?? null,
|
||||
'duration' => $e['duration'] ?? null,
|
||||
], $samples['body']['executions'] ?? []);
|
||||
|
||||
$this->fail("{$failed} executions failed. Samples: " . \json_encode($details, JSON_PRETTY_PRINT));
|
||||
}
|
||||
$this->assertEquals($expected, $completed);
|
||||
|
||||
$throughput = self::EXECUTIONS / $drained;
|
||||
|
||||
\fwrite(STDERR, \sprintf(
|
||||
"\n[load] executions=%d enqueue=%.2fs drain=%.2fs throughput=%.2f exec/s\n",
|
||||
self::EXECUTIONS,
|
||||
$enqueued,
|
||||
$drained,
|
||||
$throughput,
|
||||
));
|
||||
|
||||
$this->cleanupFunction($functionId);
|
||||
}
|
||||
|
||||
private function countExecutions(string $functionId, string $status): int
|
||||
{
|
||||
$executions = $this->listExecutions($functionId, [
|
||||
'queries' => [
|
||||
Query::equal('status', [$status])->toString(),
|
||||
Query::limit(1)->toString(),
|
||||
],
|
||||
]);
|
||||
|
||||
$this->assertEquals(200, $executions['headers']['status-code']);
|
||||
|
||||
return $executions['body']['total'];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
module.exports = async (context) => {
|
||||
await new Promise((resolve) => setTimeout(resolve, 1000));
|
||||
return context.res.send('OK');
|
||||
};
|
||||
Reference in new issue
Block a user