From 1694a972bc7cf86f6c56aaa8260ebc0833e7c9b3 Mon Sep 17 00:00:00 2001 From: codex Date: Thu, 29 Oct 2026 17:05:35 +0800 Subject: [PATCH] fix(vpc): serialize host route dispatch --- web/app/Services/Cloud/CloudJobService.php | 54 +++++++++++ web/app/Services/Cloud/CloudWorkerService.php | 1 + web/tests/CloudVpcHostDispatchQueueTest.php | 93 +++++++++++++++++++ 3 files changed, 148 insertions(+) create mode 100644 web/tests/CloudVpcHostDispatchQueueTest.php diff --git a/web/app/Services/Cloud/CloudJobService.php b/web/app/Services/Cloud/CloudJobService.php index a6874379..c3e3a916 100644 --- a/web/app/Services/Cloud/CloudJobService.php +++ b/web/app/Services/Cloud/CloudJobService.php @@ -514,6 +514,60 @@ final class CloudJobService return $query; } + /** + * Serialize host-global VPC route mutations before Agent admission. + * Independent VM-port jobs remain concurrent. A terminal route with an + * unresolved effect or finalization incident keeps the host lane fenced. + */ + public static function applyVpcHostDispatchFence($query) + { + $routeKinds = ['vpc_host_apply', 'vpc_host_remove']; + return $query->where(static function ($eligible) use ( + $routeKinds + ): void { + $eligible->whereNotIn('cloud_jobs.kind', $routeKinds) + ->orWhereNotExists(static function ($predecessors) use ( + $routeKinds + ): void { + $predecessors->selectRaw('1') + ->from('cloud_jobs as vpc_route_predecessors') + ->whereColumn( + 'vpc_route_predecessors.host_id', + 'cloud_jobs.host_id' + ) + ->whereColumn( + 'vpc_route_predecessors.id', + '<', + 'cloud_jobs.id' + ) + ->whereIn( + 'vpc_route_predecessors.kind', + $routeKinds + ) + ->where(static function ($blocking): void { + $blocking->where( + 'vpc_route_predecessors.phase', + '<>', + 'terminal' + )->orWhere(static function ($terminal): void { + $terminal->where( + 'vpc_route_predecessors.phase', + 'terminal' + )->where(static function ($unresolved): void { + $unresolved->where( + 'vpc_route_predecessors.effect_resolution', + 'unknown' + )->orWhere( + 'vpc_route_predecessors.finalization_incident', + 1 + ); + }); + }); + }); + }); + }); + } + /** * A stock-mode switch cannot cross an upgrade that still owns either the * source offer or the target offer. The offer row is locked by the caller, diff --git a/web/app/Services/Cloud/CloudWorkerService.php b/web/app/Services/Cloud/CloudWorkerService.php index 485e4d96..763f8d23 100644 --- a/web/app/Services/Cloud/CloudWorkerService.php +++ b/web/app/Services/Cloud/CloudWorkerService.php @@ -232,6 +232,7 @@ final class CloudWorkerService $now )->where('available_at', '<=', $now) ->where('lease_until', '<=', $now); + CloudJobService::applyVpcHostDispatchFence($query); if ($this->policyOnly) { $query->whereIn('kind', self::POLICY_KINDS); } elseif ($this->vpcBatchOnly) { diff --git a/web/tests/CloudVpcHostDispatchQueueTest.php b/web/tests/CloudVpcHostDispatchQueueTest.php new file mode 100644 index 00000000..c761de94 --- /dev/null +++ b/web/tests/CloudVpcHostDispatchQueueTest.php @@ -0,0 +1,93 @@ +addConnection(['driver' => 'sqlite', 'database' => ':memory:'], + 'default'); +$capsule->setAsGlobal(); +$capsule->bootEloquent(); +$capsule->schema('default')->create('cloud_jobs', static function ( + Blueprint $table +): void { + $table->increments('id'); + $table->integer('host_id')->nullable(); + $table->string('kind'); + $table->string('phase'); + $table->string('effect_resolution')->default('not_applicable'); + $table->boolean('finalization_incident')->default(0); +}); + +$insert = static function (int $id, int $hostId, string $kind): void { + Capsule::table('cloud_jobs')->insert([ + 'id' => $id, + 'host_id' => $hostId, + 'kind' => $kind, + 'phase' => 'accepted', + 'effect_resolution' => 'not_applicable', + 'finalization_incident' => 0, + ]); +}; +$insert(1, 10, 'vpc_host_apply'); +$insert(2, 10, 'vpc_host_remove'); +$insert(3, 11, 'vpc_host_apply'); +$insert(4, 10, 'vpc_port_attach'); +$insert(5, 10, 'vpc_host_apply'); + +$eligibleIds = static function (): array { + $query = CloudJob::query()->where('phase', 'accepted'); + CloudJobService::applyVpcHostDispatchFence($query); + return $query->orderBy('id')->pluck('id')->map( + static fn ($id): int => (int) $id + )->all(); +}; + +$assert($eligibleIds() === [1, 3, 4], + 'Same-host route successors were not serialized by durable job order.'); + +Capsule::table('cloud_jobs')->where('id', 1)->update([ + 'phase' => 'terminal', + 'effect_resolution' => 'not_applied', +]); +$assert($eligibleIds() === [2, 3, 4], + 'Known-not-applied predecessor did not release the next host route job.'); + +Capsule::table('cloud_jobs')->where('id', 1)->update([ + 'effect_resolution' => 'unknown', +]); +$assert($eligibleIds() === [3, 4], + 'Unknown-effect route predecessor did not fence its host lane.'); + +Capsule::table('cloud_jobs')->where('id', 1)->update([ + 'effect_resolution' => 'applied', + 'finalization_incident' => 1, +]); +$assert($eligibleIds() === [3, 4], + 'Finalization incident did not retain the host route fence.'); + +Capsule::table('cloud_jobs')->where('id', 1)->update([ + 'finalization_incident' => 0, +]); +$assert($eligibleIds() === [2, 3, 4], + 'Resolved route predecessor did not release exactly one host successor.'); + +$worker = file_get_contents( + dirname(__DIR__) . '/app/Services/Cloud/CloudWorkerService.php' +); +$assert(is_string($worker) + && str_contains($worker, + 'CloudJobService::applyVpcHostDispatchFence($query);'), + 'The worker claim path does not apply the host VPC dispatch fence.'); + +echo "Host-scoped VPC route dispatch queue passed.\n";