-
Notifications
You must be signed in to change notification settings - Fork 170
/
Copy pathreader_concurrency_semaphore.hh
270 lines (222 loc) · 8.81 KB
/
reader_concurrency_semaphore.hh
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
/*
* This file is part of Scylla.
*
* Scylla is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Scylla is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Scylla. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Copyright (C) 2017 ScyllaDB
*/
#pragma once
#include <map>
#include <seastar/core/file.hh>
#include <seastar/core/future.hh>
#include <seastar/core/semaphore.hh>
#include "db/timeout_clock.hh"
using namespace seastar;
/// Specific semaphore for controlling reader concurrency
///
/// Before creating a reader one should obtain a permit by calling
/// `wait_admission()`. This permit can then be used for tracking the
/// reader's memory consumption via `reader_resource_tracker`.
/// The permit should be held onto for the lifetime of the reader
/// and/or any buffer its tracking.
/// Reader concurrency is dual limited by count and memory.
/// The semaphore can be configured with the desired limits on
/// construction. New readers will only be admitted when there is both
/// enough count and memory units available. Readers are admitted in
/// FIFO order.
/// It's possible to specify the maximum allowed number of waiting
/// readers by the `max_queue_length` constructor parameter. When the
/// number waiting readers would be equal or greater than this number
/// (when calling `wait_admission()`) an exception will be thrown.
/// The type of the exception and optionally some additional code
/// that should be executed when this happens can be customized by the
/// `raise_queue_overloaded_exception` constructor parameter. This
/// function will be called every time the queue limit is surpassed.
/// It is expected to return an `std::exception_ptr` that will be
/// injected into the future.
class reader_concurrency_semaphore {
public:
struct resources {
int count = 0;
ssize_t memory = 0;
resources() = default;
resources(int count, ssize_t memory)
: count(count)
, memory(memory) {
}
bool operator>=(const resources& other) const {
return count >= other.count && memory >= other.memory;
}
resources& operator-=(const resources& other) {
count -= other.count;
memory -= other.memory;
return *this;
}
resources& operator+=(const resources& other) {
count += other.count;
memory += other.memory;
return *this;
}
explicit operator bool() const {
return count >= 0 && memory >= 0;
}
};
class reader_permit {
reader_concurrency_semaphore& _semaphore;
const resources _base_cost;
public:
reader_permit(reader_concurrency_semaphore& semaphore, resources base_cost)
: _semaphore(semaphore)
, _base_cost(base_cost) {
}
~reader_permit() {
_semaphore.signal(_base_cost);
}
reader_permit(const reader_permit&) = delete;
reader_permit& operator=(const reader_permit&) = delete;
reader_permit(reader_permit&& other) = delete;
reader_permit& operator=(reader_permit&& other) = delete;
void consume_memory(size_t memory) {
_semaphore.consume_memory(memory);
}
void signal_memory(size_t memory) {
_semaphore.signal_memory(memory);
}
};
class inactive_read {
public:
virtual void evict() = 0;
virtual ~inactive_read() = default;
};
class inactive_read_handle {
uint64_t _id = 0;
friend class reader_concurrency_semaphore;
inactive_read_handle() = default;
explicit inactive_read_handle(uint64_t id)
: _id(id) {
}
public:
explicit operator bool() const {
return bool(_id);
}
};
private:
struct entry {
promise<lw_shared_ptr<reader_permit>> pr;
resources res;
entry(promise<lw_shared_ptr<reader_permit>>&& pr, resources r) : pr(std::move(pr)), res(r) {}
};
struct expiry_handler {
void operator()(entry& e) noexcept {
e.pr.set_exception(semaphore_timed_out());
}
};
private:
resources _resources;
expiring_fifo<entry, expiry_handler, db::timeout_clock> _wait_list;
size_t _max_queue_length = std::numeric_limits<size_t>::max();
std::function<std::exception_ptr()> _make_queue_overloaded_exception;
uint64_t _next_id = 1;
std::map<uint64_t, std::unique_ptr<inactive_read>> _inactive_reads;
private:
static std::exception_ptr default_make_queue_overloaded_exception() {
return std::make_exception_ptr(std::runtime_error("restricted mutation reader queue overload"));
}
bool has_available_units(const resources& r) const {
return bool(_resources) && _resources >= r;
}
bool may_proceed(const resources& r) const {
return has_available_units(r) && _wait_list.empty();
}
void consume_memory(size_t memory) {
_resources.memory -= memory;
}
void signal(const resources& r);
void signal_memory(size_t memory) {
signal(resources(0, static_cast<ssize_t>(memory)));
}
public:
struct no_limits { };
reader_concurrency_semaphore(int count,
ssize_t memory,
size_t max_queue_length = std::numeric_limits<size_t>::max(),
std::function<std::exception_ptr()> raise_queue_overloaded_exception = default_make_queue_overloaded_exception)
: _resources(count, memory)
, _max_queue_length(max_queue_length)
, _make_queue_overloaded_exception(raise_queue_overloaded_exception) {
}
/// Create a semaphore with practically unlimited count and memory.
///
/// And conversely, no queue limit either.
explicit reader_concurrency_semaphore(no_limits)
: reader_concurrency_semaphore(std::numeric_limits<int>::max(), std::numeric_limits<ssize_t>::max()) {
}
reader_concurrency_semaphore(const reader_concurrency_semaphore&) = delete;
reader_concurrency_semaphore& operator=(const reader_concurrency_semaphore&) = delete;
reader_concurrency_semaphore(reader_concurrency_semaphore&&) = delete;
reader_concurrency_semaphore& operator=(reader_concurrency_semaphore&&) = delete;
/// Register an inactive read.
///
/// The semaphore will evict this read when there is a shortage of
/// permits. This might be immediate, during this register call.
/// Clients can use the returned handle to unregister the read, when it
/// stops being inactive and hence evictable.
///
/// An inactive read is an object implementing the `inactive_read`
/// interface.
/// The semaphore takes ownership of the created object and destroys it if
/// it is evicted.
inactive_read_handle register_inactive_read(std::unique_ptr<inactive_read> ir);
/// Unregister the previously registered inactive read.
///
/// If the read was not evicted, the inactive read object, passed in to the
/// register call, will be returned. Otherwise a nullptr is returned.
std::unique_ptr<inactive_read> unregister_inactive_read(inactive_read_handle irh);
/// Try to evict an inactive read.
///
/// Return true if an inactive read was evicted and false otherwise
/// (if there was no reader to evict).
bool try_evict_one_inactive_read();
void clear_inactive_reads() {
_inactive_reads.clear();
}
future<lw_shared_ptr<reader_permit>> wait_admission(size_t memory, db::timeout_clock::time_point timeout = db::no_timeout);
/// Consume the specific amount of resources without waiting.
lw_shared_ptr<reader_permit> consume_resources(resources r);
const resources available_resources() const {
return _resources;
}
size_t waiters() const {
return _wait_list.size();
}
};
class reader_resource_tracker {
lw_shared_ptr<reader_concurrency_semaphore::reader_permit> _permit;
public:
reader_resource_tracker() = default;
explicit reader_resource_tracker(lw_shared_ptr<reader_concurrency_semaphore::reader_permit> permit)
: _permit(std::move(permit)) {
}
bool operator==(const reader_resource_tracker& other) const {
return _permit == other._permit;
}
file track(file f) const;
lw_shared_ptr<reader_concurrency_semaphore::reader_permit> get_permit() const {
return _permit;
}
};
inline reader_resource_tracker no_resource_tracking() {
return {};
}