From 12be20151d3efff77438fcfb98eb1ea7436cff2a Mon Sep 17 00:00:00 2001 From: codex Date: Sun, 8 Nov 2026 01:35:37 +0800 Subject: [PATCH] feat(backup): persist automatic backup deferrals --- web/app/Command/CloudSetup.php | 6 +- web/app/Models/CloudBackupSubscription.php | 3 + .../CloudAutomaticBackupBillingService.php | 5 + .../Cloud/CloudAutomaticBackupService.php | 146 ++++++++++++++---- web/sql/cloud.sql | 12 +- web/tests/CloudAutomaticBackupBillingTest.php | 4 + .../CloudAutomaticBackupDurableIntentTest.php | 4 + ...oudAutomaticBackupEntitlementGrantTest.php | 4 + ...loudAutomaticBackupExpiryLifecycleTest.php | 37 ++++- ...CloudAutomaticBackupRetentionGraceTest.php | 4 + .../CloudAutomaticBackupSubscriptionTest.php | 4 + web/tests/CloudBackupMigrationFenceTest.php | 44 +++++- ...blicationPreparationSchemaContractTest.php | 2 +- .../CloudBackupPublicationPreparationTest.php | 4 + web/tests/CloudBillingLifecycleTest.php | 4 + web/tests/CloudFinancialWorkflowTest.php | 2 +- web/tests/CloudFreshSchemaContractTest.php | 12 +- ...oudMaintenanceNoticeSchemaContractTest.php | 2 +- web/tests/CloudPlanUpgradeServiceTest.php | 4 + ...licIpPoolReplacementSchemaContractTest.php | 2 +- ...licNetworkTransitionSchemaContractTest.php | 2 +- .../CloudRescueCatalogSchemaContractTest.php | 2 +- ...loudRestoreMigrationLedgerContractTest.php | 2 +- web/tests/CloudRuntimeHeartbeatTest.php | 1 + ...dServiceCompensationSchemaContractTest.php | 2 +- ...udStoragePublicationSchemaContractTest.php | 2 +- .../CloudTemplateImportIntentContractTest.php | 2 +- .../CloudTrafficPoolSchemaContractTest.php | 2 +- web/tests/CloudVpcSchemaContractTest.php | 2 +- web/tests/UiCopyCatalogSchemaContractTest.php | 2 +- 30 files changed, 274 insertions(+), 50 deletions(-) diff --git a/web/app/Command/CloudSetup.php b/web/app/Command/CloudSetup.php index f9d1868c..0c7a638d 100644 --- a/web/app/Command/CloudSetup.php +++ b/web/app/Command/CloudSetup.php @@ -8,7 +8,7 @@ use App\Services\Config; /** Initializes only the clean-break PHP business and Agent log schemas. */ final class CloudSetup { - private const SCHEMA_VERSION = 'go-agent-wss-v39-ui-copy-catalog'; + private const SCHEMA_VERSION = 'go-agent-wss-v40-backup-deferral'; /** Tables installed by ss.sql before cloudInit and owned by this schema. */ private const PREINSTALLED_MAIN_TABLES = [ 'ui_copy_locales', 'ui_copy_entries', 'ui_copy_drafts', @@ -340,7 +340,8 @@ final class CloudSetup 'id', 'instance_id', 'user_id', 'offer_id', 'enabled', 'renew_next_period', 'frequency', 'weekday', 'retention_count', 'timezone', 'price', 'currency', 'price_version', - 'state_revision', 'next_run_at', + 'state_revision', 'next_run_at', 'deferred_until', + 'defer_reason', 'deferred_at', 'defer_count', 'last_scheduled_at', 'last_backup_id', 'created_at', 'updated_at', ], 'cloud_backup_period_entitlements' => [ @@ -764,6 +765,7 @@ final class CloudSetup 'chk_cloud_backup_subscription_frequency', 'chk_cloud_backup_subscription_weekday', 'chk_cloud_backup_subscription_retention', + 'chk_cloud_backup_subscription_deferral', ], 'cloud_backup_period_entitlements' => [ 'chk_cloud_backup_entitlement_source', diff --git a/web/app/Models/CloudBackupSubscription.php b/web/app/Models/CloudBackupSubscription.php index 75be62ac..1487ef14 100644 --- a/web/app/Models/CloudBackupSubscription.php +++ b/web/app/Models/CloudBackupSubscription.php @@ -19,6 +19,9 @@ final class CloudBackupSubscription extends Model 'price_version' => 'int', 'state_revision' => 'int', 'next_run_at' => 'int', + 'deferred_until' => 'int', + 'deferred_at' => 'int', + 'defer_count' => 'int', 'last_scheduled_at' => 'int', 'last_backup_id' => 'int', 'created_at' => 'int', diff --git a/web/app/Services/Cloud/CloudAutomaticBackupBillingService.php b/web/app/Services/Cloud/CloudAutomaticBackupBillingService.php index 93800ff5..4f655014 100644 --- a/web/app/Services/Cloud/CloudAutomaticBackupBillingService.php +++ b/web/app/Services/Cloud/CloudAutomaticBackupBillingService.php @@ -221,6 +221,7 @@ final class CloudAutomaticBackupBillingService (string) $subscription->frequency, (int) $subscription->weekday ); + CloudAutomaticBackupService::clearDeferredState($subscription); $subscription->updated_at = $now; $subscription->save(); @@ -354,6 +355,9 @@ final class CloudAutomaticBackupBillingService || (bool) $subscription->renew_next_period) { $subscription->enabled = false; $subscription->renew_next_period = false; + CloudAutomaticBackupService::clearDeferredState( + $subscription + ); self::advanceStateRevision($subscription); $subscription->updated_at = $now; $subscription->save(); @@ -835,6 +839,7 @@ final class CloudAutomaticBackupBillingService (string) $subscription->frequency, (int) $subscription->weekday ); + CloudAutomaticBackupService::clearDeferredState($subscription); $subscription->updated_at = $now; $subscription->save(); } diff --git a/web/app/Services/Cloud/CloudAutomaticBackupService.php b/web/app/Services/Cloud/CloudAutomaticBackupService.php index 116f06af..3578b704 100644 --- a/web/app/Services/Cloud/CloudAutomaticBackupService.php +++ b/web/app/Services/Cloud/CloudAutomaticBackupService.php @@ -19,6 +19,16 @@ final class CloudAutomaticBackupService CloudInvoiceService::PAYMENT_GRACE_SECONDS; private const RETRY_SECONDS = 300; + private const MAX_DEFER_COUNT = 4294967295; + private const DEFER_REASONS = [ + CloudBackupEligibilityDecision::MIGRATION_ACTIVE, + CloudBackupEligibilityDecision::INSTANCE_MUTATION_ACTIVE, + CloudBackupEligibilityDecision::BACKUP_RESCUE_ACTIVE, + CloudBackupEligibilityDecision::BACKUP_DISK_TRANSITION_ACTIVE, + CloudBackupEligibilityDecision::DOMAIN_JOB_ACTIVE, + CloudBackupEligibilityDecision::READINESS_UNAVAILABLE, + 'scheduler_unavailable', + ]; public static function activatePurchased(CloudInstance $instance): ?CloudBackupSubscription { @@ -80,6 +90,7 @@ final class CloudAutomaticBackupService $subscription->next_run_at = self::nextRunAt( $now, 'daily', 0 ); + self::clearDeferredState($subscription); $subscription->last_scheduled_at = null; $subscription->last_backup_id = null; $subscription->created_at = $subscription->updated_at = $now; @@ -202,6 +213,7 @@ final class CloudAutomaticBackupService $locked->next_run_at = self::nextRunAt( $now, $frequency, (int) $weekday ); + self::clearDeferredState($locked); } $locked->updated_at = $now; $locked->save(); @@ -283,7 +295,8 @@ final class CloudAutomaticBackupService $now ??= time(); $ids = CloudBackupSubscription::where('enabled', 1) ->where('next_run_at', '>', 0) - ->where('next_run_at', '<=', $now)->orderBy('next_run_at') + ->where('next_run_at', '<=', $now) + ->where('deferred_until', '<=', $now)->orderBy('next_run_at') ->orderBy('id')->limit($limit)->pluck('id')->all(); $queued = 0; foreach ($ids as $id) { @@ -295,9 +308,6 @@ final class CloudAutomaticBackupService if (self::pauseIfInstanceExpired((int) $id, $now)) { continue; } - if (self::deferIfMigrationOpen((int) $id, $now)) { - continue; - } // Creation owns a durable intent before object-storage // preparation begins. Keep that exact scheduled slot stable // while the preparation worker reconciles it; moving @@ -306,13 +316,11 @@ final class CloudAutomaticBackupService if (self::hasPendingCreateIntent((int) $id, $now)) { continue; } - CloudBackupSubscription::where('id', (int) $id) - ->where('enabled', 1) - ->where('next_run_at', '<=', $now) - ->update([ - 'next_run_at' => $now + self::RETRY_SECONDS, - 'updated_at' => time(), - ]); + self::deferCurrentSlot( + (int) $id, + $now, + 'scheduler_unavailable' + ); error_log('cloud automatic backup schedule unavailable: ' . get_class($error)); } @@ -573,6 +581,7 @@ final class CloudAutomaticBackupService (string) $subscription->frequency, (int) $subscription->weekday ); + self::clearDeferredState($subscription); } $subscription->updated_at = time(); $subscription->save(); @@ -582,7 +591,8 @@ final class CloudAutomaticBackupService { $subscription = CloudBackupSubscription::find($subscriptionId); if ($subscription === null || !(bool) $subscription->enabled - || (int) $subscription->next_run_at > $now) { + || (int) $subscription->next_run_at > $now + || (int) $subscription->deferred_until > $now) { return false; } $instance = CloudInstance::find((int) $subscription->instance_id); @@ -596,9 +606,6 @@ final class CloudAutomaticBackupService || (int) $instance->user_id !== (int) $subscription->user_id) { throw new CloudError('实例当前不可创建自动备份', 409); } - if (self::deferIfMigrationOpen($subscriptionId, $now)) { - return false; - } $scheduledFor = (int) $subscription->next_run_at; if (!CloudBackupPeriodEntitlement::where( 'subscription_id', (int) $subscription->id @@ -606,7 +613,8 @@ final class CloudAutomaticBackupService ->where('user_id', (int) $instance->user_id) ->where('starts_at', '<=', $scheduledFor) ->where('ends_at', '>', $scheduledFor)->exists()) { - throw new CloudError('自动备份执行时间不在已付权益周期内', 409); + self::pauseWithoutEntitlement($subscriptionId, $scheduledFor, $now); + return false; } $actor = [ 'user_id' => (int) $instance->user_id, @@ -617,6 +625,21 @@ final class CloudAutomaticBackupService 'request_idempotency_key' => 'automatic-backup-' . (int) $subscription->id . '-' . $scheduledFor, ]; + if (!self::hasPendingCreateIntent($subscriptionId, $now)) { + $eligibility = (new CloudBackupEligibility())->assess( + $instance, + $actor + ); + if (!$eligibility->allowed()) { + self::deferCurrentSlot( + $subscriptionId, + $now, + $eligibility->reasonCode, + $scheduledFor + ); + return false; + } + } $queued = CloudBackupService::createAutomatic( $instance, $subscription, $actor, $scheduledFor ); @@ -673,6 +696,7 @@ final class CloudAutomaticBackupService || (int) $subscription->next_run_at !== 0) { $subscription->enabled = false; $subscription->next_run_at = 0; + self::clearDeferredState($subscription); self::advanceStateRevision($subscription); $subscription->updated_at = $now; $subscription->save(); @@ -682,14 +706,21 @@ final class CloudAutomaticBackupService } /** - * An open maintenance migration is a temporary instance mutation fence, - * not an automatic-backup failure. Serialize with migration start through - * the instance row, then move only the due slot by the bounded retry delay. + * Record one bounded retry without changing the logical schedule slot. + * Locking the instance before the subscription keeps mutation admission + * ordering consistent while preserving the idempotency identity. */ - private static function deferIfMigrationOpen( + private static function deferCurrentSlot( int $subscriptionId, - int $now + int $now, + string $reason, + ?int $expectedSlot = null ): bool { + if (!in_array($reason, self::DEFER_REASONS, true)) { + throw new \InvalidArgumentException( + 'Invalid automatic backup defer reason' + ); + } $probe = CloudBackupSubscription::find($subscriptionId); if ($probe === null) { return false; @@ -697,7 +728,9 @@ final class CloudAutomaticBackupService return $probe->getConnection()->transaction(function () use ( $subscriptionId, $probe, - $now + $now, + $reason, + $expectedSlot ): bool { $instance = CloudInstance::where( 'id', (int) $probe->instance_id @@ -710,18 +743,66 @@ final class CloudAutomaticBackupService || (int) $subscription->instance_id !== (int) $instance->id || (int) $subscription->next_run_at < 1 || (int) $subscription->next_run_at > $now - || !CloudInstanceMigrationService::hasOpenMigration( - (int) $instance->id - )) { + || (int) $subscription->deferred_until > $now + || ($expectedSlot !== null + && (int) $subscription->next_run_at !== $expectedSlot)) { return false; } - $subscription->next_run_at = $now + self::RETRY_SECONDS; + $count = max(0, (int) $subscription->defer_count); + $subscription->deferred_until = $now + self::RETRY_SECONDS; + $subscription->defer_reason = $reason; + $subscription->deferred_at = $now; + $subscription->defer_count = min( + self::MAX_DEFER_COUNT, + $count + 1 + ); $subscription->updated_at = $now; $subscription->save(); return true; }, 3); } + /** A missing paid slot is a parked subscription, never an infinite retry. */ + private static function pauseWithoutEntitlement( + int $subscriptionId, + int $scheduledFor, + int $now + ): void { + $subscription = CloudBackupSubscription::find($subscriptionId); + if (!$subscription instanceof CloudBackupSubscription) { + return; + } + $subscription->getConnection()->transaction(function () use ( + $subscriptionId, + $scheduledFor, + $now + ): void { + $locked = CloudBackupSubscription::where( + 'id', $subscriptionId + )->lockForUpdate()->first(); + if (!$locked instanceof CloudBackupSubscription + || !(bool) $locked->enabled + || (int) $locked->next_run_at !== $scheduledFor) { + return; + } + $locked->enabled = false; + $locked->next_run_at = 0; + self::clearDeferredState($locked); + self::advanceStateRevision($locked); + $locked->updated_at = $now; + $locked->save(); + }, 3); + } + + public static function clearDeferredState( + CloudBackupSubscription $subscription + ): void { + $subscription->deferred_until = 0; + $subscription->defer_reason = ''; + $subscription->deferred_at = null; + $subscription->defer_count = 0; + } + private static function backupsForInstance(CloudInstance $instance): array { $items = []; @@ -815,6 +896,19 @@ final class CloudAutomaticBackupService 'timezone' => self::TIMEZONE, 'run_at' => self::RUN_AT, 'next_run_at' => max(0, (int) $subscription->next_run_at), + 'deferred' => (int) $subscription->deferred_until > 0, + 'deferred_until' => max( + 0, + (int) $subscription->deferred_until + ), + 'defer_reason' => in_array( + (string) $subscription->defer_reason, + self::DEFER_REASONS, + true + ) ? (string) $subscription->defer_reason : '', + 'deferred_at' => $subscription->deferred_at === null + ? null : max(0, (int) $subscription->deferred_at), + 'defer_count' => max(0, (int) $subscription->defer_count), 'last_scheduled_at' => $subscription->last_scheduled_at === null ? null : max(0, (int) $subscription->last_scheduled_at), 'price' => (string) $subscription->price, diff --git a/web/sql/cloud.sql b/web/sql/cloud.sql index 7e6b3488..caedb983 100644 --- a/web/sql/cloud.sql +++ b/web/sql/cloud.sql @@ -1344,16 +1344,24 @@ CREATE TABLE cloud_backup_subscriptions ( price_version INT UNSIGNED NOT NULL, state_revision INT UNSIGNED NOT NULL DEFAULT 1, next_run_at BIGINT UNSIGNED NOT NULL, + deferred_until BIGINT UNSIGNED NOT NULL DEFAULT 0, + defer_reason VARCHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT '', + deferred_at BIGINT UNSIGNED DEFAULT NULL, + defer_count INT UNSIGNED NOT NULL DEFAULT 0, last_scheduled_at BIGINT UNSIGNED DEFAULT NULL, last_backup_id BIGINT UNSIGNED DEFAULT NULL, created_at BIGINT UNSIGNED NOT NULL, updated_at BIGINT UNSIGNED NOT NULL, UNIQUE KEY cloud_backup_subscription_instance (instance_id), - KEY cloud_backup_subscription_due (enabled, next_run_at, id), + KEY cloud_backup_subscription_due (enabled, next_run_at, deferred_until, id), KEY cloud_backup_subscription_owner (user_id, id), CONSTRAINT chk_cloud_backup_subscription_frequency CHECK (frequency IN ('daily','weekly')), CONSTRAINT chk_cloud_backup_subscription_weekday CHECK (weekday <= 6), - CONSTRAINT chk_cloud_backup_subscription_retention CHECK (retention_count IN (1,2)) + CONSTRAINT chk_cloud_backup_subscription_retention CHECK (retention_count IN (1,2)), + CONSTRAINT chk_cloud_backup_subscription_deferral CHECK ( + (deferred_until = 0 AND defer_reason = '' AND deferred_at IS NULL AND defer_count = 0) + OR (deferred_until > 0 AND defer_reason <> '' AND deferred_at IS NOT NULL AND defer_count > 0) + ) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; -- Proof that one service period includes paid automatic backup. Its paid -- commercial fields are immutable; an audited service compensation may extend diff --git a/web/tests/CloudAutomaticBackupBillingTest.php b/web/tests/CloudAutomaticBackupBillingTest.php index d1b65c4e..3b2dba4b 100644 --- a/web/tests/CloudAutomaticBackupBillingTest.php +++ b/web/tests/CloudAutomaticBackupBillingTest.php @@ -91,6 +91,10 @@ $schema->create('cloud_backup_subscriptions', static function (Blueprint $table) $table->integer('price_version'); $table->integer('state_revision')->default(1); $table->integer('next_run_at'); + $table->integer('deferred_until')->default(0); + $table->string('defer_reason')->default(''); + $table->integer('deferred_at')->nullable(); + $table->integer('defer_count')->default(0); $table->integer('last_scheduled_at')->nullable(); $table->integer('last_backup_id')->nullable(); $table->integer('created_at'); diff --git a/web/tests/CloudAutomaticBackupDurableIntentTest.php b/web/tests/CloudAutomaticBackupDurableIntentTest.php index dc59ca5a..a1bc6cb7 100644 --- a/web/tests/CloudAutomaticBackupDurableIntentTest.php +++ b/web/tests/CloudAutomaticBackupDurableIntentTest.php @@ -52,6 +52,10 @@ $schema->create('cloud_backup_subscriptions', $t->unsignedInteger('price_version'); $t->unsignedInteger('state_revision')->default(1); $t->unsignedBigInteger('next_run_at'); + $t->unsignedBigInteger('deferred_until')->default(0); + $t->string('defer_reason')->default(''); + $t->unsignedBigInteger('deferred_at')->nullable(); + $t->unsignedInteger('defer_count')->default(0); $t->unsignedBigInteger('last_scheduled_at')->nullable(); $t->unsignedBigInteger('last_backup_id')->nullable(); $t->unsignedBigInteger('created_at'); diff --git a/web/tests/CloudAutomaticBackupEntitlementGrantTest.php b/web/tests/CloudAutomaticBackupEntitlementGrantTest.php index d8759432..54676106 100644 --- a/web/tests/CloudAutomaticBackupEntitlementGrantTest.php +++ b/web/tests/CloudAutomaticBackupEntitlementGrantTest.php @@ -58,6 +58,10 @@ $schema->create('cloud_backup_subscriptions', static function (Blueprint $table) $table->integer('retention_count'); $table->string('timezone'); $table->integer('next_run_at'); + $table->integer('deferred_until')->default(0); + $table->string('defer_reason')->default(''); + $table->integer('deferred_at')->nullable(); + $table->integer('defer_count')->default(0); $table->integer('last_scheduled_at')->nullable(); $table->integer('last_backup_id')->nullable(); $table->integer('created_at'); diff --git a/web/tests/CloudAutomaticBackupExpiryLifecycleTest.php b/web/tests/CloudAutomaticBackupExpiryLifecycleTest.php index 8ab4fada..40bc9031 100644 --- a/web/tests/CloudAutomaticBackupExpiryLifecycleTest.php +++ b/web/tests/CloudAutomaticBackupExpiryLifecycleTest.php @@ -45,11 +45,24 @@ $schema->create('cloud_backup_subscriptions', $table->integer('price_version'); $table->integer('state_revision')->default(1); $table->integer('next_run_at'); + $table->integer('deferred_until')->default(0); + $table->string('defer_reason')->default(''); + $table->integer('deferred_at')->nullable(); + $table->integer('defer_count')->default(0); $table->integer('last_scheduled_at')->nullable(); $table->integer('last_backup_id')->nullable(); $table->integer('created_at'); $table->integer('updated_at'); }); +$schema->create('cloud_backup_period_entitlements', + static function (Blueprint $table): void { + $table->increments('id'); + $table->integer('subscription_id'); + $table->integer('instance_id'); + $table->integer('user_id'); + $table->integer('starts_at'); + $table->integer('ends_at'); + }); $now = time(); $newDue = static function (int $id, string $serviceStatus, @@ -82,11 +95,28 @@ $newDue = static function (int $id, string $serviceStatus, return $subscription; }; +$deferred = $newDue(10, 'expired_grace', $now + 3600); +$deferred->deferred_until = $now + 300; +$deferred->defer_reason = 'migration_active'; +$deferred->deferred_at = $now; +$deferred->defer_count = 1; +$deferred->save(); $grace = $newDue(11, 'expired_grace', $now + 3600); $expiredBeforeLifecycleTick = $newDue(12, 'active', $now - 1); +$missingEntitlement = $newDue(13, 'active', $now + 3600); +$queued = CloudAutomaticBackupService::scheduleDue(1, $now); +$deferred->refresh(); +$grace->refresh(); +$assert($queued === 0 && (bool) $deferred->enabled + && (int) $deferred->next_run_at === $now - 60 + && (int) $deferred->deferred_until === $now + 300 + && !(bool) $grace->enabled, + 'A future deferred row starved the next runnable subscription.'); + $queued = CloudAutomaticBackupService::scheduleDue(10, $now); $grace->refresh(); $expiredBeforeLifecycleTick->refresh(); +$missingEntitlement->refresh(); $assert($queued === 0 && !(bool) $grace->enabled @@ -96,7 +126,10 @@ $assert($queued === 0 && !(bool) $expiredBeforeLifecycleTick->enabled && (bool) $expiredBeforeLifecycleTick->renew_next_period && (int) $expiredBeforeLifecycleTick->next_run_at === 0 - && (int) $expiredBeforeLifecycleTick->state_revision === 2, - 'Expired instances were not parked and would continue retrying every five minutes.'); + && (int) $expiredBeforeLifecycleTick->state_revision === 2 + && !(bool) $missingEntitlement->enabled + && (int) $missingEntitlement->next_run_at === 0 + && (int) $missingEntitlement->deferred_until === 0, + 'Expired or unentitled instances were not parked and would retry forever.'); echo "Automatic backup expiry pause lifecycle tests passed.\n"; diff --git a/web/tests/CloudAutomaticBackupRetentionGraceTest.php b/web/tests/CloudAutomaticBackupRetentionGraceTest.php index 880fc261..6a83ebad 100644 --- a/web/tests/CloudAutomaticBackupRetentionGraceTest.php +++ b/web/tests/CloudAutomaticBackupRetentionGraceTest.php @@ -60,6 +60,10 @@ namespace { $table->integer('price_version'); $table->integer('state_revision')->default(1); $table->integer('next_run_at'); + $table->integer('deferred_until')->default(0); + $table->string('defer_reason')->default(''); + $table->integer('deferred_at')->nullable(); + $table->integer('defer_count')->default(0); $table->integer('last_scheduled_at')->nullable(); $table->integer('last_backup_id')->nullable(); $table->integer('created_at'); diff --git a/web/tests/CloudAutomaticBackupSubscriptionTest.php b/web/tests/CloudAutomaticBackupSubscriptionTest.php index d7f0ed45..092111f3 100644 --- a/web/tests/CloudAutomaticBackupSubscriptionTest.php +++ b/web/tests/CloudAutomaticBackupSubscriptionTest.php @@ -49,6 +49,10 @@ $schema->create('cloud_backup_subscriptions', $table->integer('price_version'); $table->integer('state_revision')->default(1); $table->integer('next_run_at'); + $table->integer('deferred_until')->default(0); + $table->string('defer_reason')->default(''); + $table->integer('deferred_at')->nullable(); + $table->integer('defer_count')->default(0); $table->integer('last_scheduled_at')->nullable(); $table->integer('last_backup_id')->nullable(); $table->integer('created_at'); diff --git a/web/tests/CloudBackupMigrationFenceTest.php b/web/tests/CloudBackupMigrationFenceTest.php index eca6ca95..507f426e 100644 --- a/web/tests/CloudBackupMigrationFenceTest.php +++ b/web/tests/CloudBackupMigrationFenceTest.php @@ -96,11 +96,36 @@ $schema->create('cloud_backup_subscriptions', $table->integer('price_version'); $table->integer('state_revision')->default(1); $table->integer('next_run_at'); + $table->integer('deferred_until')->default(0); + $table->string('defer_reason')->default(''); + $table->integer('deferred_at')->nullable(); + $table->integer('defer_count')->default(0); $table->integer('last_scheduled_at')->nullable(); $table->integer('last_backup_id')->nullable(); $table->integer('created_at'); $table->integer('updated_at'); }); +$schema->create('cloud_instance_backups', + static function (Blueprint $table): void { + $table->increments('id'); + $table->integer('subscription_id')->nullable(); + $table->integer('scheduled_for')->nullable(); + $table->string('source'); + $table->string('state'); + $table->integer('storage_artifact_id')->nullable(); + $table->integer('active_job_id')->nullable(); + $table->integer('create_request_actor_id')->nullable(); + $table->string('create_request_idempotency_key')->nullable(); + }); +$schema->create('cloud_backup_period_entitlements', + static function (Blueprint $table): void { + $table->increments('id'); + $table->integer('subscription_id'); + $table->integer('instance_id'); + $table->integer('user_id'); + $table->integer('starts_at'); + $table->integer('ends_at'); + }); $now = time(); $instance = new CloudInstance(); @@ -175,6 +200,13 @@ $subscription->price_version = 1; $subscription->next_run_at = $now - 60; $subscription->created_at = $subscription->updated_at = $now - 3600; $subscription->save(); +$capsule->table('cloud_backup_period_entitlements')->insert([ + 'subscription_id' => (int) $subscription->id, + 'instance_id' => (int) $instance->id, + 'user_id' => (int) $instance->user_id, + 'starts_at' => $now - 3600, + 'ends_at' => $now + 3600, +]); $logPath = tempnam(sys_get_temp_dir(), 'virt-backup-migration-fence-'); $previousLog = ini_set('error_log', $logPath); @@ -190,10 +222,16 @@ try { } $check($queued === 0 && (bool) $subscription->enabled - && (int) $subscription->next_run_at === $now + 300, - 'Automatic backup was not silently deferred for the open migration.'); + && (int) $subscription->next_run_at === $now - 60 + && (int) $subscription->deferred_until === $now + 300 + && (string) $subscription->defer_reason === 'migration_active' + && (int) $subscription->deferred_at === $now + && (int) $subscription->defer_count === 1, + 'Automatic backup was not silently deferred for the open migration: ' + . json_encode($subscription->getAttributes())); $check(!str_contains($log, 'cloud automatic backup schedule unavailable'), - 'Expected migration deferral was recorded as an automatic-backup failure.'); + 'Expected migration deferral was recorded as an automatic-backup failure: ' + . $log); if ($failures !== []) { throw new RuntimeException(implode("\n", $failures)); diff --git a/web/tests/CloudBackupPublicationPreparationSchemaContractTest.php b/web/tests/CloudBackupPublicationPreparationSchemaContractTest.php index 7db5493f..7013ada4 100644 --- a/web/tests/CloudBackupPublicationPreparationSchemaContractTest.php +++ b/web/tests/CloudBackupPublicationPreparationSchemaContractTest.php @@ -52,7 +52,7 @@ foreach ([ 'CloudSetup omits backup preparation contract: ' . $required); } $assert(str_contains($setup, - "SCHEMA_VERSION = 'go-agent-wss-v39-ui-copy-catalog'") + "SCHEMA_VERSION = 'go-agent-wss-v40-backup-deferral'") && str_contains($setup, "'columns' => [\n 'create_request_actor_id',\n 'create_request_idempotency_key',") && str_contains($setup, "'non_unique' => 0"), diff --git a/web/tests/CloudBackupPublicationPreparationTest.php b/web/tests/CloudBackupPublicationPreparationTest.php index 6332650a..ee8bb64c 100644 --- a/web/tests/CloudBackupPublicationPreparationTest.php +++ b/web/tests/CloudBackupPublicationPreparationTest.php @@ -231,6 +231,10 @@ namespace { $t->string('price'); $t->string('currency'); $t->unsignedInteger('price_version'); $t->unsignedInteger('state_revision')->default(1); $t->unsignedBigInteger('next_run_at'); + $t->unsignedBigInteger('deferred_until')->default(0); + $t->string('defer_reason')->default(''); + $t->unsignedBigInteger('deferred_at')->nullable(); + $t->unsignedInteger('defer_count')->default(0); $t->unsignedBigInteger('last_scheduled_at')->nullable(); $t->unsignedBigInteger('last_backup_id')->nullable(); $t->unsignedBigInteger('created_at'); $t->unsignedBigInteger('updated_at'); diff --git a/web/tests/CloudBillingLifecycleTest.php b/web/tests/CloudBillingLifecycleTest.php index e9809b92..4abb5598 100644 --- a/web/tests/CloudBillingLifecycleTest.php +++ b/web/tests/CloudBillingLifecycleTest.php @@ -213,6 +213,10 @@ namespace { $table->integer('price_version')->default(1); $table->integer('state_revision')->default(1); $table->integer('next_run_at')->default(0); + $table->integer('deferred_until')->default(0); + $table->string('defer_reason')->default(''); + $table->integer('deferred_at')->nullable(); + $table->integer('defer_count')->default(0); $table->integer('last_scheduled_at')->nullable(); $table->integer('last_backup_id')->nullable(); $table->integer('created_at')->default(0); diff --git a/web/tests/CloudFinancialWorkflowTest.php b/web/tests/CloudFinancialWorkflowTest.php index 2098c3fd..5986e484 100644 --- a/web/tests/CloudFinancialWorkflowTest.php +++ b/web/tests/CloudFinancialWorkflowTest.php @@ -394,7 +394,7 @@ namespace { $schema->create('cloud_service_periods', function (Blueprint $t) { $t->increments('id'); foreach (['instance_id', 'user_id'] as $v) { $t->integer($v); } foreach (['plan_id', 'order_id', 'renewal_id', 'traffic_pool_group_id', 'download_base_bytes', 'upload_base_bytes', 'activated_at'] as $v) { $t->integer($v)->nullable(); } $t->string('traffic_quota_mode')->default('finite'); foreach (['starts_at', 'ends_at', 'traffic_quota_bytes', 'download_used_bytes', 'upload_used_bytes', 'created_at', 'updated_at'] as $v) { $t->integer($v)->default(0); } $t->string('status'); $t->unique('order_id'); $t->unique('renewal_id'); }); $schema->create('cloud_invoices', function (Blueprint $t) { $t->increments('id'); $t->string('invoice_no')->unique(); $t->string('purpose')->default('renewal'); $t->integer('instance_id')->nullable(); $t->integer('order_id')->nullable()->unique(); $t->integer('user_id'); $t->integer('plan_id')->nullable(); $t->integer('offer_id')->nullable(); $t->integer('renewal_id')->nullable(); $t->integer('renews_expires_at')->nullable(); $t->integer('due_at'); $t->string('amount'); $t->string('currency')->default('USD'); $t->integer('validity_days'); $t->integer('traffic_quota_bytes'); $t->string('status'); $t->string('paid_via')->nullable(); $t->string('payment_reference')->nullable(); $t->integer('paid_at')->nullable(); $t->integer('created_at'); $t->integer('updated_at'); }); $schema->create('cloud_invoice_items', function (Blueprint $t) { $t->increments('id'); $t->integer('invoice_id'); $t->integer('order_item_id')->nullable(); $t->string('code'); $t->string('label'); $t->string('amount'); $t->string('currency')->default('USD'); $t->text('snapshot_json'); $t->integer('created_at'); $t->unique(['invoice_id', 'code']); }); - $schema->create('cloud_backup_subscriptions', function (Blueprint $t) { $t->increments('id'); $t->integer('instance_id')->unique(); $t->integer('user_id'); $t->integer('offer_id'); $t->boolean('enabled')->default(true); $t->boolean('renew_next_period')->default(true); $t->string('frequency')->default('daily'); $t->integer('weekday')->default(0); $t->integer('retention_count')->default(2); $t->string('timezone')->default('UTC'); $t->string('price'); $t->string('currency')->default('USD'); $t->integer('price_version'); $t->integer('state_revision')->default(1); $t->integer('next_run_at'); $t->integer('last_scheduled_at')->nullable(); $t->integer('last_backup_id')->nullable(); $t->integer('created_at'); $t->integer('updated_at'); }); + $schema->create('cloud_backup_subscriptions', function (Blueprint $t) { $t->increments('id'); $t->integer('instance_id')->unique(); $t->integer('user_id'); $t->integer('offer_id'); $t->boolean('enabled')->default(true); $t->boolean('renew_next_period')->default(true); $t->string('frequency')->default('daily'); $t->integer('weekday')->default(0); $t->integer('retention_count')->default(2); $t->string('timezone')->default('UTC'); $t->string('price'); $t->string('currency')->default('USD'); $t->integer('price_version'); $t->integer('state_revision')->default(1); $t->integer('next_run_at'); $t->integer('deferred_until')->default(0); $t->string('defer_reason')->default(''); $t->integer('deferred_at')->nullable(); $t->integer('defer_count')->default(0); $t->integer('last_scheduled_at')->nullable(); $t->integer('last_backup_id')->nullable(); $t->integer('created_at'); $t->integer('updated_at'); }); $schema->create('cloud_backup_period_entitlements', function (Blueprint $t) { $t->increments('id'); $t->integer('subscription_id'); $t->integer('instance_id'); $t->integer('service_period_id')->unique(); $t->integer('user_id'); $t->integer('offer_id'); $t->integer('invoice_id'); $t->integer('starts_at'); $t->integer('ends_at'); $t->string('amount'); $t->string('full_period_price'); $t->string('currency')->default('USD'); $t->integer('price_version'); $t->string('source'); $t->integer('paid_at'); $t->integer('created_at'); $t->integer('updated_at'); }); $schema->create('cloud_invoice_payments', function (Blueprint $t) { $t->increments('id'); $t->integer('invoice_id'); $t->integer('user_id'); $t->string('provider'); $t->string('provider_order_no'); $t->string('idempotency_key'); $t->string('amount'); $t->string('status'); $t->integer('created_at'); $t->integer('updated_at'); $t->unique(['provider', 'provider_order_no']); $t->unique(['user_id', 'idempotency_key']); }); $schema->create('cloud_mail_outbox', function (Blueprint $t) { $t->increments('id'); $t->integer('invoice_id'); $t->string('kind'); $t->string('status'); $t->integer('attempts'); $t->integer('available_at'); $t->string('lease_token')->nullable(); $t->integer('lease_until'); $t->integer('sent_at')->nullable(); $t->string('last_error'); $t->integer('created_at'); $t->integer('updated_at'); $t->unique(['invoice_id', 'kind']); }); diff --git a/web/tests/CloudFreshSchemaContractTest.php b/web/tests/CloudFreshSchemaContractTest.php index a005fc7c..75f59e94 100644 --- a/web/tests/CloudFreshSchemaContractTest.php +++ b/web/tests/CloudFreshSchemaContractTest.php @@ -22,14 +22,22 @@ $assert(stripos($schema, 'compatibility import') === false $assert(!preg_match('/\bengine_[A-Za-z0-9_]*\b/', $schema), 'cloud.sql retained an engine-prefixed identifier.'); $assert(str_contains($setup, - "SCHEMA_VERSION = 'go-agent-wss-v39-ui-copy-catalog'") + "SCHEMA_VERSION = 'go-agent-wss-v40-backup-deferral'") && !str_contains($setup, "SCHEMA_VERSION = 'go-v1'"), 'Outbound Agent schema must reject the incompatible earlier Go database.'); $assert(str_contains($schema, "timezone VARCHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT 'UTC'") && str_contains($schema, 'state_revision INT UNSIGNED NOT NULL DEFAULT 1') - && str_contains($setup, "'state_revision', 'next_run_at'") + && str_contains($schema, + 'deferred_until BIGINT UNSIGNED NOT NULL DEFAULT 0') + && str_contains($schema, + "defer_reason VARCHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT ''") + && str_contains($schema, + 'KEY cloud_backup_subscription_due (enabled, next_run_at, deferred_until, id)') + && str_contains($schema, + 'CONSTRAINT chk_cloud_backup_subscription_deferral CHECK (') + && str_contains($setup, "'state_revision', 'next_run_at', 'deferred_until'") && !str_contains($schema, 'America/Los_Angeles'), 'Fresh automatic-backup subscriptions must default to UTC with a state revision.'); diff --git a/web/tests/CloudMaintenanceNoticeSchemaContractTest.php b/web/tests/CloudMaintenanceNoticeSchemaContractTest.php index 9eba22e4..669cfcc6 100644 --- a/web/tests/CloudMaintenanceNoticeSchemaContractTest.php +++ b/web/tests/CloudMaintenanceNoticeSchemaContractTest.php @@ -56,7 +56,7 @@ foreach ([ } $assert(str_contains($setup, - "SCHEMA_VERSION = 'go-agent-wss-v39-ui-copy-catalog'"), + "SCHEMA_VERSION = 'go-agent-wss-v40-backup-deferral'"), 'CloudSetup schema version was not advanced for maintenance notices.'); echo "Maintenance notice schema contracts passed.\n"; diff --git a/web/tests/CloudPlanUpgradeServiceTest.php b/web/tests/CloudPlanUpgradeServiceTest.php index 9a49e42c..261014eb 100644 --- a/web/tests/CloudPlanUpgradeServiceTest.php +++ b/web/tests/CloudPlanUpgradeServiceTest.php @@ -215,6 +215,10 @@ namespace { $table->integer('price_version'); $table->integer('state_revision')->default(1); $table->integer('next_run_at')->default(0); + $table->integer('deferred_until')->default(0); + $table->string('defer_reason')->default(''); + $table->integer('deferred_at')->nullable(); + $table->integer('defer_count')->default(0); $table->integer('last_scheduled_at')->nullable(); $table->integer('last_backup_id')->nullable(); $table->integer('created_at'); diff --git a/web/tests/CloudPublicIpPoolReplacementSchemaContractTest.php b/web/tests/CloudPublicIpPoolReplacementSchemaContractTest.php index 6f2bbd52..c411afe5 100644 --- a/web/tests/CloudPublicIpPoolReplacementSchemaContractTest.php +++ b/web/tests/CloudPublicIpPoolReplacementSchemaContractTest.php @@ -53,7 +53,7 @@ $assert(is_string($schema) 'Public-pool replacement lacks durable batch, child, or claim schema.'); $assert(is_string($setup) && str_contains($setup, - "SCHEMA_VERSION = 'go-agent-wss-v39-ui-copy-catalog'") + "SCHEMA_VERSION = 'go-agent-wss-v40-backup-deferral'") && str_contains($setup, "'cloud_public_ip_pool_replacement_batches'") && str_contains($setup, diff --git a/web/tests/CloudPublicNetworkTransitionSchemaContractTest.php b/web/tests/CloudPublicNetworkTransitionSchemaContractTest.php index 531fc76a..bf229644 100644 --- a/web/tests/CloudPublicNetworkTransitionSchemaContractTest.php +++ b/web/tests/CloudPublicNetworkTransitionSchemaContractTest.php @@ -170,7 +170,7 @@ $assert(str_contains($setup, 'CloudSetup does not verify index uniqueness and ordered columns.'); $assert(str_contains($setup, - "SCHEMA_VERSION = 'go-agent-wss-v39-ui-copy-catalog'") + "SCHEMA_VERSION = 'go-agent-wss-v40-backup-deferral'") && str_contains($setup, "'cloud_public_network_transitions'") && str_contains($setup, "'cloud_public_network_transition_ips'") && str_contains($setup, "'reservation_ip_claim_id'") diff --git a/web/tests/CloudRescueCatalogSchemaContractTest.php b/web/tests/CloudRescueCatalogSchemaContractTest.php index 944c9133..40aa5491 100644 --- a/web/tests/CloudRescueCatalogSchemaContractTest.php +++ b/web/tests/CloudRescueCatalogSchemaContractTest.php @@ -57,7 +57,7 @@ $assert($replica !== '' && str_contains($replica, 'cleanup_resolved_at BIGINT UNSIGNED'), 'Rescue replica cannot prove exact identity or retain cleanup debt.'); $assert(str_contains($setup, - "SCHEMA_VERSION = 'go-agent-wss-v39-ui-copy-catalog'") + "SCHEMA_VERSION = 'go-agent-wss-v40-backup-deferral'") && str_contains($setup, "'cloud_rescue_artifact_delete_due'") && str_contains($setup, "'cloud_rescue_replica_cleanup'"), 'CloudSetup version or rescue catalog indexes are incomplete.'); diff --git a/web/tests/CloudRestoreMigrationLedgerContractTest.php b/web/tests/CloudRestoreMigrationLedgerContractTest.php index e048506c..3da5e205 100644 --- a/web/tests/CloudRestoreMigrationLedgerContractTest.php +++ b/web/tests/CloudRestoreMigrationLedgerContractTest.php @@ -218,7 +218,7 @@ foreach ([ 'CloudSetup omits restore/migration ledger contract: ' . $required); } $assert(str_contains($setup, - "SCHEMA_VERSION = 'go-agent-wss-v39-ui-copy-catalog'") + "SCHEMA_VERSION = 'go-agent-wss-v40-backup-deferral'") && str_contains($attemptModel, "protected \$table = 'cloud_backup_restore_attempts';") && str_contains($attemptModel, "'active_attempt' => 'int'") diff --git a/web/tests/CloudRuntimeHeartbeatTest.php b/web/tests/CloudRuntimeHeartbeatTest.php index cf475c18..b811d53a 100644 --- a/web/tests/CloudRuntimeHeartbeatTest.php +++ b/web/tests/CloudRuntimeHeartbeatTest.php @@ -429,6 +429,7 @@ try { $table->increments('id'); $table->boolean('enabled')->default(false); $table->integer('next_run_at')->default(0); + $table->integer('deferred_until')->default(0); $table->integer('retention_count')->default(2); }); $schema->create('cloud_backup_period_entitlements', static function ( diff --git a/web/tests/CloudServiceCompensationSchemaContractTest.php b/web/tests/CloudServiceCompensationSchemaContractTest.php index 765b9058..c602b760 100644 --- a/web/tests/CloudServiceCompensationSchemaContractTest.php +++ b/web/tests/CloudServiceCompensationSchemaContractTest.php @@ -105,7 +105,7 @@ $assert(str_contains($setup, 'CloudSetup lost the open-compensation fence index contract.'); $assert(str_contains($setup, - "SCHEMA_VERSION = 'go-agent-wss-v39-ui-copy-catalog'"), + "SCHEMA_VERSION = 'go-agent-wss-v40-backup-deferral'"), 'CloudSetup schema version was not advanced for service compensation.'); $appliedShape = preg_match( diff --git a/web/tests/CloudStoragePublicationSchemaContractTest.php b/web/tests/CloudStoragePublicationSchemaContractTest.php index 38123fd3..8382d362 100644 --- a/web/tests/CloudStoragePublicationSchemaContractTest.php +++ b/web/tests/CloudStoragePublicationSchemaContractTest.php @@ -137,7 +137,7 @@ $assert(is_string($publicationModel) && str_contains($artifactModel, "'maximum_size_bytes' => 'int'"), 'Storage publication/artifact model contracts are incomplete.'); $assert(str_contains($setup, - "SCHEMA_VERSION = 'go-agent-wss-v39-ui-copy-catalog'"), + "SCHEMA_VERSION = 'go-agent-wss-v40-backup-deferral'"), 'CloudSetup schema version was not advanced for durable publications.'); echo "Storage publication schema contracts passed.\n"; diff --git a/web/tests/CloudTemplateImportIntentContractTest.php b/web/tests/CloudTemplateImportIntentContractTest.php index 885beb3a..e6043385 100644 --- a/web/tests/CloudTemplateImportIntentContractTest.php +++ b/web/tests/CloudTemplateImportIntentContractTest.php @@ -63,7 +63,7 @@ $assert(str_contains($schema, && str_contains($schema, 'CONSTRAINT chk_cloud_template_import_completion CHECK (') && str_contains($setup, - "SCHEMA_VERSION = 'go-agent-wss-v39-ui-copy-catalog'"), + "SCHEMA_VERSION = 'go-agent-wss-v40-backup-deferral'"), 'Template imports are missing durable identity or terminal-state constraints.'); $assert(str_contains($routes, "'/admin/cloud-template-imports'") && str_contains($routes, "'/admin/cloud-template-imports/{id}'") diff --git a/web/tests/CloudTrafficPoolSchemaContractTest.php b/web/tests/CloudTrafficPoolSchemaContractTest.php index 8e463eec..d35ed647 100644 --- a/web/tests/CloudTrafficPoolSchemaContractTest.php +++ b/web/tests/CloudTrafficPoolSchemaContractTest.php @@ -71,7 +71,7 @@ $assert(str_contains($schema, 'Service-period traffic-pool identity is not frozen or indexed.'); $assert(str_contains($setup, - "SCHEMA_VERSION = 'go-agent-wss-v39-ui-copy-catalog'") + "SCHEMA_VERSION = 'go-agent-wss-v40-backup-deferral'") && str_contains($setup, "'chk_cloud_traffic_entitlement_balance'") && str_contains($setup, "'cloud_traffic_ledger_sequence'"), 'CloudSetup schema version or named-object validation is incomplete.'); diff --git a/web/tests/CloudVpcSchemaContractTest.php b/web/tests/CloudVpcSchemaContractTest.php index 114f161b..be9aa200 100644 --- a/web/tests/CloudVpcSchemaContractTest.php +++ b/web/tests/CloudVpcSchemaContractTest.php @@ -101,7 +101,7 @@ $assert(str_contains($jobs, 'vpc_id BIGINT UNSIGNED DEFAULT NULL') 'Cloud jobs cannot be correlated to VPC or VPC-port work.'); $assert(str_contains($setup, - "private const SCHEMA_VERSION = 'go-agent-wss-v39-ui-copy-catalog'") + "private const SCHEMA_VERSION = 'go-agent-wss-v40-backup-deferral'") && str_contains($setup, "'name' => 'cloud_vpc_enabled'") && str_contains($setup, "'value' => 'false'") && str_contains($setup, diff --git a/web/tests/UiCopyCatalogSchemaContractTest.php b/web/tests/UiCopyCatalogSchemaContractTest.php index 025882c7..34554401 100644 --- a/web/tests/UiCopyCatalogSchemaContractTest.php +++ b/web/tests/UiCopyCatalogSchemaContractTest.php @@ -104,7 +104,7 @@ foreach ($models as $class => $modelTable) { } $assert(str_contains($setup, - "SCHEMA_VERSION = 'go-agent-wss-v39-ui-copy-catalog'") + "SCHEMA_VERSION = 'go-agent-wss-v40-backup-deferral'") && str_contains($setup, "self::schema('ss.sql') . \"\\0\" . self::schema('cloud.sql')") && str_contains($setup,