summaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/util/shared_operation_throttler.cpp
blob: e91be68a67176052860830cc43010c09b5d37cf3 (plain) (blame)
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
381
382
383
384
385
386
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "shared_operation_throttler.h"
#include <condition_variable>
#include <cassert>
#include <functional>
#include <mutex>

namespace vespalib {

namespace {

class NoLimitsOperationThrottler final : public SharedOperationThrottler {
public:
    ~NoLimitsOperationThrottler() override = default;
    Token blocking_acquire_one() noexcept override {
        return Token(this, TokenCtorTag{});
    }
    Token blocking_acquire_one(vespalib::duration) noexcept override {
        return Token(this, TokenCtorTag{});
    }
    Token try_acquire_one() noexcept override {
        return Token(this, TokenCtorTag{});
    }
    uint32_t current_window_size() const noexcept override { return 0; }
    uint32_t waiting_threads() const noexcept override { return 0; }
private:
    void release_one() noexcept override { /* no-op */ }
};

/**
 * Effectively a 1-1 transplant of the MessageBus DynamicThrottlePolicy, but
 * without an underlying StaticThrottlePolicy and with no need for individual
 * MessageBus Message/Reply objects.
 *
 * Please keep the underlying algorithm in sync with the Java implementation,
 * as that is considered the source of truth. For descriptions of the various
 * parameters, also see the Java code:
 *   messagebus/src/main/java/com/yahoo/messagebus/DynamicThrottlePolicy.java
 */
class DynamicThrottlePolicy {
    std::function<steady_time()> _time_provider;
    uint32_t _num_sent;
    uint32_t _num_ok;
    double   _resize_rate;
    uint64_t _resize_time;
    uint64_t _time_of_last_message;
    uint64_t _idle_time_period;
    double   _efficiency_threshold;
    double   _window_size_increment;
    double   _window_size;
    double   _max_window_size;
    double   _min_window_size;
    double   _decrement_factor;
    double   _window_size_backoff;
    double   _weight;
    double   _local_max_throughput;
public:
    DynamicThrottlePolicy(const SharedOperationThrottler::DynamicThrottleParams& params,
                          std::function<steady_time()> time_provider);

    void set_window_size_increment(double window_size_increment) noexcept;
    void set_window_size_backoff(double window_size_backoff) noexcept;
    void set_resize_rate(double resize_rate) noexcept;
    void set_max_window_size(double max_size) noexcept;

    void set_min_window_size(double min_size) noexcept;
    void set_window_size_decrement_factor(double decrement_factor) noexcept;

