quickwit-indexing 0.3.0

Quickwit indexing
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
// Copyright (C) 2021 Quickwit, Inc.
//
// Quickwit is offered under the AGPL v3.0 and as commercial software.
// For commercial licensing, contact us at hello@quickwit.io.
//
// AGPL:
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#[cfg(test)]
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::Instant;

use anyhow::Context;
use quickwit_metastore::SplitMetadata;
use quickwit_storage::{PutPayload, Storage, StorageResult};
use tantivy::Directory;
use tokio::sync::Mutex;
use tracing::info;

use super::LocalSplitStore;
use crate::split_store::SPLIT_CACHE_DIR_NAME;
use crate::{
    get_tantivy_directory_from_split_bundle, MergePolicy, SplitFolder,
    StableMultitenantWithTimestampMergePolicy,
};

/// `IndexingSplitStoreParams` encapsulates the various contraints of the cache.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct IndexingSplitStoreParams {
    /// Maximum number of files allowed in the cache.
    pub max_num_splits: usize,
    /// Maximum size in bytes allowed in the cache.
    pub max_num_bytes: usize,
}

impl Default for IndexingSplitStoreParams {
    fn default() -> Self {
        Self {
            max_num_splits: 1000,
            max_num_bytes: 100_000_000_000, // 100GB
        }
    }
}

/// IndexingSplitStore is a wrapper around a regular `Storage` to upload and
/// download splits while allowing for efficient caching.
///
/// We typically index with a limited amount of RAM or some constraints on the
/// expected time-to-search.
/// Because of these constraints, the indexer produces splits that are smaller
/// than optimal and need to be merged.
///
/// A split therefore typically undergoes a few merges relatively shortly after
/// its creation.
///
/// In order to alleviate the disk IO as well as the network bandwidth,
/// we save new splits into a split store.
///
/// The role of the `IndexingSplitStore` is to act as a cache to avoid
/// unnecessary download of fresh splits. Its behavior are however very different
/// from a usual cache as we have a strong knowledge of the split lifecycle.
///
/// The splits are stored on the local filesystem in `LocalSplitStore`.
#[derive(Clone)]
pub struct IndexingSplitStore {
    /// The remote storage.
    remote_storage: Arc<dyn Storage>,

    local_split_store: Option<Arc<Mutex<LocalSplitStore>>>,

    /// The merge policy is useful to identify whether a split
    /// should be stored in the local storage or not.
    /// (mature splits do not need to be stored).
    merge_policy: Arc<dyn MergePolicy>,
}

impl IndexingSplitStore {
    /// Create an instance of [`IndexingSplitStore`]
    ///
    /// It needs the remote storage to work with.
    pub fn create_with_local_store(
        remote_storage: Arc<dyn Storage>,
        cache_directory: &Path,
        cache_params: IndexingSplitStoreParams,
        merge_policy: Arc<dyn MergePolicy>,
    ) -> StorageResult<Self> {
        let local_storage_root = cache_directory.join(SPLIT_CACHE_DIR_NAME);
        std::fs::create_dir_all(&local_storage_root)?;
        let local_split_store = LocalSplitStore::open(local_storage_root, cache_params)?;
        Ok(Self {
            remote_storage,
            local_split_store: Some(Arc::new(Mutex::new(local_split_store))),
            merge_policy,
        })
    }

    /// Create a storage with upload cache in a temp directory for tests.
    pub fn create_with_no_local_store(remote_storage: Arc<dyn Storage>) -> Self {
        IndexingSplitStore {
            remote_storage,
            local_split_store: None,
            merge_policy: Arc::new(StableMultitenantWithTimestampMergePolicy::default()),
        }
    }

