meta_srv/selector/
lease_based.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::peer::Peer;
16use snafu::ResultExt;
17
18use crate::error::{ListActiveDatanodesSnafu, Result};
19use crate::metasrv::SelectorContext;
20use crate::selector::common::{choose_items, filter_out_excluded_peers};
21use crate::selector::weighted_choose::{RandomWeightedChoose, WeightedItem};
22use crate::selector::{Selector, SelectorOptions};
23
24/// Select all alive datanodes based using a random weighted choose.
25#[derive(Default)]
26pub struct LeaseBasedSelector;
27
28#[async_trait::async_trait]
29impl Selector for LeaseBasedSelector {
30    type Context = SelectorContext;
31    type Output = Vec<Peer>;
32
33    async fn select(&self, ctx: &Self::Context, opts: SelectorOptions) -> Result<Self::Output> {
34        // 1. get alive datanodes.
35        let alive_datanodes = ctx
36            .peer_discovery
37            .active_datanodes(opts.workload_filter)
38            .await
39            .context(ListActiveDatanodesSnafu)?;
40
41        // 2. compute weight array, but the weight of each item is the same.
42        let mut weight_array = alive_datanodes
43            .into_iter()
44            .map(|p| WeightedItem {
45                item: p,
46                weight: 1.0,
47            })
48            .collect();
49
50        // 3. choose peers by weight_array.
51        filter_out_excluded_peers(&mut weight_array, &opts.exclude_peer_ids);
52        let mut weighted_choose = RandomWeightedChoose::new(weight_array);
53        let selected = choose_items(&opts, &mut weighted_choose)?;
54
55        Ok(selected)
56    }
57}