77use YdbPlatform \Ydb \Issue ;
88use YdbPlatform \Ydb \Exception ;
99use YdbPlatform \Ydb \QueryResult ;
10+ use YdbPlatform \Ydb \Ydb ;
1011
1112trait RequestTrait
1213{
@@ -30,6 +31,16 @@ trait RequestTrait
3031 */
3132 protected $ last_request_try_count = 0 ;
3233
34+ /**
35+ * @var Ydb
36+ */
37+ protected $ ydb ;
38+
39+ /**
40+ * @var int
41+ */
42+ protected $ lastDiscovery = 0 ;
43+
3344 /**
3445 * Make a request to the service with the given method.
3546 *
@@ -41,6 +52,8 @@ trait RequestTrait
4152 */
4253 protected function doRequest ($ service , $ method , array $ data = [])
4354 {
55+ $ this ->checkDiscovery ();
56+
4457 $ this ->meta ['x-ydb-auth-ticket ' ] = [$ this ->credentials ->token ()];
4558
4659 $ this ->saveLastRequest ($ service , $ method , $ data );
@@ -82,7 +95,7 @@ protected function doRequest($service, $method, array $data = [])
8295 if (method_exists ($ call , 'wait ' )) {
8396 list ($ response , $ status ) = $ call ->wait ();
8497
85- $ this ->checkGrpcStatus ($ service , $ method , $ status );
98+ $ this ->handleGrpcStatus ($ service , $ method , $ status );
8699
87100 return $ this ->processResponse ($ service , $ method , $ response , $ resultClass );
88101 }
@@ -101,6 +114,10 @@ protected function doRequest($service, $method, array $data = [])
101114 */
102115 protected function doStreamRequest ($ service , $ method , $ data = [])
103116 {
117+ $ this ->checkDiscovery ();
118+
119+ $ this ->meta ['x-ydb-auth-ticket ' ] = [$ this ->credentials ->token ()];
120+
104121 if (method_exists ($ this , 'take ' )) {
105122 $ this ->take ();
106123 }
@@ -149,10 +166,25 @@ protected function doStreamRequest($service, $method, $data = [])
149166 * @param object $status
150167 * @throws Exception
151168 */
152- protected function checkGrpcStatus ($ service , $ method , $ status )
169+ protected function handleGrpcStatus ($ service , $ method , $ status )
153170 {
154171 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+ ]);
156188 if (isset (self ::$ grpcExceptions [$ status ->code ])) {
157189 throw new self::$ grpcExceptions [$ status ->code ]($ message );
158190 } else {
@@ -272,6 +304,17 @@ protected function resetLastRequest()
272304 $ this ->last_request_try_count = 0 ;
273305 }
274306
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+
275318 private static $ ydbExceptions = [
276319 StatusCode::STATUS_CODE_UNSPECIFIED => \YdbPlatform \Ydb \Exceptions \Ydb \StatusCodeUnspecified::class,
277320 StatusCode::BAD_REQUEST => \YdbPlatform \Ydb \Exceptions \Ydb \BadRequestException::class,
@@ -313,4 +356,22 @@ protected function resetLastRequest()
313356 16 => \YdbPlatform \Ydb \Exceptions \Grpc \UnauthenticatedException::class
314357 ];
315358
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+ ];
316377}
0 commit comments