-
Notifications
You must be signed in to change notification settings - Fork 170
/
Copy pathquerier.cc
459 lines (389 loc) · 17.2 KB
/
querier.cc
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*
* Copyright (C) 2018 ScyllaDB
*/
/*
* 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/>.
*/
#include "querier.hh"
#include "schema.hh"
#include <boost/range/adaptor/map.hpp>
namespace query {
enum class can_use {
yes,
no_schema_version_mismatch,
no_ring_pos_mismatch,
no_clustering_pos_mismatch
};
static sstring cannot_use_reason(can_use cu)
{
switch (cu)
{
case can_use::yes:
return "can be used";
case can_use::no_schema_version_mismatch:
return "schema version mismatch";
case can_use::no_ring_pos_mismatch:
return "ring pos mismatch";
case can_use::no_clustering_pos_mismatch:
return "clustering pos mismatch";
}
return "unknown reason";
}
static bool ring_position_matches(const schema& s, const dht::partition_range& range, const query::partition_slice& slice,
const position_view& pos) {
const auto is_reversed = flat_mutation_reader::consume_reversed_partitions(slice.options.contains(query::partition_slice::option::reversed));
const auto expected_start = dht::ring_position_view(*pos.partition_key);
// If there are no clustering columns or the select is distinct we don't
// have clustering rows at all. In this case we can be sure we won't have
// anything more in the last page's partition and thus the start bound is
// exclusive. Otherwise there migh be clustering rows still and it is
// inclusive.
const auto expected_inclusiveness = s.clustering_key_size() > 0 &&
!slice.options.contains<query::partition_slice::option::distinct>() &&
pos.clustering_key;
const auto comparator = dht::ring_position_comparator(s);
if (is_reversed && !range.is_singular()) {
const auto& end = range.end();
return end && comparator(end->value(), expected_start) == 0 && end->is_inclusive() == expected_inclusiveness;
}
const auto& start = range.start();
return start && comparator(start->value(), expected_start) == 0 && start->is_inclusive() == expected_inclusiveness;
}
static bool clustering_position_matches(const schema& s, const query::partition_slice& slice, const position_view& pos) {
const auto& row_ranges = slice.row_ranges(s, pos.partition_key->key());
if (row_ranges.empty()) {
// This is a valid slice on the last page of a query with
// clustering restrictions. It simply means the query is
// effectively over, no further results are expected. We
// can assume the clustering position matches.
return true;
}
if (!pos.clustering_key) {
// We stopped at a non-clustering position so the partition's clustering
// row ranges should be the default row ranges.
return &row_ranges == &slice.default_row_ranges();
}
clustering_key_prefix::equality eq(s);
const auto is_reversed = flat_mutation_reader::consume_reversed_partitions(slice.options.contains(query::partition_slice::option::reversed));
// If the page ended mid-partition the first partition range should start
// with the last clustering key (exclusive).
const auto& first_row_range = row_ranges.front();
const auto& start = is_reversed ? first_row_range.end() : first_row_range.start();
if (!start) {
return false;
}
return !start->is_inclusive() && eq(start->value(), *pos.clustering_key);
}
static bool ranges_match(const schema& s, const dht::partition_range& original_range, const dht::partition_range& new_range) {
if (original_range.is_singular() != new_range.is_singular()) {
return false;
}
const auto cmp = dht::ring_position_comparator(s);
const auto bound_eq = [&] (const stdx::optional<dht::partition_range::bound>& a, const stdx::optional<dht::partition_range::bound>& b) {
return bool(a) == bool(b) && (!a || a->equal(*b, cmp));
};
// For singular ranges end() == start() so they are interchangeable.
// For non-singular ranges we check only the end().
return bound_eq(original_range.end(), new_range.end());
}
static bool ranges_match(const schema& s, dht::partition_ranges_view original_ranges, dht::partition_ranges_view new_ranges) {
if (new_ranges.empty()) {
return false;
}
if (original_ranges.size() == 1) {
if (new_ranges.size() != 1) {
return false;
}
return ranges_match(s, original_ranges.front(), new_ranges.front());
}
// As the query progresses the number of to-be-read ranges can never surpass
// that of the original ranges.
if (original_ranges.size() < new_ranges.size()) {
return false;
}
// If there is a difference in the size of the range lists we assume we
// already read ranges from the original list and these ranges are missing
// from the head of the new list.
auto new_ranges_it = new_ranges.begin();
auto original_ranges_it = original_ranges.begin() + (original_ranges.size() - new_ranges.size());
// The first range in the new list can be partially read so we only check
// that one of its bounds match that of its original counterpart, just like
// we do with single ranges.
if (!ranges_match(s, *original_ranges_it++, *new_ranges_it++)) {
return false;
}
const auto cmp = dht::ring_position_comparator(s);
// The rest of the list, those ranges that we didn't even started reading
// yet should be *identical* to their original counterparts.
return std::equal(original_ranges_it, original_ranges.end(), new_ranges_it,
[&cmp] (const dht::partition_range& a, const dht::partition_range& b) { return a.equal(b, cmp); });
}
template <typename Querier>
static can_use can_be_used_for_page(const Querier& q, const schema& s, const dht::partition_range& range, const query::partition_slice& slice) {
if (s.version() != q.schema()->version()) {
return can_use::no_schema_version_mismatch;
}
const auto pos = q.current_position();
if (!pos.partition_key) {
// There was nothing read so far so we assume we are ok.
return can_use::yes;
}
if (!ring_position_matches(s, range, slice, pos)) {
return can_use::no_ring_pos_mismatch;
}
if (!clustering_position_matches(s, slice, pos)) {
return can_use::no_clustering_pos_mismatch;
}
return can_use::yes;
}
// The time-to-live of a cache-entry.
const std::chrono::seconds querier_cache::default_entry_ttl{10};
void querier_cache::scan_cache_entries() {
const auto now = lowres_clock::now();
auto it = _entries.begin();
const auto end = _entries.end();
while (it != end && it->is_expired(now)) {
++_stats.time_based_evictions;
--_stats.population;
_sem.unregister_inactive_read(it->get_inactive_handle());
it = _entries.erase(it);
}
}
static querier_cache::entries::iterator find_querier(querier_cache::entries& entries, querier_cache::index& index, utils::UUID key,
dht::partition_ranges_view ranges, tracing::trace_state_ptr trace_state) {
const auto queriers = index.equal_range(key);
if (queriers.first == index.end()) {
tracing::trace(trace_state, "Found no cached querier for key {}", key);
return entries.end();
}
const auto it = std::find_if(queriers.first, queriers.second, [&] (const querier_cache::entry& e) {
return ranges_match(e.schema(), e.ranges(), ranges);
});
if (it == queriers.second) {
tracing::trace(trace_state, "Found cached querier(s) for key {} but none matches the query range(s) {}", key, ranges);
return entries.end();
}
tracing::trace(trace_state, "Found cached querier for key {} and range(s) {}", key, ranges);
return it->pos();
}
querier_cache::querier_cache(reader_concurrency_semaphore& sem, size_t max_cache_size, std::chrono::seconds entry_ttl)
: _sem(sem)
, _expiry_timer([this] { scan_cache_entries(); })
, _entry_ttl(entry_ttl)
, _max_queriers_memory_usage(max_cache_size) {
_expiry_timer.arm_periodic(entry_ttl / 2);
}
class querier_inactive_read : public reader_concurrency_semaphore::inactive_read {
querier_cache::entries& _entries;
querier_cache::entries::iterator _pos;
querier_cache::stats& _stats;
public:
querier_inactive_read(querier_cache::entries& entries, querier_cache::entries::iterator pos, querier_cache::stats& stats)
: _entries(entries)
, _pos(pos)
, _stats(stats) {
}
virtual void evict() override {
_entries.erase(_pos);
++_stats.resource_based_evictions;
--_stats.population;
}
};
template <typename Querier>
static void insert_querier(
reader_concurrency_semaphore& sem,
querier_cache::entries& entries,
querier_cache::index& index,
querier_cache::stats& stats,
size_t max_queriers_memory_usage,
utils::UUID key,
Querier&& q,
lowres_clock::time_point expires,
tracing::trace_state_ptr trace_state) {
// FIXME: see #3159
// In reverse mode flat_mutation_reader drops any remaining rows of the
// current partition when the page ends so it cannot be reused across
// pages.
if (q.is_reversed()) {
return;
}
tracing::trace(trace_state, "Caching querier with key {}", key);
auto memory_usage = boost::accumulate(entries | boost::adaptors::transformed(std::mem_fn(&querier_cache::entry::memory_usage)), size_t(0));
// We add the memory-usage of the to-be added querier to the memory-usage
// of all the cached queriers. We now need to makes sure this number is
// smaller then the maximum allowed memory usage. If it isn't we evict
// cached queriers and substract their memory usage from this number until
// it goes below the limit.
memory_usage += q.memory_usage();
if (memory_usage >= max_queriers_memory_usage) {
while (!entries.empty() && memory_usage >= max_queriers_memory_usage) {
auto it = entries.begin();
memory_usage -= it->memory_usage();
auto ir = sem.unregister_inactive_read(it->get_inactive_handle());
ir->evict();
// querier_inactive_read::evict() updates resource_based_evictions,
// (and population) while we want memory_based_evictions:
--stats.resource_based_evictions;
++stats.memory_based_evictions;
}
}
auto& e = entries.emplace_back(key, std::move(q), expires);
e.set_pos(--entries.end());
if (auto irh = sem.register_inactive_read(std::make_unique<querier_inactive_read>(entries, e.pos(), stats))) {
e.set_inactive_handle(irh);
index.insert(e);
++stats.population;
}
}
void querier_cache::insert(utils::UUID key, data_querier&& q, tracing::trace_state_ptr trace_state) {
insert_querier(_sem, _entries, _data_querier_index, _stats, _max_queriers_memory_usage, key, std::move(q), lowres_clock::now() + _entry_ttl,
std::move(trace_state));
}
void querier_cache::insert(utils::UUID key, mutation_querier&& q, tracing::trace_state_ptr trace_state) {
insert_querier(_sem, _entries, _mutation_querier_index, _stats, _max_queriers_memory_usage, key, std::move(q), lowres_clock::now() + _entry_ttl,
std::move(trace_state));
}
void querier_cache::insert(utils::UUID key, shard_mutation_querier&& q, tracing::trace_state_ptr trace_state) {
insert_querier(_sem, _entries, _shard_mutation_querier_index, _stats, _max_queriers_memory_usage, key, std::move(q), lowres_clock::now() + _entry_ttl,
std::move(trace_state));
}
template <typename Querier>
static std::optional<Querier> lookup_querier(
reader_concurrency_semaphore& sem,
querier_cache::entries& entries,
querier_cache::index& index,
querier_cache::stats& stats,
utils::UUID key,
const schema& s,
dht::partition_ranges_view ranges,
const query::partition_slice& slice,
tracing::trace_state_ptr trace_state) {
auto it = find_querier(entries, index, key, ranges, trace_state);
++stats.lookups;
if (it == entries.end()) {
++stats.misses;
return std::nullopt;
}
auto q = std::move(*it).template value<Querier>();
sem.unregister_inactive_read(it->get_inactive_handle());
entries.erase(it);
--stats.population;
const auto can_be_used = can_be_used_for_page(q, s, ranges.front(), slice);
if (can_be_used == can_use::yes) {
tracing::trace(trace_state, "Reusing querier");
return std::optional<Querier>(std::move(q));
}
tracing::trace(trace_state, "Dropping querier because {}", cannot_use_reason(can_be_used));
++stats.drops;
return std::nullopt;
}
std::optional<data_querier> querier_cache::lookup_data_querier(utils::UUID key,
const schema& s,
const dht::partition_range& range,
const query::partition_slice& slice,
tracing::trace_state_ptr trace_state) {
return lookup_querier<data_querier>(_sem, _entries, _data_querier_index, _stats, key, s, range, slice, std::move(trace_state));
}
std::optional<mutation_querier> querier_cache::lookup_mutation_querier(utils::UUID key,
const schema& s,
const dht::partition_range& range,
const query::partition_slice& slice,
tracing::trace_state_ptr trace_state) {
return lookup_querier<mutation_querier>(_sem, _entries, _mutation_querier_index, _stats, key, s, range, slice, std::move(trace_state));
}
std::optional<shard_mutation_querier> querier_cache::lookup_shard_mutation_querier(utils::UUID key,
const schema& s,
const dht::partition_range_vector& ranges,
const query::partition_slice& slice,
tracing::trace_state_ptr trace_state) {
return lookup_querier<shard_mutation_querier>(_sem, _entries, _shard_mutation_querier_index, _stats, key, s, ranges, slice,
std::move(trace_state));
}
void querier_cache::set_entry_ttl(std::chrono::seconds entry_ttl) {
_entry_ttl = entry_ttl;
_expiry_timer.rearm(lowres_clock::now() + _entry_ttl / 2, _entry_ttl / 2);
}
bool querier_cache::evict_one() {
if (_entries.empty()) {
return false;
}
++_stats.resource_based_evictions;
--_stats.population;
_sem.unregister_inactive_read(_entries.front().get_inactive_handle());
_entries.pop_front();
return true;
}
void querier_cache::evict_all_for_table(const utils::UUID& schema_id) {
auto it = _entries.begin();
const auto end = _entries.end();
while (it != end) {
if (it->schema().id() == schema_id) {
--_stats.population;
_sem.unregister_inactive_read(it->get_inactive_handle());
it = _entries.erase(it);
} else {
++it;
}
}
}
querier_cache_context::querier_cache_context(querier_cache& cache, utils::UUID key, bool is_first_page)
: _cache(&cache)
, _key(key)
, _is_first_page(is_first_page) {
}
void querier_cache_context::insert(data_querier&& q, tracing::trace_state_ptr trace_state) {
if (_cache && _key != utils::UUID{}) {
_cache->insert(_key, std::move(q), std::move(trace_state));
}
}
void querier_cache_context::insert(mutation_querier&& q, tracing::trace_state_ptr trace_state) {
if (_cache && _key != utils::UUID{}) {
_cache->insert(_key, std::move(q), std::move(trace_state));
}
}
void querier_cache_context::insert(shard_mutation_querier&& q, tracing::trace_state_ptr trace_state) {
if (_cache && _key != utils::UUID{}) {
_cache->insert(_key, std::move(q), std::move(trace_state));
}
}
std::optional<data_querier> querier_cache_context::lookup_data_querier(const schema& s,
const dht::partition_range& range,
const query::partition_slice& slice,
tracing::trace_state_ptr trace_state) {
if (_cache && _key != utils::UUID{} && !_is_first_page) {
return _cache->lookup_data_querier(_key, s, range, slice, std::move(trace_state));
}
return std::nullopt;
}
std::optional<mutation_querier> querier_cache_context::lookup_mutation_querier(const schema& s,
const dht::partition_range& range,
const query::partition_slice& slice,
tracing::trace_state_ptr trace_state) {
if (_cache && _key != utils::UUID{} && !_is_first_page) {
return _cache->lookup_mutation_querier(_key, s, range, slice, std::move(trace_state));
}
return std::nullopt;
}
std::optional<shard_mutation_querier> querier_cache_context::lookup_shard_mutation_querier(const schema& s,
const dht::partition_range_vector& ranges,
const query::partition_slice& slice,
tracing::trace_state_ptr trace_state) {
if (_cache && _key != utils::UUID{} && !_is_first_page) {
return _cache->lookup_shard_mutation_querier(_key, s, ranges, slice, std::move(trace_state));
}
return std::nullopt;
}
} // namespace query