-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathAdaptiveSampler.cs
More file actions
271 lines (225 loc) · 9.5 KB
/
AdaptiveSampler.cs
File metadata and controls
271 lines (225 loc) · 9.5 KB
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
// <copyright file="AdaptiveSampler.cs" company="Datadog">
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License.
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
// </copyright>
using System;
using System.Threading;
using Datadog.Trace.Logging;
using Datadog.Trace.Util;
namespace Datadog.Trace.Debugger.RateLimiting
{
/// <summary>
/// An adaptive streaming (non-remembering) sampler.
///
/// The sampler attempts to generate at most N samples per fixed time window in randomized
/// fashion. For this it divides the timeline into 'sampling windows' of constant duration. Each
/// sampling window targets a constant number of samples which are scattered randomly (uniform
/// distribution) throughout the window duration and once the window is over the real stats of
/// incoming events and the number of gathered samples is used to recompute the target probability to
/// use in the following window.
///
/// This will guarantee, if the windows are not excessively large, that the sampler will be able
/// to adjust to the changes in the rate of incoming events.
///
/// However, there might so rapid changes in incoming events rate that we will optimistically use
/// all allowed samples well before the current window has elapsed or, on the other end of the
/// spectrum, there will be to few incoming events and the sampler will not be able to generate the
/// target number of samples.
///
/// To smooth out these hiccups the sampler maintains an under-sampling budget which can be used
/// to compensate for too rapid changes in the incoming events rate and maintain the target average
/// number of samples per window.
/// </summary>
internal sealed class AdaptiveSampler : IAdaptiveSampler
{
private static readonly IDatadogLogger Log = DatadogLogging.GetLoggerFor<AdaptiveSampler>();
/// <summary>
/// Exponential Moving Average (EMA) last element weight.
/// Check out papers about using EMA for streaming data - eg.
/// https://nestedsoftware.com/2018/04/04/exponential-moving-average-on-streaming-data-4hhl.24876.html
///
/// Corresponds to 'lookback' of N values:
/// With T being the index of the most recent value the lookback of N values means that for all values with index
/// T-K, where K > N, the relative weight of that value computed as (1 - alpha)^K is less or equal than the
/// weight assigned by a plain arithmetic average (= 1/N).
/// </summary>
private readonly double _emaAlpha;
private readonly int _samplesPerWindow;
private Counts _countsRef;
private double _probability = 1;
private long _samplesBudget;
private double _totalCountRunningAverage;
private double _avgSamples;
private int _budgetLookback;
private double _budgetAlpha;
private int _countsSlotIndex;
private Counts[] _countsSlots;
private Timer _timer;
private Action _rollWindowCallback;
internal AdaptiveSampler(
TimeSpan windowDuration,
int samplesPerWindow,
int averageLookback,
int budgetLookback,
Action rollWindowCallback)
{
_timer = new Timer(state => RollWindow(), state: null, windowDuration, windowDuration);
_totalCountRunningAverage = 0;
_rollWindowCallback = rollWindowCallback;
_avgSamples = 0;
_countsSlots = new Counts[2] { new(), new() };
if (averageLookback < 1)
{
averageLookback = 1;
}
if (budgetLookback < 1)
{
budgetLookback = 1;
}
_samplesPerWindow = samplesPerWindow;
_budgetLookback = budgetLookback;
_samplesBudget = samplesPerWindow + (budgetLookback * samplesPerWindow);
_emaAlpha = ComputeIntervalAlpha(averageLookback);
_budgetAlpha = ComputeIntervalAlpha(budgetLookback);
_countsRef = _countsSlots[0];
if (windowDuration != TimeSpan.Zero)
{
_timer.Change(windowDuration, windowDuration);
}
}
public bool Sample()
{
_countsRef.AddTest();
if (NextDouble() < _probability)
{
return _countsRef.AddSample(_samplesBudget);
}
return false;
}
public bool Keep()
{
_countsRef.AddTest();
_countsRef.AddSample();
return true;
}
public bool Drop()
{
_countsRef.AddTest();
return false;
}
public double NextDouble()
{
#pragma warning disable CA5394 // Intentional: non-security randomness for rate limiting
return ThreadSafeRandom.Shared.NextDouble();
#pragma warning restore CA5394
}
private double ComputeIntervalAlpha(int lookback)
{
return 1 - Math.Pow(lookback, -1.0 / lookback);
}
private long CalculateBudgetEma(long sampledCount)
{
_avgSamples = double.IsNaN(_avgSamples) || _budgetAlpha <= 0.0
? sampledCount
: _avgSamples + (_budgetAlpha * (sampledCount - _avgSamples));
double result = Math.Round(Math.Max(_samplesPerWindow - _avgSamples, 0.0) * _budgetLookback);
return (long)result;
}
internal void RollWindow()
{
try
{
var counts = _countsSlots[_countsSlotIndex];
// Semi-atomically replace the Counts instance such that sample requests during window maintenance will be
// using the newly created counts instead of the ones currently processed by the maintenance routine.
// We are ok with slightly racy outcome where totaCount and sampledCount may not be totally in sync
// because it allows to avoid contention in the hot-path and the effect on the overall sample rate is minimal
// and will get compensated in the long run.
// Theoretically, a compensating system might be devised but it will always require introducing a single point
// of contention and add a fair amount of complexity. Considering that we are ok with keeping the target sampling
// rate within certain error margins and this data race is not breaking the margin it is better to keep the
// code simple and reasonably fast.
_countsSlotIndex = (_countsSlotIndex + 1) % 2;
_countsRef = _countsSlots[_countsSlotIndex];
var totalCount = counts.TestCount();
var sampledCount = counts.SampleCount();
_samplesBudget = CalculateBudgetEma(sampledCount);
if (_totalCountRunningAverage == 0 || _emaAlpha <= 0.0)
{
_totalCountRunningAverage = totalCount;
}
else
{
_totalCountRunningAverage = _totalCountRunningAverage + (_emaAlpha * (totalCount - _totalCountRunningAverage));
}
if (_totalCountRunningAverage <= 0)
{
_probability = 1;
}
else
{
_probability = Math.Min(_samplesBudget / _totalCountRunningAverage, 1.0);
}
counts.Reset();
if (_rollWindowCallback != null)
{
_rollWindowCallback();
}
}
catch (Exception e)
{
Log.Error(e, "AdaptiveSampler - Failed to roll window");
}
}
internal State GetInternalState()
{
var counts = _countsRef;
return new State
{
TestCount = counts.TestCount(),
SampleCount = counts.SampleCount(),
Budget = _samplesBudget,
Probability = _probability,
TotalAverage = _totalCountRunningAverage
};
}
private sealed class Counts
{
private long _testCount;
private long _sampleCount;
internal void AddTest()
{
Interlocked.Increment(ref _testCount);
}
internal bool AddSample(long limit)
{
return AtomicInt64.GetAndAccumulate(ref _sampleCount, limit, (prev, lim) => Math.Min(prev + 1, lim)) < limit;
}
internal void AddSample()
{
Interlocked.Increment(ref _sampleCount);
}
internal void Reset()
{
_testCount = 0;
_sampleCount = 0;
}
internal long SampleCount()
{
return _sampleCount;
}
internal long TestCount()
{
return _testCount;
}
}
internal sealed class State
{
public long TestCount { get; set; }
public long SampleCount { get; set; }
public long Budget { get; set; }
public double Probability { get; set; }
public double TotalAverage { get; set; }
}
}
}