Skip to content

Commit e13535b

Browse files
committed
Update tests
1 parent efed1c1 commit e13535b

13 files changed

+484
-87
lines changed

src/Exceptions/Grpc/CanceledException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace YdbPlatform\Ydb\Exceptions\Grpc;
44

5-
use YdbPlatform\Ydb\Exceptions\NonRetryableException;
5+
use YdbPlatform\Ydb\Exceptions\RetryableException;
66

7-
class CanceledException extends NonRetryableException
7+
class CanceledException extends RetryableException
88
{
99

1010
}

src/Exceptions/NonRetryableException.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace YdbPlatform\Ydb\Exceptions;
44

5-
class NonRetryableException extends \Exception
5+
use YdbPlatform\Ydb\Exception;
6+
7+
class NonRetryableException extends Exception
68
{
79
}

src/Exceptions/RetryableException.php

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,9 @@
22

33
namespace YdbPlatform\Ydb\Exceptions;
44

5-
class RetryableException extends \Exception
6-
{
7-
/**
8-
* @var bool
9-
*/
10-
protected $fastBackoff;
5+
use YdbPlatform\Ydb\Exception;
116

12-
/**
13-
* @return bool
14-
*/
15-
public function isFastBackoff(): bool
16-
{
17-
return $this->fastBackoff;
18-
}
7+
class RetryableException extends Exception
8+
{
199

2010
}

src/Exceptions/Ydb/CanceledException.php

Lines changed: 0 additions & 8 deletions
This file was deleted.
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\Ydb;
4+
5+
class CancelledException extends \YdbPlatform\Ydb\Exceptions\NonRetryableException
6+
{
7+
8+
}

src/Exceptions/Ydb/UnauthorisedException.php renamed to src/Exceptions/Ydb/StatusCodeUnspecified.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use YdbPlatform\Ydb\Exceptions\NonRetryableException;
66

7-
class UnauthorisedException extends NonRetryableException
7+
class StatusCodeUnspecified extends NonRetryableException
88
{
99

1010
}
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\Ydb;
4+
5+
use YdbPlatform\Ydb\Exceptions\NonRetryableException;
6+
7+
class UnauthorizedException extends NonRetryableException
8+
{
9+
10+
}

src/Retry/Retry.php

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ class Retry
2121
protected $slowBackOff;
2222

2323
protected $fastBackOff;
24+
protected $noBackOff;
2425

2526
public function __construct()
2627
{
2728
$this->timeoutMs = 2000;
2829
$this->fastBackOff = new Backoff(6, 5);
2930
$this->slowBackOff = new Backoff(6, 1000);
31+
$this->noBackOff = new Backoff(0, 0);
3032
}
3133

3234
protected function retryDelay(int $retryCount, Backoff $backoff)
@@ -80,56 +82,61 @@ public function retry(Closure $closure, bool $idempotent)
8082
while (microtime(true) < $startTime + $this->timeoutMs / 1000) {
8183
try {
8284
return $closure();
83-
} catch (RetryableException $e) {
84-
if (isset(self::$idempotentOnly[get_class($e)]) && $idempotent) {
85+
} catch (Exception $e) {
86+
if (!$this->canRetry($e, $idempotent)){
8587
throw $e;
8688
}
8789
$retryCount++;
8890
$this->retryDelay($retryCount, $this->backoffType($e));
8991
$lastException = $e;
90-
} catch (Exception $e) {
91-
throw $e;
9292
}
9393
}
9494
throw $lastException;
9595
}
9696

9797
/**
98-
* @param RetryableException $e
98+
* @param string $e
9999
* @return Backoff
100100
*/
101-
protected function backoffType(RetryableException $e): Backoff
101+
protected function backoffType(string $e): Backoff
102102
{
103-
return $this->fastBackOff;
104-
// if ($e instanceof AbortedException) {
105-
// return $this->fastBackOff;
106-
// } elseif ($e instanceof BadSessionException) {
107-
// return $this->fastBackOff;
108-
// } elseif ($e instanceof SessionBusyException) {
109-
// return $this->fastBackOff;
110-
// } elseif ($e instanceof UndeterminedException) {
111-
// return $this->fastBackOff;
112-
// } elseif ($e instanceof UnavailableException) {
113-
// return $this->fastBackOff;
114-
// } elseif ($e instanceof UndeterminedException) {
115-
// return $this->fastBackOff;
116-
// } elseif ($e instanceof DeadlineExceededException) {
117-
// return $this->fastBackOff;
118-
// } else {
119-
// return $this->slowBackOff;
120-
// }
103+
return in_array($e, self::$immediatelyBackoff)?$this->noBackOff:
104+
(in_array($e, self::$fastBackoff)?$this->fastBackOff:$this->slowBackOff);
105+
}
106+
107+
protected function alwaysRetry(string $exception){
108+
return in_array($exception, self::$alwaysRetry);
121109
}
122110

123-
private static $idempotentOnly = [
111+
protected function canRetry(Exception $e, bool $idempotent)
112+
{
113+
return is_a($e, RetryableException::class)&&($this->alwaysRetry(get_class($e)) || $idempotent);
114+
}
115+
private static $immediatelyBackoff = [
116+
\YdbPlatform\Ydb\Exceptions\Grpc\AbortedException::class,
117+
\YdbPlatform\Ydb\Exceptions\Ydb\BadSessionException::class,
118+
];
119+
120+
private static $fastBackoff = [
124121
\YdbPlatform\Ydb\Exceptions\Grpc\CanceledException::class,
125122
\YdbPlatform\Ydb\Exceptions\Grpc\DeadlineExceededException::class,
126123
\YdbPlatform\Ydb\Exceptions\Grpc\InternalException::class,
127124
\YdbPlatform\Ydb\Exceptions\Grpc\UnavailableException::class,
128-
\YdbPlatform\Ydb\Exceptions\Ydb\UndeterminedException::class
125+
\YdbPlatform\Ydb\Exceptions\Ydb\AbortedException::class,
126+
\YdbPlatform\Ydb\Exceptions\Ydb\UnavailableException::class,
127+
\YdbPlatform\Ydb\Exceptions\Ydb\CancelledException::class,
128+
\YdbPlatform\Ydb\Exceptions\Ydb\UndeterminedException::class,
129+
\YdbPlatform\Ydb\Exceptions\Ydb\SessionBusyException::class,
129130
];
130131

131-
private static $fastBackoff = [
132-
132+
private static $alwaysRetry = [
133+
\YdbPlatform\Ydb\Exceptions\Grpc\ResourceExhaustedException::class,
134+
\YdbPlatform\Ydb\Exceptions\Grpc\AbortedException::class,
135+
\YdbPlatform\Ydb\Exceptions\Ydb\AbortedException::class,
136+
\YdbPlatform\Ydb\Exceptions\Ydb\UnavailableException::class,
137+
\YdbPlatform\Ydb\Exceptions\Ydb\OverloadedException::class,
138+
\YdbPlatform\Ydb\Exceptions\Ydb\BadSessionException::class,
139+
\YdbPlatform\Ydb\Exceptions\Ydb\SessionBusyException::class,
133140
];
134141

135142
}

src/Table.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ public function retrySession(Closure $userFunc, bool $idempotent = false, RetryP
457457
$session = $this->session();
458458
return $userFunc($session);
459459
} catch (Exception $exception){
460-
if ($session != null && in_array(get_class($exception), self::$deleteSession)){
460+
if ($session != null && $this->deleteSession(get_class($exception))){
461461
$this->dropSession($session->id());
462462
}
463463
throw $exception;
@@ -486,6 +486,10 @@ public function retryTransaction(Closure $userFunc, bool $idempotent = false, Re
486486

487487
}
488488

489+
protected function deleteSession(string $exception): bool
490+
{
491+
return in_array($exception, self::$deleteSession);
492+
}
489493

490494
private static $deleteSession = [
491495
\YdbPlatform\Ydb\Exceptions\Grpc\CanceledException::class,
@@ -502,6 +506,7 @@ public function retryTransaction(Closure $userFunc, bool $idempotent = false, Re
502506
\YdbPlatform\Ydb\Exceptions\Grpc\UnavailableException::class,
503507
\YdbPlatform\Ydb\Exceptions\Grpc\DataLossException::class,
504508
\YdbPlatform\Ydb\Exceptions\Grpc\UnauthenticatedException::class,
509+
\YdbPlatform\Ydb\Exceptions\Ydb\StatusCodeUnspecified::class,
505510
\YdbPlatform\Ydb\Exceptions\Ydb\BadSessionException::class,
506511
\YdbPlatform\Ydb\Exceptions\Ydb\SessionExpiredException::class,
507512
\YdbPlatform\Ydb\Exceptions\Ydb\SessionBusyException::class

src/Traits/RequestTrait.php

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,6 @@
44

55
use Ydb\StatusIds\StatusCode;
66

7-
use YdbPlatform\Ydb\Exceptions\Grpc\CanceledException;
8-
use YdbPlatform\Ydb\Exceptions\Grpc\DeadlineExceededException;
9-
use YdbPlatform\Ydb\Exceptions\Grpc\InvalidArgumentException;
10-
use YdbPlatform\Ydb\Exceptions\Grpc\NotFoundException;
11-
use YdbPlatform\Ydb\Exceptions\Grpc\PermissionDeniedException;
12-
use YdbPlatform\Ydb\Exceptions\Grpc\UnknownException;
13-
use YdbPlatform\Ydb\Exceptions\Ydb\AbortedException;
14-
use YdbPlatform\Ydb\Exceptions\Ydb\AlreadyExistsException;
15-
use YdbPlatform\Ydb\Exceptions\Ydb\BadRequestException;
16-
use YdbPlatform\Ydb\Exceptions\Ydb\BadSessionException;
17-
use YdbPlatform\Ydb\Exceptions\Ydb\ClientResourceExhaustedException;
18-
use YdbPlatform\Ydb\Exceptions\Ydb\GenericErrorException;
19-
use YdbPlatform\Ydb\Exceptions\Ydb\InternalErrorException;
20-
use YdbPlatform\Ydb\Exceptions\Ydb\OverloadedException;
21-
use YdbPlatform\Ydb\Exceptions\Ydb\PreconditionFailedException;
22-
use YdbPlatform\Ydb\Exceptions\Ydb\SchemeErrorException;
23-
use YdbPlatform\Ydb\Exceptions\Ydb\SessionBusyException;
24-
use YdbPlatform\Ydb\Exceptions\Ydb\SessionExpiredException;
25-
use YdbPlatform\Ydb\Exceptions\Ydb\TimeoutException;
26-
use YdbPlatform\Ydb\Exceptions\Ydb\UnauthorisedException;
27-
use YdbPlatform\Ydb\Exceptions\Ydb\UnavailableException;
28-
use YdbPlatform\Ydb\Exceptions\Ydb\UndeterminedException;
29-
use YdbPlatform\Ydb\Exceptions\Ydb\UnsupportedException;
307
use YdbPlatform\Ydb\Issue;
318
use YdbPlatform\Ydb\Exception;
329
use YdbPlatform\Ydb\QueryResult;
@@ -176,8 +153,8 @@ protected function checkStatus($service, $method, $status)
176153
{
177154
if (isset($status->code) && $status->code !== 0) {
178155
$message = 'YDB ' . $service . ' ' . $method . ' (status code GRPC_' . $status->code . '): ' . ($status->details ?? 'no details');
179-
if (isset(self::$ydbExceptions[$status->code])) {
180-
throw new self::$ydbExceptions[$status->code]($message);
156+
if (isset(self::$grpcExceptions[$status->code])) {
157+
throw new self::$grpcExceptions[$status->code]($message);
181158
} else {
182159
throw new \Exception($message);
183160
}
@@ -206,9 +183,7 @@ protected function processResponse($service, $method, $response, $resultClass)
206183

207184
$statusCode = $response->getStatus();
208185

209-
if ($statusCode == StatusCode::STATUS_CODE_UNSPECIFIED) {
210-
return true;
211-
} elseif ($statusCode == StatusCode::SUCCESS) {
186+
if ($statusCode == StatusCode::SUCCESS) {
212187
$result = $response->getResult();
213188

214189
if ($result === null) {
@@ -298,8 +273,9 @@ protected function resetLastRequest()
298273
}
299274

300275
private static $ydbExceptions = [
276+
StatusCode::STATUS_CODE_UNSPECIFIED => \YdbPlatform\Ydb\Exceptions\Ydb\StatusCodeUnspecified::class,
301277
StatusCode::BAD_REQUEST => \YdbPlatform\Ydb\Exceptions\Ydb\BadRequestException::class,
302-
StatusCode::UNAUTHORIZED => \YdbPlatform\Ydb\Exceptions\Ydb\UnauthorisedException::class,
278+
StatusCode::UNAUTHORIZED => \YdbPlatform\Ydb\Exceptions\Ydb\UnauthorizedException::class,
303279
StatusCode::INTERNAL_ERROR => \YdbPlatform\Ydb\Exceptions\Ydb\InternalErrorException::class,
304280
StatusCode::ABORTED => \YdbPlatform\Ydb\Exceptions\Ydb\AbortedException::class,
305281
StatusCode::UNAVAILABLE => \YdbPlatform\Ydb\Exceptions\Ydb\UnavailableException::class,
@@ -312,7 +288,7 @@ protected function resetLastRequest()
312288
StatusCode::ALREADY_EXISTS => \YdbPlatform\Ydb\Exceptions\Ydb\AlreadyExistsException::class,
313289
StatusCode::NOT_FOUND => \YdbPlatform\Ydb\Exceptions\Ydb\NotFoundException::class,
314290
StatusCode::SESSION_EXPIRED => \YdbPlatform\Ydb\Exceptions\Ydb\SessionExpiredException::class,
315-
StatusCode::CANCELLED => \YdbPlatform\Ydb\Exceptions\Ydb\CanceledException::class,
291+
StatusCode::CANCELLED => \YdbPlatform\Ydb\Exceptions\Ydb\CancelledException::class,
316292
StatusCode::UNDETERMINED => \YdbPlatform\Ydb\Exceptions\Ydb\UndeterminedException::class,
317293
StatusCode::UNSUPPORTED => \YdbPlatform\Ydb\Exceptions\Ydb\UnsupportedException::class,
318294
StatusCode::SESSION_BUSY => \YdbPlatform\Ydb\Exceptions\Ydb\SessionBusyException::class,

0 commit comments

Comments
 (0)