heartbeats.rs 4.3 KB

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