Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support for disabling router cache #903

Merged
merged 1 commit into from
May 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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" })

3 changes: 1 addition & 2 deletions integration_tests/cases/env/cluster/ddl/partition_table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;
39 changes: 26 additions & 13 deletions router/src/cluster_based.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,22 @@ struct RouteData {

pub struct ClusterBasedRouter {
cluster: ClusterRef,
cache: Cache<String, RouteData>,
cache: Option<Cache<String, RouteData>>,
}

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 }
}
Expand All @@ -42,12 +48,16 @@ impl ClusterBasedRouter {
fn route_from_cache(&self, tables: &[String], routes: &mut Vec<RouteData>) -> Vec<String> {
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
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 4 additions & 1 deletion router/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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.
Expand All @@ -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,
Expand Down