Skip to content

Commit 9f22d21

Browse files
authored
Merge pull request #104 from ydb-platform/ilyakharev-patch
Update logs in retry and iam
2 parents 46deaa0 + 3e05cc0 commit 9f22d21

File tree

4 files changed

+31
-15
lines changed

4 files changed

+31
-15
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
* changed level of update token log record from info to debug
12
* created refresh token ratio parameter
23

34
## 1.9.0

src/Auth/IamAuth.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ abstract class IamAuth extends Auth
1313
*/
1414
public function requestToken($request_data)
1515
{
16-
$this->logger()->info('YDB: Obtaining new IAM token...');
16+
$this->logger()->debug('YDB: Request new IAM token...');
17+
$startTime = microtime(true);
1718

1819
$curl = curl_init(Iam::IAM_TOKEN_API_URL);
1920

@@ -33,6 +34,8 @@ public function requestToken($request_data)
3334

3435
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
3536

37+
$this->logger->debug("YDB: Received IAM response in ".((microtime(true)-$startTime)*1000)." miliseconds");
38+
3639
if ($status === 200) {
3740
$token = json_decode($result);
3841

src/Iam.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public function token($force = false)
9797
*/
9898
public function newToken()
9999
{
100-
$this->logger()->info('YDB: Obtaining new IAM token...');
100+
$this->logger()->debug('YDB: Obtaining new token...');
101101

102102
$tokenInfo = $this->config('credentials')->getTokenInfo();
103103
$this->iam_token = $tokenInfo->getToken();
@@ -394,7 +394,7 @@ protected function loadTokenFromFile()
394394
$this->iam_token = $token->iamToken;
395395
$this->expires_at = $token->expiresAt;
396396
$this->refresh_at = $token->refreshAt ?? time();
397-
$this->logger()->info('YDB: Reused IAM token [...' . substr($this->iam_token, -6) . '].');
397+
$this->logger()->debug('YDB: Reused token [...' . substr($this->iam_token, -6) . '].');
398398
return $token->iamToken;
399399
}
400400
}

src/Retry/Retry.php

+24-12
Original file line numberDiff line numberDiff line change
@@ -77,23 +77,35 @@ public function retry(Closure $closure, bool $idempotent)
7777
$startTime = microtime(true);
7878
$retryCount = 0;
7979
$lastException = null;
80-
while (microtime(true) < $startTime + $this->timeoutMs / 1000) {
81-
$this->logger->debug("YDB: Run user function. Retry count: $retryCount. s: ".(microtime(true) - $startTime));
80+
if (is_null($this->timeoutMs)) {
81+
$deadline = PHP_INT_MAX;
82+
} else {
83+
$deadline = $startTime + $this->timeoutMs / 1000;
84+
}
85+
$this->logger->debug("YDB: begin retry function. Deadline: $deadline");
86+
do {
87+
$this->logger->debug("YDB: Run user function. Retry count: $retryCount. s: " . (microtime(true) - $startTime));
8288
try {
8389
return $closure();
84-
} catch (Exception $e) {
85-
$this->logger->warning("YDB: Received exception: ".$e->getMessage());
86-
if (!$this->canRetry($e, $idempotent)){
87-
$lastException = $e;
88-
break;
90+
} catch (\Exception $e) {
91+
$this->logger->debug("YDB: Received exception: " . $e->getMessage());
92+
$lastException = $e;
93+
if (!$this->canRetry($e, $idempotent)) {
94+
$this->logger->error("YDB: Received non-retryable exception in retry. ms: "
95+
. ((microtime(true) - $startTime) * 1000) . "Retry count: $retryCount");
96+
throw $lastException;
97+
}
98+
$delay = $this->retryDelay($retryCount, $this->backoffType(get_class($e))) * 1000;
99+
if (microtime(true) + $delay / 1000000 > $deadline) {
100+
$this->logger->error("YDB: Timeout retry function. ms: "
101+
. ((microtime(true) - $startTime) * 1000) . "Retry count: $retryCount");
102+
throw $lastException;
89103
}
90104
$retryCount++;
91-
$lastException = $e;
92-
$delay = $this->retryDelay($retryCount, $this->backoffType(get_class($e)))*1000;
105+
$this->logger->debug("YDB: Sleep $delay microseconds before retry");
93106
usleep($delay);
94107
}
95-
}
96-
throw $lastException;
108+
} while (true);
97109
}
98110

99111
/**
@@ -111,7 +123,7 @@ protected function alwaysRetry(string $exception)
111123
return in_array($exception, self::$alwaysRetry);
112124
}
113125

114-
protected function canRetry(Exception $e, bool $idempotent)
126+
protected function canRetry(\Exception $e, bool $idempotent)
115127
{
116128
return is_a($e, RetryableException::class) && ($this->alwaysRetry(get_class($e)) || $idempotent);
117129
}

0 commit comments

Comments
 (0)