meta_srv/procedure/region_migration/
migration_abort.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;
16
17use common_procedure::{Context as ProcedureContext, Status};
18use common_telemetry::warn;
19use serde::{Deserialize, Serialize};
20
21use crate::error::{self, Result};
22use crate::procedure::region_migration::{Context, State};
23
24#[derive(Debug, Serialize, Deserialize)]
25pub struct RegionMigrationAbort {
26    reason: String,
27}
28
29impl RegionMigrationAbort {
30    /// Returns the [RegionMigrationAbort] with `reason`.
31    pub fn new(reason: &str) -> Self {
32        Self {
33            reason: reason.to_string(),
34        }
35    }
36}
37
38#[async_trait::async_trait]
39#[typetag::serde]
40impl State for RegionMigrationAbort {
41    async fn next(
42        &mut self,
43        ctx: &mut Context,
44        _procedure_ctx: &ProcedureContext,
45    ) -> Result<(Box<dyn State>, Status)> {
46        warn!(
47            "Region migration is aborted: {}, region_id: {}, from_peer: {}, to_peer: {}, {}",
48            self.reason,
49            ctx.region_id(),
50            ctx.persistent_ctx.from_peer,
51            ctx.persistent_ctx.to_peer,
52            ctx.volatile_ctx.metrics,
53        );
54        error::MigrationAbortSnafu {
55            reason: &self.reason,
56        }
57        .fail()
58    }
59
60    fn as_any(&self) -> &dyn Any {
61        self
62    }
63}