-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathResizer.cs
380 lines (350 loc) · 16.8 KB
/
Resizer.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
//-----------------------------------------------------------------------
// <copyright file="Resizer.cs" company="Akka.NET Project">
// Copyright (C) 2009-2021 Lightbend Inc. <http://www.lightbend.com>
// Copyright (C) 2013-2021 .NET Foundation <https://github.com/akkadotnet/akka.net>
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using Akka.Actor;
using Akka.Configuration;
namespace Akka.Routing
{
/// <summary>
/// <see cref="Akka.Routing.Pool"/> routers with dynamically resizable number of routees are implemented by providing a Resizer
/// implementation in the <see cref="Akka.Routing.Pool"/> configuration
/// </summary>
public abstract class Resizer
{
/// <summary>
/// Is it time for resizing. Typically implemented with modulo of nth message, but
/// could be based on elapsed time or something else. The messageCounter starts with 0
/// for the initial resize and continues with 1 for the first message. Make sure to perform
/// initial resize before first message (messageCounter == 0), because there is no guarantee
/// that resize will be done when concurrent messages are in play.
///
/// CAUTION: this method is invoked from the thread which tries to send a
/// message to the pool, i.e. the ActorRef.!() method, hence it may be called
/// concurrently.
/// </summary>
/// <param name="messageCounter">TBD</param>
/// <returns>TBD</returns>
public abstract bool IsTimeForResize(long messageCounter);
/// <summary>
/// Decide if the capacity of the router need to be changed. Will be invoked when `isTimeForResize`
/// returns true and no other resize is in progress.
///
/// Return the number of routees to add or remove. Negative value will remove that number of routees.
/// Positive value will add that number of routess. 0 will not change the routees.
///
/// This method is invoked only in the context of the Router actor.
/// </summary>
/// <param name="currentRoutees">TBD</param>
/// <returns>TBD</returns>
public abstract int Resize(IEnumerable<Routee> currentRoutees);
/// <summary>
/// TBD
/// </summary>
/// <param name="parentConfig">TBD</param>
/// <returns>TBD</returns>
public static Resizer FromConfig(Config parentConfig)
{
var defaultResizerConfig = parentConfig.GetConfig("resizer");
if (!defaultResizerConfig.IsNullOrEmpty() && defaultResizerConfig.GetBoolean("enabled", false))
{
return DefaultResizer.Apply(defaultResizerConfig);
}
else
{
return null;
}
}
}
/// <summary>
/// Implementation of <see cref="Akka.Routing.Resizer"/> that adjust the <see cref="Akka.Routing.Pool"/> based on specified thresholds.
/// </summary>
public class DefaultResizer : Resizer, IEquatable<DefaultResizer>
{
/// <summary>
/// Initializes a new instance of the <see cref="DefaultResizer"/> class.
/// </summary>
/// <param name="lower">TBD</param>
/// <param name="upper">TBD</param>
/// <param name="pressureThreshold">TBD</param>
/// <param name="rampupRate">TBD</param>
/// <param name="backoffThreshold">TBD</param>
/// <param name="backoffRate">TBD</param>
/// <param name="messagesPerResize">TBD</param>
/// <exception cref="ArgumentException">
/// This exception can be thrown for a number of reasons. These include:
/// <ul>
/// <li>The given <paramref name="lower"/> bound was negative.</li>
/// <li>The given <paramref name="upper"/> bound was negative.</li>
/// <li>The given <paramref name="upper"/> bound was below the <paramref name="lower"/>bound.</li>
/// <li>The given <paramref name="rampupRate"/> was negative.</li>
/// <li>The given <paramref name="backoffThreshold"/> was greater than one.</li>
/// <li>The given <paramref name="backoffRate"/> was negative.</li>
/// <li>The given <paramref name="messagesPerResize"/> was less than one.</li>
/// </ul>
/// </exception>
public DefaultResizer(
int lower,
int upper,
int pressureThreshold = 1,
double rampupRate = 0.2d,
double backoffThreshold = 0.3d,
double backoffRate = 0.1d,
int messagesPerResize = 10)
{
if (lower < 0)
throw new ArgumentException($"lowerBound must be >= 0, was: {lower}", nameof(lower));
if (upper < 0)
throw new ArgumentException($"upperBound must be >= 0, was: {upper}", nameof(upper));
if (upper < lower)
throw new ArgumentException($"upperBound must be >= lowerBound, was: {upper} < {lower}", nameof(upper));
if (rampupRate < 0.0)
throw new ArgumentException($"rampupRate must be >= 0.0, was {rampupRate}", nameof(rampupRate));
if (backoffThreshold > 1.0)
throw new ArgumentException($"backoffThreshold must be <= 1.0, was {backoffThreshold}", nameof(backoffThreshold));
if (backoffRate < 0.0)
throw new ArgumentException($"backoffRate must be >= 0.0, was {backoffRate}", nameof(backoffRate));
if (messagesPerResize <= 0)
throw new ArgumentException($"messagesPerResize must be > 0, was {messagesPerResize}", nameof(messagesPerResize));
LowerBound = lower;
UpperBound = upper;
PressureThreshold = pressureThreshold;
RampupRate = rampupRate;
BackoffThreshold = backoffThreshold;
BackoffRate = backoffRate;
MessagesPerResize = messagesPerResize;
}
/// <summary>
/// TBD
/// </summary>
/// <param name="resizerConfig">TBD</param>
/// <returns>TBD</returns>
public new static DefaultResizer FromConfig(Config resizerConfig)
{
return resizerConfig.GetBoolean("resizer.enabled", false) ? DefaultResizer.Apply(resizerConfig.GetConfig("resizer")) : null;
}
/// <summary>
/// Creates a new DefaultResizer from the given configuration
/// </summary>
/// <param name="resizerConfig">TBD</param>
/// <returns>TBD</returns>
internal static DefaultResizer Apply(Config resizerConfig)
{
if (resizerConfig.IsNullOrEmpty())
throw ConfigurationException.NullOrEmptyConfig<DefaultResizer>();
return new DefaultResizer(
resizerConfig.GetInt("lower-bound", 0),
resizerConfig.GetInt("upper-bound", 0),
resizerConfig.GetInt("pressure-threshold", 0),
resizerConfig.GetDouble("rampup-rate", 0),
resizerConfig.GetDouble("backoff-threshold", 0),
resizerConfig.GetDouble("backoff-rate", 0),
resizerConfig.GetInt("messages-per-resize", 0)
);
}
/// <summary>
/// TBD
/// </summary>
/// <param name="messageCounter">TBD</param>
/// <returns>TBD</returns>
public override bool IsTimeForResize(long messageCounter)
{
return messageCounter % MessagesPerResize == 0;
}
/// <summary>
/// TBD
/// </summary>
/// <param name="currentRoutees">TBD</param>
/// <returns>TBD</returns>
public override int Resize(IEnumerable<Routee> currentRoutees)
{
return Capacity(currentRoutees);
}
/// <summary>
/// Returns the overall desired change in resizer capacity. Positive value will
/// add routees to the resizer. Negative value will remove routees from the
/// resizer
/// </summary>
/// <param name="currentRoutees">The current actor in the resizer</param>
/// <returns>the number of routees by which the resizer should be adjusted (positive, negative or zero)</returns>
public int Capacity(IEnumerable<Routee> currentRoutees)
{
var routees = currentRoutees as Routee[] ?? currentRoutees.ToArray();
var currentSize = routees.Length;
var pressure = Pressure(routees);
var delta = Filter(pressure, currentSize);
var proposed = currentSize + delta;
if (proposed < LowerBound)
return delta + (LowerBound - proposed);
if (proposed > UpperBound)
return delta - (proposed - UpperBound);
return delta;
}
/// <summary>
/// Number of routees considered busy, or above 'pressure level'.
///
/// Implementation depends on the value of `pressureThreshold`
/// (default is 1).
/// <ul>
/// <li> 0: number of routees currently processing a message.</li>
/// <li> 1: number of routees currently processing a message has
/// some messages in mailbox.</li>
/// <li> > 1: number of routees with at least the configured `pressureThreshold`
/// messages in their mailbox. Note that estimating mailbox size of
/// default UnboundedMailbox is O(N) operation.</li>
/// </ul>
/// </summary>
/// <param name="currentRoutees">An enumeration of the current routees</param>
/// <returns>The number of routees considered to be above pressure level.</returns>
public int Pressure(IEnumerable<Routee> currentRoutees)
{
return currentRoutees.Count(
routee =>
{
if (routee is ActorRefRoutee actorRefRoutee && actorRefRoutee.Actor is ActorRefWithCell actorRef)
{
var underlying = actorRef.Underlying;
if (underlying is ActorCell cell)
{
if (PressureThreshold == 1)
return cell.Mailbox.IsScheduled() && cell.Mailbox.HasMessages;
if (PressureThreshold < 1)
return cell.Mailbox.IsScheduled() && cell.CurrentMessage != null;
return cell.Mailbox.NumberOfMessages >= PressureThreshold;
}
else
{
if (PressureThreshold == 1)
return underlying.HasMessages;
if (PressureThreshold < 1)
return true; //unstarted cells are always busy, for instance
return underlying.NumberOfMessages >= PressureThreshold;
}
}
return false;
});
}
/// <summary>
/// This method can be used to smooth the capacity delta by considering
/// the current pressure and current capacity.
/// </summary>
/// <param name="pressure">pressure current number of busy routees</param>
/// <param name="capacity">capacity current number of routees</param>
/// <returns>proposed change in the capacity</returns>
public int Filter(int pressure, int capacity)
{
return Rampup(pressure, capacity) + Backoff(pressure, capacity);
}
/// <summary>
/// Computes a proposed positive (or zero) capacity delta using
/// the configured `rampupRate`.
/// </summary>
/// <param name="pressure">the current number of busy routees</param>
/// <param name="capacity">the current number of total routees</param>
/// <returns>proposed increase in capacity</returns>
public int Rampup(int pressure, int capacity)
{
return pressure < capacity ? 0 : Convert.ToInt32(Math.Ceiling(RampupRate * capacity));
}
/// <summary>
/// Computes a proposed negative (or zero) capacity delta using
/// the configured `backoffThreshold` and `backoffRate`
/// </summary>
/// <param name="pressure">pressure current number of busy routees</param>
/// <param name="capacity">capacity current number of routees</param>
/// <returns>proposed decrease in capacity (as a negative number)</returns>
public int Backoff(int pressure, int capacity)
{
if (BackoffThreshold > 0.0 && BackoffRate > 0.0 && capacity > 0 && (Convert.ToDouble(pressure) / Convert.ToDouble(capacity)) < BackoffThreshold)
{
return Convert.ToInt32(Math.Floor(-1.0 * BackoffRate * capacity));
}
return 0;
}
/// <summary>
/// The fewest number of routees the router should ever have.
/// </summary>
public int LowerBound { get; set; }
/// <summary>
/// The most number of routees the router should ever have.
/// Must be greater than or equal to `lowerBound`.
/// </summary>
public int UpperBound { get; set; }
/// <summary>
/// * Threshold to evaluate if routee is considered to be busy (under pressure).
/// Implementation depends on this value (default is 1).
/// <ul>
/// <li> 0: number of routees currently processing a message.</li>
/// <li> 1: number of routees currently processing a message has
/// some messages in mailbox.</li>
/// <li> > 1: number of routees with at least the configured `pressureThreshold`
/// messages in their mailbox. Note that estimating mailbox size of
/// default UnboundedMailbox is O(N) operation.</li>
/// </ul>
/// </summary>
public int PressureThreshold { get; private set; }
/// <summary>
/// Percentage to increase capacity whenever all routees are busy.
/// For example, 0.2 would increase 20% (rounded up), i.e. if current
/// capacity is 6 it will request an increase of 2 more routees.
/// </summary>
public double RampupRate { get; private set; }
/// <summary>
/// Minimum fraction of busy routees before backing off.
/// For example, if this is 0.3, then we'll remove some routees only when
/// less than 30% of routees are busy, i.e. if current capacity is 10 and
/// 3 are busy then the capacity is unchanged, but if 2 or less are busy
/// the capacity is decreased.
///
/// Use 0.0 or negative to avoid removal of routees.
/// </summary>
public double BackoffThreshold { get; private set; }
/// <summary>
/// Fraction of routees to be removed when the resizer reaches the
/// backoffThreshold.
/// For example, 0.1 would decrease 10% (rounded up), i.e. if current
/// capacity is 9 it will request an decrease of 1 routee.
/// </summary>
public double BackoffRate { get; private set; }
/// <summary>
/// Number of messages between resize operation.
/// Use 1 to resize before each message.
/// </summary>
public int MessagesPerResize { get; private set; }
/// <inheritdoc/>
public bool Equals(DefaultResizer other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return MessagesPerResize == other.MessagesPerResize && BackoffRate.Equals(other.BackoffRate) && RampupRate.Equals(other.RampupRate) && BackoffThreshold.Equals(other.BackoffThreshold) && UpperBound == other.UpperBound && PressureThreshold == other.PressureThreshold && LowerBound == other.LowerBound;
}
/// <inheritdoc/>
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((DefaultResizer)obj);
}
/// <inheritdoc/>
public override int GetHashCode()
{
unchecked
{
int hashCode = MessagesPerResize;
hashCode = (hashCode * 397) ^ BackoffRate.GetHashCode();
hashCode = (hashCode * 397) ^ RampupRate.GetHashCode();
hashCode = (hashCode * 397) ^ BackoffThreshold.GetHashCode();
hashCode = (hashCode * 397) ^ UpperBound;
hashCode = (hashCode * 397) ^ PressureThreshold;
hashCode = (hashCode * 397) ^ LowerBound;
return hashCode;
}
}
}
}