-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEventualLeaderDetector.cs
83 lines (67 loc) · 2.43 KB
/
EventualLeaderDetector.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
using NewDalgs.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
namespace NewDalgs.Abstractions
{
class EventualLeaderDetector : Abstraction
{
public static readonly string Name = "eld";
private HashSet<ProtoComm.ProcessId> _suspected = new HashSet<ProtoComm.ProcessId>();
private ProtoComm.ProcessId _leader;
public EventualLeaderDetector(string abstractionId, System.System system)
: base(abstractionId, system)
{
_system.RegisterAbstraction(new EventuallyPerfectFailureDetector(AbstractionIdUtil.GetChildAbstractionId(_abstractionId, EventuallyPerfectFailureDetector.Name), _system));
}
public override bool Handle(ProtoComm.Message msg)
{
if (msg.Type == ProtoComm.Message.Types.Type.EpfdSuspect)
{
HandleEpfdSuspect(msg);
return true;
}
if (msg.Type == ProtoComm.Message.Types.Type.EpfdRestore)
{
HandleEpfdRestore(msg);
return true;
}
return false;
}
private void HandleEpfdRestore(ProtoComm.Message msg)
{
var procId = msg.EpfdRestore.Process;
_suspected.Remove(procId);
HandleInternalCheck();
}
private void HandleEpfdSuspect(ProtoComm.Message msg)
{
var procId = msg.EpfdSuspect.Process;
_suspected.Add(procId);
HandleInternalCheck();
}
private void HandleInternalCheck()
{
var tmpLeader = ProcessIdUtil.FindMaxRank(_system.Processes.Except(_suspected));
if (tmpLeader == null)
return;
if (!tmpLeader.Equals(_leader))
{
_leader = tmpLeader;
var msg = new ProtoComm.Message
{
Type = ProtoComm.Message.Types.Type.EldTrust,
EldTrust = new ProtoComm.EldTrust
{
Process = _leader
},
SystemId = _system.SystemId,
ToAbstractionId = AbstractionIdUtil.GetParentAbstractionId(_abstractionId),
FromAbstractionId = _abstractionId,
MessageUuid = Guid.NewGuid().ToString()
};
_system.TriggerEvent(msg);
}
}
}
}