    /// Stores a split.
    ///
    /// If a split is identified as mature by the merge policy,
    /// it will not be cached into the local storage.
    ///
    /// In order to limit the write IO, the file might be moved (and not copied into
    /// the store).
    /// In other words, after calling this function the file will not be available
    /// at `split_folder` anymore.
    pub async fn store_split<'a>(
        &'a self,
        split: &'a SplitMetadata,
        split_folder: &'a Path,
        put_payload: Box<dyn PutPayload>,
    ) -> anyhow::Result<()> {
        info!("store-split-remote-start");

        let start = Instant::now();
        let split_num_bytes = put_payload.len();

        let key = PathBuf::from(quickwit_common::split_file(split.split_id()));
        self.remote_storage
            .put(&key, put_payload)
            .await
            .with_context(|| {
                format!(
                    "Failed uploading key {} in bucket {}",
                    key.display(),
                    self.remote_storage.uri()
                )
            })?;
        let elapsed_secs = start.elapsed().as_secs_f32();
        let split_size_in_megabytes = split_num_bytes / 1_000_000;
        let throughput_mb_s = split_size_in_megabytes as f32 / elapsed_secs;
        let is_mature = self.merge_policy.is_mature(split);

        info!(
            split_size_in_megabytes = %split_size_in_megabytes,
            elapsed_secs = %elapsed_secs,
            throughput_mb_s = %throughput_mb_s,
            is_mature = is_mature,
            "store-split-remote-success"
        );

        if !is_mature {
            info!("store-in-cache");
            if let Some(split_store) = self.local_split_store.as_ref() {
                let mut split_store_lock = split_store.lock().await;
                let tantivy_dir = SplitFolder::new(split_folder.to_path_buf());
                if split_store_lock
                    .move_into_cache(split.split_id(), tantivy_dir, split_num_bytes as usize)
                    .await?
                {
                    return Ok(());
                }
            }
        }

        tokio::fs::remove_dir_all(split_folder).await?;

        Ok(())
    }

    /// Delete a split.
    pub async fn delete(&self, split_id: &str) -> StorageResult<()> {
        let split_filename = quickwit_common::split_file(split_id);
        let split_path = Path::new(&split_filename);
        self.remote_storage.delete(split_path).await?;
        if let Some(local_split_store) = self.local_split_store.as_ref() {
            let mut local_split_store_lock = local_split_store.lock().await;
            local_split_store_lock.remove_split(split_id).await?;
        }
        Ok(())
    }

    /// Gets a split from the split store, and makes it available to the given `output_path`.
    ///
    /// The output_path is expected to be a directory path.
    pub async fn fetch_split(
        &self,
        split_id: &str,
        output_dir_path: &Path,
    ) -> StorageResult<Box<dyn Directory>> {
        let path = PathBuf::from(quickwit_common::split_file(split_id));
        if let Some(local_split_store) = self.local_split_store.as_ref() {
            let mut local_split_store_lock = local_split_store.lock().await;
            if let Some(split_folder) = local_split_store_lock
                .get_cached_split(split_id, output_dir_path)
                .await?
            {
                return split_folder.get_tantivy_directory();
            }
        }
        let start_time = Instant::now();
        let dest_filepath = output_dir_path.join(&path);
        info!(split_id = split_id, "fetch-split-from-remote-storage-start");
        self.remote_storage
            .copy_to_file(&path, &dest_filepath)
            .await?;
        info!(split_id=split_id,elapsed=?start_time.elapsed(), "fetch-split-from_remote-storage-success");
        get_tantivy_directory_from_split_bundle(&dest_filepath)
    }

    /// Removes the danglings splits.
    /// After a restart, the store might contains splits that are not relevant anymore.
    /// For instance, if the failure happens right before its publication, the split will be in the
    /// split store but not in the metastore.
    pub async fn remove_dangling_splits(
        &self,
        published_splits: &[SplitMetadata],
    ) -> StorageResult<()> {
        if let Some(local_split_store) = self.local_split_store.as_ref() {
            let published_split_ids: Vec<&str> = published_splits
                .iter()
                .filter(|split| !self.merge_policy.is_mature(split))
                .map(|split| split.split_id())
                .collect();

            let mut local_split_store_lock = local_split_store.lock().await;
            return local_split_store_lock
                .retain_only(&published_split_ids)
                .await;
        }

        Ok(())
    }

    /// Takes a snapshot of the cache view (only used for testing).
    #[cfg(test)]
    async fn inspect_local_store(&self) -> HashMap<String, usize> {
        if let Some(split_store) = self.local_split_store.as_ref() {
            let split_store_lock = split_store.lock().await;
            split_store_lock.inspect()
        } else {
            HashMap::default()
        }
    }
}

