In commit c63e88d556, support was added for
fast_forward_to() in data_consume_rows(). Because an input stream's end
cannot be changed after creation, that patch ignores the specified end
byte, and uses the end of file as the end position of the stream.
As result of this, even when we want to read a specific byte range (e.g.,
in the repair code to checksum the partitions in a given range), the code
reads an entire 128K buffer around the end byte, or significantly more, with
read-ahead enabled. This causes repair to do more than 10 times the amount
of I/O it really has to do in the checksumming phase (which in the current
implementation, reads small ranges of partitions at a time).
This patch has two levels:
1. In the lower level, sstable::data_consume_rows(), which reads all
partitions in a given disk byte range, now gets another byte position,
"last_end". That can be the range's end, the end of the file, or anything
in between the two. It opens the disk stream until last_end, which means
1. we will never read-ahead beyond last_end, and 2. fast_fordward_to() is
not allowed beyond last_end.
2. In the upper level, we add to the various layers of sstable readers,
mutation readers, etc., a boolean flag mutation_reader::forwarding, which
says whether fast_forward_to() is allowed on the stream of mutations to
move the stream to a different partition range.
Note that this flag is separate from the existing boolean flag
streamed_mutation::fowarding - that one talks about skipping inside a
single partition, while the flag we are adding is about switching the
partition range being read. Most of the functions that previously
accepted streamed_mutation::forwarding now accept *also* the option
mutation_reader::forwarding. The exception are functions which are known
to read only a single partition, and not support fast_forward_to() a
different partition range.
We note that if mutation_reader::forwarding::no is requested, and
fast_forward_to() is forbidden, there is no point in reading anything
beyond the range's end, so data_consume_rows() is called with last_end as
the range's end. But if forwarding::yes is requested, we use the end of the
file as last_end, exactly like the code before this patch did.
Importantly, we note that the repair's partition reading code,
column_family::make_streaming_reader, uses mutation_reader::forwarding::no,
while the other existing reading code will use the default forwarding::yes.
In the future, we can further optimize the amount of bytes read from disk
by replacing forwarding::yes by an actual last partition that may ever be
read, and use its byte position as the last_end passed to data_consume_rows.
But we don't do this yet, and it's not a regression from the existing code,
which also opened the file input stream until the end of the file, and not
until the end of the range query. Moreover, such an improvement will not
improve of anything if the overall range is always very large, in which
case not over-reading at its end will not improve performance.
Signed-off-by: Nadav Har'El <nyh@scylladb.com>
Message-Id: <20170619152629.11703-1-nyh@scylladb.com>
207 lines
7.2 KiB
C++
207 lines
7.2 KiB
C++
/*
|
|
* Copyright (C) 2015 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 <map>
|
|
#include <memory>
|
|
#include "database_fwd.hh"
|
|
#include "dht/i_partitioner.hh"
|
|
#include "schema.hh"
|
|
#include "mutation_reader.hh"
|
|
#include "db/commitlog/replay_position.hh"
|
|
#include "db/commitlog/rp_set.hh"
|
|
#include "utils/logalloc.hh"
|
|
#include "partition_version.hh"
|
|
|
|
class frozen_mutation;
|
|
|
|
|
|
namespace bi = boost::intrusive;
|
|
|
|
class memtable_entry {
|
|
bi::set_member_hook<> _link;
|
|
schema_ptr _schema;
|
|
dht::decorated_key _key;
|
|
partition_entry _pe;
|
|
public:
|
|
friend class memtable;
|
|
|
|
memtable_entry(schema_ptr s, dht::decorated_key key, mutation_partition p)
|
|
: _schema(std::move(s))
|
|
, _key(std::move(key))
|
|
, _pe(std::move(p))
|
|
{ }
|
|
|
|
memtable_entry(memtable_entry&& o) noexcept;
|
|
|
|
const dht::decorated_key& key() const { return _key; }
|
|
dht::decorated_key& key() { return _key; }
|
|
const partition_entry& partition() const { return _pe; }
|
|
partition_entry& partition() { return _pe; }
|
|
const schema_ptr& schema() const { return _schema; }
|
|
schema_ptr& schema() { return _schema; }
|
|
streamed_mutation read(lw_shared_ptr<memtable> mtbl, const schema_ptr&, const query::partition_slice&, streamed_mutation::forwarding);
|
|
|
|
size_t external_memory_usage_without_rows() const {
|
|
return _key.key().external_memory_usage();
|
|
}
|
|
|
|
struct compare {
|
|
dht::decorated_key::less_comparator _c;
|
|
|
|
compare(schema_ptr s)
|
|
: _c(std::move(s))
|
|
{}
|
|
|
|
bool operator()(const dht::decorated_key& k1, const memtable_entry& k2) const {
|
|
return _c(k1, k2._key);
|
|
}
|
|
|
|
bool operator()(const memtable_entry& k1, const memtable_entry& k2) const {
|
|
return _c(k1._key, k2._key);
|
|
}
|
|
|
|
bool operator()(const memtable_entry& k1, const dht::decorated_key& k2) const {
|
|
return _c(k1._key, k2);
|
|
}
|
|
|
|
bool operator()(const memtable_entry& k1, const dht::ring_position& k2) const {
|
|
return _c(k1._key, k2);
|
|
}
|
|
|
|
bool operator()(const dht::ring_position& k1, const memtable_entry& k2) const {
|
|
return _c(k1, k2._key);
|
|
}
|
|
};
|
|
};
|
|
|
|
class dirty_memory_manager;
|
|
|
|
// Managed by lw_shared_ptr<>.
|
|
class memtable final : public enable_lw_shared_from_this<memtable>, private logalloc::region {
|
|
public:
|
|
using partitions_type = bi::set<memtable_entry,
|
|
bi::member_hook<memtable_entry, bi::set_member_hook<>, &memtable_entry::_link>,
|
|
bi::compare<memtable_entry::compare>>;
|
|
private:
|
|
dirty_memory_manager& _dirty_mgr;
|
|
memtable_list *_memtable_list;
|
|
schema_ptr _schema;
|
|
logalloc::allocating_section _read_section;
|
|
logalloc::allocating_section _allocating_section;
|
|
partitions_type partitions;
|
|
db::replay_position _replay_position;
|
|
db::rp_set _rp_set;
|
|
// mutation source to which reads fall-back after mark_flushed()
|
|
// so that memtable contents can be moved away while there are
|
|
// still active readers. This is needed for this mutation_source
|
|
// to be monotonic (not loose writes). Monotonicity of each
|
|
// mutation_source is necessary for the combined mutation source to be
|
|
// monotonic. That combined source in this case is cache + memtable.
|
|
mutation_source_opt _underlying;
|
|
uint64_t _flushed_memory = 0;
|
|
void update(db::rp_handle&&);
|
|
friend class row_cache;
|
|
friend class memtable_entry;
|
|
friend class flush_reader;
|
|
friend class flush_memory_accounter;
|
|
private:
|
|
boost::iterator_range<partitions_type::const_iterator> slice(const dht::partition_range& r) const;
|
|
partition_entry& find_or_create_partition(const dht::decorated_key& key);
|
|
partition_entry& find_or_create_partition_slow(partition_key_view key);
|
|
void upgrade_entry(memtable_entry&);
|
|
void add_flushed_memory(uint64_t);
|
|
void remove_flushed_memory(uint64_t);
|
|
void clear() noexcept;
|
|
uint64_t dirty_size() const;
|
|
public:
|
|
explicit memtable(schema_ptr schema, dirty_memory_manager&, memtable_list *memtable_list = nullptr);
|
|
// Used for testing that want to control the flush process.
|
|
explicit memtable(schema_ptr schema);
|
|
~memtable();
|
|
// Clears this memtable gradually without consuming the whole CPU.
|
|
// Never resolves with a failed future.
|
|
future<> clear_gently() noexcept;
|
|
schema_ptr schema() const { return _schema; }
|
|
void set_schema(schema_ptr) noexcept;
|
|
future<> apply(memtable&);
|
|
// Applies mutation to this memtable.
|
|
// The mutation is upgraded to current schema.
|
|
void apply(const mutation& m, db::rp_handle&& = {});
|
|
// The mutation is upgraded to current schema.
|
|
void apply(const frozen_mutation& m, const schema_ptr& m_schema, db::rp_handle&& = {});
|
|
|
|
static memtable& from_region(logalloc::region& r) {
|
|
return static_cast<memtable&>(r);
|
|
}
|
|
|
|
const logalloc::region& region() const {
|
|
return *this;
|
|
}
|
|
|
|
logalloc::region_group* region_group() {
|
|
return group();
|
|
}
|
|
public:
|
|
memtable_list* get_memtable_list() {
|
|
return _memtable_list;
|
|
}
|
|
|
|
size_t partition_count() const;
|
|
logalloc::occupancy_stats occupancy() const;
|
|
|
|
// Creates a reader of data in this memtable for given partition range.
|
|
//
|
|
// Live readers share ownership of the memtable instance, so caller
|
|
// doesn't need to ensure that memtable remains live.
|
|
//
|
|
// The 'range' parameter must be live as long as the reader is being used
|
|
//
|
|
// Mutations returned by the reader will all have given schema.
|
|
mutation_reader make_reader(schema_ptr,
|
|
const dht::partition_range& range = query::full_partition_range,
|
|
const query::partition_slice& slice = query::full_slice,
|
|
const io_priority_class& pc = default_priority_class(),
|
|
tracing::trace_state_ptr trace_state_ptr = nullptr,
|
|
streamed_mutation::forwarding fwd = streamed_mutation::forwarding::no,
|
|
mutation_reader::forwarding fwd_mr = mutation_reader::forwarding::no);
|
|
|
|
|
|
mutation_reader make_flush_reader(schema_ptr, const io_priority_class& pc);
|
|
|
|
mutation_source as_data_source();
|
|
|
|
bool empty() const { return partitions.empty(); }
|
|
void mark_flushed(mutation_source);
|
|
bool is_flushed() const;
|
|
void on_detach_from_region_group() noexcept;
|
|
void revert_flushed_memory() noexcept;
|
|
|
|
const db::replay_position& replay_position() const {
|
|
return _replay_position;
|
|
}
|
|
const db::rp_set& rp_set() const {
|
|
return _rp_set;
|
|
}
|
|
friend class iterator_reader;
|
|
};
|