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.
137 lines
5.3 KiB
PHP
137 lines
5.3 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Cloud;
|
|
|
|
/** Deterministic placement for new writes; existing operations never reroute. */
|
|
final class CloudStoragePlacementPolicy
|
|
{
|
|
/**
|
|
* @param list<array<string,mixed>> $backends
|
|
* @return list<array<string,mixed>>
|
|
*/
|
|
public static function select(
|
|
array $backends,
|
|
int $desiredCopies,
|
|
int $requiredCopies
|
|
): array {
|
|
if ($desiredCopies < 1 || $desiredCopies > 2
|
|
|| $requiredCopies < 1 || $requiredCopies > $desiredCopies) {
|
|
throw new \InvalidArgumentException(
|
|
'Storage placement copy policy is invalid'
|
|
);
|
|
}
|
|
$eligible = [];
|
|
$healthy = [];
|
|
$seen = [];
|
|
foreach ($backends as $backend) {
|
|
$id = $backend['id'] ?? null;
|
|
$revision = $backend['config_revision'] ?? null;
|
|
$tested = $backend['tested_revision'] ?? null;
|
|
$priority = $backend['write_priority'] ?? null;
|
|
if (!is_int($id) || $id < 1 || isset($seen[$id])
|
|
|| !is_int($revision) || $revision < 1
|
|
|| !is_int($tested) || $tested !== $revision
|
|
|| !is_int($priority) || $priority < 0
|
|
|| ($backend['admin_state'] ?? null) !== 'active'
|
|
|| !in_array(($backend['health_state'] ?? null), [
|
|
'healthy', 'degraded', 'unknown', 'unreachable',
|
|
], true)) {
|
|
continue;
|
|
}
|
|
$seen[$id] = true;
|
|
$eligible[] = $backend;
|
|
if ($backend['health_state'] === 'healthy') {
|
|
$healthy[] = $backend;
|
|
}
|
|
}
|
|
usort($eligible, static fn (array $left, array $right): int =>
|
|
[$left['write_priority'], $left['id']]
|
|
<=> [$right['write_priority'], $right['id']]);
|
|
usort($healthy, static fn (array $left, array $right): int =>
|
|
[$left['write_priority'], $left['id']]
|
|
<=> [$right['write_priority'], $right['id']]);
|
|
|
|
if (count($healthy) < $requiredCopies) {
|
|
throw new CloudError('健康且已验证的对象存储后端数量不足', 503);
|
|
}
|
|
|
|
// A required-two catalog artifact is not protected when both objects
|
|
// live in the same provider/failure domain. Do not silently trade the
|
|
// frozen durability contract for placement availability. Backups use
|
|
// requiredCopies=1 and retain the best-effort same-domain fallback
|
|
// below for their asynchronous secondary.
|
|
if ($requiredCopies > 1) {
|
|
$selected = [];
|
|
$domains = [];
|
|
foreach ($healthy as $backend) {
|
|
$domain = self::domain($backend);
|
|
if (isset($domains[$domain])) {
|
|
continue;
|
|
}
|
|
$selected[] = $backend;
|
|
$domains[$domain] = true;
|
|
if (count($selected) >= $desiredCopies) {
|
|
break;
|
|
}
|
|
}
|
|
if (count($selected) < $requiredCopies) {
|
|
throw new CloudError(
|
|
'健康对象存储后端缺少独立故障域',
|
|
503
|
|
);
|
|
}
|
|
return $selected;
|
|
}
|
|
|
|
// The primary must always be writable now. For an asynchronous
|
|
// required-one artifact, however, reserve a second active/tested
|
|
// backend even while it is unhealthy. Its copy operation remains
|
|
// deferred and converges after the backend recovers instead of
|
|
// leaving the artifact permanently single-copy.
|
|
$selected = [$healthy[0]];
|
|
$chosen = [(int) $healthy[0]['id'] => true];
|
|
$domains = [self::domain($healthy[0]) => true];
|
|
$healthyRemainder = array_values(array_filter(
|
|
$healthy,
|
|
static fn (array $backend): bool =>
|
|
!isset($chosen[(int) $backend['id']])
|
|
));
|
|
$standby = array_values(array_filter(
|
|
$eligible,
|
|
static fn (array $backend): bool =>
|
|
($backend['health_state'] ?? null) !== 'healthy'
|
|
));
|
|
|
|
foreach ([$healthyRemainder, $standby] as $tier) {
|
|
foreach ([true, false] as $requireNewDomain) {
|
|
foreach ($tier as $backend) {
|
|
if (count($selected) >= $desiredCopies) {
|
|
break 3;
|
|
}
|
|
$id = (int) $backend['id'];
|
|
$domain = self::domain($backend);
|
|
if (isset($chosen[$id])
|
|
|| ($requireNewDomain && isset($domains[$domain]))) {
|
|
continue;
|
|
}
|
|
$selected[] = $backend;
|
|
$chosen[$id] = true;
|
|
$domains[$domain] = true;
|
|
}
|
|
}
|
|
}
|
|
return $selected;
|
|
}
|
|
|
|
/** @param array<string,mixed> $backend */
|
|
private static function domain(array $backend): string
|
|
{
|
|
$domain = is_string($backend['failure_domain'] ?? null)
|
|
? strtolower(trim($backend['failure_domain'])) : '';
|
|
// Missing metadata is never evidence of an independent provider.
|
|
// Registry writes reject it; every malformed legacy row deliberately
|
|
// shares the same unknown domain for required-two placement.
|
|
return $domain;
|
|
}
|
|
}
|