Skip to content

Commit da43f31

Browse files
committed
Update composer.json
1 parent e8abcdd commit da43f31

File tree

9 files changed

+229
-146
lines changed

9 files changed

+229
-146
lines changed

examples/app/Commands/BasicExampleCommand.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace App\Commands;
44

55
use App\AppService;
6+
use YdbPlatform\Ydb\Retry\Backoff;
7+
use YdbPlatform\Ydb\Retry\RetryParams;
68
use YdbPlatform\Ydb\Session;
79
use YdbPlatform\Ydb\Ydb;
810
use YdbPlatform\Ydb\YdbTable;
@@ -234,6 +236,7 @@ protected function describeTable($table)
234236

235237
protected function fillTablesWithData()
236238
{
239+
$params = new RetryParams(4000,null,new Backoff(10,1000));
237240
$this->ydb->table()->retryTransaction(function (Session $session) {
238241

239242
$prepared_query = $session->prepare($this->getFillDataQuery());
@@ -244,16 +247,15 @@ protected function fillTablesWithData()
244247
'episodesData' => $this->getEpisodesData(),
245248
]);
246249

247-
});
250+
}, false, $params);
248251

249252
$this->print('Finished.');
250253
}
251254

252255
protected function selectSimple()
253256
{
257+
$params = new RetryParams(4000,new Backoff(3,20));
254258
$result = $this->ydb->table()->retryTransaction(function (Session $session) {
255-
256-
$result = $session->transaction(function (Session $session) {
257259
return $session->query('
258260
$format = DateTime::Format("%Y-%m-%d");
259261
SELECT
@@ -262,8 +264,7 @@ protected function selectSimple()
262264
$format(DateTime::FromSeconds(CAST(release_date AS Uint32))) AS release_date
263265
FROM series
264266
WHERE series_id = 1;');
265-
});
266-
});
267+
}, true, $params);
267268