    [[nodiscard]] uint32_t current_window_size() const noexcept {
        return static_cast<uint32_t>(_window_size);
    }
    [[nodiscard]] bool has_spare_capacity(uint32_t pending_count) noexcept;
    void process_request() noexcept;
    void process_response(bool success) noexcept;

private:
    [[nodiscard]] uint64_t current_time_as_millis() noexcept {
        return count_ms(_time_provider().time_since_epoch());
    }
};

DynamicThrottlePolicy::DynamicThrottlePolicy(const SharedOperationThrottler::DynamicThrottleParams& params,
                                             std::function<steady_time()> time_provider)
    : _time_provider(std::move(time_provider)),
      _num_sent(0),
      _num_ok(0),
      _resize_rate(3.0),
      _resize_time(0),
      _time_of_last_message(current_time_as_millis()),
      _idle_time_period(60000),
      _efficiency_threshold(1),
      _window_size_increment(20),
      _window_size(_window_size_increment),
      _max_window_size(INT_MAX),
      _min_window_size(_window_size_increment),
      _decrement_factor(2.0),
      _window_size_backoff(0.9),
      _weight(1),
      _local_max_throughput(0)
{
    // We use setters for convenience, since setting one parameter may imply setting others,
    // based on it, and there's frequently min/max capping of values.
    set_window_size_increment(params.window_size_increment);
    set_min_window_size(params.min_window_size);
    set_max_window_size(params.max_window_size);
    set_resize_rate(params.resize_rate);
    set_window_size_decrement_factor(params.window_size_decrement_factor);
    set_window_size_backoff(params.window_size_backoff);
}

void
DynamicThrottlePolicy::set_window_size_increment(double window_size_increment) noexcept
{
    _window_size_increment = window_size_increment;
    _window_size = std::max(_window_size, _window_size_increment);
}

void
DynamicThrottlePolicy::set_window_size_backoff(double window_size_backoff) noexcept
{
    _window_size_backoff = std::max(0.0, std::min(1.0, window_size_backoff));
}

void
DynamicThrottlePolicy::set_resize_rate(double resize_rate) noexcept
{
    _resize_rate = std::max(2.0, resize_rate);
}

void
DynamicThrottlePolicy::set_max_window_size(double max_size) noexcept
{
    _max_window_size = max_size;
}

void
DynamicThrottlePolicy::set_min_window_size(double min_size) noexcept
{
    _min_window_size = min_size;
    _window_size = std::max(_min_window_size, _window_size_increment);
}

void
DynamicThrottlePolicy::set_window_size_decrement_factor(double decrement_factor) noexcept
{
    _decrement_factor = decrement_factor;
}

bool
DynamicThrottlePolicy::has_spare_capacity(uint32_t pending_count) noexcept
{
    const uint64_t time = current_time_as_millis();
    if ((time - _time_of_last_message) > _idle_time_period) {
        _window_size = std::max(_min_window_size, std::min(_window_size, pending_count + _window_size_increment));
    }
    _time_of_last_message = time;
    const auto window_size_floored = static_cast<uint32_t>(_window_size);
    // Use floating point window sizes, so the algorithm sees the difference between 1.1 and 1.9 window size.
    const bool carry = _num_sent < ((_window_size * _resize_rate) * (_window_size - window_size_floored));
    return pending_count < (window_size_floored + (carry ? 1 : 0));
}

void
DynamicThrottlePolicy::process_request() noexcept
{
    if (++_num_sent < (_window_size * _resize_rate)) {
        return;
    }

    const uint64_t time = current_time_as_millis();
    const double elapsed = time - _resize_time;
    _resize_time = time;

    const double throughput = _num_ok / elapsed;
    _num_sent = 0;
    _num_ok = 0;

    if (throughput > _local_max_throughput) {
        _local_max_throughput = throughput;
        _window_size += _weight * _window_size_increment;
    } else {
        // scale up/down throughput for comparing to window size
        double period = 1;
        while ((throughput * (period / _window_size)) < 2) {
            period *= 10;
        }
        while ((throughput * (period / _window_size)) > 2) {
            period *= 0.1;
        }
        const double efficiency = throughput * (period / _window_size);

        if (efficiency < _efficiency_threshold) {
            _window_size = std::min(_window_size * _window_size_backoff,
                                    _window_size - _decrement_factor * _window_size_increment);
            _local_max_throughput = 0;
        } else {
            _window_size += _weight * _window_size_increment;
        }
    }
    _window_size = std::max(_min_window_size, _window_size);
    _window_size = std::min(_max_window_size, _window_size);
}

void
DynamicThrottlePolicy::process_response(bool success) noexcept
{
    if (success) {
        ++_num_ok;
    }
}

class DynamicOperationThrottler final : public SharedOperationThrottler {
    mutable std::mutex      _mutex;
    std::condition_variable _cond;
    DynamicThrottlePolicy   _throttle_policy;
    uint32_t                _pending_ops;
    uint32_t                _waiting_threads;
public:
    DynamicOperationThrottler(const DynamicThrottleParams& params,
                              std::function<steady_time()> time_provider);
    ~DynamicOperationThrottler() override;