#[cfg(test)]
mod test_split_store {
    use std::path::Path;
    use std::sync::Arc;

    use quickwit_metastore::SplitMetadata;
    use quickwit_storage::{
        PutPayload, RamStorage, SplitPayloadBuilder, Storage, StorageError, StorageErrorKind,
    };
    use tempfile::tempdir;
    use tokio::fs;

    use super::{IndexingSplitStore, IndexingSplitStoreParams};
    use crate::split_store::SPLIT_CACHE_DIR_NAME;
    use crate::{MergePolicy, StableMultitenantWithTimestampMergePolicy};

    #[tokio::test]
    async fn test_create_should_error_with_wrong_num_files() -> anyhow::Result<()> {
        let local_dir = tempdir()?;
        let root_path = local_dir.path().join(SPLIT_CACHE_DIR_NAME);
        fs::create_dir_all(&root_path).await?;

        fs::create_dir_all(&root_path.join("a.split")).await?;
        fs::create_dir_all(&root_path.join("b.split")).await?;
        fs::create_dir_all(&root_path.join("c.split")).await?;

        let cache_params = IndexingSplitStoreParams {
            max_num_splits: 2,
            max_num_bytes: 10,
        };
        let remote_storage = Arc::new(RamStorage::default());
        let merge_policy = Arc::new(StableMultitenantWithTimestampMergePolicy::default());
        let result = IndexingSplitStore::create_with_local_store(
            remote_storage,
            local_dir.path(),
            cache_params,
            merge_policy,
        );
        assert!(matches!(
            result,
            Err(StorageError {
                kind: StorageErrorKind::InternalError,
                ..
            })
        ));
        Ok(())
    }

    #[tokio::test]
    async fn test_create_should_error_with_wrong_num_bytes() -> anyhow::Result<()> {
        let local_dir = tempdir()?;
        let root_path = local_dir.path().join(SPLIT_CACHE_DIR_NAME);
        fs::create_dir_all(&root_path).await?;

        fs::create_dir_all(&root_path.join("a.split")).await?;
        fs::create_dir_all(&root_path.join("b.split")).await?;

        let cache_params = IndexingSplitStoreParams {
            max_num_splits: 4,
            max_num_bytes: 10,
        };
        let remote_storage = Arc::new(RamStorage::default());
        let merge_policy = Arc::new(StableMultitenantWithTimestampMergePolicy::default());
        let result = IndexingSplitStore::create_with_local_store(
            remote_storage,
            local_dir.path(),
            cache_params,
            merge_policy,
        );
        assert!(matches!(
            result,
            Err(StorageError {
                kind: StorageErrorKind::InternalError,
                ..
            })
        ));
        Ok(())
    }

    #[tokio::test]
    async fn test_create_should_accept_a_file_size_exeeding_constraint() -> anyhow::Result<()> {
        let local_dir = tempdir()?;
        let root_path = local_dir.path().join(SPLIT_CACHE_DIR_NAME);
        fs::create_dir_all(&root_path).await?;
        fs::write(root_path.join("b.split"), b"abcd").await?;
        fs::write(root_path.join("a.split"), b"abcdefgh").await?;

        let cache_params = IndexingSplitStoreParams {
            max_num_splits: 100,
            max_num_bytes: 100,
        };
        let remote_storage = Arc::new(RamStorage::default());
        let merge_policy = Arc::new(StableMultitenantWithTimestampMergePolicy::default());
        let result = IndexingSplitStore::create_with_local_store(
            remote_storage,
            local_dir.path(),
            cache_params,
            merge_policy,
        );
        assert!(result.is_ok());
        Ok(())
    }

    fn create_test_split_metadata(split_id: &str) -> SplitMetadata {
        SplitMetadata {
            split_id: split_id.to_string(),
            ..Default::default()
        }
    }

