-
Notifications
You must be signed in to change notification settings - Fork 383
/
ServerContext.java
728 lines (652 loc) · 22.4 KB
/
ServerContext.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
/*
* Copyright 2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.atomix.protocols.raft.server.state;
import io.atomix.cluster.NodeId;
import io.atomix.protocols.raft.cluster.RaftCluster;
import io.atomix.protocols.raft.cluster.RaftMember;
import io.atomix.protocols.raft.protocol.RaftRequest;
import io.atomix.protocols.raft.protocol.RaftResponse;
import io.atomix.protocols.raft.protocol.RaftServerProtocol;
import io.atomix.protocols.raft.protocol.RaftServerProtocolDispatcher;
import io.atomix.protocols.raft.protocol.RaftServerProtocolListener;
import io.atomix.protocols.raft.server.RaftServer;
import io.atomix.protocols.raft.server.storage.Log;
import io.atomix.protocols.raft.server.storage.LogReader;
import io.atomix.protocols.raft.server.storage.LogWriter;
import io.atomix.protocols.raft.server.storage.Reader;
import io.atomix.protocols.raft.server.storage.Storage;
import io.atomix.protocols.raft.server.storage.snapshot.SnapshotStore;
import io.atomix.protocols.raft.server.storage.system.MetaStore;
import io.atomix.util.concurrent.SingleThreadContext;
import io.atomix.util.concurrent.ThreadContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.time.Duration;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ScheduledExecutorService;
import java.util.function.Consumer;
import java.util.function.Supplier;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
/**
* Manages the volatile state and state transitions of a Copycat server.
* <p>
* This class is the primary vehicle for managing the state of a server. All state that is shared across roles (i.e. follower, candidate, leader)
* is stored in the cluster state. This includes Raft-specific state like the current leader and term, the log, and the cluster configuration.
*
* @author <a href="http://github.com/kuujo">Jordan Halterman</a>
*/
public class ServerContext implements AutoCloseable {
private static final Logger LOGGER = LoggerFactory.getLogger(ServerContext.class);
private final Set<Consumer<RaftServer.State>> stateChangeListeners = new CopyOnWriteArraySet<>();
private final Set<Consumer<RaftMember>> electionListeners = new CopyOnWriteArraySet<>();
protected final String name;
protected final ThreadContext threadContext;
protected final StateMachineRegistry registry;
protected final RaftClusterState cluster;
protected final RaftServerProtocol protocol;
protected final Storage storage;
private MetaStore meta;
private Log log;
private LogWriter writer;
private LogReader reader;
private SnapshotStore snapshot;
private ServerStateMachineManager stateMachine;
protected final ScheduledExecutorService threadPool;
protected final ThreadContext stateContext;
protected ServerState state = new InactiveState(this);
private Duration electionTimeout = Duration.ofMillis(500);
private Duration sessionTimeout = Duration.ofMillis(5000);
private Duration heartbeatInterval = Duration.ofMillis(150);
private volatile NodeId leader;
private volatile long term;
private NodeId lastVotedFor;
private long commitIndex;
@SuppressWarnings("unchecked")
public ServerContext(String name, RaftMember.Type type, NodeId localNodeId, RaftServerProtocol protocol, Storage storage, StateMachineRegistry registry, ScheduledExecutorService threadPool, ThreadContext threadContext) {
this.name = checkNotNull(name, "name cannot be null");
this.protocol = checkNotNull(protocol, "protocol cannot be null");
this.storage = checkNotNull(storage, "storage cannot be null");
this.threadContext = checkNotNull(threadContext, "threadContext cannot be null");
this.registry = checkNotNull(registry, "registry cannot be null");
this.stateContext = new SingleThreadContext(String.format("copycat-server-%s-%s-state", localNodeId, name));
this.threadPool = checkNotNull(threadPool, "threadPool cannot be null");
// Open the meta store.
CountDownLatch metaLatch = new CountDownLatch(1);
threadContext.execute(() -> {
this.meta = storage.openMetaStore(name);
metaLatch.countDown();
});
try {
metaLatch.await();
} catch (InterruptedException e) {
}
// Load the current term and last vote from disk.
this.term = meta.loadTerm();
this.lastVotedFor = meta.loadVote();
// Reset the state machine.
CountDownLatch resetLatch = new CountDownLatch(1);
threadContext.execute(() -> {
reset();
resetLatch.countDown();
});
try {
resetLatch.await();
} catch (InterruptedException e) {
}
this.cluster = new RaftClusterState(type, localNodeId, this);
// Register protocol listeners.
registerHandlers(protocol.listener());
}
/**
* Adds a state change listener.
*
* @param listener The state change listener.
*/
public void addStateChangeListener(Consumer<RaftServer.State> listener) {
stateChangeListeners.add(listener);
}
/**
* Removes a state change listener.
*
* @param listener The state change listener.
*/
public void removeStateChangeListener(Consumer<RaftServer.State> listener) {
stateChangeListeners.remove(listener);
}
/**
* Adds a leader election listener.
*
* @param listener The leader election listener.
*/
public void addLeaderElectionListener(Consumer<RaftMember> listener) {
electionListeners.add(listener);
}
/**
* Removes a leader election listener.
*
* @param listener The leader election listener.
*/
public void removeLeaderElectionListener(Consumer<RaftMember> listener) {
electionListeners.remove(listener);
}
/**
* Returns the execution context.
*
* @return The execution context.
*/
public ThreadContext getThreadContext() {
return threadContext;
}
/**
* Returns the server protocol.
*
* @return The server protocol.
*/
public RaftServerProtocol getProtocol() {
return protocol;
}
/**
* Returns the server protocol dispatcher.
*
* @return The server protocol dispatcher.
*/
public RaftServerProtocolDispatcher getProtocolDispatcher() {
return protocol.dispatcher();
}
/**
* Returns the server protocol listener.
*
* @return The server protocol listener.
*/
public RaftServerProtocolListener getProtocolListener() {
return protocol.listener();
}
/**
* Returns the server storage.
*
* @return The server storage.
*/
public Storage getStorage() {
return storage;
}
/**
* Sets the election timeout.
*
* @param electionTimeout The election timeout.
* @return The Raft context.
*/
public ServerContext setElectionTimeout(Duration electionTimeout) {
this.electionTimeout = electionTimeout;
return this;
}
/**
* Returns the election timeout.
*
* @return The election timeout.
*/
public Duration getElectionTimeout() {
return electionTimeout;
}
/**
* Sets the heartbeat interval.
*
* @param heartbeatInterval The Raft heartbeat interval.
* @return The Raft context.
*/
public ServerContext setHeartbeatInterval(Duration heartbeatInterval) {
this.heartbeatInterval = checkNotNull(heartbeatInterval, "heartbeatInterval cannot be null");
return this;
}
/**
* Returns the heartbeat interval.
*
* @return The heartbeat interval.
*/
public Duration getHeartbeatInterval() {
return heartbeatInterval;
}
/**
* Returns the session timeout.
*
* @return The session timeout.
*/
public Duration getSessionTimeout() {
return sessionTimeout;
}
/**
* Sets the session timeout.
*
* @param sessionTimeout The session timeout.
* @return The Raft state machine.
*/
public ServerContext setSessionTimeout(Duration sessionTimeout) {
this.sessionTimeout = checkNotNull(sessionTimeout, "sessionTimeout cannot be null");
return this;
}
/**
* Sets the state leader.
*
* @param leader The state leader.
* @return The Raft context.
*/
ServerContext setLeader(NodeId leader) {
if (!Objects.equals(this.leader, leader)) {
// 0 indicates no leader.
if (leader == null) {
this.leader = null;
} else {
// If a valid leader ID was specified, it must be a member that's currently a member of the
// ACTIVE members configuration. Note that we don't throw exceptions for unknown members. It's
// possible that a failure following a configuration change could result in an unknown leader
// sending AppendRequest to this server. Simply configure the leader if it's known.
RaftMemberState member = cluster.member(leader);
if (member != null) {
this.leader = leader;
LOGGER.info("{} - Found leader {}", cluster.member().id(), member.id());
electionListeners.forEach(l -> l.accept(member));
}
}
this.lastVotedFor = null;
meta.storeVote(null);
}
return this;
}
/**
* Returns the cluster state.
*
* @return The cluster state.
*/
public RaftCluster getCluster() {
return cluster;
}
/**
* Returns the cluster state.
*
* @return The cluster state.
*/
RaftClusterState getClusterState() {
return cluster;
}
/**
* Returns the state leader.
*
* @return The state leader.
*/
RaftMemberState getLeader() {
// Store in a local variable to prevent race conditions and/or multiple volatile lookups.
NodeId leader = this.leader;
return leader != null ? cluster.member(leader) : null;
}
/**
* Returns a boolean indicating whether this server is the current leader.
*
* @return Indicates whether this server is the leader.
*/
boolean isLeader() {
NodeId leader = this.leader;
return leader != null && leader.equals(cluster.member().id());
}
/**
* Sets the state term.
*
* @param term The state term.
* @return The Raft context.
*/
ServerContext setTerm(long term) {
if (term > this.term) {
this.term = term;
this.leader = null;
this.lastVotedFor = null;
meta.storeTerm(this.term);
meta.storeVote(this.lastVotedFor);
LOGGER.debug("{} - Set term {}", cluster.member().id(), term);
}
return this;
}
/**
* Returns the state term.
*
* @return The state term.
*/
long getTerm() {
return term;
}
/**
* Sets the state last voted for candidate.
*
* @param candidate The candidate that was voted for.
* @return The Raft context.
*/
ServerContext setLastVotedFor(NodeId candidate) {
// If we've already voted for another candidate in this term then the last voted for candidate cannot be overridden.
checkState(!(lastVotedFor != null && candidate != null), "Already voted for another candidate");
RaftMemberState member = cluster.member(candidate);
checkState(member != null, "Unknown candidate: %d", candidate);
this.lastVotedFor = candidate;
meta.storeVote(this.lastVotedFor);
if (candidate != null) {
LOGGER.debug("{} - Voted for {}", cluster.member().id(), member.id());
} else {
LOGGER.trace("{} - Reset last voted for", cluster.member().id());
}
return this;
}
/**
* Returns the state last voted for candidate.
*
* @return The state last voted for candidate.
*/
NodeId getLastVotedFor() {
return lastVotedFor;
}
/**
* Sets the commit index.
*
* @param commitIndex The commit index.
* @return The Raft context.
*/
ServerContext setCommitIndex(long commitIndex) {
checkArgument(commitIndex >= 0, "commitIndex must be positive");
long previousCommitIndex = this.commitIndex;
if (commitIndex > previousCommitIndex) {
this.commitIndex = commitIndex;
writer.commit(Math.min(commitIndex, writer.lastIndex()));
long configurationIndex = cluster.getConfiguration().index();
if (configurationIndex > previousCommitIndex && configurationIndex <= commitIndex) {
cluster.commit();
}
}
return this;
}
/**
* Returns the commit index.
*
* @return The commit index.
*/
long getCommitIndex() {
return commitIndex;
}
/**
* Returns the server state machine.
*
* @return The server state machine.
*/
public ServerStateMachineManager getStateMachine() {
return stateMachine;
}
/**
* Returns the server state machine registry.
*
* @return The server state machine registry.
*/
public StateMachineRegistry getStateMachineRegistry() {
return registry;
}
/**
* Returns the current state.
*
* @return The current state.
*/
public RaftServer.State getState() {
return state.type();
}
/**
* Returns the current server state.
*
* @return The current server state.
*/
ServerState getServerState() {
return state;
}
/**
* Returns the server metadata store.
*
* @return The server metadata store.
*/
public MetaStore getMetaStore() {
return meta;
}
/**
* Returns the server log.
*
* @return The server log.
*/
public Log getLog() {
return log;
}
/**
* Returns the server log writer.
*
* @return The log writer.
*/
LogWriter getLogWriter() {
return writer;
}
/**
* Returns the server log reader.
*
* @return The log reader.
*/
LogReader getLogReader() {
return reader;
}
/**
* Resets the state log.
*
* @return The server context.
*/
ServerContext reset() {
// Delete the existing log.
if (log != null) {
log.close();
storage.deleteLog(name);
}
// Delete the existing snapshot store.
if (snapshot != null) {
snapshot.close();
storage.deleteSnapshotStore(name);
}
// Open the log.
log = storage.openLog(name);
writer = log.writer();
reader = log.createReader(1, Reader.Mode.ALL);
// Open the snapshot store.
snapshot = storage.openSnapshotStore(name);
// Create a new internal server state machine.
this.stateMachine = new ServerStateMachineManager(this, threadPool, stateContext);
return this;
}
/**
* Returns the server snapshot store.
*
* @return The server snapshot store.
*/
public SnapshotStore getSnapshotStore() {
return snapshot;
}
/**
* Checks that the current thread is the state context thread.
*/
void checkThread() {
threadContext.checkThread();
}
/**
* Registers server handlers on the configured protocol.
*/
private void registerHandlers(RaftServerProtocolListener listener) {
listener.registerOpenSessionHandler(request -> runOnContext(() -> state.openSession(request)));
listener.registerCloseSessionHandler(request -> runOnContext(() -> state.closeSession(request)));
listener.registerKeepAliveHandler(request -> runOnContext(() -> state.keepAlive(request)));
listener.registerConfigureHandler(request -> runOnContext(() -> state.configure(request)));
listener.registerInstallHandler(request -> runOnContext(() -> state.install(request)));
listener.registerJoinHandler(request -> runOnContext(() -> state.join(request)));
listener.registerReconfigureHandler(request -> runOnContext(() -> state.reconfigure(request)));
listener.registerLeaveHandler(request -> runOnContext(() -> state.leave(request)));
listener.registerAppendHandler(request -> runOnContext(() -> state.append(request)));
listener.registerPollHandler(request -> runOnContext(() -> state.poll(request)));
listener.registerVoteHandler(request -> runOnContext(() -> state.vote(request)));
listener.registerCommandHandler(request -> runOnContext(() -> state.command(request)));
listener.registerQueryHandler(request -> runOnContext(() -> state.query(request)));
}
private <T extends RaftRequest, U extends RaftResponse> CompletableFuture<U> runOnContext(Supplier<CompletableFuture<U>> function) {
CompletableFuture<U> future = new CompletableFuture<U>();
threadContext.execute(() -> {
function.get().whenComplete((response, error) -> {
if (error == null) {
future.complete(response);
} else {
future.completeExceptionally(error);
}
});
});
return future;
}
/**
* Unregisters server handlers on the configured protocol.
*/
private void unregisterHandlers(RaftServerProtocolListener listener) {
listener.unregisterOpenSessionHandler();
listener.unregisterCloseSessionHandler();
listener.unregisterKeepAliveHandler();
listener.unregisterConfigureHandler();
listener.unregisterInstallHandler();
listener.unregisterJoinHandler();
listener.unregisterReconfigureHandler();
listener.unregisterLeaveHandler();
listener.unregisterAppendHandler();
listener.unregisterPollHandler();
listener.unregisterVoteHandler();
listener.unregisterCommandHandler();
listener.unregisterQueryHandler();
}
/**
* Transitions the server to the base state for the given member type.
*/
protected void transition(RaftMember.Type type) {
switch (type) {
case ACTIVE:
if (!(state instanceof ActiveState)) {
transition(RaftServer.State.FOLLOWER);
}
break;
case PASSIVE:
if (this.state.type() != RaftServer.State.PASSIVE) {
transition(RaftServer.State.PASSIVE);
}
break;
case RESERVE:
if (this.state.type() != RaftServer.State.RESERVE) {
transition(RaftServer.State.RESERVE);
}
break;
default:
if (this.state.type() != RaftServer.State.INACTIVE) {
transition(RaftServer.State.INACTIVE);
}
break;
}
}
/**
* Transition handler.
*/
public void transition(RaftServer.State state) {
checkThread();
if (this.state != null && state == this.state.type()) {
return;
}
LOGGER.info("{} - Transitioning to {}", cluster.member().id(), state);
// Close the old state.
try {
this.state.close().get();
} catch (InterruptedException | ExecutionException e) {
throw new IllegalStateException("failed to close Raft state", e);
}
// Force state transitions to occur synchronously in order to prevent race conditions.
try {
this.state = createState(state);
this.state.open().get();
} catch (InterruptedException | ExecutionException e) {
throw new IllegalStateException("failed to initialize Raft state", e);
}
stateChangeListeners.forEach(l -> l.accept(this.state.type()));
}
/**
* Creates an internal state for the given state type.
*/
private AbstractState createState(RaftServer.State state) {
switch (state) {
case INACTIVE:
return new InactiveState(this);
case RESERVE:
return new ReserveState(this);
case PASSIVE:
return new PassiveState(this);
case FOLLOWER:
return new FollowerState(this);
case CANDIDATE:
return new CandidateState(this);
case LEADER:
return new LeaderState(this);
default:
throw new AssertionError();
}
}
@Override
public void close() {
// Unregister protocol listeners.
unregisterHandlers(protocol.listener());
// Close the log.
try {
log.close();
} catch (Exception e) {
}
// Close the metastore.
try {
meta.close();
} catch (Exception e) {
}
// Close the snapshot store.
try {
snapshot.close();
} catch (Exception e) {
}
// Close the state machine and thread context.
stateMachine.close();
threadContext.close();
}
/**
* Deletes the server context.
*/
public void delete() {
// Delete the log.
storage.deleteLog(name);
// Delete the snapshot store.
storage.deleteSnapshotStore(name);
// Delete the metadata store.
storage.deleteMetaStore(name);
}
@Override
public String toString() {
return getClass().getCanonicalName();
}
}