From a6893aad421fcc242a67b859a7aa21624bbe8a17 Mon Sep 17 00:00:00 2001 From: jeremyhi Date: Wed, 11 Dec 2024 16:04:02 +0800 Subject: [PATCH] chore: set store_key_prefix for all kvbackend (#5132) --- src/meta-srv/src/bootstrap.rs | 44 +++++++++++++++++------------------ src/meta-srv/src/metasrv.rs | 4 ++++ 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/src/meta-srv/src/bootstrap.rs b/src/meta-srv/src/bootstrap.rs index 47afa0ab416b..85770e1f3d4d 100644 --- a/src/meta-srv/src/bootstrap.rs +++ b/src/meta-srv/src/bootstrap.rs @@ -206,43 +206,41 @@ pub async fn metasrv_builder( plugins: Plugins, kv_backend: Option, ) -> Result { - let (kv_backend, election) = match (kv_backend, &opts.backend) { + let (mut kv_backend, election) = match (kv_backend, &opts.backend) { (Some(kv_backend), _) => (kv_backend, None), (None, BackendImpl::MemoryStore) => (Arc::new(MemoryKvBackend::new()) as _, None), (None, BackendImpl::EtcdStore) => { let etcd_client = create_etcd_client(opts).await?; - let kv_backend = { - let etcd_backend = - EtcdStore::with_etcd_client(etcd_client.clone(), opts.max_txn_ops); - if !opts.store_key_prefix.is_empty() { - Arc::new(ChrootKvBackend::new( - opts.store_key_prefix.clone().into_bytes(), - etcd_backend, - )) - } else { - etcd_backend - } - }; - ( - kv_backend, - Some( - EtcdElection::with_etcd_client( - &opts.server_addr, - etcd_client.clone(), - opts.store_key_prefix.clone(), - ) - .await?, - ), + let kv_backend = EtcdStore::with_etcd_client(etcd_client.clone(), opts.max_txn_ops); + let election = EtcdElection::with_etcd_client( + &opts.server_addr, + etcd_client, + opts.store_key_prefix.clone(), ) + .await?; + + (kv_backend, Some(election)) } #[cfg(feature = "pg_kvbackend")] (None, BackendImpl::PostgresStore) => { let pg_client = create_postgres_client(opts).await?; let kv_backend = PgStore::with_pg_client(pg_client).await.unwrap(); + // TODO(jeremy, weny): implement election for postgres (kv_backend, None) } }; + if !opts.store_key_prefix.is_empty() { + info!( + "using chroot kv backend with prefix: {prefix}", + prefix = opts.store_key_prefix + ); + kv_backend = Arc::new(ChrootKvBackend::new( + opts.store_key_prefix.clone().into_bytes(), + kv_backend, + )) + } + let in_memory = Arc::new(MemoryKvBackend::new()) as ResettableKvBackendRef; let selector = match opts.selector { diff --git a/src/meta-srv/src/metasrv.rs b/src/meta-srv/src/metasrv.rs index 716b85f83485..da614ac9b943 100644 --- a/src/meta-srv/src/metasrv.rs +++ b/src/meta-srv/src/metasrv.rs @@ -470,6 +470,10 @@ impl Metasrv { }); } } else { + warn!( + "Ensure only one instance of Metasrv is running, as there is no election service." + ); + if let Err(e) = self.wal_options_allocator.start().await { error!(e; "Failed to start wal options allocator"); }