feat(storage): probe backend health independently
parent
adbaa7539e
commit
a03056ad4b
@ -0,0 +1,25 @@
|
||||
[Unit]
|
||||
Description=Virt Cloud object storage health probe worker
|
||||
After=network-online.target
|
||||
Wants=network-online.target
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
User=apache
|
||||
Group=apache
|
||||
WorkingDirectory=/home/virt-cloud/web
|
||||
ExecStart=/usr/bin/php /home/virt-cloud/web/xcat cloudStorageHealthWorker
|
||||
Restart=on-failure
|
||||
RestartSec=5
|
||||
LimitCORE=0
|
||||
UMask=0077
|
||||
NoNewPrivileges=true
|
||||
PrivateTmp=true
|
||||
ProtectHome=false
|
||||
ProtectSystem=strict
|
||||
ReadWritePaths=-/home/virt-cloud/runtime/php
|
||||
ReadWritePaths=-/home/virt-cloud/web/storage
|
||||
TimeoutStopSec=30
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace App\Command;
|
||||
|
||||
use App\Services\Cloud\CloudObjectStorageHealthProbeService;
|
||||
use App\Services\Cloud\CloudRuntimeHeartbeat;
|
||||
|
||||
/** Short periodic provider probes, intentionally isolated from replica copy. */
|
||||
final class CloudStorageHealthWorker
|
||||
{
|
||||
public static function run(
|
||||
array $argv = [],
|
||||
?CloudRuntimeHeartbeat $heartbeat = null
|
||||
): int {
|
||||
$once = in_array('--once', $argv, true);
|
||||
$heartbeat ??= CloudRuntimeHeartbeat::start('storage_health', $once);
|
||||
$unresolved = false;
|
||||
try {
|
||||
$worker = new CloudObjectStorageHealthProbeService();
|
||||
do {
|
||||
$heartbeat->busy('storage_health_probe');
|
||||
try {
|
||||
$worked = $worker->tick();
|
||||
$errors = $worker->observationErrors();
|
||||
} catch (\Throwable $error) {
|
||||
$errors = ['storage_health_failed'];
|
||||
$heartbeat->error('storage_health_failed');
|
||||
fwrite(STDERR, 'storage health worker temporarily unavailable: '
|
||||
. get_class($error) . PHP_EOL);
|
||||
if ($once) {
|
||||
return 1;
|
||||
}
|
||||
$worked = false;
|
||||
}
|
||||
if ($errors === []) {
|
||||
if ($worked) {
|
||||
$unresolved = false;
|
||||
$heartbeat->success();
|
||||
} elseif (!$unresolved) {
|
||||
$heartbeat->success();
|
||||
}
|
||||
} else {
|
||||
$unresolved = true;
|
||||
$heartbeat->error($errors[0]);
|
||||
}
|
||||
if (!$once && !$worked) {
|
||||
usleep(1000000);
|
||||
}
|
||||
} while (!$once);
|
||||
} catch (\Throwable $error) {
|
||||
$heartbeat->error('storage_health_failed');
|
||||
throw $error;
|
||||
} finally {
|
||||
$heartbeat->stopped();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,209 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Cloud;
|
||||
|
||||
use App\Models\CloudStorageBackend;
|
||||
|
||||
/** Periodic read-only provider checks, isolated from long replica copies. */
|
||||
final class CloudObjectStorageHealthProbeService
|
||||
{
|
||||
private const CLAIM_SECONDS = 45;
|
||||
private const HEALTHY_INTERVAL_SECONDS = 300;
|
||||
private \Closure $runtimeResolver;
|
||||
private \Closure $headBucket;
|
||||
private \Closure $clock;
|
||||
/** @var list<string> */
|
||||
private array $observationErrors = [];
|
||||
|
||||
public function __construct(
|
||||
?callable $runtimeResolver = null,
|
||||
?callable $headBucket = null,
|
||||
?callable $clock = null
|
||||
) {
|
||||
$this->runtimeResolver = \Closure::fromCallable(
|
||||
$runtimeResolver ?? static fn (int $backendId): array =>
|
||||
CloudObjectStorageBackendRegistry::runtime($backendId, false)
|
||||
);
|
||||
$this->headBucket = \Closure::fromCallable(
|
||||
$headBucket ?? static function (array $config, array $credentials): void {
|
||||
(new CloudObjectStorageS3Client())->headBucket($config, $credentials);
|
||||
}
|
||||
);
|
||||
$this->clock = \Closure::fromCallable($clock ?? 'time');
|
||||
}
|
||||
|
||||
/** Claim and inspect at most one due backend. */
|
||||
public function tick(): bool
|
||||
{
|
||||
$this->observationErrors = [];
|
||||
$claim = $this->claimDueBackend();
|
||||
if ($claim === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
$runtime = ($this->runtimeResolver)($claim['backend_id']);
|
||||
if ((int) ($runtime['backend_id'] ?? 0) !== $claim['backend_id']
|
||||
|| (int) ($runtime['revision'] ?? 0) !== $claim['revision']) {
|
||||
$this->releaseChangedRevision($claim);
|
||||
return true;
|
||||
}
|
||||
// Deliberately outside every database transaction. Credentials
|
||||
// exist only in this call frame and are never persisted or logged.
|
||||
($this->headBucket)($runtime['config'], $runtime['credentials']);
|
||||
$this->settleSuccess($claim);
|
||||
} catch (CloudObjectStorageTestError $error) {
|
||||
$this->settleFailure($claim, $error->publicCode);
|
||||
$this->observationErrors[] = 'storage_backend_unhealthy';
|
||||
} catch (\Throwable $error) {
|
||||
// Runtime/decryption/client failures are bounded to a public code;
|
||||
// exception messages can contain deployment details and stay local.
|
||||
$this->settleFailure($claim, 'request_rejected');
|
||||
$this->observationErrors[] = 'storage_health_failed';
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/** @return list<string> */
|
||||
public function observationErrors(): array
|
||||
{
|
||||
return $this->observationErrors;
|
||||
}
|
||||
|
||||
/** @return array{backend_id:int,revision:int,claim_until:int}|null */
|
||||
private function claimDueBackend(): ?array
|
||||
{
|
||||
$now = $this->now();
|
||||
$connection = (new CloudStorageBackend())->getConnection();
|
||||
return $connection->transaction(static function () use ($now): ?array {
|
||||
$backend = CloudStorageBackend::query()
|
||||
->where('admin_state', '<>', 'retired')
|
||||
->where(static function ($query) use ($now): void {
|
||||
$query->where(static function ($due) use ($now): void {
|
||||
$due->where('next_probe_at', '>', 0)
|
||||
->where('next_probe_at', '<=', $now);
|
||||
})->orWhere(static function ($bootstrap): void {
|
||||
// Earlier registry imports can carry valid current
|
||||
// health evidence without a first periodic schedule.
|
||||
$bootstrap->where('next_probe_at', 0)
|
||||
->where('health_state', 'healthy')
|
||||
->where('tested_revision', '>', 0)
|
||||
->whereColumn('tested_revision', 'config_revision');
|
||||
});
|
||||
})
|
||||
->orderBy('next_probe_at')->orderBy('id')
|
||||
->lockForUpdate()->first();
|
||||
if (!$backend instanceof CloudStorageBackend) {
|
||||
return null;
|
||||
}
|
||||
$claimUntil = $now + self::CLAIM_SECONDS;
|
||||
$backend->next_probe_at = $claimUntil;
|
||||
$backend->updated_at = $now;
|
||||
$backend->save();
|
||||
return [
|
||||
'backend_id' => (int) $backend->id,
|
||||
'revision' => (int) $backend->config_revision,
|
||||
'claim_until' => $claimUntil,
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
/** @param array{backend_id:int,revision:int,claim_until:int} $claim */
|
||||
private function settleSuccess(array $claim): bool
|
||||
{
|
||||
$now = $this->now();
|
||||
return $this->settle($claim, static function (
|
||||
CloudStorageBackend $backend
|
||||
) use ($now): void {
|
||||
$backend->tested_revision = (int) $backend->config_revision;
|
||||
$backend->health_state = 'healthy';
|
||||
$backend->consecutive_failures = 0;
|
||||
$backend->consecutive_successes =
|
||||
(int) $backend->consecutive_successes + 1;
|
||||
$backend->last_error_code = '';
|
||||
$backend->last_tested_at = $now;
|
||||
$backend->last_healthy_at = $now;
|
||||
$backend->next_probe_at = $now + self::HEALTHY_INTERVAL_SECONDS;
|
||||
$backend->updated_at = $now;
|
||||
// admin_state is intentionally untouched. A successful probe can
|
||||
// restore evidence, but cannot override an administrator decision.
|
||||
});
|
||||
}
|
||||
|
||||
/** @param array{backend_id:int,revision:int,claim_until:int} $claim */
|
||||
private function settleFailure(array $claim, string $code): bool
|
||||
{
|
||||
$now = $this->now();
|
||||
return $this->settle($claim, static function (
|
||||
CloudStorageBackend $backend
|
||||
) use ($now, $code): void {
|
||||
$backend->tested_revision = 0;
|
||||
$backend->health_state = in_array($code, [
|
||||
'endpoint_unreachable', 'service_unavailable', 'rate_limited',
|
||||
], true) ? 'unreachable' : 'degraded';
|
||||
$failures = (int) $backend->consecutive_failures + 1;
|
||||
$backend->consecutive_failures = $failures;
|
||||
$backend->consecutive_successes = 0;
|
||||
$backend->last_error_code = $code;
|
||||
$backend->last_tested_at = $now;
|
||||
$backend->next_probe_at = $now + min(
|
||||
3600,
|
||||
60 * (2 ** min(5, $failures - 1))
|
||||
);
|
||||
if ((string) $backend->admin_state === 'active') {
|
||||
$backend->admin_state = 'read_only';
|
||||
}
|
||||
$backend->updated_at = $now;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array{backend_id:int,revision:int,claim_until:int} $claim
|
||||
* @param callable(CloudStorageBackend):void $mutation
|
||||
*/
|
||||
private function settle(array $claim, callable $mutation): bool
|
||||
{
|
||||
$connection = (new CloudStorageBackend())->getConnection();
|
||||
return $connection->transaction(static function () use (
|
||||
$claim,
|
||||
$mutation
|
||||
): bool {
|
||||
$backend = CloudStorageBackend::query()
|
||||
->whereKey($claim['backend_id'])->lockForUpdate()->first();
|
||||
if (!$backend instanceof CloudStorageBackend
|
||||
|| (int) $backend->config_revision !== $claim['revision']
|
||||
|| (int) $backend->next_probe_at !== $claim['claim_until']) {
|
||||
return false;
|
||||
}
|
||||
$mutation($backend);
|
||||
$backend->save();
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
/** @param array{backend_id:int,revision:int,claim_until:int} $claim */
|
||||
private function releaseChangedRevision(array $claim): void
|
||||
{
|
||||
$now = $this->now();
|
||||
$connection = (new CloudStorageBackend())->getConnection();
|
||||
$connection->transaction(static function () use ($claim, $now): void {
|
||||
$backend = CloudStorageBackend::query()
|
||||
->whereKey($claim['backend_id'])->lockForUpdate()->first();
|
||||
if ($backend instanceof CloudStorageBackend
|
||||
&& (int) $backend->config_revision !== $claim['revision']
|
||||
&& (int) $backend->next_probe_at === $claim['claim_until']) {
|
||||
// Metadata-only transitions preserve the probe schedule. Let
|
||||
// their new revision be inspected immediately. Runtime edits
|
||||
// already reset next_probe_at to zero and do not match here.
|
||||
$backend->next_probe_at = $now;
|
||||
$backend->updated_at = $now;
|
||||
$backend->save();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private function now(): int
|
||||
{
|
||||
return max(1, (int) ($this->clock)());
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,250 @@
|
||||
<?php
|
||||
|
||||
require dirname(__DIR__) . '/vendor/autoload.php';
|
||||
|
||||
use App\Models\CloudStorageBackend;
|
||||
use App\Services\Cloud\CloudObjectStorageHealthProbeService;
|
||||
use App\Services\Cloud\CloudObjectStorageTestError;
|
||||
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);
|
||||
}
|
||||
};
|
||||
|
||||
$capsule = new Capsule();
|
||||
$capsule->addConnection(['driver' => 'sqlite', 'database' => ':memory:']);
|
||||
$capsule->setAsGlobal();
|
||||
$capsule->bootEloquent();
|
||||
$capsule->schema()->create('cloud_storage_backends', static function (
|
||||
Blueprint $table
|
||||
): void {
|
||||
$table->increments('id');
|
||||
$table->string('backend_key')->unique();
|
||||
$table->string('identity_sha256')->unique();
|
||||
$table->string('name');
|
||||
$table->string('driver');
|
||||
$table->string('endpoint');
|
||||
$table->string('region');
|
||||
$table->string('bucket');
|
||||
$table->string('prefix');
|
||||
$table->boolean('path_style');
|
||||
$table->string('failure_domain');
|
||||
$table->text('sealed_credentials');
|
||||
$table->unsignedBigInteger('config_revision');
|
||||
$table->unsignedBigInteger('tested_revision');
|
||||
$table->string('admin_state');
|
||||
$table->string('health_state');
|
||||
$table->unsignedInteger('write_priority');
|
||||
$table->unsignedInteger('consecutive_failures');
|
||||
$table->unsignedInteger('consecutive_successes');
|
||||
$table->string('last_error_code');
|
||||
$table->unsignedBigInteger('last_tested_at');
|
||||
$table->unsignedBigInteger('last_healthy_at');
|
||||
$table->unsignedBigInteger('next_probe_at');
|
||||
$table->unsignedBigInteger('created_at');
|
||||
$table->unsignedBigInteger('updated_at');
|
||||
});
|
||||
|
||||
$now = 2000000000;
|
||||
$clock = static function () use (&$now): int { return $now; };
|
||||
$insert = static function (array $values = []) use (&$now): int {
|
||||
$id = CloudStorageBackend::query()->max('id') + 1;
|
||||
return (int) Capsule::table('cloud_storage_backends')->insertGetId(array_replace([
|
||||
'backend_key' => str_pad(dechex($id), 32, '0', STR_PAD_LEFT),
|
||||
'identity_sha256' => str_repeat(dechex(($id % 15) + 1), 64),
|
||||
'name' => 'Backend ' . $id,
|
||||
'driver' => 's3',
|
||||
'endpoint' => 'https://storage-' . $id . '.example.test',
|
||||
'region' => 'test-1',
|
||||
'bucket' => 'bucket-' . $id,
|
||||
'prefix' => 'virt',
|
||||
'path_style' => 0,
|
||||
'failure_domain' => 'provider-' . $id,
|
||||
'sealed_credentials' => 'sealed-marker-' . $id,
|
||||
'config_revision' => 3,
|
||||
'tested_revision' => 3,
|
||||
'admin_state' => 'active',
|
||||
'health_state' => 'healthy',
|
||||
'write_priority' => 10,
|
||||
'consecutive_failures' => 0,
|
||||
'consecutive_successes' => 1,
|
||||
'last_error_code' => '',
|
||||
'last_tested_at' => $now - 600,
|
||||
'last_healthy_at' => $now - 600,
|
||||
'next_probe_at' => $now - 1,
|
||||
'created_at' => $now - 1000,
|
||||
'updated_at' => $now - 600,
|
||||
], $values));
|
||||
};
|
||||
$runtime = static function (int $backendId): array {
|
||||
$row = CloudStorageBackend::query()->findOrFail($backendId);
|
||||
return [
|
||||
'backend_id' => $backendId,
|
||||
'revision' => (int) $row->config_revision,
|
||||
'config' => [
|
||||
'endpoint' => (string) $row->endpoint,
|
||||
'region' => (string) $row->region,
|
||||
'bucket' => (string) $row->bucket,
|
||||
'prefix' => (string) $row->prefix,
|
||||
'path_style' => (bool) $row->path_style,
|
||||
],
|
||||
'credentials' => [
|
||||
'access_key_id' => 'runtime-access-key',
|
||||
'secret_access_key' => 'runtime-secret-key',
|
||||
],
|
||||
];
|
||||
};
|
||||
$clear = static function (): void {
|
||||
Capsule::table('cloud_storage_backends')->delete();
|
||||
};
|
||||
|
||||
// Successful probes restore current health evidence but preserve every manual
|
||||
// non-active state. Provider work must never execute inside a transaction.
|
||||
foreach (['read_only', 'draining', 'disabled'] as $manualState) {
|
||||
$clear();
|
||||
$backendId = $insert([
|
||||
'admin_state' => $manualState,
|
||||
'health_state' => 'unreachable',
|
||||
'tested_revision' => 0,
|
||||
'consecutive_failures' => 2,
|
||||
'consecutive_successes' => 0,
|
||||
'last_error_code' => 'service_unavailable',
|
||||
]);
|
||||
$providerCalls = 0;
|
||||
$service = new CloudObjectStorageHealthProbeService(
|
||||
static function (int $id) use ($runtime, $assert, $backendId): array {
|
||||
$assert($id === $backendId
|
||||
&& Capsule::connection()->transactionLevel() === 0,
|
||||
'Runtime credentials were resolved inside a claim transaction.');
|
||||
return $runtime($id);
|
||||
},
|
||||
static function (array $config, array $credentials) use (
|
||||
&$providerCalls,
|
||||
$assert
|
||||
): void {
|
||||
$providerCalls++;
|
||||
$assert(Capsule::connection()->transactionLevel() === 0,
|
||||
'HeadBucket ran inside a database transaction.');
|
||||
$assert($config['bucket'] !== ''
|
||||
&& $credentials['secret_access_key'] === 'runtime-secret-key',
|
||||
'Probe did not receive the frozen backend runtime.');
|
||||
},
|
||||
$clock
|
||||
);
|
||||
$assert($service->tick() && $providerCalls === 1
|
||||
&& $service->observationErrors() === [],
|
||||
'A due healthy backend was not probed exactly once.');
|
||||
$row = CloudStorageBackend::query()->findOrFail($backendId);
|
||||
$assert($row->admin_state === $manualState
|
||||
&& $row->health_state === 'healthy'
|
||||
&& $row->tested_revision === $row->config_revision
|
||||
&& $row->consecutive_failures === 0
|
||||
&& $row->consecutive_successes === 1
|
||||
&& $row->last_error_code === ''
|
||||
&& $row->last_tested_at === $now
|
||||
&& $row->last_healthy_at === $now
|
||||
&& $row->next_probe_at === $now + 300,
|
||||
'Success changed manual state or failed to restore current health evidence.');
|
||||
}
|
||||
|
||||
// Provider failure removes the active backend from new-write placement and
|
||||
// applies bounded exponential backoff without persisting runtime credentials.
|
||||
$clear();
|
||||
$backendId = $insert(['consecutive_failures' => 2]);
|
||||
$service = new CloudObjectStorageHealthProbeService(
|
||||
$runtime,
|
||||
static function (): void {
|
||||
if (Capsule::connection()->transactionLevel() !== 0) {
|
||||
throw new RuntimeException('provider_io_in_transaction');
|
||||
}
|
||||
throw new CloudObjectStorageTestError(
|
||||
'secret provider detail must not persist',
|
||||
'endpoint_unreachable'
|
||||
);
|
||||
},
|
||||
$clock
|
||||
);
|
||||
$assert($service->tick()
|
||||
&& $service->observationErrors() === ['storage_backend_unhealthy'],
|
||||
'Provider failure was not exposed as a bounded worker observation.');
|
||||
$row = CloudStorageBackend::query()->findOrFail($backendId);
|
||||
$databaseJson = json_encode($row->toArray(), JSON_THROW_ON_ERROR);
|
||||
$assert($row->admin_state === 'read_only'
|
||||
&& $row->health_state === 'unreachable'
|
||||
&& $row->tested_revision === 0
|
||||
&& $row->consecutive_failures === 3
|
||||
&& $row->next_probe_at === $now + 240
|
||||
&& $row->last_error_code === 'endpoint_unreachable'
|
||||
&& !str_contains($databaseJson, 'runtime-secret-key')
|
||||
&& !str_contains($databaseJson, 'secret provider detail'),
|
||||
'Failure did not stop writes, apply backoff, or leaked private details.');
|
||||
|
||||
// One tick claims only one due backend. A later tick handles the next row.
|
||||
$clear();
|
||||
$firstId = $insert(['next_probe_at' => $now - 2]);
|
||||
$secondId = $insert(['next_probe_at' => $now - 1]);
|
||||
$calls = [];
|
||||
$service = new CloudObjectStorageHealthProbeService(
|
||||
$runtime,
|
||||
static function (array $config) use (&$calls): void {
|
||||
$calls[] = $config['bucket'];
|
||||
},
|
||||
$clock
|
||||
);
|
||||
$assert($service->tick() && count($calls) === 1
|
||||
&& CloudStorageBackend::query()->findOrFail($firstId)->last_tested_at === $now
|
||||
&& CloudStorageBackend::query()->findOrFail($secondId)->last_tested_at === $now - 600,
|
||||
'One health tick claimed more than one due backend.');
|
||||
|
||||
// A configuration revision changed during the remote read makes the result
|
||||
// stale. Settlement must leave the newer row untouched.
|
||||
$clear();
|
||||
$backendId = $insert();
|
||||
$service = new CloudObjectStorageHealthProbeService(
|
||||
$runtime,
|
||||
static function () use ($backendId, $now): void {
|
||||
Capsule::table('cloud_storage_backends')->where('id', $backendId)->update([
|
||||
'config_revision' => 4,
|
||||
'tested_revision' => 0,
|
||||
'health_state' => 'unknown',
|
||||
'admin_state' => 'draft',
|
||||
'last_error_code' => '',
|
||||
'updated_at' => $now + 1,
|
||||
]);
|
||||
},
|
||||
$clock
|
||||
);
|
||||
$assert($service->tick(), 'Revision-race probe did not complete its bounded attempt.');
|
||||
$row = CloudStorageBackend::query()->findOrFail($backendId);
|
||||
$assert($row->config_revision === 4 && $row->tested_revision === 0
|
||||
&& $row->health_state === 'unknown' && $row->admin_state === 'draft'
|
||||
&& $row->last_tested_at === $now - 600,
|
||||
'A stale HeadBucket result overwrote a newer backend revision.');
|
||||
|
||||
// A previously imported healthy backend may predate the periodic schedule.
|
||||
// Current tested evidence is enough to bootstrap it once; an untested draft is
|
||||
// still excluded so automation cannot bypass first-time administrator review.
|
||||
$clear();
|
||||
$importedId = $insert(['next_probe_at' => 0]);
|
||||
$draftId = $insert([
|
||||
'next_probe_at' => 0,
|
||||
'tested_revision' => 0,
|
||||
'health_state' => 'unknown',
|
||||
'admin_state' => 'draft',
|
||||
]);
|
||||
$providerCalls = 0;
|
||||
$service = new CloudObjectStorageHealthProbeService(
|
||||
$runtime,
|
||||
static function () use (&$providerCalls): void { $providerCalls++; },
|
||||
$clock
|
||||
);
|
||||
$assert($service->tick() && $providerCalls === 1
|
||||
&& CloudStorageBackend::query()->findOrFail($importedId)->next_probe_at
|
||||
=== $now + 300
|
||||
&& CloudStorageBackend::query()->findOrFail($draftId)->next_probe_at === 0,
|
||||
'Legacy current health was not scheduled or an untested draft was probed.');
|
||||
|
||||
echo "Object storage health probe service tests passed.\n";
|
||||
@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
$root = dirname(__DIR__, 2);
|
||||
$read = static function (string $path) use ($root): string {
|
||||
$contents = file_get_contents($root . '/' . $path);
|
||||
if (!is_string($contents)) {
|
||||
throw new RuntimeException('Unable to read ' . $path);
|
||||
}
|
||||
return $contents;
|
||||
};
|
||||
$assert = static function (bool $condition, string $message): void {
|
||||
if (!$condition) {
|
||||
throw new RuntimeException($message);
|
||||
}
|
||||
};
|
||||
|
||||
$xcat = $read('web/app/Command/XCat.php');
|
||||
$command = $read('web/app/Command/CloudStorageHealthWorker.php');
|
||||
$service = $read('web/app/Services/Cloud/CloudObjectStorageHealthProbeService.php');
|
||||
$heartbeat = $read('web/app/Services/Cloud/CloudRuntimeHeartbeat.php');
|
||||
$runtime = $read('web/app/Services/Cloud/CloudRuntimeHealthService.php');
|
||||
$unit = $read('deploy/virt-cloud-storage-health-worker.service');
|
||||
$replicaUnit = $read('deploy/virt-cloud-storage-replica-worker.service');
|
||||
$deployment = $read('docs/DEPLOYMENT.md');
|
||||
|
||||
$assert(str_contains($xcat, "'cloudStorageHealthWorker'")
|
||||
&& str_contains($xcat, 'CloudStorageHealthWorker::run'),
|
||||
'Health worker command is not reachable through xcat.');
|
||||
$assert(str_contains($command, 'CloudObjectStorageHealthProbeService')
|
||||
&& str_contains($command, "in_array('--once'")
|
||||
&& str_contains($command, "'storage_health'")
|
||||
&& str_contains($command, "busy('storage_health_probe')")
|
||||
&& str_contains($command, "error('storage_health_failed')"),
|
||||
'Health worker lost its independent loop, heartbeat, or once mode.');
|
||||
$assert(str_contains($service, '->lockForUpdate()->first()')
|
||||
&& str_contains($service, "where('next_probe_at', '<=', \$now)")
|
||||
&& str_contains($service, "'claim_until' => \$claimUntil")
|
||||
&& str_contains($service, "config_revision !== \$claim['revision']")
|
||||
&& str_contains($service, "next_probe_at !== \$claim['claim_until']")
|
||||
&& str_contains($service, "admin_state === 'active'")
|
||||
&& str_contains($service, "admin_state = 'read_only'")
|
||||
&& !str_contains($service, 'access_key_id')
|
||||
&& !str_contains($service, 'secret_access_key'),
|
||||
'Probe lost its one-row claim, frozen revision settlement, write fence, or credential boundary.');
|
||||
$assert(str_contains($unit,
|
||||
'ExecStart=/usr/bin/php /home/virt-cloud/web/xcat cloudStorageHealthWorker')
|
||||
&& str_contains($unit, 'NoNewPrivileges=true')
|
||||
&& str_contains($unit, 'ProtectSystem=strict')
|
||||
&& str_contains($unit, 'LimitCORE=0')
|
||||
&& !str_contains($replicaUnit, 'cloudStorageHealthWorker'),
|
||||
'Health worker systemd isolation or hardening is incomplete.');
|
||||
$assert(str_contains($heartbeat, "'storage_health'")
|
||||
&& str_contains($heartbeat, "'storage_health_probe'")
|
||||
&& str_contains($runtime, "'storage_health' => '对象存储健康探测'")
|
||||
&& str_contains($deployment, 'virt-cloud-storage-health-worker.service')
|
||||
&& str_contains($deployment, '事务外执行最长 15 秒'),
|
||||
'Health worker deployment or runtime observability is incomplete.');
|
||||
|
||||
echo "Storage health worker contract tests passed.\n";
|
||||
Loading…
Reference in New Issue