Add backwards compat for old snapshot dirs

next
Sayan Nandan 3 years ago
parent f013a90179
commit 0b3dd9c129

@ -26,7 +26,6 @@
//! This module provides tools to handle configuration files and settings
use crate::diskstore::snapshot::DIR_SNAPSHOT;
#[cfg(test)]
use libsky::TResult;
use serde::Deserialize;
@ -36,7 +35,6 @@ use std::fs;
#[cfg(test)]
use std::net::Ipv6Addr;
use std::net::{IpAddr, Ipv4Addr};
use std::path::PathBuf;
use toml;
const DEFAULT_IPV4: IpAddr = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1));
@ -407,14 +405,10 @@ impl fmt::Display for ConfigError {
/// This parses a configuration file if it is supplied as a command line argument
/// or it returns the default configuration. **If** the configuration file
/// contains an error, then this returns it as an `Err` variant
pub fn get_config_file_or_return_cfg() -> Result<ConfigType<ParsedConfig, PathBuf>, ConfigError> {
pub fn get_config_file_or_return_cfg() -> Result<ConfigType<ParsedConfig, String>, ConfigError> {
let cfg_layout = load_yaml!("../cli.yml");
let matches = App::from_yaml(cfg_layout).get_matches();
let restorefile = matches.value_of("restore").map(|val| {
let mut path = PathBuf::from(DIR_SNAPSHOT);
path.push(val);
path
});
let restorefile = matches.value_of("restore").map(|v| v.to_string());
// Check flags
let sslonly = matches.is_present("sslonly");
let noart = matches.is_present("noart");

@ -41,7 +41,6 @@ use parking_lot::RwLock;
use parking_lot::RwLockReadGuard;
use parking_lot::RwLockWriteGuard;
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;
use tokio;
use tokio::sync::Notify;
@ -285,9 +284,9 @@ impl CoreDB {
pub fn new(
bgsave: BGSave,
snapshot_cfg: SnapshotConfig,
restore_file: Option<PathBuf>,
restore_file: Option<String>,
) -> TResult<(Self, Option<flock::FileLock>, flock::FileLock)> {
let coretable = diskstore::get_saved(restore_file)?;
let coretable = diskstore::get_snapshot(restore_file)?;
let mut background_tasks: usize = 0;
if !bgsave.is_disabled() {
background_tasks += 1;

@ -53,7 +53,6 @@ use std::fs;
use std::future::Future;
use std::io::ErrorKind;
use std::net::IpAddr;
use std::path::PathBuf;
use std::process;
use std::sync::Arc;
use tls::SslListener;
@ -315,7 +314,7 @@ pub async fn run(
bgsave_cfg: BGSave,
snapshot_cfg: SnapshotConfig,
sig: impl Future,
restore_filepath: Option<PathBuf>,
restore_filepath: Option<String>,
) -> (CoreDB, flock::FileLock) {
let (signal, _) = broadcast::channel(1);
let (terminate_tx, terminate_rx) = mpsc::channel(1);

@ -28,6 +28,7 @@
use crate::config::BGSave;
use crate::coredb::{self, Data};
use crate::diskstore::snapshot::{DIR_OLD_SNAPSHOT, DIR_SNAPSHOT};
use bincode;
use bytes::Bytes;
use libsky::TResult;
@ -53,17 +54,39 @@ lazy_static::lazy_static! {
pub static ref OLD_PATH: PathBuf = PathBuf::from("./data.bin");
}
pub fn get_snapshot(path: Option<String>) -> TResult<Option<HashMap<String, Data>>> {
if let Some(path) = path {
// the path just has the snapshot name, let's improve that
let mut snap_location = PathBuf::from(DIR_SNAPSHOT);
snap_location.push(path);
let file = match fs::read(snap_location) {
Ok(f) => f,
Err(e) => match e.kind() {
ErrorKind::NotFound => {
// Probably the old snapshot directory?
match fs::read(DIR_OLD_SNAPSHOT) {
Ok(f) => f,
_ => return Err(e.into()),
}
}
_ => return Err(e.into()),
},
};
let parsed = deserialize(file)?;
Ok(Some(parsed))
} else {
Ok(None)
}
}
/// Try to get the saved data from disk. This returns `None`, if the `data/data.bin` wasn't found
/// otherwise the `data/data.bin` file is deserialized and parsed into a `HashMap`
pub fn get_saved(location: Option<PathBuf>) -> TResult<Option<HashMap<String, Data>>> {
let file = match fs::read(
location
.map(|loc| loc.to_path_buf())
.unwrap_or(PERSIST_FILE.to_path_buf()),
) {
pub fn get_saved() -> TResult<Option<HashMap<String, Data>>> {
let file = match fs::read(&*PERSIST_FILE) {
Ok(f) => f,
Err(e) => match e.kind() {
ErrorKind::NotFound => {
// TODO(@ohsayan): Drop support for this in the future
// This might be an old installation still not using the data/data.bin path
match fs::read(OLD_PATH.to_path_buf()) {
Ok(f) => {
@ -94,6 +117,11 @@ pub fn get_saved(location: Option<PathBuf>) -> TResult<Option<HashMap<String, Da
_ => return Err(format!("Couldn't read flushed data from disk: {}", e).into()),
},
};
let parsed = deserialize(file)?;
Ok(Some(parsed))
}
fn deserialize(file: Vec<u8>) -> TResult<HashMap<String, Data>> {
let parsed: DiskStoreFromDisk = bincode::deserialize(&file)?;
let parsed: HashMap<String, Data> = HashMap::from_iter(
parsed
@ -105,7 +133,7 @@ pub fn get_saved(location: Option<PathBuf>) -> TResult<Option<HashMap<String, Da
(key, data)
}),
);
Ok(Some(parsed))
Ok(parsed)
}
/// Flush the in-memory table onto disk

@ -53,6 +53,7 @@ lazy_static::lazy_static! {
///
/// This is currently a `snapshot` directory under the current directory
pub const DIR_SNAPSHOT: &'static str = "data/snapshots";
pub const DIR_OLD_SNAPSHOT: &'static str = "snapshots";
/// The default snapshot count is 12, assuming that the user would take a snapshot
/// every 2 hours (or 7200 seconds)
const DEF_SNAPSHOT_COUNT: usize = 12;
@ -246,9 +247,7 @@ fn test_snapshot() {
let mut snapengine = SnapshotEngine::new(4, &db, Some(&ourdir)).unwrap();
let _ = snapengine.mksnap();
let current = snapengine.get_snapshots().next().unwrap();
let read_hmap = diskstore::get_saved(Some(PathBuf::from(current)))
.unwrap()
.unwrap();
let read_hmap = fs::read(PathBuf::from(current)).unwrap();
let dbhmap = db.get_hashmap_deep_clone();
assert_eq!(read_hmap, dbhmap);
snapengine.clearall().unwrap();
@ -328,6 +327,7 @@ mod queue {
//!
//! This implementation is specifically built for use with the snapshotting utility
use std::path::PathBuf;
#[cfg(test)]
use std::slice::Iter;
#[derive(Debug, PartialEq)]
pub struct Queue {
@ -368,6 +368,7 @@ mod queue {
x
}
}
#[cfg(test)]
/// Returns an iterator over the slice of strings
pub fn iter(&self) -> Iter<PathBuf> {
self.queue.iter()

Loading…
Cancel
Save