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

310 lines
13 KiB
PHP

<?php
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$helperPath = __DIR__ . '/Support/CloudPublicNetworkMysqlFixture.php';
$testPath = __DIR__ . '/CloudPublicNetworkMysqlConcurrencyTest.php';
$helper = file_get_contents($helperPath);
$test = file_get_contents($testPath);
$assert(is_string($helper) && is_string($test),
'MySQL concurrency harness sources are missing.');
foreach ([
'VIRT_ALLOW_DESTRUCTIVE_MYSQL_TEST',
'VIRT_MYSQL_TEST_DSN',
'VIRT_MYSQL_TEST_USER',
'VIRT_MYSQL_TEST_PASSWORD',
'VIRT_MYSQL_TEST_ASSERT_GLOBAL_DEADLOCKS',
] as $gate) {
$assert(str_contains($helper, $gate),
'MySQL destructive gate is missing: ' . $gate);
}
$assert(
str_contains($helper, "str_starts_with(\$dsn, 'mysql:')")
&& str_contains($helper, "'/^virt_concurrency_[a-z0-9_]+\$/D'")
&& str_contains($helper, 'SELECT DATABASE()')
&& str_contains($helper, 'PDO::ATTR_DRIVER_NAME')
&& str_contains($helper, 'mysqlConcurrencyRequireGate();'),
'MySQL driver, isolated-schema, or child-process gate drifted.'
);
$assert(
str_contains($helper,
'SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ')
&& str_contains($helper, 'SET SESSION innodb_lock_wait_timeout = 3'),
'Required InnoDB transaction settings are missing.'
);
$assert(
str_contains($helper,
"SHOW GLOBAL STATUS LIKE 'Innodb_deadlocks'")
&& str_contains($helper,
'function mysqlConcurrencyGlobalDeadlockAssertionEnabled(')
&& substr_count($test, 'mysqlConcurrencyGlobalDeadlocks(') >= 2
&& str_contains($test,
'global deadlock counter is enforced; use an exclusive temporary MySQL server')
&& str_contains($test,
'validates final serialization only')
&& str_contains($test, 'does not claim deadlock'),
'Explicit global InnoDB deadlock assertion mode is missing.'
);
$spawnStart = strpos($helper, 'function mysqlConcurrencySpawn(');
$spawnEnd = $spawnStart === false ? false
: strpos($helper, 'function mysqlConcurrencyReleaseChild(', $spawnStart);
$assert($spawnStart !== false && $spawnEnd !== false,
'Independent child-process orchestration is missing.');
$spawn = substr($helper, $spawnStart, $spawnEnd - $spawnStart);
$assert(
str_contains($spawn, 'proc_open(')
&& str_contains($spawn, 'PHP_BINARY')
&& str_contains($spawn, "'--child'")
&& str_contains($spawn, 'MYSQL_CONCURRENCY_CHILD_PAYLOAD')
&& str_contains($spawn, 'stream_set_blocking($pipes[1], false)')
&& str_contains($spawn, 'stream_set_blocking($pipes[2], false)')
&& str_contains($spawn, 'mysqlConcurrencyWaitForReady(')
&& !str_contains($spawn, 'fgets(')
&& !str_contains($spawn, 'stream_set_timeout(')
&& !str_contains($spawn, 'MYSQL_CONCURRENCY_DSN')
&& !str_contains($spawn, 'MYSQL_CONCURRENCY_USER')
&& !str_contains($spawn, 'MYSQL_CONCURRENCY_PASSWORD'),
'Child must use an independent PHP/PDO process with credentials only in env.'
);
$assert(
str_contains($helper, 'function mysqlConcurrencyTerminateChild(')
&& str_contains($helper, 'function mysqlConcurrencyDrainPipes('),
'Child readiness timeout must terminate first and drain only nonblocking pipes.'
);
$assert(
str_contains($helper, 'microtime(true) + 15.0')
&& str_contains($helper, 'mysqlConcurrencyCanonicalSnapshot')
&& str_contains($helper, 'mysqlConcurrencyAssertLedger'),
'Parent watchdog or durable invariant projection is missing.'
);
$assert(
str_contains($helper, 'VIRT_MYSQL_CHILD_ENTERED_FILE')
&& str_contains($helper,
'function mysqlConcurrencyAcknowledgeEntered(')
&& str_contains($helper, 'function mysqlConcurrencyWaitForEntered(')
&& str_contains($helper, 'rename($temporary, $enteredFile)')
&& str_contains($helper, "query('SELECT CONNECTION_ID()')")
&& substr_count($helper, 'mysqlConcurrencyWaitForEntered(') >= 3,
'Same-actor races require atomic child-entered acknowledgements.'
);
$raceStart = strpos($helper, 'function mysqlConcurrencyRace(');
$raceEnd = $raceStart === false ? false
: strpos($helper, 'function mysqlConcurrencyAssertLedger(', $raceStart);
$assert($raceStart !== false && $raceEnd !== false,
'MySQL concurrency coordinator is missing.');
$race = substr($helper, $raceStart, $raceEnd - $raceStart);
$actorLock = strpos(
$race,
'SELECT id FROM `user` WHERE id = ? FOR UPDATE'
);
$spawnA = strpos($race, '$childA = mysqlConcurrencySpawn(');
$spawnB = strpos($race, '$childB = mysqlConcurrencySpawn(');
$releaseA = strpos($race, 'mysqlConcurrencyReleaseChild($childA);');
$releaseB = strpos($race, 'mysqlConcurrencyReleaseChild($childB);');
$enteredA = strpos($race, 'mysqlConcurrencyWaitForEntered($childA);');
$enteredB = strpos($race, 'mysqlConcurrencyWaitForEntered($childB);');
$sentinelCommit = strpos($race, '$sentinel->commit();');
$assert(
$actorLock !== false
&& $spawnA !== false && $spawnA > $actorLock
&& $spawnB !== false && $spawnB > $spawnA
&& $releaseA !== false && $releaseA > $spawnB
&& $releaseB !== false && $releaseB > $releaseA
&& $enteredA !== false && $enteredA > $releaseB
&& $enteredB !== false && $enteredB > $enteredA
&& $sentinelCommit !== false && $sentinelCommit > $enteredB,
'Same-actor races must hold the actor row until both children entered.'
);
$assert(
str_contains($helper,
'function mysqlConcurrencyAssertDestructiveSchemaSafety(')
&& str_contains($helper, 'information_schema.tables')
&& str_contains($helper, 'virt_concurrency_harness_marker')
&& str_contains($helper,
'virt-public-network-mysql-concurrency-v1')
&& str_contains($helper,
'mysqlConcurrencyAssertDestructiveSchemaSafety($pdo);'),
'Dedicated-schema marker safety is missing.'
);
$assert(
str_contains($helper,
'use App\Services\Cloud\CloudPublicNetworkAllocationService;')
&& str_contains($helper, "if (\$operation === 'reserve_bundle')")
&& str_contains($helper,
'CloudPublicNetworkAllocationService::reserveBundle(')
&& str_contains($helper, "\$payload['family'] ?? 4"),
'Allocator or exact-family child operation is missing.'
);
$hostRaceStart = strpos(
$helper,
'function mysqlConcurrencyHostBoundaryRace('
);
$hostRaceEnd = $hostRaceStart === false ? false : strpos(
$helper,
'function mysqlConcurrencyAssertTransitionResponseOperation(',
$hostRaceStart
);
$assert($hostRaceStart !== false && $hostRaceEnd !== false,
'Host-boundary concurrency coordinator is missing.');
$hostRace = substr($helper, $hostRaceStart, $hostRaceEnd - $hostRaceStart);
$hostLock = strpos(
$hostRace,
'SELECT id FROM cloud_hosts WHERE id = 1 FOR UPDATE'
);
$hostSpawnA = strpos($hostRace, '$childA = mysqlConcurrencySpawn(');
$hostSpawnB = strpos($hostRace, '$childB = mysqlConcurrencySpawn(');
$hostReleaseA = strpos($hostRace, 'mysqlConcurrencyReleaseChild($childA);');
$hostReleaseB = strpos($hostRace, 'mysqlConcurrencyReleaseChild($childB);');
$hostEnteredA = strpos($hostRace, 'mysqlConcurrencyWaitForEntered($childA);');
$hostEnteredB = strpos($hostRace, 'mysqlConcurrencyWaitForEntered($childB);');
$hostCommit = strpos($hostRace, '$sentinel->commit();');
$assert(
$hostLock !== false
&& $hostSpawnA !== false && $hostSpawnA > $hostLock
&& $hostSpawnB !== false && $hostSpawnB > $hostSpawnA
&& $hostReleaseA !== false && $hostReleaseA > $hostSpawnB
&& $hostReleaseB !== false && $hostReleaseB > $hostReleaseA
&& $hostEnteredA !== false && $hostEnteredA > $hostReleaseB
&& $hostEnteredB !== false && $hostEnteredB > $hostEnteredA
&& str_contains($hostRace, 'mysqlConcurrencyWaitForInstanceLock(')
&& substr_count($hostRace, 'mysqlConcurrencyWaitForHostLock(') === 2
&& $hostCommit !== false && $hostCommit > $hostEnteredB,
'Host-boundary races must block both real operations before release.'
);
$assert(
str_contains($helper, 'function mysqlConcurrencyWaitForHostLock(')
&& str_contains($helper, "query('SHOW FULL PROCESSLIST')")
&& str_contains($helper, "stripos(\$info, 'cloud_hosts')")
&& str_contains($helper, "stripos(\$info, 'for update')"),
'Host-boundary release must observe both real MySQL lock waiters.'
);
$resetStart = strpos($helper, 'function mysqlConcurrencyResetSchema(');
$resetEnd = $resetStart === false ? false
: strpos($helper, 'function mysqlConcurrencySeed(', $resetStart);
$assert($resetStart !== false && $resetEnd !== false,
'MySQL destructive reset seam is missing.');
$reset = substr($helper, $resetStart, $resetEnd - $resetStart);
$schemaStart = strpos($helper, 'function mysqlConcurrencySchemaTables(');
$schemaEnd = $schemaStart === false ? false
: strpos(
$helper,
'function mysqlConcurrencyAssertDestructiveSchemaSafety(',
$schemaStart
);
$assert($schemaStart !== false && $schemaEnd !== false,
'MySQL destructive table allowlist is missing.');
$schemaTables = substr($helper, $schemaStart, $schemaEnd - $schemaStart);
$assert(
strpos($reset, 'mysqlConcurrencyAssertDestructiveSchemaSafety($pdo);')
< strpos($reset, 'SET FOREIGN_KEY_CHECKS = 0')
&& !str_contains($reset,
'DROP TABLE IF EXISTS `virt_concurrency_harness_marker`')
&& !str_contains($schemaTables, 'virt_concurrency_harness_marker')
&& str_contains($helper,
'in_array(MYSQL_CONCURRENCY_MARKER_TABLE, $schemaTables, true)'),
'Reset must validate ownership before DDL and must retain the marker.'
);
$assert(
str_contains($helper, '$seedSourceOperations = [')
&& str_contains($helper,
'job.operation_id as job_operation_id')
&& str_contains($helper,
'transition.operation_id as transition_operation_id')
&& str_contains($helper,
'target_claim.operation_id as target_claim_operation_id')
&& str_contains($helper,
'target_ip.operation_id as target_ip_operation_id')
&& str_contains($helper,
'Seed source claim or IP operation identity changed.')
&& str_contains($helper,
'Changed target operation or ownership identity diverged.'),
'Operation-identity and changed-target ledger assertions are incomplete.'
);
$assert(
str_contains($helper, 'foreach ([2001, 2002] as $reservationId)')
&& str_contains($helper,
'function mysqlConcurrencyAssertHeldAllocationShape(')
&& str_contains($helper,
'Held allocator exposed a partial dual-stack claim set.')
&& str_contains($helper,
'Global public-address identity was duplicated.')
&& str_contains($helper, 'mysqlConcurrencyAssertHeldAllocationShape();'),
'Held allocator seed or atomic dual-stack ledger assertions are missing.'
);
$assert(
str_contains(
$helper,
"CREATE TABLE cloud_public_ips (\n"
. ' id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,'
),
'Sparse-address races require the production auto-increment IP identity.'
);
$assert(
str_contains($helper,
'function mysqlConcurrencyAssertTransitionResponseOperation(')
&& str_contains($helper,
'function mysqlConcurrencyAssertBatchResponseOperation(')
&& substr_count(
$test,
'mysqlConcurrencyAssertTransitionResponseOperation('
) >= 6
&& str_contains($test,
'mysqlConcurrencyAssertBatchResponseOperation(')
&& str_contains($test,
'hash_equals($firstOperationId, $replayOperationId)'),
'Winner and replay response operation identities are not database-bound.'
);
$assert(
str_contains($helper,
'CloudPublicNetworkClaimSetIdentity::fromLedger(')
&& str_contains($helper, 'CloudPublicNetworkSnapshot::fromArray(')
&& str_contains($helper,
'Transition target claim-set digest does not match its ledger.')
&& str_contains($helper,
'Transition source claim-set digest does not match its ledger.'),
'Transition claim-set digests are not recomputed from production types.'
);
$assert(str_contains($test,
'SKIP Cloud public-network MySQL concurrency: opt-in env is absent.'),
'No-credential execution must report an explicit SKIP.');
foreach ([
'Scenario 1: different actors contend on one instance',
'admin versus tenant',
'tenant versus admin',
'Scenario 2: exact duplicate replays one generation',
'exact idempotent replay',
'idempotency target conflict',
'either conflicting payload may win',
'Scenario 3: a batch and a single transition serialize',
'single versus batch',
'batch versus single',
'Scenario 4: two first-time allocators contend',
'allocator versus allocator',
'Scenario 5: first-time allocation and an exact sparse IPv6 transition',
'allocator versus exact IPv6 transition',
'Scenario 6: two distinct instances request the same exact sparse IPv6',
'two transitions request the same exact IPv6',
] as $scenario) {
$assert(str_contains($test, $scenario),
'Required MySQL race scenario is missing: ' . $scenario);
}
$assert(
substr_count($test, 'mysqlConcurrencyAssertCanonical(') >= 6
&& substr_count($test, 'mysqlConcurrencyHostBoundaryRace(') === 1
&& str_contains($test,
"->value('allocation_cursor')")
&& str_contains($test,
"inet_pton('2404:c140:156:3a::1002')")
&& str_contains($test,
'C11 will extend this harness with Worker-finalizer and unknown-effect recovery races.'),
'Sequential-oracle loser checks or explicit C11 scope marker is missing.'
);
echo "Cloud public-network MySQL concurrency contract passed.\n";