Skip to content

Commit a6427a6

Browse files
authored
Added query timeout and canceled
Added query timeout and canceled
2 parents 13bbc5f + ba39a51 commit a6427a6

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
* added query timeout and canceled params
2+
13
## 1.11.0
24
* added query stats
35
* added ReadTokenFromFile

src/Session.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use Closure;
66
use Exception;
7+
use Google\Protobuf\Duration;
8+
use Ydb\Operations\OperationParams;
79
use Ydb\Table\Query;
810
use Ydb\Table\QueryCachePolicy;
911
// use Ydb\Table\StaleModeSettings;
@@ -366,6 +368,24 @@ public function query($yql, array $parameters = null, array $options = [])
366368
if(isset($options['collectStats'])){
367369
$query->collectStats($options['collectStats']);
368370
}
371+
$operationParams = new OperationParams();
372+
if(isset($options['operation_timeout_ms'])){
373+
$seconds = intdiv( $options['operation_timeout_ms'], 1000); // get seconds
374+
$nanos = fmod($options['operation_timeout_ms'], 1000) * 1000000; // get ns
375+
$operationParams->setOperationTimeout(new Duration([
376+
'seconds' => $seconds,
377+
'nanos' => $nanos
378+
]));
379+
}
380+
if(isset($options['cancel_after_ms'])){
381+
$seconds = intdiv( $options['cancel_after_ms'], 1000); // get seconds
382+
$nanos = fmod($options['operation_timeout_ms'], 1000) * 1000000; // get ns
383+
$operationParams->setCancelAfter(new Duration([
384+
'seconds' => $seconds,
385+
'nanos' => $nanos
386+
]));
387+
}
388+
$query->operationParams($operationParams);
369389

370390
return $this->executeQuery($query);
371391
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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\Logger\SimpleStdLogger;
8+
use YdbPlatform\Ydb\Session;
9+
use YdbPlatform\Ydb\Ydb;
10+
11+
class CheckTimeoutAndDurationParamsTest extends TestCase
12+
{
13+
public function testTimeoutAndDurationParams(){
14+
$config = [
15+
16+
// Database path
17+
'database' => '/local',
18+
19+
// Database endpoint
20+
'endpoint' => 'localhost:2136',
21+
22+
// Auto discovery (dedicated server only)
23+
'discovery' => false,
24+
25+
// IAM config
26+
'iam_config' => [
27+
'insecure' => true,
28+
],
29+
'credentials' => new AnonymousAuthentication()
30+
];
31+
32+
$ydb = new Ydb($config, new SimpleStdLogger(7));
33+
$table = $ydb->table();
34+
35+
$this->expectException('YdbPlatform\Ydb\Exceptions\Ydb\TimeoutException');
36+
$table->retrySession(function (Session $session){
37+
$session->query('SELECT 1;', null, [
38+
'operation_timeout_ms' => 1e-5
39+
]);
40+
});
41+
42+
$this->expectException('YdbPlatform\Ydb\Exceptions\Ydb\CancelledException');
43+
$table->retrySession(function (Session $session){
44+
$session->query('SELECT 1;', null, [
45+
'cancel_after_ms' => 1e-5
46+
]);
47+
});
48+
}
49+
}

0 commit comments

Comments
 (0)