From 0b17f7b0e4f2cfbd7fc7f804288cdb8913c10f7b Mon Sep 17 00:00:00 2001 From: Sayan Nandan Date: Sun, 27 Mar 2022 22:43:54 -0700 Subject: [PATCH] Fix new instance detection --- CHANGELOG.md | 1 + server/src/storage/v1/unflush.rs | 18 ++++++++++++------ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 17b8cfa8..a8087d45 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ All changes in this project will be noted in this file. - (skysh) Fixed handling of EOF keystroke - (skyd) Respect logging level for all terminal output - (skyd) Fixed LF check in protocol impl +- (skyd) Fixed new instance detection (now checks if data directory is empty or not) ## Version 0.7.4 diff --git a/server/src/storage/v1/unflush.rs b/server/src/storage/v1/unflush.rs index 5af04a4c..fa7b0be1 100644 --- a/server/src/storage/v1/unflush.rs +++ b/server/src/storage/v1/unflush.rs @@ -45,7 +45,7 @@ use crate::{ util::Wrapper, }; use core::mem::transmute; -use std::{fs, path::Path, sync::Arc}; +use std::{fs, io::ErrorKind, path::Path, sync::Arc}; type PreloadSet = std::collections::HashSet; const PRELOAD_PATH: &str = "data/ks/PRELOAD"; @@ -217,7 +217,7 @@ pub fn read_preload() -> StorageEngineResult { /// is also created. If this is an already initialized instance then the store /// is read and returned (and any possible errors that are encountered are returned) pub fn read_full() -> StorageEngineResult { - if is_new_instance() { + if is_new_instance()? { log::trace!("Detected new instance. Creating data directory"); /* Since the `PRELOAD` file doesn't exist -- this is a new instance @@ -251,8 +251,14 @@ pub fn read_full() -> StorageEngineResult { Ok(Memstore::init_with_all(ksmap, system_keyspace)) } -/// Check if the data/ks/PRELOAD file exists (if not: we're on a new instance) -pub fn is_new_instance() -> bool { - let path = Path::new("data/ks/PRELOAD"); - !(path.exists() && path.is_file()) +/// Check if the `data` directory is non-empty (if not: we're on a new instance) +pub fn is_new_instance() -> StorageEngineResult { + match fs::read_dir("data") { + Ok(mut dir) => Ok(dir.next().is_none()), + Err(e) if e.kind().eq(&ErrorKind::NotFound) => Ok(true), + Err(e) => Err(StorageEngineError::ioerror_extra( + e, + "while checking data directory", + )), + } }