meta_srv/gc/
util.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 common_meta::key::table_route::PhysicalTableRouteValue;
16use common_telemetry::warn;
17use store_api::storage::RegionId;
18
19use crate::gc::{Peer2Regions, Region2Peers};
20
21pub fn table_route_to_region(
22    table_route: &PhysicalTableRouteValue,
23    table_regions: &[RegionId],
24    region_to_peer: &mut Region2Peers,
25    peer_to_regions: &mut Peer2Regions,
26) {
27    for &region_id in table_regions {
28        let mut found = false;
29
30        // Find the region in the table route
31        for region_route in &table_route.region_routes {
32            if region_route.region.id == region_id
33                && let Some(leader_peer) = &region_route.leader_peer
34            {
35                region_to_peer.insert(
36                    region_id,
37                    (leader_peer.clone(), region_route.follower_peers.clone()),
38                );
39                peer_to_regions
40                    .entry(leader_peer.clone())
41                    .or_default()
42                    .insert(region_id);
43                found = true;
44                break;
45            }
46        }
47
48        if !found {
49            warn!(
50                "Failed to find region {} in table route or no leader peer found",
51                region_id,
52            );
53        }
54    }
55}