meta_srv/selector/
round_robin.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
// 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 std::sync::atomic::AtomicUsize;

use common_meta::peer::Peer;
use snafu::ensure;

use crate::error::{NoEnoughAvailableNodeSnafu, Result};
use crate::lease;
use crate::metasrv::{SelectTarget, SelectorContext};
use crate::selector::{Selector, SelectorOptions};

/// Round-robin selector that returns the next peer in the list in sequence.
/// Datanodes are ordered by their node_id.
///
/// This selector is useful when you want to distribute the load evenly across
/// all datanodes. But **it's not recommended** to use this selector in serious
/// production environments because it doesn't take into account the load of
/// each datanode.
pub struct RoundRobinSelector {
    select_target: SelectTarget,
    counter: AtomicUsize,
}

impl Default for RoundRobinSelector {
    fn default() -> Self {
        Self {
            select_target: SelectTarget::Datanode,
            counter: AtomicUsize::new(0),
        }
    }
}

impl RoundRobinSelector {
    pub fn new(select_target: SelectTarget) -> Self {
        Self {
            select_target,
            ..Default::default()
        }
    }

    async fn get_peers(
        &self,
        min_required_items: usize,
        ctx: &SelectorContext,
    ) -> Result<Vec<Peer>> {
        let mut peers = match self.select_target {
            SelectTarget::Datanode => {
                // 1. get alive datanodes.
                let lease_kvs =
                    lease::alive_datanodes(&ctx.meta_peer_client, ctx.datanode_lease_secs).await?;

                // 2. map into peers
                lease_kvs
                    .into_iter()
                    .map(|(k, v)| Peer::new(k.node_id, v.node_addr))
                    .collect::<Vec<_>>()
            }
            SelectTarget::Flownode => {
                // 1. get alive flownodes.
                let lease_kvs =
                    lease::alive_flownodes(&ctx.meta_peer_client, ctx.flownode_lease_secs).await?;

                // 2. map into peers
                lease_kvs
                    .into_iter()
                    .map(|(k, v)| Peer::new(k.node_id, v.node_addr))
                    .collect::<Vec<_>>()
            }
        };

        ensure!(
            !peers.is_empty(),
            NoEnoughAvailableNodeSnafu {
                required: min_required_items,
                available: 0usize,
                select_target: self.select_target
            }
        );

        // 3. sort by node id
        peers.sort_by_key(|p| p.id);

        Ok(peers)
    }
}

#[async_trait::async_trait]
impl Selector for RoundRobinSelector {
    type Context = SelectorContext;
    type Output = Vec<Peer>;

    async fn select(&self, ctx: &Self::Context, opts: SelectorOptions) -> Result<Vec<Peer>> {
        let peers = self.get_peers(opts.min_required_items, ctx).await?;
        // choose peers
        let mut selected = Vec::with_capacity(opts.min_required_items);
        for _ in 0..opts.min_required_items {
            let idx = self
                .counter
                .fetch_add(1, std::sync::atomic::Ordering::Relaxed)
                % peers.len();
            selected.push(peers[idx].clone());
        }

        Ok(selected)
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::test_util::{create_selector_context, put_datanodes};

    #[tokio::test]
    async fn test_round_robin_selector() {
        let selector = RoundRobinSelector::default();
        let ctx = create_selector_context();
        // add three nodes
        let peer1 = Peer {
            id: 2,
            addr: "node1".to_string(),
        };
        let peer2 = Peer {
            id: 5,
            addr: "node2".to_string(),
        };
        let peer3 = Peer {
            id: 8,
            addr: "node3".to_string(),
        };
        let peers = vec![peer1.clone(), peer2.clone(), peer3.clone()];
        put_datanodes(&ctx.meta_peer_client, peers).await;

        let peers = selector
            .select(
                &ctx,
                SelectorOptions {
                    min_required_items: 4,
                    allow_duplication: true,
                },
            )
            .await
            .unwrap();
        assert_eq!(peers.len(), 4);
        assert_eq!(
            peers,
            vec![peer1.clone(), peer2.clone(), peer3.clone(), peer1.clone()]
        );

        let peers = selector
            .select(
                &ctx,
                SelectorOptions {
                    min_required_items: 2,
                    allow_duplication: true,
                },
            )
            .await
            .unwrap();
        assert_eq!(peers.len(), 2);
        assert_eq!(peers, vec![peer2.clone(), peer3.clone()]);
    }
}