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 <zyylsxm@gmail.com>

* chore: refine for code review comments

---------

Signed-off-by: zyy17 <zyylsxm@gmail.com>
This commit is contained in:
zyy17
2023-12-27 19:26:30 +08:00
committed by GitHub
parent abeb32e042
commit b8b1e98399
6 changed files with 30 additions and 29 deletions

View File

@@ -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]

View File

@@ -119,8 +119,8 @@ struct StartCommand {
data_home: Option<String>,
/// If it's not empty, the metasrv will store all data with this key prefix.
#[clap(long)]
store_key_prefix: Option<String>,
#[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;

View File

@@ -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
}

View File

@@ -35,14 +35,14 @@ pub struct EtcdElection {
is_leader: AtomicBool,
infancy: AtomicBool,
leader_watcher: broadcast::Sender<LeaderChangeMessage>,
store_key_prefix: Option<String>,
store_key_prefix: String,
}
impl EtcdElection {
pub async fn with_endpoints<E, S>(
leader_value: E,
endpoints: S,
store_key_prefix: Option<String>,
store_key_prefix: String,
) -> Result<ElectionRef>
where
E: AsRef<str>,
@@ -58,7 +58,7 @@ impl EtcdElection {
pub async fn with_etcd_client<E>(
leader_value: E,
client: Client,
store_key_prefix: Option<String>,
store_key_prefix: String,
) -> Result<ElectionRef>
where
E: AsRef<str>,
@@ -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)
}
}
}

View File

@@ -25,14 +25,11 @@ use crate::error::Result;
#[derive(Clone)]
pub struct EtcdLock {
client: Client,
store_key_prefix: Option<String>,
store_key_prefix: String,
}
impl EtcdLock {
pub async fn with_endpoints<E, S>(
endpoints: S,
store_key_prefix: Option<String>,
) -> Result<DistLockRef>
pub async fn with_endpoints<E, S>(endpoints: S, store_key_prefix: String) -> Result<DistLockRef>
where
E: AsRef<str>,
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<String>,
) -> Result<DistLockRef> {
pub fn with_etcd_client(client: Client, store_key_prefix: String) -> Result<DistLockRef> {
Ok(Arc::new(EtcdLock {
client,
store_key_prefix,
@@ -55,13 +49,12 @@ impl EtcdLock {
}
fn lock_key(&self, key: Vec<u8>) -> Vec<u8> {
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
}
}
}

View File

@@ -75,7 +75,7 @@ pub struct MetaSrvOptions {
pub data_home: String,
pub wal: WalConfig,
pub export_metrics: ExportMetricsOption,
pub store_key_prefix: Option<String>,
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(),
}
}
}