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

Commit

Permalink
fix tabs to spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
ktoso committed Aug 8, 2015
1 parent e656196 commit da3ac83
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 199 deletions.
64 changes: 32 additions & 32 deletions src/main/scala/pl/project13/scala/akka/raft/Candidate.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,34 +32,34 @@ private[raft] trait Candidate {
stay() using includingThisVote.withVoteFor(m.currentTerm, m.clusterSelf)
}

case Event(msg: RequestVote, m: ElectionMeta) if msg.term < m.currentTerm =>
log.info("Rejecting RequestVote msg by {} in {}. Received stale {}.", candidate, m.currentTerm, msg.term)
candidate ! DeclineCandidate(m.currentTerm)
stay()
case Event(msg: RequestVote, m: ElectionMeta) if msg.term < m.currentTerm =>
log.info("Rejecting RequestVote msg by {} in {}. Received stale {}.", candidate, m.currentTerm, msg.term)
candidate ! DeclineCandidate(m.currentTerm)
stay()

case Event(msg: RequestVote, m: ElectionMeta) if msg.term > m.currentTerm =>
log.info("Received newer {}. Current term is {}. Revert to follower state.", msg.term, m.currentTerm)
goto(Follower) using m.forFollower(msg.term)
case Event(msg: RequestVote, m: ElectionMeta) if msg.term > m.currentTerm =>
log.info("Received newer {}. Current term is {}. Revert to follower state.", msg.term, m.currentTerm)
goto(Follower) using m.forFollower(msg.term)

case Event(msg: RequestVote, m: ElectionMeta) =>
if (m.canVoteIn(msg.term)) {
log.info("Voting for {} in {}.", candidate, m.currentTerm)
candidate ! VoteCandidate(m.currentTerm)
stay() using m.withVoteFor(m.currentTerm, candidate)
} else {
log.info("Rejecting RequestVote msg by {} in {}. Already voted for {}", candidate, m.currentTerm, m.votes.get(msg.term))
sender ! DeclineCandidate(m.currentTerm)
stay()
}

case Event(VoteCandidate(term), m: ElectionMeta) if term < m.currentTerm =>
log.info("Rejecting VoteCandidate msg by {} in {}. Received stale {}.", voter(), m.currentTerm, term)
voter ! DeclineCandidate(m.currentTerm)
stay()

case Event(VoteCandidate(term), m: ElectionMeta) if term > m.currentTerm =>
log.info("Received newer {}. Current term is {}. Revert to follower state.", term, m.currentTerm)
goto(Follower) using m.forFollower(term)
if (m.canVoteIn(msg.term)) {
log.info("Voting for {} in {}.", candidate, m.currentTerm)
candidate ! VoteCandidate(m.currentTerm)
stay() using m.withVoteFor(m.currentTerm, candidate)
} else {
log.info("Rejecting RequestVote msg by {} in {}. Already voted for {}", candidate, m.currentTerm, m.votes.get(msg.term))
sender ! DeclineCandidate(m.currentTerm)
stay()
}

case Event(VoteCandidate(term), m: ElectionMeta) if term < m.currentTerm =>
log.info("Rejecting VoteCandidate msg by {} in {}. Received stale {}.", voter(), m.currentTerm, term)
voter ! DeclineCandidate(m.currentTerm)
stay()

case Event(VoteCandidate(term), m: ElectionMeta) if term > m.currentTerm =>
log.info("Received newer {}. Current term is {}. Revert to follower state.", term, m.currentTerm)
goto(Follower) using m.forFollower(term)

case Event(VoteCandidate(term), m: ElectionMeta) =>
val includingThisVote = m.incVote
Expand All @@ -73,13 +73,13 @@ private[raft] trait Candidate {
}

case Event(DeclineCandidate(term), m: ElectionMeta) =>
if (term > m.currentTerm) {
log.info("Received newer {}. Current term is {}. Revert to follower state.", term, m.currentTerm)
goto(Follower) using m.forFollower(term)
} else {
log.info("Candidate is declined by {} in term {}", sender(), m.currentTerm)
stay()
}
if (term > m.currentTerm) {
log.info("Received newer {}. Current term is {}. Revert to follower state.", term, m.currentTerm)
goto(Follower) using m.forFollower(term)
} else {
log.info("Candidate is declined by {} in term {}", sender(), m.currentTerm)
stay()
}


// end of election
Expand Down
138 changes: 0 additions & 138 deletions src/main/scala/pl/project13/scala/akka/raft/Follower.scala

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ private[protocol] trait StateMetadata extends Serializable {

def withVote(term: Term, candidate: ActorRef) = {
if (term > currentTerm)
copy(currentTerm = term, votes = votes updated (term, candidate))
else
copy(votes = votes updated (term, candidate))
copy(currentTerm = term, votes = votes updated (term, candidate))
else
copy(votes = votes updated (term, candidate))
}

def withTerm(term: Term) = copy(currentTerm = term)
def withTerm(term: Term) = copy(currentTerm = term)

def withConfig(conf: ClusterConfiguration): Meta = copy(config = conf)
}
Expand Down
38 changes: 19 additions & 19 deletions src/test/scala/pl/project13/scala/akka/raft/CandidateTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -73,24 +73,24 @@ class CandidateTest extends RaftSpec with BeforeAndAfterEach
}
}

it should "reject candidate if got RequestVote message with a stale term number" in {
candidate.setState(Candidate, data)

candidate ! RequestVote(Term(1), self, Term(1), 1)
fishForMessage(max = 5 seconds) {
case DeclineCandidate(Term(3)) => true
case _ => false
}
}

it should "reject candidate if got VoteCandidate message with a stale term number" in {
candidate.setState(Candidate, data)

candidate ! VoteCandidate(Term(1))
fishForMessage(max = 5 seconds) {
case DeclineCandidate(Term(3)) => true
case _ => false
}
}
it should "reject candidate if got RequestVote message with a stale term number" in {
candidate.setState(Candidate, data)

candidate ! RequestVote(Term(1), self, Term(1), 1)
fishForMessage(max = 5 seconds) {
case DeclineCandidate(Term(3)) => true
case _ => false
}
}

it should "reject candidate if got VoteCandidate message with a stale term number" in {
candidate.setState(Candidate, data)

candidate ! VoteCandidate(Term(1))
fishForMessage(max = 5 seconds) {
case DeclineCandidate(Term(3)) => true
case _ => false
}
}

}
12 changes: 6 additions & 6 deletions src/test/scala/pl/project13/scala/akka/raft/FollowerTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ class FollowerTest extends RaftSpec with BeforeAndAfterEach
expectMsg(DeclineCandidate(Term(2)))
}

it should "update term number after getting a request with higher term number" in {
follower.setState(Follower, data)
it should "update term number after getting a request with higher term number" in {
follower.setState(Follower, data)

follower ! RequestVote(Term(3), self, Term(3), 3)
expectMsg(VoteCandidate(Term(3)))
follower ! RequestVote(Term(3), self, Term(3), 3)
expectMsg(VoteCandidate(Term(3)))

follower.stateData.currentTerm shouldBe Term(3)
}
follower.stateData.currentTerm shouldBe Term(3)
}

it should "become a Candidate if the electionTimeout has elapsed" in {
// given
Expand Down

0 comments on commit da3ac83

Please sign in to comment.