mirror of
https://github.com/GreptimeTeam/greptimedb.git
synced 2026-11-28 05:34:02 +00:00
feat(cache): configure metadata cache capacities for bulk workloads
Allow per-cache entry limits through experimental environment settings while retaining existing TTL and invalidation behavior. Expose the registered table route cache so bulk metadata reads share its coordinated loads. Signed-off-by: WenyXu <wenymedia@gmail.com>
This commit is contained in:
Generated
+1
@@ -1632,6 +1632,7 @@ dependencies = [
|
||||
"common-error",
|
||||
"common-macro",
|
||||
"common-meta",
|
||||
"common-telemetry",
|
||||
"moka",
|
||||
"partition",
|
||||
"snafu 0.8.6",
|
||||
|
||||
Vendored
+1
@@ -9,6 +9,7 @@ catalog.workspace = true
|
||||
common-error.workspace = true
|
||||
common-macro.workspace = true
|
||||
common-meta.workspace = true
|
||||
common-telemetry.workspace = true
|
||||
moka = { workspace = true, features = ["future"] }
|
||||
partition.workspace = true
|
||||
snafu.workspace = true
|
||||
|
||||
Vendored
+77
-21
@@ -35,9 +35,36 @@ const DEFAULT_CACHE_MAX_CAPACITY: u64 = 65536;
|
||||
const DEFAULT_CACHE_TTL: Duration = Duration::from_secs(10 * 60);
|
||||
const DEFAULT_CACHE_TTI: Duration = Duration::from_secs(5 * 60);
|
||||
|
||||
fn default_cache<K: Send + Sync + Hash + Eq + 'static, V: Send + Sync + Clone + 'static>()
|
||||
-> Cache<K, V> {
|
||||
CacheBuilder::new(DEFAULT_CACHE_MAX_CAPACITY)
|
||||
// Capacity is in entries, matching the existing unweighted Moka caches.
|
||||
fn cache_builder<K: Send + Sync + Hash + Eq + 'static, V: Send + Sync + Clone + 'static>(
|
||||
name: &str,
|
||||
) -> CacheBuilder<K, V, Cache<K, V>> {
|
||||
let env = format!(
|
||||
"GREPTIMEDB_EXPERIMENTAL_{}_MAX_ENTRIES",
|
||||
name.to_ascii_uppercase()
|
||||
);
|
||||
let value = std::env::var(&env).ok();
|
||||
let capacity = parse_cache_capacity(&env, value.as_deref());
|
||||
common_telemetry::info!(
|
||||
"Metadata cache {name} capacity: {capacity} entries, configured by {env}"
|
||||
);
|
||||
CacheBuilder::new(capacity)
|
||||
}
|
||||
|
||||
fn parse_cache_capacity(env: &str, value: Option<&str>) -> u64 {
|
||||
match value {
|
||||
None => DEFAULT_CACHE_MAX_CAPACITY,
|
||||
Some(value) => value.parse().unwrap_or_else(|_| {
|
||||
common_telemetry::warn!("Invalid {env}: expected an unsigned entry count; using {DEFAULT_CACHE_MAX_CAPACITY}");
|
||||
DEFAULT_CACHE_MAX_CAPACITY
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
fn default_cache<K: Send + Sync + Hash + Eq + 'static, V: Send + Sync + Clone + 'static>(
|
||||
name: &str,
|
||||
) -> Cache<K, V> {
|
||||
cache_builder(name)
|
||||
.time_to_live(DEFAULT_CACHE_TTL)
|
||||
.time_to_idle(DEFAULT_CACHE_TTI)
|
||||
.build()
|
||||
@@ -59,42 +86,42 @@ pub const PARTITION_INFO_CACHE_NAME: &str = "partition_info_cache";
|
||||
/// Invalidating the base layer first prevents a concurrent query from refilling a derived cache
|
||||
/// from stale metadata after that derived cache has been invalidated.
|
||||
pub fn build_datanode_layered_cache_registry(kv_backend: KvBackendRef) -> LayeredCacheRegistry {
|
||||
let cache = CacheBuilder::new(DEFAULT_CACHE_MAX_CAPACITY).build();
|
||||
let cache = cache_builder(TABLE_SCHEMA_NAME_CACHE_NAME).build();
|
||||
let table_id_schema_cache = Arc::new(new_table_schema_cache(
|
||||
TABLE_SCHEMA_NAME_CACHE_NAME.to_string(),
|
||||
cache,
|
||||
kv_backend.clone(),
|
||||
));
|
||||
|
||||
let cache = default_cache();
|
||||
let cache = default_cache(SCHEMA_CACHE_NAME);
|
||||
let schema_cache = Arc::new(new_schema_cache(
|
||||
SCHEMA_CACHE_NAME.to_string(),
|
||||
cache,
|
||||
kv_backend.clone(),
|
||||
));
|
||||
|
||||
let cache = default_cache();
|
||||
let cache = default_cache(TABLE_INFO_CACHE_NAME);
|
||||
let table_info_cache = Arc::new(new_table_info_cache(
|
||||
TABLE_INFO_CACHE_NAME.to_string(),
|
||||
cache,
|
||||
kv_backend.clone(),
|
||||
));
|
||||
|
||||
let cache = default_cache();
|
||||
let cache = default_cache(TABLE_NAME_CACHE_NAME);
|
||||
let table_name_cache = Arc::new(new_table_name_cache(
|
||||
TABLE_NAME_CACHE_NAME.to_string(),
|
||||
cache,
|
||||
kv_backend.clone(),
|
||||
));
|
||||
|
||||
let cache = default_cache();
|
||||
let cache = default_cache(TABLE_ROUTE_CACHE_NAME);
|
||||
let table_route_cache = Arc::new(new_table_route_cache(
|
||||
TABLE_ROUTE_CACHE_NAME.to_string(),
|
||||
cache,
|
||||
kv_backend.clone(),
|
||||
));
|
||||
|
||||
let cache = default_cache();
|
||||
let cache = default_cache(TABLE_CACHE_NAME);
|
||||
let table_cache = Arc::new(new_table_cache(
|
||||
TABLE_CACHE_NAME.to_string(),
|
||||
cache,
|
||||
@@ -102,7 +129,7 @@ pub fn build_datanode_layered_cache_registry(kv_backend: KvBackendRef) -> Layere
|
||||
table_name_cache.clone(),
|
||||
));
|
||||
|
||||
let cache = default_cache();
|
||||
let cache = default_cache(PARTITION_INFO_CACHE_NAME);
|
||||
let partition_info_cache = Arc::new(new_partition_info_cache(
|
||||
PARTITION_INFO_CACHE_NAME.to_string(),
|
||||
cache,
|
||||
@@ -136,7 +163,7 @@ pub fn build_datanode_layered_cache_registry(kv_backend: KvBackendRef) -> Layere
|
||||
/// - Schema cache
|
||||
pub fn build_fundamental_cache_registry(kv_backend: KvBackendRef) -> CacheRegistry {
|
||||
// Builds table info cache
|
||||
let cache = default_cache();
|
||||
let cache = default_cache(TABLE_INFO_CACHE_NAME);
|
||||
let table_info_cache = Arc::new(new_table_info_cache(
|
||||
TABLE_INFO_CACHE_NAME.to_string(),
|
||||
cache,
|
||||
@@ -144,7 +171,7 @@ pub fn build_fundamental_cache_registry(kv_backend: KvBackendRef) -> CacheRegist
|
||||
));
|
||||
|
||||
// Builds table name cache
|
||||
let cache = default_cache();
|
||||
let cache = default_cache(TABLE_NAME_CACHE_NAME);
|
||||
let table_name_cache = Arc::new(new_table_name_cache(
|
||||
TABLE_NAME_CACHE_NAME.to_string(),
|
||||
cache,
|
||||
@@ -152,7 +179,7 @@ pub fn build_fundamental_cache_registry(kv_backend: KvBackendRef) -> CacheRegist
|
||||
));
|
||||
|
||||
// Builds table route cache
|
||||
let cache = default_cache();
|
||||
let cache = default_cache(TABLE_ROUTE_CACHE_NAME);
|
||||
let table_route_cache = Arc::new(new_table_route_cache(
|
||||
TABLE_ROUTE_CACHE_NAME.to_string(),
|
||||
cache,
|
||||
@@ -160,7 +187,7 @@ pub fn build_fundamental_cache_registry(kv_backend: KvBackendRef) -> CacheRegist
|
||||
));
|
||||
|
||||
// Builds table flownode set cache
|
||||
let cache = default_cache();
|
||||
let cache = default_cache(TABLE_FLOWNODE_SET_CACHE_NAME);
|
||||
let table_flownode_set_cache = Arc::new(new_table_flownode_set_cache(
|
||||
TABLE_FLOWNODE_SET_CACHE_NAME.to_string(),
|
||||
cache,
|
||||
@@ -168,11 +195,11 @@ pub fn build_fundamental_cache_registry(kv_backend: KvBackendRef) -> CacheRegist
|
||||
));
|
||||
let flow_info_cache = Arc::new(new_flow_info_cache(
|
||||
FLOW_INFO_CACHE_NAME.to_string(),
|
||||
default_cache(),
|
||||
default_cache(FLOW_INFO_CACHE_NAME),
|
||||
kv_backend.clone(),
|
||||
));
|
||||
// Builds the view info cache
|
||||
let cache = default_cache();
|
||||
let cache = default_cache(VIEW_INFO_CACHE_NAME);
|
||||
let view_info_cache = Arc::new(new_view_info_cache(
|
||||
VIEW_INFO_CACHE_NAME.to_string(),
|
||||
cache,
|
||||
@@ -180,7 +207,7 @@ pub fn build_fundamental_cache_registry(kv_backend: KvBackendRef) -> CacheRegist
|
||||
));
|
||||
|
||||
// Builds schema cache
|
||||
let cache = default_cache();
|
||||
let cache = default_cache(SCHEMA_CACHE_NAME);
|
||||
let schema_cache = Arc::new(new_schema_cache(
|
||||
SCHEMA_CACHE_NAME.to_string(),
|
||||
cache,
|
||||
@@ -189,7 +216,7 @@ pub fn build_fundamental_cache_registry(kv_backend: KvBackendRef) -> CacheRegist
|
||||
|
||||
let table_id_schema_cache = Arc::new(new_table_schema_cache(
|
||||
TABLE_SCHEMA_NAME_CACHE_NAME.to_string(),
|
||||
CacheBuilder::new(DEFAULT_CACHE_MAX_CAPACITY).build(),
|
||||
cache_builder(TABLE_SCHEMA_NAME_CACHE_NAME).build(),
|
||||
kv_backend,
|
||||
));
|
||||
CacheRegistryBuilder::default()
|
||||
@@ -204,7 +231,6 @@ pub fn build_fundamental_cache_registry(kv_backend: KvBackendRef) -> CacheRegist
|
||||
.build()
|
||||
}
|
||||
|
||||
// TODO(weny): Make the cache configurable.
|
||||
pub fn with_default_composite_cache_registry(
|
||||
builder: LayeredCacheRegistryBuilder,
|
||||
) -> Result<LayeredCacheRegistryBuilder> {
|
||||
@@ -219,7 +245,7 @@ pub fn with_default_composite_cache_registry(
|
||||
})?;
|
||||
|
||||
// Builds table cache
|
||||
let cache = default_cache();
|
||||
let cache = default_cache(TABLE_CACHE_NAME);
|
||||
let table_cache = Arc::new(new_table_cache(
|
||||
TABLE_CACHE_NAME.to_string(),
|
||||
cache,
|
||||
@@ -227,7 +253,7 @@ pub fn with_default_composite_cache_registry(
|
||||
table_name_cache,
|
||||
));
|
||||
|
||||
let cache = default_cache();
|
||||
let cache = default_cache(PARTITION_INFO_CACHE_NAME);
|
||||
let partition_info_cache = Arc::new(new_partition_info_cache(
|
||||
PARTITION_INFO_CACHE_NAME.to_string(),
|
||||
cache,
|
||||
@@ -241,3 +267,33 @@ pub fn with_default_composite_cache_registry(
|
||||
|
||||
Ok(builder.add_cache_registry(registry))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_metadata_cache_builder_capacity() {
|
||||
let name = "GREPTIMEDB_EXPERIMENTAL_TABLE_INFO_CACHE_MAX_ENTRIES";
|
||||
let value = std::env::var(name).ok();
|
||||
let expected = parse_cache_capacity(name, value.as_deref());
|
||||
let cache = default_cache::<u32, u32>(TABLE_INFO_CACHE_NAME);
|
||||
assert_eq!(cache.policy().max_capacity(), Some(expected));
|
||||
assert_eq!(cache.policy().time_to_live(), Some(DEFAULT_CACHE_TTL));
|
||||
assert_eq!(cache.policy().time_to_idle(), Some(DEFAULT_CACHE_TTI));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_metadata_cache_capacity() {
|
||||
for (value, expected) in [
|
||||
(None, DEFAULT_CACHE_MAX_CAPACITY),
|
||||
(Some("0"), 0),
|
||||
(Some("262144"), 262144),
|
||||
(Some("-1"), DEFAULT_CACHE_MAX_CAPACITY),
|
||||
(Some(""), DEFAULT_CACHE_MAX_CAPACITY),
|
||||
(Some("18446744073709551616"), DEFAULT_CACHE_MAX_CAPACITY),
|
||||
] {
|
||||
assert_eq!(parse_cache_capacity("test", value), expected);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,6 +78,11 @@ impl PartitionRuleManager {
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the registered route cache, sharing loads and invalidation with routing.
|
||||
pub fn table_route_cache(&self) -> &TableRouteCacheRef {
|
||||
&self.table_route_cache
|
||||
}
|
||||
|
||||
pub async fn find_physical_table_route(
|
||||
&self,
|
||||
table_id: TableId,
|
||||
|
||||
Reference in New Issue
Block a user