meta_srv/procedure/region_migration/
close_downgraded_region.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 std::any::Any;
16use std::time::Duration;
17
18use api::v1::meta::MailboxMessage;
19use common_meta::distributed_time_constants::REGION_LEASE_SECS;
20use common_meta::instruction::{Instruction, InstructionReply, SimpleReply};
21use common_meta::key::datanode_table::RegionInfo;
22use common_meta::RegionIdent;
23use common_procedure::{Context as ProcedureContext, Status};
24use common_telemetry::{info, warn};
25use serde::{Deserialize, Serialize};
26use snafu::ResultExt;
27
28use crate::error::{self, Result};
29use crate::handler::HeartbeatMailbox;
30use crate::procedure::region_migration::migration_end::RegionMigrationEnd;
31use crate::procedure::region_migration::{Context, State};
32use crate::service::mailbox::Channel;
33
34/// Uses lease time of a region as the timeout of closing a downgraded region.
35const CLOSE_DOWNGRADED_REGION_TIMEOUT: Duration = Duration::from_secs(REGION_LEASE_SECS);
36
37#[derive(Debug, Serialize, Deserialize)]
38pub struct CloseDowngradedRegion;
39
40#[async_trait::async_trait]
41#[typetag::serde]
42impl State for CloseDowngradedRegion {
43    async fn next(
44        &mut self,
45        ctx: &mut Context,
46        _procedure_ctx: &ProcedureContext,
47    ) -> Result<(Box<dyn State>, Status)> {
48        if let Err(err) = self.close_downgraded_leader_region(ctx).await {
49            let downgrade_leader_datanode = &ctx.persistent_ctx.from_peer;
50            let region_id = ctx.region_id();
51            warn!(err; "Failed to close downgraded leader region: {region_id} on datanode {:?}", downgrade_leader_datanode);
52        }
53        info!(
54            "Region migration is finished: region_id: {}, from_peer: {}, to_peer: {}, {}",
55            ctx.region_id(),
56            ctx.persistent_ctx.from_peer,
57            ctx.persistent_ctx.to_peer,
58            ctx.volatile_ctx.metrics,
59        );
60        Ok((Box::new(RegionMigrationEnd), Status::done()))
61    }
62
63    fn as_any(&self) -> &dyn Any {
64        self
65    }
66}
67
68impl CloseDowngradedRegion {
69    /// Builds close region instruction.
70    ///
71    /// Abort(non-retry):
72    /// - Datanode Table is not found.
73    async fn build_close_region_instruction(&self, ctx: &mut Context) -> Result<Instruction> {
74        let pc = &ctx.persistent_ctx;
75        let downgrade_leader_datanode_id = pc.from_peer.id;
76        let table_id = pc.region_id.table_id();
77        let region_number = pc.region_id.region_number();
78        let datanode_table_value = ctx.get_from_peer_datanode_table_value().await?;
79
80        let RegionInfo { engine, .. } = datanode_table_value.region_info.clone();
81
82        Ok(Instruction::CloseRegion(RegionIdent {
83            datanode_id: downgrade_leader_datanode_id,
84            table_id,
85            region_number,
86            engine,
87        }))
88    }
89
90    /// Closes the downgraded leader region.
91    async fn close_downgraded_leader_region(&self, ctx: &mut Context) -> Result<()> {
92        let close_instruction = self.build_close_region_instruction(ctx).await?;
93        let region_id = ctx.region_id();
94        let pc = &ctx.persistent_ctx;
95        let downgrade_leader_datanode = &pc.from_peer;
96        let msg = MailboxMessage::json_message(
97            &format!("Close downgraded region: {}", region_id),
98            &format!("Metasrv@{}", ctx.server_addr()),
99            &format!(
100                "Datanode-{}@{}",
101                downgrade_leader_datanode.id, downgrade_leader_datanode.addr
102            ),
103            common_time::util::current_time_millis(),
104            &close_instruction,
105        )
106        .with_context(|_| error::SerializeToJsonSnafu {
107            input: close_instruction.to_string(),
108        })?;
109
110        let ch = Channel::Datanode(downgrade_leader_datanode.id);
111        let receiver = ctx
112            .mailbox
113            .send(&ch, msg, CLOSE_DOWNGRADED_REGION_TIMEOUT)
114            .await?;
115
116        match receiver.await {
117            Ok(msg) => {
118                let reply = HeartbeatMailbox::json_reply(&msg)?;
119                info!(
120                    "Received close downgraded leade region reply: {:?}, region: {}",
121                    reply, region_id
122                );
123                let InstructionReply::CloseRegion(SimpleReply { result, error }) = reply else {
124                    return error::UnexpectedInstructionReplySnafu {
125                        mailbox_message: msg.to_string(),
126                        reason: "expect close region reply",
127                    }
128                    .fail();
129                };
130
131                if result {
132                    Ok(())
133                } else {
134                    error::UnexpectedSnafu {
135                        violated: format!(
136                            "Failed to close downgraded leader region: {region_id} on datanode {:?}, error: {error:?}",
137                            downgrade_leader_datanode,
138                        ),
139                    }
140                    .fail()
141                }
142            }
143
144            Err(e) => Err(e),
145        }
146    }
147}