mirror of
https://github.com/scylladb/scylladb.git
synced 2026-04-22 01:20:39 +00:00
Currently, we use std::vector<*mutation> to keep
a list of mutations for processing.
This can lead to large allocation, e.g. when the vector
size is a function of the number of tables.
Use a chunked vector instead to prevent oversized allocations.
`perf-simple-query --smp 1` results obtained for fixed 400MHz frequency
and PGO disabled:
Before (read path):
```
enable-cache=1
Running test with config: {partitions=10000, concurrency=100, mode=read, query_single_key=no, counters=no}
Disabling auto compaction
Creating 10000 partitions...
89055.97 tps ( 66.1 allocs/op, 0.0 logallocs/op, 14.2 tasks/op, 39417 insns/op, 18003 cycles/op, 0 errors)
103372.72 tps ( 66.1 allocs/op, 0.0 logallocs/op, 14.2 tasks/op, 39380 insns/op, 17300 cycles/op, 0 errors)
98942.27 tps ( 66.1 allocs/op, 0.0 logallocs/op, 14.2 tasks/op, 39413 insns/op, 17336 cycles/op, 0 errors)
103752.93 tps ( 66.1 allocs/op, 0.0 logallocs/op, 14.2 tasks/op, 39407 insns/op, 17252 cycles/op, 0 errors)
102516.77 tps ( 66.1 allocs/op, 0.0 logallocs/op, 14.2 tasks/op, 39403 insns/op, 17288 cycles/op, 0 errors)
throughput:
mean= 99528.13 standard-deviation=6155.71
median= 102516.77 median-absolute-deviation=3844.59
maximum=103752.93 minimum=89055.97
instructions_per_op:
mean= 39403.99 standard-deviation=14.25
median= 39406.75 median-absolute-deviation=9.30
maximum=39416.63 minimum=39380.39
cpu_cycles_per_op:
mean= 17435.81 standard-deviation=318.24
median= 17300.40 median-absolute-deviation=147.59
maximum=18002.53 minimum=17251.75
```
After (read path)
```
enable-cache=1
Running test with config: {partitions=10000, concurrency=100, mode=read, query_single_key=no, counters=no}
Disabling auto compaction
Creating 10000 partitions...
59755.04 tps ( 66.2 allocs/op, 0.0 logallocs/op, 14.2 tasks/op, 39466 insns/op, 22834 cycles/op, 0 errors)
71854.16 tps ( 66.1 allocs/op, 0.0 logallocs/op, 14.2 tasks/op, 39417 insns/op, 17883 cycles/op, 0 errors)
82149.45 tps ( 66.1 allocs/op, 0.0 logallocs/op, 14.2 tasks/op, 39411 insns/op, 17409 cycles/op, 0 errors)
49640.04 tps ( 66.1 allocs/op, 0.0 logallocs/op, 14.3 tasks/op, 39474 insns/op, 19975 cycles/op, 0 errors)
54963.22 tps ( 66.1 allocs/op, 0.0 logallocs/op, 14.3 tasks/op, 39474 insns/op, 18235 cycles/op, 0 errors)
throughput:
mean= 63672.38 standard-deviation=13195.12
median= 59755.04 median-absolute-deviation=8709.16
maximum=82149.45 minimum=49640.04
instructions_per_op:
mean= 39448.38 standard-deviation=31.60
median= 39466.17 median-absolute-deviation=25.75
maximum=39474.12 minimum=39411.42
cpu_cycles_per_op:
mean= 19267.01 standard-deviation=2217.03
median= 18234.80 median-absolute-deviation=1384.25
maximum=22834.26 minimum=17408.67
```
`perf-simple-query --smp 1 --write` results obtained for fixed 400MHz frequency
and PGO disabled:
Before (write path):
```
enable-cache=1
Running test with config: {partitions=10000, concurrency=100, mode=write, query_single_key=no, counters=no}
Disabling auto compaction
63736.96 tps ( 59.4 allocs/op, 16.4 logallocs/op, 14.3 tasks/op, 49667 insns/op, 19924 cycles/op, 0 errors)
64109.41 tps ( 59.3 allocs/op, 16.0 logallocs/op, 14.3 tasks/op, 49992 insns/op, 20084 cycles/op, 0 errors)
56950.47 tps ( 59.3 allocs/op, 16.0 logallocs/op, 14.3 tasks/op, 50005 insns/op, 20501 cycles/op, 0 errors)
44858.42 tps ( 59.3 allocs/op, 16.0 logallocs/op, 14.3 tasks/op, 50014 insns/op, 21947 cycles/op, 0 errors)
28592.87 tps ( 59.3 allocs/op, 16.0 logallocs/op, 14.3 tasks/op, 50027 insns/op, 27659 cycles/op, 0 errors)
throughput:
mean= 51649.63 standard-deviation=15059.74
median= 56950.47 median-absolute-deviation=12087.33
maximum=64109.41 minimum=28592.87
instructions_per_op:
mean= 49941.18 standard-deviation=153.76
median= 50005.24 median-absolute-deviation=73.01
maximum=50027.07 minimum=49667.05
cpu_cycles_per_op:
mean= 22023.01 standard-deviation=3249.92
median= 20500.74 median-absolute-deviation=1938.76
maximum=27658.75 minimum=19924.32
```
After (write path)
```
enable-cache=1
Running test with config: {partitions=10000, concurrency=100, mode=write, query_single_key=no, counters=no}
Disabling auto compaction
53395.93 tps ( 59.4 allocs/op, 16.5 logallocs/op, 14.3 tasks/op, 50326 insns/op, 21252 cycles/op, 0 errors)
46527.83 tps ( 59.3 allocs/op, 16.0 logallocs/op, 14.3 tasks/op, 50704 insns/op, 21555 cycles/op, 0 errors)
55846.30 tps ( 59.3 allocs/op, 16.0 logallocs/op, 14.3 tasks/op, 50731 insns/op, 21060 cycles/op, 0 errors)
55669.30 tps ( 59.3 allocs/op, 16.0 logallocs/op, 14.3 tasks/op, 50735 insns/op, 21521 cycles/op, 0 errors)
52130.17 tps ( 59.3 allocs/op, 16.0 logallocs/op, 14.3 tasks/op, 50757 insns/op, 21334 cycles/op, 0 errors)
throughput:
mean= 52713.91 standard-deviation=3795.38
median= 53395.93 median-absolute-deviation=2955.40
maximum=55846.30 minimum=46527.83
instructions_per_op:
mean= 50650.57 standard-deviation=182.46
median= 50731.38 median-absolute-deviation=84.09
maximum=50756.62 minimum=50325.87
cpu_cycles_per_op:
mean= 21344.42 standard-deviation=202.86
median= 21334.00 median-absolute-deviation=176.37
maximum=21554.61 minimum=21060.24
```
Fixes #24815
Improvement for rare corner cases. No backport required
Signed-off-by: Benny Halevy <bhalevy@scylladb.com>
Closes scylladb/scylladb#24919
220 lines
9.5 KiB
C++
220 lines
9.5 KiB
C++
/*
|
|
* Copyright (C) 2019-present ScyllaDB
|
|
*
|
|
* Modified by ScyllaDB
|
|
*/
|
|
|
|
/*
|
|
* SPDX-License-Identifier: (LicenseRef-ScyllaDB-Source-Available-1.0 and Apache-2.0)
|
|
*/
|
|
|
|
#include "mutation/mutation.hh"
|
|
#include "modification_statement.hh"
|
|
#include "cas_request.hh"
|
|
#include <seastar/core/sleep.hh>
|
|
#include "cql3/result_set.hh"
|
|
#include "cql3/expr/evaluate.hh"
|
|
#include "cql3/expr/expr-utils.hh"
|
|
#include "transport/messages/result_message.hh"
|
|
#include "types/map.hh"
|
|
#include "service/storage_proxy.hh"
|
|
#include "cql3/query_processor.hh"
|
|
|
|
namespace cql3::statements {
|
|
|
|
using namespace std::chrono;
|
|
|
|
void cas_request::add_row_update(const modification_statement& stmt_arg,
|
|
std::vector<query::clustering_range> ranges_arg,
|
|
modification_statement::json_cache_opt json_cache_arg,
|
|
const query_options& options_arg) {
|
|
// TODO: reserve updates array for batches
|
|
_updates.emplace_back(cas_row_update{
|
|
.statement = stmt_arg,
|
|
.ranges = std::move(ranges_arg),
|
|
.json_cache = std::move(json_cache_arg),
|
|
.options = options_arg});
|
|
}
|
|
|
|
std::optional<mutation> cas_request::apply_updates(api::timestamp_type ts) const {
|
|
// We're working with a single partition, so there will be only one element
|
|
// in the vector. A vector is used since this is a conventional format
|
|
// to pass a mutation onward.
|
|
std::optional<mutation> mutation_set;
|
|
for (const cas_row_update& op: _updates) {
|
|
update_parameters params(_schema, op.options, ts, op.statement.get_time_to_live(op.options), _rows);
|
|
|
|
auto statement_mutations = op.statement.apply_updates(_key, op.ranges, params, op.json_cache);
|
|
// Append all mutations (in fact only one) to the consolidated one.
|
|
for (mutation& m : statement_mutations) {
|
|
if (mutation_set.has_value() == false) {
|
|
mutation_set.emplace(std::move(m));
|
|
} else {
|
|
mutation_set->apply(std::move(m));
|
|
}
|
|
}
|
|
}
|
|
|
|
return mutation_set;
|
|
}
|
|
|
|
lw_shared_ptr<query::read_command> cas_request::read_command(query_processor& qp) const {
|
|
|
|
column_set columns_to_read(_schema->all_columns_count());
|
|
std::vector<query::clustering_range> ranges;
|
|
|
|
for (const cas_row_update& op : _updates) {
|
|
if (op.statement.has_conditions() == false && op.statement.requires_read() == false) {
|
|
// No point in pre-fetching the old row if the statement doesn't check it in a CAS and
|
|
// doesn't use it to apply updates.
|
|
continue;
|
|
}
|
|
columns_to_read.union_with(op.statement.columns_to_read());
|
|
if (op.statement.has_only_static_column_conditions() && !op.statement.requires_read()) {
|
|
// If a statement has only static column conditions and doesn't have operations that
|
|
// require read, it doesn't matter what clustering key range to query - any partition
|
|
// row will do for the check.
|
|
continue;
|
|
}
|
|
ranges.reserve(op.ranges.size());
|
|
std::copy(op.ranges.begin(), op.ranges.end(), std::back_inserter(ranges));
|
|
}
|
|
uint64_t max_rows = query::partition_max_rows;
|
|
if (ranges.empty()) {
|
|
// With only a static condition, we still want to make the distinction between
|
|
// a non-existing partition and one that exists (has some live data) but has not
|
|
// static content. So we query the first live row of the partition.
|
|
ranges.emplace_back(query::clustering_range::make_open_ended_both_sides());
|
|
max_rows = 1;
|
|
} else {
|
|
// WARNING: clustering_range::deoverlap can return incorrect results - refer to scylladb#22817 and scylladb#21604
|
|
ranges = query::clustering_range::deoverlap(std::move(ranges), clustering_key::tri_compare(*_schema));
|
|
}
|
|
auto options = update_parameters::options;
|
|
options.set(query::partition_slice::option::always_return_static_content);
|
|
query::partition_slice ps(std::move(ranges), *_schema, columns_to_read, options);
|
|
ps.set_partition_row_limit(max_rows);
|
|
return make_lw_shared<query::read_command>(_schema->id(), _schema->version(), std::move(ps), qp.proxy().get_max_result_size(ps),
|
|
query::tombstone_limit(qp.proxy().get_tombstone_limit()));
|
|
}
|
|
|
|
bool cas_request::applies_to() const {
|
|
for (const cas_row_update& op: _updates) {
|
|
if (!op.statement.has_conditions()) {
|
|
continue;
|
|
}
|
|
// No need to check subsequent conditions as we have already failed the current one.
|
|
auto old_row = find_old_row(op).row;
|
|
if (!op.statement.applies_to(_rows.selection.get(), old_row, op.options)) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
std::optional<mutation> cas_request::apply(foreign_ptr<lw_shared_ptr<query::result>> qr,
|
|
const query::partition_slice& slice, api::timestamp_type ts) {
|
|
_rows = update_parameters::build_prefetch_data(_schema, *qr, slice);
|
|
if (applies_to()) {
|
|
return apply_updates(ts);
|
|
} else {
|
|
return {};
|
|
}
|
|
}
|
|
|
|
cas_request::old_row cas_request::find_old_row(const cas_row_update& op) const {
|
|
static const clustering_key empty_ckey = clustering_key::make_empty();
|
|
if (_key.empty()) {
|
|
throw exceptions::invalid_request_exception("Empty partition key range");
|
|
}
|
|
const partition_key& pkey = _key.front().start()->value().key().value();
|
|
// We must ignore statement clustering column restriction when
|
|
// choosing a row to check the conditions. If there is no
|
|
// exact match, choose static row to check if the statement
|
|
// applies.
|
|
// For example, the following update must successfully apply (effectively
|
|
// turn into INSERT), because, although the table doesn't have any regular rows matching the
|
|
// statement clustering column restriction, the static row matches the statement condition:
|
|
// CREATE TABLE t(p int, c int, s int static, v int, PRIMARY KEY(p, c));
|
|
// INSERT INTO t(p, s) VALUES(1, 1);
|
|
// UPDATE t SET v=1 WHERE p=1 AND c=1 IF s=1;
|
|
if (op.ranges.empty()) {
|
|
throw exceptions::invalid_request_exception("Empty clustering range");
|
|
}
|
|
const clustering_key& ckey = op.ranges.front().start() ? op.ranges.front().start()->value() : empty_ckey;
|
|
auto row = _rows.find_row(pkey, ckey);
|
|
auto ckey_ptr = &ckey;
|
|
if (row == nullptr && !ckey.is_empty() &&
|
|
!op.statement.has_if_exist_condition() && !op.statement.has_if_not_exist_condition()) {
|
|
row = _rows.find_row(pkey, empty_ckey);
|
|
ckey_ptr = &empty_ckey;
|
|
}
|
|
if (!row) {
|
|
ckey_ptr = &empty_ckey;
|
|
}
|
|
return old_row{ckey_ptr, row};
|
|
}
|
|
|
|
seastar::shared_ptr<cql_transport::messages::result_message>
|
|
cas_request::build_cas_result_set(seastar::shared_ptr<cql3::metadata> metadata,
|
|
const column_set& columns,
|
|
bool is_applied) const {
|
|
const partition_key& pkey = _key.front().start()->value().key().value();
|
|
const clustering_key empty_ckey = clustering_key::make_empty();
|
|
auto result_set = std::make_unique<cql3::result_set>(metadata);
|
|
|
|
auto pkey_bytes = pkey.explode();
|
|
|
|
for (const cas_row_update& op: _updates) {
|
|
// Construct the result set row
|
|
std::vector<bytes_opt> rs_row;
|
|
rs_row.reserve(metadata->value_count());
|
|
rs_row.emplace_back(boolean_type->decompose(is_applied));
|
|
// Get old row from prefetched data for the row update
|
|
auto old_row = find_old_row(op);
|
|
if (!old_row.row) {
|
|
// In case there is no old row, leave all other columns null
|
|
// so that we can infer whether the update attempts to insert a
|
|
// non-existing row.
|
|
rs_row.resize(metadata->value_count());
|
|
result_set->add_row(std::move(rs_row));
|
|
continue;
|
|
}
|
|
auto ckey_bytes = old_row.ckey->explode();
|
|
|
|
auto eval_inputs = expr::evaluation_inputs{
|
|
.partition_key = pkey_bytes,
|
|
.clustering_key = ckey_bytes,
|
|
.static_and_regular_columns = old_row.row->cells,
|
|
.selection = _rows.selection.get(),
|
|
};
|
|
|
|
// Fill in the cells from prefetch data (old row) into the result set row
|
|
for (ordinal_column_id id = columns.find_first(); id != column_set::npos; id = columns.find_next(id)) {
|
|
auto& cdef = _schema->column_at(id);
|
|
auto val = expr::extract_column_value(&cdef, eval_inputs);
|
|
if (!val) {
|
|
rs_row.emplace_back(bytes_opt{});
|
|
continue;
|
|
}
|
|
const abstract_type& column_type = *cdef.type;
|
|
|
|
if (column_type.is_listlike() && column_type.is_multi_cell()) {
|
|
// List/sets are fetched as maps, but need to be stored as sets.
|
|
const listlike_collection_type_impl& list_type = static_cast<const listlike_collection_type_impl&>(column_type);
|
|
auto map_type_holder = map_type_impl::get_instance(list_type.name_comparator(), list_type.value_comparator(), list_type.is_multi_cell());
|
|
const map_type_impl& map_type = static_cast<const map_type_impl&>(*map_type_holder);
|
|
auto cell = map_type.deserialize(managed_bytes_view(*val));
|
|
rs_row.emplace_back(list_type.serialize_map(map_type, cell));
|
|
} else {
|
|
rs_row.emplace_back(to_bytes(*val));
|
|
}
|
|
}
|
|
result_set->add_row(std::move(rs_row));
|
|
}
|
|
cql3::result result(std::move(result_set));
|
|
return seastar::make_shared<cql_transport::messages::result_message::rows>(std::move(result));
|
|
}
|
|
|
|
} // end of namespace "cql3::statements"
|