|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of CodeIgniter 4 framework. |
| 7 | + * |
| 8 | + * (c) CodeIgniter Foundation <admin@codeigniter.com> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view |
| 11 | + * the LICENSE file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace CodeIgniter\Database\Live\MySQLi; |
| 15 | + |
| 16 | +use CodeIgniter\Database\BaseConnection; |
| 17 | +use CodeIgniter\Test\CIUnitTestCase; |
| 18 | +use CodeIgniter\Test\DatabaseTestTrait; |
| 19 | +use Config\Database; |
| 20 | +use mysqli; |
| 21 | +use PHPUnit\Framework\Attributes\Group; |
| 22 | +use Throwable; |
| 23 | + |
| 24 | +/** |
| 25 | + * InnoDB errno 1213 rolls the victim transaction back. These tests lock two |
| 26 | + * rows in opposite orders so the CodeIgniter connection is that victim. |
| 27 | + * |
| 28 | + * Errno 1205 is a different outcome and is not a passing result here. |
| 29 | + * |
| 30 | + * @internal |
| 31 | + */ |
| 32 | +#[Group('DatabaseLive')] |
| 33 | +final class DeadlockTransactionTest extends CIUnitTestCase |
| 34 | +{ |
| 35 | + use DatabaseTestTrait; |
| 36 | + |
| 37 | + protected $migrate = false; |
| 38 | + private ?BaseConnection $waiter = null; |
| 39 | + private ?mysqli $held = null; |
| 40 | + private ?string $probeTable = null; |
| 41 | + |
| 42 | + protected function setUp(): void |
| 43 | + { |
| 44 | + $config = config('Database'); |
| 45 | + |
| 46 | + if ($config->tests['DBDriver'] !== 'MySQLi') { |
| 47 | + $this->markTestSkipped('Only MySQLi can complete this test.'); |
| 48 | + } |
| 49 | + |
| 50 | + $this->db = Database::connect($this->DBGroup, false); |
| 51 | + |
| 52 | + parent::setUp(); |
| 53 | + } |
| 54 | + |
| 55 | + protected function tearDown(): void |
| 56 | + { |
| 57 | + $this->releaseDeadlock(); |
| 58 | + |
| 59 | + parent::tearDown(); |
| 60 | + } |
| 61 | + |
| 62 | + public function testFollowingWriteDoesNotAutocommitAndTransCommitReturnsFalse(): void |
| 63 | + { |
| 64 | + $this->assertDeadlockVictimDoesNotPublishTheNextWrite(false); |
| 65 | + } |
| 66 | + |
| 67 | + public function testFollowingWriteDoesNotAutocommitAndTransCompleteRollsItBack(): void |
| 68 | + { |
| 69 | + $this->assertDeadlockVictimDoesNotPublishTheNextWrite(true); |
| 70 | + } |
| 71 | + |
| 72 | + private function assertDeadlockVictimDoesNotPublishTheNextWrite(bool $useComplete): void |
| 73 | + { |
| 74 | + $table = $this->db->escapeIdentifier($this->db->prefixTable('deadlock_probe')); |
| 75 | + $this->probeTable = $table; |
| 76 | + $this->forceDeadlock($table); |
| 77 | + |
| 78 | + $this->assertSame(1213, (int) $this->db->error()['code']); |
| 79 | + $this->assertSame(1, $this->db->transDepth); |
| 80 | + |
| 81 | + $this->db->query('INSERT INTO ' . $table . " (id, note) VALUES (102, 'after')"); |
| 82 | + |
| 83 | + $visibleBefore = $this->committedIds($table); |
| 84 | + $finished = $useComplete ? $this->db->transComplete() : $this->db->transCommit(); |
| 85 | + $visibleAfter = $this->committedIds($table); |
| 86 | + |
| 87 | + $this->assertSame( |
| 88 | + ['visible_before' => [], 'finished' => false, 'visible_after' => []], |
| 89 | + [ |
| 90 | + 'visible_before' => $visibleBefore, |
| 91 | + 'finished' => $finished, |
| 92 | + 'visible_after' => $visibleAfter, |
| 93 | + ], |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Make $this->db the InnoDB deadlock victim. The victim row is id 101. |
| 99 | + */ |
| 100 | + private function forceDeadlock(string $table): void |
| 101 | + { |
| 102 | + for ($attempt = 1; $attempt <= 3; $attempt++) { |
| 103 | + $this->releaseDeadlock(); |
| 104 | + $this->resetProbe($table); |
| 105 | + |
| 106 | + $this->waiter = Database::connect($this->DBGroup, false); |
| 107 | + $this->waiter->initialize(); |
| 108 | + $this->waiter->query('SET SESSION innodb_lock_wait_timeout = 3'); |
| 109 | + $this->waiter->transStart(); |
| 110 | + |
| 111 | + for ($i = 0; $i < 20; $i++) { |
| 112 | + $this->waiter->query('UPDATE ' . $table . " SET note = 'w{$i}' WHERE id = 1"); |
| 113 | + } |
| 114 | + |
| 115 | + $held = $this->waiter->getConnection(); |
| 116 | + $this->assertInstanceOf(mysqli::class, $held); |
| 117 | + $this->held = $held; |
| 118 | + |
| 119 | + // The victim has to lock row 2 before the waiter asks for it. |
| 120 | + // Sending that request first lets the waiter take row 2, and the |
| 121 | + // victim then waits out innodb_lock_wait_timeout (errno 1205). |
| 122 | + $this->db->query('SET SESSION innodb_lock_wait_timeout = 3'); |
| 123 | + $this->db->transException(false); |
| 124 | + $this->db->transStart(); |
| 125 | + $this->db->query('INSERT INTO ' . $table . " (id, note) VALUES (101, 'victim')"); |
| 126 | + $this->db->query('UPDATE ' . $table . " SET note = 'held' WHERE id = 2"); |
| 127 | + |
| 128 | + $this->held->query('UPDATE ' . $table . " SET note = 'waiting' WHERE id = 2", MYSQLI_ASYNC); |
| 129 | + usleep(400000); |
| 130 | + |
| 131 | + $this->db->query('UPDATE ' . $table . " SET note = 'closing' WHERE id = 1"); |
| 132 | + |
| 133 | + if ((int) $this->db->error()['code'] === 1213) { |
| 134 | + return; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + $this->fail('Precondition: InnoDB did not report errno 1213 on the CodeIgniter connection.'); |
| 139 | + } |
| 140 | + |
| 141 | + private function resetProbe(string $table): void |
| 142 | + { |
| 143 | + if ($this->db->transDepth > 0) { |
| 144 | + $this->db->transRollback(); |
| 145 | + } |
| 146 | + |
| 147 | + $this->db->query('DROP TABLE IF EXISTS ' . $table); |
| 148 | + $this->db->query( |
| 149 | + 'CREATE TABLE ' . $table . ' (' |
| 150 | + . 'id INT NOT NULL, ' |
| 151 | + . 'note VARCHAR(32) NOT NULL, ' |
| 152 | + . 'PRIMARY KEY (id)' |
| 153 | + . ') ENGINE=InnoDB', |
| 154 | + ); |
| 155 | + $this->db->query('INSERT INTO ' . $table . " (id, note) VALUES (1, 'one'), (2, 'two')"); |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * @return list<int> |
| 160 | + */ |
| 161 | + private function committedIds(string $table): array |
| 162 | + { |
| 163 | + $observer = Database::connect($this->DBGroup, false); |
| 164 | + $observer->initialize(); |
| 165 | + $rows = $observer->query('SELECT id FROM ' . $table . ' WHERE id IN (101, 102) ORDER BY id')->getResultArray(); |
| 166 | + $observer->close(); |
| 167 | + |
| 168 | + $ids = []; |
| 169 | + |
| 170 | + foreach ($rows as $row) { |
| 171 | + $ids[] = (int) $row['id']; |
| 172 | + } |
| 173 | + |
| 174 | + return $ids; |
| 175 | + } |
| 176 | + |
| 177 | + private function releaseDeadlock(): void |
| 178 | + { |
| 179 | + if ($this->held instanceof mysqli) { |
| 180 | + try { |
| 181 | + $this->held->reap_async_query(); |
| 182 | + } catch (Throwable) { |
| 183 | + // The waiting statement already finished, or the socket is closed. |
| 184 | + } |
| 185 | + |
| 186 | + $this->held = null; |
| 187 | + } |
| 188 | + |
| 189 | + if ($this->waiter instanceof BaseConnection) { |
| 190 | + if ($this->waiter->transDepth > 0) { |
| 191 | + $this->waiter->transRollback(); |
| 192 | + } |
| 193 | + |
| 194 | + $this->waiter->close(); |
| 195 | + $this->waiter = null; |
| 196 | + } |
| 197 | + |
| 198 | + if ($this->db instanceof BaseConnection && $this->db->transDepth > 0) { |
| 199 | + $this->db->transRollback(); |
| 200 | + } |
| 201 | + |
| 202 | + if ($this->probeTable !== null && $this->db instanceof BaseConnection) { |
| 203 | + $this->db->query('DROP TABLE IF EXISTS ' . $this->probeTable); |
| 204 | + $this->probeTable = null; |
| 205 | + } |
| 206 | + } |
| 207 | +} |
0 commit comments