diff --git a/Cargo.lock b/Cargo.lock index b4eb237452d..d5747db10a6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1632,6 +1632,7 @@ dependencies = [ "common-error", "common-macro", "common-meta", + "common-telemetry", "moka", "partition", "snafu 0.8.6", diff --git a/src/cache/Cargo.toml b/src/cache/Cargo.toml index 814eb9e484f..3e67bceca72 100644 --- a/src/cache/Cargo.toml +++ b/src/cache/Cargo.toml @@ -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 diff --git a/src/cache/src/lib.rs b/src/cache/src/lib.rs index 967302cd553..01d54fe526c 100644 --- a/src/cache/src/lib.rs +++ b/src/cache/src/lib.rs @@ -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() --> Cache { - CacheBuilder::new(DEFAULT_CACHE_MAX_CAPACITY) +// Capacity is in entries, matching the existing unweighted Moka caches. +fn cache_builder( + name: &str, +) -> CacheBuilder> { + 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( + name: &str, +) -> Cache { + 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 { @@ -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::(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); + } + } +} diff --git a/src/partition/src/manager.rs b/src/partition/src/manager.rs index 8c4a8c9cd99..63159e99d85 100644 --- a/src/partition/src/manager.rs +++ b/src/partition/src/manager.rs @@ -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,