meta_srv/handler/
extract_stat_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 common_meta::datanode::Stat;
17use common_telemetry::{info, warn};
18
19use crate::error::Result;
20use crate::handler::{HandleControl, HeartbeatAccumulator, HeartbeatHandler};
21use crate::metasrv::Context;
22use crate::metrics::{METRIC_META_HEARTBEAT_RATE, METRIC_META_HEARTBEAT_STAT_MEMORY_SIZE};
23
24pub struct ExtractStatHandler;
25
26#[async_trait::async_trait]
27impl HeartbeatHandler for ExtractStatHandler {
28    fn is_acceptable(&self, role: Role) -> bool {
29        role == Role::Datanode
30    }
31
32    async fn handle(
33        &self,
34        req: &HeartbeatRequest,
35        _ctx: &mut Context,
36        acc: &mut HeartbeatAccumulator,
37    ) -> Result<HandleControl> {
38        if req.mailbox_message.is_some() {
39            // If the heartbeat is a mailbox message, it may have no other valid information,
40            // so we don't need to collect stats.
41            return Ok(HandleControl::Continue);
42        }
43
44        match Stat::try_from(req) {
45            Ok(stat) => {
46                METRIC_META_HEARTBEAT_RATE.inc();
47                METRIC_META_HEARTBEAT_STAT_MEMORY_SIZE.observe(stat.memory_size() as f64);
48                let _ = acc.stat.insert(stat);
49            }
50            Err(Some(header)) => {
51                info!("New handshake request: {:?}", header);
52            }
53            Err(_) => {
54                warn!("Incomplete heartbeat data: {:?}", req);
55            }
56        };
57
58        Ok(HandleControl::Continue)
59    }
60}