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
// 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, Ordering};
use std::sync::Arc;

use rand::Rng;
use snafu::ensure;

use crate::error::{EmptyTopicPoolSnafu, Result};

/// Controls topic selection.
pub(crate) trait TopicSelector: Send + Sync {
    /// Selects a topic from the topic pool.
    fn select<'a>(&self, topic_pool: &'a [String]) -> Result<&'a String>;
}

/// Arc wrapper of TopicSelector.
pub(crate) type TopicSelectorRef = Arc<dyn TopicSelector>;

/// A topic selector with the round-robin strategy, i.e. selects topics in a round-robin manner.
#[derive(Default)]
pub(crate) struct RoundRobinTopicSelector {
    cursor: AtomicUsize,
}

impl RoundRobinTopicSelector {
    // The cursor in the round-robin selector is not persisted which may break the round-robin strategy cross crashes.
    // Introducing a shuffling strategy may help mitigate this issue.
    pub fn with_shuffle() -> Self {
        let offset = rand::thread_rng().gen_range(0..64);
        Self {
            cursor: AtomicUsize::new(offset),
        }
    }
}

impl TopicSelector for RoundRobinTopicSelector {
    fn select<'a>(&self, topic_pool: &'a [String]) -> Result<&'a String> {
        ensure!(!topic_pool.is_empty(), EmptyTopicPoolSnafu);
        let which = self.cursor.fetch_add(1, Ordering::Relaxed) % topic_pool.len();
        Ok(&topic_pool[which])
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Tests that a selector behaves as expected when the given topic pool is empty.
    #[test]
    fn test_empty_topic_pool() {
        let topic_pool = vec![];
        let selector = RoundRobinTopicSelector::default();
        assert!(selector.select(&topic_pool).is_err());
    }

    #[test]
    fn test_round_robin_topic_selector() {
        let topic_pool: Vec<_> = [0, 1, 2].into_iter().map(|v| v.to_string()).collect();
        let selector = RoundRobinTopicSelector::default();

        assert_eq!(selector.select(&topic_pool).unwrap(), "0");
        assert_eq!(selector.select(&topic_pool).unwrap(), "1");
        assert_eq!(selector.select(&topic_pool).unwrap(), "2");
        assert_eq!(selector.select(&topic_pool).unwrap(), "0");

        // Creates a round-robin selector with shuffle.
        let selector = RoundRobinTopicSelector::with_shuffle();
        let topic = selector.select(&topic_pool).unwrap();
        assert!(topic_pool.contains(topic));
    }
}