-
Notifications
You must be signed in to change notification settings - Fork 2k
feat: classify retryable transaction exceptions #10162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
memleakd
wants to merge
2
commits into
codeigniter4:4.8
Choose a base branch
from
memleakd:feat/retryable-transaction-exceptions
base: 4.8
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
system/Database/Exceptions/RetryableTransactionException.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * This file is part of CodeIgniter 4 framework. | ||
| * | ||
| * (c) CodeIgniter Foundation <admin@codeigniter.com> | ||
| * | ||
| * For the full copyright and license information, please view | ||
| * the LICENSE file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace CodeIgniter\Database\Exceptions; | ||
|
|
||
| class RetryableTransactionException extends DatabaseException | ||
| { | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
170 changes: 170 additions & 0 deletions
170
tests/system/Database/RetryableTransactionExceptionTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * This file is part of CodeIgniter 4 framework. | ||
| * | ||
| * (c) CodeIgniter Foundation <admin@codeigniter.com> | ||
| * | ||
| * For the full copyright and license information, please view | ||
| * the LICENSE file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace CodeIgniter\Database; | ||
|
|
||
| use CodeIgniter\Database\Exceptions\DatabaseException; | ||
| use CodeIgniter\Database\Exceptions\RetryableTransactionException; | ||
| use CodeIgniter\Database\MySQLi\Connection as MySQLiConnection; | ||
| use CodeIgniter\Database\OCI8\Connection as OCI8Connection; | ||
| use CodeIgniter\Database\Postgre\Connection as PostgreConnection; | ||
| use CodeIgniter\Database\SQLite3\Connection as SQLite3Connection; | ||
| use CodeIgniter\Database\SQLSRV\Connection as SQLSRVConnection; | ||
| use CodeIgniter\Test\CIUnitTestCase; | ||
| use CodeIgniter\Test\Mock\MockConnection; | ||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use PHPUnit\Framework\Attributes\Group; | ||
| use ReflectionMethod; | ||
|
|
||
| /** | ||
| * @internal | ||
| */ | ||
| #[Group('Others')] | ||
| final class RetryableTransactionExceptionTest extends CIUnitTestCase | ||
| { | ||
| #[DataProvider('provideCreatesRetryableTransactionExceptions')] | ||
| public function testCreatesRetryableTransactionExceptions(BaseConnection $db, int|string $code): void | ||
| { | ||
| $exception = self::createDatabaseException($db, 'Retryable transaction failure.', $code); | ||
|
|
||
| $this->assertInstanceOf(RetryableTransactionException::class, $exception); | ||
| $this->assertSame($code, $exception->getDatabaseCode()); | ||
| } | ||
|
|
||
| /** | ||
| * @return iterable<string, array{BaseConnection, int|string}> | ||
| */ | ||
| public static function provideCreatesRetryableTransactionExceptions(): iterable | ||
| { | ||
| yield 'MySQLi deadlock' => [self::connection(MySQLiConnection::class, 'MySQLi'), 1213]; | ||
|
|
||
| yield 'Postgre serialization failure' => [self::connection(PostgreConnection::class, 'Postgre'), '40001']; | ||
|
|
||
| yield 'Postgre deadlock' => [self::connection(PostgreConnection::class, 'Postgre'), '40P01']; | ||
|
|
||
| yield 'SQLite busy' => [self::connection(SQLite3Connection::class, 'SQLite3'), 5]; | ||
|
|
||
| yield 'SQLSRV deadlock' => [self::connection(SQLSRVConnection::class, 'SQLSRV'), '40001/1205']; | ||
|
|
||
| yield 'SQLSRV vendor deadlock' => [self::connection(SQLSRVConnection::class, 'SQLSRV'), 1205]; | ||
|
|
||
| yield 'SQLSRV snapshot isolation conflict' => [self::connection(SQLSRVConnection::class, 'SQLSRV'), 'HY000/3960']; | ||
|
|
||
| if (defined('OCI_COMMIT_ON_SUCCESS')) { | ||
| yield 'OCI8 deadlock' => [self::connection(OCI8Connection::class, 'OCI8'), 60]; | ||
|
|
||
| yield 'OCI8 serialization failure' => [self::connection(OCI8Connection::class, 'OCI8'), 8177]; | ||
| } | ||
| } | ||
|
|
||
| #[DataProvider('provideCreatesBaseDatabaseExceptionsForNonRetryableErrors')] | ||
| public function testCreatesBaseDatabaseExceptionsForNonRetryableErrors(BaseConnection $db, int|string $code): void | ||
| { | ||
| $exception = self::createDatabaseException($db, 'Non-retryable transaction failure.', $code); | ||
|
|
||
| $this->assertNotInstanceOf(RetryableTransactionException::class, $exception); | ||
| } | ||
|
|
||
| /** | ||
| * @return iterable<string, array{BaseConnection, int|string}> | ||
| */ | ||
| public static function provideCreatesBaseDatabaseExceptionsForNonRetryableErrors(): iterable | ||
| { | ||
| yield 'Base connection default' => [self::connection(MockConnection::class, 'MockDriver'), 1213]; | ||
|
|
||
| yield 'MySQLi lock wait timeout' => [self::connection(MySQLiConnection::class, 'MySQLi'), 1205]; | ||
|
|
||
| yield 'MySQLi duplicate key' => [self::connection(MySQLiConnection::class, 'MySQLi'), 1062]; | ||
|
|
||
| yield 'Postgre unique violation' => [self::connection(PostgreConnection::class, 'Postgre'), '23505']; | ||
|
|
||
| yield 'Postgre exclusion violation' => [self::connection(PostgreConnection::class, 'Postgre'), '23P01']; | ||
|
|
||
| yield 'SQLite locked' => [self::connection(SQLite3Connection::class, 'SQLite3'), 6]; | ||
|
|
||
| yield 'SQLite busy snapshot extended code' => [self::connection(SQLite3Connection::class, 'SQLite3'), 517]; | ||
|
|
||
| yield 'SQLite constraint' => [self::connection(SQLite3Connection::class, 'SQLite3'), 19]; | ||
|
|
||
| yield 'SQLSRV lock timeout' => [self::connection(SQLSRVConnection::class, 'SQLSRV'), 'HYT00/1222']; | ||
|
|
||
| yield 'SQLSRV SQLSTATE without vendor code' => [self::connection(SQLSRVConnection::class, 'SQLSRV'), '40001']; | ||
|
|
||
| yield 'SQLSRV unique constraint' => [self::connection(SQLSRVConnection::class, 'SQLSRV'), '23000/2627']; | ||
|
|
||
| yield 'SQLSRV unique index' => [self::connection(SQLSRVConnection::class, 'SQLSRV'), '23000/2601']; | ||
|
|
||
| if (defined('OCI_COMMIT_ON_SUCCESS')) { | ||
| yield 'OCI8 resource busy' => [self::connection(OCI8Connection::class, 'OCI8'), 54]; | ||
|
|
||
| yield 'OCI8 unique constraint' => [self::connection(OCI8Connection::class, 'OCI8'), 1]; | ||
| } | ||
| } | ||
|
|
||
| public function testQueryThrowsRetryableTransactionExceptionFromDriverExecutionPath(): void | ||
| { | ||
| $db = $this->getMockBuilder(MySQLiConnection::class) | ||
| ->setConstructorArgs([self::config('MySQLi')]) | ||
| ->onlyMethods(['connect', 'execute']) | ||
| ->getMock(); | ||
|
|
||
| $db->method('connect')->willReturn(mysqli_init()); | ||
| $db->method('execute')->willThrowException( | ||
| self::createDatabaseException($db, 'Deadlock found when trying to get lock.', 1213), | ||
| ); | ||
|
|
||
| $this->expectException(RetryableTransactionException::class); | ||
|
|
||
| $db->query('SELECT * FROM test'); | ||
| } | ||
|
|
||
| /** | ||
| * @param class-string<BaseConnection> $connectionClass | ||
| */ | ||
| private static function connection(string $connectionClass, string $driver): BaseConnection | ||
| { | ||
| return new $connectionClass(self::config($driver)); | ||
| } | ||
|
|
||
| /** | ||
| * @return array<string, mixed> | ||
| */ | ||
| private static function config(string $driver): array | ||
| { | ||
| return [ | ||
| 'DSN' => '', | ||
| 'hostname' => 'localhost', | ||
| 'username' => '', | ||
| 'password' => '', | ||
| 'database' => 'test', | ||
| 'DBDriver' => $driver, | ||
| 'DBDebug' => true, | ||
| 'charset' => 'utf8', | ||
| 'DBCollat' => 'utf8_general_ci', | ||
| 'swapPre' => '', | ||
| 'encrypt' => false, | ||
| 'compress' => false, | ||
| 'failover' => [], | ||
| ]; | ||
| } | ||
|
|
||
| private static function createDatabaseException( | ||
| BaseConnection $db, | ||
| string $message, | ||
| int|string $code, | ||
| ): DatabaseException { | ||
| $method = new ReflectionMethod($db, 'createDatabaseException'); | ||
|
|
||
| return $method->invoke($db, $message, $code); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One thing I think can be improved is, let
$this->createDatabaseExceptionhandle all types of exception includingUniqueConstraintViolation. Similar toisRetryableTransactionErrorCode, there could be a protected function likeisUniqueConstraintViolation(int|string $code): booland this function will be used bycreateDatabaseExceptionfunction and it returnsUniqueConstraintViolationExceptionfor that particular error code. In this way, we can avoid this ternary operator in all connection implementations, and in future, if we need to add more refined exceptions, we can do so just inBaseConnectionwithout messy ternary operatorsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, definitely a good idea.