meta_srv/handler/
filter_inactive_region_stats.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;
17use common_telemetry::warn;
18
19use crate::error::Result;
20use crate::handler::{HandleControl, HeartbeatAccumulator, HeartbeatHandler};
21use crate::metasrv::Context;
22
23pub const NAME: &str = "FilterInactiveRegionStatsHandler";
24
25pub struct FilterInactiveRegionStatsHandler;
26
27#[async_trait]
28impl HeartbeatHandler for FilterInactiveRegionStatsHandler {
29    fn is_acceptable(&self, role: Role) -> bool {
30        role == Role::Datanode
31    }
32
33    async fn handle(
34        &self,
35        req: &HeartbeatRequest,
36        _ctx: &mut Context,
37        acc: &mut HeartbeatAccumulator,
38    ) -> Result<HandleControl> {
39        if acc.inactive_region_ids.is_empty() {
40            return Ok(HandleControl::Continue);
41        }
42
43        warn!(
44            "The heartbeat of this node {:?} contains some inactive regions: {:?}",
45            req.peer, acc.inactive_region_ids
46        );
47
48        let Some(stat) = acc.stat.as_mut() else {
49            return Ok(HandleControl::Continue);
50        };
51
52        stat.retain_active_region_stats(&acc.inactive_region_ids);
53
54        Ok(HandleControl::Continue)
55    }
56}