@@ -26,7 +26,7 @@ use futures::StreamExt;
26
26
use itertools:: Itertools ;
27
27
use quickwit_common:: pretty:: PrettySample ;
28
28
use quickwit_common:: uri:: Uri ;
29
- use quickwit_common:: { get_bool_from_env, ServiceStream } ;
29
+ use quickwit_common:: { get_bool_from_env, rate_limited_error , ServiceStream } ;
30
30
use quickwit_config:: {
31
31
validate_index_id_pattern, IndexTemplate , IndexTemplateId , PostgresMetastoreConfig ,
32
32
} ;
@@ -304,17 +304,20 @@ async fn try_apply_delta_v2(
304
304
/// We still use this macro for them in order to make the code
305
305
/// "trivially correct".
306
306
macro_rules! run_with_tx {
307
- ( $connection_pool: expr, $tx_refmut: ident, $x: block) => { {
307
+ ( $connection_pool: expr, $tx_refmut: ident, $label : literal , $ x: block) => { {
308
308
let mut tx: Transaction <' _, Postgres > = $connection_pool. begin( ) . await ?;
309
309
let $tx_refmut = & mut tx;
310
310
let op_fut = move || async move { $x } ;
311
311
let op_result: MetastoreResult <_> = op_fut( ) . await ;
312
- if op_result. is_ok( ) {
313
- debug!( "committing transaction" ) ;
314
- tx. commit( ) . await ?;
315
- } else {
316
- warn!( "rolling transaction back" ) ;
317
- tx. rollback( ) . await ?;
312
+ match & op_result {
313
+ Ok ( _) => {
314
+ debug!( "committing transaction" ) ;
315
+ tx. commit( ) . await ?;
316
+ }
317
+ Err ( error) => {
318
+ rate_limited_error!( limit_per_min = 60 , error=%error, "failed to {}, rolling transaction back" , $label) ;
319
+ tx. rollback( ) . await ?;
320
+ }
318
321
}
319
322
op_result
320
323
} } ;
@@ -331,22 +334,17 @@ where
331
334
{
332
335
let index_id = & index_uid. index_id ;
333
336
let mut index_metadata = index_metadata ( tx, index_id, true ) . await ?;
337
+
334
338
if index_metadata. index_uid != index_uid {
335
339
return Err ( MetastoreError :: NotFound ( EntityKind :: Index {
336
340
index_id : index_id. to_string ( ) ,
337
341
} ) ) ;
338
342
}
339
-
340
343
if let MutationOccurred :: No ( ( ) ) = mutate_fn ( & mut index_metadata) ? {
341
344
return Ok ( index_metadata) ;
342
345
}
346
+ let index_metadata_json = serde_utils:: to_json_str ( & index_metadata) ?;
343
347
344
- let index_metadata_json = serde_json:: to_string ( & index_metadata) . map_err ( |error| {
345
- MetastoreError :: JsonSerializeError {
346
- struct_name : "IndexMetadata" . to_string ( ) ,
347
- message : error. to_string ( ) ,
348
- }
349
- } ) ?;
350
348
let update_index_res = sqlx:: query (
351
349
r#"
352
350
UPDATE indexes
@@ -426,7 +424,7 @@ impl MetastoreService for PostgresqlMetastore {
426
424
let doc_mapping = request. deserialize_doc_mapping ( ) ?;
427
425
428
426
let index_uid: IndexUid = request. index_uid ( ) . clone ( ) ;
429
- let updated_index_metadata = run_with_tx ! ( self . connection_pool, tx, {
427
+ let updated_index_metadata = run_with_tx ! ( self . connection_pool, tx, "update index" , {
430
428
mutate_index_metadata:: <MetastoreError , _>( tx, index_uid, |index_metadata| {
431
429
let mut mutation_occurred =
432
430
index_metadata. set_retention_policy( retention_policy_opt) ;
@@ -622,7 +620,7 @@ impl MetastoreService for PostgresqlMetastore {
622
620
tracing:: Span :: current ( ) . record ( "split_ids" , format ! ( "{split_ids:?}" ) ) ;
623
621
624
622
// TODO: Remove transaction.
625
- run_with_tx ! ( self . connection_pool, tx, {
623
+ run_with_tx ! ( self . connection_pool, tx, "stage splits" , {
626
624
let upserted_split_ids: Vec <String > = sqlx:: query_scalar( r#"
627
625
INSERT INTO splits
628
626
(split_id, time_range_start, time_range_end, tags, split_metadata_json, delete_opstamp, maturity_timestamp, split_state, index_uid, node_id)
@@ -698,7 +696,7 @@ impl MetastoreService for PostgresqlMetastore {
698
696
let staged_split_ids = request. staged_split_ids ;
699
697
let replaced_split_ids = request. replaced_split_ids ;
700
698
701
- run_with_tx ! ( self . connection_pool, tx, {
699
+ run_with_tx ! ( self . connection_pool, tx, "publish splits" , {
702
700
let mut index_metadata = index_metadata( tx, & index_uid. index_id, true ) . await ?;
703
701
if index_metadata. index_uid != index_uid {
704
702
return Err ( MetastoreError :: NotFound ( EntityKind :: Index {
@@ -744,12 +742,7 @@ impl MetastoreService for PostgresqlMetastore {
744
742
} ) ?;
745
743
}
746
744
}
747
- let index_metadata_json = serde_json:: to_string( & index_metadata) . map_err( |error| {
748
- MetastoreError :: JsonSerializeError {
749
- struct_name: "IndexMetadata" . to_string( ) ,
750
- message: error. to_string( ) ,
751
- }
752
- } ) ?;
745
+ let index_metadata_json = serde_utils:: to_json_str( & index_metadata) ?;
753
746
754
747
const PUBLISH_SPLITS_QUERY : & str = r#"
755
748
-- Select the splits to update, regardless of their state.
@@ -854,8 +847,7 @@ impl MetastoreService for PostgresqlMetastore {
854
847
}
855
848
info!(
856
849
%index_uid,
857
- "published {} splits and marked {} for deletion successfully" ,
858
- num_published_splits, num_marked_splits
850
+ "published {num_published_splits} splits and marked {num_marked_splits} for deletion successfully"
859
851
) ;
860
852
Ok ( EmptyResponse { } )
861
853
} )
@@ -1077,7 +1069,7 @@ impl MetastoreService for PostgresqlMetastore {
1077
1069
async fn add_source ( & self , request : AddSourceRequest ) -> MetastoreResult < EmptyResponse > {
1078
1070
let source_config = request. deserialize_source_config ( ) ?;
1079
1071
let index_uid: IndexUid = request. index_uid ( ) . clone ( ) ;
1080
- run_with_tx ! ( self . connection_pool, tx, {
1072
+ run_with_tx ! ( self . connection_pool, tx, "add source" , {
1081
1073
mutate_index_metadata:: <MetastoreError , _>( tx, index_uid, |index_metadata| {
1082
1074
index_metadata. add_source( source_config) ?;
1083
1075
Ok ( MutationOccurred :: Yes ( ( ) ) )
@@ -1091,7 +1083,7 @@ impl MetastoreService for PostgresqlMetastore {
1091
1083
#[ instrument( skip( self ) ) ]
1092
1084
async fn toggle_source ( & self , request : ToggleSourceRequest ) -> MetastoreResult < EmptyResponse > {
1093
1085
let index_uid: IndexUid = request. index_uid ( ) . clone ( ) ;
1094
- run_with_tx ! ( self . connection_pool, tx, {
1086
+ run_with_tx ! ( self . connection_pool, tx, "toggle source" , {
1095
1087
mutate_index_metadata( tx, index_uid, |index_metadata| {
1096
1088
if index_metadata. toggle_source( & request. source_id, request. enable) ? {
1097
1089
Ok :: <_, MetastoreError >( MutationOccurred :: Yes ( ( ) ) )
@@ -1109,7 +1101,7 @@ impl MetastoreService for PostgresqlMetastore {
1109
1101
async fn delete_source ( & self , request : DeleteSourceRequest ) -> MetastoreResult < EmptyResponse > {
1110
1102
let index_uid: IndexUid = request. index_uid ( ) . clone ( ) ;
1111
1103
let source_id = request. source_id . clone ( ) ;
1112
- run_with_tx ! ( self . connection_pool, tx, {
1104
+ run_with_tx ! ( self . connection_pool, tx, "delete source" , {
1113
1105
mutate_index_metadata( tx, index_uid. clone( ) , |index_metadata| {
1114
1106
index_metadata. delete_source( & source_id) ?;
1115
1107
Ok :: <_, MetastoreError >( MutationOccurred :: Yes ( ( ) ) )
@@ -1138,7 +1130,7 @@ impl MetastoreService for PostgresqlMetastore {
1138
1130
request : ResetSourceCheckpointRequest ,
1139
1131
) -> MetastoreResult < EmptyResponse > {
1140
1132
let index_uid: IndexUid = request. index_uid ( ) . clone ( ) ;
1141
- run_with_tx ! ( self . connection_pool, tx, {
1133
+ run_with_tx ! ( self . connection_pool, tx, "reset source checkpoint" , {
1142
1134
mutate_index_metadata( tx, index_uid, |index_metadata| {
1143
1135
if index_metadata. checkpoint. reset_source( & request. source_id) {
1144
1136
Ok :: <_, MetastoreError >( MutationOccurred :: Yes ( ( ) ) )
@@ -1178,12 +1170,7 @@ impl MetastoreService for PostgresqlMetastore {
1178
1170
/// Creates a delete task from a delete query.
1179
1171
#[ instrument( skip( self ) ) ]
1180
1172
async fn create_delete_task ( & self , delete_query : DeleteQuery ) -> MetastoreResult < DeleteTask > {
1181
- let delete_query_json = serde_json:: to_string ( & delete_query) . map_err ( |error| {
1182
- MetastoreError :: JsonSerializeError {
1183
- struct_name : "DeleteQuery" . to_string ( ) ,
1184
- message : error. to_string ( ) ,
1185
- }
1186
- } ) ?;
1173
+ let delete_query_json = serde_utils:: to_json_str ( & delete_query) ?;
1187
1174
let ( create_timestamp, opstamp) : ( sqlx:: types:: time:: PrimitiveDateTime , i64 ) =
1188
1175
sqlx:: query_as (
1189
1176
r#"
0 commit comments