Skip to main content

common_meta/
cache_invalidator.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
15use std::sync::Arc;
16
17use crate::error::Result;
18use crate::flow_name::FlowName;
19use crate::instruction::{CacheIdent, DropFlow};
20use crate::key::flow::flow_info::FlowInfoKey;
21use crate::key::flow::flow_name::FlowNameKey;
22use crate::key::flow::flow_route::FlowRouteKey;
23use crate::key::flow::flownode_flow::FlownodeFlowKey;
24use crate::key::flow::table_flow::TableFlowKey;
25use crate::key::node_address::NodeAddressKey;
26use crate::key::schema_name::SchemaNameKey;
27use crate::key::table_info::TableInfoKey;
28use crate::key::table_name::TableNameKey;
29use crate::key::table_route::TableRouteKey;
30use crate::key::tombstone::to_tombstone_key;
31use crate::key::view_info::ViewInfoKey;
32use crate::key::{
33    MetadataKey, drop_generation_key, dropped_at_key, purging_key, retention_expires_at_key,
34};
35
36/// KvBackend cache invalidator
37#[async_trait::async_trait]
38pub trait KvCacheInvalidator: Send + Sync {
39    async fn invalidate_key(&self, key: &[u8]);
40}
41
42pub type KvCacheInvalidatorRef = Arc<dyn KvCacheInvalidator>;
43
44pub struct DummyKvCacheInvalidator;
45
46#[async_trait::async_trait]
47impl KvCacheInvalidator for DummyKvCacheInvalidator {
48    async fn invalidate_key(&self, _key: &[u8]) {}
49}
50
51/// Places context of invalidating cache. e.g., span id, trace id etc.
52#[derive(Default)]
53pub struct Context {
54    pub subject: Option<String>,
55}
56
57#[async_trait::async_trait]
58pub trait CacheInvalidator: Send + Sync {
59    async fn invalidate(&self, ctx: &Context, caches: &[CacheIdent]) -> Result<()>;
60
61    /// Invalidates every cache entry owned by this invalidator.
62    ///
63    /// This method is required so each implementer explicitly decides how
64    /// full-cache invalidation should behave. Implementations that intentionally
65    /// do nothing must document why a no-op is safe.
66    fn invalidate_all(&self) -> Result<()>;
67
68    fn name(&self) -> &'static str {
69        std::any::type_name::<Self>()
70    }
71}
72
73pub type CacheInvalidatorRef = Arc<dyn CacheInvalidator>;
74
75pub struct DummyCacheInvalidator;
76
77#[async_trait::async_trait]
78impl CacheInvalidator for DummyCacheInvalidator {
79    async fn invalidate(&self, _ctx: &Context, _caches: &[CacheIdent]) -> Result<()> {
80        Ok(())
81    }
82
83    fn invalidate_all(&self) -> Result<()> {
84        // Dummy invalidator owns no cache state, so there is nothing to clear.
85        Ok(())
86    }
87}
88
89#[async_trait::async_trait]
90impl<T> CacheInvalidator for T
91where
92    T: KvCacheInvalidator,
93{
94    async fn invalidate(&self, _ctx: &Context, caches: &[CacheIdent]) -> Result<()> {
95        for cache in caches {
96            match cache {
97                CacheIdent::TableId(table_id) => {
98                    let key = TableInfoKey::new(*table_id);
99                    self.invalidate_key(&key.to_bytes()).await;
100
101                    let key = TableRouteKey::new(*table_id);
102                    self.invalidate_key(&key.to_bytes()).await;
103
104                    let key = ViewInfoKey::new(*table_id);
105                    self.invalidate_key(&key.to_bytes()).await;
106
107                    for key in [
108                        dropped_at_key(*table_id),
109                        retention_expires_at_key(*table_id),
110                        drop_generation_key(*table_id),
111                        purging_key(*table_id),
112                    ] {
113                        self.invalidate_key(&to_tombstone_key(&key)).await;
114                    }
115                }
116                CacheIdent::TableName(table_name) => {
117                    let key: TableNameKey = table_name.into();
118                    self.invalidate_key(&key.to_bytes()).await
119                }
120                CacheIdent::SchemaName(schema_name) => {
121                    let key: SchemaNameKey = schema_name.into();
122                    self.invalidate_key(&key.to_bytes()).await;
123                }
124                CacheIdent::CreateFlow(_) => {
125                    // Do nothing
126                }
127                CacheIdent::DropFlow(DropFlow {
128                    flow_id,
129                    source_table_ids,
130                    flow_part2node_id,
131                }) => {
132                    // invalidate flow route/flownode flow/table flow
133                    let mut keys = Vec::with_capacity(
134                        source_table_ids.len() * flow_part2node_id.len()
135                            + flow_part2node_id.len() * 2,
136                    );
137                    for table_id in source_table_ids {
138                        for (partition_id, node_id) in flow_part2node_id {
139                            let key =
140                                TableFlowKey::new(*table_id, *node_id, *flow_id, *partition_id)
141                                    .to_bytes();
142                            keys.push(key);
143                        }
144                    }
145
146                    for (partition_id, node_id) in flow_part2node_id {
147                        let key =
148                            FlownodeFlowKey::new(*node_id, *flow_id, *partition_id).to_bytes();
149                        keys.push(key);
150                        let key = FlowRouteKey::new(*flow_id, *partition_id).to_bytes();
151                        keys.push(key);
152                    }
153
154                    for key in keys {
155                        self.invalidate_key(&key).await;
156                    }
157                }
158                CacheIdent::FlowName(FlowName {
159                    catalog_name,
160                    flow_name,
161                }) => {
162                    let key = FlowNameKey::new(catalog_name, flow_name);
163                    self.invalidate_key(&key.to_bytes()).await
164                }
165                CacheIdent::FlowId(flow_id) => {
166                    let key = FlowInfoKey::new(*flow_id);
167                    self.invalidate_key(&key.to_bytes()).await;
168                }
169                CacheIdent::FlowNodeAddressChange(node_id) => {
170                    // other caches doesn't need to be invalidated
171                    // since this is only for flownode address change not id change
172                    common_telemetry::info!("Invalidate flow node cache for node_id: {}", node_id);
173                    let key = NodeAddressKey::with_flownode(*node_id);
174                    self.invalidate_key(&key.to_bytes()).await;
175                }
176                CacheIdent::User(_) => {
177                    // User cache invalidation is handled by external
178                    // CacheInvalidator implementations.
179                }
180            }
181        }
182        Ok(())
183    }
184
185    fn invalidate_all(&self) -> Result<()> {
186        // KvCacheInvalidator only knows how to invalidate explicit metadata
187        // keys. There is no safe generic way to enumerate or clear the backend
188        // keyspace, so full invalidation is intentionally a no-op here.
189        Ok(())
190    }
191}