/
Candidate.scala
181 lines (159 loc) · 8.39 KB
/
Candidate.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package lerna.akka.entityreplication.raft
import lerna.akka.entityreplication.raft.RaftProtocol._
import lerna.akka.entityreplication.raft.protocol.RaftCommands._
import lerna.akka.entityreplication.raft.protocol.{ FetchEntityEvents, SuspendEntity, TryCreateEntity }
import lerna.akka.entityreplication.raft.snapshot.SnapshotProtocol
import lerna.akka.entityreplication.raft.snapshot.sync.SnapshotSyncManager
private[raft] trait Candidate { this: RaftActor =>
import RaftActor._
def candidateBehavior: Receive = {
case ElectionTimeout =>
if (log.isInfoEnabled)
log.info("[Candidate] Election timeout at {}. Retrying leader election.", currentData.currentTerm)
val newTerm = currentData.currentTerm.next()
cancelElectionTimeoutTimer()
broadcast(
RequestVote(
shardId,
newTerm,
selfMemberIndex,
currentData.replicatedLog.lastLogIndex,
currentData.replicatedLog.lastLogTerm,
),
) // TODO: 永続化前に broadcast して問題ないか調べる
applyDomainEvent(BegunNewTerm(newTerm)) { _ =>
become(Candidate)
}
case request: RequestVote => receiveRequestVote(request)
case response: RequestVoteResponse => receiveRequestVoteResponse(response)
case request: AppendEntries => receiveAppendEntries(request)
case request: InstallSnapshot => receiveInstallSnapshot(request)
case _: InstallSnapshotResponse => // ignore, because I'm not a leader
case response: SnapshotSyncManager.Response => receiveSyncSnapshotResponse(response)
case command: Command => handleCommand(command)
case _: ForwardedCommand => // ignore, because I'm not a leader
case TryCreateEntity(_, entityId) => createEntityIfNotExists(entityId)
case request: FetchEntityEvents => receiveFetchEntityEvents(request)
case EntityTerminated(id) => receiveEntityTerminated(id)
case SuspendEntity(_, entityId, stopMessage) => suspendEntity(entityId, stopMessage)
case SnapshotTick => handleSnapshotTick()
case response: Snapshot => receiveEntitySnapshotResponse(response)
case response: SnapshotProtocol.SaveSnapshotResponse => receiveSaveSnapshotResponse(response)
case _: akka.persistence.SaveSnapshotSuccess => // ignore
case _: akka.persistence.SaveSnapshotFailure => // ignore: no problem because events exist even if snapshot saving failed
}
private[this] def receiveRequestVote(request: RequestVote): Unit =
request match {
case RequestVote(_, term, candidate, _, _) if term == currentData.currentTerm && candidate == selfMemberIndex =>
if (log.isDebugEnabled) log.debug("=== [Candidate] accept self RequestVote ===")
applyDomainEvent(Voted(term, selfMemberIndex)) { _ =>
sender() ! RequestVoteAccepted(term, selfMemberIndex)
}
case RequestVote(_, term, otherCandidate, lastLogIndex, lastLogTerm)
if term.isNewerThan(
currentData.currentTerm,
) && lastLogTerm >= currentData.replicatedLog.lastLogTerm && lastLogIndex >= currentData.replicatedLog.lastLogIndex =>
if (log.isDebugEnabled) log.debug("=== [Candidate] accept RequestVote({}, {}) ===", term, otherCandidate)
cancelElectionTimeoutTimer()
applyDomainEvent(Voted(term, otherCandidate)) { domainEvent =>
sender() ! RequestVoteAccepted(domainEvent.term, selfMemberIndex)
become(Follower)
}
case request: RequestVote =>
if (log.isDebugEnabled) log.debug("=== [Candidate] deny {} ===", request)
if (request.term.isNewerThan(currentData.currentTerm)) {
cancelElectionTimeoutTimer()
applyDomainEvent(DetectedNewTerm(request.term)) { _ =>
sender() ! RequestVoteDenied(currentData.currentTerm)
become(Follower)
}
} else {
// the request has the same or old term
sender() ! RequestVoteDenied(currentData.currentTerm)
}
}
private[this] def receiveRequestVoteResponse(response: RequestVoteResponse): Unit =
response match {
case accepted: RequestVoteAccepted if accepted.term.isNewerThan(currentData.currentTerm) =>
unhandled(accepted) // TODO: 不具合の可能性が高いのでエラーとして報告
case accepted: RequestVoteAccepted if accepted.term == currentData.currentTerm =>
cancelElectionTimeoutTimer()
applyDomainEvent(AcceptedRequestVote(accepted.sender)) { _ =>
if (log.isDebugEnabled) log.debug("=== [Candidate] accept for {} ===", accepted.sender)
if (currentData.gotAcceptionMajorityOf(numberOfMembers)) {
become(Leader)
} else {
resetElectionTimeoutTimer()
}
}
case accepted: RequestVoteAccepted if accepted.term.isOlderThan(currentData.currentTerm) =>
// ignore
case accepted: RequestVoteAccepted =>
unhandled(accepted)
case denied: RequestVoteDenied if denied.term.isNewerThan(currentData.currentTerm) =>
cancelElectionTimeoutTimer()
applyDomainEvent(DetectedNewTerm(denied.term)) { _ =>
become(Follower)
}
case denied: RequestVoteDenied if denied.term == currentData.currentTerm => // ignore
case denied: RequestVoteDenied if denied.term.isOlderThan(currentData.currentTerm) =>
// lastLogIndex が古かった場合、RequestVote が拒否される
// 1つの Follower から拒否されたからといって Leader になれないとも限らないため単純に無視する
case denied: RequestVoteDenied =>
unhandled(denied)
}
private[this] def receiveAppendEntries(request: AppendEntries): Unit =
request match {
case appendEntries: AppendEntries if appendEntries.term.isOlderThan(currentData.currentTerm) =>
sender() ! AppendEntriesFailed(currentData.currentTerm, selfMemberIndex)
case appendEntries: AppendEntries =>
if (currentData.hasMatchLogEntry(appendEntries.prevLogIndex, appendEntries.prevLogTerm)) {
if (log.isDebugEnabled) log.debug("=== [Candidate] append {} ===", appendEntries)
cancelElectionTimeoutTimer()
if (appendEntries.entries.isEmpty && appendEntries.term == currentData.currentTerm) {
// do not persist event when no need
applyDomainEvent(FollowedLeaderCommit(appendEntries.leader, appendEntries.leaderCommit)) { _ =>
sender() ! AppendEntriesSucceeded(
appendEntries.term,
currentData.replicatedLog.lastLogIndex,
selfMemberIndex,
)
become(Follower)
}
} else {
applyDomainEvent(AppendedEntries(appendEntries.term, appendEntries.entries, appendEntries.prevLogIndex)) {
domainEvent =>
applyDomainEvent(FollowedLeaderCommit(appendEntries.leader, appendEntries.leaderCommit)) { _ =>
sender() ! AppendEntriesSucceeded(
domainEvent.term,
currentData.replicatedLog.lastLogIndex,
selfMemberIndex,
)
become(Follower)
}
}
}
} else { // prevLogIndex と prevLogTerm がマッチするエントリが無かった
if (log.isDebugEnabled) log.debug("=== [Candidate] could not append {} ===", appendEntries)
cancelElectionTimeoutTimer()
if (appendEntries.term == currentData.currentTerm) {
applyDomainEvent(DetectedLeaderMember(appendEntries.leader)) { _ =>
sender() ! AppendEntriesFailed(currentData.currentTerm, selfMemberIndex)
become(Follower)
}
} else {
applyDomainEvent(DetectedNewTerm(appendEntries.term)) { domainEvent =>
applyDomainEvent(DetectedLeaderMember(appendEntries.leader)) { _ =>
sender() ! AppendEntriesFailed(domainEvent.term, selfMemberIndex)
become(Follower)
}
}
}
}
}
private[this] def handleCommand(command: Command): Unit =
command match {
case _ =>
stash()
}
}