-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
ElectionState.java
369 lines (335 loc) · 19.8 KB
/
ElectionState.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
* Copyright (c) 2002-2018 "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.cluster.protocol.election;
import java.net.URI;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.neo4j.cluster.InstanceId;
import org.neo4j.cluster.com.message.Message;
import org.neo4j.cluster.com.message.MessageHolder;
import org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.AtomicBroadcastMessage;
import org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.ProposerMessage;
import org.neo4j.cluster.protocol.cluster.ClusterMessage;
import org.neo4j.cluster.statemachine.State;
import org.neo4j.helpers.collection.Iterables;
import org.neo4j.logging.Log;
import static org.neo4j.helpers.collection.Iterables.firstOrNull;
/**
* State machine that implements the {@link Election} API.
*/
public enum ElectionState
implements State<ElectionContext, ElectionMessage>
{
start
{
@Override
public State<?, ?> handle( ElectionContext context,
Message<ElectionMessage> message,
MessageHolder outgoing
)
{
if ( message.getMessageType() == ElectionMessage.created )
{
context.created();
return election;
}
else if ( message.getMessageType() == ElectionMessage.join )
{
return election;
}
return this;
}
},
election
{
@Override
public State<?, ?> handle( ElectionContext context,
Message<ElectionMessage> message,
MessageHolder outgoing
)
{
Log log = context.getLog( ElectionState.class );
switch ( message.getMessageType() )
{
case demote:
{
if ( !context.electionOk() )
{
log.warn( "Context says election is not OK to proceed. " +
"Failed instances are: " +
context.getFailed() +
", cluster members are: " +
context.getMembers() );
break;
}
InstanceId demoteNode = message.getPayload();
// TODO Could perhaps be done better?
context.nodeFailed( demoteNode );
if ( context.isInCluster() )
{
// Only the first alive server should try elections. Everyone else waits
List<InstanceId> aliveInstances = Iterables.asList(context.getAlive());
Collections.sort( aliveInstances );
boolean isElector = aliveInstances.indexOf( context.getMyId() ) == 0;
if ( isElector )
{
log.debug( "I (" + context.getMyId() +
") am the elector, executing the election" );
// Start election process for all roles that are currently unassigned
Iterable<String> rolesRequiringElection = context.getRolesRequiringElection();
for ( String role : rolesRequiringElection )
{
if ( !context.isElectionProcessInProgress( role ) )
{
log.debug( "Starting election process for role " + role );
context.startElectionProcess( role );
// Allow other live nodes to vote which one should take over
for ( Map.Entry<InstanceId, URI> server : context.getMembers().entrySet() )
{
if ( !context.getFailed().contains( server.getKey() ) )
{
// This is a candidate - allow it to vote itself for promotion
outgoing.offer( Message.to( ElectionMessage.vote, server.getValue(),
context.voteRequestForRole( new ElectionRole( role ) ) ) );
}
}
context.setTimeout( "election-" + role,
Message.timeout( ElectionMessage.electionTimeout, message,
new ElectionTimeoutData( role, message ) ) );
}
else
{
log.debug( "Election already in progress for role " + role );
}
}
}
}
break;
}
case performRoleElections:
{
if ( !context.electionOk() )
{
log.warn( "Context says election is not OK to proceed. " +
"Failed instances are: " +
context.getFailed() +
", cluster members are: " +
context.getMembers() );
break;
}
if ( context.isInCluster() )
{
boolean isElector = context.isElector();
if ( isElector )
{
context.getLog( ElectionState.class ).info( "I am the elector, doing election..." );
// Start election process for all roles
Iterable<ElectionRole> rolesRequiringElection = context.getPossibleRoles();
for ( ElectionRole role : rolesRequiringElection )
{
String roleName = role.getName();
if ( !context.isElectionProcessInProgress( roleName ) )
{
context.getLog( ElectionState.class ).debug(
"Starting election process for role " + roleName );
context.startElectionProcess( roleName );
boolean sentSome = false;
// Allow other live nodes to vote which one should take over
for ( Map.Entry<InstanceId, URI> server : context.getMembers().entrySet() )
{
/*
* Skip dead nodes and the current role holder. Dead nodes are not
* candidates anyway and the current role holder will be asked last,
* after everyone else has cast votes.
*/
if ( !context.isFailed( server.getKey() ) &&
!server.getKey().equals( context.getElected( roleName ) ) )
{
// This is a candidate - allow it to vote itself for promotion
outgoing.offer( Message.to( ElectionMessage.vote,
server.getValue(), context.voteRequestForRole( role ) ) );
sentSome = true;
}
}
if ( !sentSome )
{
/*
* If we didn't send any messages, we are the only non-failed cluster
* member and probably (not necessarily) hold the role, though that
* doesn't matter. So we ask ourselves to vote, if we didn't above.
* In this case, no timeout is required, because no messages are
* expected. If we are indeed the role holder, then we'll cast our
* vote as a response to this message, which will complete the election.
*/
outgoing.offer( Message.internal( ElectionMessage.vote,
context.voteRequestForRole( new ElectionRole( roleName ) ) ) );
}
else
{
context.setTimeout( "election-" + roleName,
Message.timeout( ElectionMessage.electionTimeout, message,
new ElectionTimeoutData( roleName, message ) ) );
}
}
else
{
log.debug( "Election already in progress for role " + roleName );
}
}
}
else
{
/*
* We take alive instances as determined by suspicions and remove those that are
* marked as failed in the failed set. This is done so that an instance which
* just joined can use the failed set provided in the configuration response to
* correctly determine the instances that are failed and skip them.
* Basically, this is to solve an issue where if an instance joins and is the
* lowest numbered alive but not overall will not try to get the failed lower
* numbered one to do elections.
*/
Set<InstanceId> aliveInstances = Iterables.asSet( context.getAlive() );
aliveInstances.removeAll( context.getFailed() );
List<InstanceId> adjustedAlive = Iterables.asList( aliveInstances );
Collections.sort( adjustedAlive );
context.getLog( ElectionState.class ).info( "I am NOT the elector, sending to " + adjustedAlive );
outgoing.offer( message.setHeader( Message.TO,
context.getUriForId( firstOrNull( adjustedAlive ) ).toString() ) );
}
}
break;
}
case vote:
{
Object request = message.getPayload();
ElectionContext.VoteRequest voteRequest = (ElectionContext.VoteRequest) request;
outgoing.offer( Message.respond( ElectionMessage.voted, message,
new ElectionMessage.VersionedVotedData( voteRequest.getRole(), context.getMyId(),
context.getCredentialsForRole( voteRequest.getRole() ),
voteRequest.getVersion() ) ) );
break;
}
case voted:
{
ElectionMessage.VotedData data = message.getPayload();
long version = -1;
if ( data instanceof ElectionMessage.VersionedVotedData )
{
version = ((ElectionMessage.VersionedVotedData) data).getVersion();
}
boolean accepted =
context.voted( data.getRole(), data.getInstanceId(), data.getElectionCredentials(),
version );
String voter = message.hasHeader( Message.FROM ) ? message.getHeader( Message.FROM ) : "I";
log.debug( voter + " voted " + data + " which i " +
( accepted ? "accepted" : "did not accept" ) );
if ( !accepted )
{
break;
}
/*
* This is the URI of the current role holder and, yes, it could very well be null. However
* we don't really care. If it is null then the election would not have sent one vote
* request less than needed (i.e. ask the master last) since, well, it doesn't exist. So
* the immediate effect is that the else (which checks for null) will never be called.
*/
InstanceId currentElected = context.getElected( data.getRole() );
if ( context.getVoteCount( data.getRole() ) == context.getNeededVoteCount() )
{
// We have all votes now
InstanceId winner = context.getElectionWinner( data.getRole() );
context.cancelTimeout( "election-" + data.getRole() );
context.forgetElection( data.getRole() );
if ( winner != null )
{
log.debug( "Elected " + winner + " as " + data.getRole() );
// Broadcast this
ClusterMessage.VersionedConfigurationStateChange configurationChangeState =
context.newConfigurationStateChange();
configurationChangeState.elected( data.getRole(), winner );
outgoing.offer( Message.internal( AtomicBroadcastMessage.broadcast,
configurationChangeState ) );
}
else
{
log.warn( "Election could not pick a winner" );
if ( currentElected != null )
{
// Someone had the role and doesn't anymore. Broadcast this
ClusterMessage.ConfigurationChangeState configurationChangeState =
new ClusterMessage.ConfigurationChangeState();
configurationChangeState.unelected( data.getRole(), currentElected );
outgoing.offer( Message.internal( ProposerMessage.propose,
configurationChangeState ) );
}
}
}
else if ( context.getVoteCount( data.getRole() ) == context.getNeededVoteCount() - 1 &&
currentElected != null &&
!context.hasCurrentlyElectedVoted( data.getRole(), currentElected ) )
{
// Missing one vote, the one from the current role holder
outgoing.offer( Message.to( ElectionMessage.vote,
context.getUriForId( currentElected ),
context.voteRequestForRole( new ElectionRole( data.getRole() ) ) ) );
}
break;
}
case electionTimeout:
{
// Election failed - try again
ElectionTimeoutData electionTimeoutData = message.getPayload();
log.warn( String.format(
"Election timed out for '%s'- trying again", electionTimeoutData.getRole() ) );
context.forgetElection( electionTimeoutData.getRole() );
outgoing.offer( electionTimeoutData.getMessage() );
break;
}
case leave:
{
return start;
}
default:
break;
}
return this;
}
};
public static class ElectionTimeoutData
{
private final String role;
private final Message message;
public ElectionTimeoutData( String role, Message message )
{
this.role = role;
this.message = message;
}
public String getRole()
{
return role;
}
public Message getMessage()
{
return message;
}
}
}