Files
scylladb/test/lib/data_model.cc
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

361 lines
16 KiB
C++

/*
* Copyright (C) 2019-present ScyllaDB
*/
/*
* SPDX-License-Identifier: LicenseRef-ScyllaDB-Source-Available-1.0
*/
#include <algorithm>
#include "utils/assert.hh"
#include "test/lib/data_model.hh"
#include "schema/schema_builder.hh"
#include "concrete_types.hh"
namespace tests::data_model {
mutation_description::atomic_value::atomic_value(bytes value, api::timestamp_type timestamp) : value(std::move(value)), timestamp(timestamp)
{ }
mutation_description::atomic_value::atomic_value(bytes value, api::timestamp_type timestamp, gc_clock::duration ttl, gc_clock::time_point expiry_point)
: value(std::move(value)), timestamp(timestamp), expiring(expiry_info{ttl, expiry_point})
{ }
mutation_description::collection::collection(std::initializer_list<collection_element> elements) : elements(elements)
{ }
mutation_description::collection::collection(std::vector<collection_element> elements) : elements(std::move(elements))
{ }
mutation_description::row_marker::row_marker(api::timestamp_type timestamp) : timestamp(timestamp)
{ }
mutation_description::row_marker::row_marker(api::timestamp_type timestamp, gc_clock::duration ttl, gc_clock::time_point expiry_point)
: timestamp(timestamp), expiring(expiry_info{ttl, expiry_point})
{ }
void mutation_description::remove_column(row& r, const sstring& name) {
auto it = std::ranges::find_if(r, [&] (const cell& c) {
return c.column_name == name;
});
if (it != r.end()) {
r.erase(it);
}
}
mutation_description::mutation_description(key partition_key)
: _partition_key(std::move(partition_key))
{ }
void mutation_description::set_partition_tombstone(tombstone partition_tombstone) {
_partition_tombstone = partition_tombstone;
}
void mutation_description::add_static_cell(const sstring& column, value v) {
_static_row.emplace_back(cell { column, std::move(v) });
}
void mutation_description::add_clustered_cell(const key& ck, const sstring& column, value v) {
_clustered_rows[ck].cells.emplace_back(cell { column, std::move(v) });
}
void mutation_description::add_clustered_row_marker(const key& ck, row_marker marker) {
_clustered_rows[ck].marker = marker;
}
void mutation_description::add_clustered_row_tombstone(const key& ck, row_tombstone tomb) {
_clustered_rows[ck].tomb = tomb;
}
void mutation_description::remove_static_column(const sstring& name) {
remove_column(_static_row, name);
}
void mutation_description::remove_regular_column(const sstring& name) {
for (auto& [ ckey, cr ] : _clustered_rows) {
(void)ckey;
remove_column(cr.cells, name);
}
}
void mutation_description::add_range_tombstone(const key& start, const key& end, tombstone tomb) {
add_range_tombstone(interval<key>::make(start, end), tomb);
}
void mutation_description::add_range_tombstone(interval<key> range, tombstone tomb) {
_range_tombstones.emplace_back(range_tombstone { std::move(range), tomb });
}
mutation mutation_description::build(schema_ptr s) const {
auto m = mutation(s, partition_key::from_exploded(*s, _partition_key));
m.partition().apply(_partition_tombstone);
for (auto& [ column, value_or_collection ] : _static_row) {
auto cdef = s->get_column_definition(utf8_type->decompose(column));
SCYLLA_ASSERT(cdef);
std::visit(make_visitor(
[&] (const atomic_value& v) {
SCYLLA_ASSERT(cdef->is_atomic());
if (!v.expiring) {
m.set_static_cell(*cdef, atomic_cell::make_live(*cdef->type, v.timestamp, v.value));
} else {
m.set_static_cell(*cdef, atomic_cell::make_live(*cdef->type, v.timestamp, v.value,
v.expiring->expiry_point, v.expiring->ttl));
}
},
[&] (const collection& c) {
SCYLLA_ASSERT(!cdef->is_atomic());
auto get_value_type = visit(*cdef->type, make_visitor(
[] (const collection_type_impl& ctype) -> std::function<const abstract_type&(bytes_view)> {
return [&] (bytes_view) -> const abstract_type& { return *ctype.value_comparator(); };
},
[] (const user_type_impl& utype) -> std::function<const abstract_type&(bytes_view)> {
return [&] (bytes_view key) -> const abstract_type& { return *utype.type(deserialize_field_index(key)); };
},
[] (const abstract_type& o) -> std::function<const abstract_type&(bytes_view)> {
SCYLLA_ASSERT(false);
}
));
collection_mutation_description mut;
mut.tomb = c.tomb;
for (auto& [ key, value ] : c.elements) {
if (!value.expiring) {
mut.cells.emplace_back(key, atomic_cell::make_live(get_value_type(key), value.timestamp,
value.value, atomic_cell::collection_member::yes));
} else {
mut.cells.emplace_back(key, atomic_cell::make_live(get_value_type(key),
value.timestamp,
value.value,
value.expiring->expiry_point,
value.expiring->ttl,
atomic_cell::collection_member::yes));
}
}
m.set_static_cell(*cdef, mut.serialize(*cdef->type));
}
), value_or_collection);
}
for (auto& [ ckey, cr ] : _clustered_rows) {
auto& [ marker, tomb, cells ] = cr;
auto ck = clustering_key::from_exploded(*s, ckey);
for (auto& [ column, value_or_collection ] : cells) {
auto cdef = s->get_column_definition(utf8_type->decompose(column));
SCYLLA_ASSERT(cdef);
std::visit(make_visitor(
[&] (const atomic_value& v) {
SCYLLA_ASSERT(cdef->is_atomic());
if (!v.expiring) {
m.set_clustered_cell(ck, *cdef, atomic_cell::make_live(*cdef->type, v.timestamp, v.value));
} else {
m.set_clustered_cell(ck, *cdef, atomic_cell::make_live(*cdef->type, v.timestamp, v.value,
v.expiring->expiry_point, v.expiring->ttl));
}
},
[&] (const collection& c) {
SCYLLA_ASSERT(!cdef->is_atomic());
auto get_value_type = visit(*cdef->type, make_visitor(
[] (const collection_type_impl& ctype) -> std::function<const abstract_type&(bytes_view)> {
return [&] (bytes_view) -> const abstract_type& { return *ctype.value_comparator(); };
},
[] (const user_type_impl& utype) -> std::function<const abstract_type&(bytes_view)> {
return [&] (bytes_view key) -> const abstract_type& { return *utype.type(deserialize_field_index(key)); };
},
[] (const abstract_type& o) -> std::function<const abstract_type&(bytes_view)> {
SCYLLA_ASSERT(false);
}
));
collection_mutation_description mut;
mut.tomb = c.tomb;
for (auto& [ key, value ] : c.elements) {
if (!value.expiring) {
mut.cells.emplace_back(key, atomic_cell::make_live(get_value_type(key), value.timestamp,
value.value, atomic_cell::collection_member::yes));
} else {
mut.cells.emplace_back(key, atomic_cell::make_live(get_value_type(key),
value.timestamp,
value.value,
value.expiring->expiry_point,
value.expiring->ttl,
atomic_cell::collection_member::yes));
}
}
m.set_clustered_cell(ck, *cdef, mut.serialize(*cdef->type));
}
), value_or_collection);
}
if (marker.timestamp != api::missing_timestamp) {
if (marker.expiring) {
m.partition().clustered_row(*s, ckey).apply(::row_marker(marker.timestamp, marker.expiring->ttl, marker.expiring->expiry_point));
} else {
m.partition().clustered_row(*s, ckey).apply(::row_marker(marker.timestamp));
}
}
if (tomb) {
m.partition().clustered_row(*s, ckey).apply(tomb);
}
}
clustering_key::less_compare cmp(*s);
for (auto& [ range, tomb ] : _range_tombstones) {
auto clustering_range = range.transform([&s = *s] (const key& k) {
return clustering_key::from_exploded(s, k);
});
if (!clustering_range.is_singular()) {
auto start = clustering_range.start();
auto end = clustering_range.end();
if (start && end && cmp(end->value(), start->value())) {
clustering_range = interval<clustering_key>(std::move(end), std::move(start));
}
}
auto rt = ::range_tombstone(
bound_view::from_range_start(clustering_range),
bound_view::from_range_end(clustering_range),
tomb);
m.partition().apply_delete(*s, std::move(rt));
}
return m;
}
std::vector<table_description::column>::iterator table_description::find_column(std::vector<column>& columns, const sstring& name) {
return std::ranges::find_if(columns, [&] (const column& c) {
return std::get<sstring>(c) == name;
});
}
void table_description::add_column(std::vector<column>& columns, const sstring& name, data_type type) {
SCYLLA_ASSERT(find_column(columns, name) == columns.end());
columns.emplace_back(name, type);
}
void table_description::add_old_column(const sstring& name, data_type type) {
_removed_columns.emplace_back(removed_column { name, type, previously_removed_column_timestamp });
}
void table_description::remove_column(std::vector<column>& columns, const sstring& name) {
auto it = find_column(columns, name);
SCYLLA_ASSERT(it != columns.end());
_removed_columns.emplace_back(removed_column { name, std::get<data_type>(*it), column_removal_timestamp });
columns.erase(it);
}
void table_description::alter_column_type(std::vector<column>& columns, const sstring& name, data_type new_type) {
auto it = find_column(columns, name);
SCYLLA_ASSERT(it != columns.end());
std::get<data_type>(*it) = new_type;
}
schema_ptr table_description::build_schema() const {
auto sb = schema_builder("ks", "cf");
for (auto&& [ name, type ] : _partition_key) {
sb.with_column(utf8_type->decompose(name), type, column_kind::partition_key);
}
for (auto&& [ name, type ] : _clustering_key) {
sb.with_column(utf8_type->decompose(name), type, column_kind::clustering_key);
}
for (auto&& [ name, type ] : _static_columns) {
sb.with_column(utf8_type->decompose(name), type, column_kind::static_column);
}
for (auto&& [ name, type ] : _regular_columns) {
sb.with_column(utf8_type->decompose(name), type);
}
for (auto&& [ name, type, timestamp ] : _removed_columns) {
sb.without_column(name, type, timestamp);
}
return sb.build();
}
utils::chunked_vector<mutation> table_description::build_mutations(schema_ptr s) const {
auto ms = _mutations | std::views::transform([&] (const mutation_description& md) {
return md.build(s);
}) | std::ranges::to<utils::chunked_vector<mutation>>();
std::ranges::sort(ms, mutation_decorated_key_less_comparator());
return ms;
}
table_description::table_description(std::vector<column> partition_key, std::vector<column> clustering_key)
: _partition_key(std::move(partition_key))
, _clustering_key(std::move(clustering_key))
{ }
void table_description::add_static_column(const sstring& name, data_type type) {
_change_log.emplace_back(format("added static column \'{}\' of type \'{}\'", name, type->as_cql3_type().to_string()));
add_column(_static_columns, name, type);
}
void table_description::add_regular_column(const sstring& name, data_type type) {
_change_log.emplace_back(format("added regular column \'{}\' of type \'{}\'", name, type->as_cql3_type().to_string()));
add_column(_regular_columns, name, type);
}
void table_description::add_old_static_column(const sstring& name, data_type type) {
add_old_column(name, type);
}
void table_description::add_old_regular_column(const sstring& name, data_type type) {
add_old_column(name, type);
}
void table_description::remove_static_column(const sstring& name) {
_change_log.emplace_back(format("removed static column \'{}\'", name));
remove_column(_static_columns, name);
for (auto& m : _mutations) {
m.remove_static_column(name);
}
}
void table_description::remove_regular_column(const sstring& name) {
_change_log.emplace_back(format("removed regular column \'{}\'", name));
remove_column(_regular_columns, name);
for (auto& m : _mutations) {
m.remove_regular_column(name);
}
}
void table_description::alter_partition_column_type(const sstring& name, data_type new_type) {
_change_log.emplace_back(format("altered partition column \'{}\' type to \'{}\'", name, new_type->as_cql3_type().to_string()));
alter_column_type(_partition_key, name, new_type);
}
void table_description::alter_clustering_column_type(const sstring& name, data_type new_type) {
_change_log.emplace_back(format("altered clustering column \'{}\' type to \'{}\'", name, new_type->as_cql3_type().to_string()));
alter_column_type(_clustering_key, name, new_type);
}
void table_description::alter_static_column_type(const sstring& name, data_type new_type) {
_change_log.emplace_back(format("altered static column \'{}\' type to \'{}\'", name, new_type->as_cql3_type().to_string()));
alter_column_type(_static_columns, name, new_type);
}
void table_description::alter_regular_column_type(const sstring& name, data_type new_type) {
_change_log.emplace_back(format("altered regular column \'{}\' type to \'{}\'", name, new_type->as_cql3_type().to_string()));
alter_column_type(_regular_columns, name, new_type);
}
void table_description::rename_partition_column(const sstring& from, const sstring& to) {
_change_log.emplace_back(format("renamed partition column \'{}\' to \'{}\'", from, to));
auto it = find_column(_partition_key, from);
SCYLLA_ASSERT(it != _partition_key.end());
std::get<sstring>(*it) = to;
}
void table_description::rename_clustering_column(const sstring& from, const sstring& to) {
_change_log.emplace_back(format("renamed clustering column \'{}\' to \'{}\'", from, to));
auto it = find_column(_clustering_key, from);
SCYLLA_ASSERT(it != _clustering_key.end());
std::get<sstring>(*it) = to;
}
table_description::table table_description::build() const {
auto s = build_schema();
return { fmt::to_string(fmt::join(_change_log, "\n")), s, build_mutations(s) };
}
}