fix(cloud): fence superseded renewal mail

go
codex 2 weeks ago
parent 836835ff41
commit e56ca68542

@ -363,6 +363,15 @@ final class CloudInvoiceService
}
if ($source !== null) {
if (!self::fenceRenewalMailForCompensationLocked(
$source,
$now
)) {
throw new CloudError(
'实例旧续费账单提醒邮件仍在投递',
409
);
}
$source->status = 'void';
$source->updated_at = $now;
$source->save();
@ -382,6 +391,63 @@ final class CloudInvoiceService
];
}
/**
* Fence an obsolete renewal reminder before replacing its invoice.
*
* The caller already holds the source invoice inside the compensation
* transaction. A sender claims the outbox row before contacting SMTP, so
* locking that row gives the transaction a strict ordering: admission may
* first lock without mutating, then reconciliation cancels pending mail
* before the invoice is voided. An already-sending message makes
* compensation wait without holding a transaction across the SMTP call.
*
* @return bool false only while the reminder is actively sending
*/
public static function fenceRenewalMailForCompensationLocked(
CloudInvoice $invoice,
int $now,
bool $cancelPending = true
): bool {
if ((int) $invoice->id < 1
|| (string) $invoice->purpose !== 'renewal') {
throw new CloudError('实例旧续费账单邮件目标无效', 409);
}
$messages = CloudMailOutbox::where(
'invoice_id',
(int) $invoice->id
)->where('kind', 'renewal_due')->orderBy('id')
->lockForUpdate()->get();
if ($messages->count() > 1) {
throw new CloudError('实例旧续费账单存在重复邮件任务', 409);
}
$message = $messages->first();
if ($message === null) {
return true;
}
$status = (string) $message->status;
if ($status === 'sending') {
return false;
}
if ($status === 'pending') {
if (!$cancelPending) {
return true;
}
$message->status = 'cancelled';
$message->available_at = 0;
$message->lease_token = null;
$message->lease_until = 0;
$message->sent_at = null;
$message->last_error = '续费目标已因实例服务时间补偿变更';
$message->updated_at = $now;
$message->save();
return true;
}
if (in_array($status, ['sent', 'failed', 'cancelled'], true)) {
return true;
}
throw new CloudError('实例旧续费账单邮件状态异常', 409);
}
private static function refreshRenewalSnapshotLocked(
CloudInstance $instance,
int $now,

@ -645,6 +645,18 @@ final class CloudServiceCompensationExecutionService
'旧续费账单存在已支付或效果未知的支付记录'
);
}
if (!CloudInvoiceService::fenceRenewalMailForCompensationLocked(
$invoice,
$this->now(),
false
)) {
return $this->decision(
'deferred',
'renewal_mail_sending',
'旧续费账单提醒邮件仍在投递',
false
);
}
return null;
}