268269
$this->print($result->rows());
269270
}
@@ -338,7 +339,7 @@ protected function selectPrepared($series_id, $season_id, $episode_id)
338339
*/
339340
protected function explicitTcl($series_id, $season_id, $episode_id)
340341
{
341-
$this->ydb->table()->transaction(function (Session $session) use ($series_id, $season_id, $episode_id) {
342+
$this->ydb->table()->retryTransaction(function (Session $session) use ($series_id, $season_id, $episode_id) {
342343

343344
$prepared_query = $session->prepare('
344345
DECLARE $today AS Uint64;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace YdbPlatform\Ydb\Exceptions\Grpc;
4+
5+
use YdbPlatform\Ydb\Exceptions\NonRetryableException;
6+
7+
class CanceledException extends NonRetryableException
8+
{
9+
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace YdbPlatform\Ydb\Exceptions\Grpc;
4+
5+
use YdbPlatform\Ydb\Exceptions\RetryableException;
6+
7+
class DeadlineExceededException extends RetryableException
8+
{
9+
10+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace YdbPlatform\Ydb\Exceptions\Grpc;
4+
5+
class InvalidArgumentException extends \YdbPlatform\Ydb\Exceptions\NonRetryableException
6+
{
7+
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace YdbPlatform\Ydb\Exceptions\Grpc;
4+
5+
use YdbPlatform\Ydb\Exceptions\NonRetryableException;
6+
7+
class UnknownException extends NonRetryableException
8+
{
9+
}

src/Retry/Retry.php

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,15 @@
33
namespace YdbPlatform\Ydb\Retry;
44

55
use Closure;
6+
use YdbPlatform\Ydb\Exception;
7+
use YdbPlatform\Ydb\Exceptions\Grpc\DeadlineExceededException;
68
use YdbPlatform\Ydb\Exceptions\NonRetryableException;
79
use YdbPlatform\Ydb\Exceptions\RetryableException;
10+
use YdbPlatform\Ydb\Exceptions\Ydb\AbortedException;
11+
use YdbPlatform\Ydb\Exceptions\Ydb\BadSessionException;
12+
use YdbPlatform\Ydb\Exceptions\Ydb\SessionBusyException;
13+
use YdbPlatform\Ydb\Exceptions\Ydb\UnavailableException;
14+
use YdbPlatform\Ydb\Exceptions\Ydb\UndeterminedException;
815

916
class Retry
1017
{
@@ -72,16 +79,47 @@ public function retry(Closure $closure, bool $idempotent){
7279
while (microtime(true) < $startTime+$this->timeoutMs/1000){
7380
try {
7481
return $closure();
82+
} catch (UndeterminedException $undeterminedException){
83+
$lastException = $undeterminedException;
84+
if (!$idempotent){
85+
break;
86+
}
87+
$retryCount++;
88+
$this->retryDelay($retryCount,$this->backoffType($e));
7589
} catch (RetryableException $e){
7690
$retryCount++;
77-
$this->retryDelay($retryCount,
78-
$e->isFastBackoff() ? $params->getFastBackOff() : $params->getSlowBackOff());
91+
$this->retryDelay($retryCount,$this->backoffType($e));
7992
$lastException = $e;
80-
} catch (NonRetryableException $e){
93+
} catch (Exception $e){
8194
throw $e;
8295
}
8396
}
8497
throw $lastException;
8598
}
8699

100+
/**
101+
* @param RetryableException $e
102+
* @return Backoff
103+
*/
104+
protected function backoffType(RetryableException $e): Backoff
105+
{
106+
if ($e instanceof AbortedException){
107+
return $this->fastBackOff;
108+
} elseif ($e instanceof BadSessionException) {
109+
return $this->fastBackOff;
110+
} elseif ($e instanceof SessionBusyException) {
111+
return $this->fastBackOff;
112+
} elseif ($e instanceof UndeterminedException) {
113+
return $this->fastBackOff;
114+
} elseif ($e instanceof UnavailableException) {
115+
return $this->fastBackOff;
116+
} elseif ($e instanceof UndeterminedException) {
117+
return $this->fastBackOff;
118+
} elseif ($e instanceof DeadlineExceededException){
119+
return $this->fastBackOff;
120+
} else {
121+
return $this->slowBackOff;
122+
}
123+
}
124+
87125
}

src/Table.php

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@
88
use Ydb\Table\Query;
99
use Ydb\Table\V1\TableServiceClient as ServiceClient;
1010
use YdbPlatform\Ydb\Contracts\SessionPoolContract;
11+
use YdbPlatform\Ydb\Exceptions\Grpc\InvalidArgumentException;
12+
use YdbPlatform\Ydb\Exceptions\Grpc\UnknownException;
1113
use YdbPlatform\Ydb\Exceptions\NonRetryableException;
1214
use YdbPlatform\Ydb\Exceptions\RetryableException;
1315
use YdbPlatform\Ydb\Exceptions\Ydb\BadSessionException;
16+
use YdbPlatform\Ydb\Exceptions\Ydb\SessionBusyException;
17+
use YdbPlatform\Ydb\Exceptions\Ydb\SessionExpiredException;
1418
use YdbPlatform\Ydb\Retry\Backoff;
1519
use YdbPlatform\Ydb\Retry\Retry;
1620
use YdbPlatform\Ydb\Retry\RetryParams;
@@ -448,14 +452,32 @@ public function setRetryParams(RetryParams $params): void
448452
*/
449453
public function retrySession(Closure $userFunc, bool $idempotent = false, RetryParams $params = null){
450454
return $this->retry->withParams($params)->retry(function () use ($userFunc){
451-
$sessionId = null;
452-
try{
455+
$session = null;
456+
try {
453457
$session = $this->session();
454-
$sessionId = $session->id();
455458
return $userFunc($session);
456-
}catch (BadSessionException $bse){
457-
$this->dropSession($sessionId);
458-
throw $bse;
459+
} catch (UnknownException $unknownException) {
460+
$this->dropSession($session->id());
461+
throw $unknownException;
462+
} catch (InvalidArgumentException $invalidArgumentException){
463+
$this->dropSession($session->id());
464+
throw $invalidArgumentException;
465+
} catch (BadSessionException $badSessionException){
466+
$this->dropSession($session->id());
467+
throw $badSessionException;
468+
} catch (SessionBusyException $sessionBusyException){
469+
$this->dropSession($session->id());
470+
throw $sessionBusyException;
471+
} catch (SessionExpiredException $sessionExpiredException){
472+
$this->dropSession($session->id());
473+
throw $sessionExpiredException;
474+
} catch (Exception $exception){
475+
try {
476+
$session->rollbackTransaction();
477+
} catch (Exception $e){
478+
479+
}
480+
throw $exception;
459481
}
460482
}, $idempotent);
461483

@@ -464,18 +486,31 @@ public function retrySession(Closure $userFunc, bool $idempotent = false, RetryP
464486
public function retryTransaction(Closure $userFunc, bool $idempotent = false, RetryParams $params = null){
465487

466488
return $this->retry->withParams($params)->retry(function () use ($userFunc){
467-
$sessionId = null;
489+
$session = null;
468490
try{
469491
$session = $this->session();
470-
$sessionId = $session->id();
471492
$session->beginTransaction();
472493
$result = $userFunc($session);
473494
$session->commitTransaction();
474495
return $result;
475-
}catch (BadSessionException $bse){
476-
$this->dropSession($sessionId);
477-
throw $bse;
496+
} catch (BadSessionException $badSessionException){
497+
$this->dropSession($session->id());
498+
throw $badSessionException;
499+
} catch (SessionBusyException $sessionBusyException){
500+
$this->dropSession($session->id());
501+
throw $sessionBusyException;
502+
} catch (SessionExpiredException $sessionExpiredException){
503+
$this->dropSession($session->id());
504+
throw $sessionExpiredException;
505+
} catch (Exception $exception){
506+
try {
507+
$session->rollbackTransaction();
508+
} catch (Exception $e){
509+
510+
}
511+
throw $exception;
478512
}
513+
479514
}, $idempotent);
480515

481516
}

0 commit comments

Comments
 (0)