heartbeats.rs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. use std::pin::pin;
  2. use std::sync::atomic::{AtomicU64, Ordering};
  3. use std::sync::Arc;
  4. use std::time::{Duration, Instant};
  5. use parking_lot::Mutex;
  6. use crate::remote::RemoteContext;
  7. use crate::utils::{retry_rpc, RPC_DEADLINE};
  8. use crate::{AppendEntriesArgs, Peer, Raft, RaftState, ReplicableCommand};
  9. pub(crate) const HEARTBEAT_INTERVAL: Duration = Duration::from_millis(150);
  10. #[derive(Clone)]
  11. pub(crate) struct HeartbeatsDaemon {
  12. start: Instant,
  13. last_trigger: Arc<AtomicU64>,
  14. sender: tokio::sync::broadcast::Sender<()>,
  15. }
  16. impl HeartbeatsDaemon {
  17. const HEARTBEAT_MAX_DELAY_MILLIS: u64 = 30;
  18. pub fn create() -> Self {
  19. let (sender, _) = tokio::sync::broadcast::channel(1);
  20. Self {
  21. start: Instant::now(),
  22. last_trigger: Arc::new(AtomicU64::new(0)),
  23. sender,
  24. }
  25. }
  26. pub fn trigger(&self, force: bool) {
  27. let now = self.start.elapsed().as_millis();
  28. // u64 is big enough for more than 500 million years.
  29. let now_lower_bits = (now & (u64::MAX) as u128) as u64;
  30. let last_trigger = self.last_trigger.load(Ordering::Acquire);
  31. let next_trigger =
  32. last_trigger.wrapping_add(Self::HEARTBEAT_MAX_DELAY_MILLIS);
  33. // Do not trigger heartbeats too frequently, unless we are forced.
  34. if force || next_trigger < now_lower_bits {
  35. let previous_trigger = self
  36. .last_trigger
  37. .fetch_max(now_lower_bits, Ordering::AcqRel);
  38. if last_trigger == previous_trigger {
  39. let _ = self.sender.send(());
  40. }
  41. }
  42. }
  43. }
  44. // Command must be
  45. // 0. 'static: Raft<Command> must be 'static, it is moved to another thread.
  46. // 1. clone: they are copied to the persister.
  47. // 2. send: Arc<Mutex<Vec<LogEntry<Command>>>> must be send, it is moved to another thread.
  48. // 3. serialize: they are converted to bytes to persist.
  49. impl<Command: ReplicableCommand> Raft<Command> {
  50. /// Schedules tasks that send heartbeats to peers.
  51. ///
  52. /// One task is scheduled for each peer. The task sleeps for a duration
  53. /// specified by `interval`, wakes up, builds the request message to send
  54. /// and delegates the actual RPC-sending to another task before going back
  55. /// to sleep.
  56. ///
  57. /// The sleeping task does nothing if we are not the leader.
  58. ///
  59. /// The request message is a stripped down version of `AppendEntries`. The
  60. /// response from the peer is ignored.
  61. pub(crate) fn schedule_heartbeats(&self, interval: Duration) {
  62. // rf is now owned by the outer async function.
  63. let rf = self.inner_state.clone();
  64. // A on-demand trigger to sending a heartbeat.
  65. let mut trigger = self.heartbeats_daemon.sender.subscribe();
  66. // Shutdown signal.
  67. let keep_running = self.keep_running.clone();
  68. let peers = self.peers.clone();
  69. self.thread_pool.spawn(async move {
  70. let mut interval = tokio::time::interval(interval);
  71. while keep_running.load(Ordering::Relaxed) {
  72. let tick = pin!(interval.tick());
  73. let trigger = pin!(trigger.recv());
  74. let _ = futures_util::future::select(tick, trigger).await;
  75. if let Some(args) = Self::build_heartbeat(&rf) {
  76. for peer in &peers {
  77. tokio::spawn(Self::send_heartbeat(*peer, args.clone()));
  78. }
  79. }
  80. }
  81. });
  82. }
  83. fn build_heartbeat(
  84. rf: &Mutex<RaftState<Command>>,
  85. ) -> Option<AppendEntriesArgs<Command>> {
  86. let rf = rf.lock();
  87. if !rf.is_leader() {
  88. return None;
  89. }
  90. let last_log = rf.log.last_index_term();
  91. let args = AppendEntriesArgs {
  92. term: rf.current_term,
  93. leader_id: rf.leader_id,
  94. prev_log_index: last_log.index,
  95. prev_log_term: last_log.term,
  96. entries: vec![],
  97. leader_commit: rf.commit_index,
  98. };
  99. Some(args)
  100. }
  101. const HEARTBEAT_RETRY: usize = 1;
  102. async fn send_heartbeat(
  103. peer: Peer,
  104. args: AppendEntriesArgs<Command>,
  105. ) -> std::io::Result<()> {
  106. let term = args.term;
  107. let beat_ticker = RemoteContext::<Command>::beat_ticker(peer);
  108. let beat = beat_ticker.next_beat();
  109. // Passing a reference that is moved to the following closure.
  110. //
  111. // It won't work if the rpc_client of type Arc is moved into the closure
  112. // directly. To clone the Arc, the function must own a mutable reference
  113. // to it. At the same time, rpc_client.call_append_entries() returns a
  114. // future that must own a reference, too. That caused a compiling error
  115. // of FnMut allowing "references to captured variables to escape".
  116. //
  117. // By passing-in a reference instead of an Arc, the closure becomes a Fn
  118. // (no Mut), which can allow references to escape.
  119. //
  120. // Another option is to use non-move closures, in which case rpc_client
  121. // of type Arc can be passed-in directly. However that requires args to
  122. // be sync because they can be shared by more than one futures.
  123. let rpc_client = RemoteContext::<Command>::rpc_client(peer);
  124. let response =
  125. retry_rpc(Self::HEARTBEAT_RETRY, RPC_DEADLINE, move |_round| {
  126. rpc_client.append_entries(args.clone())
  127. })
  128. .await?;
  129. if term == response.term {
  130. beat_ticker.tick(beat);
  131. } else {
  132. RemoteContext::<Command>::term_marker().mark(response.term);
  133. }
  134. Ok(())
  135. }
  136. }