You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/tests/CloudCatalogLockOrderContra...

251 lines
8.5 KiB
PHTML

<?php
declare(strict_types=1);
$root = dirname(__DIR__);
$worker = file_get_contents(
$root . '/app/Services/Cloud/CloudWorkerService.php'
);
$recovery = file_get_contents(
$root . '/app/Services/Cloud/CloudOperationRecoveryService.php'
);
$automatic = file_get_contents(
$root . '/app/Services/Cloud/CloudAutomaticOperationRecoveryService.php'
);
$jobs = file_get_contents(
$root . '/app/Services/Cloud/CloudJobService.php'
);
$templates = file_get_contents(
$root . '/app/Services/Cloud/CloudTemplateCatalogService.php'
);
$isos = file_get_contents(
$root . '/app/Services/Cloud/CloudUserIsoService.php'
);
if (!is_string($worker) || !is_string($recovery) || !is_string($automatic)
|| !is_string($jobs)
|| !is_string($templates) || !is_string($isos)) {
throw new RuntimeException('Cannot read catalog lock-order sources.');
}
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$method = static function (
string $source,
string $start,
string $end
): string {
$from = strpos($source, $start);
$to = $from === false ? false : strpos($source, $end, $from + 1);
return $from === false || $to === false
? '' : substr($source, $from, $to - $from);
};
$ordered = static function (
string $source,
array $needles
): bool {
$offset = -1;
foreach ($needles as $needle) {
$next = strpos($source, $needle, $offset + 1);
if ($next === false || $next <= $offset) {
return false;
}
$offset = $next;
}
return true;
};
$finish = $method(
$worker,
'$aggregateParentId = (int) ($claimed->parent_job_id ?? 0);',
'private static function throwInvalidSettingsResult('
);
$assert($ordered($finish, [
"CloudJob::where('id', \$aggregateParentId)",
'CloudTemplateCatalogService::lockAggregateCatalogLocked(',
'CloudUserIsoService::lockAggregateCatalogLocked(',
"CloudJob::where('id', \$claimed->id)",
'CloudTemplateCatalogService::finishJobLocked(',
]), 'Worker completion does not lock parent, catalog, child, replica in order.');
foreach ([
'prepareApplied' => [
'end' => 'public static function commitApplied(',
'resolution' => 'applied',
],
'prepareNotApplied' => [
'end' => 'public static function commitNotApplied(',
'resolution' => 'not_applied',
],
] as $name => $contract) {
$wrapper = $method(
$recovery,
'public static function ' . $name . '(',
$contract['end']
);
$assert($ordered($wrapper, [
'return self::prepareResolution(',
"'" . $contract['resolution'] . "'",
]), $name . ' does not preserve its public resolution adapter.');
}
foreach ([
'commitApplied' => 'applied',
'commitNotApplied' => 'not_applied',
] as $name => $resolution) {
$wrapper = $method(
$recovery,
'public static function ' . $name . '(',
$name === 'commitApplied'
? 'public static function prepareNotApplied('
: 'private static function prepareResolution('
);
$assert($ordered($wrapper, [
'return self::commitResolution(',
"'" . $resolution . "'",
'$resolvedBy',
'$completionEvent',
]), $name . ' does not preserve its public resolution adapter.');
}
$commit = $method(
$recovery,
'private static function commitResolution(',
'public static function settleLateReceipt('
);
$assert($ordered($commit, [
'$aggregateParent = CloudJob::where(',
'$aggregateParentId',
'CloudTemplateCatalogService::lockAggregateCatalogLocked(',
'CloudUserIsoService::lockAggregateCatalogLocked(',
"CloudJob::where('id', \$jobId)",
]), 'Shared recovery commit does not lock parent, catalog, then child.');
$assert($ordered($commit, [
'self::assertLockedAgentRecoveryEnvelope(',
'self::assertEvidenceSource(',
'self::assertOperationIdentity(',
'self::lockLateReceiptEvent(',
'self::assertRecoveryCommitMayPublish(',
'self::assertRecoverableEvidence(',
'self::preparedMarker(',
'self::preparedMarkerMatches(',
'$job->effect_resolution = $resolution;',
'$job->sealed_payload = self::finalizedRecoveryPayload($job);',
'$job->save();',
'CloudJobService::reconcileAggregateParentEffectLocked(',
]), 'Shared recovery commit changed validation or ledger publish order.');
$commitSave = strpos($commit, '$job->save();');
$commitReconcile = strpos(
$commit,
'CloudJobService::reconcileAggregateParentEffectLocked('
);
$commitAck = strrpos($commit, 'self::markLateReceiptApplied(');
$assert($commitSave !== false && $commitReconcile !== false
&& $commitAck !== false && $commitSave < $commitReconcile
&& $commitReconcile < $commitAck,
'Shared recovery commit ACKs a receipt before ledger convergence.');
$automaticCommit = $method(
$automatic,
'private static function resolveFenceAndCommit(',
'private static function assertFreshInspection('
);
$assert($ordered($automaticCommit, [
'$aggregateParent = CloudJob::where(',
'$aggregateParentId',
'CloudTemplateCatalogService::lockAggregateCatalogLocked(',
'CloudUserIsoService::lockAggregateCatalogLocked(',
"\$aggregateChildren = CloudJob::where(",
"'parent_job_id',",
'$aggregateParentId',
]), 'Automatic fence commit locks the child before its catalog.');
$aggregateCommit = $method(
$jobs,
'public static function reconcileAggregateParentEffectLocked(',
'private static function wherePublishedAggregateNeedsRecheck('
);
$assert($ordered($aggregateCommit, [
"CloudJob::where('id', \$parentId)",
'CloudTemplateCatalogService::lockAggregateCatalogLocked(',
'CloudUserIsoService::lockAggregateCatalogLocked(',
"\$children = CloudJob::where('parent_job_id', \$parentId)",
]), 'Aggregate recovery commit locks children before the catalog.');
$legacy = $method(
$jobs,
'public static function reconcileOneLegacyAggregateEffect(): bool',
'private static function aggregateRelationshipConflict('
);
$assert($ordered($legacy, [
'->lockForUpdate()',
'CloudTemplateCatalogService::lockAggregateCatalogLocked(',
'CloudUserIsoService::lockAggregateCatalogLocked(',
'$children = CloudJob::where(',
"'parent_job_id',",
]), 'Legacy aggregate sweep locks children before the catalog.');
$templateMutation = $method(
$templates,
'private static function lockTemplateForMutation(',
'private static function assertMutationBoundaryLocked('
);
$isoMutation = $method(
$isos,
'private static function lockIsoForMutation(',
'private static function assertMutationBoundaryLocked('
);
$assert($ordered($templateMutation, [
'$parentSnapshot = $jobId > 0',
'self::isTemplateAggregateParent($parentSnapshot)',
"? CloudJob::where('id', \$jobId)",
"->where('execution_mode', 'aggregate')",
"->whereNull('parent_job_id')",
'->lockForUpdate()->first()',
'$template = CloudTemplate::query()',
'->lockForUpdate()->first();',
'self::assertMutationBoundaryLocked(',
]), 'Template mutation still locks the catalog before its prior parent.');
$assert($ordered($isoMutation, [
'$parentSnapshot = $jobId > 0',
'self::isIsoAggregateParent($parentSnapshot)',
"? CloudJob::where('id', \$jobId)",
"->where('execution_mode', 'aggregate')",
"->whereNull('parent_job_id')",
'->lockForUpdate()->first()',
'$iso = CloudUserIso::query()',
'->lockForUpdate()->first();',
'self::assertMutationBoundaryLocked(',
]), 'ISO mutation still locks the catalog before its prior parent.');
$isoImportMutation = $method(
$isos,
'private static function lockIsoRowsForImport(',
'private static function assertMutationBoundaryLocked('
);
$assert($ordered($isoImportMutation, [
'$parentSnapshots =',
'$parentIds =',
'self::isIsoAggregateParent($job)',
'$parents =',
"->where('execution_mode', 'aggregate')",
"->whereNull('parent_job_id')",
"->orderBy('id')->lockForUpdate()->get()->keyBy('id')",
'$rows = CloudUserIso::query()',
'->lockForUpdate()->get();',
]), 'ISO import locks an invalid pointer or locks catalogs before parents.');
$templateDelete = $method(
$templates,
'public static function delete(',
'public static function finishJobLocked('
);
$assert($ordered($templateDelete, [
'self::assertNoOrderReferencesLocked($id);',
'self::lockTemplateForMutation($id);',
'self::assertNoJobReferencesLocked($id);',
]), 'Template deletion locks its catalog before pending orders.');
echo "Cloud catalog lock-order contract checks passed.\n";