Skip to content

Commit 318310f

Browse files
authored
Merge pull request #56 from ydb-platform/fix-retry
Fixed retry at BAD_SESSION
2 parents 5f0cc37 + 469174c commit 318310f

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
* fixed retry at BAD_SESSION
12
* added credentials authentication
23
* added CI test
34

src/Traits/RequestTrait.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,13 +222,18 @@ protected function processResponse($service, $method, $response, $resultClass)
222222
case StatusCode::BAD_SESSION:
223223
if (method_exists($this, 'refresh'))
224224
{
225+
$data = $this->last_request_data;
226+
225227
$session = $this->refresh();
226228

227229
if (isset($this->last_request_data['session_id']))
228230
{
229231
$this->last_request_data['session_id'] = $session->id();
230232
}
231233

234+
$data['session_id'] = $session->id();
235+
$this->saveLastRequest($service, $method, $data);
236+
232237
// only 10 retries are allowed!
233238
if ($this->last_request_try_count < 10)
234239
{
@@ -313,7 +318,7 @@ protected function retryLastRequest($sleep = 100)
313318
$this->logger()->info('Going to retry the last request!');
314319

315320
usleep(max($this->last_request_try_count, 1) * $sleep * 1000); // waiting 100 ms more
316-
$this->doRequest($this->last_request_service, $this->last_request_method, $this->last_request_data);
321+
return $this->doRequest($this->last_request_service, $this->last_request_method, $this->last_request_data);
317322
}
318323
}
319324

tests/RetryOnBadSessionTest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace YdbPlatform\Ydb\Test;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use YdbPlatform\Ydb\Auth\Implement\AnonymousAuthentication;
7+
use YdbPlatform\Ydb\Ydb;
8+
9+
class SessionManager extends \YdbPlatform\Ydb\Session{
10+
public static function setSessionId(\YdbPlatform\Ydb\Session $session, string $id){
11+
$session->session_id = $id;
12+
return $session;
13+
}
14+
public static function getSessionId(\YdbPlatform\Ydb\Session $session){
15+
return $session->session_id;
16+
}
17+
}
18+
19+
class RetryOnBadSessionTest extends TestCase
20+
{
21+
public function test(){
22+
23+
$config = [
24+
25+
// Database path
26+
'database' => '/local',
27+
28+
// Database endpoint
29+
'endpoint' => 'localhost:2136',
30+
31+
// Auto discovery (dedicated server only)
32+
'discovery' => false,
33+
34+
// IAM config
35+
'iam_config' => [
36+
'insecure' => true,
37+
],
38+
'credentials' => new AnonymousAuthentication()
39+
];
40+
$ydb = new Ydb($config);
41+
$table = $ydb->table();
42+
$session = $table->session();
43+
$oldSessionId = SessionManager::getSessionId($session);
44+
$session->delete();
45+
$session = $table->session();
46+
SessionManager::setSessionId($session,$oldSessionId);
47+
print_r($session->query('select 1 as res'));
48+
self::assertEquals(
49+
1,
50+
$session->query('select 1 as res')->rows()[0]['res']
51+
);
52+
}
53+
}

0 commit comments

Comments
 (0)