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...

192 lines
6.5 KiB
PHP

<?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 (['commitApplied', 'commitNotApplied'] as $name) {
$commit = $method(
$recovery,
'public static function ' . $name . '(',
$name === 'commitApplied'
? 'public static function prepareNotApplied('
: 'private static function assertResolvedBy('
);
$assert($ordered($commit, [
'$aggregateParent = CloudJob::where(',
'$aggregateParentId',
'CloudTemplateCatalogService::lockAggregateCatalogLocked(',
'CloudUserIsoService::lockAggregateCatalogLocked(',
"CloudJob::where('id', \$jobId)",
]), $name . ' takes a child lock before its catalog lock.');
}
$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";