33namespace App \Commands ;
44
55use App \AppService ;
6+ use YdbPlatform \Ydb \Retry \Backoff ;
7+ use YdbPlatform \Ydb \Retry \RetryParams ;
8+ use YdbPlatform \Ydb \Session ;
9+ use YdbPlatform \Ydb \Ydb ;
610use YdbPlatform \Ydb \YdbTable ;
711use Symfony \Component \Console \Helper \Table ;
812use Symfony \Component \Console \Command \Command ;
@@ -67,12 +71,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
6771 */
6872 protected function print ($ value )
6973 {
70- if (is_array ($ value ))
71- {
74+ if (is_array ($ value )) {
7275 $ this ->table ($ value );
73- }
74- else
75- {
76+ } else {
7677 $ this ->output ->writeln ($ value );
7778 }
7879 }
@@ -82,48 +83,62 @@ protected function print($value)
8283 */
8384 protected function table ($ array )
8485 {
85- if ($ array )
86- {
86+ if ($ array ) {
8787 $ table = new Table ($ this ->output );
8888 $ table
8989 ->setHeaders (array_keys ($ array [0 ]))
90- ->setRows ($ array )
91- ;
90+ ->setRows ($ array );
9291 $ table ->render ();
9392 }
9493 }
9594
9695 protected function runExample ()
9796 {
9897 $ this ->runQuery ('Create tables ' ,
99- function () { $ this ->createTables (); });
98+ function () {
99+ $ this ->createTables ();
100+ });
100101
101102 $ this ->runQuery ('Describe table ' ,
102- function () { $ this ->describeTable ('seasons ' ); });
103+ function () {
104+ $ this ->describeTable ('seasons ' );
105+ });
103106
104107 $ this ->runQuery ('Fill tables with data ' ,
105- function () { $ this ->fillTablesWithData (); });
108+ function () {
109+ $ this ->fillTablesWithData ();
110+ });
106111
107112 $ this ->runQuery ('Select simple transaction ' ,
108- function () { $ this ->selectSimple (); });
113+ function () {
114+ $ this ->selectSimple ();
115+ });
109116
110117 $ this ->runQuery ('Upsert simple transaction ' ,
111- function () { $ this ->upsertSimple (); });
118+ function () {
119+ $ this ->upsertSimple ();
120+ });
112121
113122 $ this ->runQuery ('Bulk upsert ' ,
114- function () { $ this ->bulkUpsert (); });
123+ function () {
124+ $ this ->bulkUpsert ();
125+ });
115126
116127 $ this ->runQuery ('Select prepared ' ,
117- function () {
128+ function () {
118129 $ this ->selectPrepared (2 , 3 , 7 );
119130 $ this ->selectPrepared (2 , 3 , 8 );
120131 });
121132
122133 $ this ->runQuery ('Explicit TCL ' ,
123- function () { $ this ->explicitTcl (2 , 6 , 1 ); });
134+ function () {
135+ $ this ->explicitTcl (2 , 6 , 1 );
136+ });
124137
125138 $ this ->runQuery ('Select prepared ' ,
126- function () { $ this ->selectPrepared (2 , 6 , 1 ); });
139+ function () {
140+ $ this ->selectPrepared (2 , 6 , 1 );
141+ });
127142 }
128143
129144 /**
@@ -140,43 +155,53 @@ protected function runQuery($header, $closure)
140155
141156 protected function createTables ()
142157 {
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 );
154171
155172 $ this ->print ('Table `series` has been created. ' );
156173
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 );
167188
168189 $ this ->print ('Table `seasons` has been created. ' );
169190
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 );
180205
181206 $ this ->print ('Table `episodes` has been created. ' );
182207 }
@@ -186,14 +211,16 @@ protected function createTables()
186211 */
187212 protected function describeTable ($ table )
188213 {
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 );
190219
191220 $ columns = [];
192221
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 ' ])) {
197224 $ columns [] = [
198225 'Name ' => $ column ['name ' ],
199226 'Type ' => $ column ['type ' ]['optionalType ' ]['item ' ]['typeId ' ],
@@ -205,72 +232,71 @@ protected function describeTable($table)
205232 $ this ->print ($ columns );
206233 $ this ->print ('' );
207234 $ this ->print ('Primary key: ' . implode (', ' , (array )$ data ['primaryKey ' ]));
208-
209- // print_r($columns);
210235 }
211236
212237 protected function fillTablesWithData ()
213238 {
214- $ session = $ this ->ydb ->table ()->session ();
239+ $ params = new RetryParams (4000 ,null ,new Backoff (10 ,1000 ));
240+ $ this ->ydb ->table ()->retryTransaction (function (Session $ session ) {
215241
216- $ prepared_query = $ session ->prepare ($ this ->getFillDataQuery ());
242+ $ prepared_query = $ session ->prepare ($ this ->getFillDataQuery ());
217243
218- $ session ->transaction (function () use ($ prepared_query ) {
219244 $ prepared_query ->execute ([
220245 'seriesData ' => $ this ->getSeriesData (),
221246 'seasonsData ' => $ this ->getSeasonsData (),
222247 'episodesData ' => $ this ->getEpisodesData (),
223248 ]);
224- });
249+
250+ }, false , $ params );
225251
226252 $ this ->print ('Finished. ' );
227253 }
228254
229255 protected function selectSimple ()
230256 {
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 ('
235260 $format = DateTime::Format("%Y-%m-%d");
236261 SELECT
237262 series_id,
238263 title,
239264 $format(DateTime::FromSeconds(CAST(release_date AS Uint32))) AS release_date
240265 FROM series
241266 WHERE series_id = 1; ' );
242- });
267+ }, true , $ params );
268+
243269 $ this ->print ($ result ->rows ());
244270 }
245271
246272 protected function upsertSimple ()
247273 {
248- $ session = $ this ->ydb ->table ()->session ();
249-
250- $ session ->transaction (function ($ session ) {
274+ $ this ->ydb ->table ()->retryTransaction (function (Session $ session ) {
251275 return $ session ->query ('
252276 UPSERT INTO episodes (series_id, season_id, episode_id, title)
253277 VALUES (2, 6, 1, "TBD"); ' );
254- });
278+ }, true );
255279
256280 $ this ->print ('Finished. ' );
257281 }
258282
259283 protected function bulkUpsert ()
260284 {
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 );
274300
275301 $ this ->print ('Finished. ' );
276302 }
@@ -282,9 +308,9 @@ protected function bulkUpsert()
282308 */
283309 protected function selectPrepared ($ series_id , $ season_id , $ episode_id )
284310 {
285- $ session = $ this ->ydb ->table ()->session ();
311+ $ result = $ this ->ydb ->table ()->retryTransaction ( function ( Session $ session) use ( $ series_id , $ season_id , $ episode_id ) {
286312
287- $ prepared_query = $ session ->prepare ('
313+ $ prepared_query = $ session ->prepare ('
288314 DECLARE $series_id AS Uint64;
289315 DECLARE $season_id AS Uint64;
290316 DECLARE $episode_id AS Uint64;
@@ -296,13 +322,12 @@ protected function selectPrepared($series_id, $season_id, $episode_id)
296322 FROM episodes
297323 WHERE series_id = $series_id AND season_id = $season_id AND episode_id = $episode_id; ' );
298324
299- $ result = $ session ->transaction (function ($ session ) use ($ prepared_query , $ series_id , $ season_id , $ episode_id ) {
300325 return $ prepared_query ->execute (compact (
301326 'series_id ' ,
302327 'season_id ' ,
303328 'episode_id '
304329 ));
305- });
330+ }, true );
306331
307332 $ this ->print ($ result ->rows ());
308333 }
@@ -314,9 +339,9 @@ protected function selectPrepared($series_id, $season_id, $episode_id)
314339 */
315340 protected function explicitTcl ($ series_id , $ season_id , $ episode_id )
316341 {
317- $ session = $ this ->ydb ->table ()->session ();
342+ $ this ->ydb ->table ()->retryTransaction ( function ( Session $ session) use ( $ series_id , $ season_id , $ episode_id ) {
318343
319- $ prepared_query = $ session ->prepare ('
344+ $ prepared_query = $ session ->prepare ('
320345 DECLARE $today AS Uint64;
321346 DECLARE $series_id AS Uint64;
322347 DECLARE $season_id AS Uint64;
@@ -326,18 +351,17 @@ protected function explicitTcl($series_id, $season_id, $episode_id)
326351 SET air_date = $today
327352 WHERE series_id = $series_id AND season_id = $season_id AND episode_id = $episode_id; ' );
328353
329- $ session ->beginTransaction ();
330354
331- $ today = strtotime ('today ' );
355+ $ today = strtotime ('today ' );
332356
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+ ));
339363
340- $ session -> commitTransaction ( );
364+ } );
341365
342366 $ this ->print ('Finished. ' );
343367 }
0 commit comments