Skip to content

Commit

Permalink
refactor: use string type instead of Option type for '--store-key-pre…
Browse files Browse the repository at this point in the history
…fix' (#3018)

* refactor: use string type instead of Option type for '--store-key-prefix'

Signed-off-by: zyy17 <[email protected]>

* chore: refine for code review comments

---------

Signed-off-by: zyy17 <[email protected]>
  • Loading branch information
zyy17 authored Dec 27, 2023
1 parent abeb32e commit b8b1e98
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 29 deletions.
2 changes: 2 additions & 0 deletions config/metasrv.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
8 changes: 5 additions & 3 deletions src/cmd/src/metasrv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 5 additions & 2 deletions src/meta-srv/src/bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
13 changes: 7 additions & 6 deletions src/meta-srv/src/election/etcd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand All @@ -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>,
Expand Down Expand Up @@ -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)
}
}
}
Expand Down
25 changes: 9 additions & 16 deletions src/meta-srv/src/lock/etcd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]>,
Expand All @@ -44,24 +41,20 @@ 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,
}))
}

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
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/meta-srv/src/metasrv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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(),
}
}
}
Expand Down

0 comments on commit b8b1e98

Please sign in to comment.