From 8e492a9797be231687d771975a46d6f0b7c12b53 Mon Sep 17 00:00:00 2001 From: Eldad Fux Date: Fri, 28 Oct 2022 17:45:16 +0300 Subject: [PATCH] Tests --- .env | 2 + app/config/collections.php | 2 +- app/http.php | 14 ++- app/init.php | 221 ++++++++++++++++++++++++++++++++++++- app/tasks/specs.php | 2 +- 5 files changed, 232 insertions(+), 9 deletions(-) diff --git a/.env b/.env index 950fd44821..4a29d7eb37 100644 --- a/.env +++ b/.env @@ -25,6 +25,8 @@ _APP_DB_PASS=password _APP_DB_ROOT_PASS=rootsecretpassword _APP_CONNECTIONS_DB_PROJECT=db_fra1_02=mysql://user:password@mariadb:3306/appwrite _APP_CONNECTIONS_DB_CONSOLE=db_fra1_01=mysql://user:password@mariadb:3306/appwrite +#_APP_CONNECTIONS_DB_PROJECT=db_fra1_02=mysql://doadmin:AVNS_sgtjg6ZSHBdg66422x-@db-mysql-fra1-shmuel-test-do-user-10204879-0.b.db.ondigitalocean.com:25060/console +#_APP_CONNECTIONS_DB_CONSOLE=db_fra1_01=mysql://doadmin:AVNS_sgtjg6ZSHBdg66422x-@db-mysql-fra1-shmuel-test-do-user-10204879-0.b.db.ondigitalocean.com:25060/projects _APP_CONNECTIONS_CACHE=redis_fra1_01=redis://redis:6379 _APP_CONNECTIONS_QUEUE=redis_fra1_01=redis://redis:6379 _APP_CONNECTIONS_PUBSUB=redis_fra1_01=redis://redis:6379 diff --git a/app/config/collections.php b/app/config/collections.php index 4d88518737..5d5b8890b0 100644 --- a/app/config/collections.php +++ b/app/config/collections.php @@ -1003,7 +1003,7 @@ $collections = [ '$id' => ID::custom('secret'), 'type' => Database::VAR_STRING, 'format' => '', - 'size' => 512, // var_dump of \bin2hex(\random_bytes(128)) => string(256) doubling for encryption + 'size' => 512, // Output of \bin2hex(\random_bytes(128)) => string(256) doubling for encryption 'signed' => true, 'required' => true, 'default' => null, diff --git a/app/http.php b/app/http.php index 7b4ac39cef..0c9597933f 100644 --- a/app/http.php +++ b/app/http.php @@ -20,6 +20,7 @@ use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Swoole\Files; use Appwrite\Utopia\Request; +use Swoole\Coroutine\Channel; use Utopia\Logger\Log; use Utopia\Logger\Log\User; use Utopia\Pools\Group; @@ -59,9 +60,13 @@ include __DIR__ . '/controllers/general.php'; $http->on('start', function (Server $http) use ($payloadSize, $register) { $app = new App('UTC'); + global $http; + var_dump('Test size is:'.$register->get('test')->length()); + + var_dump('Startup is served by:'.$http->worker_id); go(function () use ($register, $app) { - + var_dump('Test 2 size is:'.$register->get('test')->length()); $pools = $register->get('pools'); /** @var Group $pools */ App::setResource('pools', fn() => $pools); @@ -119,7 +124,7 @@ $http->on('start', function (Server $http) use ($payloadSize, $register) { /** * Skip to prevent 0.16 migration issues. */ - if (in_array($key, ['cache', 'variables']) && $dbForConsole->exists(App::getEnv('_APP_DB_SCHEMA', 'appwrite'), 'bucket_1')) { + if (in_array($key, ['cache', 'variables']) && $dbForConsole->exists($dbForConsole->getDefaultDatabase(), 'bucket_1')) { continue; } @@ -155,7 +160,7 @@ $http->on('start', function (Server $http) use ($payloadSize, $register) { $dbForConsole->createCollection($key, $attributes, $indexes); } - if ($dbForConsole->getDocument('buckets', 'default')->isEmpty() && !$dbForConsole->exists(App::getEnv('_APP_DB_SCHEMA', 'appwrite'), 'bucket_1')) { + if ($dbForConsole->getDocument('buckets', 'default')->isEmpty() && !$dbForConsole->exists($dbForConsole->getDefaultDatabase(), 'bucket_1')) { Console::success('[Setup] - Creating default bucket...'); $dbForConsole->createDocument('buckets', new Document([ '$id' => ID::custom('default'), @@ -233,6 +238,9 @@ $http->on('start', function (Server $http) use ($payloadSize, $register) { $http->on('request', function (SwooleRequest $swooleRequest, SwooleResponse $swooleResponse) use ($register) { $request = new Request($swooleRequest); $response = new Response($swooleResponse); + var_dump('Test 3 size is:'.$register->get('test')->length()); + global $http; + var_dump('Served by:'.$http->worker_id); if (Files::isFileLoaded($request->getURI())) { $time = (60 * 60 * 24 * 365 * 2); // 45 days cache diff --git a/app/init.php b/app/init.php index 3a05ea5ddf..0f1ef4b4ec 100644 --- a/app/init.php +++ b/app/init.php @@ -1,5 +1,206 @@ name = $name; + $this->size = $size; + $this->init = $init; + $this->channel = new Channel($size); + } + + /** + * @return string + */ + public function getName(): string + { + return $this->name; + } + + /** + * @return int + */ + public function getSize(): int + { + return $this->size; + } + + /** + * @return int + */ + public function getReconnectAttempts(): int + { + return $this->reconnectAttempts; + } + + /** + * @var int $reconnectAttempts + * @return self + */ + public function setReconnectAttempts(int $reconnectAttempts): self + { + $this->reconnectAttempts = $reconnectAttempts; + return $this; + } + + /** + * @return int + */ + public function getReconnectSleep(): int + { + return $this->reconnectSleep; + } + + /** + * @var int $reconnectSleep + * @return self + */ + public function setReconnectSleep(int $reconnectSleep): self + { + $this->reconnectSleep = $reconnectSleep; + return $this; + } + + /** + * @return self + */ + public function fill(): self + { + if(!$this->channel->isEmpty()) { + return $this; + } + for ($i=0; $i < $this->size; $i++) { + $attempts = 0; + + do { + try { + $attempts++; + $connection = new Connection(($this->init)()); + break; // leave loop if successful + } catch (\Exception $e) { + if ($attempts >= $this->getReconnectAttempts()) { + throw new \Exception('Failed to create connection: ' . $e->getMessage()); + } + sleep($this->getReconnectSleep()); + } + } while ($attempts < $this->getReconnectAttempts()); + + $connection->setID($this->getName().'-'.$i); + + $this->channel->push($connection); + } + + return $this; + } + + /** + * @return Connection + */ + public function pop(): Connection + { + if ($this->channel->isEmpty()) { + throw new Exception('Pool is empty'); + } + + $connection = $this->channel->pop(); + $this->active[$connection->getID()] = $connection; + + return $connection; + } + + /** + * @param Connection $connection + * @return self + */ + public function push(Connection $connection): self + { + $this->channel->push($connection); + unset($this->active[$connection->getID()]); + + return $this; + } + + /** + * @return int + */ + public function count(): int + { + return $this->channel->length(); + } + + /** + * @return self + */ + public function reclaim(): self + { + foreach ($this->active as $connection) { + $this->push($connection); + } + return $this; + } + + /** + * @return bool + */ + public function isEmpty(): bool + { + return $this->channel->isEmpty(); + } + + /** + * @return bool + */ + public function isFull(): bool + { + return $this->channel->isFull(); + } +} + /** * Init * @@ -69,7 +270,6 @@ use Utopia\CLI\Console; use Utopia\Database\Adapter\MariaDB; use Utopia\Database\Adapter\MySQL; use Utopia\Pools\Group; -use Utopia\Pools\Pool; use Ahc\Jwt\JWT; use Ahc\Jwt\JWTException; use MaxMind\Db\Reader; @@ -498,7 +698,8 @@ $register->set('logger', function () { return new Logger($adapter); }); $register->set('pools', function () { - + Console::info('Initializing pools'); + $group = new Group(); $fallbackForDB = AppwriteURL::unparse([ @@ -622,6 +823,9 @@ $register->set('pools', function () { break; } + + + $pool = new Pool($name, 64, function () use ($type, $resource, $dsn) { // Get Adapter $adapter = null; @@ -634,7 +838,6 @@ $register->set('pools', function () { default => null }; - var_dump($dsn->getDatabase()); $adapter->setDefaultDatabase($dsn->getDatabase()); break; @@ -658,7 +861,7 @@ $register->set('pools', function () { return $adapter; }); - +var_dump($pool->channel->length()); $group->add($pool); } @@ -667,12 +870,22 @@ $register->set('pools', function () { try { $group->fill(); + Console::success('Pools have been filled succefully'); } catch (\Throwable $th) { Console::error('Connection failure: ' . $th->getMessage()); } return $group; }); +$register->set('test', function () { + var_dump('[[init test!!]]'); + $test = new Channel(5); + $test->push(1); + $test->push(1); + $test->push(1); + $test->push(1); + return $test; +}); $register->set('influxdb', function () { // Register DB connection $host = App::getEnv('_APP_INFLUXDB_HOST', ''); diff --git a/app/tasks/specs.php b/app/tasks/specs.php index 8d1bc5039f..b3ac763b39 100644 --- a/app/tasks/specs.php +++ b/app/tasks/specs.php @@ -206,7 +206,7 @@ $cli unset($models[$key]); } } - // var_dump($models); + $arguments = [new App('UTC'), $services, $routes, $models, $keys[$platform], $authCounts[$platform] ?? 0]; foreach (['swagger2', 'open-api3'] as $format) { $formatInstance = match ($format) {