Skip to content

Commit 73501f8

Browse files
authored
Merge pull request #130 Add transaction mode for retryTransaction from ilyakharev/tx-modes
2 parents ba30662 + c9a9149 commit 73501f8

File tree

5 files changed

+108
-88
lines changed

5 files changed

+108
-88
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
* added transaction mode for retryTransaction
12
* fix keepInCache param in YdbQuery
23
* added Yson type
34
* add logger as Ydb config

src/Session.php

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
use Exception;
77
use Google\Protobuf\Duration;
88
use Ydb\Operations\OperationParams;
9+
use Ydb\Table\OnlineModeSettings;
910
use Ydb\Table\Query;
1011
use Ydb\Table\QueryCachePolicy;
11-
// use Ydb\Table\StaleModeSettings;
12-
// use Ydb\Table\OnlineModeSettings;
12+
use Ydb\Table\SnapshotModeSettings;
13+
use Ydb\Table\StaleModeSettings;
1314
use Ydb\Table\TransactionControl;
1415
use Ydb\Table\TransactionSettings;
1516
use Ydb\Table\SerializableModeSettings;
@@ -195,9 +196,9 @@ public function keepAlive()
195196
* @return mixed
196197
* @throws Exception
197198
*/
198-
public function transaction(Closure $closure)
199+
public function transaction(Closure $closure, string $mode = 'serializable_read_write')
199200
{
200-
$this->beginTransaction();
201+
$this->beginTransaction($mode);
201202
try
202203
{
203204
$result = $closure($this);
@@ -218,15 +219,9 @@ public function transaction(Closure $closure)
218219
* @return mixed
219220
* @throws Exception
220221
*/
221-
public function beginTransaction()
222+
public function beginTransaction(string $mode = 'serializable_read_write')
222223
{
223-
$serializable_read_write = new SerializableModeSettings;
224-
// $online_read_only = new OnlineModeSettings;
225-
// $stale_read_only = new StaleModeSettings;
226-
227-
$transaction_settings = new TransactionSettings([
228-
'serializable_read_write' => $serializable_read_write,
229-
]);
224+
$transaction_settings = new TransactionSettings(parseTxMode($mode));
230225

231226
$result = $this->request('BeginTransaction', [
232227
'session_id' => $this->session_id,
@@ -646,3 +641,51 @@ protected function streamRequest($method, array $data = [])
646641
return $this->doStreamRequest('Table', $method, $data);
647642
}
648643
}
644+
645+
/**
646+
* @param string $mode
647+
* @return array
648+
* @throws Exception
649+
*/
650+
function parseTxMode(string $mode): array
651+
{
652+
$tx_settings = [];
653+
654+
switch ($mode)
655+
{
656+
case 'stale':
657+
case 'stale_read_only':
658+
$tx_settings['stale_read_only'] = new StaleModeSettings;
659+
break;
660+
661+
case 'online':
662+
case 'online_read_only':
663+
$tx_settings['online_read_only'] = new OnlineModeSettings([
664+
'allow_inconsistent_reads' => false,
665+
]);
666+
break;
667+
668+
case 'inconsistent_reads':
669+
case 'online_inconsistent':
670+
case 'online_inconsistent_reads':
671+
$tx_settings['online_read_only'] = new OnlineModeSettings([
672+
'allow_inconsistent_reads' => true,
673+
]);
674+
break;
675+
676+
case 'snapshot':
677+
case 'snapshot_read_only':
678+
$tx_settings['snapshot_read_only'] = new SnapshotModeSettings;
679+
break;
680+
681+
case 'serializable':
682+
case 'serializable_read_write':
683+
$tx_settings['serializable_read_write'] = new SerializableModeSettings;
684+
break;
685+
686+
default:
687+
throw new Exception("Tx mode '".($mode ?? 'null')."' is not valid");
688+
}
689+
690+
return $tx_settings;
691+
}

src/Table.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,9 @@ public function sessionReleased($session)
227227
* @return mixed
228228
* @throws Exception
229229
*/
230-
public function transaction(Closure $closure)
230+
public function transaction(Closure $closure, string $mode = 'serializable_read_write')
231231
{
232-
return $this->session()->transaction($closure);
232+
return $this->session()->transaction($closure, $mode);
233233
}
234234

235235
/**
@@ -505,9 +505,12 @@ public function retryTransaction(Closure $userFunc, bool $idempotent = null, Ret
505505
if (!isset($options['callback_on_error'])) {
506506
$options['callback_on_error'] = function (\Exception $exception) {};
507507
}
508-
return $this->retrySession(function (Session $session) use ($options, $userFunc) {
508+
509+
$txMode = $options['tx_mode'] ?? 'serializable_read_write';
510+
511+
return $this->retrySession(function (Session $session) use ($txMode, $options, $userFunc) {
509512
try {
510-
$session->beginTransaction();
513+
$session->beginTransaction($txMode);
511514
$result = $userFunc($session);
512515
$session->commitTransaction();
513516
return $result;

src/YdbQuery.php

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Ydb\Table\SerializableModeSettings;
1313
use Ydb\Operations\OperationParams;
1414

15+
require_once 'Table.php';
1516
class YdbQuery
1617
{
1718
/**
@@ -147,41 +148,8 @@ public function txControl(TransactionControl $tx_control)
147148
*/
148149
public function beginTx($mode)
149150
{
150-
$tx_settings = [];
151151

152-
switch ($mode)
153-
{
154-
case 'stale':
155-
case 'stale_read_only':
156-
$tx_settings['stale_read_only'] = new StaleModeSettings;
157-
break;
158-
159-
case 'online':
160-
case 'online_read_only':
161-
$tx_settings['online_read_only'] = new OnlineModeSettings([
162-
'allow_inconsistent_reads' => false,
163-
]);
164-
break;
165-
166-
case 'inconsistent_reads':
167-
case 'online_inconsistent':
168-
case 'online_inconsistent_reads':
169-
$tx_settings['online_read_only'] = new OnlineModeSettings([
170-
'allow_inconsistent_reads' => true,
171-
]);
172-
break;
173-
174-
case 'snapshot':
175-
case 'snapshot_read_only':
176-
$tx_settings['snapshot_read_only'] = new SnapshotModeSettings;
177-
break;
178-
179-
case 'serializable':
180-
case 'serializable_read_write':
181-
default:
182-
$tx_settings['serializable_read_write'] = new SerializableModeSettings;
183-
break;
184-
}
152+
$tx_settings = parseTxMode($mode);
185153

186154
$this->tx_control = new TransactionControl([
187155
'begin_tx' => new TransactionSettings($tx_settings),

tests/CheckTxSettingsTest.php

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,21 @@
33
namespace YdbPlatform\Ydb\Test;
44

55
use PHPUnit\Framework\TestCase;
6+
use Ydb\Table\OnlineModeSettings;
7+
use Ydb\Table\SerializableModeSettings;
8+
use Ydb\Table\SnapshotModeSettings;
9+
use Ydb\Table\StaleModeSettings;
610
use YdbPlatform\Ydb\Auth\Implement\AnonymousAuthentication;
711
use YdbPlatform\Ydb\Logger\SimpleStdLogger;
812
use YdbPlatform\Ydb\Ydb;
13+
use function YdbPlatform\Ydb\parseTxMode;
14+
15+
require_once __DIR__.'/../src/Session.php';
916

1017
class CheckTxSettingsTest extends TestCase
1118
{
1219

13-
/**
14-
* @var Ydb
15-
*/
16-
protected $ydb;
17-
/**
18-
* @var \YdbPlatform\Ydb\Table
19-
*/
20-
protected $table;
21-
/**
22-
* @var \YdbPlatform\Ydb\Session|null
23-
*/
24-
protected $session;
25-
26-
public function __construct(?string $name = null, array $data = [], $dataName = '')
20+
public function testParseTxMode(?string $name = null, array $data = [], $dataName = '')
2721
{
2822
parent::__construct($name, $data, $dataName);
2923
$config = [
@@ -43,30 +37,41 @@ public function __construct(?string $name = null, array $data = [], $dataName =
4337
],
4438
'credentials' => new AnonymousAuthentication()
4539
];
46-
$this->ydb = new Ydb($config, new SimpleStdLogger(SimpleStdLogger::DEBUG));
47-
$this->table = $this->ydb->table();
48-
$this->session = $this->table->session();
49-
}
40+
$ydb = new Ydb($config, new SimpleStdLogger(SimpleStdLogger::DEBUG));
41+
$table = $ydb->table();
42+
$session = $table->createSession();
5043

51-
public function testSerializableTxConfig(){
52-
$this->checkTx('serializable', 'serializable_read_write');
53-
}
54-
55-
public function testSnapshotTxConfig(){
56-
$this->checkTx('snapshot', 'snapshot_read_only');
57-
}
58-
public function testStaleTxConfig(){
59-
$this->checkTx('stale', 'stale_read_only');
60-
}
61-
public function testOnlineTxConfig(){
62-
$this->checkTx('online', 'online_read_only');
63-
}
64-
65-
protected function checkTx(string $mode, string $value)
66-
{
67-
$query= $this->session->newQuery("SELECT 1;")
68-
->beginTx($mode);
69-
self::assertEquals($value, $query->getRequestData()['tx_control']->getBeginTx()->getTxMode());
70-
$query->execute();
44+
$testsQuery = [
45+
['mode' => 'stale_read_only', 'result' => ['stale_read_only' => new StaleModeSettings], 'interactive' => false],
46+
['mode' => 'stale', 'result' => ['stale_read_only' => new StaleModeSettings], 'interactive' => false],
47+
['mode' => 'online_read_only', 'result' => ['online_read_only' => new OnlineModeSettings([
48+
'allow_inconsistent_reads' => false,
49+
])], 'interactive' => false],
50+
['mode' => 'online', 'result' => ['online_read_only' => new OnlineModeSettings([
51+
'allow_inconsistent_reads' => false,
52+
])], 'interactive' => false],
53+
['mode' => 'inconsistent_reads', 'result' => ['online_read_only' => new OnlineModeSettings([
54+
'allow_inconsistent_reads' => true,
55+
])], 'interactive' => false],
56+
['mode' => 'online_inconsistent', 'result' => ['online_read_only' => new OnlineModeSettings([
57+
'allow_inconsistent_reads' => true,
58+
])], 'interactive' => false],
59+
['mode' => 'online_inconsistent_reads', 'result' => ['online_read_only' => new OnlineModeSettings([
60+
'allow_inconsistent_reads' => true,
61+
])], 'interactive' => false],
62+
['mode' => 'snapshot', 'result' => ['snapshot_read_only' => new SnapshotModeSettings], 'interactive' => true],
63+
['mode' => 'snapshot_read_only', 'result' => ['snapshot_read_only' => new SnapshotModeSettings], 'interactive' => true],
64+
['mode' => 'serializable', 'result' => ['serializable_read_write' => new SerializableModeSettings], 'interactive' => true],
65+
['mode' => 'serializable_read_write', 'result' => ['serializable_read_write' => new SerializableModeSettings], 'interactive' => true],
66+
];
67+
foreach ($testsQuery as $i => $test){
68+
self::assertEquals($test["result"], parseTxMode($test["mode"]));
69+
$query= $session->newQuery("SELECT 1;")
70+
->beginTx($test['mode']);
71+
$query->execute();
72+
if ($test['interactive']){
73+
$table->transaction(function (){}, $test['mode']);
74+
}
75+
}
7176
}
7277
}

0 commit comments

Comments
 (0)