cache/
lib.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15pub mod error;
16
17use std::hash::Hash;
18use std::sync::Arc;
19use std::time::Duration;
20
21use catalog::kvbackend::new_table_cache;
22use common_meta::cache::{
23    CacheRegistry, CacheRegistryBuilder, LayeredCacheRegistryBuilder, new_schema_cache,
24    new_table_flownode_set_cache, new_table_info_cache, new_table_name_cache,
25    new_table_route_cache, new_table_schema_cache, new_view_info_cache,
26};
27use common_meta::kv_backend::KvBackendRef;
28use moka::future::{Cache, CacheBuilder};
29use partition::cache::new_partition_info_cache;
30use snafu::OptionExt;
31
32use crate::error::Result;
33
34const DEFAULT_CACHE_MAX_CAPACITY: u64 = 65536;
35const DEFAULT_CACHE_TTL: Duration = Duration::from_secs(10 * 60);
36const DEFAULT_CACHE_TTI: Duration = Duration::from_secs(5 * 60);
37
38fn default_cache<K: Send + Sync + Hash + Eq + 'static, V: Send + Sync + Clone + 'static>()
39-> Cache<K, V> {
40    CacheBuilder::new(DEFAULT_CACHE_MAX_CAPACITY)
41        .time_to_live(DEFAULT_CACHE_TTL)
42        .time_to_idle(DEFAULT_CACHE_TTI)
43        .build()
44}
45
46pub const TABLE_INFO_CACHE_NAME: &str = "table_info_cache";
47pub const VIEW_INFO_CACHE_NAME: &str = "view_info_cache";
48pub const TABLE_NAME_CACHE_NAME: &str = "table_name_cache";
49pub const TABLE_CACHE_NAME: &str = "table_cache";
50pub const SCHEMA_CACHE_NAME: &str = "schema_cache";
51pub const TABLE_SCHEMA_NAME_CACHE_NAME: &str = "table_schema_name_cache";
52pub const TABLE_FLOWNODE_SET_CACHE_NAME: &str = "table_flownode_set_cache";
53pub const TABLE_ROUTE_CACHE_NAME: &str = "table_route_cache";
54pub const PARTITION_INFO_CACHE_NAME: &str = "partition_info_cache";
55
56/// Builds cache registry for datanode, including:
57/// - Schema cache.
58/// - Table id to schema name cache.
59pub fn build_datanode_cache_registry(kv_backend: KvBackendRef) -> CacheRegistry {
60    // Builds table id schema name cache that never expires.
61    let cache = CacheBuilder::new(DEFAULT_CACHE_MAX_CAPACITY).build();
62    let table_id_schema_cache = Arc::new(new_table_schema_cache(
63        TABLE_SCHEMA_NAME_CACHE_NAME.to_string(),
64        cache,
65        kv_backend.clone(),
66    ));
67
68    // Builds schema cache
69    let cache = default_cache();
70    let schema_cache = Arc::new(new_schema_cache(
71        SCHEMA_CACHE_NAME.to_string(),
72        cache,
73        kv_backend.clone(),
74    ));
75
76    CacheRegistryBuilder::default()
77        .add_cache(table_id_schema_cache)
78        .add_cache(schema_cache)
79        .build()
80}
81
82/// Builds cache registry for frontend and datanode, including:
83/// - Table info cache
84/// - Table name cache
85/// - Table route cache
86/// - Table flow node cache
87/// - View cache
88/// - Schema cache
89pub fn build_fundamental_cache_registry(kv_backend: KvBackendRef) -> CacheRegistry {
90    // Builds table info cache
91    let cache = default_cache();
92    let table_info_cache = Arc::new(new_table_info_cache(
93        TABLE_INFO_CACHE_NAME.to_string(),
94        cache,
95        kv_backend.clone(),
96    ));
97
98    // Builds table name cache
99    let cache = default_cache();
100    let table_name_cache = Arc::new(new_table_name_cache(
101        TABLE_NAME_CACHE_NAME.to_string(),
102        cache,
103        kv_backend.clone(),
104    ));
105
106    // Builds table route cache
107    let cache = default_cache();
108    let table_route_cache = Arc::new(new_table_route_cache(
109        TABLE_ROUTE_CACHE_NAME.to_string(),
110        cache,
111        kv_backend.clone(),
112    ));
113
114    // Builds table flownode set cache
115    let cache = default_cache();
116    let table_flownode_set_cache = Arc::new(new_table_flownode_set_cache(
117        TABLE_FLOWNODE_SET_CACHE_NAME.to_string(),
118        cache,
119        kv_backend.clone(),
120    ));
121    // Builds the view info cache
122    let cache = default_cache();
123    let view_info_cache = Arc::new(new_view_info_cache(
124        VIEW_INFO_CACHE_NAME.to_string(),
125        cache,
126        kv_backend.clone(),
127    ));
128
129    // Builds schema cache
130    let cache = default_cache();
131    let schema_cache = Arc::new(new_schema_cache(
132        SCHEMA_CACHE_NAME.to_string(),
133        cache,
134        kv_backend.clone(),
135    ));
136
137    let table_id_schema_cache = Arc::new(new_table_schema_cache(
138        TABLE_SCHEMA_NAME_CACHE_NAME.to_string(),
139        CacheBuilder::new(DEFAULT_CACHE_MAX_CAPACITY).build(),
140        kv_backend,
141    ));
142    CacheRegistryBuilder::default()
143        .add_cache(table_info_cache)
144        .add_cache(table_name_cache)
145        .add_cache(table_route_cache)
146        .add_cache(view_info_cache)
147        .add_cache(table_flownode_set_cache)
148        .add_cache(schema_cache)
149        .add_cache(table_id_schema_cache)
150        .build()
151}
152
153// TODO(weny): Make the cache configurable.
154pub fn with_default_composite_cache_registry(
155    builder: LayeredCacheRegistryBuilder,
156) -> Result<LayeredCacheRegistryBuilder> {
157    let table_info_cache = builder.get().context(error::CacheRequiredSnafu {
158        name: TABLE_INFO_CACHE_NAME,
159    })?;
160    let table_name_cache = builder.get().context(error::CacheRequiredSnafu {
161        name: TABLE_NAME_CACHE_NAME,
162    })?;
163    let table_route_cache = builder.get().context(error::CacheRequiredSnafu {
164        name: TABLE_ROUTE_CACHE_NAME,
165    })?;
166
167    // Builds table cache
168    let cache = default_cache();
169    let table_cache = Arc::new(new_table_cache(
170        TABLE_CACHE_NAME.to_string(),
171        cache,
172        table_info_cache,
173        table_name_cache,
174    ));
175
176    let cache = default_cache();
177    let partition_info_cache = Arc::new(new_partition_info_cache(
178        PARTITION_INFO_CACHE_NAME.to_string(),
179        cache,
180        table_route_cache,
181    ));
182
183    let registry = CacheRegistryBuilder::default()
184        .add_cache(table_cache)
185        .add_cache(partition_info_cache)
186        .build();
187
188    Ok(builder.add_cache_registry(registry))
189}