-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Leader.java
286 lines (248 loc) · 11.4 KB
/
Leader.java
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
* Copyright (c) 2002-2017 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.causalclustering.core.consensus.roles;
import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.neo4j.causalclustering.core.consensus.Followers;
import org.neo4j.causalclustering.core.consensus.RaftMessageHandler;
import org.neo4j.causalclustering.core.consensus.RaftMessages;
import org.neo4j.causalclustering.core.consensus.RaftMessages.Heartbeat;
import org.neo4j.causalclustering.core.consensus.RaftMessages.LogCompactionInfo;
import org.neo4j.causalclustering.core.consensus.outcome.Outcome;
import org.neo4j.causalclustering.core.consensus.outcome.ShipCommand;
import org.neo4j.causalclustering.core.consensus.roles.follower.FollowerState;
import org.neo4j.causalclustering.core.consensus.roles.follower.FollowerStates;
import org.neo4j.causalclustering.core.consensus.state.ReadableRaftState;
import org.neo4j.causalclustering.core.replication.ReplicatedContent;
import org.neo4j.causalclustering.identity.MemberId;
import org.neo4j.helpers.collection.FilteringIterable;
import org.neo4j.logging.Log;
import static java.lang.Math.max;
import static org.neo4j.causalclustering.core.consensus.MajorityIncludingSelfQuorum.isQuorum;
import static org.neo4j.causalclustering.core.consensus.roles.Role.FOLLOWER;
import static org.neo4j.causalclustering.core.consensus.roles.Role.LEADER;
public class Leader implements RaftMessageHandler
{
private final Set<MemberId> heartbeatResponses = Collections.synchronizedSet( new HashSet<>() );
private boolean receivedHeartbeats = false;
private static Iterable<MemberId> replicationTargets( final ReadableRaftState ctx )
{
return new FilteringIterable<>( ctx.replicationMembers(), member -> !member.equals( ctx.myself() ) );
}
private static void sendHeartbeats( ReadableRaftState ctx, Outcome outcome ) throws IOException
{
long commitIndex = ctx.commitIndex();
long commitIndexTerm = ctx.entryLog().readEntryTerm( commitIndex );
Heartbeat heartbeat = new Heartbeat( ctx.myself(), ctx.term(), commitIndex, commitIndexTerm );
for ( MemberId to : replicationTargets( ctx ) )
{
outcome.addOutgoingMessage( new RaftMessages.Directed( to, heartbeat ) );
}
}
@Override
public Outcome handle( RaftMessages.RaftMessage message, ReadableRaftState ctx, Log log ) throws IOException
{
Outcome outcome = new Outcome( LEADER, ctx );
switch ( message.type() )
{
case HEARTBEAT:
{
Heartbeat req = (Heartbeat) message;
if ( req.leaderTerm() < ctx.term() )
{
break;
}
stepDownToFollower( outcome );
log.info( "Moving to FOLLOWER state after receiving heartbeat at term %d (my term is " + "%d) from %s",
req.leaderTerm(), ctx.term(), req.from() );
Heart.beat( ctx, outcome, (Heartbeat) message, log );
break;
}
case HEARTBEAT_TIMEOUT:
{
sendHeartbeats( ctx, outcome );
break;
}
case HEARTBEAT_RESPONSE:
{
receivedHeartbeats = true;
heartbeatResponses.add( message.from() );
break;
}
case ELECTION_TIMEOUT:
{
if ( !isQuorum( ctx.votingMembers().size(), heartbeatResponses.size() ) && receivedHeartbeats )
{
stepDownToFollower( outcome );
log.info( "Moving to FOLLOWER state after not receiving heartbeat responses in this election timeout " +
"period. Heartbeats received: %s", heartbeatResponses );
}
else
{
heartbeatResponses.clear();
}
break;
}
case APPEND_ENTRIES_REQUEST:
{
RaftMessages.AppendEntries.Request req = (RaftMessages.AppendEntries.Request) message;
if ( req.leaderTerm() < ctx.term() )
{
RaftMessages.AppendEntries.Response appendResponse =
new RaftMessages.AppendEntries.Response( ctx.myself(), ctx.term(), false, -1,
ctx.entryLog().appendIndex() );
outcome.addOutgoingMessage( new RaftMessages.Directed( req.from(), appendResponse ) );
break;
}
else if ( req.leaderTerm() == ctx.term() )
{
throw new IllegalStateException( "Two leaders in the same term." );
}
else
{
// There is a new leader in a later term, we should revert to follower. (§5.1)
stepDownToFollower( outcome );
log.info( "Moving to FOLLOWER state after receiving append request at term %d (my term is " +
"%d) from %s", req.leaderTerm(), ctx.term(), req.from() );
Appending.handleAppendEntriesRequest( ctx, outcome, req, log );
break;
}
}
case APPEND_ENTRIES_RESPONSE:
{
RaftMessages.AppendEntries.Response response = (RaftMessages.AppendEntries.Response) message;
if ( response.term() < ctx.term() )
{
/* Ignore responses from old terms! */
break;
}
else if ( response.term() > ctx.term() )
{
outcome.setNextTerm( response.term() );
stepDownToFollower( outcome );
log.info( "Moving to FOLLOWER state after receiving append response at term %d (my term is " +
"%d) from %s", response.term(), ctx.term(), response.from() );
outcome.replaceFollowerStates( new FollowerStates<>() );
break;
}
FollowerState follower = ctx.followerStates().get( response.from() );
if ( response.success() )
{
assert response.matchIndex() <= ctx.entryLog().appendIndex();
boolean followerProgressed = response.matchIndex() > follower.getMatchIndex();
outcome.replaceFollowerStates( outcome.getFollowerStates()
.onSuccessResponse( response.from(), max( response.matchIndex(), follower.getMatchIndex() ) ) );
outcome.addShipCommand( new ShipCommand.Match( response.matchIndex(), response.from() ) );
/*
* Matches from older terms can in complicated leadership change / log truncation scenarios
* be overwritten, even if they were replicated to a majority of instances. Thus we must only
* consider matches from this leader's term when figuring out which have been safely replicated
* and are ready for commit.
* This is explained nicely in Figure 3.7 of the thesis
*/
boolean matchInCurrentTerm = ctx.entryLog().readEntryTerm( response.matchIndex() ) == ctx.term();
/*
* The quorum situation may have changed only if the follower actually progressed.
*/
if ( followerProgressed && matchInCurrentTerm )
{
// TODO: Test that mismatch between voting and participating members affects commit outcome
long quorumAppendIndex =
Followers.quorumAppendIndex( ctx.votingMembers(), outcome.getFollowerStates() );
if ( quorumAppendIndex > ctx.commitIndex() )
{
outcome.setLeaderCommit( quorumAppendIndex );
outcome.setCommitIndex( quorumAppendIndex );
outcome.addShipCommand( new ShipCommand.CommitUpdate() );
}
}
}
else // Response indicated failure.
{
if ( response.appendIndex() > -1 && response.appendIndex() >= ctx.entryLog().prevIndex() )
{
// Signal a mismatch to the log shipper, which will serve an earlier entry.
outcome.addShipCommand( new ShipCommand.Mismatch( response.appendIndex(), response.from() ) );
}
else
{
// There are no earlier entries, message the follower that we have compacted so that
// it can take appropriate action.
LogCompactionInfo compactionInfo =
new LogCompactionInfo( ctx.myself(), ctx.term(), ctx.entryLog().prevIndex() );
RaftMessages.Directed directedCompactionInfo =
new RaftMessages.Directed( response.from(), compactionInfo );
outcome.addOutgoingMessage( directedCompactionInfo );
}
}
break;
}
case VOTE_REQUEST:
{
RaftMessages.Vote.Request req = (RaftMessages.Vote.Request) message;
if ( req.term() > ctx.term() )
{
stepDownToFollower( outcome );
log.info(
"Moving to FOLLOWER state after receiving vote request at term %d (my term is " + "%d) from %s",
req.term(), ctx.term(), req.from() );
Voting.handleVoteRequest( ctx, outcome, req );
break;
}
outcome.addOutgoingMessage( new RaftMessages.Directed( req.from(),
new RaftMessages.Vote.Response( ctx.myself(), ctx.term(), false ) ) );
break;
}
case NEW_ENTRY_REQUEST:
{
RaftMessages.NewEntry.Request req = (RaftMessages.NewEntry.Request) message;
ReplicatedContent content = req.content();
Appending.appendNewEntry( ctx, outcome, content );
break;
}
case NEW_BATCH_REQUEST:
{
RaftMessages.NewEntry.BatchRequest req = (RaftMessages.NewEntry.BatchRequest) message;
List<ReplicatedContent> contents = req.contents();
Appending.appendNewEntries( ctx, outcome, contents );
break;
}
case PRUNE_REQUEST:
{
Pruning.handlePruneRequest( outcome, (RaftMessages.PruneRequest) message );
break;
}
default:
break;
}
return outcome;
}
private void stepDownToFollower( Outcome outcome )
{
heartbeatResponses.clear();
receivedHeartbeats = false;
outcome.steppingDown();
outcome.setNextRole( FOLLOWER );
outcome.setLeader( null );
}
}