3
3
namespace App \Commands ;
4
4
5
5
use App \AppService ;
6
+ use YdbPlatform \Ydb \Retry \Backoff ;
7
+ use YdbPlatform \Ydb \Retry \RetryParams ;
8
+ use YdbPlatform \Ydb \Session ;
9
+ use YdbPlatform \Ydb \Ydb ;
6
10
use YdbPlatform \Ydb \YdbTable ;
7
11
use Symfony \Component \Console \Helper \Table ;
8
12
use Symfony \Component \Console \Command \Command ;
@@ -67,12 +71,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
67
71
*/
68
72
protected function print ($ value )
69
73
{
70
- if (is_array ($ value ))
71
- {
74
+ if (is_array ($ value )) {
72
75
$ this ->table ($ value );
73
- }
74
- else
75
- {
76
+ } else {
76
77
$ this ->output ->writeln ($ value );
77
78
}
78
79
}
@@ -82,48 +83,62 @@ protected function print($value)
82
83
*/
83
84
protected function table ($ array )
84
85
{
85
- if ($ array )
86
- {
86
+ if ($ array ) {
87
87
$ table = new Table ($ this ->output );
88
88
$ table
89
89
->setHeaders (array_keys ($ array [0 ]))
90
- ->setRows ($ array )
91
- ;
90
+ ->setRows ($ array );
92
91
$ table ->render ();
93
92
}
94
93
}
95
94
96
95
protected function runExample ()
97
96
{
98
97
$ this ->runQuery ('Create tables ' ,
99
- function () { $ this ->createTables (); });
98
+ function () {
99
+ $ this ->createTables ();
100
+ });
100
101
101
102
$ this ->runQuery ('Describe table ' ,
102
- function () { $ this ->describeTable ('seasons ' ); });
103
+ function () {
104
+ $ this ->describeTable ('seasons ' );
105
+ });
103
106
104
107
$ this ->runQuery ('Fill tables with data ' ,
105
- function () { $ this ->fillTablesWithData (); });
108
+ function () {
109
+ $ this ->fillTablesWithData ();
110
+ });
106
111
107
112
$ this ->runQuery ('Select simple transaction ' ,
108
- function () { $ this ->selectSimple (); });
113
+ function () {
114
+ $ this ->selectSimple ();
115
+ });
109
116
110
117
$ this ->runQuery ('Upsert simple transaction ' ,
111
- function () { $ this ->upsertSimple (); });
118
+ function () {
119
+ $ this ->upsertSimple ();
120
+ });
112
121
113
122
$ this ->runQuery ('Bulk upsert ' ,
114
- function () { $ this ->bulkUpsert (); });
123
+ function () {
124
+ $ this ->bulkUpsert ();
125
+ });
115
126
116
127
$ this ->runQuery ('Select prepared ' ,
117
- function () {
128
+ function () {
118
129
$ this ->selectPrepared (2 , 3 , 7 );
119
130
$ this ->selectPrepared (2 , 3 , 8 );
120
131
});
121
132
122
133
$ this ->runQuery ('Explicit TCL ' ,
123
- function () { $ this ->explicitTcl (2 , 6 , 1 ); });
134
+ function () {
135
+ $ this ->explicitTcl (2 , 6 , 1 );
136
+ });
124
137
125
138
$ this ->runQuery ('Select prepared ' ,
126
- function () { $ this ->selectPrepared (2 , 6 , 1 ); });
139
+ function () {
140
+ $ this ->selectPrepared (2 , 6 , 1 );
141
+ });
127
142
}
128
143
129
144
/**
@@ -140,43 +155,53 @@ protected function runQuery($header, $closure)
140
155
141
156
protected function createTables ()
142
157
{
143
- $ session = $ this ->ydb ->table ()->session ();
144
-
145
- $ session ->createTable (
146
- 'series ' ,
147
- YdbTable::make ()
148
- ->addColumn ('series_id ' , 'UINT64 ' )
149
- ->addColumn ('title ' , 'UTF8 ' )
150
- ->addColumn ('series_info ' , 'UTF8 ' )
151
- ->addColumn ('release_date ' , 'UINT64 ' )
152
- ->primaryKey ('series_id ' )
153
- );
158
+ $ this ->ydb ->table ()->retrySession (function (Session $ session ) {
159
+
160
+ $ session ->createTable (
161
+ 'series ' ,
162
+ YdbTable::make ()
163
+ ->addColumn ('series_id ' , 'UINT64 ' )
164
+ ->addColumn ('title ' , 'UTF8 ' )
165
+ ->addColumn ('series_info ' , 'UTF8 ' )
166
+ ->addColumn ('release_date ' , 'UINT64 ' )
167
+ ->primaryKey ('series_id ' )
168
+ );
169
+
170
+ }, true );
154
171
155
172
$ this ->print ('Table `series` has been created. ' );
156
173
157
- $ session ->createTable (
158
- 'seasons ' ,
159
- YdbTable::make ()
160
- ->addColumn ('series_id ' , 'UINT64 ' )
161
- ->addColumn ('season_id ' , 'UINT64 ' )
162
- ->addColumn ('title ' , 'UTF8 ' )
163
- ->addColumn ('first_aired ' , 'UINT64 ' )
164
- ->addColumn ('last_aired ' , 'UINT64 ' )
165
- ->primaryKey (['series_id ' , 'season_id ' ])
166
- );
174
+ $ this ->ydb ->table ()->retrySession (function (Session $ session ) {
175
+
176
+ $ session ->createTable (
177
+ 'seasons ' ,
178
+ YdbTable::make ()
179
+ ->addColumn ('series_id ' , 'UINT64 ' )
180
+ ->addColumn ('season_id ' , 'UINT64 ' )
181
+ ->addColumn ('title ' , 'UTF8 ' )
182
+ ->addColumn ('first_aired ' , 'UINT64 ' )
183
+ ->addColumn ('last_aired ' , 'UINT64 ' )
184
+ ->primaryKey (['series_id ' , 'season_id ' ])
185
+ );
186
+
187
+ }, true );
167
188
168
189
$ this ->print ('Table `seasons` has been created. ' );
169
190
170
- $ session ->createTable (
171
- 'episodes ' ,
172
- YdbTable::make ()
173
- ->addColumn ('series_id ' , 'UINT64 ' )
174
- ->addColumn ('season_id ' , 'UINT64 ' )
175
- ->addColumn ('episode_id ' , 'UINT64 ' )
176
- ->addColumn ('title ' , 'UTF8 ' )
177
- ->addColumn ('air_date ' , 'UINT64 ' )
178
- ->primaryKey (['series_id ' , 'season_id ' , 'episode_id ' ])
179
- );
191
+ $ this ->ydb ->table ()->retrySession (function (Session $ session ) {
192
+
193
+ $ session ->createTable (
194
+ 'episodes ' ,
195
+ YdbTable::make ()
196
+ ->addColumn ('series_id ' , 'UINT64 ' )
197
+ ->addColumn ('season_id ' , 'UINT64 ' )
198
+ ->addColumn ('episode_id ' , 'UINT64 ' )
199
+ ->addColumn ('title ' , 'UTF8 ' )
200
+ ->addColumn ('air_date ' , 'UINT64 ' )
201
+ ->primaryKey (['series_id ' , 'season_id ' , 'episode_id ' ])
202
+ );
203
+
204
+ }, true );
180
205
181
206
$ this ->print ('Table `episodes` has been created. ' );
182
207
}
@@ -186,14 +211,16 @@ protected function createTables()
186
211
*/
187
212
protected function describeTable ($ table )
188
213
{
189
- $ data = $ this ->ydb ->table ()->session ()->describeTable ($ table );
214
+ $ data = $ this ->ydb ->table ()->retrySession (function (Session $ session ) use ($ table ) {
215
+
216
+ return $ session ->describeTable ($ table );
217
+
218
+ }, true );
190
219
191
220
$ columns = [];
192
221
193
- foreach ($ data ['columns ' ] as $ column )
194
- {
195
- if (isset ($ column ['type ' ]['optionalType ' ]['item ' ]['typeId ' ]))
196
- {
222
+ foreach ($ data ['columns ' ] as $ column ) {
223
+ if (isset ($ column ['type ' ]['optionalType ' ]['item ' ]['typeId ' ])) {
197
224
$ columns [] = [
198
225
'Name ' => $ column ['name ' ],
199
226
'Type ' => $ column ['type ' ]['optionalType ' ]['item ' ]['typeId ' ],
@@ -205,72 +232,71 @@ protected function describeTable($table)
205
232
$ this ->print ($ columns );
206
233
$ this ->print ('' );
207
234
$ this ->print ('Primary key: ' . implode (', ' , (array )$ data ['primaryKey ' ]));
208
-
209
- // print_r($columns);
210
235
}
211
236
212
237
protected function fillTablesWithData ()
213
238
{
214
- $ session = $ this ->ydb ->table ()->session ();
239
+ $ params = new RetryParams (4000 ,null ,new Backoff (10 ,1000 ));
240
+ $ this ->ydb ->table ()->retryTransaction (function (Session $ session ) {
215
241
216
- $ prepared_query = $ session ->prepare ($ this ->getFillDataQuery ());
242
+ $ prepared_query = $ session ->prepare ($ this ->getFillDataQuery ());
217
243
218
- $ session ->transaction (function () use ($ prepared_query ) {
219
244
$ prepared_query ->execute ([
220
245
'seriesData ' => $ this ->getSeriesData (),
221
246
'seasonsData ' => $ this ->getSeasonsData (),
222
247
'episodesData ' => $ this ->getEpisodesData (),
223
248
]);
224
- });
249
+
250
+ }, false , $ params );
225
251
226
252
$ this ->print ('Finished. ' );
227
253
}
228
254
229
255
protected function selectSimple ()
230
256
{
231
- $ session = $ this ->ydb ->table ()->session ();
232
-
233
- $ result = $ session ->transaction (function ($ session ) {
234
- return $ session ->query ('
257
+ $ params = new RetryParams (4000 ,new Backoff (3 ,20 ));
258
+ $ result = $ this ->ydb ->table ()->retryTransaction (function (Session $ session ) {
259
+ return $ session ->query ('
235
260
$format = DateTime::Format("%Y-%m-%d");
236
261
SELECT
237
262
series_id,
238
263
title,
239
264
$format(DateTime::FromSeconds(CAST(release_date AS Uint32))) AS release_date
240
265
FROM series
241
266
WHERE series_id = 1; ' );
242
- });
267
+ }, true , $ params );
268
+
243
269
$ this ->print ($ result ->rows ());
244
270
}
245
271
246
272
protected function upsertSimple ()
247
273
{
248
- $ session = $ this ->ydb ->table ()->session ();
249
-
250
- $ session ->transaction (function ($ session ) {
274
+ $ this ->ydb ->table ()->retryTransaction (function (Session $ session ) {
251
275
return $ session ->query ('
252
276
UPSERT INTO episodes (series_id, season_id, episode_id, title)
253
277
VALUES (2, 6, 1, "TBD"); ' );
254
- });
278
+ }, true );
255
279
256
280
$ this ->print ('Finished. ' );
257
281
}
258
282
259
283
protected function bulkUpsert ()
260
284
{
261
- $ table = $ this ->ydb ->table ();
262
-
263
- $ table ->bulkUpsert (
264
- 'episodes ' ,
265
- $ this ->getEpisodesDataForBulkUpsert (),
266
- [
267
- 'series_id ' => 'Uint64 ' ,
268
- 'season_id ' => 'Uint64 ' ,
269
- 'episode_id ' => 'Uint64 ' ,
270
- 'title ' => 'Utf8 ' ,
271
- 'air_date ' => 'Uint64 ' ,
272
- ]
273
- );
285
+ $ this ->ydb ->retry (function (Ydb $ ydb ) {
286
+ $ table = $ ydb ->table ();
287
+
288
+ $ table ->bulkUpsert (
289
+ 'episodes ' ,
290
+ $ this ->getEpisodesDataForBulkUpsert (),
291
+ [
292
+ 'series_id ' => 'Uint64 ' ,
293
+ 'season_id ' => 'Uint64 ' ,
294
+ 'episode_id ' => 'Uint64 ' ,
295
+ 'title ' => 'Utf8 ' ,
296
+ 'air_date ' => 'Uint64 ' ,
297
+ ]
298
+ );
299
+ }, true );
274
300
275
301
$ this ->print ('Finished. ' );
276
302
}
@@ -282,9 +308,9 @@ protected function bulkUpsert()
282
308
*/
283
309
protected function selectPrepared ($ series_id , $ season_id , $ episode_id )
284
310
{
285
- $ session = $ this ->ydb ->table ()->session ();
311
+ $ result = $ this ->ydb ->table ()->retryTransaction ( function ( Session $ session) use ( $ series_id , $ season_id , $ episode_id ) {
286
312
287
- $ prepared_query = $ session ->prepare ('
313
+ $ prepared_query = $ session ->prepare ('
288
314
DECLARE $series_id AS Uint64;
289
315
DECLARE $season_id AS Uint64;
290
316
DECLARE $episode_id AS Uint64;
@@ -296,13 +322,12 @@ protected function selectPrepared($series_id, $season_id, $episode_id)
296
322
FROM episodes
297
323
WHERE series_id = $series_id AND season_id = $season_id AND episode_id = $episode_id; ' );
298
324
299
- $ result = $ session ->transaction (function ($ session ) use ($ prepared_query , $ series_id , $ season_id , $ episode_id ) {
300
325
return $ prepared_query ->execute (compact (
301
326
'series_id ' ,
302
327
'season_id ' ,
303
328
'episode_id '
304
329
));
305
- });
330
+ }, true );
306
331
307
332
$ this ->print ($ result ->rows ());
308
333
}
@@ -314,9 +339,9 @@ protected function selectPrepared($series_id, $season_id, $episode_id)
314
339
*/
315
340
protected function explicitTcl ($ series_id , $ season_id , $ episode_id )
316
341
{
317
- $ session = $ this ->ydb ->table ()->session ();
342
+ $ this ->ydb ->table ()->retryTransaction ( function ( Session $ session) use ( $ series_id , $ season_id , $ episode_id ) {
318
343
319
- $ prepared_query = $ session ->prepare ('
344
+ $ prepared_query = $ session ->prepare ('
320
345
DECLARE $today AS Uint64;
321
346
DECLARE $series_id AS Uint64;
322
347
DECLARE $season_id AS Uint64;
@@ -326,18 +351,17 @@ protected function explicitTcl($series_id, $season_id, $episode_id)
326
351
SET air_date = $today
327
352
WHERE series_id = $series_id AND season_id = $season_id AND episode_id = $episode_id; ' );
328
353
329
- $ session ->beginTransaction ();
330
354
331
- $ today = strtotime ('today ' );
355
+ $ today = strtotime ('today ' );
332
356
333
- $ prepared_query ->execute (compact (
334
- 'series_id ' ,
335
- 'season_id ' ,
336
- 'episode_id ' ,
337
- 'today '
338
- ));
357
+ $ prepared_query ->execute (compact (
358
+ 'series_id ' ,
359
+ 'season_id ' ,
360
+ 'episode_id ' ,
361
+ 'today '
362
+ ));
339
363
340
- $ session -> commitTransaction ( );
364
+ } );
341
365
342
366
$ this ->print ('Finished. ' );
343
367
}
0 commit comments