meta_srv/
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 api::v1::meta::MailboxMessage;
16use async_trait::async_trait;
17use common_error::ext::BoxedError;
18use common_meta::cache_invalidator::{CacheInvalidator, Context};
19use common_meta::error::{self as meta_error, Result as MetaResult};
20use common_meta::instruction::{CacheIdent, Instruction};
21use snafu::ResultExt;
22
23use crate::metasrv::MetasrvInfo;
24use crate::service::mailbox::{BroadcastChannel, MailboxRef};
25
26const DEFAULT_SUBJECT: &str = "Invalidate table";
27
28pub struct MetasrvCacheInvalidator {
29    mailbox: MailboxRef,
30    // Metasrv infos
31    info: MetasrvInfo,
32}
33
34impl MetasrvCacheInvalidator {
35    pub fn new(mailbox: MailboxRef, info: MetasrvInfo) -> Self {
36        Self { mailbox, info }
37    }
38}
39
40impl MetasrvCacheInvalidator {
41    async fn broadcast(&self, ctx: &Context, instruction: Instruction) -> MetaResult<()> {
42        let subject = &ctx
43            .subject
44            .clone()
45            .unwrap_or_else(|| DEFAULT_SUBJECT.to_string());
46
47        let mut msg = MailboxMessage::json_message(
48            subject,
49            &format!("Metasrv@{}", self.info.server_addr),
50            "Frontend broadcast",
51            common_time::util::current_time_millis(),
52            &instruction,
53        )
54        .with_context(|_| meta_error::SerdeJsonSnafu)?;
55
56        self.mailbox
57            .broadcast(&BroadcastChannel::Frontend, &msg)
58            .await
59            .map_err(BoxedError::new)
60            .context(meta_error::ExternalSnafu)?;
61
62        msg.to = "Datanode broadcast".to_string();
63        self.mailbox
64            .broadcast(&BroadcastChannel::Datanode, &msg)
65            .await
66            .map_err(BoxedError::new)
67            .context(meta_error::ExternalSnafu)?;
68
69        msg.to = "Flownode broadcast".to_string();
70        self.mailbox
71            .broadcast(&BroadcastChannel::Flownode, &msg)
72            .await
73            .map_err(BoxedError::new)
74            .context(meta_error::ExternalSnafu)
75    }
76}
77
78#[async_trait]
79impl CacheInvalidator for MetasrvCacheInvalidator {
80    async fn invalidate(&self, ctx: &Context, caches: &[CacheIdent]) -> MetaResult<()> {
81        let instruction = Instruction::InvalidateCaches(caches.to_vec());
82        self.broadcast(ctx, instruction).await
83    }
84}