You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
110 lines
4.3 KiB
PHP
110 lines
4.3 KiB
PHP
<?php
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
use App\Services\Cloud\CloudStorageProtectionSummaryService;
|
|
use Illuminate\Database\Capsule\Manager as Capsule;
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
|
|
$assert = static function (bool $condition, string $message): void {
|
|
if (!$condition) {
|
|
throw new RuntimeException($message);
|
|
}
|
|
};
|
|
|
|
$db = new Capsule();
|
|
$db->addConnection(['driver' => 'sqlite', 'database' => ':memory:']);
|
|
$db->setAsGlobal();
|
|
$db->bootEloquent();
|
|
$db->schema()->create('cloud_storage_artifacts', static function (Blueprint $t): void {
|
|
$t->increments('id');
|
|
$t->unsignedTinyInteger('desired_copies');
|
|
$t->unsignedTinyInteger('required_copies');
|
|
$t->unsignedBigInteger('size_bytes');
|
|
$t->string('sha256');
|
|
$t->string('state');
|
|
$t->unsignedBigInteger('created_at');
|
|
});
|
|
$db->schema()->create('cloud_storage_replicas', static function (Blueprint $t): void {
|
|
$t->increments('id');
|
|
$t->unsignedBigInteger('artifact_id');
|
|
$t->unsignedBigInteger('size_bytes');
|
|
$t->string('sha256');
|
|
$t->unsignedBigInteger('verified_at');
|
|
$t->string('state');
|
|
});
|
|
|
|
$now = 2_000;
|
|
$artifacts = [
|
|
['id' => 1, 'desired_copies' => 2, 'required_copies' => 1,
|
|
'size_bytes' => 100, 'sha256' => str_repeat('a', 64),
|
|
'state' => 'protected', 'created_at' => 1_800],
|
|
['id' => 2, 'desired_copies' => 2, 'required_copies' => 1,
|
|
'size_bytes' => 200, 'sha256' => str_repeat('b', 64),
|
|
'state' => 'available', 'created_at' => 1_700],
|
|
['id' => 3, 'desired_copies' => 2, 'required_copies' => 2,
|
|
'size_bytes' => 300, 'sha256' => str_repeat('c', 64),
|
|
'state' => 'publishing', 'created_at' => 1_600],
|
|
['id' => 4, 'desired_copies' => 2, 'required_copies' => 1,
|
|
'size_bytes' => 400, 'sha256' => str_repeat('d', 64),
|
|
'state' => 'retiring', 'created_at' => 1_000],
|
|
];
|
|
Capsule::table('cloud_storage_artifacts')->insert($artifacts);
|
|
Capsule::table('cloud_storage_replicas')->insert([
|
|
['artifact_id' => 1, 'size_bytes' => 100,
|
|
'sha256' => str_repeat('a', 64), 'verified_at' => 1,
|
|
'state' => 'ready'],
|
|
['artifact_id' => 1, 'size_bytes' => 100,
|
|
'sha256' => str_repeat('a', 64), 'verified_at' => 2,
|
|
'state' => 'ready'],
|
|
['artifact_id' => 2, 'size_bytes' => 200,
|
|
'sha256' => str_repeat('b', 64), 'verified_at' => 3,
|
|
'state' => 'ready'],
|
|
['artifact_id' => 2, 'size_bytes' => 0, 'sha256' => '',
|
|
'verified_at' => 0, 'state' => 'copying'],
|
|
// State alone must not count a corrupted or stale identity as coverage.
|
|
['artifact_id' => 3, 'size_bytes' => 301,
|
|
'sha256' => str_repeat('c', 64), 'verified_at' => 4,
|
|
'state' => 'ready'],
|
|
['artifact_id' => 4, 'size_bytes' => 400,
|
|
'sha256' => str_repeat('d', 64), 'verified_at' => 5,
|
|
'state' => 'ready'],
|
|
]);
|
|
|
|
$summary = CloudStorageProtectionSummaryService::summary($now);
|
|
$assert($summary === [
|
|
'protected_artifact_count' => 1,
|
|
'degraded_artifact_count' => 1,
|
|
'replication_backlog_count' => 2,
|
|
'oldest_replication_age_seconds' => 400,
|
|
], 'Replica coverage summary does not match verified ready replicas.');
|
|
|
|
Capsule::table('cloud_storage_replicas')->where('artifact_id', 2)
|
|
->where('state', 'copying')->update([
|
|
'state' => 'ready', 'size_bytes' => 200,
|
|
'sha256' => str_repeat('b', 64), 'verified_at' => 6,
|
|
]);
|
|
Capsule::table('cloud_storage_replicas')->where('artifact_id', 3)
|
|
->where('state', 'ready')->update([
|
|
'size_bytes' => 300, 'sha256' => str_repeat('c', 64),
|
|
'verified_at' => 7,
|
|
]);
|
|
$recovered = CloudStorageProtectionSummaryService::summary($now);
|
|
$assert($recovered['protected_artifact_count'] === 2
|
|
&& $recovered['degraded_artifact_count'] === 0
|
|
&& $recovered['replication_backlog_count'] === 1
|
|
&& $recovered['oldest_replication_age_seconds'] === 400,
|
|
'Summary did not converge when replicas became ready.');
|
|
|
|
Capsule::table('cloud_storage_artifacts')->whereIn('id', [1, 2, 3])
|
|
->update(['state' => 'deleted']);
|
|
$empty = CloudStorageProtectionSummaryService::summary($now);
|
|
$assert($empty === [
|
|
'protected_artifact_count' => 0,
|
|
'degraded_artifact_count' => 0,
|
|
'replication_backlog_count' => 0,
|
|
'oldest_replication_age_seconds' => 0,
|
|
], 'Deleted artifacts leaked into the protection overview.');
|
|
|
|
echo "Object-storage replica protection summary passed.\n";
|