Skip to content

Commit 8b21de3

Browse files
authored
fix: Janitor list splits query Closes quickwit-oss#5848 (quickwit-oss#5850)
1 parent 5d2cc89 commit 8b21de3

File tree

5 files changed

+40
-1
lines changed

5 files changed

+40
-1
lines changed

quickwit/quickwit-janitor/src/retention_policy_execution.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub async fn run_execute_retention_policy(
4848
let max_retention_timestamp = current_timestamp - retention_period.as_secs() as i64;
4949
let query = ListSplitsQuery::for_index(index_uid.clone())
5050
.with_split_state(SplitState::Published)
51-
.with_time_range_end_lte(max_retention_timestamp);
51+
.with_max_time_range_end(max_retention_timestamp);
5252

5353
let list_splits_request = ListSplitsRequest::try_from_list_splits_query(&query)?;
5454
let (expired_splits, ignored_splits): (Vec<SplitMetadata>, Vec<SplitMetadata>) = ctx

quickwit/quickwit-metastore/src/metastore/file_backed/file_backed_index/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,11 @@ fn split_query_predicate(split: &&Split, query: &ListSplitsQuery) -> bool {
735735
if !query.time_range.overlaps_with(range.clone()) {
736736
return false;
737737
}
738+
if let Some(v) = query.max_time_range_end {
739+
if range.end() > &v {
740+
return false;
741+
}
742+
}
738743
}
739744

740745
if let Some(node_id) = &query.node_id {
@@ -889,6 +894,12 @@ mod tests {
889894
assert!(split_query_predicate(&&split_1, &query));
890895
assert!(!split_query_predicate(&&split_2, &query));
891896
assert!(!split_query_predicate(&&split_3, &query));
897+
898+
let query = ListSplitsQuery::for_index(IndexUid::new_with_random_ulid("test-index"))
899+
.with_max_time_range_end(50);
900+
assert!(split_query_predicate(&&split_1, &query));
901+
assert!(split_query_predicate(&&split_2, &query));
902+
assert!(!split_query_predicate(&&split_3, &query));
892903
}
893904

894905
#[test]

quickwit/quickwit-metastore/src/metastore/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,9 @@ pub struct ListSplitsQuery {
655655
/// The time range to filter by.
656656
pub time_range: FilterRange<i64>,
657657

658+
/// The maximum time range end to filter by.
659+
pub max_time_range_end: Option<i64>,
660+
658661
/// The delete opstamp range to filter by.
659662
pub delete_opstamp: FilterRange<u64>,
660663

@@ -721,6 +724,7 @@ impl ListSplitsQuery {
721724
split_states: Vec::new(),
722725
tags: None,
723726
time_range: Default::default(),
727+
max_time_range_end: None,
724728
delete_opstamp: Default::default(),
725729
update_timestamp: Default::default(),
726730
create_timestamp: Default::default(),
@@ -744,6 +748,7 @@ impl ListSplitsQuery {
744748
split_states: Vec::new(),
745749
tags: None,
746750
time_range: Default::default(),
751+
max_time_range_end: None,
747752
delete_opstamp: Default::default(),
748753
update_timestamp: Default::default(),
749754
create_timestamp: Default::default(),
@@ -763,6 +768,7 @@ impl ListSplitsQuery {
763768
split_states: Vec::new(),
764769
tags: None,
765770
time_range: Default::default(),
771+
max_time_range_end: None,
766772
delete_opstamp: Default::default(),
767773
update_timestamp: Default::default(),
768774
create_timestamp: Default::default(),
@@ -836,6 +842,13 @@ impl ListSplitsQuery {
836842
self
837843
}
838844

845+
/// Retains only splits with a time range end that is
846+
/// *less than or equal to* the provided value.
847+
pub fn with_max_time_range_end(mut self, v: i64) -> Self {
848+
self.max_time_range_end = Some(v);
849+
self
850+
}
851+
839852
/// Sets the field's lower bound to match values that are
840853
/// *less than or equal to* the provided value.
841854
pub fn with_delete_opstamp_lte(mut self, v: u64) -> Self {

quickwit/quickwit-metastore/src/metastore/postgres/metastore.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2110,6 +2110,17 @@ mod tests {
21102110
sql.to_string(PostgresQueryBuilder),
21112111
r#"SELECT * FROM "splits" WHERE "split_state" IN ('Staged')"#
21122112
);
2113+
2114+
let mut select_statement = Query::select();
2115+
let sql = select_statement.column(Asterisk).from(Splits::Table);
2116+
2117+
let query = ListSplitsQuery::for_all_indexes().with_max_time_range_end(42);
2118+
append_query_filters_and_order_by(sql, &query);
2119+
2120+
assert_eq!(
2121+
sql.to_string(PostgresQueryBuilder),
2122+
r#"SELECT * FROM "splits" WHERE "time_range_end" <= 42"#
2123+
);
21132124
}
21142125

21152126
#[test]

quickwit/quickwit-metastore/src/metastore/postgres/utils.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ pub(super) fn append_query_filters_and_order_by(
122122
sql.cond_where(generate_sql_condition(tags));
123123
};
124124

125+
if let Some(v) = query.max_time_range_end {
126+
sql.cond_where(Expr::col(Splits::TimeRangeEnd).lte(v));
127+
}
128+
125129
match query.time_range.start {
126130
Bound::Included(v) => {
127131
sql.cond_where(any![

0 commit comments

Comments
 (0)