From 9ef7883590cbbccaa400ee58da57bf7ada7bbf59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=82=8E=E6=B3=BC?= Date: Thu, 7 Nov 2024 12:31:22 +0800 Subject: [PATCH] refactor: OnDisk ensures dir --- src/meta/binaries/metactl/main.rs | 5 +++++ src/meta/raft-store/src/ondisk/mod.rs | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/meta/binaries/metactl/main.rs b/src/meta/binaries/metactl/main.rs index f71704e101bb..be0d0b60f73c 100644 --- a/src/meta/binaries/metactl/main.rs +++ b/src/meta/binaries/metactl/main.rs @@ -34,6 +34,7 @@ use databend_common_base::base::tokio; use databend_common_meta_client::MetaGrpcClient; use databend_common_meta_kvapi::kvapi::KVApi; use databend_common_meta_raft_store::config::RaftConfig; +use databend_common_meta_raft_store::ondisk::OnDisk; use databend_common_meta_sled_store::init_sled_db; use databend_common_tracing::init_logging; use databend_common_tracing::Config as LogConfig; @@ -362,6 +363,10 @@ async fn main() -> anyhow::Result<()> { }; let _guards = init_logging("metactl", &log_config, BTreeMap::new()); + if let Some(raft_dir) = &app.globals.raft_dir { + OnDisk::ensure_dirs(raft_dir)?; + } + match app.command { Some(ref cmd) => match cmd { CtlCommand::Status(args) => { diff --git a/src/meta/raft-store/src/ondisk/mod.rs b/src/meta/raft-store/src/ondisk/mod.rs index e8fcf6915dae..f18ef3d70cc7 100644 --- a/src/meta/raft-store/src/ondisk/mod.rs +++ b/src/meta/raft-store/src/ondisk/mod.rs @@ -74,6 +74,26 @@ impl fmt::Display for OnDisk { impl OnDisk { pub const KEY_HEADER: &'static str = "header"; + pub fn ensure_dirs(raft_dir: &str) -> Result<(), io::Error> { + let raft_dir = Path::new(raft_dir); + let version_dir = raft_dir.join("df_meta").join(format!("{}", DATA_VERSION)); + + let log_dir = version_dir.join("log"); + + if !log_dir.exists() { + fs::create_dir_all(&log_dir) + .context(format!("creating dir {}", log_dir.as_path().display()))?; + } + + let snapshot_dir = version_dir.join("snapshot"); + if !snapshot_dir.exists() { + fs::create_dir_all(&snapshot_dir) + .context(format!("creating dir {}", snapshot_dir.as_path().display()))?; + } + + Ok(()) + } + /// Initialize data version for local store, returns the loaded version. #[fastrace::trace] pub async fn open(db: &sled::Db, config: &RaftConfig) -> Result {