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