mirror of
https://github.com/GreptimeTeam/greptimedb.git
synced 2026-11-28 05:34:02 +00:00
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 <wenymedia@gmail.com>
This commit is contained in:
+16
-13
@@ -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 => {
|
||||
|
||||
@@ -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<T, S, R> ClosableKvBackend for RdsStore<T, S, R>
|
||||
where
|
||||
R: 'static,
|
||||
Self: KvQueryExecutor<T> + Send + Sync,
|
||||
T: Executor + 'static,
|
||||
S: ExecutorFactory<T> + '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<bool> {
|
||||
if txn_ops.is_empty() {
|
||||
|
||||
@@ -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<MySql>].
|
||||
/// Create [MySqlStore] impl of [ClosableKvBackendRef] from [sqlx::Pool<MySql>].
|
||||
pub async fn with_mysql_pool(
|
||||
pool: Pool<MySql>,
|
||||
table_name: &str,
|
||||
max_txn_ops: usize,
|
||||
) -> Result<KvBackendRef> {
|
||||
) -> Result<ClosableKvBackendRef> {
|
||||
// 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.
|
||||
|
||||
@@ -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<KvBackendRef> {
|
||||
) -> Result<ClosableKvBackendRef> {
|
||||
// Ensure the postgres metadata backend is ready to use.
|
||||
let client = match pool.get().await {
|
||||
Ok(client) => client,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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<KvBackendRef> {
|
||||
) -> Result<ClosableKvBackendRef> {
|
||||
let pool = create_mysql_pool(store_addrs, tls_config).await?;
|
||||
MySqlStore::with_mysql_pool(pool, table_name, max_txn_ops)
|
||||
.await
|
||||
|
||||
@@ -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<KvBackendRef> {
|
||||
) -> Result<ClosableKvBackendRef> {
|
||||
let pool = create_postgres_pool(store_addrs, cfg, tls_config).await?;
|
||||
PgStore::with_pg_pool(
|
||||
pool,
|
||||
|
||||
Reference in New Issue
Block a user