From cd03e7c593f61d2afcd490cf4a7e09b7b711568b Mon Sep 17 00:00:00 2001 From: "chunshao.rcs" Date: Thu, 18 May 2023 15:57:04 +0800 Subject: [PATCH] feat: support for disabling router cache --- .../env/cluster/ddl/partition_table.result | 4 ++ .../cases/env/cluster/ddl/partition_table.sql | 3 +- router/src/cluster_based.rs | 39 ++++++++++++------- router/src/lib.rs | 5 ++- 4 files changed, 35 insertions(+), 16 deletions(-) diff --git a/integration_tests/cases/env/cluster/ddl/partition_table.result b/integration_tests/cases/env/cluster/ddl/partition_table.result index b095bc9e84..fba53a2351 100644 --- a/integration_tests/cases/env/cluster/ddl/partition_table.result +++ b/integration_tests/cases/env/cluster/ddl/partition_table.result @@ -94,3 +94,7 @@ DROP TABLE IF EXISTS `partition_table_t`; affected_rows: 0 +SHOW CREATE TABLE partition_table_t; + +Failed to execute query, err: Server(ServerError { code: 500, msg: "Failed to create plan, query: SHOW CREATE TABLE partition_table_t;. Caused by: Failed to create plan, err:Table not found, table:partition_table_t" }) + diff --git a/integration_tests/cases/env/cluster/ddl/partition_table.sql b/integration_tests/cases/env/cluster/ddl/partition_table.sql index a408cad222..59724d430e 100644 --- a/integration_tests/cases/env/cluster/ddl/partition_table.sql +++ b/integration_tests/cases/env/cluster/ddl/partition_table.sql @@ -42,5 +42,4 @@ SELECT * from partition_table_t where name in ("ceresdb5", "ceresdb6", "ceresdb7 DROP TABLE IF EXISTS `partition_table_t`; --- The route cache will cause the data table to be queried after it is deleted. Refer to #893. --- SHOW CREATE TABLE partition_table_t; +SHOW CREATE TABLE partition_table_t; diff --git a/router/src/cluster_based.rs b/router/src/cluster_based.rs index e54da642ef..0274971ce4 100644 --- a/router/src/cluster_based.rs +++ b/router/src/cluster_based.rs @@ -23,16 +23,22 @@ struct RouteData { pub struct ClusterBasedRouter { cluster: ClusterRef, - cache: Cache, + cache: Option>, } impl ClusterBasedRouter { pub fn new(cluster: ClusterRef, cache_config: RouteCacheConfig) -> Self { - let cache = Cache::builder() - .time_to_live(cache_config.ttl.0) - .time_to_idle(cache_config.tti.0) - .max_capacity(cache_config.capacity) - .build(); + let cache = if cache_config.enable { + Some( + Cache::builder() + .time_to_live(cache_config.ttl.0) + .time_to_idle(cache_config.tti.0) + .max_capacity(cache_config.capacity) + .build(), + ) + } else { + None + }; Self { cluster, cache } } @@ -42,12 +48,16 @@ impl ClusterBasedRouter { fn route_from_cache(&self, tables: &[String], routes: &mut Vec) -> Vec { let mut miss = vec![]; - for table in tables { - if let Some(route) = self.cache.get(table) { - routes.push(route.clone()); - } else { - miss.push(table.clone()); + if let Some(cache) = &self.cache { + for table in tables { + if let Some(route) = cache.get(table) { + routes.push(route.clone()); + } else { + miss.push(table.clone()); + } } + } else { + miss = tables.to_vec(); } miss @@ -97,8 +107,10 @@ impl ClusterBasedRouter { }; if let Some(route) = route { - // There may be data race here, and it is acceptable currently. - self.cache.insert(table_name.clone(), route.clone()).await; + if let Some(cache) = &self.cache { + // There may be data race here, and it is acceptable currently. + cache.insert(table_name.clone(), route.clone()).await; + } routes.push(route); } } @@ -257,6 +269,7 @@ mod tests { let mock_cluster = MockClusterImpl {}; let config = RouteCacheConfig { + enable: true, ttl: ReadableDuration::from(Duration::from_secs(4)), tti: ReadableDuration::from(Duration::from_secs(2)), capacity: 2, diff --git a/router/src/lib.rs b/router/src/lib.rs index 2b047c5244..61ccbde0c7 100644 --- a/router/src/lib.rs +++ b/router/src/lib.rs @@ -2,7 +2,7 @@ pub mod cluster_based; pub mod endpoint; -pub(crate) mod hash; +mod hash; pub mod rule_based; use std::{sync::Arc, time::Duration}; @@ -68,6 +68,8 @@ pub trait Router { #[derive(Clone, Debug, Deserialize, Serialize)] pub struct RouteCacheConfig { + /// Enable route cache, default false. + enable: bool, /// Time to live (TTL) in second. ttl: ReadableDuration, /// Time to idle (TTI) in second. @@ -79,6 +81,7 @@ pub struct RouteCacheConfig { impl Default for RouteCacheConfig { fn default() -> Self { Self { + enable: false, ttl: ReadableDuration::from(Duration::from_secs(5)), tti: ReadableDuration::from(Duration::from_secs(5)), capacity: 10_000,