Skip to content
This repository has been archived by the owner on Nov 12, 2024. It is now read-only.

Raft 45 #70

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/main/scala/pl/project13/scala/akka/raft/Candidate.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ private[raft] trait Candidate {
val request = RequestVote(m.currentTerm, m.clusterSelf, replicatedLog.lastTerm, replicatedLog.lastIndex)
m.membersExceptSelf foreach { _ ! request }

val includingThisVote = m.incVote
val includingThisVote = m.incVote(self)
stay() using includingThisVote.withVoteFor(m.currentTerm, m.clusterSelf)
}

Expand Down Expand Up @@ -62,7 +62,7 @@ private[raft] trait Candidate {
goto(Follower) using m.forFollower(term)

case Event(VoteCandidate(term), m: ElectionMeta) =>
val includingThisVote = m.incVote
val includingThisVote = m.incVote(voter())

if (includingThisVote.hasMajority) {
log.info("Received vote by {}. Won election with {} of {} votes", voter(), includingThisVote.votesReceived, m.config.members.size)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private[protocol] trait StateMetadata extends Serializable {
) extends Metadata {

// transition helpers
def forNewElection: ElectionMeta = ElectionMeta(clusterSelf, currentTerm.next, 0, config, votes)
def forNewElection: ElectionMeta = ElectionMeta(clusterSelf, currentTerm.next, Set.empty, config, votes)

def withVote(term: Term, candidate: ActorRef) = {
if (term > currentTerm)
Expand All @@ -66,15 +66,16 @@ private[protocol] trait StateMetadata extends Serializable {
case class ElectionMeta(
clusterSelf: ActorRef,
currentTerm: Term,
votesReceived: Int,
votesReceived: Set[String],
config: ClusterConfiguration,
votes: Map[Term, Candidate]
) extends Metadata {

def hasMajority = votesReceived > config.members.size / 2
def hasMajority = votesReceived.size > config.members.size / 2

// transistion helpers
def incVote = copy(votesReceived = votesReceived + 1)
def incVote(voter: Candidate) = copy(
votesReceived = votesReceived + voter.path.name)
def incTerm = copy(currentTerm = currentTerm.next)

def withVoteFor(term: Term, candidate: ActorRef) = copy(votes = votes + (term -> candidate))
Expand Down