Skip to content

Commit f49abc0

Browse files
authored
feat(query-cache): add config to toggle meta-service ownership cache on query (#18197)
1 parent bb430f3 commit f49abc0

File tree

10 files changed

+56
-19
lines changed

10 files changed

+56
-19
lines changed

src/bendsave/src/storage.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use bytes::BytesMut;
2525
use databend_common_base::base::GlobalInstance;
2626
use databend_common_base::runtime::GlobalIORuntime;
2727
use databend_common_catalog::session_type::SessionType;
28+
use databend_common_config::CacheConfig;
2829
use databend_common_config::Config;
2930
use databend_common_config::GlobalConfig;
3031
use databend_common_config::InnerConfig;
@@ -106,6 +107,7 @@ pub async fn verify_query_license(cfg: &InnerConfig) -> Result<()> {
106107
SessionManager::init(cfg)?;
107108
UserApiProvider::init(
108109
cfg.meta.to_meta_grpc_client_conf(),
110+
&CacheConfig::default(),
109111
BuiltIn::default(),
110112
&cfg.query.tenant_id,
111113
cfg.query.tenant_quota.clone(),

src/meta/client/src/established_client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ impl fmt::Display for EstablishedClient {
155155
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
156156
write!(
157157
f,
158-
"EstablishedClient[uniq={}, {}]{{ target: {}, least_required_server: {}, endpoints: {} }}",
158+
"EstablishedClient[uniq={}, {}]{{ target: {}, server_version: {}, endpoints: {} }}",
159159
self.uniq,
160160
self.created_at,
161161
self.target_endpoint,

src/query/config/src/config.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3143,6 +3143,11 @@ impl TryInto<InnerLocalConfig> for LocalConfig {
31433143
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Args)]
31443144
#[serde(default)]
31453145
pub struct CacheConfig {
3146+
/// The data in meta-service using key `TenantOwnershipObjectIdent`
3147+
#[clap(long = "cache-enable-meta-service-ownership", default_value = "false")]
3148+
#[serde(default = "bool_false")]
3149+
pub meta_service_ownership_cache: bool,
3150+
31463151
/// Enable table meta cache. Default is enabled. Set it to false to disable all the table meta caches
31473152
#[clap(long = "cache-enable-table-meta-cache", default_value = "true")]
31483153
#[serde(default = "bool_true")]
@@ -3380,6 +3385,11 @@ fn bool_true() -> bool {
33803385
true
33813386
}
33823387

3388+
#[inline]
3389+
fn bool_false() -> bool {
3390+
false
3391+
}
3392+
33833393
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, ValueEnum)]
33843394
#[serde(rename_all = "lowercase")]
33853395
pub enum CacheStorageTypeConfig {
@@ -3573,6 +3583,7 @@ mod cache_config_converters {
35733583

35743584
fn try_from(value: CacheConfig) -> std::result::Result<Self, Self::Error> {
35753585
Ok(Self {
3586+
meta_service_ownership_cache: value.meta_service_ownership_cache,
35763587
enable_table_meta_cache: value.enable_table_meta_cache,
35773588
table_meta_snapshot_count: value.table_meta_snapshot_count,
35783589
table_meta_segment_bytes: value.table_meta_segment_bytes,
@@ -3607,6 +3618,7 @@ mod cache_config_converters {
36073618
impl From<inner::CacheConfig> for CacheConfig {
36083619
fn from(value: inner::CacheConfig) -> Self {
36093620
Self {
3621+
meta_service_ownership_cache: value.meta_service_ownership_cache,
36103622
enable_table_meta_cache: value.enable_table_meta_cache,
36113623
table_meta_snapshot_count: value.table_meta_snapshot_count,
36123624
table_meta_segment_bytes: value.table_meta_segment_bytes,

src/query/config/src/inner.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,9 @@ impl Default for LocalConfig {
556556

557557
#[derive(Clone, Debug, PartialEq, Eq)]
558558
pub struct CacheConfig {
559+
/// The data in meta-service using key `TenantOwnershipObjectIdent`
560+
pub meta_service_ownership_cache: bool,
561+
559562
/// Enable table meta cache. Default is enabled. Set it to false to disable all the table meta caches
560563
pub enable_table_meta_cache: bool,
561564

@@ -727,6 +730,7 @@ impl Default for DiskCacheConfig {
727730
impl Default for CacheConfig {
728731
fn default() -> Self {
729732
Self {
733+
meta_service_ownership_cache: false,
730734
enable_table_meta_cache: true,
731735
table_meta_snapshot_count: 256,
732736
table_meta_segment_bytes: 1073741824,

src/query/management/src/role/role_mgr.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ static BUILTIN_ROLE_ACCOUNT_ADMIN: &str = "account_admin";
6666
pub struct RoleMgr {
6767
kv_api: Arc<dyn kvapi::KVApi<Error = MetaError> + Send + Sync>,
6868
tenant: Tenant,
69-
ownership_cache: Arc<Mutex<Cache>>,
69+
ownership_cache: Option<Arc<Mutex<Cache>>>,
7070
upgrade_to_pb: bool,
7171
}
7272

@@ -75,7 +75,7 @@ impl RoleMgr {
7575
kv_api: Arc<dyn kvapi::KVApi<Error = MetaError> + Send + Sync>,
7676
tenant: &Tenant,
7777
upgrade_to_pb: bool,
78-
ownership_cache: Arc<Mutex<Cache>>,
78+
ownership_cache: Option<Arc<Mutex<Cache>>>,
7979
) -> Self {
8080
RoleMgr {
8181
kv_api,
@@ -246,24 +246,28 @@ impl RoleApi for RoleMgr {
246246
let object_owner_prefix = self.ownership_object_prefix();
247247

248248
let cached = {
249-
let mut cache = self.ownership_cache.lock().await;
250-
let kvs = cache.try_list_dir(object_owner_prefix.as_str()).await;
249+
if let Some(ownership_cache) = &self.ownership_cache {
250+
let mut cache = ownership_cache.lock().await;
251+
let kvs = cache.try_list_dir(object_owner_prefix.as_str()).await;
251252

252-
match &kvs {
253-
Ok(kvs) => {
254-
info!(
253+
match &kvs {
254+
Ok(kvs) => {
255+
info!(
255256
"RoleMgr::list_ownerships() returned from cache: {} keys; first key: {:?}; last key: {:?}",
256257
kvs.len(),
257258
kvs.first().map(|(k, _)| k),
258259
kvs.last().map(|(k, _)| k),
259260
);
261+
}
262+
Err(err) => {
263+
warn!("list ownerships from cache failed. err: {}; It is not a functional issue but may be a performance issue with more than 100_000 ownership records", err);
264+
}
260265
}
261-
Err(err) => {
262-
warn!("list ownerships from cache failed. err: {}; It is not a functional issue but may be a performance issue with more than 100_000 ownership records", err);
263-
}
264-
}
265266

266-
kvs.ok()
267+
kvs.ok()
268+
} else {
269+
None
270+
}
267271
};
268272

269273
let values = if let Some(kvs) = cached {

src/query/management/tests/it/role.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ async fn new_role_api(
103103
test_api.clone(),
104104
&tenant,
105105
enable_meta_data_upgrade_json_to_pb_from_v307,
106-
Arc::new(Mutex::new(cache)),
106+
Some(Arc::new(Mutex::new(cache))),
107107
);
108108
Ok((test_api, mgr))
109109
}

src/query/service/src/global_services.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ impl GlobalServices {
135135
};
136136
UserApiProvider::init(
137137
config.meta.to_meta_grpc_client_conf(),
138+
&config.cache,
138139
builtin,
139140
&config.query.tenant_id,
140141
config.query.tenant_quota.clone(),

src/query/service/src/test_kits/config.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,11 @@ ADDRESS = 'https://databend.com';"
155155
self
156156
}
157157

158+
pub fn enable_meta_service_ownership_cache(mut self) -> ConfigBuilder {
159+
self.conf.cache.meta_service_ownership_cache = true;
160+
self
161+
}
162+
158163
pub fn enable_table_meta_cache(mut self) -> ConfigBuilder {
159164
self.conf.cache.enable_table_meta_cache = true;
160165
self

src/query/service/tests/it/storages/testdata/configs_table_basic.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ DB.Table: 'system'.'configs', Table: configs-table_id:1, ver:0, Engine: SystemCo
1919
| 'cache' | 'inverted_index_filter_memory_ratio' | '0' | '' |
2020
| 'cache' | 'inverted_index_filter_size' | '2147483648' | '' |
2121
| 'cache' | 'inverted_index_meta_count' | '3000' | '' |
22+
| 'cache' | 'meta_service_ownership_cache' | 'false' | '' |
2223
| 'cache' | 'segment_block_metas_count' | '0' | '' |
2324
| 'cache' | 'table_bloom_index_filter_count' | '0' | '' |
2425
| 'cache' | 'table_bloom_index_filter_size' | '2147483648' | '' |

src/query/users/src/user_api.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use std::sync::Arc;
1818

1919
use databend_common_base::base::tokio::sync::Mutex;
2020
use databend_common_base::base::GlobalInstance;
21+
use databend_common_config::CacheConfig;
2122
use databend_common_config::GlobalConfig;
2223
use databend_common_exception::ErrorCode;
2324
use databend_common_exception::Result;
@@ -65,7 +66,7 @@ pub struct UserApiProvider {
6566
/// When UserApiProvider is created, the cache will be created and initialized.
6667
/// The `Cache` instance internally stores the status about if databend-meta service supports cache.
6768
/// If not, every access returns [`Unsupported`] error.
68-
ownership_cache: Arc<Mutex<Cache>>,
69+
ownership_cache: Option<Arc<Mutex<Cache>>>,
6970

7071
builtin: BuiltIn,
7172
}
@@ -74,11 +75,12 @@ impl UserApiProvider {
7475
#[async_backtrace::framed]
7576
pub async fn init(
7677
conf: RpcClientConf,
78+
cache_config: &CacheConfig,
7779
builtin: BuiltIn,
7880
tenant: &Tenant,
7981
quota: Option<TenantQuota>,
8082
) -> Result<()> {
81-
GlobalInstance::set(Self::try_create(conf, builtin, tenant).await?);
83+
GlobalInstance::set(Self::try_create(conf, cache_config, builtin, tenant).await?);
8284
let user_mgr = UserApiProvider::instance();
8385

8486
if let Some(q) = quota {
@@ -92,6 +94,7 @@ impl UserApiProvider {
9294
#[async_backtrace::framed]
9395
pub async fn try_create(
9496
conf: RpcClientConf,
97+
cache_config: &CacheConfig,
9598
builtin: BuiltIn,
9699
tenant: &Tenant,
97100
) -> Result<Arc<UserApiProvider>> {
@@ -104,8 +107,13 @@ impl UserApiProvider {
104107

105108
let client = meta_store.deref().clone();
106109

107-
let cache = RoleMgr::new_cache(client.clone()).await;
108-
let cache = Arc::new(Mutex::new(cache));
110+
let cache = if cache_config.meta_service_ownership_cache {
111+
let cache = RoleMgr::new_cache(client.clone()).await;
112+
let cache = Arc::new(Mutex::new(cache));
113+
Some(cache)
114+
} else {
115+
None
116+
};
109117

110118
let user_mgr = UserApiProvider {
111119
meta: meta_store.clone(),
@@ -137,7 +145,7 @@ impl UserApiProvider {
137145
conf: RpcClientConf,
138146
tenant: &Tenant,
139147
) -> Result<Arc<UserApiProvider>> {
140-
Self::try_create(conf, BuiltIn::default(), tenant).await
148+
Self::try_create(conf, &CacheConfig::default(), BuiltIn::default(), tenant).await
141149
}
142150

143151
pub fn instance() -> Arc<UserApiProvider> {

0 commit comments

Comments
 (0)