Merge branch '1.6.x' into add-hostname-to-audits

This commit is contained in:
Darshan 2025-01-08 19:11:23 +05:30 committed by GitHub
commit 2ea3bad502
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 211 additions and 91 deletions

View file

@ -6,7 +6,7 @@ return [
Specification::S_05VCPU_512MB => [ Specification::S_05VCPU_512MB => [
'slug' => Specification::S_05VCPU_512MB, 'slug' => Specification::S_05VCPU_512MB,
'memory' => 512, 'memory' => 512,
'cpus' => 1 // TODO: revert this, it's a temporary change to test function performance. 'cpus' => 0.5
], ],
Specification::S_1VCPU_512MB => [ Specification::S_1VCPU_512MB => [
'slug' => Specification::S_1VCPU_512MB, 'slug' => Specification::S_1VCPU_512MB,

View file

@ -2944,7 +2944,11 @@ App::post('/v1/databases/:databaseId/collections/:collectionId/documents')
$data['$permissions'] = $permissions; $data['$permissions'] = $permissions;
$document = new Document($data); $document = new Document($data);
$checkPermissions = function (Document $collection, Document $document, string $permission) use (&$checkPermissions, $dbForProject, $database) { $operations = 0;
$checkPermissions = function (Document $collection, Document $document, string $permission) use (&$checkPermissions, $dbForProject, $database, &$operations) {
$operations++;
$documentSecurity = $collection->getAttribute('documentSecurity', false); $documentSecurity = $collection->getAttribute('documentSecurity', false);
$validator = new Authorization($permission); $validator = new Authorization($permission);
@ -3036,6 +3040,7 @@ App::post('/v1/databases/:databaseId/collections/:collectionId/documents')
throw new Exception(Exception::COLLECTION_NOT_FOUND); throw new Exception(Exception::COLLECTION_NOT_FOUND);
} }
// Add $collectionId and $databaseId for all documents // Add $collectionId and $databaseId for all documents
$processDocument = function (Document $collection, Document $document) use (&$processDocument, $dbForProject, $database) { $processDocument = function (Document $collection, Document $document) use (&$processDocument, $dbForProject, $database) {
$document->setAttribute('$databaseId', $database->getId()); $document->setAttribute('$databaseId', $database->getId());
@ -3071,6 +3076,13 @@ App::post('/v1/databases/:databaseId/collections/:collectionId/documents')
$processDocument($collection, $document); $processDocument($collection, $document);
$queueForUsage
->addMetric(METRIC_DATABASES_OPERATIONS_WRITES, $operations)
->addMetric(str_replace('{databaseInternalId}', $database->getInternalId(), METRIC_DATABASE_ID_OPERATIONS_WRITES), $operations)
->addMetric(str_replace(['{databaseInternalId}', '{collectionInternalId}'], [$database->getInternalId(), $collection->getInternalId()], METRIC_DATABASE_ID_COLLECTION_ID_STORAGE), 1); // per collection
$response->addHeader('X-Debug-Operations', $operations);
$response $response
->setStatusCode(Response::STATUS_CODE_CREATED) ->setStatusCode(Response::STATUS_CODE_CREATED)
->dynamic($document, Response::MODEL_DOCUMENT); ->dynamic($document, Response::MODEL_DOCUMENT);
@ -3090,10 +3102,6 @@ App::post('/v1/databases/:databaseId/collections/:collectionId/documents')
->setContext('collection', $collection) ->setContext('collection', $collection)
->setContext('database', $database) ->setContext('database', $database)
->setPayload($response->getPayload(), sensitive: $relationships); ->setPayload($response->getPayload(), sensitive: $relationships);
$queueForUsage
->addMetric(str_replace(['{databaseInternalId}', '{collectionInternalId}'], [$database->getInternalId(), $collection->getInternalId()], METRIC_DATABASE_ID_COLLECTION_ID_STORAGE), 1); // per collection
}); });
App::get('/v1/databases/:databaseId/collections/:collectionId/documents') App::get('/v1/databases/:databaseId/collections/:collectionId/documents')
@ -3116,7 +3124,8 @@ App::get('/v1/databases/:databaseId/collections/:collectionId/documents')
->inject('response') ->inject('response')
->inject('dbForProject') ->inject('dbForProject')
->inject('mode') ->inject('mode')
->action(function (string $databaseId, string $collectionId, array $queries, Response $response, Database $dbForProject, string $mode) { ->inject('queueForUsage')
->action(function (string $databaseId, string $collectionId, array $queries, Response $response, Database $dbForProject, string $mode, Usage $queueForUsage) {
$database = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId)); $database = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId));
$isAPIKey = Auth::isAppUser(Authorization::getRoles()); $isAPIKey = Auth::isAppUser(Authorization::getRoles());
$isPrivilegedUser = Auth::isPrivilegedUser(Authorization::getRoles()); $isPrivilegedUser = Auth::isPrivilegedUser(Authorization::getRoles());
@ -3152,7 +3161,6 @@ App::get('/v1/databases/:databaseId/collections/:collectionId/documents')
throw new Exception(Exception::GENERAL_QUERY_INVALID, $validator->getDescription()); throw new Exception(Exception::GENERAL_QUERY_INVALID, $validator->getDescription());
} }
$documentId = $cursor->getValue(); $documentId = $cursor->getValue();
$cursorDocument = Authorization::skip(fn () => $dbForProject->getDocument('database_' . $database->getInternalId() . '_collection_' . $collection->getInternalId(), $documentId)); $cursorDocument = Authorization::skip(fn () => $dbForProject->getDocument('database_' . $database->getInternalId() . '_collection_' . $collection->getInternalId(), $documentId));
@ -3167,12 +3175,16 @@ App::get('/v1/databases/:databaseId/collections/:collectionId/documents')
$documents = $dbForProject->find('database_' . $database->getInternalId() . '_collection_' . $collection->getInternalId(), $queries); $documents = $dbForProject->find('database_' . $database->getInternalId() . '_collection_' . $collection->getInternalId(), $queries);
$total = $dbForProject->count('database_' . $database->getInternalId() . '_collection_' . $collection->getInternalId(), $queries, APP_LIMIT_COUNT); $total = $dbForProject->count('database_' . $database->getInternalId() . '_collection_' . $collection->getInternalId(), $queries, APP_LIMIT_COUNT);
$operations = 0;
// Add $collectionId and $databaseId for all documents // Add $collectionId and $databaseId for all documents
$processDocument = (function (Document $collection, Document $document) use (&$processDocument, $dbForProject, $database): bool { $processDocument = (function (Document $collection, Document $document) use (&$processDocument, $dbForProject, $database, &$operations): bool {
if ($document->isEmpty()) { if ($document->isEmpty()) {
return false; return false;
} }
$operations++;
$document->removeAttribute('$collection'); $document->removeAttribute('$collection');
$document->setAttribute('$databaseId', $database->getId()); $document->setAttribute('$databaseId', $database->getId());
$document->setAttribute('$collectionId', $collection->getId()); $document->setAttribute('$collectionId', $collection->getId());
@ -3186,8 +3198,13 @@ App::get('/v1/databases/:databaseId/collections/:collectionId/documents')
$related = $document->getAttribute($relationship->getAttribute('key')); $related = $document->getAttribute($relationship->getAttribute('key'));
if (empty($related)) { if (empty($related)) {
if (\in_array(\gettype($related), ['array', 'object'])) {
$operations++;
}
continue; continue;
} }
if (!\is_array($related)) { if (!\is_array($related)) {
$relations = [$related]; $relations = [$related];
} else { } else {
@ -3195,6 +3212,7 @@ App::get('/v1/databases/:databaseId/collections/:collectionId/documents')
} }
$relatedCollectionId = $relationship->getAttribute('relatedCollection'); $relatedCollectionId = $relationship->getAttribute('relatedCollection');
// todo: Use local cache for this getDocument
$relatedCollection = Authorization::skip(fn () => $dbForProject->getDocument('database_' . $database->getInternalId(), $relatedCollectionId)); $relatedCollection = Authorization::skip(fn () => $dbForProject->getDocument('database_' . $database->getInternalId(), $relatedCollectionId));
foreach ($relations as $index => $doc) { foreach ($relations as $index => $doc) {
@ -3219,6 +3237,13 @@ App::get('/v1/databases/:databaseId/collections/:collectionId/documents')
$processDocument($collection, $document); $processDocument($collection, $document);
} }
$queueForUsage
->addMetric(METRIC_DATABASES_OPERATIONS_READS, $operations)
->addMetric(str_replace('{databaseInternalId}', $database->getInternalId(), METRIC_DATABASE_ID_OPERATIONS_READS), $operations)
;
$response->addHeader('X-Debug-Operations', $operations);
$select = \array_reduce($queries, function ($result, $query) { $select = \array_reduce($queries, function ($result, $query) {
return $result || ($query->getMethod() === Query::TYPE_SELECT); return $result || ($query->getMethod() === Query::TYPE_SELECT);
}, false); }, false);
@ -3274,9 +3299,9 @@ App::get('/v1/databases/:databaseId/collections/:collectionId/documents/:documen
->inject('response') ->inject('response')
->inject('dbForProject') ->inject('dbForProject')
->inject('mode') ->inject('mode')
->action(function (string $databaseId, string $collectionId, string $documentId, array $queries, Response $response, Database $dbForProject, string $mode) { ->inject('queueForUsage')
->action(function (string $databaseId, string $collectionId, string $documentId, array $queries, Response $response, Database $dbForProject, string $mode, Usage $queueForUsage) {
$database = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId)); $database = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId));
$isAPIKey = Auth::isAppUser(Authorization::getRoles()); $isAPIKey = Auth::isAppUser(Authorization::getRoles());
$isPrivilegedUser = Auth::isPrivilegedUser(Authorization::getRoles()); $isPrivilegedUser = Auth::isPrivilegedUser(Authorization::getRoles());
@ -3303,12 +3328,16 @@ App::get('/v1/databases/:databaseId/collections/:collectionId/documents/:documen
throw new Exception(Exception::DOCUMENT_NOT_FOUND); throw new Exception(Exception::DOCUMENT_NOT_FOUND);
} }
$operations = 0;
// Add $collectionId and $databaseId for all documents // Add $collectionId and $databaseId for all documents
$processDocument = function (Document $collection, Document $document) use (&$processDocument, $dbForProject, $database) { $processDocument = function (Document $collection, Document $document) use (&$processDocument, $dbForProject, $database, &$operations) {
if ($document->isEmpty()) { if ($document->isEmpty()) {
return; return;
} }
$operations++;
$document->setAttribute('$databaseId', $database->getId()); $document->setAttribute('$databaseId', $database->getId());
$document->setAttribute('$collectionId', $collection->getId()); $document->setAttribute('$collectionId', $collection->getId());
@ -3321,8 +3350,13 @@ App::get('/v1/databases/:databaseId/collections/:collectionId/documents/:documen
$related = $document->getAttribute($relationship->getAttribute('key')); $related = $document->getAttribute($relationship->getAttribute('key'));
if (empty($related)) { if (empty($related)) {
if (\in_array(\gettype($related), ['array', 'object'])) {
$operations++;
}
continue; continue;
} }
if (!\is_array($related)) { if (!\is_array($related)) {
$related = [$related]; $related = [$related];
} }
@ -3342,6 +3376,13 @@ App::get('/v1/databases/:databaseId/collections/:collectionId/documents/:documen
$processDocument($collection, $document); $processDocument($collection, $document);
$queueForUsage
->addMetric(METRIC_DATABASES_OPERATIONS_READS, $operations)
->addMetric(str_replace('{databaseInternalId}', $database->getInternalId(), METRIC_DATABASE_ID_OPERATIONS_READS), $operations)
;
$response->addHeader('X-Debug-Operations', $operations);
$response->dynamic($document, Response::MODEL_DOCUMENT); $response->dynamic($document, Response::MODEL_DOCUMENT);
}); });
@ -3444,6 +3485,7 @@ App::get('/v1/databases/:databaseId/collections/:collectionId/documents/:documen
$output[$i]['countryName'] = $locale->getText('locale.country.unknown'); $output[$i]['countryName'] = $locale->getText('locale.country.unknown');
} }
} }
$response->dynamic(new Document([ $response->dynamic(new Document([
'total' => $audit->countLogsByResource($resource), 'total' => $audit->countLogsByResource($resource),
'logs' => $output, 'logs' => $output,
@ -3481,7 +3523,8 @@ App::patch('/v1/databases/:databaseId/collections/:collectionId/documents/:docum
->inject('dbForProject') ->inject('dbForProject')
->inject('queueForEvents') ->inject('queueForEvents')
->inject('mode') ->inject('mode')
->action(function (string $databaseId, string $collectionId, string $documentId, string|array $data, ?array $permissions, ?\DateTime $requestTimestamp, Response $response, Database $dbForProject, Event $queueForEvents, string $mode) { ->inject('queueForUsage')
->action(function (string $databaseId, string $collectionId, string $documentId, string|array $data, ?array $permissions, ?\DateTime $requestTimestamp, Response $response, Database $dbForProject, Event $queueForEvents, string $mode, Usage $queueForUsage) {
$data = (\is_string($data)) ? \json_decode($data, true) : $data; // Cast to JSON array $data = (\is_string($data)) ? \json_decode($data, true) : $data; // Cast to JSON array
@ -3548,7 +3591,12 @@ App::patch('/v1/databases/:databaseId/collections/:collectionId/documents/:docum
$data['$permissions'] = $permissions; $data['$permissions'] = $permissions;
$newDocument = new Document($data); $newDocument = new Document($data);
$setCollection = (function (Document $collection, Document $document) use (&$setCollection, $dbForProject, $database) { $operations = 0;
$setCollection = (function (Document $collection, Document $document) use (&$setCollection, $dbForProject, $database, &$operations) {
$operations++;
$relationships = \array_filter( $relationships = \array_filter(
$collection->getAttribute('attributes', []), $collection->getAttribute('attributes', []),
fn ($attribute) => $attribute->getAttribute('type') === Database::VAR_RELATIONSHIP fn ($attribute) => $attribute->getAttribute('type') === Database::VAR_RELATIONSHIP
@ -3616,6 +3664,13 @@ App::patch('/v1/databases/:databaseId/collections/:collectionId/documents/:docum
$setCollection($collection, $newDocument); $setCollection($collection, $newDocument);
$queueForUsage
->addMetric(METRIC_DATABASES_OPERATIONS_WRITES, $operations)
->addMetric(str_replace('{databaseInternalId}', $database->getInternalId(), METRIC_DATABASE_ID_OPERATIONS_WRITES), $operations)
;
$response->addHeader('X-Debug-Operations', $operations);
try { try {
$document = $dbForProject->withRequestTimestamp( $document = $dbForProject->withRequestTimestamp(
$requestTimestamp, $requestTimestamp,
@ -3787,6 +3842,13 @@ App::delete('/v1/databases/:databaseId/collections/:collectionId/documents/:docu
$processDocument($collection, $document); $processDocument($collection, $document);
$queueForUsage
->addMetric(METRIC_DATABASES_OPERATIONS_WRITES, 1)
->addMetric(str_replace('{databaseInternalId}', $database->getInternalId(), METRIC_DATABASE_ID_OPERATIONS_WRITES), 1)
->addMetric(str_replace(['{databaseInternalId}', '{collectionInternalId}'], [$database->getInternalId(), $collection->getInternalId()], METRIC_DATABASE_ID_COLLECTION_ID_STORAGE), 1); // per collection
$response->addHeader('X-Debug-Operations', 1);
$relationships = \array_map( $relationships = \array_map(
fn ($document) => $document->getAttribute('key'), fn ($document) => $document->getAttribute('key'),
\array_filter( \array_filter(
@ -3803,9 +3865,6 @@ App::delete('/v1/databases/:databaseId/collections/:collectionId/documents/:docu
->setContext('database', $database) ->setContext('database', $database)
->setPayload($response->output($document, Response::MODEL_DOCUMENT), sensitive: $relationships); ->setPayload($response->output($document, Response::MODEL_DOCUMENT), sensitive: $relationships);
$queueForUsage
->addMetric(str_replace(['{databaseInternalId}', '{collectionInternalId}'], [$database->getInternalId(), $collection->getInternalId()], METRIC_DATABASE_ID_COLLECTION_ID_STORAGE), 1); // per collection
$response->noContent(); $response->noContent();
}); });

View file

@ -6,6 +6,7 @@ use Appwrite\Auth\Auth;
use Appwrite\ClamAV\Network; use Appwrite\ClamAV\Network;
use Appwrite\Event\Delete; use Appwrite\Event\Delete;
use Appwrite\Event\Event; use Appwrite\Event\Event;
use Appwrite\Event\Usage;
use Appwrite\Extend\Exception; use Appwrite\Extend\Exception;
use Appwrite\OpenSSL\OpenSSL; use Appwrite\OpenSSL\OpenSSL;
use Appwrite\Utopia\Database\Validator\CustomId; use Appwrite\Utopia\Database\Validator\CustomId;
@ -886,7 +887,8 @@ App::get('/v1/storage/buckets/:bucketId/files/:fileId/preview')
->inject('mode') ->inject('mode')
->inject('deviceForFiles') ->inject('deviceForFiles')
->inject('deviceForLocal') ->inject('deviceForLocal')
->action(function (string $bucketId, string $fileId, int $width, int $height, string $gravity, int $quality, int $borderWidth, string $borderColor, int $borderRadius, float $opacity, int $rotation, string $background, string $output, Request $request, Response $response, Document $project, Database $dbForProject, string $mode, Device $deviceForFiles, Device $deviceForLocal) { ->inject('queueForUsage')
->action(function (string $bucketId, string $fileId, int $width, int $height, string $gravity, int $quality, int $borderWidth, string $borderColor, int $borderRadius, float $opacity, int $rotation, string $background, string $output, Request $request, Response $response, Document $project, Database $dbForProject, string $mode, Device $deviceForFiles, Device $deviceForLocal, Usage $queueForUsage) {
if (!\extension_loaded('imagick')) { if (!\extension_loaded('imagick')) {
throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Imagick extension is missing'); throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Imagick extension is missing');
@ -1014,6 +1016,11 @@ App::get('/v1/storage/buckets/:bucketId/files/:fileId/preview')
$contentType = (\array_key_exists($output, $outputs)) ? $outputs[$output] : $outputs['jpg']; $contentType = (\array_key_exists($output, $outputs)) ? $outputs[$output] : $outputs['jpg'];
$queueForUsage
->addMetric(METRIC_FILES_TRANSFORMATIONS, 1)
->addMetric(str_replace('{bucketInternalId}', $bucket->getInternalId(), METRIC_BUCKET_ID_FILES_TRANSFORMATIONS), 1)
;
$response $response
->addHeader('Cache-Control', 'private, max-age=2592000') // 30 days ->addHeader('Cache-Control', 'private, max-age=2592000') // 30 days
->setContentType($contentType) ->setContentType($contentType)

View file

@ -21,6 +21,7 @@ use Appwrite\Utopia\Response\Filters\V18 as ResponseV18;
use Appwrite\Utopia\View; use Appwrite\Utopia\View;
use Executor\Executor; use Executor\Executor;
use MaxMind\Db\Reader; use MaxMind\Db\Reader;
use Swoole\Database\DetectsLostConnections;
use Swoole\Http\Request as SwooleRequest; use Swoole\Http\Request as SwooleRequest;
use Utopia\App; use Utopia\App;
use Utopia\CLI\Console; use Utopia\CLI\Console;
@ -28,6 +29,7 @@ use Utopia\Config\Config;
use Utopia\Database\Database; use Utopia\Database\Database;
use Utopia\Database\DateTime; use Utopia\Database\DateTime;
use Utopia\Database\Document; use Utopia\Database\Document;
use Utopia\Database\Exception as DatabaseException;
use Utopia\Database\Helpers\ID; use Utopia\Database\Helpers\ID;
use Utopia\Database\Query; use Utopia\Database\Query;
use Utopia\Database\Validator\Authorization; use Utopia\Database\Validator\Authorization;
@ -38,6 +40,7 @@ use Utopia\Logger\Adapter\Sentry;
use Utopia\Logger\Log; use Utopia\Logger\Log;
use Utopia\Logger\Log\User; use Utopia\Logger\Log\User;
use Utopia\Logger\Logger; use Utopia\Logger\Logger;
use Utopia\Pools\Connection;
use Utopia\System\System; use Utopia\System\System;
use Utopia\Validator\Hostname; use Utopia\Validator\Hostname;
use Utopia\Validator\Text; use Utopia\Validator\Text;
@ -746,7 +749,16 @@ App::error()
->inject('logger') ->inject('logger')
->inject('log') ->inject('log')
->inject('queueForUsage') ->inject('queueForUsage')
->action(function (Throwable $error, App $utopia, Request $request, Response $response, Document $project, ?Logger $logger, Log $log, Usage $queueForUsage) { ->inject('connectionForProject')
->action(function (Throwable $error, App $utopia, Request $request, Response $response, Document $project, ?Logger $logger, Log $log, Usage $queueForUsage, Connection $connectionForProject) {
if (
($error instanceof PDOException || $error instanceof DatabaseException)
&& DetectsLostConnections::causedByLostConnection($error)
) {
// Mark connection as unhealthy so it will be recycled on next reclaim.
$connectionForProject->setHealthy(false);
}
$version = System::getEnv('_APP_VERSION', 'UNKNOWN'); $version = System::getEnv('_APP_VERSION', 'UNKNOWN');
$route = $utopia->getRoute(); $route = $utopia->getRoute();
$class = \get_class($error); $class = \get_class($error);
@ -835,7 +847,7 @@ App::error()
$adapter = new Sentry($projectId, $key, $host); $adapter = new Sentry($projectId, $key, $host);
$logger = new Logger($adapter); $logger = new Logger($adapter);
$logger->setSample(0.04); $logger->setSample(0.01);
$publish = true; $publish = true;
} else { } else {
throw new \Exception('Invalid experimental logging provider'); throw new \Exception('Invalid experimental logging provider');

View file

@ -5,6 +5,7 @@ require_once __DIR__ . '/../vendor/autoload.php';
use Appwrite\Utopia\Request; use Appwrite\Utopia\Request;
use Appwrite\Utopia\Response; use Appwrite\Utopia\Response;
use Swoole\Constant; use Swoole\Constant;
use Swoole\Database\DetectsLostConnections;
use Swoole\Http\Request as SwooleRequest; use Swoole\Http\Request as SwooleRequest;
use Swoole\Http\Response as SwooleResponse; use Swoole\Http\Response as SwooleResponse;
use Swoole\Http\Server; use Swoole\Http\Server;
@ -17,6 +18,7 @@ use Utopia\Config\Config;
use Utopia\Database\Database; use Utopia\Database\Database;
use Utopia\Database\DateTime; use Utopia\Database\DateTime;
use Utopia\Database\Document; use Utopia\Database\Document;
use Utopia\Database\Exception as DatabaseException;
use Utopia\Database\Exception\Duplicate; use Utopia\Database\Exception\Duplicate;
use Utopia\Database\Helpers\ID; use Utopia\Database\Helpers\ID;
use Utopia\Database\Helpers\Permission; use Utopia\Database\Helpers\Permission;
@ -42,7 +44,7 @@ $http = new Server(
); );
$payloadSize = 12 * (1024 * 1024); // 12MB - adding slight buffer for headers and other data that might be sent with the payload - update later with valid testing $payloadSize = 12 * (1024 * 1024); // 12MB - adding slight buffer for headers and other data that might be sent with the payload - update later with valid testing
$totalWorkers = swoole_cpu_num() * intval(System::getEnv('_APP_WORKER_PER_CORE', 6)); $totalWorkers = intval(System::getEnv('_APP_CPU_NUM', swoole_cpu_num())) * intval(System::getEnv('_APP_WORKER_PER_CORE', 6));
$http $http
->set([ ->set([
@ -346,6 +348,15 @@ $http->on(Constant::EVENT_REQUEST, function (SwooleRequest $swooleRequest, Swool
$app->run($request, $response); $app->run($request, $response);
} catch (\Throwable $th) { } catch (\Throwable $th) {
if (
($th instanceof PDOException || $th instanceof DatabaseException)
&& DetectsLostConnections::causedByLostConnection($th)
) {
// Mark connection as unhealthy so it will be recycled on next reclaim.
$connectionForProject = $app->getResource('connectionForProject');
$connectionForProject->setHealthy(false);
}
$version = System::getEnv('_APP_VERSION', 'UNKNOWN'); $version = System::getEnv('_APP_VERSION', 'UNKNOWN');
$logger = $app->getResource("logger"); $logger = $app->getResource("logger");

View file

@ -74,6 +74,7 @@ use Utopia\Logger\Adapter\Raygun;
use Utopia\Logger\Adapter\Sentry; use Utopia\Logger\Adapter\Sentry;
use Utopia\Logger\Log; use Utopia\Logger\Log;
use Utopia\Logger\Logger; use Utopia\Logger\Logger;
use Utopia\Pools\Connection as PoolConnection;
use Utopia\Pools\Group; use Utopia\Pools\Group;
use Utopia\Pools\Pool; use Utopia\Pools\Pool;
use Utopia\Queue; use Utopia\Queue;
@ -259,9 +260,15 @@ const METRIC_DOCUMENTS = 'documents';
const METRIC_DATABASE_ID_DOCUMENTS = '{databaseInternalId}.documents'; const METRIC_DATABASE_ID_DOCUMENTS = '{databaseInternalId}.documents';
const METRIC_DATABASE_ID_COLLECTION_ID_DOCUMENTS = '{databaseInternalId}.{collectionInternalId}.documents'; const METRIC_DATABASE_ID_COLLECTION_ID_DOCUMENTS = '{databaseInternalId}.{collectionInternalId}.documents';
const METRIC_DATABASE_ID_COLLECTION_ID_STORAGE = '{databaseInternalId}.{collectionInternalId}.databases.storage'; const METRIC_DATABASE_ID_COLLECTION_ID_STORAGE = '{databaseInternalId}.{collectionInternalId}.databases.storage';
const METRIC_DATABASES_OPERATIONS_READS = 'databases.operations.reads';
const METRIC_DATABASE_ID_OPERATIONS_READS = '{databaseInternalId}.databases.operations.reads';
const METRIC_DATABASES_OPERATIONS_WRITES = 'databases.operations.writes';
const METRIC_DATABASE_ID_OPERATIONS_WRITES = '{databaseInternalId}.databases.operations.writes';
const METRIC_BUCKETS = 'buckets'; const METRIC_BUCKETS = 'buckets';
const METRIC_FILES = 'files'; const METRIC_FILES = 'files';
const METRIC_FILES_STORAGE = 'files.storage'; const METRIC_FILES_STORAGE = 'files.storage';
const METRIC_FILES_TRANSFORMATIONS = 'files.transformations';
const METRIC_BUCKET_ID_FILES_TRANSFORMATIONS = '{bucketInternalId}.files.transformations';
const METRIC_BUCKET_ID_FILES = '{bucketInternalId}.files'; const METRIC_BUCKET_ID_FILES = '{bucketInternalId}.files';
const METRIC_BUCKET_ID_FILES_STORAGE = '{bucketInternalId}.files.storage'; const METRIC_BUCKET_ID_FILES_STORAGE = '{bucketInternalId}.files.storage';
const METRIC_FUNCTIONS = 'functions'; const METRIC_FUNCTIONS = 'functions';
@ -895,7 +902,7 @@ $register->set('pools', function () {
$multiprocessing = System::getEnv('_APP_SERVER_MULTIPROCESS', 'disabled') === 'enabled'; $multiprocessing = System::getEnv('_APP_SERVER_MULTIPROCESS', 'disabled') === 'enabled';
if ($multiprocessing) { if ($multiprocessing) {
$workerCount = swoole_cpu_num() * intval(System::getEnv('_APP_WORKER_PER_CORE', 6)); $workerCount = intval(System::getEnv('_APP_CPU_NUM', swoole_cpu_num())) * intval(System::getEnv('_APP_WORKER_PER_CORE', 6));
} else { } else {
$workerCount = 1; $workerCount = 1;
} }
@ -1406,7 +1413,26 @@ App::setResource('console', function () {
]); ]);
}, []); }, []);
App::setResource('dbForProject', function (Group $pools, Database $dbForPlatform, Cache $cache, Document $project) { App::setResource('connectionForProject', function (Group $pools, Document $project) {
if ($project->isEmpty() || $project->getId() === 'console') {
return $pools
->get('console')
->pop();
}
try {
$dsn = new DSN($project->getAttribute('database'));
} catch (\InvalidArgumentException) {
// TODO: Temporary until all projects are using shared tables
$dsn = new DSN('mysql://' . $project->getAttribute('database'));
}
return $pools
->get($dsn->getHost())
->pop();
}, ['pools', 'project']);
App::setResource('dbForProject', function (Group $pools, PoolConnection $connectionForProject, Database $dbForPlatform, Cache $cache, Document $project) {
if ($project->isEmpty() || $project->getId() === 'console') { if ($project->isEmpty() || $project->getId() === 'console') {
return $dbForPlatform; return $dbForPlatform;
} }
@ -1418,12 +1444,7 @@ App::setResource('dbForProject', function (Group $pools, Database $dbForPlatform
$dsn = new DSN('mysql://' . $project->getAttribute('database')); $dsn = new DSN('mysql://' . $project->getAttribute('database'));
} }
$dbAdapter = $pools $database = new Database($connectionForProject->getResource(), $cache);
->get($dsn->getHost())
->pop()
->getResource();
$database = new Database($dbAdapter, $cache);
$database $database
->setMetadata('host', \gethostname()) ->setMetadata('host', \gethostname())
@ -1446,7 +1467,7 @@ App::setResource('dbForProject', function (Group $pools, Database $dbForPlatform
} }
return $database; return $database;
}, ['pools', 'dbForPlatform', 'cache', 'project']); }, ['pools', 'connectionForProject', 'dbForPlatform', 'cache', 'project']);
App::setResource('dbForPlatform', function (Group $pools, Cache $cache) { App::setResource('dbForPlatform', function (Group $pools, Cache $cache) {
$dbAdapter = $pools $dbAdapter = $pools

View file

@ -193,7 +193,7 @@ $stats->create();
$containerId = uniqid(); $containerId = uniqid();
$statsDocument = null; $statsDocument = null;
$workerNumber = swoole_cpu_num() * intval(System::getEnv('_APP_WORKER_PER_CORE', 6)); $workerNumber = intval(System::getEnv('_APP_CPU_NUM', swoole_cpu_num())) * intval(System::getEnv('_APP_WORKER_PER_CORE', 6));
$adapter = new Adapter\Swoole(port: System::getEnv('PORT', 80)); $adapter = new Adapter\Swoole(port: System::getEnv('PORT', 80));
$adapter $adapter

View file

@ -16,6 +16,7 @@ use Appwrite\Event\Migration;
use Appwrite\Event\Usage; use Appwrite\Event\Usage;
use Appwrite\Event\UsageDump; use Appwrite\Event\UsageDump;
use Appwrite\Platform\Appwrite; use Appwrite\Platform\Appwrite;
use Swoole\Database\DetectsLostConnections;
use Swoole\Runtime; use Swoole\Runtime;
use Utopia\Abuse\Adapters\TimeLimit\Redis as TimeLimitRedis; use Utopia\Abuse\Adapters\TimeLimit\Redis as TimeLimitRedis;
use Utopia\Cache\Adapter\Sharding; use Utopia\Cache\Adapter\Sharding;
@ -25,11 +26,13 @@ use Utopia\Config\Config;
use Utopia\Database\Database; use Utopia\Database\Database;
use Utopia\Database\DateTime; use Utopia\Database\DateTime;
use Utopia\Database\Document; use Utopia\Database\Document;
use Utopia\Database\Exception as DatabaseException;
use Utopia\Database\Validator\Authorization; use Utopia\Database\Validator\Authorization;
use Utopia\DSN\DSN; use Utopia\DSN\DSN;
use Utopia\Logger\Log; use Utopia\Logger\Log;
use Utopia\Logger\Logger; use Utopia\Logger\Logger;
use Utopia\Platform\Service; use Utopia\Platform\Service;
use Utopia\Pools\Connection as PoolConnection;
use Utopia\Pools\Group; use Utopia\Pools\Group;
use Utopia\Queue\Connection; use Utopia\Queue\Connection;
use Utopia\Queue\Message; use Utopia\Queue\Message;
@ -66,13 +69,13 @@ Server::setResource('project', function (Message $message, Database $dbForPlatfo
return $dbForPlatform->getDocument('projects', $project->getId()); return $dbForPlatform->getDocument('projects', $project->getId());
}, ['message', 'dbForPlatform']); }, ['message', 'dbForPlatform']);
Server::setResource('dbForProject', function (Cache $cache, Registry $register, Message $message, Document $project, Database $dbForPlatform) { Server::setResource('connectionForProject', function (Group $pools, Document $project) {
if ($project->isEmpty() || $project->getId() === 'console') { if ($project->isEmpty() || $project->getId() === 'console') {
return $dbForPlatform; return $pools
->get('console')
->pop();
} }
$pools = $register->get('pools');
try { try {
$dsn = new DSN($project->getAttribute('database')); $dsn = new DSN($project->getAttribute('database'));
} catch (\InvalidArgumentException) { } catch (\InvalidArgumentException) {
@ -80,12 +83,17 @@ Server::setResource('dbForProject', function (Cache $cache, Registry $register,
$dsn = new DSN('mysql://' . $project->getAttribute('database')); $dsn = new DSN('mysql://' . $project->getAttribute('database'));
} }
$adapter = $pools return $pools
->get($dsn->getHost()) ->get($dsn->getHost())
->pop() ->pop();
->getResource(); }, ['pools', 'project']);
$database = new Database($adapter, $cache); Server::setResource('dbForProject', function (PoolConnection $connectionForProject, Cache $cache, Registry $register, Message $message, Document $project, Database $dbForPlatform) {
if ($project->isEmpty() || $project->getId() === 'console') {
return $dbForPlatform;
}
$database = new Database($connectionForProject->getResource(), $cache);
try { try {
$dsn = new DSN($project->getAttribute('database')); $dsn = new DSN($project->getAttribute('database'));
@ -109,12 +117,12 @@ Server::setResource('dbForProject', function (Cache $cache, Registry $register,
} }
return $database; return $database;
}, ['cache', 'register', 'message', 'project', 'dbForPlatform']); }, ['connectionForProject', 'cache', 'register', 'message', 'project', 'dbForPlatform']);
Server::setResource('getProjectDB', function (Group $pools, Database $dbForPlatform, $cache) { Server::setResource('getProjectDB', function (Group $pools, PoolConnection $connectionForProject, Database $dbForPlatform, $cache) {
$databases = []; // TODO: @Meldiron This should probably be responsibility of utopia-php/pools $databases = []; // TODO: @Meldiron This should probably be responsibility of utopia-php/pools
return function (Document $project) use ($pools, $dbForPlatform, $cache, &$databases): Database { return function (Document $project) use ($pools, $connectionForProject, $dbForPlatform, $cache, &$databases): Database {
if ($project->isEmpty() || $project->getId() === 'console') { if ($project->isEmpty() || $project->getId() === 'console') {
return $dbForPlatform; return $dbForPlatform;
} }
@ -146,12 +154,7 @@ Server::setResource('getProjectDB', function (Group $pools, Database $dbForPlatf
return $database; return $database;
} }
$dbAdapter = $pools $database = new Database($connectionForProject->getResource(), $cache);
->get($dsn->getHost())
->pop()
->getResource();
$database = new Database($dbAdapter, $cache);
$databases[$dsn->getHost()] = $database; $databases[$dsn->getHost()] = $database;
@ -171,7 +174,7 @@ Server::setResource('getProjectDB', function (Group $pools, Database $dbForPlatf
return $database; return $database;
}; };
}, ['pools', 'dbForPlatform', 'cache']); }, ['pools', 'connectionForProject', 'dbForPlatform', 'cache']);
Server::setResource('abuseRetention', function () { Server::setResource('abuseRetention', function () {
return time() - (int) System::getEnv('_APP_MAINTENANCE_RETENTION_ABUSE', 86400); return time() - (int) System::getEnv('_APP_MAINTENANCE_RETENTION_ABUSE', 86400);
@ -410,7 +413,16 @@ $worker
->inject('log') ->inject('log')
->inject('pools') ->inject('pools')
->inject('project') ->inject('project')
->action(function (Throwable $error, ?Logger $logger, Log $log, Group $pools, Document $project) use ($queueName) { ->inject('connectionForProject')
->action(function (Throwable $error, ?Logger $logger, Log $log, Group $pools, Document $project, PoolConnection $connectionForProject) use ($queueName) {
if (
($error instanceof PDOException || $error instanceof DatabaseException)
&& DetectsLostConnections::causedByLostConnection($error)
) {
// Mark connection as unhealthy, it will be recycled on next reclaim.
$connectionForProject->setHealthy(false);
}
$pools->reclaim(); $pools->reclaim();
$version = System::getEnv('_APP_VERSION', 'UNKNOWN'); $version = System::getEnv('_APP_VERSION', 'UNKNOWN');

View file

@ -63,7 +63,7 @@
"utopia-php/migration": "0.6.*", "utopia-php/migration": "0.6.*",
"utopia-php/orchestration": "0.9.*", "utopia-php/orchestration": "0.9.*",
"utopia-php/platform": "0.7.*", "utopia-php/platform": "0.7.*",
"utopia-php/pools": "0.5.*", "utopia-php/pools": "0.6.*",
"utopia-php/preloader": "0.2.*", "utopia-php/preloader": "0.2.*",
"utopia-php/queue": "0.7.*", "utopia-php/queue": "0.7.*",
"utopia-php/registry": "0.5.*", "utopia-php/registry": "0.5.*",

76
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "6b136b5490c0d5d331eac0d70bb3e198", "content-hash": "7b5b5926b452186543903eb539f59c2d",
"packages": [ "packages": [
{ {
"name": "adhocore/jwt", "name": "adhocore/jwt",
@ -2453,16 +2453,16 @@
}, },
{ {
"name": "symfony/http-client", "name": "symfony/http-client",
"version": "v7.2.1", "version": "v7.2.2",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/symfony/http-client.git", "url": "https://github.com/symfony/http-client.git",
"reference": "ff4df2b68d1c67abb9fef146e6540ea16b58d99e" "reference": "339ba21476eb184290361542f732ad12c97591ec"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/symfony/http-client/zipball/ff4df2b68d1c67abb9fef146e6540ea16b58d99e", "url": "https://api.github.com/repos/symfony/http-client/zipball/339ba21476eb184290361542f732ad12c97591ec",
"reference": "ff4df2b68d1c67abb9fef146e6540ea16b58d99e", "reference": "339ba21476eb184290361542f732ad12c97591ec",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -2528,7 +2528,7 @@
"http" "http"
], ],
"support": { "support": {
"source": "https://github.com/symfony/http-client/tree/v7.2.1" "source": "https://github.com/symfony/http-client/tree/v7.2.2"
}, },
"funding": [ "funding": [
{ {
@ -2544,7 +2544,7 @@
"type": "tidelift" "type": "tidelift"
} }
], ],
"time": "2024-12-07T08:50:44+00:00" "time": "2024-12-30T18:35:15+00:00"
}, },
{ {
"name": "symfony/http-client-contracts", "name": "symfony/http-client-contracts",
@ -3929,16 +3929,16 @@
}, },
{ {
"name": "utopia-php/migration", "name": "utopia-php/migration",
"version": "0.6.13", "version": "0.6.14",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/utopia-php/migration.git", "url": "https://github.com/utopia-php/migration.git",
"reference": "68d9b0a9477755afcda607e7e8109785cae17a13" "reference": "59a19f09ded0ccab4c8cca35b1242c01e2b9cfd2"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/utopia-php/migration/zipball/68d9b0a9477755afcda607e7e8109785cae17a13", "url": "https://api.github.com/repos/utopia-php/migration/zipball/59a19f09ded0ccab4c8cca35b1242c01e2b9cfd2",
"reference": "68d9b0a9477755afcda607e7e8109785cae17a13", "reference": "59a19f09ded0ccab4c8cca35b1242c01e2b9cfd2",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -3979,9 +3979,9 @@
], ],
"support": { "support": {
"issues": "https://github.com/utopia-php/migration/issues", "issues": "https://github.com/utopia-php/migration/issues",
"source": "https://github.com/utopia-php/migration/tree/0.6.13" "source": "https://github.com/utopia-php/migration/tree/0.6.14"
}, },
"time": "2024-11-26T13:57:53+00:00" "time": "2025-01-08T01:07:25+00:00"
}, },
{ {
"name": "utopia-php/mongo", "name": "utopia-php/mongo",
@ -4145,25 +4145,25 @@
}, },
{ {
"name": "utopia-php/pools", "name": "utopia-php/pools",
"version": "0.5.0", "version": "0.6.0",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/utopia-php/pools.git", "url": "https://github.com/utopia-php/pools.git",
"reference": "6f716a213a08db95eda1b5dddfa90983c1834817" "reference": "59414ab7b57728edfde6d5ccc5a2583b7967ac18"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/utopia-php/pools/zipball/6f716a213a08db95eda1b5dddfa90983c1834817", "url": "https://api.github.com/repos/utopia-php/pools/zipball/59414ab7b57728edfde6d5ccc5a2583b7967ac18",
"reference": "6f716a213a08db95eda1b5dddfa90983c1834817", "reference": "59414ab7b57728edfde6d5ccc5a2583b7967ac18",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
"php": ">=8.0" "php": ">=8.3"
}, },
"require-dev": { "require-dev": {
"laravel/pint": "1.2.*", "laravel/pint": "1.*",
"phpstan/phpstan": "1.8.*", "phpstan/phpstan": "1.*",
"phpunit/phpunit": "^9.3" "phpunit/phpunit": "11.*"
}, },
"type": "library", "type": "library",
"autoload": { "autoload": {
@ -4190,9 +4190,9 @@
], ],
"support": { "support": {
"issues": "https://github.com/utopia-php/pools/issues", "issues": "https://github.com/utopia-php/pools/issues",
"source": "https://github.com/utopia-php/pools/tree/0.5.0" "source": "https://github.com/utopia-php/pools/tree/0.6.0"
}, },
"time": "2024-04-19T11:11:54+00:00" "time": "2025-01-08T07:58:42+00:00"
}, },
{ {
"name": "utopia-php/preloader", "name": "utopia-php/preloader",
@ -5126,16 +5126,16 @@
}, },
{ {
"name": "laravel/pint", "name": "laravel/pint",
"version": "v1.18.3", "version": "v1.19.0",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/laravel/pint.git", "url": "https://github.com/laravel/pint.git",
"reference": "cef51821608239040ab841ad6e1c6ae502ae3026" "reference": "8169513746e1bac70c85d6ea1524d9225d4886f0"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/laravel/pint/zipball/cef51821608239040ab841ad6e1c6ae502ae3026", "url": "https://api.github.com/repos/laravel/pint/zipball/8169513746e1bac70c85d6ea1524d9225d4886f0",
"reference": "cef51821608239040ab841ad6e1c6ae502ae3026", "reference": "8169513746e1bac70c85d6ea1524d9225d4886f0",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -5146,10 +5146,10 @@
"php": "^8.1.0" "php": "^8.1.0"
}, },
"require-dev": { "require-dev": {
"friendsofphp/php-cs-fixer": "^3.65.0", "friendsofphp/php-cs-fixer": "^3.66.0",
"illuminate/view": "^10.48.24", "illuminate/view": "^10.48.25",
"larastan/larastan": "^2.9.11", "larastan/larastan": "^2.9.12",
"laravel-zero/framework": "^10.4.0", "laravel-zero/framework": "^10.48.25",
"mockery/mockery": "^1.6.12", "mockery/mockery": "^1.6.12",
"nunomaduro/termwind": "^1.17.0", "nunomaduro/termwind": "^1.17.0",
"pestphp/pest": "^2.36.0" "pestphp/pest": "^2.36.0"
@ -5188,7 +5188,7 @@
"issues": "https://github.com/laravel/pint/issues", "issues": "https://github.com/laravel/pint/issues",
"source": "https://github.com/laravel/pint" "source": "https://github.com/laravel/pint"
}, },
"time": "2024-11-26T15:34:00+00:00" "time": "2024-12-30T16:20:10+00:00"
}, },
{ {
"name": "matthiasmullie/minify", "name": "matthiasmullie/minify",
@ -7735,16 +7735,16 @@
}, },
{ {
"name": "symfony/finder", "name": "symfony/finder",
"version": "v7.2.0", "version": "v7.2.2",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/symfony/finder.git", "url": "https://github.com/symfony/finder.git",
"reference": "6de263e5868b9a137602dd1e33e4d48bfae99c49" "reference": "87a71856f2f56e4100373e92529eed3171695cfb"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/symfony/finder/zipball/6de263e5868b9a137602dd1e33e4d48bfae99c49", "url": "https://api.github.com/repos/symfony/finder/zipball/87a71856f2f56e4100373e92529eed3171695cfb",
"reference": "6de263e5868b9a137602dd1e33e4d48bfae99c49", "reference": "87a71856f2f56e4100373e92529eed3171695cfb",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -7779,7 +7779,7 @@
"description": "Finds files and directories via an intuitive fluent interface", "description": "Finds files and directories via an intuitive fluent interface",
"homepage": "https://symfony.com", "homepage": "https://symfony.com",
"support": { "support": {
"source": "https://github.com/symfony/finder/tree/v7.2.0" "source": "https://github.com/symfony/finder/tree/v7.2.2"
}, },
"funding": [ "funding": [
{ {
@ -7795,7 +7795,7 @@
"type": "tidelift" "type": "tidelift"
} }
], ],
"time": "2024-10-23T06:56:12+00:00" "time": "2024-12-30T19:00:17+00:00"
}, },
{ {
"name": "symfony/options-resolver", "name": "symfony/options-resolver",

View file

@ -284,8 +284,6 @@ class Databases extends Action
$dbForProject->deleteDocument('attributes', $relatedAttribute->getId()); $dbForProject->deleteDocument('attributes', $relatedAttribute->getId());
} }
throw $e;
} catch (\Throwable $e) { } catch (\Throwable $e) {
Console::error($e->getMessage()); Console::error($e->getMessage());