@ -860,6 +860,18 @@ namespace {
$compensatedOldExpiry,
$now
);
$compensatedOldMail = new CloudMailOutbox();
$compensatedOldMail->invoice_id = (int) $compensatedOldInvoice->id;
$compensatedOldMail->kind = 'renewal_due';
$compensatedOldMail->status = 'pending';
$compensatedOldMail->attempts = 0;
$compensatedOldMail->available_at = $now;
$compensatedOldMail->lease_token = null;
$compensatedOldMail->lease_until = 0;
$compensatedOldMail->sent_at = null;
$compensatedOldMail->last_error = '';
$compensatedOldMail->created_at = $compensatedOldMail->updated_at = $now;
$compensatedOldMail->save();
$compensatedNewExpiry = $compensatedOldExpiry + 3600;
$compensatedInstance->expires_at = $compensatedNewExpiry;
$compensatedInstance->save();
@ -879,9 +891,71 @@ namespace {
=== $compensatedNewExpiry
&& (int) $compensatedReplacement->due_at
=== $compensatedNewExpiry
&& (string) $compensatedReplacement->status === 'open',
&& (string) $compensatedReplacement->status === 'open'
&& (string) $compensatedOldMail->fresh()->status === 'cancelled'
&& str_contains(
(string) $compensatedOldMail->fresh()->last_error,
'服务时间补偿'
)
&& CloudMailOutbox::where(
'invoice_id',
(int) $compensatedReplacement->id
)->where('kind', 'renewal_due')->where('status', 'pending')->exists(),
'Service compensation rewrote or failed to replace the old renewal target.');
$sendingMailInstance = $makeLifecycleInstance(
'11111111-2222-4333-8444-555555555576',
'sending-mail-compensated-invoice-vm',
$now + 1200,
'active',
'active'
);
$sendingMailOldExpiry = (int) $sendingMailInstance->expires_at;
$sendingMailInvoice = $makeLifecycleInvoice(
$sendingMailInstance,
'VC-COMPENSATION-MAIL-SENDING',
$sendingMailOldExpiry,
$now
);
$sendingMail = new CloudMailOutbox();
$sendingMail->invoice_id = (int) $sendingMailInvoice->id;
$sendingMail->kind = 'renewal_due';
$sendingMail->status = 'sending';
$sendingMail->attempts = 1;
$sendingMail->available_at = $now;
$sendingMail->lease_token = str_repeat('a', 32);
$sendingMail->lease_until = $now + 300;
$sendingMail->sent_at = null;
$sendingMail->last_error = '';
$sendingMail->created_at = $sendingMail->updated_at = $now;
$sendingMail->save();
$sendingMailNewExpiry = $sendingMailOldExpiry + 3600;
$sendingMailInstance->expires_at = $sendingMailNewExpiry;
$sendingMailInstance->save();
try {
CloudInvoiceService::reconcileRenewalAfterCompensationLocked(
$sendingMailInstance,
$sendingMailOldExpiry,
$sendingMailNewExpiry,
$now
);
throw new RuntimeException(
'A sending renewal reminder did not fence service compensation.'
);
} catch (CloudError $error) {
$assert($error->httpStatus === 409
&& str_contains($error->getMessage(), '提醒邮件仍在投递'),
'Sending renewal reminder returned the wrong compensation fence.');
}
$assert((string) $sendingMailInvoice->fresh()->status === 'open'
&& (string) $sendingMail->fresh()->status === 'sending'
&& !CloudInvoice::where(
'instance_id',
(int) $sendingMailInstance->id
)->where('renews_expires_at', $sendingMailNewExpiry)
->where('status', '<>', 'void')->exists(),
'Sending renewal reminder allowed compensation to replace its invoice.');
$farInstance = $makeLifecycleInstance(
'11111111-2222-4333-8444-555555555567',
'far-compensated-invoice-vm',

@ -71,6 +71,7 @@ foreach ([
'instance_operation_open',
'renewal_payment_job_open',
'renewal_payment_open',
'renewal_mail_sending',
] as $externalWaitCode) {
$assert(preg_match(
"/'deferred',\\s*'" . preg_quote($externalWaitCode, '/')
@ -89,6 +90,10 @@ $assert(preg_match(
'$item->attempts = max(0, (int) $item->attempts - 1);'
),
'External service-period wait lost its bounded non-consuming transition.');
$assert(str_contains(
$source,
'CloudInvoiceService::fenceRenewalMailForCompensationLocked('
), 'Compensation no longer fences an in-flight renewal reminder.');
$invoiceProjection = strpos($apply,
'reconcileRenewalAfterCompensationLocked(');

@ -124,6 +124,17 @@ namespace {
$table->integer('created_at');
$table->integer('updated_at');
});
$schema->create('cloud_invoice_items', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('invoice_id');
$table->integer('order_item_id')->nullable();
$table->string('code');
$table->string('label');
$table->string('amount');
$table->string('currency')->default('USD');
$table->text('snapshot_json');
$table->integer('created_at');
});
$schema->create('cloud_mail_outbox', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('invoice_id');
@ -478,5 +489,90 @@ namespace {
=== $pendingExpiry + 3600,
'Resolved payment wait did not resume compensation exactly once.');
$mailExpiry = $clockNow + 3600;
$makeInstance(3, $mailExpiry, 'vm-renewal-mail-sending');
$makeBatch(3, 3, 'c');
Capsule::table('cloud_invoices')->insert([
'id' => 3, 'invoice_no' => 'INV-MAIL-SENDING',
'purpose' => 'renewal', 'instance_id' => 3, 'order_id' => null,
'user_id' => 1, 'plan_id' => 5, 'renewal_id' => null,
'renews_expires_at' => $mailExpiry, 'due_at' => $mailExpiry,
'amount' => '12.00', 'currency' => 'USD', 'validity_days' => 30,
'traffic_quota_bytes' => 2048, 'status' => 'open',
'paid_via' => null, 'payment_reference' => null, 'paid_at' => null,
'created_at' => $now, 'updated_at' => $now,
]);
Capsule::table('cloud_mail_outbox')->insert([
'invoice_id' => 3, 'kind' => 'renewal_due', 'status' => 'sending',
'attempts' => 1, 'available_at' => $clockNow,
'lease_token' => str_repeat('f', 32),
'lease_until' => $clockNow + 300, 'sent_at' => null,
'last_error' => '', 'created_at' => $now, 'updated_at' => $now,
]);
$mailClaim = $execution->claimOne(3);
$assert(is_array($mailClaim),
'Mail-fenced compensation item was not claimed.');
$mailDeferred = $execution->applyClaim(
(int) $mailClaim['item_id'],
(string) $mailClaim['token']
);
$mailWaiting = CloudServiceCompensationItem::findOrFail(3);
$assert(($mailDeferred['state'] ?? '') === 'deferred'
&& ($mailDeferred['last_error_code'] ?? '')
=== 'renewal_mail_sending'
&& (int) $mailWaiting->attempts === 0
&& (int) $mailWaiting->available_at === $clockNow + 300
&& (int) CloudInstance::findOrFail(3)->expires_at === $mailExpiry
&& (string) Capsule::table('cloud_invoices')
->where('id', 3)->value('status') === 'open'
&& (string) Capsule::table('cloud_mail_outbox')
->where('invoice_id', 3)->value('status') === 'sending',
'Sending renewal reminder was not a recoverable compensation fence.');
for ($wait = 1; $wait < 10; $wait++) {
$mailWaiting = CloudServiceCompensationItem::findOrFail(3);
$clockNow = (int) $mailWaiting->available_at;
$repeatMailClaim = $execution->claimOne(3);
$assert(is_array($repeatMailClaim),
'Long-running renewal mail stopped being claimable for recheck.');
$execution->applyClaim(
(int) $repeatMailClaim['item_id'],
(string) $repeatMailClaim['token']
);
}
$mailWaiting = CloudServiceCompensationItem::findOrFail(3);
$assert((string) $mailWaiting->state === 'deferred'
&& (int) $mailWaiting->attempts === 0
&& (string) $mailWaiting->last_error_code
=== 'renewal_mail_sending',
'Long-running renewal mail exhausted compensation retry budget.');
Capsule::table('cloud_mail_outbox')->where('invoice_id', 3)->update([
'status' => 'failed', 'lease_token' => null, 'lease_until' => 0,
'available_at' => 0,
'last_error' => '邮件投递结果已核对', 'updated_at' => $clockNow,
]);
$clockNow = (int) $mailWaiting->available_at;
$mailResumedClaim = $execution->claimOne(3);
$assert(is_array($mailResumedClaim),
'Resolved renewal-mail fence did not make compensation claimable.');
$mailApplied = $execution->applyClaim(
(int) $mailResumedClaim['item_id'],
(string) $mailResumedClaim['token']
);
$replacementInvoiceId = (int) CloudServiceCompensationItem::findOrFail(3)
->replacement_invoice_id;
$assert(($mailApplied['state'] ?? '') === 'applied'
&& (int) CloudInstance::findOrFail(3)->expires_at
=== $mailExpiry + 3600
&& (string) Capsule::table('cloud_invoices')
->where('id', 3)->value('status') === 'void'
&& $replacementInvoiceId > 0
&& Capsule::table('cloud_mail_outbox')
->where('invoice_id', $replacementInvoiceId)
->where('kind', 'renewal_due')->where('status', 'pending')
->exists(),
'Resolved renewal-mail fence did not replace the invoice exactly once.');
echo "Service compensation execution tests passed.\n";
}

Loading…
Cancel
Save