diff --git a/.env b/.env index aa7438f6bb..61de326ee5 100644 --- a/.env +++ b/.env @@ -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 diff --git a/app/init/registers.php b/app/init/registers.php index 9ea19eee24..881aadd249 100644 --- a/app/init/registers.php +++ b/app/init/registers.php @@ -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()), diff --git a/app/worker.php b/app/worker.php index 169b8e9770..e65655c2de 100644 --- a/app/worker.php +++ b/app/worker.php @@ -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']); diff --git a/composer.json b/composer.json index 1e3ce344b8..b78637ccd9 100644 --- a/composer.json +++ b/composer.json @@ -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.*", diff --git a/composer.lock b/composer.lock index 11711529ba..b456b34ec7 100644 --- a/composer.lock +++ b/composer.lock @@ -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", diff --git a/docker-compose.yml b/docker-compose.yml index ab51ad32c0..dca86166e0 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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 diff --git a/tests/e2e/Services/Functions/FunctionsLoadTest.php b/tests/e2e/Services/Functions/FunctionsLoadTest.php new file mode 100644 index 0000000000..81d861fc6b --- /dev/null +++ b/tests/e2e/Services/Functions/FunctionsLoadTest.php @@ -0,0 +1,126 @@ +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']; + } +} diff --git a/tests/resources/functions/sleep/index.js b/tests/resources/functions/sleep/index.js new file mode 100644 index 0000000000..416bb512ec --- /dev/null +++ b/tests/resources/functions/sleep/index.js @@ -0,0 +1,4 @@ +module.exports = async (context) => { + await new Promise((resolve) => setTimeout(resolve, 1000)); + return context.res.send('OK'); +};