4
4
5
5
use Closure ;
6
6
use YdbPlatform \Ydb \Exception ;
7
- use YdbPlatform \Ydb \Exceptions \Grpc \DeadlineExceededException ;
8
- use YdbPlatform \Ydb \Exceptions \NonRetryableException ;
9
7
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 ;
8
+ use Psr \Log \LoggerInterface ;
15
9
16
10
class Retry
17
11
{
@@ -22,9 +16,14 @@ class Retry
22
16
23
17
protected $ fastBackOff ;
24
18
protected $ noBackOff ;
19
+ /**
20
+ * @var LoggerInterface
21
+ */
22
+ protected $ logger ;
25
23
26
- public function __construct ()
24
+ public function __construct (LoggerInterface $ logger )
27
25
{
26
+ $ this ->logger = $ logger ;
28
27
$ this ->timeoutMs = 2000 ;
29
28
$ this ->fastBackOff = new Backoff (6 , 5 );
30
29
$ this ->slowBackOff = new Backoff (6 , 1000 );
@@ -79,11 +78,14 @@ public function retry(Closure $closure, bool $idempotent)
79
78
$ retryCount = 0 ;
80
79
$ lastException = null ;
81
80
while (microtime (true ) < $ startTime + $ this ->timeoutMs / 1000 ) {
81
+ $ this ->logger ->debug ("YDB: Run user function. Retry count: $ retryCount. Ms: " .(microtime (true ) - $ startTime ));
82
82
try {
83
83
return $ closure ();
84
84
} catch (Exception $ e ) {
85
+ $ this ->logger ->warning ("YDB: Received exception: " .$ e ->getMessage ());
85
86
if (!$ this ->canRetry ($ e , $ idempotent )){
86
- throw $ e ;
87
+ $ lastException = $ e ;
88
+ break ;
87
89
}
88
90
$ retryCount ++;
89
91
$ this ->retryDelay ($ retryCount , $ this ->backoffType ($ e ));
@@ -99,18 +101,20 @@ public function retry(Closure $closure, bool $idempotent)
99
101
*/
100
102
protected function backoffType (string $ e ): Backoff
101
103
{
102
- return in_array ($ e , self ::$ immediatelyBackoff )? $ this ->noBackOff :
103
- (in_array ($ e , self ::$ fastBackoff )? $ this ->fastBackOff : $ this ->slowBackOff );
104
+ return in_array ($ e , self ::$ immediatelyBackoff ) ? $ this ->noBackOff :
105
+ (in_array ($ e , self ::$ fastBackoff ) ? $ this ->fastBackOff : $ this ->slowBackOff );
104
106
}
105
107
106
- protected function alwaysRetry (string $ exception ){
108
+ protected function alwaysRetry (string $ exception )
109
+ {
107
110
return in_array ($ exception , self ::$ alwaysRetry );
108
111
}
109
112
110
113
protected function canRetry (Exception $ e , bool $ idempotent )
111
114
{
112
- return is_a ($ e , RetryableException::class)&& ($ this ->alwaysRetry (get_class ($ e )) || $ idempotent );
115
+ return is_a ($ e , RetryableException::class) && ($ this ->alwaysRetry (get_class ($ e )) || $ idempotent );
113
116
}
117
+
114
118
private static $ immediatelyBackoff = [
115
119
\YdbPlatform \Ydb \Exceptions \Grpc \AbortedException::class,
116
120
\YdbPlatform \Ydb \Exceptions \Ydb \BadSessionException::class,
0 commit comments