Files
scylladb/cql3/statements/modification_statement.hh
Benny Halevy 3feb759943 everywhere: use utils::chunked_vector for list of mutations
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
2025-07-13 19:13:11 +03:00

277 lines
11 KiB
C++

/*
* Copyright (C) 2015-present ScyllaDB
*
* Modified by ScyllaDB
*/
/*
* SPDX-License-Identifier: (LicenseRef-ScyllaDB-Source-Available-1.0 and Apache-2.0)
*/
#pragma once
#include "cql3/stats.hh"
#include "cql3/update_parameters.hh"
#include "cql3/cql_statement.hh"
#include "cql3/restrictions/statement_restrictions.hh"
#include "cql3/statements/statement_type.hh"
#include "exceptions/coordinator_result.hh"
#include <seastar/core/shared_ptr.hh>
#include <memory>
#include <optional>
namespace cql3 {
class query_processor;
class attributes;
class operation;
namespace statements {
class strongly_consistent_modification_statement;
namespace raw { class modification_statement; }
/*
* Abstract parent class of individual modifications, i.e. INSERT, UPDATE and DELETE.
*/
class modification_statement : public cql_statement_opt_metadata {
public:
const statement_type type;
bool _may_use_token_aware_routing;
private:
const uint32_t _bound_terms;
// If we have operation on list entries, such as adding or
// removing an entry, the modification statement must prefetch
// the old values of the list to create an idempotent mutation.
// If the statement has conditions, conditional columns must
// also be prefetched, to evaluate conditions. If the
// statement has IF EXISTS/IF NOT EXISTS, we prefetch all
// columns, to match Cassandra behaviour.
// This bitset contains a mask of ordinal_id identifiers
// of the required columns.
column_set _columns_to_read;
// A CAS statement returns a result set with the columns
// used in condition expression. This is a mask of ordinal_id
// identifiers of the required columns. Contains all columns
// of a schema if we have IF EXISTS/IF NOT EXISTS. Does *not*
// contain LIST columns prefetched to apply updates, unless
// these columns are also used in conditions.
column_set _columns_of_cas_result_set;
public:
const schema_ptr s;
const std::unique_ptr<attributes> attrs;
protected:
std::vector<::shared_ptr<operation>> _column_operations;
cql_stats& _stats;
expr::expression _condition = expr::conjunction{{}}; // TRUE
private:
const ks_selector _ks_sel;
// True if this statement has _if_exists or _if_not_exists or other
// conditions that apply to static/regular columns, respectively.
// Pre-computed during statement prepare.
bool _has_static_column_conditions = false;
bool _has_regular_column_conditions = false;
// True if any of update operations requires a prefetch.
// Pre-computed during statement prepare.
bool _requires_read = false;
bool _if_not_exists = false;
bool _if_exists = false;
// True if this statement has column operations that apply to static/regular
// columns, respectively.
bool _sets_static_columns = false;
bool _sets_regular_columns = false;
// True if this statement has column operations or conditions for a column
// that stores a collection.
bool _selects_a_collection = false;
std::optional<bool> _is_raw_counter_shard_write;
protected:
std::optional<restrictions::statement_restrictions> _restrictions;
public:
typedef std::optional<std::unordered_map<sstring, bytes_opt>> json_cache_opt;
modification_statement(
statement_type type_,
uint32_t bound_terms,
schema_ptr schema_,
std::unique_ptr<attributes> attrs_,
cql_stats& stats_);
virtual ~modification_statement() override;
virtual bool require_full_clustering_key() const = 0;
virtual bool allow_clustering_key_slices() const = 0;
virtual void add_update_for_key(mutation& m, const query::clustering_range& range, const update_parameters& params, const json_cache_opt& json_cache) const = 0;
virtual uint32_t get_bound_terms() const override;
virtual const sstring& keyspace() const;
virtual const sstring& column_family() const;
virtual bool is_counter() const;
virtual bool is_view() const;
int64_t get_timestamp(int64_t now, const query_options& options) const;
bool is_timestamp_set() const;
std::optional<gc_clock::duration> get_time_to_live(const query_options& options) const;
virtual future<> check_access(query_processor& qp, const service::client_state& state) const override;
// Validate before execute, using client state and current schema
void validate(query_processor&, const service::client_state& state) const override;
virtual bool depends_on(std::string_view ks_name, std::optional<std::string_view> cf_name) const override;
void add_operation(::shared_ptr<operation> op);
void inc_cql_stats(bool is_internal) const;
const restrictions::statement_restrictions& restrictions() const {
return *_restrictions;
}
bool is_conditional() const override;
public:
void analyze_condition(expr::expression cond);
void set_if_not_exist_condition();
bool has_if_not_exist_condition() const;
void set_if_exist_condition();
bool has_if_exist_condition() const;
bool is_raw_counter_shard_write() const {
return _is_raw_counter_shard_write.value_or(false);
}
void process_where_clause(data_dictionary::database db, expr::expression where_clause, prepare_context& ctx);
// CAS statement returns a result set. Prepare result set metadata
// so that get_result_metadata() returns a meaningful value.
void build_cas_result_set_metadata();
public:
virtual dht::partition_range_vector build_partition_keys(const query_options& options, const json_cache_opt& json_cache) const;
virtual query::clustering_row_ranges create_clustering_ranges(const query_options& options, const json_cache_opt& json_cache) const;
private:
// Return true if this statement doesn't update or read any regular rows, only static rows.
// Note, it isn't enough to just check !_sets_regular_columns && _regular_conditions.empty(),
// because a DELETE statement that deletes whole rows (DELETE FROM ...) technically doesn't
// have any column operations and hence doesn't have _sets_regular_columns set. It doesn't
// have _sets_static_columns set either so checking the latter flag too here guarantees that
// this function works as expected in all cases.
bool applies_only_to_static_columns() const {
return _sets_static_columns && !_sets_regular_columns && !_has_regular_column_conditions;
}
public:
// True if any of update operations of this statement requires
// a prefetch of the old cell.
bool requires_read() const { return _requires_read; }
// Columns used in this statement conditions or operations.
const column_set& columns_to_read() const { return _columns_to_read; }
// Columns of the statement result set (only CAS statement
// returns a result set).
const column_set& columns_of_cas_result_set() const { return _columns_of_cas_result_set; }
// Build a read_command instance to fetch the previous mutation from storage. The mutation is
// fetched if we need to check LWT conditions or apply updates to non-frozen list elements.
lw_shared_ptr<query::read_command> read_command(query_processor& qp, query::clustering_row_ranges ranges, db::consistency_level cl) const;
// Create a mutation object for the update operation represented by this modification statement.
// A single mutation object for lightweight transactions, which can only span one partition, or a vector
// of mutations, one per partition key, for statements which affect multiple partition keys,
// e.g. DELETE FROM table WHERE pk IN (1, 2, 3).
utils::chunked_vector<mutation> apply_updates(
const std::vector<dht::partition_range>& keys,
const std::vector<query::clustering_range>& ranges,
const update_parameters& params,
const json_cache_opt& json_cache) const;
/**
* Checks whether the conditions represented by this statement apply provided the current state of the row on
* which those conditions are.
*
* @param row the row with current data corresponding to these conditions. Can be null if there
* is no matching row.
* @return whether the conditions represented by this statement apply or not.
*/
bool applies_to(const selection::selection* selection, const update_parameters::prefetch_data::row* row, const query_options& options) const;
private:
future<::shared_ptr<cql_transport::messages::result_message>>
do_execute(query_processor& qp, service::query_state& qs, const query_options& options) const;
friend class modification_statement_executor;
public:
// True if the statement has IF conditions. Pre-computed during prepare.
bool has_conditions() const { return _has_regular_column_conditions || _has_static_column_conditions; }
// True if the statement has IF conditions that apply to static columns.
bool has_static_column_conditions() const { return _has_static_column_conditions; }
// True if this statement needs to read only static column values to check if it can be applied.
bool has_only_static_column_conditions() const { return !_has_regular_column_conditions && _has_static_column_conditions; }
virtual future<::shared_ptr<cql_transport::messages::result_message>>
execute(query_processor& qp, service::query_state& qs, 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& qs, const query_options& options, std::optional<service::group0_guard> guard) const override;
private:
future<exceptions::coordinator_result<>>
execute_without_condition(query_processor& qp, service::query_state& qs, const query_options& options, json_cache_opt& json_cache, std::vector<dht::partition_range> keys) const;
future<::shared_ptr<cql_transport::messages::result_message>>
execute_with_condition(query_processor& qp, service::query_state& qs, const query_options& options) const;
public:
/**
* Convert statement into a list of mutations to apply on the server
*
* @param options value for prepared statement markers
* @param local if true, any requests (for collections) performed by getMutation should be done locally only.
* @param now the current timestamp in microseconds to use if no timestamp is user provided.
*
* @return vector of the mutations
* @throws invalid_request_exception on invalid requests
*/
future<utils::chunked_vector<mutation>> get_mutations(query_processor& qp, const query_options& options, db::timeout_clock::time_point timeout, bool local, int64_t now, service::query_state& qs, json_cache_opt& json_cache, std::vector<dht::partition_range> keys) const;
virtual json_cache_opt maybe_prepare_json_cache(const query_options& options) const;
virtual ::shared_ptr<strongly_consistent_modification_statement> prepare_for_broadcast_tables() const;
protected:
/**
* If there are conditions on the statement, this is called after the where clause and conditions have been
* processed to check that they are compatible.
* @throws InvalidRequestException
*/
virtual void validate_where_clause_for_conditions() const;
db::timeout_clock::duration get_timeout(const service::client_state& state, const query_options& options) const;
friend class raw::modification_statement;
};
}
}