common_meta/
cache_invalidator.rs1use 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#[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#[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 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 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 }
115 CacheIdent::DropFlow(DropFlow {
116 flow_id,
117 source_table_ids,
118 flow_part2node_id,
119 }) => {
120 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 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 }
168 }
169 }
170 Ok(())
171 }
172
173 fn invalidate_all(&self) -> Result<()> {
174 Ok(())
178 }
179}