feat(storage): allow controlled backend replacement

go
codex 3 weeks ago
parent 369c47d52d
commit beafadc16b

@ -6,15 +6,17 @@ use App\Models\CloudStorageBackend;
use App\Models\CloudStorageReplica;
/**
* Transactional registry for the two object-storage failure domains.
* Transactional registry for object-storage failure domains.
*
* This service owns configuration and health only. It deliberately does not
* change the legacy runtime facade or copy/delete provider objects.
* Historical or failed rows remain addressable for exact reconciliation;
* only two backends may be active for new writes at the same time.
*/
final class CloudObjectStorageBackendRegistry
{
private const CREDENTIAL_PURPOSE = 'object-storage-backend-credentials';
private const MAX_BACKENDS = 2;
private const MAX_ACTIVE_BACKENDS = 2;
/** @return list<array<string,mixed>> */
public static function listView(): array
@ -53,9 +55,6 @@ final class CloudObjectStorageBackendRegistry
self::lockCatalog($connection);
$rows = CloudStorageBackend::query()->where('admin_state', '<>', 'retired')
->orderBy('id')->lockForUpdate()->get();
if ($rows->count() >= self::MAX_BACKENDS) {
throw new CloudError('当前版本最多配置两个对象存储后端', 409);
}
if ($rows->contains(static fn (CloudStorageBackend $row): bool =>
hash_equals((string) $row->identity_sha256, $spec['identity_sha256']))) {
throw new CloudError('相同 Endpoint、Bucket 和前缀的对象存储已存在', 409);
@ -269,11 +268,6 @@ final class CloudObjectStorageBackendRegistry
}
return $match;
}
$rows = CloudStorageBackend::query()->where('admin_state', '<>', 'retired')
->orderBy('id')->lockForUpdate()->get();
if ($rows->count() >= self::MAX_BACKENDS) {
throw new CloudError('当前版本最多配置两个对象存储后端', 409);
}
$now = time();
$revision = max(1, (int) ($view['revision'] ?? 0));
$ready = (bool) ($view['ready'] ?? false);
@ -332,14 +326,20 @@ final class CloudObjectStorageBackendRegistry
self::assertRevision($expectedRevision);
$connection = (new CloudStorageBackend())->getConnection();
$backend = $connection->transaction(static function () use (
$connection,
$backendId,
$expectedRevision,
$allowed,
$target,
$requireHealthy
): CloudStorageBackend {
$backend = CloudStorageBackend::query()->whereKey($backendId)
->lockForUpdate()->first();
self::lockCatalog($connection);
$backends = CloudStorageBackend::query()
->where('admin_state', '<>', 'retired')
->orderBy('id')->lockForUpdate()->get();
$backend = $backends->first(static fn (
CloudStorageBackend $row
): bool => (int) $row->id === $backendId);
if (!$backend instanceof CloudStorageBackend) {
throw new CloudError('对象存储后端不存在', 404);
}
@ -350,6 +350,18 @@ final class CloudObjectStorageBackendRegistry
if ($requireHealthy && !self::hasCurrentHealth($backend)) {
throw new CloudError('请先通过当前配置的连接测试', 409);
}
if ($target === 'active') {
$otherActive = $backends->filter(static fn (
CloudStorageBackend $row
): bool => (int) $row->id !== $backendId
&& (string) $row->admin_state === 'active')->count();
if ($otherActive >= self::MAX_ACTIVE_BACKENDS) {
throw new CloudError(
'同时最多启用两个对象存储后端,请先排空或停用一个后端',
409
);
}
}
$oldRevision = (int) $backend->config_revision;
$wasTested = (int) $backend->tested_revision === $oldRevision;
$backend->config_revision = $oldRevision + 1;

@ -194,14 +194,25 @@ namespace {
&& CloudObjectStorageBackendRegistry::summary()['writable_backends'] === 2,
'Explicit activation did not expose two healthy writable backends.');
$expectCloudError(static fn () => CloudObjectStorageBackendRegistry::create(
$third = CloudObjectStorageBackendRegistry::create(
array_replace($secondaryInput, [
'name' => 'Third',
'endpoint' => 'https://third.storage.example',
'bucket' => 'third-bucket',
]),
1
);
$assert($third['admin_state'] === 'draft'
&& CloudStorageBackend::count() === 3,
'A replacement backend could not be registered beside two live rows.');
CloudObjectStorageBackendRegistry::test(
(int) $third['id'], 1, 1, $s3(200)
);
$expectCloudError(static fn () => CloudObjectStorageBackendRegistry::activate(
(int) $third['id'], 1, 1
), 409);
$assert(CloudObjectStorageBackendRegistry::summary()['active_backends'] === 2,
'The registry allowed more than two active storage backends.');
Capsule::table('cloud_storage_replicas')->insert([
'backend_id' => (int) $secondary['id'],

Loading…
Cancel
Save