    #[tokio::test]
    async fn test_local_store_cache_in_and_out() -> anyhow::Result<()> {
        let temp_dir = tempfile::tempdir()?;
        let split_cache_dir = tempdir()?;
        let merge_policy = Arc::new(StableMultitenantWithTimestampMergePolicy::default());
        let remote_storage = Arc::new(RamStorage::default());
        let split_store = IndexingSplitStore::create_with_local_store(
            remote_storage,
            split_cache_dir.path(),
            IndexingSplitStoreParams::default(),
            merge_policy.clone(),
        )?;
        {
            let split_path = temp_dir.path().join("split1");
            fs::create_dir_all(&split_path).await?;
            let split_metadata1 = create_test_split_metadata("split1");

            split_store
                .store_split(&split_metadata1, &split_path, Box::new(vec![1, 2, 3, 4]))
                .await?;
            assert!(!split_path.exists());
            assert!(split_cache_dir
                .path()
                .join(SPLIT_CACHE_DIR_NAME)
                .join("split1.split")
                .exists());
            let local_store_stats = split_store.inspect_local_store().await;
            assert_eq!(local_store_stats.len(), 1);
            assert_eq!(local_store_stats.get("split1").cloned(), Some(4));
        }
        {
            let split_path = temp_dir.path().join("split2");
            fs::create_dir_all(&split_path).await?;
            let split_metadata1 = create_test_split_metadata("split2");
            split_store
                .store_split(
                    &split_metadata1,
                    &split_path,
                    Box::new(SplitPayloadBuilder::get_split_payload(&[], &[5, 5, 5])?),
                )
                .await?;
            assert!(!split_path.exists());
            assert!(split_cache_dir
                .path()
                .join(SPLIT_CACHE_DIR_NAME)
                .join("split2.split")
                .exists());
            let local_store_stats = split_store.inspect_local_store().await;
            assert_eq!(local_store_stats.len(), 2);
            assert_eq!(local_store_stats.get("split1").cloned(), Some(4));
            assert_eq!(local_store_stats.get("split2").cloned(), Some(31));
        }

        Ok(())
    }

    #[tokio::test]
    async fn test_put_should_not_store_in_cache_when_max_num_files_reached() -> anyhow::Result<()> {
        let temp_dir = tempfile::tempdir()?;

        let split_cache_dir = tempdir()?;
        let merge_policy = Arc::new(StableMultitenantWithTimestampMergePolicy::default());
        let remote_storage = Arc::new(RamStorage::default());
        let split_store = IndexingSplitStore::create_with_local_store(
            remote_storage,
            split_cache_dir.path(),
            IndexingSplitStoreParams {
                max_num_splits: 1,
                max_num_bytes: 1_000_000,
            },
            merge_policy.clone(),
        )?;

        {
            let split_path = temp_dir.path().join("split1");
            fs::create_dir_all(&split_path).await?;
            let split_metadata1 = create_test_split_metadata("split1");
            split_store
                .store_split(
                    &split_metadata1,
                    &split_path,
                    Box::new(SplitPayloadBuilder::get_split_payload(&[], &[5, 5, 5])?),
                )
                .await?;
            assert!(!split_path.exists());
            assert!(split_cache_dir
                .path()
                .join(SPLIT_CACHE_DIR_NAME)
                .join("split1.split")
                .exists());
            let local_store_stats = split_store.inspect_local_store().await;
            assert_eq!(local_store_stats.len(), 1);
            assert_eq!(local_store_stats.get("split1").cloned(), Some(31));
        }
        {
            let split_path = temp_dir.path().join("split2");
            fs::create_dir_all(&split_path).await?;
            let split_metadata2 = create_test_split_metadata("split2");

            split_store
                .store_split(
                    &split_metadata2,
                    &split_path,
                    Box::new(SplitPayloadBuilder::get_split_payload(&[], &[5, 5, 5])?),
                )
                .await?;
            assert!(!split_path.exists());
            assert!(!split_cache_dir
                .path()
                .join(SPLIT_CACHE_DIR_NAME)
                .join("split2.split")
                .exists());
            let local_store_stats = split_store.inspect_local_store().await;
            assert_eq!(local_store_stats.len(), 1);
            assert_eq!(local_store_stats.get("split1").cloned(), Some(31));
        }
        {
            let output = tempfile::tempdir()?;
            // get from cache
            let _split1 = split_store.fetch_split("split1", output.path()).await?;
            // get from remote storage
            let _split2 = split_store.fetch_split("split2", output.path()).await?;
        }
        Ok(())
    }

