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.
182 lines
7.1 KiB
PHP
182 lines
7.1 KiB
PHP
<?php
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
use App\Services\Cloud\CloudError;
|
|
use App\Services\Cloud\CloudStorageDurabilityPolicy;
|
|
use App\Services\Cloud\CloudStorageReadRouter;
|
|
use App\Services\Cloud\CloudStoragePlacementPolicy;
|
|
|
|
$assert = static function (bool $condition, string $message): void {
|
|
if (!$condition) {
|
|
throw new RuntimeException($message);
|
|
}
|
|
};
|
|
$sha = str_repeat('a', 64);
|
|
$artifact = static fn (string $kind): array => [
|
|
'kind' => $kind,
|
|
'size_bytes' => 1024,
|
|
'sha256' => $sha,
|
|
] + CloudStorageDurabilityPolicy::forKind($kind) + ['state' => 'publishing'];
|
|
$replica = static fn (int $id, int $backendId, string $state = 'ready'): array => [
|
|
'id' => $id,
|
|
'backend_id' => $backendId,
|
|
'role' => $id === 1 ? 'primary' : 'secondary',
|
|
'state' => $state,
|
|
'size_bytes' => 1024,
|
|
'sha256' => $sha,
|
|
'verified_at' => $state === 'ready' ? 100 : 0,
|
|
];
|
|
|
|
$backup = $artifact('backup');
|
|
$one = CloudStorageDurabilityPolicy::project($backup, [$replica(1, 10)]);
|
|
$assert($one['state'] === 'available' && $one['can_read']
|
|
&& $one['meets_publication_requirement']
|
|
&& !$one['protected'],
|
|
'One verified backup copy must be available but not protected.');
|
|
$two = CloudStorageDurabilityPolicy::project(
|
|
$backup,
|
|
[$replica(1, 10), $replica(2, 20)]
|
|
);
|
|
$assert($two['state'] === 'protected' && $two['protected'],
|
|
'Two verified backup copies must be protected.');
|
|
|
|
$template = $artifact('template');
|
|
$oneTemplate = CloudStorageDurabilityPolicy::project(
|
|
$template,
|
|
[$replica(1, 10), $replica(2, 20, 'copying')]
|
|
);
|
|
$assert($oneTemplate['state'] === 'publishing'
|
|
&& $oneTemplate['can_read']
|
|
&& !$oneTemplate['meets_publication_requirement'],
|
|
'One exact template replica must remain readable without releasing publication.');
|
|
$twoTemplate = CloudStorageDurabilityPolicy::project(
|
|
$template,
|
|
[$replica(1, 10), $replica(2, 20)]
|
|
);
|
|
$assert($twoTemplate['state'] === 'protected'
|
|
&& $twoTemplate['can_read']
|
|
&& $twoTemplate['meets_publication_requirement'],
|
|
'A required-two template must become readable after both copies verify.');
|
|
|
|
$degradedTemplate = CloudStorageDurabilityPolicy::project(
|
|
array_replace($template, ['state' => 'protected']),
|
|
[$replica(1, 10), $replica(2, 20, 'degraded')]
|
|
);
|
|
$assert($degradedTemplate['state'] === 'degraded'
|
|
&& $degradedTemplate['can_read']
|
|
&& !$degradedTemplate['meets_publication_requirement']
|
|
&& !$degradedTemplate['protected'],
|
|
'A published template must remain readable from one verified replica while degraded.');
|
|
|
|
$rescue = $artifact('rescue');
|
|
$oneRescue = CloudStorageDurabilityPolicy::project(
|
|
$rescue,
|
|
[$replica(1, 10), $replica(2, 20, 'copying')]
|
|
);
|
|
$assert($oneRescue['can_read']
|
|
&& !$oneRescue['meets_publication_requirement'],
|
|
'One rescue replica must not satisfy its initial publication requirement.');
|
|
$noHealthyRescue = CloudStorageDurabilityPolicy::project(
|
|
$rescue,
|
|
[$replica(1, 10, 'degraded'), $replica(2, 20, 'degraded')]
|
|
);
|
|
$assert(!$noHealthyRescue['can_read']
|
|
&& !$noHealthyRescue['meets_publication_requirement'],
|
|
'A rescue artifact without a verified replica must not be readable.');
|
|
$deletingTemplate = CloudStorageDurabilityPolicy::project(
|
|
array_replace($template, ['state' => 'delete_attention']),
|
|
[$replica(1, 10), $replica(2, 20)]
|
|
);
|
|
$assert($deletingTemplate['state'] === 'delete_attention'
|
|
&& !$deletingTemplate['can_read'],
|
|
'A delete-attention artifact must not mint new read capabilities.');
|
|
|
|
$candidates = [
|
|
$replica(1, 10) + ['backend' => [
|
|
'admin_state' => 'active', 'health_state' => 'unreachable',
|
|
'write_priority' => 1,
|
|
]],
|
|
$replica(2, 20) + ['backend' => [
|
|
'admin_state' => 'draining', 'health_state' => 'healthy',
|
|
'write_priority' => 100,
|
|
]],
|
|
];
|
|
$selected = CloudStorageReadRouter::select($backup, $candidates);
|
|
$assert($selected['backend_id'] === 20,
|
|
'Reads must fail over to a verified healthy replica on a draining backend.');
|
|
|
|
$candidates[0]['backend']['health_state'] = 'healthy';
|
|
$selected = CloudStorageReadRouter::select($backup, $candidates);
|
|
$assert($selected['backend_id'] === 10,
|
|
'Reads must prefer a healthy active primary replica.');
|
|
|
|
$backends = [
|
|
['id' => 1, 'config_revision' => 2, 'tested_revision' => 2,
|
|
'admin_state' => 'active', 'health_state' => 'healthy',
|
|
'write_priority' => 10, 'failure_domain' => 'provider-a'],
|
|
['id' => 2, 'config_revision' => 1, 'tested_revision' => 1,
|
|
'admin_state' => 'active', 'health_state' => 'healthy',
|
|
'write_priority' => 30, 'failure_domain' => 'provider-b'],
|
|
['id' => 3, 'config_revision' => 1, 'tested_revision' => 1,
|
|
'admin_state' => 'active', 'health_state' => 'healthy',
|
|
'write_priority' => 20, 'failure_domain' => 'provider-a'],
|
|
];
|
|
$placed = CloudStoragePlacementPolicy::select($backends, 2, 2);
|
|
$assert(array_column($placed, 'id') === [1, 2],
|
|
'Placement must prefer a distinct failure domain over a lower-priority peer.');
|
|
$sameDomainRequired = [$backends[0], $backends[2]];
|
|
try {
|
|
CloudStoragePlacementPolicy::select($sameDomainRequired, 2, 2);
|
|
$assert(false,
|
|
'Required-two placement accepted two backends in one failure domain.');
|
|
} catch (CloudError $error) {
|
|
$assert($error->httpStatus === 503
|
|
&& $error->getMessage() === '健康对象存储后端缺少独立故障域',
|
|
'Same-domain required placement did not fail as an explicit 503.');
|
|
}
|
|
$backends[1]['health_state'] = 'unreachable';
|
|
$backupPlacement = CloudStoragePlacementPolicy::select($backends, 2, 1);
|
|
$assert(array_column($backupPlacement, 'id') === [1, 3],
|
|
'Async backup placement may use two healthy backends in one domain.');
|
|
$backends[2]['health_state'] = 'unreachable';
|
|
$backupDeferredPlacement = CloudStoragePlacementPolicy::select(
|
|
$backends,
|
|
2,
|
|
1
|
|
);
|
|
$assert(array_column($backupDeferredPlacement, 'id') === [1, 2],
|
|
'Async backup placement must reserve a deferred second failure domain.');
|
|
$backends[0]['health_state'] = 'unreachable';
|
|
$backends[1]['health_state'] = 'healthy';
|
|
$backupFailoverPlacement = CloudStoragePlacementPolicy::select(
|
|
$backends,
|
|
2,
|
|
1
|
|
);
|
|
$assert(array_column($backupFailoverPlacement, 'id') === [2, 1],
|
|
'Async backup placement must choose a healthy primary before a standby.');
|
|
$backends[0]['health_state'] = 'healthy';
|
|
$backends[1]['health_state'] = 'unreachable';
|
|
$backends[2]['tested_revision'] = 0;
|
|
try {
|
|
CloudStoragePlacementPolicy::select($backends, 2, 2);
|
|
$assert(false, 'Required-two placement accepted only one eligible backend.');
|
|
} catch (CloudError $error) {
|
|
$assert($error->httpStatus === 503,
|
|
'Insufficient required placement must be a 503 business error.');
|
|
}
|
|
|
|
$badIdentity = $candidates;
|
|
$badIdentity[0]['sha256'] = str_repeat('b', 64);
|
|
$badIdentity[1]['state'] = 'degraded';
|
|
try {
|
|
CloudStorageReadRouter::select($backup, $badIdentity);
|
|
$assert(false, 'A mismatched or non-ready replica was selected.');
|
|
} catch (CloudError $error) {
|
|
$assert($error->httpStatus === 503,
|
|
'Unavailable replica routing must be a 503 business error.');
|
|
}
|
|
|
|
echo "Storage durability policy tests passed.\n";
|