From 44850e103612b423e2febf7c9b477917069a84af Mon Sep 17 00:00:00 2001 From: PSeitz Date: Thu, 22 Jun 2023 12:59:11 +0800 Subject: [PATCH] move fail dep to dev only (#2094) wasm compilation fails with dep only --- Cargo.toml | 4 ++-- src/core/segment_reader.rs | 4 +--- src/directory/ram_directory.rs | 3 +-- src/indexer/segment_updater.rs | 3 +-- src/lib.rs | 29 +++++++++++++++++++++++++++++ src/postings/serializer.rs | 3 +-- 6 files changed, 35 insertions(+), 11 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3b4bebf8e..bab8c72bd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -44,7 +44,7 @@ census = "0.4.0" rustc-hash = "1.1.0" thiserror = "1.0.30" htmlescape = "0.3.1" -fail = "0.5.0" +fail = { version = "0.5.0", optional = true } murmurhash32 = "0.3.0" time = { version = "0.3.10", features = ["serde-well-known"] } smallvec = "1.8.0" @@ -112,7 +112,7 @@ lz4-compression = ["lz4_flex"] snappy-compression = ["snap"] zstd-compression = ["zstd"] -failpoints = ["fail/failpoints"] +failpoints = ["fail", "fail/failpoints"] unstable = [] # useful for benches. quickwit = ["sstable", "futures-util"] diff --git a/src/core/segment_reader.rs b/src/core/segment_reader.rs index f42610e12..a65161cad 100644 --- a/src/core/segment_reader.rs +++ b/src/core/segment_reader.rs @@ -2,8 +2,6 @@ use std::collections::HashMap; use std::sync::{Arc, RwLock}; use std::{fmt, io}; -use fail::fail_point; - use crate::core::{InvertedIndexReader, Segment, SegmentComponent, SegmentId}; use crate::directory::{CompositeFile, FileSlice}; use crate::error::DataCorruption; @@ -151,7 +149,7 @@ impl SegmentReader { let store_file = segment.open_read(SegmentComponent::Store)?; - fail_point!("SegmentReader::open#middle"); + crate::fail_point!("SegmentReader::open#middle"); let postings_file = segment.open_read(SegmentComponent::Postings)?; let postings_composite = CompositeFile::open(&postings_file)?; diff --git a/src/directory/ram_directory.rs b/src/directory/ram_directory.rs index 4fc00c887..56e9e3648 100644 --- a/src/directory/ram_directory.rs +++ b/src/directory/ram_directory.rs @@ -5,7 +5,6 @@ use std::sync::{Arc, RwLock}; use std::{fmt, result}; use common::HasLen; -use fail::fail_point; use super::FileHandle; use crate::core::META_FILEPATH; @@ -184,7 +183,7 @@ impl Directory for RamDirectory { } fn delete(&self, path: &Path) -> result::Result<(), DeleteError> { - fail_point!("RamDirectory::delete", |_| { + crate::fail_point!("RamDirectory::delete", |_| { Err(DeleteError::IoError { io_error: Arc::new(io::Error::from(io::ErrorKind::Other)), filepath: path.to_path_buf(), diff --git a/src/indexer/segment_updater.rs b/src/indexer/segment_updater.rs index 5fa027530..6b8efc7bf 100644 --- a/src/indexer/segment_updater.rs +++ b/src/indexer/segment_updater.rs @@ -6,7 +6,6 @@ use std::path::PathBuf; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::{Arc, RwLock}; -use fail::fail_point; use rayon::{ThreadPool, ThreadPoolBuilder}; use super::segment_manager::SegmentManager; @@ -43,7 +42,7 @@ pub(crate) fn save_metas(metas: &IndexMeta, directory: &dyn Directory) -> crate: let mut buffer = serde_json::to_vec_pretty(metas)?; // Just adding a new line at the end of the buffer. writeln!(&mut buffer)?; - fail_point!("save_metas", |msg| Err(crate::TantivyError::from( + crate::fail_point!("save_metas", |msg| Err(crate::TantivyError::from( std::io::Error::new( std::io::ErrorKind::Other, msg.unwrap_or_else(|| "Undefined".to_string()) diff --git a/src/lib.rs b/src/lib.rs index 286c44e93..fcb6ed8c3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -299,6 +299,35 @@ pub struct DocAddress { pub doc_id: DocId, } +#[macro_export] +/// Enable fail_point if feature is enabled. +macro_rules! fail_point { + ($name:expr) => {{ + #[cfg(feature = "failpoints")] + { + fail::eval($name, |_| { + panic!("Return is not supported for the fail point \"{}\"", $name); + }); + } + }}; + ($name:expr, $e:expr) => {{ + #[cfg(feature = "failpoints")] + { + if let Some(res) = fail::eval($name, $e) { + return res; + } + } + }}; + ($name:expr, $cond:expr, $e:expr) => {{ + #[cfg(feature = "failpoints")] + { + if $cond { + fail::fail_point!($name, $e); + } + } + }}; +} + #[cfg(test)] pub mod tests { use common::{BinarySerializable, FixedSize}; diff --git a/src/postings/serializer.rs b/src/postings/serializer.rs index ddd681c05..afaedf0fd 100644 --- a/src/postings/serializer.rs +++ b/src/postings/serializer.rs @@ -2,7 +2,6 @@ use std::cmp::Ordering; use std::io::{self, Write}; use common::{BinarySerializable, CountingWriter, VInt}; -use fail::fail_point; use super::TermInfo; use crate::core::Segment; @@ -205,7 +204,7 @@ impl<'a> FieldSerializer<'a> { /// If the current block is incomplete, it needs to be encoded /// using `VInt` encoding. pub fn close_term(&mut self) -> io::Result<()> { - fail_point!("FieldSerializer::close_term", |msg: Option| { + crate::fail_point!("FieldSerializer::close_term", |msg: Option| { Err(io::Error::new(io::ErrorKind::Other, format!("{msg:?}"))) }); if self.term_open {