Skip to content

Commit

Permalink
Add cache fallback into DB library
Browse files Browse the repository at this point in the history
  • Loading branch information
PineappleIOnic committed Dec 17, 2024
1 parent 7421381 commit a5b8017
Show file tree
Hide file tree
Showing 11 changed files with 210 additions and 115 deletions.
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ RUN \
git \
brotli-dev \
linux-headers \
docker-cli \
docker-cli-compose \
&& docker-php-ext-install opcache pgsql pdo_mysql pdo_pgsql \
&& apk del postgresql-dev \
&& rm -rf /var/cache/apk/*
Expand Down
169 changes: 85 additions & 84 deletions composer.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ services:
- ./dev:/usr/src/code/dev
- ./phpunit.xml:/usr/src/code/phpunit.xml
- ./dev/xdebug.ini:/usr/local/etc/php/conf.d/xdebug.ini
- /var/run/docker.sock:/var/run/docker.sock
- ./docker-compose.yml:/usr/src/code/docker-compose.yml

adminer:
image: adminer
Expand Down
19 changes: 16 additions & 3 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Exception;
use Utopia\Cache\Cache;
use Utopia\CLI\Console;
use Utopia\Database\Exception as DatabaseException;
use Utopia\Database\Exception\Authorization as AuthorizationException;
use Utopia\Database\Exception\Conflict as ConflictException;
Expand Down Expand Up @@ -2956,7 +2957,14 @@ public function getDocument(string $collection, string $id, array $queries = [],
$documentCacheHash .= ':' . \md5(\implode($selections));
}

if ($cache = $this->cache->load($documentCacheKey, self::TTL, $documentCacheHash)) {
try {
$cache = $this->cache->load($documentCacheKey, self::TTL, $documentCacheHash);
} catch (Exception $e) {
Console::warning('Warning: Failed to get document from cache: ' . $e->getMessage());
$cache = null;
}

if ($cache) {
$document = new Document($cache);

if ($collection->getId() !== self::METADATA) {
Expand Down Expand Up @@ -5321,8 +5329,13 @@ public function purgeCachedDocument(string $collectionId, string $id): bool
$collectionKey = $this->cacheName . '-cache-' . $this->getNamespace() . ':' . $this->adapter->getTenant() . ':collection:' . $collectionId;
$documentKey = $collectionKey . ':' . $id;

$this->cache->purge($collectionKey, $documentKey);
$this->cache->purge($documentKey);
try {
$this->cache->purge($collectionKey, $documentKey);
$this->cache->purge($documentKey);
} catch (Exception $e) {
Console::warning('Warning: Failed to purge cache: ' . $e->getMessage());
return false;
}

return true;
}
Expand Down
41 changes: 41 additions & 0 deletions tests/e2e/Adapter/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Exception;
use PHPUnit\Framework\TestCase;
use Throwable;
use Utopia\CLI\CLI;
use Utopia\CLI\Console;
use Utopia\Database\Adapter\SQL;
use Utopia\Database\Database;
use Utopia\Database\DateTime;
Expand Down Expand Up @@ -70,6 +72,10 @@ public function setUp(): void
public function tearDown(): void
{
Authorization::setDefaultStatus(true);

$stdout = '';
$stderr = '';
Console::execute('docker ps -a --filter "name=utopia-redis" --format "{{.Names}}" | xargs -r docker start', "", $stdout, $stderr);
}

protected string $testDatabase = 'utopiaTests';
Expand Down Expand Up @@ -17102,6 +17108,41 @@ public function testUpdateDocumentsRelationships(): void
}
}

public function testRedisFallback(): void
{
Authorization::cleanRoles();
Authorization::setRole(Role::any()->toString());
$database = static::getDatabase();

$stdout = '';
$stderr = '';
Console::execute('docker ps -a --filter "name=utopia-redis" --format "{{.Names}}" | xargs -r docker stop', "", $stdout, $stderr);

$database->createCollection('testRedisFallback', attributes: [
new Document([
'$id' => ID::custom('string'),
'type' => Database::VAR_STRING,
'size' => 767,
'required' => true,
])
], permissions: [
Permission::read(Role::any()),
Permission::create(Role::any()),
Permission::update(Role::any()),
Permission::delete(Role::any())
]);

$database->createDocument('testRedisFallback', new Document([
'$id' => 'doc1',
'string' => 'text📝',
]));

$database->createIndex('testRedisFallback', 'index1', Database::INDEX_KEY, ['string']);

// Bring backup Redis
Console::execute('cd /usr/src/code && docker compose up redis -d', "", $stdout, $stderr);
}

public function testEvents(): void
{
Authorization::skip(function () {
Expand Down
13 changes: 9 additions & 4 deletions tests/e2e/Adapter/MariaDBTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PDO;
use Redis;
use Utopia\Cache\Adapter\None;
use Utopia\Cache\Adapter\Redis as RedisAdapter;
use Utopia\Cache\Cache;
use Utopia\Database\Adapter\MariaDB;
Expand Down Expand Up @@ -41,10 +42,14 @@ public static function getDatabase(bool $fresh = false): Database
$dbPass = 'password';

$pdo = new PDO("mysql:host={$dbHost};port={$dbPort};charset=utf8mb4", $dbUser, $dbPass, MariaDB::getPDOAttributes());
$redis = new Redis();
$redis->connect('redis', 6379);
$redis->flushAll();
$cache = new Cache(new RedisAdapter($redis));
try {
$redis = new Redis();
$redis->connect('redis', 6379);
$redis->flushAll();
$cache = new Cache(new RedisAdapter($redis));
} catch (\Exception $e) {
$cache = new Cache(new None());
}

$database = new Database(new MariaDB($pdo), $cache);
$database
Expand Down
27 changes: 19 additions & 8 deletions tests/e2e/Adapter/MirrorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PDO;
use Redis;
use Utopia\Cache\Adapter\None;
use Utopia\Cache\Adapter\Redis as RedisAdapter;
use Utopia\Cache\Cache;
use Utopia\Database\Adapter\MariaDB;
Expand Down Expand Up @@ -45,10 +46,15 @@ protected static function getDatabase(bool $fresh = false): Mirror
$dbPass = 'password';

$pdo = new PDO("mysql:host={$dbHost};port={$dbPort};charset=utf8mb4", $dbUser, $dbPass, MariaDB::getPDOAttributes());
$redis = new Redis();
$redis->connect('redis');
$redis->flushAll();
$cache = new Cache(new RedisAdapter($redis));

try {
$redis = new Redis();
$redis->connect('redis', 6379);
$redis->flushAll();
$cache = new Cache(new RedisAdapter($redis));
} catch (\Exception $e) {
$cache = new Cache(new None());
}

self::$sourcePdo = $pdo;
self::$source = new Database(new MariaDB($pdo), $cache);
Expand All @@ -59,10 +65,15 @@ protected static function getDatabase(bool $fresh = false): Mirror
$mirrorPass = 'password';

$mirrorPdo = new PDO("mysql:host={$mirrorHost};port={$mirrorPort};charset=utf8mb4", $mirrorUser, $mirrorPass, MariaDB::getPDOAttributes());
$mirrorRedis = new Redis();
$mirrorRedis->connect('redis-mirror');
$mirrorRedis->flushAll();
$mirrorCache = new Cache(new RedisAdapter($mirrorRedis));

try {
$mirrorRedis = new Redis();
$mirrorRedis->connect('redis-mirror');
$mirrorRedis->flushAll();
$mirrorCache = new Cache(new RedisAdapter($mirrorRedis));
} catch (\Exception $e) {
$mirrorCache = new Cache(new None());
}

self::$destinationPdo = $mirrorPdo;
self::$destination = new Database(new MariaDB($mirrorPdo), $mirrorCache);
Expand Down
13 changes: 9 additions & 4 deletions tests/e2e/Adapter/MongoDBTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Exception;
use Redis;
use Utopia\Cache\Adapter\None;
use Utopia\Cache\Adapter\Redis as RedisAdapter;
use Utopia\Cache\Cache;
use Utopia\Database\Adapter\Mongo;
Expand Down Expand Up @@ -35,10 +36,14 @@ public static function getDatabase(): Database
return self::$database;
}

$redis = new Redis();
$redis->connect('redis', 6379);
$redis->flushAll();
$cache = new Cache(new RedisAdapter($redis));
try {
$redis = new Redis();
$redis->connect('redis', 6379);
$redis->flushAll();
$cache = new Cache(new RedisAdapter($redis));
} catch (\Exception $e) {
$cache = new Cache(new None());
}

$schema = 'utopiaTests'; // same as $this->testDatabase
$client = new Client(
Expand Down
14 changes: 9 additions & 5 deletions tests/e2e/Adapter/MySQLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PDO;
use Redis;
use Utopia\Cache\Adapter\None;
use Utopia\Cache\Adapter\Redis as RedisAdapter;
use Utopia\Cache\Cache;
use Utopia\Database\Adapter\MySQL;
Expand Down Expand Up @@ -42,11 +43,14 @@ public static function getDatabase(): Database

$pdo = new PDO("mysql:host={$dbHost};port={$dbPort};charset=utf8mb4", $dbUser, $dbPass, MySQL::getPDOAttributes());

$redis = new Redis();
$redis->connect('redis', 6379);
$redis->flushAll();

$cache = new Cache(new RedisAdapter($redis));
try {
$redis = new Redis();
$redis->connect('redis', 6379);
$redis->flushAll();
$cache = new Cache(new RedisAdapter($redis));
} catch (\Exception $e) {
$cache = new Cache(new None());
}

$database = new Database(new MySQL($pdo), $cache);
$database
Expand Down
13 changes: 9 additions & 4 deletions tests/e2e/Adapter/PostgresTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PDO;
use Redis;
use Utopia\Cache\Adapter\None;
use Utopia\Cache\Adapter\Redis as RedisAdapter;
use Utopia\Cache\Cache;
use Utopia\Database\Adapter\Postgres;
Expand Down Expand Up @@ -40,10 +41,14 @@ public static function getDatabase(): Database
$dbPass = 'password';

$pdo = new PDO("pgsql:host={$dbHost};port={$dbPort};", $dbUser, $dbPass, Postgres::getPDOAttributes());
$redis = new Redis();
$redis->connect('redis', 6379);
$redis->flushAll();
$cache = new Cache(new RedisAdapter($redis));
try {
$redis = new Redis();
$redis->connect('redis', 6379);
$redis->flushAll();
$cache = new Cache(new RedisAdapter($redis));
} catch (\Exception $e) {
$cache = new Cache(new None());
}

$database = new Database(new Postgres($pdo), $cache);
$database
Expand Down
12 changes: 9 additions & 3 deletions tests/e2e/Adapter/SQLiteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PDO;
use Redis;
use Utopia\Cache\Adapter\None;
use Utopia\Cache\Adapter\Redis as RedisAdapter;
use Utopia\Cache\Cache;
use Utopia\Database\Adapter\SQLite;
Expand Down Expand Up @@ -45,9 +46,14 @@ public static function getDatabase(): Database
//$dsn = 'memory'; // Overwrite for fast tests
$pdo = new PDO("sqlite:" . $dsn, null, null, SQLite::getPDOAttributes());

$redis = new Redis();
$redis->connect('redis');
$redis->flushAll();
try {
$redis = new Redis();
$redis->connect('redis', 6379);
$redis->flushAll();
$cache = new Cache(new RedisAdapter($redis));
} catch (\Exception $e) {
$cache = new Cache(new None());
}

$cache = new Cache(new RedisAdapter($redis));

Expand Down

0 comments on commit a5b8017

Please sign in to comment.