ktoso / akka-raft

A toy project implementing RAFT on top of Akka Cluster (not prod ready)
http://blog.project13.pl
Apache License 2.0
280 stars 42 forks source link

Log consistency check is incorrect #59

Open colin-scott opened 9 years ago

colin-scott commented 9 years ago

The log consistency check is currently implemented as follows:

  /**                                                                  
   * Performs the "consistency check", which checks if the data that we just got from the
   */                                                                  
  def containsMatchingEntry(otherPrevTerm: Term, otherPrevIndex: Int): Boolean =
    (otherPrevTerm == Term(0) && otherPrevIndex == 0) || (lastTerm == otherPrevTerm && lastIndex == otherPrevIndex)

I don't think the second disjunct is correct. The Raft paper states that the log consistency check should fail "if log doesn’t contain an entry at prevLogIndex whose term matches prevLogTerm".

So I think the second disjunct should be:

(entries.isDefinedAt(otherPrevIndex - 1) && entries(otherPrevIndex - 1).term == otherPrevTerm)

(assuming 1-indexed log entries, following the raft paper)

colin-scott commented 9 years ago

For what it's worth, I have a (non-pull-request-worthy) fix to this issue here:

NetSys/sts2-applications@705f8de