From c284c04441b17dd6e2f0a1f7cdfcf0a88b0b4d2c Mon Sep 17 00:00:00 2001 From: WenyXu Date: Thu, 12 Nov 2026 13:15:32 +0000 Subject: [PATCH] feat(common-meta): make RDS metadata backends closable Standalone closes the metadata backend on shutdown, and `MetadataKvBackendCreator` now returns a `ClosableKvBackendRef`. Only the raft-engine backend implemented `ClosableKvBackend`, so external metadata backends could not be wired into the standalone creator. Implement `ClosableKvBackend` for `RdsStore` and return `ClosableKvBackendRef` from `with_mysql_pool`/`with_pg_pool` and the metasrv RDS builders. The stores only hold reference-counted connection pools that are released with the last backend handle, so `close()` has nothing to release explicitly. Signed-off-by: WenyXu --- src/cli/src/common/store.rs | 29 ++++++++++--------- src/common/meta/src/kv_backend/rds.rs | 18 +++++++++++- src/common/meta/src/kv_backend/rds/mysql.rs | 9 +++--- .../meta/src/kv_backend/rds/postgres.rs | 10 ++++--- src/meta-srv/src/bootstrap.rs | 4 +-- src/meta-srv/src/utils/mysql.rs | 6 ++-- src/meta-srv/src/utils/postgres.rs | 6 ++-- 7 files changed, 52 insertions(+), 30 deletions(-) diff --git a/src/cli/src/common/store.rs b/src/cli/src/common/store.rs index c3b8e477ed1..25a247309e6 100644 --- a/src/cli/src/common/store.rs +++ b/src/cli/src/common/store.rs @@ -167,30 +167,33 @@ impl StoreConfig { BackendImpl::PostgresStore => { let table_name = &self.meta_table_name; let tls_config = self.tls_config(); - Ok(meta_srv::utils::postgres::build_postgres_kv_backend( - store_addrs, - None, - tls_config, - self.meta_schema_name.as_deref(), - table_name, - max_txn_ops, - self.auto_create_schema, - ) - .await - .map_err(BoxedError::new)?) + let backend: KvBackendRef = + meta_srv::utils::postgres::build_postgres_kv_backend( + store_addrs, + None, + tls_config, + self.meta_schema_name.as_deref(), + table_name, + max_txn_ops, + self.auto_create_schema, + ) + .await + .map_err(BoxedError::new)?; + Ok(backend) } #[cfg(feature = "mysql_kvbackend")] BackendImpl::MysqlStore => { let table_name = &self.meta_table_name; let tls_config = self.tls_config(); - Ok(meta_srv::utils::mysql::build_mysql_kv_backend( + let backend: KvBackendRef = meta_srv::utils::mysql::build_mysql_kv_backend( store_addrs, tls_config.as_ref(), table_name, max_txn_ops, ) .await - .map_err(BoxedError::new)?) + .map_err(BoxedError::new)?; + Ok(backend) } #[cfg(not(test))] BackendImpl::MemoryStore => { diff --git a/src/common/meta/src/kv_backend/rds.rs b/src/common/meta/src/kv_backend/rds.rs index 1e94741dd8e..bf65f92dd68 100644 --- a/src/common/meta/src/kv_backend/rds.rs +++ b/src/common/meta/src/kv_backend/rds.rs @@ -26,7 +26,7 @@ use crate::error::{Error, RdsTransactionRetryFailedSnafu, Result, UnexpectedSnaf use crate::kv_backend::txn::{ Compare, Txn as KvTxn, TxnOp, TxnOpResponse, TxnResponse as KvTxnResponse, }; -use crate::kv_backend::{KvBackend, TxnService}; +use crate::kv_backend::{ClosableKvBackend, KvBackend, TxnService}; use crate::metrics::METRIC_META_TXN_REQUEST; use crate::rpc::KeyValue; use crate::rpc::store::{ @@ -585,6 +585,22 @@ where } } +#[async_trait::async_trait] +impl ClosableKvBackend for RdsStore +where + R: 'static, + Self: KvQueryExecutor + Send + Sync, + T: Executor + 'static, + S: ExecutorFactory + 'static, +{ + async fn close(&self) -> Result<()> { + // The store only holds a reference-counted connection pool, which is + // released together with the last backend handle. There is no explicit + // shutdown step for the pool itself. + Ok(()) + } +} + /// Checks if the transaction operations are the same type. fn check_txn_ops(txn_ops: &[TxnOp]) -> Result { if txn_ops.is_empty() { diff --git a/src/common/meta/src/kv_backend/rds/mysql.rs b/src/common/meta/src/kv_backend/rds/mysql.rs index c933bb345f6..ebd0859b94b 100644 --- a/src/common/meta/src/kv_backend/rds/mysql.rs +++ b/src/common/meta/src/kv_backend/rds/mysql.rs @@ -27,13 +27,13 @@ use strum::AsRefStr; use crate::error::{ CreateMySqlPoolSnafu, MySqlExecutionSnafu, MySqlTransactionSnafu, Result, UnexpectedSnafu, }; -use crate::kv_backend::KvBackendRef; use crate::kv_backend::rds::{ Executor, ExecutorFactory, ExecutorImpl, KvQueryExecutor, RDS_STORE_OP_BATCH_DELETE, RDS_STORE_OP_BATCH_GET, RDS_STORE_OP_BATCH_PUT, RDS_STORE_OP_RANGE_DELETE, RDS_STORE_OP_RANGE_QUERY, RDS_STORE_TXN_RETRY_COUNT, RdsStore, Transaction, ensure_rustls_crypto_provider_installed, }; +use crate::kv_backend::{ClosableKvBackendRef, KvBackendRef}; use crate::rpc::KeyValue; use crate::rpc::store::{ BatchDeleteRequest, BatchDeleteResponse, BatchGetRequest, BatchGetResponse, BatchPutRequest, @@ -637,15 +637,16 @@ impl MySqlStore { let pool = MySqlPool::connect(url) .await .context(CreateMySqlPoolSnafu)?; - Self::with_mysql_pool(pool, table_name, max_txn_ops).await + let backend: KvBackendRef = Self::with_mysql_pool(pool, table_name, max_txn_ops).await?; + Ok(backend) } - /// Create [MySqlStore] impl of [KvBackendRef] from [sqlx::Pool]. + /// Create [MySqlStore] impl of [ClosableKvBackendRef] from [sqlx::Pool]. pub async fn with_mysql_pool( pool: Pool, table_name: &str, max_txn_ops: usize, - ) -> Result { + ) -> Result { // This step ensures the mysql metadata backend is ready to use. // We check if greptime_metakv table exists, and we will create a new table // if it does not exist. diff --git a/src/common/meta/src/kv_backend/rds/postgres.rs b/src/common/meta/src/kv_backend/rds/postgres.rs index 4a2f8b13564..5ff99d7fe1a 100644 --- a/src/common/meta/src/kv_backend/rds/postgres.rs +++ b/src/common/meta/src/kv_backend/rds/postgres.rs @@ -36,13 +36,13 @@ use crate::error::{ CreatePostgresPoolSnafu, GetPostgresConnectionSnafu, LoadTlsCertificateSnafu, PostgresExecutionSnafu, PostgresTlsConfigSnafu, PostgresTransactionSnafu, Result, }; -use crate::kv_backend::KvBackendRef; use crate::kv_backend::rds::{ Executor, ExecutorFactory, ExecutorImpl, KvQueryExecutor, RDS_STORE_OP_BATCH_DELETE, RDS_STORE_OP_BATCH_GET, RDS_STORE_OP_BATCH_PUT, RDS_STORE_OP_RANGE_DELETE, RDS_STORE_OP_RANGE_QUERY, RDS_STORE_TXN_RETRY_COUNT, RdsStore, Transaction, ensure_rustls_crypto_provider_installed, }; +use crate::kv_backend::{ClosableKvBackendRef, KvBackendRef}; use crate::rpc::KeyValue; use crate::rpc::store::{ BatchDeleteRequest, BatchDeleteResponse, BatchGetRequest, BatchGetResponse, BatchPutRequest, @@ -850,7 +850,9 @@ impl PgStore { .context(CreatePostgresPoolSnafu)?, }; - Self::with_pg_pool(pool, None, table_name, max_txn_ops, false).await + let backend: KvBackendRef = + Self::with_pg_pool(pool, None, table_name, max_txn_ops, false).await?; + Ok(backend) } /// Create [PgStore] impl of [KvBackendRef] from url (backward compatibility). @@ -858,14 +860,14 @@ impl PgStore { Self::with_url_and_tls(url, table_name, max_txn_ops, None).await } - /// Create [PgStore] impl of [KvBackendRef] from [deadpool_postgres::Pool] with optional schema. + /// Create [PgStore] impl of [ClosableKvBackendRef] from [deadpool_postgres::Pool] with optional schema. pub async fn with_pg_pool( pool: Pool, schema_name: Option<&str>, table_name: &str, max_txn_ops: usize, auto_create_schema: bool, - ) -> Result { + ) -> Result { // Ensure the postgres metadata backend is ready to use. let client = match pool.get().await { Ok(client) => client, diff --git a/src/meta-srv/src/bootstrap.rs b/src/meta-srv/src/bootstrap.rs index 700cc5d8ce9..3be37c1a436 100644 --- a/src/meta-srv/src/bootstrap.rs +++ b/src/meta-srv/src/bootstrap.rs @@ -343,7 +343,7 @@ pub async fn metasrv_builder( ) .await?; - let kv_backend = build_postgres_kv_backend( + let kv_backend: KvBackendRef = build_postgres_kv_backend( &opts.store_addrs, Some(cfg), opts.backend_tls.clone(), @@ -364,7 +364,7 @@ pub async fn metasrv_builder( use crate::utils::mysql::{build_mysql_election, build_mysql_kv_backend}; - let kv_backend = build_mysql_kv_backend( + let kv_backend: KvBackendRef = build_mysql_kv_backend( &opts.store_addrs, opts.backend_tls.as_ref(), &opts.meta_table_name, diff --git a/src/meta-srv/src/utils/mysql.rs b/src/meta-srv/src/utils/mysql.rs index 326f3880f43..5bbfb82357a 100644 --- a/src/meta-srv/src/utils/mysql.rs +++ b/src/meta-srv/src/utils/mysql.rs @@ -14,7 +14,7 @@ use common_meta::election::ElectionRef; use common_meta::election::rds::mysql::{ElectionMysqlClient, MySqlElection}; -use common_meta::kv_backend::KvBackendRef; +use common_meta::kv_backend::ClosableKvBackendRef; use common_meta::kv_backend::rds::MySqlStore; use common_telemetry::info; use servers::tls::{TlsMode, TlsOption}; @@ -88,7 +88,7 @@ pub async fn create_mysql_pool( Ok(pool) } -/// Builds a MySQL-backed metadata [`KvBackendRef`]. +/// Builds a MySQL-backed metadata [`ClosableKvBackendRef`]. /// /// * `store_addrs` - MySQL connection URLs; only the first address is used. /// * `tls_config` - optional TLS settings for the MySQL connection. @@ -99,7 +99,7 @@ pub async fn build_mysql_kv_backend( tls_config: Option<&TlsOption>, table_name: &str, max_txn_ops: usize, -) -> Result { +) -> Result { let pool = create_mysql_pool(store_addrs, tls_config).await?; MySqlStore::with_mysql_pool(pool, table_name, max_txn_ops) .await diff --git a/src/meta-srv/src/utils/postgres.rs b/src/meta-srv/src/utils/postgres.rs index 148a654e2cd..f4bed382237 100644 --- a/src/meta-srv/src/utils/postgres.rs +++ b/src/meta-srv/src/utils/postgres.rs @@ -18,7 +18,7 @@ use std::str::FromStr; use common_error::ext::BoxedError; use common_meta::election::ElectionRef; use common_meta::election::rds::postgres::{ElectionPgClient, PgElection}; -use common_meta::kv_backend::KvBackendRef; +use common_meta::kv_backend::ClosableKvBackendRef; use common_meta::kv_backend::rds::PgStore; use common_meta::kv_backend::rds::postgres::{ TlsMode as PgTlsMode, TlsOption as PgTlsOption, create_postgres_tls_connector, @@ -117,7 +117,7 @@ fn is_unix_socket_url(_: &str) -> bool { false } -/// Builds a Postgres-backed metadata [`KvBackendRef`]. +/// Builds a Postgres-backed metadata [`ClosableKvBackendRef`]. /// /// * `store_addrs` - Postgres connection URLs; only the first address is used. /// * `cfg` - optional deadpool config to customize pool/session behavior. @@ -135,7 +135,7 @@ pub async fn build_postgres_kv_backend( table_name: &str, max_txn_ops: usize, auto_create_schema: bool, -) -> Result { +) -> Result { let pool = create_postgres_pool(store_addrs, cfg, tls_config).await?; PgStore::with_pg_pool( pool,