Skip to content

Commit 026202b

Browse files
Add a hard limit on field list size (#5843)
Co-authored-by: Paul Masurel <paul.masurel@datadoghq.com>
1 parent c28caba commit 026202b

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

quickwit/quickwit-search/src/list_fields.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use anyhow::Context;
2121
use futures::future;
2222
use futures::future::try_join_all;
2323
use itertools::Itertools;
24+
use once_cell::sync::OnceCell;
2425
use quickwit_common::shared_consts::SPLIT_FIELDS_FILE_NAME;
2526
use quickwit_common::uri::Uri;
2627
use quickwit_metastore::SplitMetadata;
@@ -37,6 +38,19 @@ use crate::search_job_placer::group_jobs_by_index_id;
3738
use crate::service::SearcherContext;
3839
use crate::{ClusterClient, SearchError, SearchJob, list_relevant_splits, resolve_index_patterns};
3940

41+
/// QW_FIELD_LIST_SIZE_LIMIT defines a hard limit on the number of fields that
42+
/// can be returned (error otherwise).
43+
///
44+
/// Having many fields can happen when a user is creating fields dynamically in
45+
/// a JSON type with random field names. This leads to huge memory consumption
46+
/// when building the response. This is a workaround until a way is found to
47+
/// prune the long tail of rare fields.
48+
fn get_field_list_size_limit() -> usize {
49+
static FIELD_LIST_SIZE_LIMIT: OnceCell<usize> = OnceCell::new();
50+
*FIELD_LIST_SIZE_LIMIT
51+
.get_or_init(|| quickwit_common::get_from_env("QW_FIELD_LIST_SIZE_LIMIT", 100_000))
52+
}
53+
4054
/// Get the list of splits for the request which we need to scan.
4155
pub async fn get_fields_from_split(
4256
searcher_context: &SearcherContext,
@@ -184,6 +198,12 @@ fn merge_leaf_list_fields(
184198
flush_group(&mut responses, &mut current_group);
185199
}
186200
}
201+
if responses.len() >= get_field_list_size_limit() {
202+
return Err(SearchError::Internal(format!(
203+
"list fields response exceeded {} fields",
204+
get_field_list_size_limit()
205+
)));
206+
}
187207
current_group.push(entry);
188208
}
189209
if !current_group.is_empty() {

0 commit comments

Comments
 (0)