meta_srv/handler/
publish_heartbeat_handler.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::{HeartbeatRequest, Role};
16use async_trait::async_trait;
17
18use crate::error::Result;
19use crate::handler::{HandleControl, HeartbeatAccumulator, HeartbeatHandler};
20use crate::metasrv::Context;
21use crate::pubsub::{Message, PublisherRef};
22
23pub struct PublishHeartbeatHandler {
24    publisher: PublisherRef,
25}
26
27impl PublishHeartbeatHandler {
28    pub fn new(publisher: PublisherRef) -> PublishHeartbeatHandler {
29        PublishHeartbeatHandler { publisher }
30    }
31}
32
33#[async_trait]
34impl HeartbeatHandler for PublishHeartbeatHandler {
35    fn is_acceptable(&self, role: Role) -> bool {
36        role == Role::Datanode
37    }
38
39    async fn handle(
40        &self,
41        req: &HeartbeatRequest,
42        _: &mut Context,
43        _: &mut HeartbeatAccumulator,
44    ) -> Result<HandleControl> {
45        let msg = Message::Heartbeat(Box::new(req.clone()));
46        self.publisher.publish(msg).await;
47
48        Ok(HandleControl::Continue)
49    }
50}