Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions quickwit/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions quickwit/quickwit-cli/src/tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ use quickwit_search::{SearchResponseRest, single_node_search};
use quickwit_serve::{
BodyFormat, SearchRequestQueryString, SortBy, search_request_from_api_request,
};
use quickwit_storage::{BundleStorage, Storage};
use quickwit_storage::BundleFileRanges;
use quickwit_transport::ChannelFactory;
use thousands::Separable;
use tracing::debug;
Expand Down Expand Up @@ -755,14 +755,15 @@ async fn extract_split_cli(args: ExtractSplitArgs) -> anyhow::Result<()> {
let index_storage = storage_resolver.resolve(index_metadata.index_uri()).await?;
let split_file = PathBuf::from(format!("{}.split", args.split_id));
let split_bytes = index_storage.get_all(split_file.as_path()).await?;
let (bundle_storage, _hotcache_bytes) =
BundleStorage::open_from_split_bytes(index_storage, split_file, split_bytes)?;
let (file_ranges, _hotcache_bytes) =
BundleFileRanges::open_from_split_bytes(split_bytes.clone())?;
std::fs::create_dir_all(&args.target_dir)?;
for path in bundle_storage.iter_files() {
for (path, range) in file_ranges.files {
let mut out_path = args.target_dir.to_owned();
out_path.push(path);
out_path.push(&path);
println!("Copying {out_path:?}");
bundle_storage.copy_to_file(path, &out_path).await?;
let file_slice = &split_bytes.as_slice()[range.start as usize..range.end as usize];
std::fs::write(&out_path, file_slice)?;
}

println!("{} Split successfully extracted.", "✔".color(GREEN_COLOR));
Expand Down
19 changes: 0 additions & 19 deletions quickwit/quickwit-config/src/node_config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -644,25 +644,6 @@ impl CacheConfig {
self.memory_capacity.unwrap_or_default()
}

pub fn memory_capacity_for_virtual_cache(
&mut self,
real_memory_capacity: ByteSize,
) -> ByteSize {
let memory_capacity = self.memory_capacity.unwrap_or(real_memory_capacity);
self.memory_capacity = Some(memory_capacity);
memory_capacity
}

pub fn policy(&self) -> CachePolicy {
self.policy.unwrap_or_default()
}

pub fn policy_for_virtual_cache(&mut self, real_policy: CachePolicy) -> CachePolicy {
let policy = self.policy.unwrap_or(real_policy);
self.policy = Some(policy);
policy
}

fn deserialize_with_default<'de, D, const DEFAULT_MEMORY_CAPACITY: u64>(
deserializer: D,
) -> Result<CacheConfig, D::Error>
Expand Down
7 changes: 3 additions & 4 deletions quickwit/quickwit-directories/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,15 @@ futures = { workspace = true }
postcard = { workspace = true }
serde = { workspace = true }
tantivy = { workspace = true }
tantivy-common = { workspace = true }
time = { workspace = true }
tokio = { workspace = true }
tracing = { workspace = true }

quickwit-common = { workspace = true }
quickwit-storage = { workspace = true }

[dev-dependencies]
proptest = { workspace = true }
quickwit-common = { workspace = true }
tantivy-fst = { workspace = true }
quickwit-storage = { workspace = true, features = ["testsuite"] }
rand = { workspace = true }
tempfile = { workspace = true }
tokio = { workspace = true }
11 changes: 6 additions & 5 deletions quickwit/quickwit-directories/src/bundle_directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use std::sync::Arc;
use std::{fmt, io};

use quickwit_storage::{
BundleFileRanges, OwnedBytes, Storage, StorageErrorKind, StorageResult,
BundleFileRanges, OwnedBytes, SplitStorageReader, Storage, StorageErrorKind, StorageResult,
locate_split_footer_range, strip_split_footer_trailer,
};
use tantivy::directory::error::OpenReadError;
Expand All @@ -29,7 +29,7 @@ use tantivy::{Directory, HasLen};
/// `BundleDirectory` is a read-only directory that opens a "split bundle" and serves its files
/// through Tantivy's [`Directory`] interface.
///
/// It is the `Directory` equivalent of `BundleStorage`.
/// It is the local-file `Directory` counterpart of `SplitBundle`.
///
/// A split has the following layout (all integer fields are little-endian):
///
Expand Down Expand Up @@ -99,11 +99,12 @@ pub async fn read_split_footer(
storage: Arc<dyn Storage>,
path: &Path,
) -> StorageResult<(OwnedBytes, OwnedBytes)> {
let split_len = storage.file_num_bytes(path).await?;
let footer_range = locate_split_footer_range(storage.as_ref(), path, split_len)
let reader = SplitStorageReader::new(storage);
let split_len = reader.file_num_bytes(path).await?;
let footer_range = locate_split_footer_range(&reader, path, split_len)
.await
.map_err(|error| StorageErrorKind::Internal.with_error(error))?;
let split_footer_with_trailer = storage
let split_footer_with_trailer = reader
.get_slice(path, footer_range.start as usize..footer_range.end as usize)
.await?;
let split_footer =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,26 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::collections::BTreeMap;
use std::collections::{BTreeMap, HashMap};
use std::ops::Range;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex};

use rustc_hash::FxHashMap;
use tantivy::directory::OwnedBytes;

struct CacheValue {
range_end: usize,
bytes: OwnedBytes,
}

#[derive(Default)]
struct FileByteRangeCacheState {
blocks: BTreeMap<usize, CacheValue>,
num_bytes: u64,
}

impl FileByteRangeCacheState {
fn with_infinite_capacity() -> Self {
FileByteRangeCacheState {
blocks: BTreeMap::new(),
num_bytes: 0,
}
}

fn get_slice(&mut self, byte_range: Range<usize>) -> Option<OwnedBytes> {
if byte_range.start == byte_range.end {
return Some(OwnedBytes::empty());
Expand Down Expand Up @@ -190,15 +183,15 @@ impl FileByteRangeCacheState {
/// Clones share the same cached ranges. Obtain this cache from
/// [`ByteRangeCache::get_file_cache`] so that all handles for one path share it.
#[derive(Clone)]
pub struct FileByteRangeCache {
pub(crate) struct FileByteRangeCache {
state: Arc<Mutex<FileByteRangeCacheState>>,
total_num_stored_bytes: Arc<AtomicU64>,
}

impl FileByteRangeCache {
fn with_total_num_stored_bytes(total_num_stored_bytes: Arc<AtomicU64>) -> Self {
FileByteRangeCache {
state: Arc::new(Mutex::new(FileByteRangeCacheState::with_infinite_capacity())),
state: Arc::new(Mutex::new(FileByteRangeCacheState::default())),
total_num_stored_bytes,
}
}
Expand Down Expand Up @@ -240,36 +233,26 @@ impl FileByteRangeCache {
/// once when opening a file, avoiding a path lookup and cross-file lock
/// contention for each range read.
///
/// Contrary to `MemorySizedCache`, it's able to answer subset of known ranges,
/// does not have any eviction, and assumes an infinite capacity.
/// It answers arbitrary sub-ranges of cached slices without eviction, and has no built-in
/// capacity limit.
///
/// This cache assume immutable data: if you put a new slice and it overlap with
/// cached data, the changes may or may not get recorded.
///
/// At the moment this is hardly a cache as it features no eviction policy.
#[derive(Clone)]
pub struct ByteRangeCache {
#[derive(Clone, Default)]
pub(crate) struct ByteRangeCache {
inner_arc: Arc<Inner>,
}

#[derive(Default)]
struct Inner {
total_num_stored_bytes: Arc<AtomicU64>,
// Paths identify virtual Tantivy files within this split, not storage object paths.
file_caches: Mutex<FxHashMap<PathBuf, FileByteRangeCache>>,
file_caches: Mutex<HashMap<PathBuf, FileByteRangeCache>>,
}

impl ByteRangeCache {
/// Creates a slice cache that never removes any entry.
pub fn with_infinite_capacity() -> Self {
let inner = Inner {
total_num_stored_bytes: Arc::new(AtomicU64::default()),
file_caches: Mutex::new(FxHashMap::default()),
};
ByteRangeCache {
inner_arc: Arc::new(inner),
}
}

/// Returns the shared byte-range cache for `path`.
pub fn get_file_cache(&self, path: &Path) -> FileByteRangeCache {
let mut file_caches = self
Expand Down Expand Up @@ -302,9 +285,9 @@ mod tests {
use std::path::Path;

use proptest::prelude::*;
use tantivy::directory::OwnedBytes;

use super::ByteRangeCache;
use crate::OwnedBytes;

#[derive(Debug)]
enum Operation {
Expand Down Expand Up @@ -355,7 +338,7 @@ mod tests {
state.insert("path1", vec![false; 12]);
state.insert("path2", vec![false; 12]);

let cache = ByteRangeCache::with_infinite_capacity();
let cache = ByteRangeCache::default();

for op in ops {
match op {
Expand Down Expand Up @@ -419,7 +402,7 @@ mod tests {

#[test]
fn test_byte_range_cache_is_shared_for_same_path() {
let cache = ByteRangeCache::with_infinite_capacity();
let cache = ByteRangeCache::default();
let first_file_cache = cache.get_file_cache(Path::new("path1"));
let second_file_cache = cache.get_file_cache(Path::new("path1"));
let other_file_cache = cache.get_file_cache(Path::new("path2"));
Expand All @@ -433,7 +416,7 @@ mod tests {

#[test]
fn test_byte_range_cache_put_slice_returns_num_added_bytes() {
let cache = ByteRangeCache::with_infinite_capacity();
let cache = ByteRangeCache::default();
let file_cache = cache.get_file_cache(Path::new("key"));
let bytes =
|range: Range<usize>| OwnedBytes::new(range.map(|i| i as u8).collect::<Vec<u8>>());
Expand All @@ -451,7 +434,7 @@ mod tests {

#[test]
fn test_byte_range_cache_doesnt_merge_unnecessarily() {
let cache = ByteRangeCache::with_infinite_capacity();
let cache = ByteRangeCache::default();
let file_cache = cache.get_file_cache(Path::new("key"));

file_cache.put_slice(0..5, OwnedBytes::new((0..5).collect::<Vec<_>>()));
Expand Down
Loading