@@ -24,6 +24,7 @@ use databend_common_meta_app::app_error::UnknownShareAccounts;
24
24
use databend_common_meta_app:: app_error:: UnknownTable ;
25
25
use databend_common_meta_app:: app_error:: WrongShare ;
26
26
use databend_common_meta_app:: app_error:: WrongShareObject ;
27
+ use databend_common_meta_app:: schema:: CreateOption ;
27
28
use databend_common_meta_app:: schema:: DBIdTableName ;
28
29
use databend_common_meta_app:: schema:: DatabaseId ;
29
30
use databend_common_meta_app:: schema:: DatabaseIdToName ;
@@ -988,17 +989,33 @@ impl<KV: kvapi::KVApi<Error = MetaError>> ShareApi for KV {
988
989
"create_share_endpoint"
989
990
) ;
990
991
992
+ let mut condition = vec ! [ ] ;
993
+ let mut if_then = vec ! [ ] ;
994
+
991
995
if share_endpoint_id_seq > 0 {
992
- return if req. if_not_exists {
993
- Ok ( CreateShareEndpointReply { share_endpoint_id } )
996
+ if let CreateOption :: CreateIfNotExists ( if_not_exists) = req. create_option {
997
+ return if if_not_exists {
998
+ Ok ( CreateShareEndpointReply { share_endpoint_id } )
999
+ } else {
1000
+ Err ( KVAppError :: AppError ( AppError :: ShareEndpointAlreadyExists (
1001
+ ShareEndpointAlreadyExists :: new (
1002
+ & name_key. endpoint ,
1003
+ format ! ( "create share endpoint: tenant: {}" , name_key. tenant) ,
1004
+ ) ,
1005
+ ) ) )
1006
+ } ;
994
1007
} else {
995
- Err ( KVAppError :: AppError ( AppError :: ShareEndpointAlreadyExists (
996
- ShareEndpointAlreadyExists :: new (
997
- & name_key. endpoint ,
998
- format ! ( "create share endpoint: tenant: {}" , name_key. tenant) ,
999
- ) ,
1000
- ) ) )
1001
- } ;
1008
+ construct_drop_share_endpoint_txn_operations (
1009
+ self ,
1010
+ name_key,
1011
+ false ,
1012
+ false ,
1013
+ func_name ! ( ) ,
1014
+ & mut condition,
1015
+ & mut if_then,
1016
+ )
1017
+ . await ?;
1018
+ }
1002
1019
}
1003
1020
1004
1021
// Create share endpoint by inserting these record:
@@ -1019,16 +1036,19 @@ impl<KV: kvapi::KVApi<Error = MetaError>> ShareApi for KV {
1019
1036
// Create share endpoint by transaction.
1020
1037
{
1021
1038
let share_endpoint_meta = ShareEndpointMeta :: new ( & req) ;
1022
- let txn_req = TxnRequest {
1023
- condition : vec ! [
1024
- txn_cond_seq( name_key, Eq , 0 ) ,
1025
- txn_cond_seq( & id_to_name_key, Eq , 0 ) ,
1026
- ] ,
1027
- if_then : vec ! [
1039
+ condition. extend ( vec ! [
1040
+ txn_cond_seq( name_key, Eq , share_endpoint_id_seq) ,
1041
+ txn_cond_seq( & id_to_name_key, Eq , 0 ) ,
1042
+ ] ) ;
1043
+ if_then. extend ( vec ! [
1028
1044
txn_op_put( name_key, serialize_u64( share_endpoint_id) ?) , /* (tenant, share_endpoint_name) -> share_endpoint_id */
1029
1045
txn_op_put( & id_key, serialize_struct( & share_endpoint_meta) ?) , /* (share_endpoint_id) -> share_endpoint_meta */
1030
1046
txn_op_put( & id_to_name_key, serialize_struct( name_key) ?) , /* __fd_share_endpoint_id_to_name/<share_endpoint_id> -> (tenant,share_endpoint_name) */
1031
- ] ,
1047
+ ] ) ;
1048
+
1049
+ let txn_req = TxnRequest {
1050
+ condition,
1051
+ if_then,
1032
1052
else_then : vec ! [ ] ,
1033
1053
} ;
1034
1054
@@ -1200,83 +1220,115 @@ impl<KV: kvapi::KVApi<Error = MetaError>> ShareApi for KV {
1200
1220
loop {
1201
1221
trials. next ( ) . unwrap ( ) ?. await ;
1202
1222
1203
- let res = get_share_endpoint_or_err (
1204
- self ,
1205
- name_key,
1206
- format ! ( "drop_share_endpoint: {}" , & name_key) ,
1207
- )
1208
- . await ;
1209
-
1210
- let (
1211
- share_endpoint_id_seq,
1212
- share_endpoint_id,
1213
- share_endpoint_meta_seq,
1214
- _share_endpoint_meta,
1215
- ) = match res {
1216
- Ok ( x) => x,
1217
- Err ( e) => {
1218
- if let KVAppError :: AppError ( AppError :: UnknownShareEndpoint ( _) ) = e {
1219
- if req. if_exists {
1220
- return Ok ( DropShareEndpointReply { } ) ;
1221
- }
1222
- }
1223
+ let mut condition = vec ! [ ] ;
1224
+ let mut if_then = vec ! [ ] ;
1223
1225
1224
- return Err ( e) ;
1225
- }
1226
+ let share_endpoint_id = if let Some ( share_endpoint_id) =
1227
+ construct_drop_share_endpoint_txn_operations (
1228
+ self ,
1229
+ name_key,
1230
+ req. if_exists ,
1231
+ true ,
1232
+ func_name ! ( ) ,
1233
+ & mut condition,
1234
+ & mut if_then,
1235
+ )
1236
+ . await ?
1237
+ {
1238
+ share_endpoint_id
1239
+ } else {
1240
+ return Ok ( DropShareEndpointReply { } ) ;
1226
1241
} ;
1227
- let ( share_endpoint_name_seq, _share_endpoint) = get_share_endpoint_id_to_name_or_err (
1228
- self ,
1229
- share_endpoint_id,
1230
- format ! ( "drop_share_endpoint: {}" , & name_key) ,
1231
- )
1232
- . await ?;
1233
1242
1234
- // Delete share endpoint by these operations:
1235
- // del (tenant, share_endpoint)
1236
- // del share_endpoint_id
1237
- // del (share_endpoint_id) -> (tenant, share_endpoint)
1243
+ let txn_req = TxnRequest {
1244
+ condition,
1245
+ if_then,
1246
+ else_then : vec ! [ ] ,
1247
+ } ;
1238
1248
1239
- let mut condition = vec ! [ ] ;
1240
- let mut if_then = vec ! [ ] ;
1249
+ let ( succ, _responses) = send_txn ( self , txn_req) . await ?;
1241
1250
1242
1251
let share_id_key = ShareEndpointId { share_endpoint_id } ;
1243
- let id_name_key = ShareEndpointIdToName { share_endpoint_id } ;
1244
-
1245
1252
debug ! (
1246
- share_endpoint_id = share_endpoint_id,
1247
- name_key = as_debug!( name_key) ;
1253
+ name = as_debug!( name_key) ,
1254
+ id = as_debug!( & share_id_key) ,
1255
+ succ = succ;
1248
1256
"drop_share_endpoint"
1249
1257
) ;
1250
1258
1251
- {
1252
- condition. push ( txn_cond_seq ( name_key, Eq , share_endpoint_id_seq) ) ;
1253
- condition. push ( txn_cond_seq ( & share_id_key, Eq , share_endpoint_meta_seq) ) ;
1254
- condition. push ( txn_cond_seq ( & id_name_key, Eq , share_endpoint_name_seq) ) ;
1255
- if_then. push ( txn_op_del ( name_key) ) ; // del (tenant, share_endpoint)
1256
- if_then. push ( txn_op_del ( & share_id_key) ) ; // del share_endpoint_id
1257
- if_then. push ( txn_op_del ( & id_name_key) ) ; // del (share_endpoint_id) -> (tenant, share_endpoint)
1258
-
1259
- let txn_req = TxnRequest {
1260
- condition,
1261
- if_then,
1262
- else_then : vec ! [ ] ,
1263
- } ;
1264
-
1265
- let ( succ, _responses) = send_txn ( self , txn_req) . await ?;
1259
+ if succ {
1260
+ return Ok ( DropShareEndpointReply { } ) ;
1261
+ }
1262
+ }
1263
+ }
1264
+ }
1266
1265
1267
- debug ! (
1268
- name = as_debug!( name_key) ,
1269
- id = as_debug!( & share_id_key) ,
1270
- succ = succ;
1271
- "drop_share_endpoint"
1272
- ) ;
1266
+ async fn construct_drop_share_endpoint_txn_operations (
1267
+ kv_api : & ( impl kvapi:: KVApi < Error = MetaError > + ?Sized ) ,
1268
+ name_key : & ShareEndpointIdent ,
1269
+ drop_if_exists : bool ,
1270
+ if_delete : bool ,
1271
+ ctx : & str ,
1272
+ condition : & mut Vec < TxnCondition > ,
1273
+ if_then : & mut Vec < TxnOp > ,
1274
+ ) -> Result < Option < u64 > , KVAppError > {
1275
+ let res = get_share_endpoint_or_err (
1276
+ kv_api,
1277
+ name_key,
1278
+ format ! (
1279
+ "construct_drop_share_endpoint_txn_operations: {}" ,
1280
+ & name_key
1281
+ ) ,
1282
+ )
1283
+ . await ;
1273
1284
1274
- if succ {
1275
- return Ok ( DropShareEndpointReply { } ) ;
1285
+ let ( share_endpoint_id_seq, share_endpoint_id, share_endpoint_meta_seq, _share_endpoint_meta) =
1286
+ match res {
1287
+ Ok ( x) => x,
1288
+ Err ( e) => {
1289
+ if let KVAppError :: AppError ( AppError :: UnknownShareEndpoint ( _) ) = e {
1290
+ if drop_if_exists {
1291
+ return Ok ( None ) ;
1292
+ }
1276
1293
}
1294
+
1295
+ return Err ( e) ;
1277
1296
}
1278
- }
1297
+ } ;
1298
+ let ( share_endpoint_name_seq, _share_endpoint) = get_share_endpoint_id_to_name_or_err (
1299
+ kv_api,
1300
+ share_endpoint_id,
1301
+ format ! (
1302
+ "construct_drop_share_endpoint_txn_operations: {}" ,
1303
+ & name_key
1304
+ ) ,
1305
+ )
1306
+ . await ?;
1307
+
1308
+ // Delete share endpoint by these operations:
1309
+ // del (tenant, share_endpoint)
1310
+ // del share_endpoint_id
1311
+ // del (share_endpoint_id) -> (tenant, share_endpoint)
1312
+ let share_id_key = ShareEndpointId { share_endpoint_id } ;
1313
+ let id_name_key = ShareEndpointIdToName { share_endpoint_id } ;
1314
+
1315
+ debug ! (
1316
+ share_endpoint_id = share_endpoint_id,
1317
+ name_key = as_debug!( name_key) ,
1318
+ ctx = ctx;
1319
+ "construct_drop_share_endpoint_txn_operations"
1320
+ ) ;
1321
+
1322
+ condition. push ( txn_cond_seq ( & share_id_key, Eq , share_endpoint_meta_seq) ) ;
1323
+ condition. push ( txn_cond_seq ( & id_name_key, Eq , share_endpoint_name_seq) ) ;
1324
+ if_then. push ( txn_op_del ( & share_id_key) ) ; // del share_endpoint_id
1325
+ if_then. push ( txn_op_del ( & id_name_key) ) ; // del (share_endpoint_id) -> (tenant, share_endpoint)
1326
+ if if_delete {
1327
+ condition. push ( txn_cond_seq ( name_key, Eq , share_endpoint_id_seq) ) ;
1328
+ if_then. push ( txn_op_del ( name_key) ) ; // del (tenant, share_endpoint)
1279
1329
}
1330
+
1331
+ Ok ( Some ( share_endpoint_id) )
1280
1332
}
1281
1333
1282
1334
async fn get_share_database_name (
0 commit comments