Skip to content

Commit 5725fa0

Browse files
authored
Update RequestTrait.php
1 parent 9e54630 commit 5725fa0

File tree

1 file changed

+62
-3
lines changed

1 file changed

+62
-3
lines changed

src/Traits/RequestTrait.php

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use YdbPlatform\Ydb\Issue;
88
use YdbPlatform\Ydb\Exception;
99
use YdbPlatform\Ydb\QueryResult;
10+
use YdbPlatform\Ydb\Ydb;
1011

1112
trait 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,23 @@ 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+
try{
173+
$this->ydb->discover();
174+
}catch (\Exception $e){}
175+
$message = 'YDB ' . $service . ' ' . $method . ' (status code GRPC_'.
176+
(isset(self::$grpcExceptions[$status->code])?self::$grpcNames[$status->code]:$status->code)
177+
.' ' . $status->code . '): ' . ($status->details ?? 'no details');
178+
$endpoint = $this->ydb->endpoint();
179+
$this->logger->error($message);
180+
if ($this->ydb->needDiscovery()){
181+
$endpoint = $this->ydb->cluster()->all()[array_rand($this->ydb->cluster()->all())]->endpoint();
182+
}
183+
$this->client = new $this->client($endpoint,[
184+
'credentials' => $this->ydb->iam()->getCredentials()
185+
]);
156186
if (isset(self::$grpcExceptions[$status->code])) {
157187
throw new self::$grpcExceptions[$status->code]($message);
158188
} else {
@@ -272,6 +302,17 @@ protected function resetLastRequest()
272302
$this->last_request_try_count = 0;
273303
}
274304

305+
protected function checkDiscovery(){
306+
if ($this->ydb->needDiscovery() && time()-$this->lastDiscovery>$this->ydb->discoveryInterval()){
307+
try{
308+
$this->lastDiscovery = time();
309+
$this->ydb->discover();
310+
} catch (\Exception $e){
311+
312+
}
313+
}
314+
}
315+
275316
private static $ydbExceptions = [
276317
StatusCode::STATUS_CODE_UNSPECIFIED => \YdbPlatform\Ydb\Exceptions\Ydb\StatusCodeUnspecified::class,
277318
StatusCode::BAD_REQUEST => \YdbPlatform\Ydb\Exceptions\Ydb\BadRequestException::class,
@@ -313,4 +354,22 @@ protected function resetLastRequest()
313354
16 => \YdbPlatform\Ydb\Exceptions\Grpc\UnauthenticatedException::class
314355
];
315356

357+
private static $grpcNames = [
358+
1 => "CANCELLED",
359+
2 => "UNKNOWN",
360+
3 => "INVALID_ARGUMENT",
361+
4 => "DEADLINE_EXCEEDED",
362+
5 => "NOT_FOUND",
363+
6 => "ALREADY_EXISTS",
364+
7 => "PERMISSION_DENIED",
365+
8 => "RESOURCE_EXHAUSTED",
366+
9 => "FAILED_PRECONDITION",
367+
10 => "ABORTED",
368+
11 => "OUT_OF_RANGE",
369+
12 => "UNIMPLEMENTED",
370+
13 => "INTERNAL",
371+
14 => "UNAVAILABLE",
372+
15 => "DATA_LOSS",
373+
16 => "UNAUTHENTICATED"
374+
];
316375
}

0 commit comments

Comments
 (0)