    Token blocking_acquire_one() noexcept override;
    Token blocking_acquire_one(vespalib::duration timeout) noexcept override;
    Token try_acquire_one() noexcept override;
    uint32_t current_window_size() const noexcept override;
    uint32_t waiting_threads() const noexcept override;
private:
    void release_one() noexcept override;
    // Non-const since actually checking the send window of a dynamic throttler might change
    // it if enough time has passed.
    [[nodiscard]] bool has_spare_capacity_in_active_window() noexcept;
    void add_one_to_active_window_size() noexcept;
    void subtract_one_from_active_window_size() noexcept;
};

DynamicOperationThrottler::DynamicOperationThrottler(const DynamicThrottleParams& params,
                                                     std::function<steady_time()> time_provider)
    : _mutex(),
      _cond(),
      _throttle_policy(params, std::move(time_provider)),
      _pending_ops(0),
      _waiting_threads(0)
{
}

DynamicOperationThrottler::~DynamicOperationThrottler() = default;

bool
DynamicOperationThrottler::has_spare_capacity_in_active_window() noexcept
{
    return _throttle_policy.has_spare_capacity(_pending_ops);
}

void
DynamicOperationThrottler::add_one_to_active_window_size() noexcept
{
    _throttle_policy.process_request();
    ++_pending_ops;
}

void
DynamicOperationThrottler::subtract_one_from_active_window_size() noexcept
{
    _throttle_policy.process_response(true); // TODO support failure push-back
    assert(_pending_ops > 0);
    --_pending_ops;
}

DynamicOperationThrottler::Token
DynamicOperationThrottler::blocking_acquire_one() noexcept
{
    std::unique_lock lock(_mutex);
    if (!has_spare_capacity_in_active_window()) {
        ++_waiting_threads;
        _cond.wait(lock, [&] {
            return has_spare_capacity_in_active_window();
        });
        --_waiting_threads;
    }
    add_one_to_active_window_size();
    return Token(this, TokenCtorTag{});
}

DynamicOperationThrottler::Token
DynamicOperationThrottler::blocking_acquire_one(vespalib::duration timeout) noexcept
{
    std::unique_lock lock(_mutex);
    if (!has_spare_capacity_in_active_window()) {
        ++_waiting_threads;
        const bool accepted = _cond.wait_for(lock, timeout, [&] {
            return has_spare_capacity_in_active_window();
        });
        --_waiting_threads;
        if (!accepted) {
            return Token();
        }
    }
    add_one_to_active_window_size();
    return Token(this, TokenCtorTag{});
}

DynamicOperationThrottler::Token
DynamicOperationThrottler::try_acquire_one() noexcept
{
    std::unique_lock lock(_mutex);
    if (!has_spare_capacity_in_active_window()) {
        return Token();
    }
    add_one_to_active_window_size();
    return Token(this, TokenCtorTag{});
}

void
DynamicOperationThrottler::release_one() noexcept
{
    std::unique_lock lock(_mutex);
    subtract_one_from_active_window_size();
    // Only wake up a waiting thread if doing so would possibly result in success.
    if ((_waiting_threads > 0) && has_spare_capacity_in_active_window()) {
        lock.unlock();
        _cond.notify_one();
    }
}

uint32_t
DynamicOperationThrottler::current_window_size() const noexcept
{
    std::unique_lock lock(_mutex);
    return _throttle_policy.current_window_size();
}

uint32_t
DynamicOperationThrottler::waiting_threads() const noexcept
{
    std::unique_lock lock(_mutex);
    return _waiting_threads;
}

} // anonymous namespace

std::unique_ptr<SharedOperationThrottler>
SharedOperationThrottler::make_unlimited_throttler()
{
    return std::make_unique<NoLimitsOperationThrottler>();
}

std::unique_ptr<SharedOperationThrottler>
SharedOperationThrottler::make_dynamic_throttler(const DynamicThrottleParams& params)
{
    return std::make_unique<DynamicOperationThrottler>(params, []() noexcept { return steady_clock::now(); });
}

std::unique_ptr<SharedOperationThrottler>
SharedOperationThrottler::make_dynamic_throttler(const DynamicThrottleParams& params,
                                                 std::function<steady_time()> time_provider)
{
    return std::make_unique<DynamicOperationThrottler>(params, std::move(time_provider));
}

DynamicOperationThrottler::Token::~Token()
{
    if (_throttler) {
        _throttler->release_one();
    }
}

void
DynamicOperationThrottler::Token::reset() noexcept
{
    if (_throttler) {
        _throttler->release_one();
        _throttler = nullptr;
    }
}

DynamicOperationThrottler::Token&
DynamicOperationThrottler::Token::operator=(Token&& rhs) noexcept
{
    reset();
    _throttler = rhs._throttler;
    rhs._throttler = nullptr;
    return *this;
}

}