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
158 lines
5.8 KiB
C++
158 lines
5.8 KiB
C++
/*
|
|
* Modified by ScyllaDB
|
|
* Copyright (C) 2015-present ScyllaDB
|
|
*/
|
|
|
|
/*
|
|
* SPDX-License-Identifier: (LicenseRef-ScyllaDB-Source-Available-1.0 and Apache-2.0)
|
|
*/
|
|
#pragma once
|
|
|
|
#include "cql3/cql_statement.hh"
|
|
#include "raw/batch_statement.hh"
|
|
#include "timestamp.hh"
|
|
#include "utils/log.hh"
|
|
#include "service_permit.hh"
|
|
#include "exceptions/coordinator_result.hh"
|
|
#include "tracing/trace_state.hh"
|
|
|
|
namespace cql_transport::messages {
|
|
class result_message;
|
|
}
|
|
|
|
namespace cql3 {
|
|
|
|
class query_processor;
|
|
|
|
namespace statements {
|
|
|
|
class modification_statement;
|
|
|
|
/**
|
|
* A <code>BATCH</code> statement parsed from a CQL query.
|
|
*
|
|
*/
|
|
class batch_statement : public cql_statement_opt_metadata {
|
|
static logging::logger _logger;
|
|
public:
|
|
using type = raw::batch_statement::type;
|
|
|
|
struct single_statement {
|
|
shared_ptr<modification_statement> statement;
|
|
bool needs_authorization = true;
|
|
|
|
public:
|
|
single_statement(shared_ptr<modification_statement> s)
|
|
: statement(std::move(s))
|
|
{}
|
|
single_statement(shared_ptr<modification_statement> s, bool na)
|
|
: statement(std::move(s))
|
|
, needs_authorization(na)
|
|
{}
|
|
};
|
|
private:
|
|
int _bound_terms;
|
|
type _type;
|
|
std::vector<single_statement> _statements;
|
|
std::unique_ptr<attributes> _attrs;
|
|
// True if *any* statement of the batch has IF .. clause. In
|
|
// this case entire batch is considered a CAS batch.
|
|
bool _has_conditions;
|
|
// If the BATCH has conditions, it must return columns which
|
|
// are involved in condition expressions in its result set.
|
|
// Unlike Cassandra, Scylla always returns all columns,
|
|
// regardless of whether the batch succeeds or not - this
|
|
// allows clients to prepare a CAS statement like any other
|
|
// statement, and trust the returned statement metadata.
|
|
// Cassandra returns a result set only if CAS succeeds. If
|
|
// any statement in the batch has IF EXISTS, we must return
|
|
// all columns of the table, including the primary key.
|
|
column_set _columns_of_cas_result_set;
|
|
cql_stats& _stats;
|
|
public:
|
|
/**
|
|
* Creates a new BatchStatement from a list of statements
|
|
*
|
|
* @param type type of the batch
|
|
* @param statements a list of UpdateStatements
|
|
* @param attrs additional attributes for statement (CL, timestamp, timeToLive)
|
|
*/
|
|
batch_statement(int bound_terms, type type_,
|
|
std::vector<single_statement> statements,
|
|
std::unique_ptr<attributes> attrs,
|
|
cql_stats& stats);
|
|
|
|
batch_statement(type type_,
|
|
std::vector<single_statement> statements,
|
|
std::unique_ptr<attributes> attrs,
|
|
cql_stats& stats);
|
|
|
|
const std::vector<single_statement>& statements() const { return _statements; }
|
|
|
|
virtual bool depends_on(std::string_view ks_name, std::optional<std::string_view> cf_name) const override;
|
|
|
|
virtual uint32_t get_bound_terms() const override;
|
|
|
|
virtual future<> check_access(query_processor& qp, const service::client_state& state) const override;
|
|
|
|
// Validates a prepared batch statement without validating its nested statements.
|
|
void validate();
|
|
|
|
bool has_conditions() const { return _has_conditions; }
|
|
|
|
void build_cas_result_set_metadata();
|
|
|
|
// The batch itself will be validated in either Parsed#prepare() - for regular CQL3 batches,
|
|
// or in QueryProcessor.processBatch() - for native protocol batches.
|
|
virtual void validate(query_processor& qp, const service::client_state& state) const override;
|
|
|
|
const std::vector<single_statement>& get_statements();
|
|
private:
|
|
future<utils::chunked_vector<mutation>> get_mutations(query_processor& qp, const query_options& options, db::timeout_clock::time_point timeout,
|
|
bool local, api::timestamp_type now, service::query_state& query_state) const;
|
|
|
|
public:
|
|
/**
|
|
* Checks batch size to ensure threshold is met. If not, a warning is logged.
|
|
* @param cfs ColumnFamilies that will store the batch's mutations.
|
|
*/
|
|
static void verify_batch_size(query_processor& qp, const utils::chunked_vector<mutation>& mutations);
|
|
|
|
virtual future<shared_ptr<cql_transport::messages::result_message>> execute(
|
|
query_processor& qp, service::query_state& state, const query_options& options, std::optional<service::group0_guard> guard) const override;
|
|
|
|
virtual future<shared_ptr<cql_transport::messages::result_message>> execute_without_checking_exception_message(
|
|
query_processor& qp, service::query_state& state, const query_options& options, std::optional<service::group0_guard> guard) const override;
|
|
private:
|
|
friend class batch_statement_executor;
|
|
future<shared_ptr<cql_transport::messages::result_message>> do_execute(
|
|
query_processor& qp,
|
|
service::query_state& query_state, const query_options& options,
|
|
bool local, api::timestamp_type now) const;
|
|
|
|
future<exceptions::coordinator_result<>> execute_without_conditions(
|
|
query_processor& qp,
|
|
utils::chunked_vector<mutation> mutations,
|
|
db::consistency_level cl,
|
|
db::timeout_clock::time_point timeout,
|
|
tracing::trace_state_ptr tr_state,
|
|
service_permit permit) const;
|
|
|
|
future<shared_ptr<cql_transport::messages::result_message>> execute_with_conditions(
|
|
query_processor& qp,
|
|
const query_options& options,
|
|
service::query_state& state) const;
|
|
|
|
db::timeout_clock::duration get_timeout(const service::client_state& state, const query_options& options) const;
|
|
public:
|
|
// FIXME: no cql_statement::to_string() yet
|
|
#if 0
|
|
sstring to_string() const {
|
|
return format("BatchStatement(type={}, statements={})", _type, join(", ", _statements));
|
|
}
|
|
#endif
|
|
};
|
|
|
|
}
|
|
}
|