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
use super::election::LeaderElection;
use super::state::BrokerState;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
pub struct Heartbeat {
pub last_beat: Arc<Mutex<Instant>>,
pub timeout: Duration,
}
impl Heartbeat {
/// Creates a new heartbeat instance.
///
/// # Arguments
///
/// * `timeout` - The duration after which the heartbeat times out.
///
/// # Examples
///
/// ```
/// use rust_kafka_like::broker::leader::heartbeat::Heartbeat;
/// use std::time::Duration;
///
/// let heartbeat = Heartbeat::new(Duration::from_secs(1));
/// assert!(heartbeat.last_beat.lock().unwrap().elapsed() < Duration::from_secs(1));
/// ```
pub fn new(timeout: Duration) -> Self {
Heartbeat {
last_beat: Arc::new(Mutex::new(Instant::now())),
timeout,
}
}
/// Starts the heartbeat mechanism.
///
/// # Arguments
///
/// * `election` - The leader election instance.
///
/// # Examples
///
/// ```
/// use rust_kafka_like::broker::leader::heartbeat::Heartbeat;
/// use rust_kafka_like::broker::leader::election::LeaderElection;
/// use std::collections::HashMap;
/// use std::time::Duration;
///
/// let peers = HashMap::new();
/// let election = LeaderElection::new("broker1", peers);
/// Heartbeat::start(election);
/// ```
pub fn start(election: LeaderElection) {
let heartbeat = Arc::new(Self::new(Duration::from_secs(1)));
let election = Arc::new(election);
// Heartbeat Transmission Thread
let send_election = election.clone();
let send_heartbeat = heartbeat.clone();
std::thread::spawn(move || {
while *send_election.state.lock().unwrap() == BrokerState::Leader {
Self::send_heartbeat(&send_election);
*send_heartbeat.last_beat.lock().unwrap() = Instant::now();
std::thread::sleep(Duration::from_millis(500));
}
});
// Heartbeat monitoring thread
let monitor_election = election.clone();
let monitor_heartbeat = heartbeat.clone();
std::thread::spawn(move || {
while *monitor_election.state.lock().unwrap() != BrokerState::Leader {
if Self::check_timeout(&monitor_heartbeat) {
println!("Heartbeat timeout, starting election");
monitor_election.start_election();
}
std::thread::sleep(Duration::from_millis(100));
}
});
}
/// Sends a heartbeat to the peers.
///
/// # Arguments
///
/// * `election` - The leader election instance.
///
/// # Examples
///
/// ```
/// use rust_kafka_like::broker::leader::heartbeat::Heartbeat;
/// use rust_kafka_like::broker::leader::election::LeaderElection;
/// use std::collections::HashMap;
///
/// let peers = HashMap::new();
/// let election = LeaderElection::new("broker1", peers);
/// Heartbeat::send_heartbeat(&election);
/// ```
pub fn send_heartbeat(election: &LeaderElection) {
let peers = election.peers.lock().unwrap();
for (peer_id, _) in peers.iter() {
println!("Sending heartbeat to peer: {}", peer_id);
// TODO Implementing actual network communication here
}
}
fn check_timeout(&self) -> bool {
let last = *self.last_beat.lock().unwrap();
last.elapsed() > self.timeout
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_heartbeat_timeout() {
let heartbeat = Heartbeat::new(Duration::from_millis(100));
std::thread::sleep(Duration::from_millis(150));
assert!(heartbeat.check_timeout());
}
#[test]
fn test_heartbeat_within_timeout() {
let heartbeat = Heartbeat::new(Duration::from_millis(100));
assert!(!heartbeat.check_timeout());
}
}