1pub 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, LayeredCacheRegistry, LayeredCacheRegistryBuilder,
24 new_flow_info_cache, new_schema_cache, new_table_flownode_set_cache, new_table_info_cache,
25 new_table_name_cache, 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 FLOW_INFO_CACHE_NAME: &str = "flow_info_cache";
47pub const TABLE_INFO_CACHE_NAME: &str = "table_info_cache";
48pub const VIEW_INFO_CACHE_NAME: &str = "view_info_cache";
49pub const TABLE_NAME_CACHE_NAME: &str = "table_name_cache";
50pub const TABLE_CACHE_NAME: &str = "table_cache";
51pub const SCHEMA_CACHE_NAME: &str = "schema_cache";
52pub const TABLE_SCHEMA_NAME_CACHE_NAME: &str = "table_schema_name_cache";
53pub const TABLE_FLOWNODE_SET_CACHE_NAME: &str = "table_flownode_set_cache";
54pub const TABLE_ROUTE_CACHE_NAME: &str = "table_route_cache";
55pub const PARTITION_INFO_CACHE_NAME: &str = "partition_info_cache";
56
57pub fn build_datanode_layered_cache_registry(kv_backend: KvBackendRef) -> LayeredCacheRegistry {
62 let cache = CacheBuilder::new(DEFAULT_CACHE_MAX_CAPACITY).build();
63 let table_id_schema_cache = Arc::new(new_table_schema_cache(
64 TABLE_SCHEMA_NAME_CACHE_NAME.to_string(),
65 cache,
66 kv_backend.clone(),
67 ));
68
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 let cache = default_cache();
77 let table_info_cache = Arc::new(new_table_info_cache(
78 TABLE_INFO_CACHE_NAME.to_string(),
79 cache,
80 kv_backend.clone(),
81 ));
82
83 let cache = default_cache();
84 let table_name_cache = Arc::new(new_table_name_cache(
85 TABLE_NAME_CACHE_NAME.to_string(),
86 cache,
87 kv_backend.clone(),
88 ));
89
90 let cache = default_cache();
91 let table_route_cache = Arc::new(new_table_route_cache(
92 TABLE_ROUTE_CACHE_NAME.to_string(),
93 cache,
94 kv_backend.clone(),
95 ));
96
97 let cache = default_cache();
98 let table_cache = Arc::new(new_table_cache(
99 TABLE_CACHE_NAME.to_string(),
100 cache,
101 table_info_cache.clone(),
102 table_name_cache.clone(),
103 ));
104
105 let cache = default_cache();
106 let partition_info_cache = Arc::new(new_partition_info_cache(
107 PARTITION_INFO_CACHE_NAME.to_string(),
108 cache,
109 table_route_cache.clone(),
110 ));
111
112 let base_registry = CacheRegistryBuilder::default()
113 .add_cache(table_id_schema_cache)
114 .add_cache(schema_cache)
115 .add_cache(table_info_cache)
116 .add_cache(table_name_cache)
117 .add_cache(table_route_cache)
118 .build();
119 let derived_registry = CacheRegistryBuilder::default()
120 .add_cache(table_cache)
121 .add_cache(partition_info_cache)
122 .build();
123
124 LayeredCacheRegistryBuilder::default()
125 .add_cache_registry(base_registry)
126 .add_cache_registry(derived_registry)
127 .build()
128}
129
130pub fn build_fundamental_cache_registry(kv_backend: KvBackendRef) -> CacheRegistry {
138 let cache = default_cache();
140 let table_info_cache = Arc::new(new_table_info_cache(
141 TABLE_INFO_CACHE_NAME.to_string(),
142 cache,
143 kv_backend.clone(),
144 ));
145
146 let cache = default_cache();
148 let table_name_cache = Arc::new(new_table_name_cache(
149 TABLE_NAME_CACHE_NAME.to_string(),
150 cache,
151 kv_backend.clone(),
152 ));
153
154 let cache = default_cache();
156 let table_route_cache = Arc::new(new_table_route_cache(
157 TABLE_ROUTE_CACHE_NAME.to_string(),
158 cache,
159 kv_backend.clone(),
160 ));
161
162 let cache = default_cache();
164 let table_flownode_set_cache = Arc::new(new_table_flownode_set_cache(
165 TABLE_FLOWNODE_SET_CACHE_NAME.to_string(),
166 cache,
167 kv_backend.clone(),
168 ));
169 let flow_info_cache = Arc::new(new_flow_info_cache(
170 FLOW_INFO_CACHE_NAME.to_string(),
171 default_cache(),
172 kv_backend.clone(),
173 ));
174 let cache = default_cache();
176 let view_info_cache = Arc::new(new_view_info_cache(
177 VIEW_INFO_CACHE_NAME.to_string(),
178 cache,
179 kv_backend.clone(),
180 ));
181
182 let cache = default_cache();
184 let schema_cache = Arc::new(new_schema_cache(
185 SCHEMA_CACHE_NAME.to_string(),
186 cache,
187 kv_backend.clone(),
188 ));
189
190 let table_id_schema_cache = Arc::new(new_table_schema_cache(
191 TABLE_SCHEMA_NAME_CACHE_NAME.to_string(),
192 CacheBuilder::new(DEFAULT_CACHE_MAX_CAPACITY).build(),
193 kv_backend,
194 ));
195 CacheRegistryBuilder::default()
196 .add_cache(table_info_cache)
197 .add_cache(table_name_cache)
198 .add_cache(table_route_cache)
199 .add_cache(view_info_cache)
200 .add_cache(flow_info_cache)
201 .add_cache(table_flownode_set_cache)
202 .add_cache(schema_cache)
203 .add_cache(table_id_schema_cache)
204 .build()
205}
206
207pub fn with_default_composite_cache_registry(
209 builder: LayeredCacheRegistryBuilder,
210) -> Result<LayeredCacheRegistryBuilder> {
211 let table_info_cache = builder.get().context(error::CacheRequiredSnafu {
212 name: TABLE_INFO_CACHE_NAME,
213 })?;
214 let table_name_cache = builder.get().context(error::CacheRequiredSnafu {
215 name: TABLE_NAME_CACHE_NAME,
216 })?;
217 let table_route_cache = builder.get().context(error::CacheRequiredSnafu {
218 name: TABLE_ROUTE_CACHE_NAME,
219 })?;
220
221 let cache = default_cache();
223 let table_cache = Arc::new(new_table_cache(
224 TABLE_CACHE_NAME.to_string(),
225 cache,
226 table_info_cache,
227 table_name_cache,
228 ));
229
230 let cache = default_cache();
231 let partition_info_cache = Arc::new(new_partition_info_cache(
232 PARTITION_INFO_CACHE_NAME.to_string(),
233 cache,
234 table_route_cache,
235 ));
236
237 let registry = CacheRegistryBuilder::default()
238 .add_cache(table_cache)
239 .add_cache(partition_info_cache)
240 .build();
241
242 Ok(builder.add_cache_registry(registry))
243}