/
RaftEngine.cs
1012 lines (885 loc) · 37.5 KB
/
RaftEngine.cs
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
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// -----------------------------------------------------------------------
// <copyright file="RaftEngine.cs" company="Hibernating Rhinos LTD">
// Copyright (c) Hibernating Rhinos LTD. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Rachis.Behaviors;
using Rachis.Commands;
using Rachis.Interfaces;
using Rachis.Messages;
using Rachis.Storage;
using Rachis.Transport;
using Rachis.Utils;
using Raven.Abstractions.Extensions;
using Raven.Abstractions.Logging;
using Raven.Imports.Newtonsoft.Json;
namespace Rachis
{
public class RaftEngine : IDisposable
{
private readonly RaftEngineOptions _raftEngineOptions;
private readonly CancellationTokenSource _eventLoopCancellationTokenSource;
private readonly ManualResetEventSlim _leaderSelectedEvent = new ManualResetEventSlim();
private readonly ManualResetEventSlim _leaderConfirmedEvent = new ManualResetEventSlim();
private TaskCompletionSource<object> _steppingDownCompletionSource;
private Topology _currentTopology;
internal ILog _log;
public IRaftStateMachine StateMachine { get { return _raftEngineOptions.StateMachine; } }
public ITransport Transport { get { return _raftEngineOptions.Transport; } }
public string Name { get; set; }
public PersistentState PersistentState { get; set; }
public RaftEngineStatistics EngineStatistics;
public string CurrentLeader
{
get
{
return _currentLeader;
}
set
{
if (_currentLeader == value)
return;
if (_log.IsDebugEnabled)
_log.Debug("Setting CurrentLeader: {0} on term {1}", value, PersistentState.CurrentTerm);
_currentLeader = value;
if (value == null)
{
_leaderSelectedEvent.Reset();
_leaderConfirmedEvent.Reset();
}
else
{
_leaderSelectedEvent.Set();
}
}
}
public long CommitIndex
{
get { return StateMachine.LastAppliedIndex; }
}
public RaftEngineState State
{
get
{
var behavior = StateBehavior;
if (behavior == null)
return RaftEngineState.None;
return behavior.State;
}
}
/// <summary>
/// This method is intended to prevent a node from becoming a leader by registering
/// ProposingCandidacy event and setting the veto result to false
/// </summary>
/// <returns> The veto result with the reasoning</returns>
public ProposingCandidacyResult CheckIfThereIsVetoOnBecomingCandidate()
{
var candidacyResult = new ProposingCandidacyResult();
var onProposingCandidacy = ProposingCandidacy;
if (onProposingCandidacy == null)
return candidacyResult;
onProposingCandidacy(this, candidacyResult);
return candidacyResult;
}
private readonly Task _eventLoopTask;
private long _commitIndex;
private string _currentLeader;
private Task _snapshottingTask;
private Task _changingTopology;
private AbstractRaftStateBehavior StateBehavior { get; set; }
public CancellationToken CancellationToken { get { return _eventLoopCancellationTokenSource.Token; } }
public event EventHandler<ProposingCandidacyResult> ProposingCandidacy;
public event Action<RaftEngineState> StateChanged;
public event Action<Command> CommitApplied;
public event Action<long> NewTerm;
public event Action ElectionStarted;
public event Action StateTimeout;
public event Action<LogEntry[]> EntriesAppended;
public event Action<long, long> CommitIndexChanged;
public event Action ElectedAsLeader;
public event Action<TopologyChangeCommand> TopologyChanged;
public event Action TopologyChanging;
public event Action CreatingSnapshot;
public event Action CreatedSnapshot;
public event Action InstallingSnapshot;
public event Action SnapshotInstalled;
public event Action<Exception> SnapshotCreationError;
/// <summary>
/// will fire each time event loop of the node will process events and send response messages
/// </summary>
public event Action EventsProcessed;
public RaftEngine(RaftEngineOptions raftEngineOptions)
{
_raftEngineOptions = raftEngineOptions;
EngineStatistics = new RaftEngineStatistics(this);
Debug.Assert(raftEngineOptions.Stopwatch != null);
_log = LogManager.GetLogger(raftEngineOptions.Name + "." + GetType().FullName);
_eventLoopCancellationTokenSource = new CancellationTokenSource();
Name = raftEngineOptions.Name;
PersistentState = new PersistentState(raftEngineOptions.Name, raftEngineOptions.StorageOptions, _eventLoopCancellationTokenSource.Token)
{
CommandSerializer = new JsonCommandSerializer()
};
_currentTopology = PersistentState.GetCurrentTopology();
//warm up to make sure that the serializer don't take too long and force election timeout
PersistentState.CommandSerializer.Serialize(new NopCommand());
var thereAreOthersInTheCluster = CurrentTopology.QuorumSize > 1;
if (thereAreOthersInTheCluster == false && CurrentTopology.IsVoter(Name))
{
PersistentState.UpdateTermTo(this, PersistentState.CurrentTerm + 1);// restart means new term
SetState(RaftEngineState.Leader);
}
else
{
SetState(RaftEngineState.Follower);
}
_commitIndex = StateMachine.LastAppliedIndex;
_eventLoopTask = Task.Factory.StartNew(EventLoop, TaskCreationOptions.LongRunning);
}
public RaftEngineOptions Options
{
get { return _raftEngineOptions; }
}
protected void EventLoop()
{
while (_eventLoopCancellationTokenSource.IsCancellationRequested == false)
{
try
{
MessageContext message;
var behavior = StateBehavior;
var lastHeartBeat = (int) (DateTime.UtcNow - behavior.LastHeartbeatTime).TotalMilliseconds;
var timeout = behavior.Timeout - lastHeartBeat;
var hasMessage = Transport.TryReceiveMessage(timeout, _eventLoopCancellationTokenSource.Token, out message);
var messageBase = message?.Message as BaseMessage;
//Append entries is a very common messgae (it is the heartbeat)
//We don't want to keep empty append entries in the log so we filter them.
AppendEntriesRequestWithEntries appendEntriesRequest;
if (messageBase != null && ShouldNotFilterMessage(messageBase,out appendEntriesRequest))
EngineStatistics.Messages.LimitedSizeEnqueue(
new MessageWithTimingInformation {Message = appendEntriesRequest??messageBase, MessageReceiveTime = DateTime.UtcNow},
RaftEngineStatistics.NumberOfMessagesToTrack);
if (_eventLoopCancellationTokenSource.IsCancellationRequested)
{
if (_log.IsDebugEnabled)
_log.Debug("Cancellation request from event loop");
break;
}
//The behavior may have changed while we were waiting for messages (this happens when forcing leadership).
var hasStateChagned = behavior != StateBehavior;
var oldBehavior = behavior;
behavior = StateBehavior;
if (hasMessage == false)
{
if (hasStateChagned)
{
if(_log.IsDebugEnabled)
_log.Debug("State {0} timeout but the behavior has changed to {2} so we will skeep timeout handling ({1:#,#;;0} ms).", oldBehavior.State, oldBehavior.Timeout,behavior.State);
continue;
}
if (State != RaftEngineState.Leader && _log.IsDebugEnabled)
_log.Debug("State {0} timeout ({1:#,#;;0} ms).", State, behavior.Timeout);
EngineStatistics.TimeOuts.LimitedSizeEnqueue(
new TimeoutInformation()
{ActualTimeout = lastHeartBeat, State = State, Timeout = behavior.Timeout, TimeOutTime = DateTime.UtcNow}
, RaftEngineStatistics.NumberOfTimeoutsToTrack);
behavior.HandleTimeout();
OnStateTimeout();
continue;
}
if (_log.IsDebugEnabled)
_log.Debug("{0}: {1} {2}", State,
message.Message.GetType().Name,
message.Message is BaseMessage ? JsonConvert.SerializeObject(message.Message) : string.Empty
);
behavior.HandleMessage(message);
}
catch (OperationCanceledException)
{
break;
}
}
}
private bool ShouldNotFilterMessage(BaseMessage message,out AppendEntriesRequestWithEntries appendEntriesRequest)
{
var appendMessage = message as AppendEntriesRequest;
appendEntriesRequest = null;
if (appendMessage == null)
return true;
if (appendMessage.EntriesCount == 0)
return false;
appendEntriesRequest = AppendEntriesRequestWithEntries.FromAppendEntriesRequest(appendMessage);
return true;
}
internal void UpdateCurrentTerm(long term, string leader)
{
PersistentState.UpdateTermTo(this, term);
SetState(RaftEngineState.Follower);
if (_log.IsDebugEnabled)
_log.Debug("UpdateCurrentTerm() setting new leader : {0}", leader ?? "no leader currently");
CurrentLeader = leader;
}
internal void SetState(RaftEngineState state)
{
if (state == State)
return;
if (State == RaftEngineState.Leader)
_leaderSelectedEvent.Reset();
var oldState = State;
if (StateBehavior != null)
StateBehavior.Dispose();
switch (state)
{
case RaftEngineState.Follower:
case RaftEngineState.FollowerAfterStepDown:
StateBehavior = new FollowerStateBehavior(this, state == RaftEngineState.FollowerAfterStepDown);
break;
case RaftEngineState.CandidateByRequest:
StateBehavior = new CandidateStateBehavior(this, true);
//setting state before calling election because the state may change to leader before
//we set it to CandidateByRequest
AssertStateAndRaiseStateChanged(state, oldState);
StateBehavior.HandleTimeout();
return;
case RaftEngineState.Candidate:
StateBehavior = new CandidateStateBehavior(this, false);
//setting state before calling election because the state may change to leader before
//we set it to CandidateStateBehavior
AssertStateAndRaiseStateChanged(state, oldState);
StateBehavior.HandleTimeout();
return;
case RaftEngineState.SnapshotInstallation:
StateBehavior = new SnapshotInstallationStateBehavior(this);
break;
case RaftEngineState.Leader:
StateBehavior = new LeaderStateBehavior(this);
CurrentLeader = Name;
OnElectedAsLeader();
break;
case RaftEngineState.SteppingDown:
StateBehavior = new SteppingDownStateBehavior(this);
CurrentLeader = Name;
break;
default:
throw new ArgumentOutOfRangeException(state.ToString());
}
AssertStateAndRaiseStateChanged(state, oldState);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void AssertStateAndRaiseStateChanged(RaftEngineState state, RaftEngineState oldState)
{
Debug.Assert(StateBehavior != null, "StateBehavior != null");
OnStateChanged(state);
if (_log.IsDebugEnabled)
_log.Debug("{0} ==> {1}", oldState, state);
}
public void ForceCandidateState()
{
if (_log.IsDebugEnabled)
_log.Debug("Forced into candidate mode");
SetState(RaftEngineState.CandidateByRequest);
}
public Task StepDownAsync()
{
if (State != RaftEngineState.Leader)
throw new NotLeadingException("Not a leader, and only leaders can step down");
if (CurrentTopology.QuorumSize == 1)
{
throw new InvalidOperationException("Cannot step down if I'm the only one in the cluster");
}
_steppingDownCompletionSource = new TaskCompletionSource<object>();
if (_log.IsDebugEnabled)
_log.Debug("Got a request to step down from leadership position, personal reasons, you understand.");
SetState(RaftEngineState.SteppingDown);
return _steppingDownCompletionSource.Task;
}
public Task RemoveFromClusterAsync(NodeConnectionInfo node)
{
if (_currentTopology.Contains(node.Name) == false)
throw new InvalidOperationException("Node " + node + " was not found in the cluster");
if (string.Equals(node.Name, Name, StringComparison.OrdinalIgnoreCase))
throw new InvalidOperationException("You cannot remove the current node from the cluster, step down this node and then remove it from the new leader");
var requestedTopology = new Topology(
_currentTopology.TopologyId,
_currentTopology.AllVotingNodes.Where(x => string.Equals(x.Name, node.Name, StringComparison.OrdinalIgnoreCase) == false),
_currentTopology.NonVotingNodes.Where(x => string.Equals(x.Name, node.Name, StringComparison.OrdinalIgnoreCase) == false),
_currentTopology.PromotableNodes.Where(x => string.Equals(x.Name, node.Name, StringComparison.OrdinalIgnoreCase) == false)
);
if (_log.IsInfoEnabled)
{
_log.Info("RemoveFromClusterAsync, requestedTopology: {0}", requestedTopology);
}
return ModifyTopology(requestedTopology);
}
public Task AddToClusterAsync(NodeConnectionInfo node)
{
if (_currentTopology.Contains(node.Name))
throw new InvalidOperationException("Node " + node.Name + " is already in the cluster");
var requestedTopology = new Topology(
_currentTopology.TopologyId,
_currentTopology.AllVotingNodes,
node.IsNoneVoter ? _currentTopology.NonVotingNodes.Union(new[] { node }) : _currentTopology.NonVotingNodes,
node.IsNoneVoter ? _currentTopology.PromotableNodes : _currentTopology.PromotableNodes.Union(new[] { node })
);
if (_log.IsInfoEnabled)
{
_log.Info("AddToClusterClusterAsync, requestedTopology: {0}", requestedTopology);
}
return ModifyTopology(requestedTopology);
}
public Task UpdateNodeAsync(NodeConnectionInfo node)
{
if (State == RaftEngineState.Leader && node.IsNoneVoter)
throw new InvalidOperationException("Can't change leader voting mode to none voter");
var currentTopology = CurrentTopology;
var allVotingNodes = currentTopology.AllVotingNodes.Select(n => n.Uri.AbsoluteUri == node.Uri.AbsoluteUri ? node : n).ToList();
var nonVotingNodes = currentTopology.NonVotingNodes.Select(n => n.Uri.AbsoluteUri == node.Uri.AbsoluteUri ? node : n).ToList();
var promotableNodes = currentTopology.PromotableNodes.Select(n => n.Uri.AbsoluteUri == node.Uri.AbsoluteUri ? node : n).ToList();
var currentNodeConfiguration = currentTopology.AllNodes.First(x => x.Uri.AbsoluteUri == node.Uri.AbsoluteUri);
if (currentNodeConfiguration.IsNoneVoter != node.IsNoneVoter)
{
if (node.IsNoneVoter)
{
// move node from voting -> non-voting
allVotingNodes.Remove(node);
nonVotingNodes.Add(node);
promotableNodes.Remove(node);
}
else
{
// move node from non-voting -> voting
allVotingNodes.Add(node);
nonVotingNodes.Remove(node);
promotableNodes.Add(node);
}
}
var requestedTopology = new Topology(
currentTopology.TopologyId,
allVotingNodes,
nonVotingNodes,
promotableNodes);
return ModifyTopology(requestedTopology);
}
public Task ModifyNodeVotingModeAsync(NodeConnectionInfo node, bool votingMode)
{
if (!_currentTopology.Contains(node.Name))
throw new InvalidOperationException("Node " + node.Name + " is not in the cluster");
node.IsNoneVoter = !votingMode;
var requestedTopology = new Topology(
_currentTopology.TopologyId,
votingMode ? _currentTopology.AllVotingNodes: _currentTopology.AllVotingNodes.Where(x => string.Equals(x.Name, node.Name, StringComparison.OrdinalIgnoreCase) == false),
votingMode ? _currentTopology.NonVotingNodes.Where(x => string.Equals(x.Name, node.Name, StringComparison.OrdinalIgnoreCase) == false):
_currentTopology.NonVotingNodes.Union(new[] { node}),
votingMode ? _currentTopology.PromotableNodes.Union(new [] {node}) : _currentTopology.PromotableNodes.Where(x => string.Equals(x.Name, node.Name, StringComparison.OrdinalIgnoreCase) == false)
);
if (_log.IsInfoEnabled)
{
_log.Info("ModifyNodeVotingModeAsync, requestedTopology: {0}", requestedTopology);
}
return ModifyTopology(requestedTopology);
}
internal bool CurrentlyChangingTopology()
{
return Interlocked.CompareExchange(ref _changingTopology, null, null) == null;
}
internal Task ModifyTopology(Topology requested)
{
if (State != RaftEngineState.Leader)
throw new NotLeadingException("Cannot modify topology from a non leader node, current leader is: " +
(CurrentLeader ?? "no leader"));
var logEntry = PersistentState.GetLogEntry(CommitIndex);
if (logEntry == null)
throw new InvalidOperationException("No log entry for committed for index " + CommitIndex + ", this is probably a brand new cluster with no committed entries or a serious problem");
if (logEntry.Term != PersistentState.CurrentTerm)
throw new InvalidOperationException("Cannot modify the cluster topology when the committed index " + CommitIndex + " is in term " + logEntry.Term + " but the current term is " +
PersistentState.CurrentTerm + ". Wait until the leader finishes committing entries from the current term and try again");
var tcc = new TopologyChangeCommand
{
Completion = new TaskCompletionSource<object>(),
Requested = requested,
Previous = _currentTopology,
BufferCommand = false,
};
if (Interlocked.CompareExchange(ref _changingTopology, tcc.Completion.Task, null) != null)
throw new InvalidOperationException("Cannot change the cluster topology while another topology change is in progress");
try
{
if (_log.IsDebugEnabled)
_log.Debug("Topology change started on leader");
StartTopologyChange(tcc);
AppendCommand(tcc);
return tcc.Completion.Task;
}
catch (Exception)
{
Interlocked.Exchange(ref _changingTopology, null);
throw;
}
}
public Topology CurrentTopology
{
get { return _currentTopology; }
}
internal bool LogIsUpToDate(long lastLogTerm, long lastLogIndex)
{
// Raft paper 5.4.1
var lastLogEntry = PersistentState.LastLogEntry();
if (lastLogEntry.Term < lastLogTerm)
return true;
return lastLogEntry.Index <= lastLogIndex;
}
public bool WaitForLeader(int timeout = 10*1000)
{
return _leaderSelectedEvent.Wait(timeout, CancellationToken);
}
public bool WaitForLeaderConfirmed(int timeout = 10 * 1000)
{
return _leaderConfirmedEvent.Wait(timeout, CancellationToken);
}
public void AppendCommand(Command command)
{
if (command == null) throw new ArgumentNullException("command");
var leaderStateBehavior = StateBehavior as LeaderStateBehavior;
if (leaderStateBehavior == null || leaderStateBehavior.State != RaftEngineState.Leader)
throw new NotLeadingException("Command can be appended only on leader node. This node behavior type is " +
StateBehavior.GetType().Name)
{
CurrentLeader = CurrentLeader
};
leaderStateBehavior.AppendCommand(command);
}
public void ApplyCommits(long from, long to)
{
Debug.Assert(to >= from);
foreach (var entry in PersistentState.LogEntriesAfter(from, to))
{
try
{
var oldCommitIndex = CommitIndex;
var command = PersistentState.CommandSerializer.Deserialize(entry.Data);
StateMachine.Apply(entry, command);
Debug.Assert(entry.Index == StateMachine.LastAppliedIndex);
if (_log.IsDebugEnabled)
_log.Debug("Committing entry #{0}", entry.Index);
var tcc = command as TopologyChangeCommand;
if (tcc != null)
{
if (_log.IsInfoEnabled)
{
_log.Info("ApplyCommits for TopologyChangedCommand, tcc.Requested = {0}, Name = {1}",
tcc.Requested, Name);
}
// StartTopologyChange(tcc); - not sure why it was needed, see RavenDB-3808 for details
CommitTopologyChange(tcc);
}
var noop = command as NopCommand;
if (noop != null && entry.Term == PersistentState.CurrentTerm)
{
if (_log.IsInfoEnabled)
{
_log.Info($"Raising leaderConfirmedEvent, Term = {entry.Term}, Index = {entry.Index}, Name = {Name}");
}
_leaderConfirmedEvent.Set();
}
OnCommitIndexChanged(oldCommitIndex, CommitIndex);
OnCommitApplied(command);
}
catch (Exception e)
{
_log.Error("Failed to apply commit", e);
throw;
}
}
if (StateMachine.SupportSnapshots == false)
return;
var commitedEntriesCount = PersistentState.GetCommitedEntriesCount(to);
if (commitedEntriesCount >= Options.MaxLogLengthBeforeCompaction)
{
// we need to pass this to the state machine so it can signal us
// when it is fine to continue modifications again, otherwise
// we might start the snapshot in a different term / index than we
// are actually saying we did.
var allowFurtherModifications = new ManualResetEventSlim();
SnapshotAndTruncateLog(to, allowFurtherModifications);
allowFurtherModifications.Wait(CancellationToken);
}
}
private void SnapshotAndTruncateLog(long to, ManualResetEventSlim allowFurtherModifications)
{
var task = new Task(() =>
{
OnSnapshotCreationStarted();
try
{
var currentTerm = PersistentState.CurrentTerm;
_log.Info("Starting to create snapshot up to {0} in term {1}", to, currentTerm);
StateMachine.CreateSnapshot(to, currentTerm, allowFurtherModifications);
PersistentState.MarkSnapshotFor(to, currentTerm,
Options.MaxLogLengthBeforeCompaction - (Options.MaxLogLengthBeforeCompaction/8));
_log.Info("Finished creating snapshot for {0} in term {1}", to, currentTerm);
OnSnapshotCreationEnded();
}
catch (Exception e)
{
_log.Error("Failed to create snapshot", e);
OnSnapshotCreationError(e);
}
finally
{
Interlocked.Exchange(ref _snapshottingTask, null);
}
});
if (Interlocked.CompareExchange(ref _snapshottingTask, task, null) != null)
{
allowFurtherModifications.Set();
return;
}
task.Start();
}
public void CommitTopologyChange(TopologyChangeCommand tcc)
{
//it is logical that _before_ OnTopologyChanged is fired the topology change task will be complete
// - since this task is used to track progress of topoplogy changes in the interface
Interlocked.Exchange(ref _changingTopology, null);
//if no topology was present and TopologyChangeCommand is issued to just
//accept new topology id - then tcc.Previous == null - it means that
//shouldRemainInTopology should be true - because there is no removal from topology actually
var isRemovedFromTopology = tcc.Requested.Contains(Name) == false &&
tcc.Previous != null &&
tcc.Previous.Contains(Name);
if (isRemovedFromTopology)
{
if (_log.IsDebugEnabled)
_log.Debug("This node is being removed from topology, setting its state to follower, it will be idle until a leader will join it to the cluster again");
CurrentLeader = null;
SetState(RaftEngineState.Follower);
}
else
{
if (_log.IsInfoEnabled)
{
_log.Info("Finished applying new topology: {0}{1}", _currentTopology,
tcc.Previous == null ? ", Previous topology was null - perhaps it is setting topology for the first time?" : String.Empty);
}
}
OnTopologyChanged(tcc);
}
public void Dispose()
{
_eventLoopCancellationTokenSource.Cancel();
var exceptions = new List<Exception>();
try
{
_eventLoopTask.Wait(1000);
}
catch (Exception e)
{
exceptions.Add(e);
}
try
{
PersistentState.Dispose();
}
catch (Exception e)
{
exceptions.Add(e);
}
try
{
_raftEngineOptions.Dispose();
}
catch (Exception e)
{
exceptions.Add(e);
}
if (exceptions.Count == 0)
return;
throw new AggregateException(exceptions);
}
internal virtual void OnCandidacyAnnounced()
{
var handler = ElectionStarted;
if (handler != null)
{
try
{
handler();
}
catch (Exception e)
{
_log.Error("Error on raising ElectionStarted event" + e);
}
}
}
protected virtual void OnStateChanged(RaftEngineState state)
{
var handler = StateChanged;
if (handler != null)
{
try
{
handler(state);
}
catch (Exception e)
{
_log.Error("Error on raising StateChanged event" + e);
}
}
}
protected virtual void OnStateTimeout()
{
var handler = StateTimeout;
if (handler != null)
{
try
{
handler();
}
catch (Exception e)
{
_log.Error("Error on raising StateTimeout event" + e);
}
}
}
internal virtual void OnEntriesAppended(LogEntry[] logEntries)
{
var handler = EntriesAppended;
if (handler != null)
{
try
{
handler(logEntries);
}
catch (Exception e)
{
_log.Error("Error on raising EntriesAppended event", e);
}
}
}
protected virtual void OnCommitIndexChanged(long oldCommitIndex, long newCommitIndex)
{
var handler = CommitIndexChanged;
if (handler != null)
{
try
{
handler(oldCommitIndex, newCommitIndex);
}
catch (Exception e)
{
_log.Error("Error on raising CommitIndexChanged event", e);
}
}
}
protected virtual void OnElectedAsLeader()
{
var handler = ElectedAsLeader;
if (handler != null)
{
try
{
handler();
}
catch (Exception e)
{
_log.Error("Error on raising ElectedAsLeader event", e);
}
}
}
protected virtual void OnTopologyChanged(TopologyChangeCommand cmd)
{
_log.Info ("OnTopologyChanged() - " + this.Name);
var handler = TopologyChanged;
if (handler != null)
{
try
{
handler(cmd);
}
catch (Exception e)
{
_log.Error("Error on raising TopologyChanged event", e);
}
}
}
protected virtual void OnCommitApplied(Command cmd)
{
var handler = CommitApplied;
if (handler != null)
{
try
{
handler(cmd);
}
catch (Exception e)
{
_log.Error("Error on raising CommitApplied event", e);
}
}
}
private void OnTopologyChanging(TopologyChangeCommand tcc)
{
var handler = TopologyChanging;
if (handler != null)
{
try
{
handler();
}
catch (Exception e)
{
_log.Error("Error on raising TopologyChanging event", e);
}
}
}
protected virtual void OnSnapshotCreationStarted()
{
var handler = CreatingSnapshot;
if (handler != null)
{
try
{
handler();
}
catch (Exception e)
{
_log.Error("Error on raising event", e);
}
}
}
protected virtual void OnSnapshotCreationEnded()
{
var handler = CreatedSnapshot;
if (handler != null)
{
try
{
handler();
}
catch (Exception e)
{
_log.Error("Error on raising event", e);
}
}
}
protected virtual void OnSnapshotCreationError(Exception e)
{
var handler = SnapshotCreationError;
if (handler != null)
{
try
{
handler(e);
}
catch (Exception ex)
{
_log.Error("Error on raising SnapshotCreationError event", ex);
}
}
}
internal virtual void OnEventsProcessed()
{
var handler = EventsProcessed;
if (handler != null)
{
try
{
handler();
}
catch (Exception e)
{
_log.Error("Error on raising EventsProcessed event", e);
}
}
}
internal virtual void OnSnapshotInstallationStarted()
{
var handler = InstallingSnapshot;
if (handler != null)
{
try
{
handler();
}
catch (Exception e)
{
_log.Error("Error on raising event", e);
}
}
}
internal void OnNewTerm(long term)
{
var handler = NewTerm;
if (handler != null)
{
try
{
handler(term);
}
catch (Exception e)
{
_log.Error("Error on raising event", e);
}
}
}
internal virtual void OnSnapshotInstallationEnded(long snapshotTerm)
{
var handler = SnapshotInstalled;
if (handler != null)
{
try
{
handler();
}
catch (Exception e)
{
_log.Error("Error on raising event", e);
}
}
}
public override string ToString()
{
return string.Format("Name: {0} = {1}", Name, State);
}
public void StartTopologyChange(TopologyChangeCommand tcc)
{
Interlocked.Exchange(ref _currentTopology, tcc.Requested);
Interlocked.Exchange(ref _changingTopology, new TaskCompletionSource<object>().Task);
OnTopologyChanging(tcc);
}
internal void RevertTopologyTo(Topology previous)
{
_log.Info("Reverting topology because the topology change command was reverted");
Interlocked.Exchange(ref _changingTopology, null);
Interlocked.Exchange(ref _currentTopology, previous);
OnTopologyChanged(new TopologyChangeCommand
{
Requested = previous
});
}
internal void FinishSteppingDown()
{
var steppingDownCompletionSource = _steppingDownCompletionSource;
if (steppingDownCompletionSource == null)
return;
_steppingDownCompletionSource = null;
steppingDownCompletionSource.TrySetResult(null);
}