meta_srv/
cache_invalidator.rsuse api::v1::meta::MailboxMessage;
use async_trait::async_trait;
use common_error::ext::BoxedError;
use common_meta::cache_invalidator::{CacheInvalidator, Context};
use common_meta::error::{self as meta_error, Result as MetaResult};
use common_meta::instruction::{CacheIdent, Instruction};
use snafu::ResultExt;
use crate::metasrv::MetasrvInfo;
use crate::service::mailbox::{BroadcastChannel, MailboxRef};
const DEFAULT_SUBJECT: &str = "Invalidate table";
pub struct MetasrvCacheInvalidator {
mailbox: MailboxRef,
info: MetasrvInfo,
}
impl MetasrvCacheInvalidator {
pub fn new(mailbox: MailboxRef, info: MetasrvInfo) -> Self {
Self { mailbox, info }
}
}
impl MetasrvCacheInvalidator {
async fn broadcast(&self, ctx: &Context, instruction: Instruction) -> MetaResult<()> {
let subject = &ctx
.subject
.clone()
.unwrap_or_else(|| DEFAULT_SUBJECT.to_string());
let mut msg = MailboxMessage::json_message(
subject,
&format!("Metasrv@{}", self.info.server_addr),
"Frontend broadcast",
common_time::util::current_time_millis(),
&instruction,
)
.with_context(|_| meta_error::SerdeJsonSnafu)?;
self.mailbox
.broadcast(&BroadcastChannel::Frontend, &msg)
.await
.map_err(BoxedError::new)
.context(meta_error::ExternalSnafu)?;
msg.to = "Datanode broadcast".to_string();
self.mailbox
.broadcast(&BroadcastChannel::Datanode, &msg)
.await
.map_err(BoxedError::new)
.context(meta_error::ExternalSnafu)?;
msg.to = "Flownode broadcast".to_string();
self.mailbox
.broadcast(&BroadcastChannel::Flownode, &msg)
.await
.map_err(BoxedError::new)
.context(meta_error::ExternalSnafu)
}
}
#[async_trait]
impl CacheInvalidator for MetasrvCacheInvalidator {
async fn invalidate(&self, ctx: &Context, caches: &[CacheIdent]) -> MetaResult<()> {
let instruction = Instruction::InvalidateCaches(caches.to_vec());
self.broadcast(ctx, instruction).await
}
}