Skip to main content

meta_srv/procedure/drain_datanode/
drain_datanode_start.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::info;
19use serde::{Deserialize, Serialize};
20
21use crate::error::Result;
22use crate::key::NodeSchedulingRole;
23use crate::procedure::drain_datanode::cursor::DrainDatanodeCursor;
24use crate::procedure::drain_datanode::{Context, State};
25
26/// Fences the datanode before scanning its regions.
27#[derive(Debug, Clone, Serialize, Deserialize)]
28pub(crate) struct DrainDatanodeStart;
29
30#[async_trait::async_trait]
31#[typetag::serde]
32impl State for DrainDatanodeStart {
33    async fn next(
34        &mut self,
35        ctx: &mut Context,
36        _procedure_ctx: &ProcedureContext,
37    ) -> Result<(Box<dyn State>, Status)> {
38        let node_id = ctx.persistent_ctx.node_id;
39        let marked = ctx
40            .node_scheduling_manager
41            .mark_unschedulable(NodeSchedulingRole::Datanode, node_id)
42            .await?;
43        if marked {
44            info!("Marked datanode {} as unschedulable", node_id);
45        }
46
47        // The only persisted status of the procedure: persisting it registers the procedure,
48        // so that a metasrv restart resumes it. Scan state and progress stay in
49        // memory, and a resumed procedure starts a fresh scan of the
50        // datanode.
51        Ok((
52            Box::new(DrainDatanodeCursor::default()),
53            Status::executing(true),
54        ))
55    }
56
57    fn as_any(&self) -> &dyn Any {
58        self
59    }
60}
61
62#[cfg(test)]
63mod tests {
64    use common_procedure::Status;
65    use store_api::storage::RegionId;
66
67    use crate::key::NodeSchedulingRole;
68    use crate::procedure::drain_datanode::cursor::DrainDatanodeCursor;
69    use crate::procedure::drain_datanode::drain_datanode_start::DrainDatanodeStart;
70    use crate::procedure::drain_datanode::test_util::{
71        TestEnv, leader_region, new_persistent_context,
72    };
73    use crate::procedure::drain_datanode::{DrainDatanodeStrategy, State};
74    use crate::procedure::region_migration::test_util::TestingEnv;
75
76    #[tokio::test]
77    async fn test_drain_datanode_start_transitions_to_cursor() {
78        let env = TestEnv::new().await;
79        env.create_table(vec![leader_region(RegionId::new(1024, 1), 1)])
80            .await;
81
82        let mut ctx = env.context(new_persistent_context(
83            Some(2),
84            DrainDatanodeStrategy::FailFast,
85        ));
86        let procedure_ctx = TestingEnv::procedure_context();
87
88        let (next, status) = DrainDatanodeStart
89            .next(&mut ctx, &procedure_ctx)
90            .await
91            .unwrap();
92
93        assert!(
94            next.as_any()
95                .downcast_ref::<DrainDatanodeCursor>()
96                .is_some()
97        );
98        // Registering the procedure is the only persisted step of the run.
99        assert!(matches!(status, Status::Executing { persist: true, .. }));
100        assert!(
101            !env.node_scheduling_manager
102                .is_schedulable(NodeSchedulingRole::Datanode, 1)
103                .await
104                .unwrap()
105        );
106    }
107}