    #[tokio::test]
    async fn test_put_should_not_store_in_cache_when_max_num_bytes_reached() -> anyhow::Result<()> {
        let temp_dir = tempfile::tempdir()?;

        let split_cache_dir = tempdir()?;
        let merge_policy = Arc::new(StableMultitenantWithTimestampMergePolicy::default());
        let remote_storage = Arc::new(RamStorage::default());
        let split_store = IndexingSplitStore::create_with_local_store(
            remote_storage,
            split_cache_dir.path(),
            IndexingSplitStoreParams {
                max_num_splits: 10,
                max_num_bytes: 40,
            },
            merge_policy.clone(),
        )?;

        {
            let split_path = temp_dir.path().join("split1");
            fs::create_dir_all(&split_path).await?;
            let split_metadata1 = create_test_split_metadata("split1");
            split_store
                .store_split(
                    &split_metadata1,
                    &split_path,
                    Box::new(SplitPayloadBuilder::get_split_payload(&[], &[5, 5, 5])?),
                )
                .await?;
            assert!(!split_path.exists());
            assert!(split_cache_dir
                .path()
                .join(SPLIT_CACHE_DIR_NAME)
                .join("split1.split")
                .exists());
            let local_store_stats = split_store.inspect_local_store().await;
            assert_eq!(local_store_stats.len(), 1);
            assert_eq!(local_store_stats.get("split1").cloned(), Some(31));
        }

        {
            let split_path = temp_dir.path().join("split2");
            fs::create_dir_all(&split_path).await?;
            let split_metadata2 = create_test_split_metadata("split2");
            split_store
                .store_split(
                    &split_metadata2,
                    &split_path,
                    Box::new(SplitPayloadBuilder::get_split_payload(&[], &[5, 5, 5])?),
                )
                .await?;
            assert!(!split_path.exists());
            assert!(!split_cache_dir
                .path()
                .join(SPLIT_CACHE_DIR_NAME)
                .join("split2.split")
                .exists());
            let local_store_stats = split_store.inspect_local_store().await;
            assert_eq!(local_store_stats.len(), 1);
            assert_eq!(local_store_stats.get("split2").cloned(), None);
        }
        Ok(())
    }

    #[tokio::test]
    async fn test_delete_should_remove_from_both_storage() -> anyhow::Result<()> {
        let temp_dir = tempfile::tempdir()?;

        let split_cache_dir = tempdir()?;
        let merge_policy = Arc::new(StableMultitenantWithTimestampMergePolicy::default());
        let remote_storage = Arc::new(RamStorage::default());
        let split_store = IndexingSplitStore::create_with_local_store(
            remote_storage.clone(),
            split_cache_dir.path(),
            IndexingSplitStoreParams {
                max_num_splits: 10,
                max_num_bytes: 40,
            },
            merge_policy.clone(),
        )?;

        let split_streamer = SplitPayloadBuilder::get_split_payload(&[], &[5, 5, 5])?;
        {
            let split_path = temp_dir.path().join("split2");
            fs::create_dir_all(&split_path).await?;
            let split_metadata1 = create_test_split_metadata("split1");
            split_store
                .store_split(
                    &split_metadata1,
                    &split_path,
                    Box::new(split_streamer.clone()),
                )
                .await?;
            assert!(!split_path.exists());
            assert!(split_cache_dir
                .path()
                .join(SPLIT_CACHE_DIR_NAME)
                .join("split1.split")
                .exists());
            let local_store_stats = split_store.inspect_local_store().await;
            assert_eq!(local_store_stats.len(), 1);
            assert_eq!(local_store_stats.get("split1").cloned(), Some(31));
        }

        let split1_bytes = remote_storage.get_all(Path::new("split1.split")).await?;
        assert_eq!(split1_bytes, &split_streamer.read_all().await?);

        split_store.delete("split1").await?;

        let storage_err = remote_storage
            .get_all(Path::new("split1.split"))
            .await
            .unwrap_err();
        assert_eq!(storage_err.kind(), StorageErrorKind::DoesNotExist);

        Ok(())
    }

