Remove platform DB lock wrapper

This commit is contained in:
Prem Palanisamy committed 2026-07-03 08:56:32 +01:00
1 parent 5ee2c46d75
commit 016f47c456
6 files changed
+59 -315

No files matched your search

+23 -15
View File
@@ -12,7 +12,7 @@ use Appwrite\Event\Message\Delete as DeleteMessage;
use Appwrite\Event\Publisher\Certificate;
use Appwrite\Event\Publisher\Delete as DeletePublisher;
use Appwrite\Extend\Exception as AppwriteException;
use Appwrite\Locking\PlatformDBLock;
use Appwrite\Locking\Lock;
use Appwrite\Network\Cors;
use Appwrite\Platform\Appwrite;
use Appwrite\SDK\AuthType;
@@ -77,7 +77,7 @@ use Utopia\Validator\Text;
Config::setParam('cookieSamesite', Response::COOKIE_SAMESITE_NONE);
function router(Http $utopia, Database $dbForPlatform, callable $getProjectDB, SwooleRequest $swooleRequest, Request $request, Response $response, Log $log, Event $queueForEvents, Bus $bus, Executor $executor, Reader $geodb, callable $isResourceBlocked, array $platform, string $previewHostname, Authorization $authorization, ?Key $apiKey, DeletePublisher $publisherForDeletes, int $executionsRetentionCount, PlatformDBLock $platformDBLock)
function router(Http $utopia, Database $dbForPlatform, callable $getProjectDB, SwooleRequest $swooleRequest, Request $request, Response $response, Log $log, Event $queueForEvents, Bus $bus, Executor $executor, Reader $geodb, callable $isResourceBlocked, array $platform, string $previewHostname, Authorization $authorization, ?Key $apiKey, DeletePublisher $publisherForDeletes, int $executionsRetentionCount, Lock $lock)
{
$host = $request->getHostname();
if (!empty($previewHostname)) {
@@ -146,7 +146,15 @@ function router(Http $utopia, Database $dbForPlatform, callable $getProjectDB, S
if (!$project->isEmpty() && $project->getId() !== 'console') {
$accessedAt = $project->getAttribute('accessedAt', 0);
if (DateTime::formatTz(DateTime::addSeconds(new \DateTime(), -APP_PROJECT_ACCESS)) > $accessedAt) {
$platformDBLock->tryUpdateAttribute('projects', $project->getId(), 'accessedAt', DateTime::now(), $project);
$lock->tryWithKey(
$lock->keyForProject($project, 'projects', $project->getId(), 'accessedAt'),
fn () => $authorization->skip(fn () => $dbForPlatform->updateDocument(
'projects',
$project->getId(),
new Document(['accessedAt' => DateTime::now()])
)),
target: 'projects'
);
}
/**
@@ -840,9 +848,9 @@ Http::init()
->inject('authorization')
->inject('publisherForDeletes')
->inject('executionsRetentionCount')
->inject('platformDBLock')
->inject('lock')
->inject('params')
->action(function (Http $utopia, SwooleRequest $swooleRequest, Request $request, Response $response, Log $log, Document $project, Database $dbForPlatform, callable $getProjectDB, Locale $locale, array $localeCodes, Reader $geodb, Event $queueForEvents, Bus $bus, Executor $executor, array $platform, callable $isResourceBlocked, string $previewHostname, Document $devKey, ?Key $apiKey, Cors $cors, Authorization $authorization, DeletePublisher $publisherForDeletes, int $executionsRetentionCount, PlatformDBLock $platformDBLock, array $params) {
->action(function (Http $utopia, SwooleRequest $swooleRequest, Request $request, Response $response, Log $log, Document $project, Database $dbForPlatform, callable $getProjectDB, Locale $locale, array $localeCodes, Reader $geodb, Event $queueForEvents, Bus $bus, Executor $executor, array $platform, callable $isResourceBlocked, string $previewHostname, Document $devKey, ?Key $apiKey, Cors $cors, Authorization $authorization, DeletePublisher $publisherForDeletes, int $executionsRetentionCount, Lock $lock, array $params) {
/*
* Appwrite Router
*/
@@ -850,7 +858,7 @@ Http::init()
$platformHostnames = $platform['hostnames'] ?? [];
// Only run Router when external domain
if (!\in_array($hostname, $platformHostnames) || !empty($previewHostname)) {
if (router($utopia, $dbForPlatform, $getProjectDB, $swooleRequest, $request, $response, $log, $queueForEvents, $bus, $executor, $geodb, $isResourceBlocked, $platform, $previewHostname, $authorization, $apiKey, $publisherForDeletes, $executionsRetentionCount, $platformDBLock)) {
if (router($utopia, $dbForPlatform, $getProjectDB, $swooleRequest, $request, $response, $log, $queueForEvents, $bus, $executor, $geodb, $isResourceBlocked, $platform, $previewHostname, $authorization, $apiKey, $publisherForDeletes, $executionsRetentionCount, $lock)) {
$utopia->match($request)?->route->label('router', true);
}
}
@@ -1153,15 +1161,15 @@ Http::options()
->inject('authorization')
->inject('publisherForDeletes')
->inject('executionsRetentionCount')
->inject('platformDBLock')
->action(function (Http $utopia, SwooleRequest $swooleRequest, Request $request, Response $response, Log $log, Database $dbForPlatform, callable $getProjectDB, Event $queueForEvents, Bus $bus, Executor $executor, Reader $geodb, callable $isResourceBlocked, array $platform, string $previewHostname, Document $project, Document $devKey, ?Key $apiKey, Cors $cors, Authorization $authorization, DeletePublisher $publisherForDeletes, int $executionsRetentionCount, PlatformDBLock $platformDBLock) {
->inject('lock')
->action(function (Http $utopia, SwooleRequest $swooleRequest, Request $request, Response $response, Log $log, Database $dbForPlatform, callable $getProjectDB, Event $queueForEvents, Bus $bus, Executor $executor, Reader $geodb, callable $isResourceBlocked, array $platform, string $previewHostname, Document $project, Document $devKey, ?Key $apiKey, Cors $cors, Authorization $authorization, DeletePublisher $publisherForDeletes, int $executionsRetentionCount, Lock $lock) {
/*
* Appwrite Router
*/
$platformHostnames = $platform['hostnames'] ?? [];
// Only run Router when external domain
if (!in_array($request->getHostname(), $platformHostnames) || !empty($previewHostname)) {
if (router($utopia, $dbForPlatform, $getProjectDB, $swooleRequest, $request, $response, $log, $queueForEvents, $bus, $executor, $geodb, $isResourceBlocked, $platform, $previewHostname, $authorization, $apiKey, $publisherForDeletes, $executionsRetentionCount, $platformDBLock)) {
if (router($utopia, $dbForPlatform, $getProjectDB, $swooleRequest, $request, $response, $log, $queueForEvents, $bus, $executor, $geodb, $isResourceBlocked, $platform, $previewHostname, $authorization, $apiKey, $publisherForDeletes, $executionsRetentionCount, $lock)) {
$utopia->match($request)?->route->label('router', true);
}
}
@@ -1556,14 +1564,14 @@ Http::get('/robots.txt')
->inject('authorization')
->inject('publisherForDeletes')
->inject('executionsRetentionCount')
->inject('platformDBLock')
->action(function (Http $utopia, SwooleRequest $swooleRequest, Request $request, Response $response, Log $log, Database $dbForPlatform, callable $getProjectDB, Event $queueForEvents, Bus $bus, Executor $executor, Reader $geodb, callable $isResourceBlocked, array $platform, string $previewHostname, ?Key $apiKey, Authorization $authorization, DeletePublisher $publisherForDeletes, int $executionsRetentionCount, PlatformDBLock $platformDBLock) {
->inject('lock')
->action(function (Http $utopia, SwooleRequest $swooleRequest, Request $request, Response $response, Log $log, Database $dbForPlatform, callable $getProjectDB, Event $queueForEvents, Bus $bus, Executor $executor, Reader $geodb, callable $isResourceBlocked, array $platform, string $previewHostname, ?Key $apiKey, Authorization $authorization, DeletePublisher $publisherForDeletes, int $executionsRetentionCount, Lock $lock) {
$platformHostnames = $platform['hostnames'] ?? [];
if (in_array($request->getHostname(), $platformHostnames) || !empty($previewHostname)) {
$template = new View(__DIR__ . '/../views/general/robots.phtml');
$response->text($template->render(false));
} else {
if (router($utopia, $dbForPlatform, $getProjectDB, $swooleRequest, $request, $response, $log, $queueForEvents, $bus, $executor, $geodb, $isResourceBlocked, $platform, $previewHostname, $authorization, $apiKey, $publisherForDeletes, $executionsRetentionCount, $platformDBLock)) {
if (router($utopia, $dbForPlatform, $getProjectDB, $swooleRequest, $request, $response, $log, $queueForEvents, $bus, $executor, $geodb, $isResourceBlocked, $platform, $previewHostname, $authorization, $apiKey, $publisherForDeletes, $executionsRetentionCount, $lock)) {
$utopia->match($request)?->route->label('router', true);
}
}
@@ -1591,14 +1599,14 @@ Http::get('/humans.txt')
->inject('authorization')
->inject('publisherForDeletes')
->inject('executionsRetentionCount')
->inject('platformDBLock')
->action(function (Http $utopia, SwooleRequest $swooleRequest, Request $request, Response $response, Log $log, Database $dbForPlatform, callable $getProjectDB, Event $queueForEvents, Bus $bus, Executor $executor, Reader $geodb, callable $isResourceBlocked, array $platform, string $previewHostname, ?Key $apiKey, Authorization $authorization, DeletePublisher $publisherForDeletes, int $executionsRetentionCount, PlatformDBLock $platformDBLock) {
->inject('lock')
->action(function (Http $utopia, SwooleRequest $swooleRequest, Request $request, Response $response, Log $log, Database $dbForPlatform, callable $getProjectDB, Event $queueForEvents, Bus $bus, Executor $executor, Reader $geodb, callable $isResourceBlocked, array $platform, string $previewHostname, ?Key $apiKey, Authorization $authorization, DeletePublisher $publisherForDeletes, int $executionsRetentionCount, Lock $lock) {
$platformHostnames = $platform['hostnames'] ?? [];
if (in_array($request->getHostname(), $platformHostnames) || !empty($previewHostname)) {
$template = new View(__DIR__ . '/../views/general/humans.phtml');
$response->text($template->render(false));
} else {
if (router($utopia, $dbForPlatform, $getProjectDB, $swooleRequest, $request, $response, $log, $queueForEvents, $bus, $executor, $geodb, $isResourceBlocked, $platform, $previewHostname, $authorization, $apiKey, $publisherForDeletes, $executionsRetentionCount, $platformDBLock)) {
if (router($utopia, $dbForPlatform, $getProjectDB, $swooleRequest, $request, $response, $log, $queueForEvents, $bus, $executor, $geodb, $isResourceBlocked, $platform, $previewHostname, $authorization, $apiKey, $publisherForDeletes, $executionsRetentionCount, $lock)) {
$utopia->match($request)?->route->label('router', true);
}
}
+26 -6
View File
@@ -16,7 +16,7 @@ use Appwrite\Event\Webhook;
use Appwrite\Extend\Exception;
use Appwrite\Extend\Exception as AppwriteException;
use Appwrite\Functions\EventProcessor;
use Appwrite\Locking\PlatformDBLock;
use Appwrite\Locking\Lock;
use Appwrite\Platform\Modules\Storage\Config\CacheControl;
use Appwrite\Platform\Modules\Storage\Config\StorageCacheControl;
use Appwrite\Reference\Renderer;
@@ -61,10 +61,10 @@ Http::init()
->inject('team')
->inject('apiKey')
->inject('authorization')
->inject('platformDBLock')
->inject('lock')
->inject('impersonatorUser')
->inject('targetUser')
->action(function (Route $route, Request $request, Database $dbForPlatform, Database $dbForProject, AuditContext $auditContext, Document $project, User $user, ?Document $session, array $servers, string $mode, Document $team, ?Key $apiKey, Authorization $authorization, PlatformDBLock $platformDBLock, Document $impersonatorUser, User $targetUser) {
->action(function (Route $route, Request $request, Database $dbForPlatform, Database $dbForProject, AuditContext $auditContext, Document $project, User $user, ?Document $session, array $servers, string $mode, Document $team, ?Key $apiKey, Authorization $authorization, Lock $lock, Document $impersonatorUser, User $targetUser) {
/**
* Handle user authentication and session validation.
@@ -211,7 +211,11 @@ Http::init()
$updatedKey = $updates->isEmpty()
? null
: $platformDBLock->tryUpdateDocument('keys', $dbKey->getId(), $updates);
: $lock->tryWithKey(
$lock->key('keys', $dbKey->getId()),
fn () => $authorization->skip(fn () => $dbForPlatform->updateDocument('keys', $dbKey->getId(), $updates)),
target: 'keys'
);
if ($updatedKey instanceof Document) {
if (! empty($apiKey->getProjectId())) {
@@ -376,7 +380,15 @@ Http::init()
if ($project->getId() !== 'console') {
$accessedAt = $project->getAttribute('accessedAt', 0);
if (DateTime::formatTz(DateTime::addSeconds(new \DateTime(), -APP_PROJECT_ACCESS)) > $accessedAt) {
$platformDBLock->tryUpdateAttribute('projects', $project->getId(), 'accessedAt', DateTime::now());
$lock->tryWithKey(
$lock->key('projects', $project->getId(), 'accessedAt'),
fn () => $authorization->skip(fn () => $dbForPlatform->updateDocument(
'projects',
$project->getId(),
new Document(['accessedAt' => DateTime::now()])
)),
target: 'projects'
);
}
}
@@ -392,7 +404,15 @@ Http::init()
'accessedAt' => $user->getAttribute('accessedAt')
]));
} else {
$platformDBLock->tryUpdateAttribute('users', $user->getId(), 'accessedAt', $user->getAttribute('accessedAt'));
$lock->tryWithKey(
$lock->key('users', $user->getId(), 'accessedAt'),
fn () => $authorization->skip(fn () => $dbForPlatform->updateDocument(
'users',
$user->getId(),
new Document(['accessedAt' => $user->getAttribute('accessedAt')])
)),
target: 'users'
);
}
}
}
-7
View File
@@ -15,7 +15,6 @@ use Appwrite\Extend\Exception;
use Appwrite\Functions\EventProcessor;
use Appwrite\GraphQL\Schema;
use Appwrite\Locking\Lock;
use Appwrite\Locking\PlatformDBLock;
use Appwrite\Network\Cors;
use Appwrite\Network\Platform;
use Appwrite\Network\Validator\Origin;
@@ -80,12 +79,6 @@ return function (Container $context): void {
);
}, ['pools', 'telemetry', 'logger', 'project']);
$context->set('platformDBLock', fn (Lock $lock, Database $dbForPlatform, Authorization $authorization): PlatformDBLock => new PlatformDBLock(
$lock,
$dbForPlatform,
$authorization
), ['lock', 'dbForPlatform', 'authorization']);
$context->set('authorization', fn () => new Authorization(), []);
$context->set('store', fn (): Store => new Store(), []);
-72
View File
@@ -1,72 +0,0 @@
<?php
namespace Appwrite\Locking;
use Closure;
use Utopia\Database\Database;
use Utopia\Database\Document;
use Utopia\Database\Validator\Authorization;
final class PlatformDBLock
{
public function __construct(
private readonly Lock $lock,
private readonly Database $dbForPlatform,
private readonly Authorization $authorization,
) {
}
public function tryRun(
string $collection,
string $id,
Closure $callback,
?string $attribute = null,
?Document $project = null,
): mixed {
$key = $project === null
? $this->lock->key($collection, $id, $attribute)
: $this->lock->keyForProject($project, $collection, $id, $attribute);
return $this->lock->tryWithKey($key, $callback, target: $collection);
}
public function tryUpdateAttribute(
string $collection,
string $id,
string $attribute,
mixed $value,
?Document $project = null,
): ?Document {
/** @var Document|null */
return $this->tryRun(
$collection,
$id,
fn () => $this->authorization->skip(fn () => $this->dbForPlatform->updateDocument(
$collection,
$id,
new Document([$attribute => $value])
)),
$attribute,
$project
);
}
public function tryUpdateDocument(
string $collection,
string $id,
Document $updates,
?Document $project = null,
): ?Document {
/** @var Document|null */
return $this->tryRun(
$collection,
$id,
fn () => $this->authorization->skip(fn () => $this->dbForPlatform->updateDocument(
$collection,
$id,
$updates
)),
project: $project
);
}
}
+10
View File
@@ -357,6 +357,16 @@ final class LockTest extends TestCase
);
}
public function testKeyUsesAttributeSuffix(): void
{
$lock = $this->makeLock();
$this->assertSame(
self::KEY_PREFIX.'projects:p1:accessedAt',
$lock->key('projects', 'p1', 'accessedAt')
);
}
public function testPoolCheckoutExceptionRunsCallbackUnlocked(): void
{
$lock = new Lock(
-215
View File
@@ -1,215 +0,0 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Locking;
use Appwrite\Locking\Lock;
use Appwrite\Locking\PlatformDBLock;
use PHPUnit\Framework\TestCase;
use Utopia\Config\Config;
use Utopia\Database\Database;
use Utopia\Database\Document;
use Utopia\Database\Validator\Authorization;
use Utopia\Lock\Exception\Contention as LockContention;
use Utopia\Lock\Lock as UtopiaLock;
use Utopia\Telemetry\Adapter\None as NoTelemetry;
final class PlatformDBLockTest extends TestCase
{
/**
* @var array<string, bool>
*/
private array $heldLocks = [];
private const PROJECT_SEQUENCE = '42';
private const KEY_PREFIX = 'lock:platform:'.self::PROJECT_SEQUENCE.':';
protected function setUp(): void
{
Config::setParam('errors', require __DIR__.'/../../../app/config/errors.php');
$this->heldLocks = [];
}
private function makeLock(?Document $project = null): Lock
{
return new Lock(
fn (string $key, int $ttl, \Closure $callback): mixed => $callback(new PlatformDBLockMemoryLock($key, $this->heldLocks)),
new NoTelemetry(),
null,
$project ?? new Document([
'$id' => 'test-project',
'$sequence' => self::PROJECT_SEQUENCE,
]),
);
}
public function testTryUpdateAttributeUsesAttributeKey(): void
{
$dbForPlatform = $this->createMock(Database::class);
$dbForPlatform
->expects($this->once())
->method('updateDocument')
->with(
'projects',
'p1',
$this->callback(function (Document $document): bool {
$this->assertSame('now', $document->getAttribute('accessedAt'));
return true;
})
)
->willReturn(new Document(['$id' => 'p1']));
$platformDBLock = new PlatformDBLock($this->makeLock(), $dbForPlatform, new Authorization());
$this->assertSame(
'p1',
$platformDBLock->tryUpdateAttribute('projects', 'p1', 'accessedAt', 'now')?->getId()
);
$this->assertArrayNotHasKey(self::KEY_PREFIX.'projects:p1:accessedAt', $this->heldLocks);
}
public function testTryRunUsesDocumentKey(): void
{
$dbForPlatform = $this->createStub(Database::class);
$platformDBLock = new PlatformDBLock($this->makeLock(), $dbForPlatform, new Authorization());
$called = false;
$platformDBLock->tryRun('keys', 'k1', function () use (&$called): void {
$called = true;
});
$this->assertTrue($called);
$this->assertArrayNotHasKey(self::KEY_PREFIX.'keys:k1', $this->heldLocks);
}
public function testTryRunSkipsOnContention(): void
{
$this->heldLocks[self::KEY_PREFIX.'keys:k1'] = true;
$dbForPlatform = $this->createStub(Database::class);
$platformDBLock = new PlatformDBLock($this->makeLock(), $dbForPlatform, new Authorization());
$called = false;
$platformDBLock->tryRun('keys', 'k1', function () use (&$called): void {
$called = true;
});
$this->assertFalse($called);
}
public function testTryUpdateDocumentUsesDocumentKey(): void
{
$updates = new Document(['accessedAt' => 'now', 'sdks' => ['php']]);
$dbForPlatform = $this->createMock(Database::class);
$dbForPlatform
->expects($this->once())
->method('updateDocument')
->with('keys', 'k1', $updates)
->willReturn(new Document(['$id' => 'k1']));
$platformDBLock = new PlatformDBLock($this->makeLock(), $dbForPlatform, new Authorization());
$this->assertSame(
'k1',
$platformDBLock->tryUpdateDocument('keys', 'k1', $updates)?->getId()
);
$this->assertArrayNotHasKey(self::KEY_PREFIX.'keys:k1', $this->heldLocks);
}
public function testTryUpdateDocumentSkipsOnContention(): void
{
$this->heldLocks[self::KEY_PREFIX.'keys:k1'] = true;
$dbForPlatform = $this->createMock(Database::class);
$dbForPlatform
->expects($this->never())
->method('updateDocument');
$platformDBLock = new PlatformDBLock($this->makeLock(), $dbForPlatform, new Authorization());
$this->assertNotInstanceOf(Document::class, $platformDBLock->tryUpdateDocument('keys', 'k1', new Document(['accessedAt' => 'now'])));
}
public function testTryUpdateAttributeCanUseExplicitProjectScope(): void
{
$project = new Document([
'$id' => 'routed-project',
'$sequence' => '84',
]);
$this->heldLocks['lock:platform:84:projects:routed-project:accessedAt'] = true;
$dbForPlatform = $this->createMock(Database::class);
$dbForPlatform
->expects($this->never())
->method('updateDocument');
$platformDBLock = new PlatformDBLock($this->makeLock(new Document()), $dbForPlatform, new Authorization());
$this->assertNotInstanceOf(Document::class, $platformDBLock->tryUpdateAttribute(
'projects',
'routed-project',
'accessedAt',
'now',
$project
));
}
}
final class PlatformDBLockMemoryLock implements UtopiaLock
{
private bool $acquired = false;
/**
* @param array<string, bool> $heldLocks
*/
public function __construct(
private readonly string $key,
private array &$heldLocks,
) {
}
public function acquire(float $timeout = 0.0): bool
{
return $this->tryAcquire();
}
public function tryAcquire(): bool
{
if (isset($this->heldLocks[$this->key])) {
return false;
}
$this->heldLocks[$this->key] = true;
$this->acquired = true;
return true;
}
public function release(): void
{
if (! $this->acquired) {
return;
}
unset($this->heldLocks[$this->key]);
$this->acquired = false;
}
public function withLock(callable $callback, float $timeout = 0.0): mixed
{
if (! $this->acquire($timeout)) {
throw new LockContention("Failed to acquire distributed lock: {$this->key}");
}
try {
return $callback();
} finally {
$this->release();
}
}
}