meta_srv/procedure/region_migration/update_metadata/
rollback_downgraded_region.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// Copyright 2023 Greptime Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use common_error::ext::BoxedError;
use snafu::ResultExt;

use crate::error::{self, Result};
use crate::procedure::region_migration::update_metadata::UpdateMetadata;
use crate::procedure::region_migration::Context;

impl UpdateMetadata {
    /// Rollbacks the downgraded leader region if the candidate region is unreachable.
    ///
    /// Abort(non-retry):
    /// - TableRoute is not found.
    ///
    /// Retry:
    /// - Failed to update [TableRouteValue](common_meta::key::table_region::TableRegionValue).
    /// - Failed to retrieve the metadata of table.
    pub async fn rollback_downgraded_region(&self, ctx: &mut Context) -> Result<()> {
        let table_metadata_manager = ctx.table_metadata_manager.clone();
        let region_id = ctx.region_id();
        let table_id = region_id.table_id();
        let current_table_route_value = ctx.get_table_route_value().await?;

        if let Err(err) = table_metadata_manager
            .update_leader_region_status(table_id, current_table_route_value, |route| {
                if route.region.id == region_id {
                    Some(None)
                } else {
                    None
                }
            })
            .await
            .context(error::TableMetadataManagerSnafu)
        {
            ctx.remove_table_route_value();
            return Err(BoxedError::new(err)).context(error::RetryLaterWithSourceSnafu {
                reason: format!("Failed to update the table route during the rollback downgraded leader region: {region_id}"),
            });
        }

        ctx.register_failure_detectors().await;
        ctx.remove_table_route_value();

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use std::assert_matches::assert_matches;
    use std::sync::Arc;

    use common_meta::key::test_utils::new_test_table_info;
    use common_meta::peer::Peer;
    use common_meta::rpc::router::{LeaderState, Region, RegionRoute};
    use store_api::storage::RegionId;

    use crate::error::Error;
    use crate::procedure::region_migration::migration_abort::RegionMigrationAbort;
    use crate::procedure::region_migration::test_util::{self, TestingEnv};
    use crate::procedure::region_migration::update_metadata::UpdateMetadata;
    use crate::procedure::region_migration::{ContextFactory, PersistentContext, State};
    use crate::region::supervisor::RegionFailureDetectorControl;

    fn new_persistent_context() -> PersistentContext {
        test_util::new_persistent_context(1, 2, RegionId::new(1024, 1))
    }

    #[tokio::test]
    async fn test_table_route_is_not_found_error() {
        let state = UpdateMetadata::Rollback;
        let env = TestingEnv::new();
        let persistent_context = new_persistent_context();
        let mut ctx = env.context_factory().new_context(persistent_context);

        let err = state.downgrade_leader_region(&mut ctx).await.unwrap_err();

        assert_matches!(err, Error::TableRouteNotFound { .. });

        assert!(!err.is_retryable());
    }

    #[tokio::test]
    async fn test_update_table_route_with_retry() {
        let state = UpdateMetadata::Rollback;
        let persistent_context = new_persistent_context();
        let from_peer = persistent_context.from_peer.clone();

        let env = TestingEnv::new();
        let mut ctx = env.context_factory().new_context(persistent_context);
        let (tx, mut rx) = tokio::sync::mpsc::channel(8);
        ctx.region_failure_detector_controller = Arc::new(RegionFailureDetectorControl::new(tx));
        let table_id = ctx.region_id().table_id();

        let table_info = new_test_table_info(1024, vec![1, 2, 3]).into();
        let region_routes = vec![
            RegionRoute {
                region: Region::new_test(RegionId::new(1024, 1)),
                leader_peer: Some(from_peer.clone()),
                leader_state: Some(LeaderState::Downgrading),
                ..Default::default()
            },
            RegionRoute {
                region: Region::new_test(RegionId::new(1024, 2)),
                leader_peer: Some(Peer::empty(4)),
                leader_state: Some(LeaderState::Downgrading),
                ..Default::default()
            },
            RegionRoute {
                region: Region::new_test(RegionId::new(1024, 3)),
                leader_peer: Some(Peer::empty(5)),
                ..Default::default()
            },
        ];

        let expected_region_routes = {
            let mut region_routes = region_routes.clone();
            region_routes[0].leader_state = None;
            region_routes[1].leader_state = None;
            region_routes
        };

        env.create_physical_table_metadata(table_info, region_routes)
            .await;

        let table_metadata_manager = env.table_metadata_manager();
        let old_table_route = table_metadata_manager
            .table_route_manager()
            .table_route_storage()
            .get_with_raw_bytes(table_id)
            .await
            .unwrap()
            .unwrap();

        // modifies the table route.
        table_metadata_manager
            .update_leader_region_status(table_id, &old_table_route, |route| {
                if route.region.id == RegionId::new(1024, 2) {
                    Some(None)
                } else {
                    None
                }
            })
            .await
            .unwrap();

        ctx.volatile_ctx.table_route = Some(old_table_route);

        let err = state
            .rollback_downgraded_region(&mut ctx)
            .await
            .unwrap_err();
        assert!(ctx.volatile_ctx.table_route.is_none());
        assert!(err.is_retryable());
        assert!(format!("{err:?}").contains("Failed to update the table route"));
        assert_eq!(rx.len(), 0);
        state.rollback_downgraded_region(&mut ctx).await.unwrap();
        let event = rx.try_recv().unwrap();
        let detecting_regions = event.into_region_failure_detectors();
        assert_eq!(
            detecting_regions,
            vec![(from_peer.id, ctx.persistent_ctx.region_id)]
        );

        let table_route = table_metadata_manager
            .table_route_manager()
            .table_route_storage()
            .get(table_id)
            .await
            .unwrap()
            .unwrap();
        assert_eq!(
            &expected_region_routes,
            table_route.region_routes().unwrap()
        );
    }

    #[tokio::test]
    async fn test_next_migration_end_state() {
        let mut state = Box::new(UpdateMetadata::Rollback);
        let persistent_context = new_persistent_context();
        let from_peer = persistent_context.from_peer.clone();

        let env = TestingEnv::new();
        let mut ctx = env.context_factory().new_context(persistent_context);
        let table_id = ctx.region_id().table_id();

        let table_info = new_test_table_info(1024, vec![1, 2, 3]).into();
        let region_routes = vec![
            RegionRoute {
                region: Region::new_test(RegionId::new(1024, 1)),
                leader_peer: Some(from_peer.clone()),
                leader_state: Some(LeaderState::Downgrading),
                ..Default::default()
            },
            RegionRoute {
                region: Region::new_test(RegionId::new(1024, 2)),
                leader_peer: Some(Peer::empty(4)),
                leader_state: Some(LeaderState::Downgrading),
                ..Default::default()
            },
            RegionRoute {
                region: Region::new_test(RegionId::new(1024, 3)),
                leader_peer: Some(Peer::empty(5)),
                ..Default::default()
            },
        ];

        let expected_region_routes = {
            let mut region_routes = region_routes.clone();
            region_routes[0].leader_state = None;
            region_routes
        };

        env.create_physical_table_metadata(table_info, region_routes)
            .await;

        let table_metadata_manager = env.table_metadata_manager();

        let (next, _) = state.next(&mut ctx).await.unwrap();

        let _ = next
            .as_any()
            .downcast_ref::<RegionMigrationAbort>()
            .unwrap();

        assert!(ctx.volatile_ctx.table_route.is_none());

        let table_route = table_metadata_manager
            .table_route_manager()
            .table_route_storage()
            .get(table_id)
            .await
            .unwrap()
            .unwrap();
        assert_eq!(
            &expected_region_routes,
            table_route.region_routes().unwrap()
        );
    }
}