Skip to content

Commit

Permalink
Try to campaign when starting and being a voter
Browse files Browse the repository at this point in the history
In order to avoid the delay of an election timeout when restarting a RaftMetadataServer,
they try to campaing immediately if they are part of the current configuration as voters.
This won't disturb an existing leader because we have pre-vote enabled.
  • Loading branch information
tillrohrmann committed Feb 6, 2025
1 parent 25d4990 commit 5b2b055
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions crates/metadata-server/src/raft/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,7 @@ impl RaftMetadataServer {
Ok(raft_configuration) => {
let _ = request.result_tx.send(Ok(true));
debug!(member_id = %raft_configuration.my_member_id, "Successfully provisioned the metadata store");
let mut member = self.become_member(raft_configuration)?;
member.campaign_immediately()?;
let member = self.become_member(raft_configuration)?;
break Provisioned::Member(member);
},
Err(err) => {
Expand Down Expand Up @@ -579,7 +578,19 @@ impl Member {
}

config.validate()?;
let raw_node = RawNode::new(&config, storage, &logger)?;
let mut raw_node = RawNode::new(&config, storage, &logger)?;

if raw_node
.raft
.prs()
.conf()
.voters()
.contains(to_raft_id(my_member_id.node_id))
{
// Campaign if we are part of the voters to quickly become leader if there is none. This
// won't cause the current leader to step down since pre-vote is enabled.
raw_node.campaign()?;
}

let mut tick_interval = time::interval(raft_options.raft_tick_interval.into());
tick_interval.set_missed_tick_behavior(MissedTickBehavior::Burst);
Expand Down Expand Up @@ -611,12 +622,6 @@ impl Member {
Ok(member)
}

/// Sets the Raft node up to start right away with a leader election.
pub fn campaign_immediately(&mut self) -> Result<(), Error> {
self.raw_node.campaign()?;
Ok(())
}

#[instrument(level = "info", skip_all, fields(member_id = %self.my_member_id))]
pub async fn run(mut self) -> Result<Standby, Error> {
info!(configuration = %self.configuration, "Run as member of the metadata cluster");
Expand Down Expand Up @@ -889,6 +894,7 @@ impl Member {
&mut self.configuration,
&mut self.kv_storage,
)?;
info!(configuration = %self.configuration, "Restored configuration from snapshot");

self.validate_metadata_server_configuration();

Expand All @@ -915,8 +921,6 @@ impl Member {
.map(MetadataServerConfiguration::from)
.expect("configuration metadata expected");

info!(%configuration, "Restored configuration from snapshot");

kv_storage.restore(snapshot)?;
Ok(())
}
Expand Down

0 comments on commit 5b2b055

Please sign in to comment.