fix(storage): count only exact ready replicas

go
codex 2 weeks ago
parent 70077cef5b
commit 9eb795364d

@ -21,7 +21,18 @@ final class CloudStorageProtectionSummaryService
->from('cloud_storage_artifacts as artifacts')
->leftJoin('cloud_storage_replicas as replicas', static function ($join): void {
$join->on('replicas.artifact_id', '=', 'artifacts.id')
->where('replicas.state', '=', 'ready');
->where('replicas.state', '=', 'ready')
->whereColumn(
'replicas.size_bytes',
'=',
'artifacts.size_bytes'
)
->whereColumn(
'replicas.sha256',
'=',
'artifacts.sha256'
)
->where('replicas.verified_at', '>', 0);
})
->whereNotIn('artifacts.state', [
'retiring', 'delete_attention', 'deleted',

@ -20,34 +20,55 @@ $db->schema()->create('cloud_storage_artifacts', static function (Blueprint $t):
$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, 'state' => 'ready'],
['artifact_id' => 1, 'state' => 'ready'],
['artifact_id' => 2, 'state' => 'ready'],
['artifact_id' => 2, 'state' => 'copying'],
['artifact_id' => 3, 'state' => 'copying'],
['artifact_id' => 4, 'state' => 'ready'],
['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);
@ -59,9 +80,15 @@ $assert($summary === [
], 'Replica coverage summary does not match verified ready replicas.');
Capsule::table('cloud_storage_replicas')->where('artifact_id', 2)
->where('state', 'copying')->update(['state' => 'ready']);
->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', 'copying')->update(['state' => 'ready']);
->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

Loading…
Cancel
Save