-
Notifications
You must be signed in to change notification settings - Fork 19
/
DistributedHashTableMaster.cs
233 lines (207 loc) · 6.71 KB
/
DistributedHashTableMaster.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using Rhino.DistributedHashTable.Commands;
using Rhino.DistributedHashTable.Util;
namespace Rhino.DistributedHashTable.Internal
{
/// <summary>
/// The master is a SIGNLE THREADED service that manages all
/// operations in the cluster.
/// </summary>
public class DistributedHashTableMaster : IDistributedHashTableMaster
{
public event Action<BackupState, NodeEndpoint, Segment> BackupChanged = delegate { };
public event Action TopologyChanged = delegate { };
private readonly ILog log = LogManager.GetLogger(typeof(DistributedHashTableMaster));
public Topology Topology { get; set; }
private readonly HashSet<NodeEndpoint> endpoints = new HashSet<NodeEndpoint>();
public Segment[] Segments
{
get { return Topology.Segments; }
}
public int NumberOfBackCopiesToKeep
{
get;
set;
}
public DistributedHashTableMaster()
{
NumberOfBackCopiesToKeep = 2;
Topology = new Topology(CreateDefaultSegments().ToArray());
}
private static IEnumerable<Segment> CreateDefaultSegments()
{
for (int i = 0; i < 8192; i++)
{
var range = new Segment
{
Index = i
};
yield return range;
}
}
/// <summary>
/// This method is called when a new node wants to join the cluster.
/// The result is the ranges that this node is responsible for, if it is an
/// existing one, or the list of ranges that it needs to pull from the currently
/// assigned node.
/// Note:
/// that if it needs to pull date from the currently assigned node, it will
/// also need to call the <see cref="CaughtUp"/> method to let the master know
/// that it is done and that the topology changed.
/// </summary>
public Segment[] Join(NodeEndpoint endPoint)
{
var newlyAlocatedSegments = JoinInternal(endPoint);
RearrangeBackups();
if (log.IsDebugEnabled)
{
var sb = new StringBuilder()
.Append("After join of ")
.Append(endPoint)
.Append(" range allocation is:")
.AppendLine();
foreach (var range in Segments)
{
sb.Append("\t").Append(range).AppendLine();
}
log.Debug(sb.ToString());
}
return newlyAlocatedSegments;
}
/// <summary>
/// Notify the master that the endpoint has caught up on all the specified ranges
/// </summary>
public void CaughtUp(NodeEndpoint endPoint, params int[] caughtUpSegments)
{
Segment[] matchingSegments = GetMatchingSegments(caughtUpSegments, endPoint);
var modifiedSegments = from range in Segments
join caughtUpSegment in matchingSegments on range.Index equals caughtUpSegment.Index into maybeMatchingSegment
select
new { range, maybeMatchingSegment };
Topology = new Topology((
from modifiedSegment in modifiedSegments
let x = modifiedSegment.maybeMatchingSegment.FirstOrDefault()
select x == null
? modifiedSegment.range
: new Segment
{
Index = x.Index,
InProcessOfMovingToEndpoint = null,
AssignedEndpoint = endPoint,
Backups = x.Backups
.Append(x.AssignedEndpoint)
.Where(e => e != endPoint)
.ToSet()
}).ToArray()
);
RearrangeBackups();
TopologyChanged();
}
public void GaveUp(NodeEndpoint endpoint,
params int[] rangesGivingUpOn)
{
var matchingSegments = GetMatchingSegments(rangesGivingUpOn, endpoint);
foreach (var range in matchingSegments)
{
range.InProcessOfMovingToEndpoint = null;
}
}
private Segment[] GetMatchingSegments(IEnumerable<int> ranges,
NodeEndpoint endPoint)
{
var matchingSegments = ranges.Select(i => Segments[i]).ToArray();
var rangesNotBeloningToThespecifiedEndpoint = matchingSegments
.Where(x => x.InProcessOfMovingToEndpoint != null)
.Where(x => endPoint.Equals(x.InProcessOfMovingToEndpoint) == false);
if (rangesNotBeloningToThespecifiedEndpoint.Count() != 0)
throw new InvalidOperationException("Could not catch up or give up on ranges that belong to another endpoint");
return matchingSegments;
}
private void RearrangeBackups()
{
var rearranger = new RearrangeBackups(Segments, endpoints, NumberOfBackCopiesToKeep);
var rearranged = rearranger.Rearranging();
if (rearranged == false)
return;
foreach (var backUpAdded in rearranger.Changed)
{
BackupChanged(BackupState.Added, backUpAdded.Endpoint, backUpAdded.Segment);
}
}
private Segment[] JoinInternal(NodeEndpoint endPoint)
{
log.DebugFormat("Endpoint {0} joining", endPoint);
endpoints.Add(endPoint);
if (Segments.Any(x => x.BelongsTo(endPoint)))
{
log.DebugFormat("Endpoint {0} is already registered, probably an end point restart, ignoring", endPoint);
return Segments.Where(x => x.BelongsTo(endPoint)).ToArray();
}
var rangesThatHadNoOwner = Segments
.Where(x => x.AssignedEndpoint == null)
.Apply(x => x.AssignedEndpoint = endPoint)
.ToArray();
if (rangesThatHadNoOwner.Length > 0)
{
log.DebugFormat("Endpoint {0} was assigned all ranges without owners", endPoint);
return rangesThatHadNoOwner;
}
log.DebugFormat("New endpoint {0}, allocating ranges for it", endPoint);
return RestructureSegmentsFairly(endPoint);
}
private Segment[] RestructureSegmentsFairly(NodeEndpoint point)
{
var newSegments = new List<Segment>();
int index = 0;
foreach (var range in Segments)
{
index += 1;
if (range.InProcessOfMovingToEndpoint != null)
{
newSegments.Add(range);
continue;
}
if (index % endpoints.Count == 0)
{
newSegments.Add(new Segment
{
AssignedEndpoint = range.AssignedEndpoint,
InProcessOfMovingToEndpoint = point,
Index = range.Index,
Backups = range.Backups
});
}
else
{
newSegments.Add(range);
}
}
// this does NOT create a new topology version
Topology = new Topology(newSegments.ToArray(), Topology.Version);
return Segments.Where(x => x.BelongsTo(point)).ToArray();
}
public void Decommision(NodeEndpoint endPoint)
{
throw new NotImplementedException();
}
/// <summary>
/// Sync the master with the list of items maintained by a copy of the master
/// </summary>
public void SyncUp(Topology topology)
{
if (Topology.Version == topology.Version)
return;
if (Topology.Timestamp > topology.Timestamp)
return;
Topology = topology;
}
public Topology GetTopology()
{
return Topology;
}
}
}