Skip to content

Commit f351b87

Browse files
authored
Merge pull request #108 from ydb-platform/lambda-on-exception
Add lambda on exception in retryTransaction
2 parents 89ab62b + 76b3260 commit f351b87

File tree

4 files changed

+104
-15
lines changed

4 files changed

+104
-15
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
* added lambda on exception in retryTransaction
2+
13
## 1.10.0
24
* changed level of update token log record from info to debug
35
* created refresh token ratio parameter

src/Table.php

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -481,22 +481,45 @@ public function retrySession(Closure $userFunc, bool $idempotent = false, RetryP
481481

482482
}
483483

484-
public function retryTransaction(Closure $userFunc, bool $idempotent = false, RetryParams $params = null){
485-
486-
return $this->retrySession(function (Session $session) use ($userFunc) {
487-
try{
488-
$session->beginTransaction();
489-
$result = $userFunc($session);
490-
$session->commitTransaction();
491-
return $result;
492-
} catch (Exception $exception){
493-
try {
494-
$session->rollbackTransaction();
495-
} catch (Exception $e){}
496-
throw $exception;
497-
}
498-
}, $idempotent, $params);
484+
public function retryTransaction(Closure $userFunc, bool $idempotent = null, RetryParams $params = null, array $options){
485+
if ($options == null) {
486+
$options = [];
487+
}
499488

489+
if (isset($options['idempotent']) && !is_null($idempotent)){
490+
throw new \YdbPlatform\Ydb\Exception('Idempotent flag set in 2 params');
491+
}
492+
else if (!is_null($idempotent)) {
493+
$options['idempotent'] = $idempotent;
494+
} else {
495+
$options['idempotent'] = false;
496+
}
497+
498+
if (isset($options['retryParams']) && !is_null($params)){
499+
throw new \YdbPlatform\Ydb\Exception('RetryParams set in 2 params');
500+
}
501+
else if (!isset($options['retryParams'])) {
502+
$options['retryParams'] = $params;
503+
}
504+
505+
if (!isset($options['callback_on_error'])) {
506+
$options['callback_on_error'] = function (\Exception $exception) {};
507+
}
508+
return $this->retrySession(function (Session $session) use ($options, $userFunc) {
509+
try {
510+
$session->beginTransaction();
511+
$result = $userFunc($session);
512+
$session->commitTransaction();
513+
return $result;
514+
} catch (\Exception $exception) {
515+
$options['callback_on_error']($exception);
516+
try {
517+
$session->rollbackTransaction();
518+
} catch (Exception $e) {
519+
}
520+
throw $exception;
521+
}
522+
}, $options['idempotent'], $options['retryParams']);
500523
}
501524

502525
protected function deleteSession(string $exception): bool
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace YdbPlatform\Ydb\Test;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use YdbPlatform\Ydb\Exception;
7+
use YdbPlatform\Ydb\Retry\RetryParams;
8+
use YdbPlatform\Ydb\Session;
9+
use YdbPlatform\Ydb\Ydb;
10+
use YdbPlatform\Ydb\YdbTable;
11+
12+
class CheckParamsInRetryTransactionTest extends TestCase
13+
{
14+
public function testRun()
15+
{
16+
$config = [
17+
18+
// Database path
19+
'database' => '/local',
20+
21+
// Database endpoint
22+
'endpoint' => 'localhost:2136',
23+
24+
// Auto discovery (dedicated server only)
25+
'discovery' => false,
26+
27+
// IAM config
28+
'iam_config' => [
29+
'anonymous' => true,
30+
'insecure' => true
31+
],
32+
];
33+
34+
$ydb = new Ydb($config);
35+
36+
$table = $ydb->table();
37+
38+
try {
39+
$table->retryTransaction(function (Session $session){}, true, null, ['idempotent'=>true]);
40+
throw new \Exception('retryTransaction does not throw exception');
41+
} catch (\YdbPlatform\Ydb\Exception $e){
42+
self::assertEquals(1,1);
43+
}
44+
45+
try {
46+
$table->retryTransaction(function (Session $session){}, null, new RetryParams(), ['retryParams'=>new RetryParams()]);
47+
throw new \Exception('retryTransaction does not throw exception');
48+
} catch (\YdbPlatform\Ydb\Exception $e){
49+
self::assertEquals(1,1);
50+
}
51+
52+
}
53+
}

tests/RetryOnExceptionTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PHPUnit\Framework\TestCase;
66
use YdbPlatform\Ydb\Auth\Implement\AnonymousAuthentication;
7+
use YdbPlatform\Ydb\Exceptions\Grpc\ResourceExhaustedException;
78
use YdbPlatform\Ydb\Retry\RetryParams;
89
use YdbPlatform\Ydb\Session;
910
use YdbPlatform\Ydb\Table;
@@ -70,5 +71,15 @@ private function retryTest(Table $table)
7071
$tres
7172
);
7273
}, true, new RetryParams(2000));
74+
$i = 0;
75+
$table->retryTransaction(function (Session $session) use (&$i) {
76+
if($i == 0){
77+
throw new ResourceExhaustedException('Test exception');
78+
}
79+
self::assertEquals(5, $i);
80+
}, null, null, [
81+
'idempotent' => true,
82+
'callback_on_error' => function (\Exception $exception) use (&$i) {$i=5;}
83+
]);
7384
}
7485
}

0 commit comments

Comments
 (0)