index: print a milder "Reindexing..." message on version mismatch

Closes #3323.
This commit is contained in:
Martin von Zweigbergk 2024-03-18 13:02:10 -07:00 committed by Martin von Zweigbergk
parent 965e0e8957
commit f865c1bc5d
2 changed files with 49 additions and 23 deletions

View File

@ -37,13 +37,20 @@ use crate::store::Store;
/// Error while loading index segment file. /// Error while loading index segment file.
#[derive(Debug, Error)] #[derive(Debug, Error)]
#[error("Failed to load commit index file '{name}'")] pub enum ReadonlyIndexLoadError {
pub struct ReadonlyIndexLoadError { #[error("Unexpected index version")]
/// Index file name. UnexpectedVersion {
pub name: String, found_version: u32,
/// Underlying error. expected_version: u32,
#[source] },
pub error: io::Error, #[error("Failed to load commit index file '{name}'")]
Other {
/// Index file name.
name: String,
/// Underlying error.
#[source]
error: io::Error,
},
} }
impl ReadonlyIndexLoadError { impl ReadonlyIndexLoadError {
@ -55,7 +62,7 @@ impl ReadonlyIndexLoadError {
} }
fn from_io_err(name: impl Into<String>, error: io::Error) -> Self { fn from_io_err(name: impl Into<String>, error: io::Error) -> Self {
ReadonlyIndexLoadError { ReadonlyIndexLoadError::Other {
name: name.into(), name: name.into(),
error, error,
} }
@ -63,12 +70,19 @@ impl ReadonlyIndexLoadError {
/// Returns true if the underlying error suggests data corruption. /// Returns true if the underlying error suggests data corruption.
pub(super) fn is_corrupt_or_not_found(&self) -> bool { pub(super) fn is_corrupt_or_not_found(&self) -> bool {
// If the parent file name field is corrupt, the file wouldn't be found. match self {
// And there's no need to distinguish it from an empty file. ReadonlyIndexLoadError::UnexpectedVersion { .. } => true,
matches!( ReadonlyIndexLoadError::Other { name: _, error } => {
self.error.kind(), // If the parent file name field is corrupt, the file wouldn't be found.
io::ErrorKind::NotFound | io::ErrorKind::InvalidData | io::ErrorKind::UnexpectedEof // And there's no need to distinguish it from an empty file.
) matches!(
error.kind(),
io::ErrorKind::NotFound
| io::ErrorKind::InvalidData
| io::ErrorKind::UnexpectedEof
)
}
}
} }
} }
@ -244,10 +258,10 @@ impl ReadonlyIndexSegment {
}; };
let format_version = read_u32(file)?; let format_version = read_u32(file)?;
if format_version != INDEX_SEGMENT_FILE_FORMAT_VERSION { if format_version != INDEX_SEGMENT_FILE_FORMAT_VERSION {
return Err(ReadonlyIndexLoadError::invalid_data( return Err(ReadonlyIndexLoadError::UnexpectedVersion {
&name, found_version: format_version,
format!("unsupported file format version: {format_version}"), expected_version: INDEX_SEGMENT_FILE_FORMAT_VERSION,
)); });
} }
let parent_filename_len = read_u32(file)?; let parent_filename_len = read_u32(file)?;
let maybe_parent_file = if parent_filename_len > 0 { let maybe_parent_file = if parent_filename_len > 0 {

View File

@ -330,11 +330,23 @@ impl IndexStore for DefaultIndexStore {
Err(DefaultIndexStoreError::LoadIndex(err)) if err.is_corrupt_or_not_found() => { Err(DefaultIndexStoreError::LoadIndex(err)) if err.is_corrupt_or_not_found() => {
// If the index was corrupt (maybe it was written in a different format), // If the index was corrupt (maybe it was written in a different format),
// we just reindex. // we just reindex.
// TODO: Move this message to a callback or something. match &err {
eprintln!( ReadonlyIndexLoadError::UnexpectedVersion {
"{err} (maybe the format has changed): {source}. Reindexing...", found_version,
source = err.error expected_version,
); } => {
eprintln!(
"Found index format version {found_version}, expected version \
{expected_version}. Reindexing..."
);
}
ReadonlyIndexLoadError::Other { name: _, error } => {
eprintln!(
"{err} (maybe the format has changed): {source}. Reindexing...",
source = error
);
}
}
self.reinit().map_err(|err| IndexReadError(err.into()))?; self.reinit().map_err(|err| IndexReadError(err.into()))?;
self.build_index_segments_at_operation(op, store) self.build_index_segments_at_operation(op, store)
} }