Raft AppendEntries

medium · distributed-systems, consensus, raft

Raft AppendEntries

A leader sends an AppendEntries request containing:

  • prevLogIndex and prevLogTerm
  • a list of new entry terms to append

The follower should accept only if its log matches at prevLogIndex. If accepted, it must truncate any conflicting entries after prevLogIndex then append the new entries.

Log indices are 1-based. prevLogIndex == 0 always matches.

Function signature

func AppendEntries(logTerms []int, prevLogIndex int, prevLogTerm int, entries []int) (newLog []int, ok bool)

Example

logTerms = [1,1,2,2]
prevLogIndex = 2
prevLogTerm = 1
entries = [3,3]

output log = [1,1,3,3], ok = true

Rules

  • If prevLogIndex > len(logTerms), reject.
  • If prevLogIndex > 0 and logTerms[prevLogIndex-1] != prevLogTerm, reject.
  • Otherwise, accept: keep entries up to prevLogIndex, append entries.

Constraints

  • 0 <= prevLogIndex <= len(logTerms)
Run tests to see results
No issues detected