meta_srv/handler/
on_leader_start_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};
16
17use crate::error::Result;
18use crate::handler::{HandleControl, HeartbeatAccumulator, HeartbeatHandler};
19use crate::metasrv::Context;
20
21pub struct OnLeaderStartHandler;
22
23#[async_trait::async_trait]
24impl HeartbeatHandler for OnLeaderStartHandler {
25    fn is_acceptable(&self, _: Role) -> bool {
26        true
27    }
28
29    async fn handle(
30        &self,
31        _req: &HeartbeatRequest,
32        ctx: &mut Context,
33        _acc: &mut HeartbeatAccumulator,
34    ) -> Result<HandleControl> {
35        let Some(election) = &ctx.election else {
36            return Ok(HandleControl::Continue);
37        };
38
39        if election.in_leader_infancy() {
40            ctx.is_infancy = true;
41            // TODO(weny): Unifies the multiple leader state between Context and Metasrv.
42            // we can't ensure the in-memory kv has already been reset in the outside loop.
43            // We still use heartbeat requests to trigger resetting in-memory kv.
44            ctx.reset_in_memory();
45        }
46
47        Ok(HandleControl::Continue)
48    }
49}