Files
scylladb/tests/memtable_snapshot_source.hh
Botond Dénes eb357a385d flat_mutation_reader: make timeout opt-out rather than opt-in
Currently timeout is opt-in, that is, all methods that even have it
default it to `db::no_timeout`. This means that ensuring timeout is used
where it should be is completely up to the author and the reviewrs of
the code. As humans are notoriously prone to mistakes this has resulted
in a very inconsistent usage of timeout, many clients of
`flat_mutation_reader` passing the timeout only to some members and only
on certain call sites. This is small wonder considering that some core
operations like `operator()()` only recently received a timeout
parameter and others like `peek()` didn't even have one until this
patch. Both of these methods call `fill_buffer()` which potentially
talks to the lower layers and is supposed to propagate the timeout.
All this makes the `flat_mutation_reader`'s timeout effectively useless.

To make order in this chaos make the timeout parameter a mandatory one
on all `flat_mutation_reader` methods that need it. This ensures that
humans now get a reminder from the compiler when they forget to pass the
timeout. Clients can still opt-out from passing a timeout by passing
`db::no_timeout` (the previous default value) but this will be now
explicit and developers should think before typing it.

There were suprisingly few core call sites to fix up. Where a timeout
was available nearby I propagated it to be able to pass it to the
reader, where I couldn't I passed `db::no_timeout`. Authors of the
latter kind of code (view, streaming and repair are some of the notable
examples) should maybe consider propagating down a timeout if needed.
In the test code (the wast majority of the changes) I just used
`db::no_timeout` everywhere.

Tests: unit(release, debug)

Signed-off-by: Botond Dénes <bdenes@scylladb.com>

Message-Id: <1edc10802d5eb23de8af28c9f48b8d3be0f1a468.1536744563.git.bdenes@scylladb.com>
2018-09-20 11:31:24 +02:00

136 lines
4.4 KiB
C++

/*
* Copyright (C) 2017 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/>.
*/
#pragma once
#include "mutation_reader.hh"
#include "memtable.hh"
#include "utils/phased_barrier.hh"
#include <seastar/core/circular_buffer.hh>
#include <seastar/core/thread.hh>
#include <seastar/core/condition-variable.hh>
// in-memory snapshottable mutation source.
// Must be destroyed in a seastar thread.
class memtable_snapshot_source {
schema_ptr _s;
circular_buffer<lw_shared_ptr<memtable>> _memtables;
utils::phased_barrier _apply;
bool _closed = false;
seastar::condition_variable _should_compact;
future<> _compactor;
private:
bool should_compact() const {
return !_closed && _memtables.size() >= 3;
}
lw_shared_ptr<memtable> new_memtable() {
return make_lw_shared<memtable>(_s);
}
lw_shared_ptr<memtable> pending() {
if (_memtables.empty()) {
_memtables.push_back(new_memtable());
on_new_memtable();
}
return _memtables.back();
}
void on_new_memtable() {
if (should_compact()) {
_should_compact.signal();
}
}
void compact() {
if (_memtables.empty()) {
return;
}
auto count = _memtables.size();
auto op = _apply.start();
auto new_mt = make_lw_shared<memtable>(_memtables.back()->schema());
std::vector<flat_mutation_reader> readers;
for (auto&& mt : _memtables) {
readers.push_back(mt->make_flat_reader(new_mt->schema(),
query::full_partition_range,
new_mt->schema()->full_slice(),
default_priority_class(),
nullptr,
streamed_mutation::forwarding::no,
mutation_reader::forwarding::yes));
}
_memtables.push_back(new_memtable());
auto&& rd = make_combined_reader(new_mt->schema(), std::move(readers));
consume_partitions(rd, [&] (mutation&& m) {
new_mt->apply(std::move(m));
return stop_iteration::no;
}, db::no_timeout).get();
_memtables.erase(_memtables.begin(), _memtables.begin() + count);
_memtables.push_back(new_mt);
}
public:
memtable_snapshot_source(schema_ptr s)
: _s(s)
, _compactor(seastar::async([this] () noexcept {
while (!_closed) {
_should_compact.wait().get();
while (should_compact()) {
memory::disable_failure_guard dfg;
compact();
}
}
}))
{ }
memtable_snapshot_source(memtable_snapshot_source&&) = delete; // 'this' captured.
~memtable_snapshot_source() {
_closed = true;
_should_compact.broadcast();
_compactor.get();
}
// Must run in a seastar thread
void clear() {
_memtables.erase(_memtables.begin(), _memtables.end());
_apply.advance_and_await().get();
_memtables.erase(_memtables.begin(), _memtables.end());
}
void apply(const mutation& mt) {
pending()->apply(mt);
}
// Must run in a seastar thread
void apply(memtable& mt) {
auto op = _apply.start();
auto new_mt = new_memtable();
new_mt->apply(mt).get();
_memtables.push_back(new_mt);
}
// mt must not change from now on.
void apply(lw_shared_ptr<memtable> mt) {
auto op = _apply.start();
_memtables.push_back(std::move(mt));
on_new_memtable();
}
mutation_source operator()() {
std::vector<mutation_source> src;
for (auto&& mt : _memtables) {
src.push_back(mt->as_data_source());
}
_memtables.push_back(new_memtable()); // so that src won't change any more.
on_new_memtable();
return make_combined_mutation_source(std::move(src));
}
};