    #[tokio::test]
    async fn test_remove_danglings_splits_should_remove_files() -> anyhow::Result<()> {
        let local_dir = tempdir()?;
        let root_path = local_dir.path().join(SPLIT_CACHE_DIR_NAME);
        fs::create_dir_all(&root_path).await?;
        fs::create_dir_all(&root_path.join("a.split")).await?;
        fs::create_dir_all(&root_path.join("b.split")).await?;
        fs::create_dir_all(&root_path.join("c.split")).await?;
        fs::write(root_path.join("a.split").join("termdict"), b"a").await?;
        fs::write(root_path.join("b.split").join("termdict"), b"b").await?;
        fs::write(root_path.join("c.split").join("termdict"), b"c").await?;

        let cache_params = IndexingSplitStoreParams {
            max_num_splits: 100,
            max_num_bytes: 200,
        };
        let remote_storage = Arc::new(RamStorage::default());
        let merge_policy = Arc::new(StableMultitenantWithTimestampMergePolicy::default());
        let split_store = IndexingSplitStore::create_with_local_store(
            remote_storage,
            local_dir.path(),
            cache_params,
            merge_policy,
        )?;
        let published_splits = vec![SplitMetadata {
            split_id: "b".to_string(),
            footer_offsets: 5..20,
            ..Default::default()
        }];
        split_store
            .remove_dangling_splits(&published_splits)
            .await?;
        assert!(!root_path.join("a.split").as_path().exists());
        assert!(!root_path.join("c.split").as_path().exists());
        assert!(root_path.join("b.split").as_path().exists());
        Ok(())
    }

    #[tokio::test]
    async fn test_mature_splits() -> anyhow::Result<()> {
        #[derive(Debug)]
        struct SplitsAreMature {}
        impl MergePolicy for SplitsAreMature {
            fn operations(
                &self,
                _: &mut Vec<SplitMetadata>,
            ) -> Vec<crate::merge_policy::MergeOperation> {
                unimplemented!()
            }
            fn is_mature(&self, _: &SplitMetadata) -> bool {
                true
            }
        }
        let temp_dir = tempfile::tempdir()?;
        let split_cache_dir = tempdir()?;
        let remote_storage = Arc::new(RamStorage::default());
        let split_store = IndexingSplitStore::create_with_local_store(
            remote_storage,
            split_cache_dir.path(),
            IndexingSplitStoreParams::default(),
            Arc::new(SplitsAreMature {}),
        )?;
        {
            let split_path = temp_dir.path().join("split1");
            fs::create_dir_all(&split_path).await?;
            let file_in_split = split_path.join("myfile");
            fs::write(&file_in_split, b"abcdefgh").await?;
            let split_metadata1 = create_test_split_metadata("split1");

            split_store
                .store_split(
                    &split_metadata1,
                    &split_path,
                    Box::new(SplitPayloadBuilder::get_split_payload(
                        &[file_in_split.to_owned()],
                        &[1, 2, 3],
                    )?),
                )
                .await?;
            assert!(!split_path.exists());
            assert!(!split_cache_dir
                .path()
                .join(SPLIT_CACHE_DIR_NAME)
                .join("split1.split")
                .exists());
            let local_store_stats = split_store.inspect_local_store().await;
            assert_eq!(local_store_stats.len(), 0);
            assert_eq!(local_store_stats.get("split1").cloned(), None);
        }

        Ok(())
    }
}