common_meta/heartbeat/handler/
invalidate_table_cache.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 async_trait::async_trait;
16use common_telemetry::debug;
17
18use crate::cache_invalidator::{CacheInvalidatorRef, Context};
19use crate::error::Result as MetaResult;
20use crate::heartbeat::handler::{
21    HandleControl, HeartbeatResponseHandler, HeartbeatResponseHandlerContext,
22};
23use crate::instruction::Instruction;
24
25#[derive(Clone)]
26pub struct InvalidateCacheHandler {
27    cache_invalidator: CacheInvalidatorRef,
28}
29
30#[async_trait]
31impl HeartbeatResponseHandler for InvalidateCacheHandler {
32    fn is_acceptable(&self, ctx: &HeartbeatResponseHandlerContext) -> bool {
33        matches!(
34            ctx.incoming_message.as_ref(),
35            Some((_, Instruction::InvalidateCaches(_)))
36        )
37    }
38
39    async fn handle(&self, ctx: &mut HeartbeatResponseHandlerContext) -> MetaResult<HandleControl> {
40        let Some((_, Instruction::InvalidateCaches(caches))) = ctx.incoming_message.take() else {
41            unreachable!("InvalidateCacheHandler: should be guarded by 'is_acceptable'")
42        };
43
44        debug!("InvalidateCacheHandler: invalidating caches: {:?}", caches);
45
46        // Invalidate local cache always success
47        let _ = self
48            .cache_invalidator
49            .invalidate(&Context::default(), &caches)
50            .await?;
51
52        Ok(HandleControl::Done)
53    }
54}
55
56impl InvalidateCacheHandler {
57    pub fn new(cache_invalidator: CacheInvalidatorRef) -> Self {
58        Self { cache_invalidator }
59    }
60}