7
7
use YdbPlatform \Ydb \Issue ;
8
8
use YdbPlatform \Ydb \Exception ;
9
9
use YdbPlatform \Ydb \QueryResult ;
10
+ use YdbPlatform \Ydb \Ydb ;
10
11
11
12
trait RequestTrait
12
13
{
@@ -30,6 +31,16 @@ trait RequestTrait
30
31
*/
31
32
protected $ last_request_try_count = 0 ;
32
33
34
+ /**
35
+ * @var Ydb
36
+ */
37
+ protected $ ydb ;
38
+
39
+ /**
40
+ * @var int
41
+ */
42
+ protected $ lastDiscovery = 0 ;
43
+
33
44
/**
34
45
* Make a request to the service with the given method.
35
46
*
@@ -41,6 +52,8 @@ trait RequestTrait
41
52
*/
42
53
protected function doRequest ($ service , $ method , array $ data = [])
43
54
{
55
+ $ this ->checkDiscovery ();
56
+
44
57
$ this ->meta ['x-ydb-auth-ticket ' ] = [$ this ->credentials ->token ()];
45
58
46
59
$ this ->saveLastRequest ($ service , $ method , $ data );
@@ -82,7 +95,7 @@ protected function doRequest($service, $method, array $data = [])
82
95
if (method_exists ($ call , 'wait ' )) {
83
96
list ($ response , $ status ) = $ call ->wait ();
84
97
85
- $ this ->checkGrpcStatus ($ service , $ method , $ status );
98
+ $ this ->handleGrpcStatus ($ service , $ method , $ status );
86
99
87
100
return $ this ->processResponse ($ service , $ method , $ response , $ resultClass );
88
101
}
@@ -101,6 +114,10 @@ protected function doRequest($service, $method, array $data = [])
101
114
*/
102
115
protected function doStreamRequest ($ service , $ method , $ data = [])
103
116
{
117
+ $ this ->checkDiscovery ();
118
+
119
+ $ this ->meta ['x-ydb-auth-ticket ' ] = [$ this ->credentials ->token ()];
120
+
104
121
if (method_exists ($ this , 'take ' )) {
105
122
$ this ->take ();
106
123
}
@@ -149,10 +166,25 @@ protected function doStreamRequest($service, $method, $data = [])
149
166
* @param object $status
150
167
* @throws Exception
151
168
*/
152
- protected function checkGrpcStatus ($ service , $ method , $ status )
169
+ protected function handleGrpcStatus ($ service , $ method , $ status )
153
170
{
154
171
if (isset ($ status ->code ) && $ status ->code !== 0 ) {
155
- $ message = 'YDB ' . $ service . ' ' . $ method . ' (status code GRPC_ ' . $ status ->code . '): ' . ($ status ->details ?? 'no details ' );
172
+ $ message = 'YDB ' . $ service . ' ' . $ method . ' (status code GRPC_ ' .
173
+ (isset (self ::$ grpcExceptions [$ status ->code ])?self ::$ grpcNames [$ status ->code ]:$ status ->code )
174
+ .' ' . $ status ->code . '): ' . ($ status ->details ?? 'no details ' );
175
+ $ this ->logger ->error ($ message );
176
+ if ($ this ->ydb ->needDiscovery ()){
177
+ try {
178
+ $ this ->ydb ->discover ();
179
+ }catch (\Exception $ e ){}
180
+ }
181
+ $ endpoint = $ this ->ydb ->endpoint ();
182
+ if ($ this ->ydb ->needDiscovery ()){
183
+ $ endpoint = $ this ->ydb ->cluster ()->all ()[array_rand ($ this ->ydb ->cluster ()->all ())]->endpoint ();
184
+ }
185
+ $ this ->client = new $ this ->client ($ endpoint ,[
186
+ 'credentials ' => $ this ->ydb ->iam ()->getCredentials ()
187
+ ]);
156
188
if (isset (self ::$ grpcExceptions [$ status ->code ])) {
157
189
throw new self::$ grpcExceptions [$ status ->code ]($ message );
158
190
} else {
@@ -272,6 +304,17 @@ protected function resetLastRequest()
272
304
$ this ->last_request_try_count = 0 ;
273
305
}
274
306
307
+ protected function checkDiscovery (){
308
+ if ($ this ->ydb ->needDiscovery () && time ()-$ this ->lastDiscovery >$ this ->ydb ->discoveryInterval ()){
309
+ try {
310
+ $ this ->lastDiscovery = time ();
311
+ $ this ->ydb ->discover ();
312
+ } catch (\Exception $ e ){
313
+
314
+ }
315
+ }
316
+ }
317
+
275
318
private static $ ydbExceptions = [
276
319
StatusCode::STATUS_CODE_UNSPECIFIED => \YdbPlatform \Ydb \Exceptions \Ydb \StatusCodeUnspecified::class,
277
320
StatusCode::BAD_REQUEST => \YdbPlatform \Ydb \Exceptions \Ydb \BadRequestException::class,
@@ -313,4 +356,22 @@ protected function resetLastRequest()
313
356
16 => \YdbPlatform \Ydb \Exceptions \Grpc \UnauthenticatedException::class
314
357
];
315
358
359
+ private static $ grpcNames = [
360
+ 1 => "CANCELLED " ,
361
+ 2 => "UNKNOWN " ,
362
+ 3 => "INVALID_ARGUMENT " ,
363
+ 4 => "DEADLINE_EXCEEDED " ,
364
+ 5 => "NOT_FOUND " ,
365
+ 6 => "ALREADY_EXISTS " ,
366
+ 7 => "PERMISSION_DENIED " ,
367
+ 8 => "RESOURCE_EXHAUSTED " ,
368
+ 9 => "FAILED_PRECONDITION " ,
369
+ 10 => "ABORTED " ,
370
+ 11 => "OUT_OF_RANGE " ,
371
+ 12 => "UNIMPLEMENTED " ,
372
+ 13 => "INTERNAL " ,
373
+ 14 => "UNAVAILABLE " ,
374
+ 15 => "DATA_LOSS " ,
375
+ 16 => "UNAUTHENTICATED "
376
+ ];
316
377
}
0 commit comments