Skip to content

Commit

Permalink
raft: immediately commit and apply first entry on single-node clusters
Browse files Browse the repository at this point in the history
  • Loading branch information
erikgrinaker committed Jun 7, 2024
1 parent 7e1d562 commit 9a9e022
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/raft/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ impl RawNode<Candidate> {
// We do this prior to the heartbeat, to avoid a wasted replication
// roundtrip if the heartbeat response indicates the peer is behind.
node.propose(None)?;
node.maybe_commit_and_apply()?;
node.heartbeat()?;

Ok(node)
Expand Down Expand Up @@ -1505,6 +1506,8 @@ mod tests {
};
self.nodes.insert(id, Node::new(id, peers, log, state, node_tx, opts)?);

while applied_rx.try_recv().is_ok() {} // drain first apply

self.nodes_rx.insert(id, node_rx);
self.nodes_pending.insert(id, Vec::new());
self.applied_rx.insert(id, applied_rx);
Expand Down
3 changes: 1 addition & 2 deletions src/raft/testscripts/node/request_leader_single
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

cluster nodes=1
---
n1@1 leader last=1@1 commit=0@0 apply=0 progress={}
n1@1 leader last=1@1 commit=1@1 apply=1 progress={}

# An initial get on the leader yields None.
get 1 foo
Expand All @@ -19,7 +19,6 @@ stabilize heartbeat=true
c1@1 → n1 ClientRequest id=0x02 write 0x0103666f6f03626172
n1@1 append 2@1 put foo=bar
n1@1 commit 2@1
n1@1 apply 1@1 None
n1@1 apply 2@1 put foo=bar
n1@1 → c1 ClientResponse id=0x02 write 0x0102
c1@1 put foo=bar ⇒ 2
Expand Down
2 changes: 1 addition & 1 deletion src/raft/testscripts/node/request_status_single
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

cluster nodes=1
---
n1@1 leader last=1@1 commit=0@0 apply=0 progress={}
n1@1 leader last=1@1 commit=1@1 apply=1 progress={}

# Perform a write.
(put 1 foo=bar)
Expand Down

0 comments on commit 9a9e022

Please sign in to comment.