Skip to content

Commit 3e54c37

Browse files
authored
Refactor test_cmd_garbage_collect_spares_files_within_grace_period (#2169)
1 parent ca969e2 commit 3e54c37

File tree

1 file changed

+63
-66
lines changed
  • quickwit/quickwit-cli/tests

1 file changed

+63
-66
lines changed

quickwit/quickwit-cli/tests/cli.rs

Lines changed: 63 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ use quickwit_common::ChecklistError;
4040
use quickwit_config::service::QuickwitService;
4141
use quickwit_config::CLI_INGEST_SOURCE_ID;
4242
use quickwit_indexing::actors::INDEXING_DIR_NAME;
43-
use quickwit_metastore::{quickwit_metastore_uri_resolver, Metastore, MetastoreError};
43+
use quickwit_metastore::{quickwit_metastore_uri_resolver, Metastore, MetastoreError, SplitState};
4444
use serde_json::{json, Number, Value};
4545
use tokio::time::{sleep, Duration};
4646

@@ -617,100 +617,97 @@ async fn test_garbage_collect_cli_no_grace() {
617617
}
618618

619619
#[tokio::test]
620-
async fn test_cmd_garbage_collect_spares_files_within_grace_period() -> Result<()> {
620+
async fn test_garbage_collect_index_cli() {
621621
let index_id = append_random_suffix("test-gc-cmd");
622-
let test_env = create_test_env(index_id, TestStorageType::LocalFileSystem)?;
622+
let test_env = create_test_env(index_id.clone(), TestStorageType::LocalFileSystem).unwrap();
623623
create_logs_index(&test_env);
624624
ingest_docs(test_env.resource_files["logs"].as_path(), &test_env);
625625

626-
let metastore = test_env.metastore().await?;
627-
let splits = metastore.list_all_splits(&test_env.index_id).await?;
626+
let refresh_metastore = |metastore| {
627+
// In this test we rely on the file backed metastore and
628+
// modify it but the file backed metastore caches results.
629+
// Therefore we need to force reading the disk to update split info.
630+
//
631+
// We do that by dropping and recreating our metastore.
632+
drop(metastore);
633+
quickwit_metastore_uri_resolver().resolve(&test_env.metastore_uri)
634+
};
635+
636+
let create_gc_args = |grace_period_secs| GarbageCollectIndexArgs {
637+
config_uri: test_env.config_uri.clone(),
638+
index_id: index_id.clone(),
639+
grace_period: Duration::from_secs(grace_period_secs),
640+
dry_run: false,
641+
};
642+
643+
let metastore = quickwit_metastore_uri_resolver()
644+
.resolve(&test_env.metastore_uri)
645+
.await
646+
.unwrap();
647+
648+
let splits = metastore.list_all_splits(&test_env.index_id).await.unwrap();
628649
assert_eq!(splits.len(), 1);
629-
make_command(
630-
format!(
631-
"index gc --index {} --config {}",
632-
test_env.index_id,
633-
test_env.resource_files["config"].display(),
634-
)
635-
.as_str(),
636-
)
637-
.assert()
638-
.success()
639-
.stdout(predicate::str::contains(
640-
"No dangling files to garbage collect",
641-
));
642650

643651
let index_path = test_env.indexes_dir_path.join(&test_env.index_id);
644652
let split_filename = quickwit_common::split_file(splits[0].split_metadata.split_id.as_str());
645653
let split_path = index_path.join(&split_filename);
646654
assert_eq!(split_path.exists(), true);
647655

656+
let args = create_gc_args(3600);
657+
658+
garbage_collect_index_cli(args).await.unwrap();
659+
660+
// Split should still exists within grace period.
661+
let metastore = refresh_metastore(metastore).await.unwrap();
662+
let splits = metastore.list_all_splits(&test_env.index_id).await.unwrap();
663+
assert_eq!(splits.len(), 1);
664+
648665
// The following steps help turn an existing published split into a staged one
649666
// without deleting the files.
650667
let split = splits[0].clone();
651668
let split_ids = [split.split_metadata.split_id.as_str()];
652669
metastore
653670
.mark_splits_for_deletion(&test_env.index_id, &split_ids)
654-
.await?;
671+
.await
672+
.unwrap();
655673
metastore
656674
.delete_splits(&test_env.index_id, &split_ids)
657-
.await?;
675+
.await
676+
.unwrap();
658677
metastore
659678
.stage_split(&test_env.index_id, split.split_metadata)
660-
.await?;
679+
.await
680+
.unwrap();
661681
assert_eq!(split_path.exists(), true);
662682

663-
make_command(
664-
format!(
665-
"index gc --index {} --config {} --grace-period 2s",
666-
test_env.index_id,
667-
test_env.resource_files["config"].display(),
668-
)
669-
.as_str(),
670-
)
671-
.assert()
672-
.success()
673-
.stdout(predicate::str::contains(
674-
"No dangling files to garbage collect",
675-
));
683+
let metastore = refresh_metastore(metastore).await.unwrap();
684+
let splits = metastore.list_all_splits(&test_env.index_id).await.unwrap();
685+
assert_eq!(splits[0].split_state, SplitState::Staged);
686+
687+
let args = create_gc_args(1);
688+
689+
garbage_collect_index_cli(args).await.unwrap();
690+
676691
assert_eq!(split_path.exists(), true);
692+
// Staged splits should still exist within grace period.
693+
let metastore = refresh_metastore(metastore).await.unwrap();
694+
let splits = metastore.list_all_splits(&test_env.index_id).await.unwrap();
695+
assert_eq!(splits.len(), 1);
696+
assert_eq!(splits[0].split_state, SplitState::Staged);
677697

678698
// Wait for grace period.
679699
// TODO: edit split update timestamps and remove this sleep.
680-
sleep(Duration::from_secs(3)).await;
681-
make_command(
682-
format!(
683-
"index gc --index {} --config {} --dry-run --grace-period 2s",
684-
test_env.index_id,
685-
test_env.resource_files["config"].display(),
686-
)
687-
.as_str(),
688-
)
689-
.assert()
690-
.success()
691-
.stdout(predicate::str::contains(
692-
"The following files will be garbage collected.",
693-
))
694-
.stdout(predicate::str::contains(&split_filename));
695-
assert_eq!(split_path.exists(), true);
700+
sleep(Duration::from_secs(2)).await;
696701

697-
make_command(
698-
format!(
699-
"index gc --index {} --config {} --grace-period 2s",
700-
test_env.index_id,
701-
test_env.resource_files["config"].display(),
702-
)
703-
.as_str(),
704-
)
705-
.assert()
706-
.success()
707-
.stdout(predicate::str::contains(format!(
708-
"Index `{}` successfully garbage collected",
709-
test_env.index_id
710-
)));
711-
assert_eq!(split_path.exists(), false);
702+
let args = create_gc_args(1);
712703

713-
Ok(())
704+
garbage_collect_index_cli(args).await.unwrap();
705+
706+
let metastore = refresh_metastore(metastore).await.unwrap();
707+
let splits = metastore.list_all_splits(&test_env.index_id).await.unwrap();
708+
// Splits should be deleted from both metastore and file system.
709+
assert_eq!(splits.len(), 0);
710+
assert_eq!(split_path.exists(), false);
714711
}
715712

716713
#[tokio::test]

0 commit comments

Comments
 (0)