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::sync::Arc;
18use std::time::Duration;
19
20use catalog::kvbackend::new_table_cache;
21use common_meta::cache::{
22    new_schema_cache, new_table_flownode_set_cache, new_table_info_cache, new_table_name_cache,
23    new_table_route_cache, new_table_schema_cache, new_view_info_cache, CacheRegistry,
24    CacheRegistryBuilder, LayeredCacheRegistryBuilder,
25};
26use common_meta::kv_backend::KvBackendRef;
27use moka::future::CacheBuilder;
28use snafu::OptionExt;
29
30use crate::error::Result;
31
32const DEFAULT_CACHE_MAX_CAPACITY: u64 = 65536;
33const DEFAULT_CACHE_TTL: Duration = Duration::from_secs(10 * 60);
34const DEFAULT_CACHE_TTI: Duration = Duration::from_secs(5 * 60);
35
36pub const TABLE_INFO_CACHE_NAME: &str = "table_info_cache";
37pub const VIEW_INFO_CACHE_NAME: &str = "view_info_cache";
38pub const TABLE_NAME_CACHE_NAME: &str = "table_name_cache";
39pub const TABLE_CACHE_NAME: &str = "table_cache";
40pub const SCHEMA_CACHE_NAME: &str = "schema_cache";
41pub const TABLE_SCHEMA_NAME_CACHE_NAME: &str = "table_schema_name_cache";
42pub const TABLE_FLOWNODE_SET_CACHE_NAME: &str = "table_flownode_set_cache";
43pub const TABLE_ROUTE_CACHE_NAME: &str = "table_route_cache";
44
45/// Builds cache registry for datanode, including:
46/// - Schema cache.
47/// - Table id to schema name cache.
48pub fn build_datanode_cache_registry(kv_backend: KvBackendRef) -> CacheRegistry {
49    // Builds table id schema name cache that never expires.
50    let cache = CacheBuilder::new(DEFAULT_CACHE_MAX_CAPACITY).build();
51    let table_id_schema_cache = Arc::new(new_table_schema_cache(
52        TABLE_SCHEMA_NAME_CACHE_NAME.to_string(),
53        cache,
54        kv_backend.clone(),
55    ));
56
57    // Builds schema cache
58    let cache = CacheBuilder::new(DEFAULT_CACHE_MAX_CAPACITY)
59        .time_to_live(DEFAULT_CACHE_TTL)
60        .time_to_idle(DEFAULT_CACHE_TTI)
61        .build();
62    let schema_cache = Arc::new(new_schema_cache(
63        SCHEMA_CACHE_NAME.to_string(),
64        cache,
65        kv_backend.clone(),
66    ));
67
68    CacheRegistryBuilder::default()
69        .add_cache(table_id_schema_cache)
70        .add_cache(schema_cache)
71        .build()
72}
73
74/// Builds cache registry for frontend and datanode, including:
75/// - Table info cache
76/// - Table name cache
77/// - Table route cache
78/// - Table flow node cache
79/// - View cache
80/// - Schema cache
81pub fn build_fundamental_cache_registry(kv_backend: KvBackendRef) -> CacheRegistry {
82    // Builds table info cache
83    let cache = CacheBuilder::new(DEFAULT_CACHE_MAX_CAPACITY)
84        .time_to_live(DEFAULT_CACHE_TTL)
85        .time_to_idle(DEFAULT_CACHE_TTI)
86        .build();
87    let table_info_cache = Arc::new(new_table_info_cache(
88        TABLE_INFO_CACHE_NAME.to_string(),
89        cache,
90        kv_backend.clone(),
91    ));
92
93    // Builds table name cache
94    let cache = CacheBuilder::new(DEFAULT_CACHE_MAX_CAPACITY)
95        .time_to_live(DEFAULT_CACHE_TTL)
96        .time_to_idle(DEFAULT_CACHE_TTI)
97        .build();
98    let table_name_cache = Arc::new(new_table_name_cache(
99        TABLE_NAME_CACHE_NAME.to_string(),
100        cache,
101        kv_backend.clone(),
102    ));
103
104    // Builds table route cache
105    let cache = CacheBuilder::new(DEFAULT_CACHE_MAX_CAPACITY)
106        .time_to_live(DEFAULT_CACHE_TTL)
107        .time_to_idle(DEFAULT_CACHE_TTI)
108        .build();
109    let table_route_cache = Arc::new(new_table_route_cache(
110        TABLE_ROUTE_CACHE_NAME.to_string(),
111        cache,
112        kv_backend.clone(),
113    ));
114
115    // Builds table flownode set cache
116    let cache = CacheBuilder::new(DEFAULT_CACHE_MAX_CAPACITY)
117        .time_to_live(DEFAULT_CACHE_TTL)
118        .time_to_idle(DEFAULT_CACHE_TTI)
119        .build();
120    let table_flownode_set_cache = Arc::new(new_table_flownode_set_cache(
121        TABLE_FLOWNODE_SET_CACHE_NAME.to_string(),
122        cache,
123        kv_backend.clone(),
124    ));
125    // Builds the view info cache
126    let cache = CacheBuilder::new(DEFAULT_CACHE_MAX_CAPACITY)
127        .time_to_live(DEFAULT_CACHE_TTL)
128        .time_to_idle(DEFAULT_CACHE_TTI)
129        .build();
130    let view_info_cache = Arc::new(new_view_info_cache(
131        VIEW_INFO_CACHE_NAME.to_string(),
132        cache,
133        kv_backend.clone(),
134    ));
135
136    // Builds schema cache
137    let cache = CacheBuilder::new(DEFAULT_CACHE_MAX_CAPACITY)
138        .time_to_live(DEFAULT_CACHE_TTL)
139        .time_to_idle(DEFAULT_CACHE_TTI)
140        .build();
141    let schema_cache = Arc::new(new_schema_cache(
142        SCHEMA_CACHE_NAME.to_string(),
143        cache,
144        kv_backend.clone(),
145    ));
146
147    let table_id_schema_cache = Arc::new(new_table_schema_cache(
148        TABLE_SCHEMA_NAME_CACHE_NAME.to_string(),
149        CacheBuilder::new(DEFAULT_CACHE_MAX_CAPACITY).build(),
150        kv_backend,
151    ));
152    CacheRegistryBuilder::default()
153        .add_cache(table_info_cache)
154        .add_cache(table_name_cache)
155        .add_cache(table_route_cache)
156        .add_cache(view_info_cache)
157        .add_cache(table_flownode_set_cache)
158        .add_cache(schema_cache)
159        .add_cache(table_id_schema_cache)
160        .build()
161}
162
163// TODO(weny): Make the cache configurable.
164pub fn with_default_composite_cache_registry(
165    builder: LayeredCacheRegistryBuilder,
166) -> Result<LayeredCacheRegistryBuilder> {
167    let table_info_cache = builder.get().context(error::CacheRequiredSnafu {
168        name: TABLE_INFO_CACHE_NAME,
169    })?;
170    let table_name_cache = builder.get().context(error::CacheRequiredSnafu {
171        name: TABLE_NAME_CACHE_NAME,
172    })?;
173
174    // Builds table cache
175    let cache = CacheBuilder::new(DEFAULT_CACHE_MAX_CAPACITY)
176        .time_to_live(DEFAULT_CACHE_TTL)
177        .time_to_idle(DEFAULT_CACHE_TTI)
178        .build();
179    let table_cache = Arc::new(new_table_cache(
180        TABLE_CACHE_NAME.to_string(),
181        cache,
182        table_info_cache,
183        table_name_cache,
184    ));
185
186    let registry = CacheRegistryBuilder::default()
187        .add_cache(table_cache)
188        .build();
189
190    Ok(builder.add_cache_registry(registry))
191}