@@ -21,6 +21,7 @@ use catalog::{
21
21
} ,
22
22
table_operator:: TableOperator ,
23
23
} ;
24
+ use common_types:: table:: ShardVersion ;
24
25
use generic_error:: BoxError ;
25
26
use logger:: info;
26
27
use snafu:: ResultExt ;
@@ -219,24 +220,16 @@ impl ShardOperator {
219
220
Ok ( ( ) )
220
221
}
221
222
222
- pub async fn create_table ( & self , ctx : CreateTableContext ) -> Result < ( ) > {
223
- let shard_info = ctx. updated_table_info . shard_info . clone ( ) ;
224
- let table_info = ctx. updated_table_info . table_info . clone ( ) ;
223
+ pub async fn create_table ( & self , ctx : CreateTableContext ) -> Result < ShardVersion > {
224
+ let shard_info = & ctx. updated_table_info . shard_info ;
225
+ let table_info = & ctx. updated_table_info . table_info ;
225
226
226
227
info ! (
227
- "ShardOperator create table sequentially begin, shard_info:{shard_info:?}, table_info:{table_info:?}" ,
228
+ "ShardOperator create table sequentially begin, shard_id:{}, table:{}, shard_info:{shard_info:?}, table_info:{table_info:?}" ,
229
+ shard_info. id,
230
+ table_info. name,
228
231
) ;
229
232
230
- // FIXME: maybe should insert table from cluster after having created table.
231
- {
232
- let mut data = self . data . write ( ) . unwrap ( ) ;
233
- data. try_insert_table ( ctx. updated_table_info )
234
- . box_err ( )
235
- . with_context ( || CreateTableWithCause {
236
- msg : format ! ( "shard_info:{shard_info:?}, table_info:{table_info:?}" ) ,
237
- } ) ?;
238
- }
239
-
240
233
// Create the table by operator afterwards.
241
234
let ( table_engine, partition_info) = match table_info. partition_info . clone ( ) {
242
235
Some ( v) => ( ctx. partition_table_engine . clone ( ) , Some ( v) ) ,
@@ -275,30 +268,37 @@ impl ShardOperator {
275
268
} ) ?;
276
269
277
270
info ! (
278
- "ShardOperator create table sequentially finish, shard_info:{shard_info:?}, table_info:{table_info:?}" ,
271
+ "ShardOperator table is created by operator, shard_id:{}, table:{}" ,
272
+ shard_info. id, table_info. name,
279
273
) ;
280
274
281
- Ok ( ( ) )
275
+ let latest_version = {
276
+ let mut data = self . data . write ( ) . unwrap ( ) ;
277
+ data. try_create_table ( ctx. updated_table_info . clone ( ) )
278
+ . box_err ( )
279
+ . with_context ( || CreateTableWithCause {
280
+ msg : format ! ( "shard_info:{shard_info:?}, table_info:{table_info:?}" ) ,
281
+ } ) ?
282
+ } ;
283
+
284
+ info ! (
285
+ "ShardOperator create table sequentially finish, shard_id:{}, shard_version:{}, table:{}" ,
286
+ shard_info. id, shard_info. version, table_info. name,
287
+ ) ;
288
+
289
+ Ok ( latest_version)
282
290
}
283
291
284
- pub async fn drop_table ( & self , ctx : DropTableContext ) -> Result < ( ) > {
285
- let shard_info = ctx. updated_table_info . shard_info . clone ( ) ;
286
- let table_info = ctx. updated_table_info . table_info . clone ( ) ;
292
+ pub async fn drop_table ( & self , ctx : DropTableContext ) -> Result < ShardVersion > {
293
+ let shard_info = & ctx. updated_table_info . shard_info ;
294
+ let table_info = & ctx. updated_table_info . table_info ;
287
295
288
296
info ! (
289
- "ShardOperator drop table sequentially begin, shard_info:{shard_info:?}, table_info:{table_info:?}" ,
297
+ "ShardOperator drop table sequentially begin, shard_id:{}, table:{}, shard_info:{shard_info:?}, table_info:{table_info:?}" ,
298
+ shard_info. id,
299
+ table_info. name,
290
300
) ;
291
301
292
- // FIXME: maybe should insert table from cluster after having dropped table.
293
- {
294
- let mut data = self . data . write ( ) . unwrap ( ) ;
295
- data. try_remove_table ( ctx. updated_table_info )
296
- . box_err ( )
297
- . with_context ( || DropTableWithCause {
298
- msg : format ! ( "shard_info:{shard_info:?}, table_info:{table_info:?}" ) ,
299
- } ) ?;
300
- }
301
-
302
302
// Drop the table by operator afterwards.
303
303
let drop_table_request = DropTableRequest {
304
304
catalog_name : ctx. catalog ,
@@ -319,31 +319,41 @@ impl ShardOperator {
319
319
} ) ?;
320
320
321
321
info ! (
322
- "ShardOperator drop table sequentially finish, shard_info:{shard_info:?}, table_info:{table_info:?}" ,
322
+ "ShardOperator table is dropped, shard_id:{}, table:{}" ,
323
+ shard_info. id, table_info. name,
323
324
) ;
324
325
325
- Ok ( ( ) )
326
+ // Update the shard info after the table is dropped.
327
+ let latest_version = {
328
+ let mut data = self . data . write ( ) . unwrap ( ) ;
329
+ data. try_drop_table ( ctx. updated_table_info . clone ( ) )
330
+ . box_err ( )
331
+ . with_context ( || DropTableWithCause {
332
+ msg : format ! ( "shard_info:{shard_info:?}, table_info:{table_info:?}" ) ,
333
+ } ) ?
334
+ } ;
335
+
336
+ info ! (
337
+ "ShardOperator drop table sequentially finish, latest_version:{latest_version}, shard_id:{}, old_shard_version:{}, table:{}" ,
338
+ shard_info. id,
339
+ shard_info. version,
340
+ table_info. name,
341
+ ) ;
342
+
343
+ Ok ( latest_version)
326
344
}
327
345
328
346
pub async fn open_table ( & self , ctx : OpenTableContext ) -> Result < ( ) > {
329
- let shard_info = ctx. updated_table_info . shard_info . clone ( ) ;
330
- let table_info = ctx. updated_table_info . table_info . clone ( ) ;
347
+ let shard_info = & ctx. updated_table_info . shard_info ;
348
+ let table_info = & ctx. updated_table_info . table_info ;
331
349
332
350
info ! (
333
- "ShardOperator open table sequentially begin, shard_info:{shard_info:?}, table_info:{table_info:?}" ,
351
+ "ShardOperator open table sequentially begin, shard_id:{}, table:{}, shard_info:{shard_info:?}, table_info:{table_info:?}" ,
352
+ shard_info. id,
353
+ table_info. name,
334
354
) ;
335
355
336
- // FIXME: maybe should insert table from cluster after having opened table.
337
- {
338
- let mut data = self . data . write ( ) . unwrap ( ) ;
339
- data. try_insert_table ( ctx. updated_table_info )
340
- . box_err ( )
341
- . with_context ( || OpenTableWithCause {
342
- msg : format ! ( "shard_info:{shard_info:?}, table_info:{table_info:?}" ) ,
343
- } ) ?;
344
- }
345
-
346
- // Open the table by operator afterwards.
356
+ // Open the table by operator.
347
357
let open_table_request = OpenTableRequest {
348
358
catalog_name : ctx. catalog ,
349
359
schema_name : table_info. schema_name . clone ( ) ,
@@ -366,28 +376,34 @@ impl ShardOperator {
366
376
} ) ?;
367
377
368
378
info ! (
369
- "ShardOperator open table sequentially finish, shard_info:{shard_info:?}, table_info:{table_info:?}" ,
379
+ "ShardOperator table is opened by operator, shard_id:{}, table:{}" ,
380
+ shard_info. id, table_info. name
370
381
) ;
371
382
372
- Ok ( ( ) )
373
- }
374
-
375
- pub async fn close_table ( & self , ctx : CloseTableContext ) -> Result < ( ) > {
376
- let shard_info = ctx. updated_table_info . shard_info . clone ( ) ;
377
- let table_info = ctx. updated_table_info . table_info . clone ( ) ;
378
-
379
- info ! ( "ShardOperator close table sequentially begin, shard_info:{shard_info:?}, table_info:{table_info:?}" ) ;
380
-
381
- // FIXME: maybe should remove table from cluster after having closed table.
383
+ // Update the shard info after the table is opened.
382
384
{
383
385
let mut data = self . data . write ( ) . unwrap ( ) ;
384
- data. try_remove_table ( ctx. updated_table_info )
386
+ data. try_open_table ( ctx. updated_table_info . clone ( ) )
385
387
. box_err ( )
386
- . with_context ( || CloseTableWithCause {
388
+ . with_context ( || OpenTableWithCause {
387
389
msg : format ! ( "shard_info:{shard_info:?}, table_info:{table_info:?}" ) ,
388
390
} ) ?;
389
391
}
390
392
393
+ info ! (
394
+ "ShardOperator open table sequentially finish, shard_id:{}, table:{}" ,
395
+ shard_info. id, table_info. name
396
+ ) ;
397
+
398
+ Ok ( ( ) )
399
+ }
400
+
401
+ pub async fn close_table ( & self , ctx : CloseTableContext ) -> Result < ( ) > {
402
+ let shard_info = & ctx. updated_table_info . shard_info ;
403
+ let table_info = & ctx. updated_table_info . table_info ;
404
+
405
+ info ! ( "ShardOperator close table sequentially begin, shard_id:{}, table:{}, shard_info:{shard_info:?}, table_info:{table_info:?}" , shard_info. id, table_info. name) ;
406
+
391
407
// Close the table by catalog manager afterwards.
392
408
let close_table_request = CloseTableRequest {
393
409
catalog_name : ctx. catalog ,
@@ -409,7 +425,25 @@ impl ShardOperator {
409
425
msg : format ! ( "shard_info:{shard_info:?}, table_info:{table_info:?}" ) ,
410
426
} ) ?;
411
427
412
- info ! ( "ShardOperator close table sequentially finish, shard_info:{shard_info:?}, table_info:{table_info:?}" ) ;
428
+ info ! (
429
+ "ShardOperator table is closed by operator, shard_id:{}, table:{}" ,
430
+ shard_info. id, table_info. name
431
+ ) ;
432
+
433
+ // Update the shard info after the table is closed.
434
+ {
435
+ let mut data = self . data . write ( ) . unwrap ( ) ;
436
+ data. try_close_table ( ctx. updated_table_info . clone ( ) )
437
+ . box_err ( )
438
+ . with_context ( || CloseTableWithCause {
439
+ msg : format ! ( "shard_info:{shard_info:?}, table_info:{table_info:?}" ) ,
440
+ } ) ?;
441
+ }
442
+
443
+ info ! (
444
+ "ShardOperator close table sequentially finish, shard_id:{}, table:{}" ,
445
+ shard_info. id, table_info. name
446
+ ) ;
413
447
414
448
Ok ( ( ) )
415
449
}
0 commit comments