Skip to content

Commit

Permalink
handle empty indices in cat indices
Browse files Browse the repository at this point in the history
  • Loading branch information
PSeitz committed Jan 31, 2024
1 parent 8f63440 commit 31490d0
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 29 deletions.
21 changes: 13 additions & 8 deletions quickwit/quickwit-serve/src/elasticsearch_api/model/cat_indices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use std::collections::HashSet;
use std::ops::AddAssign;

use hyper::StatusCode;
use quickwit_metastore::SplitMetadata;
use quickwit_metastore::{IndexMetadata, SplitMetadata};
use serde::{Deserialize, Serialize, Serializer};

use super::ElasticsearchError;
Expand Down Expand Up @@ -135,13 +135,6 @@ impl ElasticsearchCatIndexResponse {
}
impl AddAssign for ElasticsearchCatIndexResponse {
fn add_assign(&mut self, rhs: Self) {
// pri and rep are always 1, so we can just overwrite them
self.pri = rhs.pri;
self.rep = rhs.rep;
// Set index, since this may be a default entry
self.index = rhs.index;
self.uuid = rhs.uuid;

self.health += rhs.health;
self.status += rhs.status;
self.docs_count += rhs.docs_count;
Expand All @@ -152,6 +145,18 @@ impl AddAssign for ElasticsearchCatIndexResponse {
}
}

impl From<IndexMetadata> for ElasticsearchCatIndexResponse {
fn from(index_metadata: IndexMetadata) -> Self {
ElasticsearchCatIndexResponse {
uuid: index_metadata.index_uid.to_string(),
index: index_metadata.index_config.index_id.to_string(),
pri: "1".to_string(),
rep: "1".to_string(),
..Default::default()
}
}
}

impl From<SplitMetadata> for ElasticsearchCatIndexResponse {
fn from(split_metadata: SplitMetadata) -> Self {
ElasticsearchCatIndexResponse {
Expand Down
41 changes: 20 additions & 21 deletions quickwit/quickwit-serve/src/elasticsearch_api/rest_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,21 +426,23 @@ async fn es_compat_index_cat_indices(
) -> Result<Vec<serde_json::Value>, ElasticsearchError> {
query_params.validate()?;
let indexes_metadata = resolve_index_patterns(&index_id_patterns, &mut metastore).await?;
// Index id to index uid mapping
let index_uid_to_index_id: HashMap<IndexUid, String> = indexes_metadata
let mut index_id_to_resp: HashMap<IndexUid, ElasticsearchCatIndexResponse> = indexes_metadata
.iter()
.map(|metadata| (metadata.index_uid.clone(), metadata.index_id().to_owned()))
.map(|metadata| (metadata.index_uid.to_owned(), metadata.clone().into()))
.collect();

let index_uids = indexes_metadata
.into_iter()
.map(|index_metadata| index_metadata.index_uid)
.collect_vec();
// calling into the search module is not necessary, but reuses established patterns
let splits_metadata = list_all_splits(index_uids, &mut metastore).await?;
let splits_metadata = {
let index_uids = indexes_metadata
.into_iter()
.map(|index_metadata| index_metadata.index_uid)
.collect_vec();

// calling into the search module is not necessary, but reuses established patterns
list_all_splits(index_uids, &mut metastore).await?
};

let search_response_rest: Vec<ElasticsearchCatIndexResponse> =
convert_to_es_cat_indices_response(index_uid_to_index_id, splits_metadata);
convert_to_es_cat_indices_response(&mut index_id_to_resp, splits_metadata);

let search_response_rest = search_response_rest
.into_iter()
Expand Down Expand Up @@ -625,27 +627,24 @@ async fn es_scroll(
}

fn convert_to_es_cat_indices_response(
index_uid_to_index_id: HashMap<IndexUid, String>,
index_id_to_resp: &mut HashMap<IndexUid, ElasticsearchCatIndexResponse>,
splits: Vec<SplitMetadata>,
) -> Vec<ElasticsearchCatIndexResponse> {
let mut per_index: HashMap<String, ElasticsearchCatIndexResponse> = HashMap::new();

for split_metadata in splits {
let index_id = index_uid_to_index_id
.get(&split_metadata.index_uid)
let index_stats_entry = index_id_to_resp
.get_mut(&split_metadata.index_uid)
.unwrap_or_else(|| {
panic!(
"index_uid {} not found in index_uid_to_index_id",
"index_id {} not found in index_id_to_resp",
split_metadata.index_uid
)
});
let mut cat_index_entry: ElasticsearchCatIndexResponse = split_metadata.into();
cat_index_entry.index = index_id.to_owned();

let index_stats_entry = per_index.entry(index_id.to_owned()).or_default();
let cat_index_entry: ElasticsearchCatIndexResponse = split_metadata.into();
*index_stats_entry += cat_index_entry.clone();
}
let indices: Vec<ElasticsearchCatIndexResponse> = per_index.values().cloned().collect();
let mut indices: Vec<ElasticsearchCatIndexResponse> =
index_id_to_resp.values().cloned().collect();
indices.sort_by(|a, b| a.index.cmp(&b.index));

indices
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ engines:
- quickwit
endpoint: "_cat/indices?format=json"
expected:
- index: empty_index
docs.count: '0'
- dataset.size: 222.8kb
docs.count: '100'
docs.deleted: '0'
Expand All @@ -14,6 +16,10 @@ expected:
status: open
store.size: 271.8kb
#uuid: gharchive:01HN2SDANHDN6WFAFNH7BBMQ8C
- index: otel-logs-v0_7
docs.count: '0'
- index: otel-traces-v0_7
docs.count: '0'
---
method: [GET]
engines:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@ method: DELETE
endpoint: gharchive
status_code: null
---
# empty index
method: PUT
endpoint: empty_index
json: {
"mappings": {
"properties": {
"created_at": {
"type": "date",
"store": true
}
}
}
}
---
# Create index
method: PUT
endpoint: gharchive
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@ status_code: null
method: POST
api_root: http://localhost:7280/api/v1/
endpoint: indexes/
json:
version: "0.7"
index_id: empty_index
doc_mapping:
field_mappings:
- name: created_at
type: datetime
fast: true
sleep_after: 3
---
# Create index
method: POST
api_root: http://localhost:7280/api/v1/
endpoint: indexes/
json:
version: "0.7"
index_id: gharchive
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Delete possibly remaining index
method: DELETE
endpoint: gharchive
---
method: DELETE
endpoint: empty_index
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@
method: DELETE
api_root: http://localhost:7280/api/v1/
endpoint: indexes/gharchive
---
# Delete index
method: DELETE
api_root: http://localhost:7280/api/v1/
endpoint: indexes/empty_index

0 comments on commit 31490d0

Please sign in to comment.