fix: ignore NotReady error for IndexServer (#559)

dedup-snippet-at-index
Meng Zhang 2023-10-14 02:21:17 -07:00 committed by GitHub
parent b3c119d6f3
commit d27c09d75d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 34 additions and 9 deletions

9
Cargo.lock generated
View File

@ -3127,6 +3127,7 @@ dependencies = [
"tabby-scheduler",
"tantivy",
"textdistance",
"thiserror",
"tokio",
"tower",
"tower-http 0.4.0",
@ -3400,18 +3401,18 @@ checksum = "d321c8576c2b47e43953e9cce236550d4cd6af0a6ce518fe084340082ca6037b"
[[package]]
name = "thiserror"
version = "1.0.45"
version = "1.0.49"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dedd246497092a89beedfe2c9f176d44c1b672ea6090edc20544ade01fbb7ea0"
checksum = "1177e8c6d7ede7afde3585fd2513e611227efd6481bd78d2e82ba1ce16557ed4"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "1.0.45"
version = "1.0.49"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7d7b1fadccbbc7e19ea64708629f9d8dccd007c260d66485f20a6d41bc1cf4b3"
checksum = "10712f02019e9288794769fba95cd6847df9874d49d871d062172f9dd41bc4cc"
dependencies = [
"proc-macro2",
"quote",

View File

@ -37,3 +37,4 @@ tokenizers = "0.13.4-rc3"
futures = "0.3.28"
async-stream = "0.3.5"
regex = "1.10.0"
thiserror = "1.0.49"

View File

@ -42,6 +42,7 @@ axum-streams = { version = "0.9.1", features = ["json"] }
minijinja = { version = "1.0.8", features = ["loader"] }
textdistance = "1.0.2"
regex.workspace = true
thiserror.workspace = true
[target.'cfg(all(target_os="macos", target_arch="aarch64"))'.dependencies]
llama-cpp-bindings = { path = "../llama-cpp-bindings" }

View File

@ -7,7 +7,10 @@ use textdistance::Algorithm;
use tracing::warn;
use super::{Segments, Snippet};
use crate::serve::{completions::languages::get_language, search::IndexServer};
use crate::serve::{
completions::languages::get_language,
search::{IndexServer, IndexServerError},
};
static MAX_SNIPPETS_TO_FETCH: usize = 20;
static MAX_SNIPPET_CHARS_IN_PROMPT: usize = 768;
@ -109,7 +112,11 @@ fn collect_snippets(index_server: &IndexServer, language: &str, text: &str) -> V
let serp = match index_server.search(&query_text, MAX_SNIPPETS_TO_FETCH, 0) {
Ok(serp) => serp,
Err(err) => {
Err(IndexServerError::NotReady) => {
// Ignore.
return vec![];
}
Err(IndexServerError::TantivyError(err)) => {
warn!("Failed to search query: {}", err);
return ret;
}

View File

@ -14,6 +14,7 @@ use tantivy::{
schema::Field,
DocAddress, Document, Index, IndexReader,
};
use thiserror::Error;
use tokio::{sync::OnceCell, task, time::sleep};
use tracing::{debug, instrument, log::info};
use utoipa::{IntoParams, ToSchema};
@ -198,11 +199,25 @@ impl IndexServer {
}
}
pub fn search(&self, q: &str, limit: usize, offset: usize) -> tantivy::Result<SearchResponse> {
pub fn search(
&self,
q: &str,
limit: usize,
offset: usize,
) -> Result<SearchResponse, IndexServerError> {
if let Some(imp) = self.get_cell() {
imp.search(q, limit, offset)
Ok(imp.search(q, limit, offset)?)
} else {
Err(tantivy::TantivyError::InternalError("Not Ready".to_owned()))
Err(IndexServerError::NotReady)
}
}
}
#[derive(Error, Debug)]
pub enum IndexServerError {
#[error("index not ready")]
NotReady,
#[error("underlying tantivy error")]
TantivyError(#[from] tantivy::TantivyError),
}