Compare commits

...
Author SHA1 Message Date
Jake Barnby 7a3ea14abf Merge remote-tracking branch 'origin/1.6.x' into fix-dead-connections
# Conflicts:
#	app/controllers/general.php
#	app/http.php
#	composer.json
#	composer.lock
2025-02-18 00:49:33 +13:00
Jake Barnby 4bb11ef9c2 Lint 2025-01-21 21:40:55 +13:00
Jake Barnby 00419e5a3e Update dev 2025-01-21 21:40:29 +13:00
Jake Barnby 473248b248 Merge remote-tracking branch 'origin/1.6.x' into fix-dead-connections
# Conflicts:
#	composer.json
#	composer.lock
2025-01-21 21:38:55 +13:00
Jake Barnby ec16ec2ff1 Revert "Use connection for project on create"
This reverts commit 75d3d42de6.
2025-01-20 19:28:34 +13:00
Jake Barnby ce7ef42b61 Fix getProjectDB resource 2025-01-20 18:01:07 +13:00
Jake Barnby ef4cc82ded Connection in project for DB 2025-01-20 17:56:51 +13:00
Jake Barnby 099235c69f Revert "Disallow injecting platform DB as project DB"
This reverts commit 09c132414f.

# Conflicts:
#	src/Appwrite/Platform/Workers/Deletes.php
2025-01-20 17:55:48 +13:00
Jake Barnby 62c0226e1b Format 2025-01-20 17:11:40 +13:00
Jake Barnby 4557f2a2df Merge remote-tracking branch 'origin/1.6.x' into fix-dead-connections
# Conflicts:
#	composer.lock
2025-01-20 17:08:55 +13:00
Jake Barnby 09c132414f Disallow injecting platform DB as project DB 2025-01-20 16:57:40 +13:00
Jake Barnby 75d3d42de6 Use connection for project on create 2025-01-20 15:28:50 +13:00
Jake Barnby 7f575acdf6 Remove PDO persist 2025-01-20 15:15:35 +13:00
Jake Barnby 5861111cce Merge remote-tracking branch 'origin/1.6.x' into fix-dead-connections 2025-01-20 14:29:18 +13:00
Jake Barnby 99e764cc51 Update lock 2025-01-17 19:21:45 +13:00
Jake Barnby d7c12babf2 Merge remote-tracking branch 'origin/1.6.x' into fix-dead-connections
# Conflicts:
#	composer.json
#	composer.lock
2025-01-17 17:21:30 +13:00
Jake Barnby c828ade22c Revert platform update 2025-01-15 18:50:39 +13:00
Jake Barnby 4e0ad8650a Update deps 2025-01-15 17:58:47 +13:00
Jake Barnby 3f620bbc44 Merge pull request #9213 from appwrite/fix-dead-connections
Fix dead connections
2025-01-15 15:27:42 +13:00
Jake Barnby 7adf81708c Revert "Force CI"
This reverts commit 651c83ee95.
2025-01-15 15:23:57 +13:00
Jake Barnby 651c83ee95 Force CI 2025-01-15 15:23:52 +13:00
Jake Barnby 8bb1d23a9b Update pools 2025-01-15 15:11:28 +13:00
5 changed files with 77 additions and 37 deletions
+8
View File
@@ -25,6 +25,7 @@ use Appwrite\Utopia\Response\Filters\V18 as ResponseV18;
use Appwrite\Utopia\View;
use Executor\Executor;
use MaxMind\Db\Reader;
use Swoole\Database\DetectsLostConnections;
use Swoole\Http\Request as SwooleRequest;
use Utopia\App;
use Utopia\CLI\Console;
@@ -32,6 +33,7 @@ use Utopia\Config\Config;
use Utopia\Database\Database;
use Utopia\Database\DateTime;
use Utopia\Database\Document;
use Utopia\Database\Exception as DatabaseException;
use Utopia\Database\Helpers\ID;
use Utopia\Database\Query;
use Utopia\Database\Validator\Authorization;
@@ -42,6 +44,7 @@ use Utopia\Logger\Adapter\Sentry;
use Utopia\Logger\Log;
use Utopia\Logger\Log\User;
use Utopia\Logger\Logger;
use Utopia\Pools\Connection;
use Utopia\System\System;
use Utopia\Validator\Hostname;
use Utopia\Validator\Text;
@@ -772,6 +775,11 @@ App::error()
->inject('log')
->inject('queueForStatsUsage')
->action(function (Throwable $error, App $utopia, Request $request, Response $response, Document $project, ?Logger $logger, Log $log, StatsUsage $queueForStatsUsage) {
if (
($error instanceof PDOException || $error instanceof DatabaseException)
&& DetectsLostConnections::causedByLostConnection($error)
) {
}
$version = System::getEnv('_APP_VERSION', 'UNKNOWN');
$route = $utopia->getRoute();
$class = \get_class($error);
+9
View File
@@ -5,6 +5,7 @@ require_once __DIR__ . '/../vendor/autoload.php';
use Appwrite\Utopia\Request;
use Appwrite\Utopia\Response;
use Swoole\Constant;
use Swoole\Database\DetectsLostConnections;
use Swoole\Http\Request as SwooleRequest;
use Swoole\Http\Response as SwooleResponse;
use Swoole\Http\Server;
@@ -17,6 +18,8 @@ use Utopia\Config\Config;
use Utopia\Database\Database;
use Utopia\Database\DateTime;
use Utopia\Database\Document;
use Utopia\Database\Exception as DatabaseException;
use Utopia\Database\Exception\Duplicate;
use Utopia\Database\Helpers\ID;
use Utopia\Database\Helpers\Permission;
use Utopia\Database\Helpers\Role;
@@ -353,6 +356,12 @@ $http->on(Constant::EVENT_REQUEST, function (SwooleRequest $swooleRequest, Swool
$app->run($request, $response);
} catch (\Throwable $th) {
if (
($th instanceof PDOException || $th instanceof DatabaseException)
&& DetectsLostConnections::causedByLostConnection($th)
) {
}
$version = System::getEnv('_APP_VERSION', 'UNKNOWN');
$logger = $app->getResource("logger");
+29 -17
View File
@@ -74,6 +74,7 @@ use Utopia\Logger\Adapter\Raygun;
use Utopia\Logger\Adapter\Sentry;
use Utopia\Logger\Log;
use Utopia\Logger\Logger;
use Utopia\Pools\Connection as PoolConnection;
use Utopia\Pools\Group;
use Utopia\Pools\Pool;
use Utopia\Queue;
@@ -985,7 +986,7 @@ $register->set('pools', function () {
return new PDOProxy(function () use ($dsnHost, $dsnPort, $dsnUser, $dsnPass, $dsnDatabase) {
return new PDO("mysql:host={$dsnHost};port={$dsnPort};dbname={$dsnDatabase};charset=utf8mb4", $dsnUser, $dsnPass, array(
PDO::ATTR_TIMEOUT => 3, // Seconds
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_PERSISTENT => false, // We manage our own pool
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => true,
PDO::ATTR_STRINGIFY_FETCHES => true
@@ -1445,7 +1446,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') {
return $dbForPlatform;
}
@@ -1457,12 +1477,7 @@ App::setResource('dbForProject', function (Group $pools, Database $dbForPlatform
$dsn = new DSN('mysql://' . $project->getAttribute('database'));
}
$dbAdapter = $pools
->get($dsn->getHost())
->pop()
->getResource();
$database = new Database($dbAdapter, $cache);
$database = new Database($connectionForProject->getResource(), $cache);
$database
->setMetadata('host', \gethostname())
@@ -1485,7 +1500,7 @@ App::setResource('dbForProject', function (Group $pools, Database $dbForPlatform
}
return $database;
}, ['pools', 'dbForPlatform', 'cache', 'project']);
}, ['pools', 'connectionForProject', 'dbForPlatform', 'cache', 'project']);
App::setResource('dbForPlatform', function (Group $pools, Cache $cache) {
$dbAdapter = $pools
@@ -1505,10 +1520,10 @@ App::setResource('dbForPlatform', function (Group $pools, Cache $cache) {
return $database;
}, ['pools', 'cache']);
App::setResource('getProjectDB', function (Group $pools, Database $dbForPlatform, $cache) {
$databases = []; // TODO: @Meldiron This should probably be responsibility of utopia-php/pools
App::setResource('getProjectDB', function (Group $pools, Database $dbForPlatform, PoolConnection $connectionForProject, $cache) {
$databases = [];
return function (Document $project) use ($pools, $dbForPlatform, $cache, &$databases) {
return function (Document $project) use ($pools, $connectionForProject, $dbForPlatform, $cache, &$databases) {
if ($project->isEmpty() || $project->getId() === 'console') {
return $dbForPlatform;
}
@@ -1548,10 +1563,7 @@ App::setResource('getProjectDB', function (Group $pools, Database $dbForPlatform
return $database;
}
$dbAdapter = $pools
->get($dsn->getHost())
->pop()
->getResource();
$dbAdapter = $connectionForProject->getResource();
$database = new Database($dbAdapter, $cache);
$databases[$dsn->getHost()] = $database;
@@ -1559,7 +1571,7 @@ App::setResource('getProjectDB', function (Group $pools, Database $dbForPlatform
return $database;
};
}, ['pools', 'dbForPlatform', 'cache']);
}, ['pools', 'dbForPlatform', 'connectionForProject', 'cache']);
App::setResource('getLogsDB', function (Group $pools, Cache $cache) {
$database = null;
+31 -19
View File
@@ -20,6 +20,7 @@ use Appwrite\Event\Usage;
use Appwrite\Event\UsageDump;
/** /remove */
use Appwrite\Platform\Appwrite;
use Swoole\Database\DetectsLostConnections;
use Swoole\Runtime;
use Utopia\Abuse\Adapters\TimeLimit\Redis as TimeLimitRedis;
use Utopia\Cache\Adapter\Sharding;
@@ -29,11 +30,13 @@ use Utopia\Config\Config;
use Utopia\Database\Database;
use Utopia\Database\DateTime;
use Utopia\Database\Document;
use Utopia\Database\Exception as DatabaseException;
use Utopia\Database\Validator\Authorization;
use Utopia\DSN\DSN;
use Utopia\Logger\Log;
use Utopia\Logger\Logger;
use Utopia\Platform\Service;
use Utopia\Pools\Connection as PoolConnection;
use Utopia\Pools\Group;
use Utopia\Queue\Message;
use Utopia\Queue\Publisher;
@@ -70,13 +73,13 @@ Server::setResource('project', function (Message $message, Database $dbForPlatfo
return $dbForPlatform->getDocument('projects', $project->getId());
}, ['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') {
return $dbForPlatform;
return $pools
->get('console')
->pop();
}
$pools = $register->get('pools');
try {
$dsn = new DSN($project->getAttribute('database'));
} catch (\InvalidArgumentException) {
@@ -84,12 +87,17 @@ Server::setResource('dbForProject', function (Cache $cache, Registry $register,
$dsn = new DSN('mysql://' . $project->getAttribute('database'));
}
$adapter = $pools
return $pools
->get($dsn->getHost())
->pop()
->getResource();
->pop();
}, ['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 {
$dsn = new DSN($project->getAttribute('database'));
@@ -113,12 +121,12 @@ Server::setResource('dbForProject', function (Cache $cache, Registry $register,
}
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
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') {
return $dbForPlatform;
}
@@ -150,12 +158,7 @@ Server::setResource('getProjectDB', function (Group $pools, Database $dbForPlatf
return $database;
}
$dbAdapter = $pools
->get($dsn->getHost())
->pop()
->getResource();
$database = new Database($dbAdapter, $cache);
$database = new Database($connectionForProject->getResource(), $cache);
$databases[$dsn->getHost()] = $database;
@@ -175,7 +178,7 @@ Server::setResource('getProjectDB', function (Group $pools, Database $dbForPlatf
return $database;
};
}, ['pools', 'dbForPlatform', 'cache']);
}, ['pools', 'connectionForProject', 'dbForPlatform', 'cache']);
Server::setResource('getLogsDB', function (Group $pools, Cache $cache) {
$database = null;
@@ -459,7 +462,16 @@ $worker
->inject('log')
->inject('pools')
->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();
$version = System::getEnv('_APP_VERSION', 'UNKNOWN');
@@ -20,7 +20,6 @@ use Utopia\Database\Exception as DatabaseException;
use Utopia\Database\Exception\Authorization;
use Utopia\Database\Exception\Conflict;
use Utopia\Database\Exception\Restricted;
use Utopia\Database\Exception\Structure;
use Utopia\Database\Query;
use Utopia\DSN\DSN;
use Utopia\Logger\Log;