heartbeats.rs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. use std::sync::atomic::Ordering;
  2. use std::time::Duration;
  3. use parking_lot::Mutex;
  4. use crate::term_marker::TermMarker;
  5. use crate::utils::{retry_rpc, RPC_DEADLINE};
  6. use crate::{AppendEntriesArgs, Raft, RaftState, RemoteRaft};
  7. // Command must be
  8. // 0. 'static: Raft<Command> must be 'static, it is moved to another thread.
  9. // 1. clone: they are copied to the persister.
  10. // 2. send: Arc<Mutex<Vec<LogEntry<Command>>>> must be send, it is moved to another thread.
  11. // 3. serialize: they are converted to bytes to persist.
  12. impl<Command> Raft<Command>
  13. where
  14. Command: 'static + Clone + Send + serde::Serialize,
  15. {
  16. /// Schedules tasks that send heartbeats to peers.
  17. ///
  18. /// One task is scheduled for each peer. The task sleeps for a duration
  19. /// specified by `interval`, wakes up, builds the request message to send
  20. /// and delegates the actual RPC-sending to another task before going back
  21. /// to sleep.
  22. ///
  23. /// The sleeping task does nothing if we are not the leader.
  24. ///
  25. /// The request message is a stripped down version of `AppendEntries`. The
  26. /// response from the peer is ignored.
  27. pub(crate) fn schedule_heartbeats(&self, interval: Duration) {
  28. for (peer_index, rpc_client) in self.peers.iter().enumerate() {
  29. if peer_index != self.me.0 {
  30. // rf is now owned by the outer async function.
  31. let rf = self.inner_state.clone();
  32. // A function that updates term with responses to heartbeats.
  33. let term_marker = self.term_marker();
  34. // RPC client must be cloned into the outer async function.
  35. let rpc_client = rpc_client.clone();
  36. // Shutdown signal.
  37. let keep_running = self.keep_running.clone();
  38. self.thread_pool.spawn(async move {
  39. let mut interval = tokio::time::interval(interval);
  40. while keep_running.load(Ordering::SeqCst) {
  41. interval.tick().await;
  42. if let Some(args) = Self::build_heartbeat(&rf) {
  43. tokio::spawn(Self::send_heartbeat(
  44. rpc_client.clone(),
  45. args,
  46. term_marker.clone(),
  47. ));
  48. }
  49. }
  50. });
  51. }
  52. }
  53. }
  54. fn build_heartbeat(
  55. rf: &Mutex<RaftState<Command>>,
  56. ) -> Option<AppendEntriesArgs<Command>> {
  57. let rf = rf.lock();
  58. if !rf.is_leader() {
  59. return None;
  60. }
  61. let last_log = rf.log.last_index_term();
  62. let args = AppendEntriesArgs {
  63. term: rf.current_term,
  64. leader_id: rf.leader_id,
  65. prev_log_index: last_log.index,
  66. prev_log_term: last_log.term,
  67. entries: vec![],
  68. leader_commit: rf.commit_index,
  69. };
  70. Some(args)
  71. }
  72. const HEARTBEAT_RETRY: usize = 1;
  73. async fn send_heartbeat(
  74. rpc_client: impl RemoteRaft<Command>,
  75. args: AppendEntriesArgs<Command>,
  76. term_watermark: TermMarker<Command>,
  77. ) -> std::io::Result<()> {
  78. // Passing a reference that is moved to the following closure.
  79. //
  80. // It won't work if the rpc_client of type Arc is moved into the closure
  81. // directly. To clone the Arc, the function must own a mutable reference
  82. // to it. At the same time, rpc_client.call_append_entries() returns a
  83. // future that must own a reference, too. That caused a compiling error
  84. // of FnMut allowing "references to captured variables to escape".
  85. //
  86. // By passing-in a reference instead of an Arc, the closure becomes a Fn
  87. // (no Mut), which can allow references to escape.
  88. //
  89. // Another option is to use non-move closures, in which case rpc_client
  90. // of type Arc can be passed-in directly. However that requires args to
  91. // be sync because they can be shared by more than one futures.
  92. let rpc_client = &rpc_client;
  93. let response =
  94. retry_rpc(Self::HEARTBEAT_RETRY, RPC_DEADLINE, move |_round| {
  95. rpc_client.append_entries(args.clone())
  96. })
  97. .await?;
  98. term_watermark.mark(response.term);
  99. Ok(())
  100. }
  101. }