From b8b1e98399614de4ab8d23c886261c01b24739ea Mon Sep 17 00:00:00 2001 From: zyy17 Date: Wed, 27 Dec 2023 19:26:30 +0800 Subject: [PATCH] refactor: use string type instead of Option type for '--store-key-prefix' (#3018) * refactor: use string type instead of Option type for '--store-key-prefix' Signed-off-by: zyy17 * chore: refine for code review comments --------- Signed-off-by: zyy17 --- config/metasrv.example.toml | 2 ++ src/cmd/src/metasrv.rs | 8 +++++--- src/meta-srv/src/bootstrap.rs | 7 +++++-- src/meta-srv/src/election/etcd.rs | 13 +++++++------ src/meta-srv/src/lock/etcd.rs | 25 +++++++++---------------- src/meta-srv/src/metasrv.rs | 4 ++-- 6 files changed, 30 insertions(+), 29 deletions(-) diff --git a/config/metasrv.example.toml b/config/metasrv.example.toml index ff05a9c095e8..8a55bee7913f 100644 --- a/config/metasrv.example.toml +++ b/config/metasrv.example.toml @@ -15,6 +15,8 @@ selector = "lease_based" use_memory_store = false # Whether to enable greptimedb telemetry, true by default. enable_telemetry = true +# If it's not empty, the metasrv will store all data with this key prefix. +store_key_prefix = "" # Log options, see `standalone.example.toml` # [logging] diff --git a/src/cmd/src/metasrv.rs b/src/cmd/src/metasrv.rs index 6653d2431c13..9ef9608f2497 100644 --- a/src/cmd/src/metasrv.rs +++ b/src/cmd/src/metasrv.rs @@ -119,8 +119,8 @@ struct StartCommand { data_home: Option, /// If it's not empty, the metasrv will store all data with this key prefix. - #[clap(long)] - store_key_prefix: Option, + #[clap(long, default_value = "")] + store_key_prefix: String, } impl StartCommand { @@ -177,7 +177,9 @@ impl StartCommand { opts.data_home = data_home.clone(); } - opts.store_key_prefix = self.store_key_prefix.clone(); + if !self.store_key_prefix.is_empty() { + opts.store_key_prefix = self.store_key_prefix.clone() + } // Disable dashboard in metasrv. opts.http.disable_dashboard = true; diff --git a/src/meta-srv/src/bootstrap.rs b/src/meta-srv/src/bootstrap.rs index 196ded82b9b5..eb937a7444be 100644 --- a/src/meta-srv/src/bootstrap.rs +++ b/src/meta-srv/src/bootstrap.rs @@ -192,8 +192,11 @@ pub async fn metasrv_builder( let etcd_client = create_etcd_client(opts).await?; let kv_backend = { let etcd_backend = EtcdStore::with_etcd_client(etcd_client.clone()); - if let Some(prefix) = opts.store_key_prefix.clone() { - Arc::new(ChrootKvBackend::new(prefix.into_bytes(), etcd_backend)) + if !opts.store_key_prefix.is_empty() { + Arc::new(ChrootKvBackend::new( + opts.store_key_prefix.clone().into_bytes(), + etcd_backend, + )) } else { etcd_backend } diff --git a/src/meta-srv/src/election/etcd.rs b/src/meta-srv/src/election/etcd.rs index 992be6d61a53..80d3b51152fd 100644 --- a/src/meta-srv/src/election/etcd.rs +++ b/src/meta-srv/src/election/etcd.rs @@ -35,14 +35,14 @@ pub struct EtcdElection { is_leader: AtomicBool, infancy: AtomicBool, leader_watcher: broadcast::Sender, - store_key_prefix: Option, + store_key_prefix: String, } impl EtcdElection { pub async fn with_endpoints( leader_value: E, endpoints: S, - store_key_prefix: Option, + store_key_prefix: String, ) -> Result where E: AsRef, @@ -58,7 +58,7 @@ impl EtcdElection { pub async fn with_etcd_client( leader_value: E, client: Client, - store_key_prefix: Option, + store_key_prefix: String, ) -> Result where E: AsRef, @@ -105,9 +105,10 @@ impl EtcdElection { } fn election_key(&self) -> String { - match &self.store_key_prefix { - Some(prefix) => format!("{}{}", prefix, ELECTION_KEY), - None => ELECTION_KEY.to_string(), + if self.store_key_prefix.is_empty() { + ELECTION_KEY.to_string() + } else { + format!("{}{}", self.store_key_prefix, ELECTION_KEY) } } } diff --git a/src/meta-srv/src/lock/etcd.rs b/src/meta-srv/src/lock/etcd.rs index 14f1e9c615ff..3f53b40e15cd 100644 --- a/src/meta-srv/src/lock/etcd.rs +++ b/src/meta-srv/src/lock/etcd.rs @@ -25,14 +25,11 @@ use crate::error::Result; #[derive(Clone)] pub struct EtcdLock { client: Client, - store_key_prefix: Option, + store_key_prefix: String, } impl EtcdLock { - pub async fn with_endpoints( - endpoints: S, - store_key_prefix: Option, - ) -> Result + pub async fn with_endpoints(endpoints: S, store_key_prefix: String) -> Result where E: AsRef, S: AsRef<[E]>, @@ -44,10 +41,7 @@ impl EtcdLock { Self::with_etcd_client(client, store_key_prefix) } - pub fn with_etcd_client( - client: Client, - store_key_prefix: Option, - ) -> Result { + pub fn with_etcd_client(client: Client, store_key_prefix: String) -> Result { Ok(Arc::new(EtcdLock { client, store_key_prefix, @@ -55,13 +49,12 @@ impl EtcdLock { } fn lock_key(&self, key: Vec) -> Vec { - match &self.store_key_prefix { - Some(prefix) => { - let mut prefix = prefix.as_bytes().to_vec(); - prefix.extend_from_slice(&key); - prefix - } - None => key, + if self.store_key_prefix.is_empty() { + key + } else { + let mut prefix = self.store_key_prefix.as_bytes().to_vec(); + prefix.extend_from_slice(&key); + prefix } } } diff --git a/src/meta-srv/src/metasrv.rs b/src/meta-srv/src/metasrv.rs index 613d282449c9..4d6782a2b630 100644 --- a/src/meta-srv/src/metasrv.rs +++ b/src/meta-srv/src/metasrv.rs @@ -75,7 +75,7 @@ pub struct MetaSrvOptions { pub data_home: String, pub wal: WalConfig, pub export_metrics: ExportMetricsOption, - pub store_key_prefix: Option, + pub store_key_prefix: String, } impl Default for MetaSrvOptions { @@ -102,7 +102,7 @@ impl Default for MetaSrvOptions { data_home: METASRV_HOME.to_string(), wal: WalConfig::default(), export_metrics: ExportMetricsOption::default(), - store_key_prefix: None, + store_key_prefix: String::new(), } } }