From 867f1349ed0143308edbe61e8ab90bad6d171336 Mon Sep 17 00:00:00 2001 From: CookiePieWw Date: Mon, 6 Jan 2025 14:06:51 +0800 Subject: [PATCH] refactor: use unified global var --- src/cli/src/bench.rs | 4 ++-- src/common/meta/src/kv_backend/postgres.rs | 8 +++++--- src/meta-srv/src/election/postgres.rs | 20 ++++++++++++-------- src/meta-srv/src/metasrv.rs | 6 +++--- 4 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/cli/src/bench.rs b/src/cli/src/bench.rs index ce933688d678..27ffeab2bb8a 100644 --- a/src/cli/src/bench.rs +++ b/src/cli/src/bench.rs @@ -24,7 +24,7 @@ use common_meta::key::{TableMetadataManager, TableMetadataManagerRef}; use common_meta::kv_backend::etcd::EtcdStore; use common_meta::kv_backend::memory::MemoryKvBackend; #[cfg(feature = "pg_kvbackend")] -use common_meta::kv_backend::postgres::PgStore; +use common_meta::kv_backend::postgres::{PgStore, META_TABLE_NAME}; use common_meta::peer::Peer; use common_meta::rpc::router::{Region, RegionRoute}; use common_telemetry::info; @@ -78,7 +78,7 @@ impl BenchTableMetadataCommand { #[cfg(feature = "pg_kvbackend")] let kv_backend = if let Some(postgres_addr) = &self.postgres_addr { info!("Using postgres as kv backend"); - PgStore::with_url(postgres_addr, 128, "greptime_metakv".to_string()) + PgStore::with_url(postgres_addr, 128, META_TABLE_NAME.to_string()) .await .unwrap() } else { diff --git a/src/common/meta/src/kv_backend/postgres.rs b/src/common/meta/src/kv_backend/postgres.rs index 83f118d8642f..3eb80c076e06 100644 --- a/src/common/meta/src/kv_backend/postgres.rs +++ b/src/common/meta/src/kv_backend/postgres.rs @@ -37,6 +37,8 @@ use crate::rpc::store::{ }; use crate::rpc::KeyValue; +pub const META_TABLE_NAME: &str = "greptime_metakv"; + type PgClient = deadpool::managed::Object; enum PgQueryExecutor<'a> { @@ -982,7 +984,7 @@ mod tests { let client = pool.get().await.unwrap(); client .execute( - &replace_table_name(METADKV_CREATION, "greptime_metakv"), + &replace_table_name(METADKV_CREATION, META_TABLE_NAME), &[], ) .await @@ -991,7 +993,7 @@ mod tests { Some(PgStore { pool, max_txn_ops: 128, - table_name: "greptime_metakv".to_string(), + table_name: META_TABLE_NAME.to_string(), }) } @@ -1046,7 +1048,7 @@ mod tests { .get_client() .await .unwrap() - .execute("DELETE FROM greptime_metakv", &[]) + .execute(&replace_table_name("DELETE FROM {}", META_TABLE_NAME), &[]) .await .unwrap(); } diff --git a/src/meta-srv/src/election/postgres.rs b/src/meta-srv/src/election/postgres.rs index 7556a51e9293..923e7760f3e9 100644 --- a/src/meta-srv/src/election/postgres.rs +++ b/src/meta-srv/src/election/postgres.rs @@ -647,6 +647,7 @@ impl PgElection { mod tests { use std::env; + use common_meta::kv_backend::postgres::META_TABLE_NAME; use tokio_postgres::{Client, NoTls}; use super::*; @@ -668,7 +669,10 @@ mod tests { }); let _ = client .execute( - "CREATE TABLE IF NOT EXISTS greptime_metakv(k varchar PRIMARY KEY, v varchar)", + &replace_table_name( + "CREATE TABLE IF NOT EXISTS {}(k varchar PRIMARY KEY, v varchar)", + META_TABLE_NAME, + ), &[], ) .await @@ -692,7 +696,7 @@ mod tests { leader_watcher: tx, store_key_prefix: uuid::Uuid::new_v4().to_string(), candidate_lease_ttl_secs: 10, - table_name: "greptime_metakv".to_string(), + table_name: META_TABLE_NAME.to_string(), lock_id: 28319, }; @@ -767,7 +771,7 @@ mod tests { leader_watcher: tx, store_key_prefix, candidate_lease_ttl_secs, - table_name: "greptime_metakv".to_string(), + table_name: META_TABLE_NAME.to_string(), lock_id: 28319, }; @@ -810,7 +814,7 @@ mod tests { leader_watcher: tx, store_key_prefix: store_key_prefix.clone(), candidate_lease_ttl_secs, - table_name: "greptime_metakv".to_string(), + table_name: META_TABLE_NAME.to_string(), lock_id: 28319, }; @@ -852,7 +856,7 @@ mod tests { leader_watcher: tx, store_key_prefix: uuid::Uuid::new_v4().to_string(), candidate_lease_ttl_secs, - table_name: "greptime_metakv".to_string(), + table_name: META_TABLE_NAME.to_string(), lock_id: 28320, }; @@ -962,7 +966,7 @@ mod tests { leader_watcher: tx, store_key_prefix, candidate_lease_ttl_secs, - table_name: "greptime_metakv".to_string(), + table_name: META_TABLE_NAME.to_string(), lock_id: 28321, }; @@ -1193,7 +1197,7 @@ mod tests { leader_watcher: tx, store_key_prefix: store_key_prefix.clone(), candidate_lease_ttl_secs, - table_name: "greptime_metakv".to_string(), + table_name: META_TABLE_NAME.to_string(), lock_id: 28322, }; @@ -1207,7 +1211,7 @@ mod tests { leader_watcher: tx, store_key_prefix, candidate_lease_ttl_secs, - table_name: "greptime_metakv".to_string(), + table_name: META_TABLE_NAME.to_string(), lock_id: 28322, }; diff --git a/src/meta-srv/src/metasrv.rs b/src/meta-srv/src/metasrv.rs index ca04c1f5277c..c19618ae8d04 100644 --- a/src/meta-srv/src/metasrv.rs +++ b/src/meta-srv/src/metasrv.rs @@ -28,6 +28,8 @@ use common_meta::cache_invalidator::CacheInvalidatorRef; use common_meta::ddl::ProcedureExecutorRef; use common_meta::key::maintenance::MaintenanceModeManagerRef; use common_meta::key::TableMetadataManagerRef; +#[cfg(feature = "pg_kvbackend")] +use common_meta::kv_backend::postgres::META_TABLE_NAME; use common_meta::kv_backend::{KvBackendRef, ResettableKvBackend, ResettableKvBackendRef}; use common_meta::leadership_notifier::{ LeadershipChangeNotifier, LeadershipChangeNotifierCustomizerRef, @@ -70,8 +72,6 @@ use crate::state::{become_follower, become_leader, StateRef}; pub const TABLE_ID_SEQ: &str = "table_id"; pub const FLOW_ID_SEQ: &str = "flow_id"; pub const METASRV_HOME: &str = "/tmp/metasrv"; -#[cfg(feature = "pg_kvbackend")] -pub const PG_TABLE_NAME: &str = "greptime_metakv"; // The datastores that implements metadata kvbackend. #[derive(Clone, Debug, PartialEq, Serialize, Default, Deserialize, ValueEnum)] @@ -179,7 +179,7 @@ impl Default for MetasrvOptions { tracing: TracingOptions::default(), backend: BackendImpl::EtcdStore, #[cfg(feature = "pg_kvbackend")] - meta_table_name: PG_TABLE_NAME.to_string(), + meta_table_name: META_TABLE_NAME.to_string(), } } }