-
Notifications
You must be signed in to change notification settings - Fork 1k
/
WriteAggregator.cs
337 lines (275 loc) · 12.2 KB
/
WriteAggregator.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
//-----------------------------------------------------------------------
// <copyright file="WriteAggregator.cs" company="Akka.NET Project">
// Copyright (C) 2009-2023 Lightbend Inc. <http://www.lightbend.com>
// Copyright (C) 2013-2023 .NET Foundation <https://github.com/akkadotnet/akka.net>
// </copyright>
//-----------------------------------------------------------------------
using Akka.Actor;
using Akka.DistributedData.Internal;
using System;
using System.Collections.Immutable;
using Akka.Cluster;
using Akka.Event;
namespace Akka.DistributedData
{
internal class WriteAggregator : ReadWriteAggregator
{
public static Props Props(IKey key, DataEnvelope envelope, Delta delta, IWriteConsistency consistency, object req, IImmutableList<Address> nodes, IImmutableSet<Address> unreachable, bool shuffle, IActorRef replyTo, bool durable) =>
Actor.Props.Create(() => new WriteAggregator(key, envelope, delta, consistency, req, nodes, unreachable, shuffle, replyTo, durable)).WithDeploy(Deploy.Local);
private readonly IKey _key;
private readonly DataEnvelope _envelope;
private readonly IWriteConsistency _consistency;
private readonly object _req;
private readonly IActorRef _replyTo;
private readonly Write _write;
private readonly DeltaPropagation _delta;
private readonly bool _durable;
private readonly UniqueAddress _selfUniqueAddress;
private bool _gotLocalStoreReply;
private ImmutableHashSet<Address> _gotNackFrom;
public WriteAggregator(IKey key, DataEnvelope envelope, Delta delta, IWriteConsistency consistency, object req, IImmutableList<Address> nodes,
IImmutableSet<Address> unreachable, bool shuffle, IActorRef replyTo, bool durable)
: base(nodes, unreachable, consistency.Timeout, shuffle)
{
_selfUniqueAddress = Cluster.Cluster.Get(Context.System).SelfUniqueAddress;
_key = key;
_envelope = envelope;
_consistency = consistency;
_req = req;
_replyTo = replyTo;
_durable = durable;
_write = new Write(key.Id, envelope, _selfUniqueAddress);
_delta = delta == null
? null
: new DeltaPropagation(_selfUniqueAddress, true,
ImmutableDictionary<string, Delta>.Empty.Add(key.Id, delta));
_gotLocalStoreReply = !durable;
_gotNackFrom = ImmutableHashSet<Address>.Empty;
DoneWhenRemainingSize = GetDoneWhenRemainingSize();
}
protected bool IsDone => _gotLocalStoreReply && (Remaining.Count <= DoneWhenRemainingSize || (Remaining.Except(_gotNackFrom).Count == 0) || NotEnoughNodes);
protected bool NotEnoughNodes => DoneWhenRemainingSize < 0 || Nodes.Count < DoneWhenRemainingSize;
protected override int DoneWhenRemainingSize { get; }
private int GetDoneWhenRemainingSize()
{
switch (_consistency)
{
case WriteTo write: return Nodes.Count - (write.Count - 1);
case WriteAll _: return 0;
case WriteMajority write:
{
// +1 because local node is not included in 'Nodes'
var n = Nodes.Count + 1;
var w = CalculateMajority(write.MinCapacity, n, 0);
Log.Debug("WriteMajority [{0}] [{1}] of [{2}].", _key, w, n);
return n - w;
}
case WriteMajorityPlus write:
{
// +1 because local node is not included in 'Nodes'
var n = Nodes.Count + 1;
var w = CalculateMajority(write.MinCapacity, n, write.Additional);
Log.Debug("WriteMajorityPlus [{0}] [{1}] of [{2}].", _key, w, n);
return n - w;
}
case WriteLocal _: throw new ArgumentException("WriteAggregator does not support WriteLocal");
default: throw new ArgumentException("Invalid consistency level");
}
}
protected virtual Address SenderAddress => Sender.Path.Address;
protected override void PreStart()
{
var msg = (object)_delta ?? _write;
foreach (var n in PrimaryNodes)
{
var replica = Replica(n);
replica.Tell(msg);
}
if (IsDone) Reply(isTimeout: false);
}
protected override bool Receive(object message)
{
switch (message)
{
case WriteAck _:
Remaining = Remaining.Remove(SenderAddress);
if (IsDone) Reply(isTimeout: false);
return true;
case WriteNack _:
_gotNackFrom = _gotNackFrom.Add(SenderAddress);
if (IsDone) Reply(isTimeout: false);
return true;
case DeltaNack _:
Sender.Tell(_write);
return true;
case UpdateSuccess _:
_gotLocalStoreReply = true;
if (IsDone) Reply(isTimeout: false);
return true;
case StoreFailure _:
_gotLocalStoreReply = true;
_gotNackFrom = _gotNackFrom.Add(_selfUniqueAddress.Address);
if (IsDone) Reply(isTimeout: false);
return true;
case SendToSecondary _:
if (_delta != null)
{
// Deltas must be applied in order and we can't keep track of ordering of
// simultaneous updates so there is a chance that the delta could not be applied.
// Try again with the full state to the primary nodes that have not acked.
foreach (var address in PrimaryNodes)
{
if (Remaining.Contains(address))
Replica(address).Tell(_write);
}
}
foreach (var n in SecondaryNodes)
Replica(n).Tell(_write);
return true;
case ReceiveTimeout _:
Reply(isTimeout: true);
return true;
}
return false;
}
private void Reply(bool isTimeout)
{
var notEnoughNodes = NotEnoughNodes;
var isDelete = _envelope.Data is DeletedData;
var done = DoneWhenRemainingSize;
var isSuccess = Remaining.Count <= DoneWhenRemainingSize && !notEnoughNodes;
Log.Debug("write acks remaining: {0}, done when: {1}", Remaining.Count, done);
var isTimeoutOrNotEnoughNodes = isTimeout || notEnoughNodes || _gotNackFrom.IsEmpty;
object reply;
if (isSuccess && isDelete) reply = new DeleteSuccess(_key, _req);
else if (isSuccess) reply = new UpdateSuccess(_key, _req);
else if (isTimeoutOrNotEnoughNodes && isDelete) reply = new ReplicationDeleteFailure(_key);
else if (isTimeoutOrNotEnoughNodes || !_durable) reply = new UpdateTimeout(_key, _req);
else reply = new StoreFailure(_key, _req);
_replyTo.Tell(reply, Context.Parent);
Context.Stop(Self);
}
}
public interface IWriteConsistency
{
TimeSpan Timeout { get; }
}
public sealed class WriteLocal : IWriteConsistency
{
public static readonly WriteLocal Instance = new();
public TimeSpan Timeout => TimeSpan.Zero;
public override bool Equals(object obj)
{
return obj != null && obj is WriteLocal;
}
private WriteLocal() { }
public override string ToString() => "WriteLocal";
}
public sealed class WriteTo : IWriteConsistency, IEquatable<WriteTo>
{
public int Count { get; }
public TimeSpan Timeout { get; }
public WriteTo(int count, TimeSpan timeout)
{
if (count < 2) throw new ArgumentException("WriteTo requires count > 2, Use WriteLocal for count=1");
Count = count;
Timeout = timeout;
}
public override bool Equals(object obj) => Equals(obj as WriteTo);
public override string ToString() => $"WriteTo({Count}, timeout={Timeout})";
public bool Equals(WriteTo other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Count == other.Count && Timeout.Equals(other.Timeout);
}
public override int GetHashCode()
{
unchecked
{
return (Count * 397) ^ Timeout.GetHashCode();
}
}
}
public sealed class WriteMajority : IWriteConsistency, IEquatable<WriteMajority>
{
public TimeSpan Timeout { get; }
public int MinCapacity { get; }
public WriteMajority(TimeSpan timeout, int minCapacity = 0)
{
Timeout = timeout;
MinCapacity = minCapacity;
}
public override bool Equals(object obj) => Equals(obj as WriteMajority);
public override string ToString() => $"WriteMajority(timeout={Timeout})";
public bool Equals(WriteMajority other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Timeout.Equals(other.Timeout) && MinCapacity == other.MinCapacity;
}
public override int GetHashCode()
{
unchecked
{
return (Timeout.GetHashCode() * 397) ^ MinCapacity;
}
}
}
/// <summary>
/// <see cref="WriteMajority"/> but with the given number of <see cref="Additional"/> nodes added to the majority count. At most
/// all nodes. Exiting nodes are excluded using `WriteMajorityPlus` because those are typically
/// about to be removed and will not be able to respond.
/// </summary>
public sealed class WriteMajorityPlus : IWriteConsistency, IEquatable<WriteMajorityPlus>
{
public TimeSpan Timeout { get; }
public int Additional { get; }
public int MinCapacity { get; }
public WriteMajorityPlus(TimeSpan timeout, int additional, int minCapacity = 0)
{
Timeout = timeout;
Additional = additional;
MinCapacity = minCapacity;
}
public override bool Equals(object obj) => Equals(obj as WriteMajorityPlus);
public override string ToString() => $"WriteMajorityPlus(timeout={Timeout}, additional={Additional})";
public bool Equals(WriteMajorityPlus other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Timeout.Equals(other.Timeout) && Additional == other.Additional && MinCapacity == other.MinCapacity;
}
public override int GetHashCode()
{
unchecked
{
int hashCode = 13;
hashCode = (hashCode * 397) ^ Timeout.GetHashCode();
hashCode = (hashCode * 397) ^ Additional.GetHashCode();
hashCode = (hashCode * 397) ^ MinCapacity.GetHashCode();
return hashCode;
}
}
}
public sealed class WriteAll : IWriteConsistency, IEquatable<WriteAll>
{
public TimeSpan Timeout { get; }
public WriteAll(TimeSpan timeout)
{
Timeout = timeout;
}
public override bool Equals(object obj) => Equals(obj as WriteAll);
public override string ToString() => $"WriteAll(timeout={Timeout})";
public bool Equals(WriteAll other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Timeout.Equals(other.Timeout);
}
public override int GetHashCode()
{
return Timeout.GetHashCode();
}
}
}