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

280 lines
12 KiB
C++

/*
* Copyright (C) 2019-present ScyllaDB
*/
/*
* SPDX-License-Identifier: LicenseRef-ScyllaDB-Source-Available-1.0
*/
#pragma once
#include "schema/schema.hh"
#include "test/lib/data_model.hh"
///
/// Random schema and random data generation related utilities.
///
class cql_test_env;
namespace tests {
class random_schema_specification {
public:
using compress_sstable = bool_class<class compress_sstable_tag>;
private:
sstring _keyspace_name;
public:
explicit random_schema_specification(sstring keyspace_name) : _keyspace_name(std::move(keyspace_name)) { }
virtual ~random_schema_specification() = default;
// Should be the same for all invocations
const sstring& keyspace_name() const { return _keyspace_name; }
// Should be unique on the instance level.
virtual sstring table_name(std::mt19937& engine) = 0;
// Should be unique on the instance level.
virtual sstring udt_name(std::mt19937& engine) = 0;
virtual std::vector<data_type> partition_key_columns(std::mt19937& engine) = 0;
virtual std::vector<data_type> clustering_key_columns(std::mt19937& engine) = 0;
virtual std::vector<data_type> regular_columns(std::mt19937& engine) = 0;
virtual std::vector<data_type> static_columns(std::mt19937& engine) = 0;
virtual compress_sstable& compress() = 0;
};
/// Helper class that can generate a subset of all valid combination of types.
///
/// Can be used to implement concrete random schema specifications.
/// TODO: counters
class type_generator {
public:
using is_multi_cell = bool_class<class is_multi_cell_tag>;
private:
using generator = std::function<data_type(std::mt19937&, is_multi_cell)>;
private:
random_schema_specification& _spec;
std::vector<generator> _generators;
public:
explicit type_generator(random_schema_specification& spec);
// This is captured.
type_generator(type_generator&&) = delete;
data_type operator()(std::mt19937& engine, is_multi_cell multi_cell);
};
/// The default random schema specification.
///
/// Warning: reusing the same keyspace_name across specs can lead to user
/// defined type clashes.
std::unique_ptr<random_schema_specification> make_random_schema_specification(
sstring keyspace_name,
std::uniform_int_distribution<size_t> partition_column_count_dist = std::uniform_int_distribution<size_t>(1, 4),
std::uniform_int_distribution<size_t> clustering_column_count_dist = std::uniform_int_distribution<size_t>(0, 4),
std::uniform_int_distribution<size_t> regular_column_count_dist = std::uniform_int_distribution<size_t>(1, 4),
std::uniform_int_distribution<size_t> static_column_count_dist = std::uniform_int_distribution<size_t>(0, 4),
random_schema_specification::compress_sstable compress = random_schema_specification::compress_sstable::yes);
/// Generate values for any type.
///
/// Values sizes:
/// * string types (ascii, utf8, bytes):
/// - 95.0% [ 0, 32) characters.
/// - 4.5% [ 32, 100) characters.
/// - 0.4% [ 100, 1000) characters.
/// - 0.1% [1000, 10000) characters.
/// * collections: max 16 elements.
/// * frozen collections: max 4 elements.
/// For native types, the intent is to cover the entire value range.
/// TODO: counters
class value_generator {
public:
using atomic_value_generator = std::function<data_value(std::mt19937&, size_t, size_t)>;
using generator = std::function<data_model::mutation_description::value(std::mt19937&)>;
static const size_t no_size_in_bytes_limit{std::numeric_limits<size_t>::max()};
private:
std::unordered_map<const abstract_type*, atomic_value_generator> _regular_value_generators;
std::unordered_map<const abstract_type*, size_t> _regular_value_min_sizes;
public:
value_generator();
value_generator(value_generator&&) = delete;
/// Only for atomic types.
size_t min_size(const abstract_type& type);
atomic_value_generator get_atomic_value_generator(const abstract_type& type);
// Generate a value for the given type, according to the provided size constraints.
// Controlling the size of values only really works with string-like types and collections of these.
data_value generate_atomic_value(std::mt19937& engine, const abstract_type& type, size_t max_size_in_bytes = no_size_in_bytes_limit);
data_value generate_atomic_value(std::mt19937& engine, const abstract_type& type, size_t min_size_in_bytes, size_t max_size_in_bytes);
generator get_generator(const abstract_type& type);
data_model::mutation_description::value generate_value(std::mt19937& engine, const abstract_type& type);
};
enum class timestamp_destination {
partition_tombstone,
row_marker,
cell_timestamp,
collection_cell_timestamp,
row_tombstone,
collection_tombstone,
range_tombstone,
};
/// Functor that generates timestamps for various destinations.
using timestamp_generator = std::function<api::timestamp_type(std::mt19937& engine, timestamp_destination destination,
api::timestamp_type min_timestamp)>;
/// The default timestamp generator.
///
/// Generates fully random timestamps in the range:
/// [api::min_timestamp, api::max_timestamp]
/// Ignores timestamp destination.
timestamp_generator default_timestamp_generator();
/// Use this to generate mutations that cannot be compacted
///
/// Tombstones will not cover lower level tombstones, or data.
timestamp_generator uncompactible_timestamp_generator(uint32_t seed);
struct expiry_info {
gc_clock::duration ttl;
gc_clock::time_point expiry_point;
};
/// Functor that generates expiry for various destinations.
/// A disengaged optional means the cell doesn't expire. When the destination is
/// a tombstone, gc_clock::now() + schema::gc_grace_seconds() will be used as
/// the expiry instead.
/// The `expiry_info::ttl` is always ignored for tombstone destinations (because
/// they have a fixed ttl as determined by `schema::gc_grace_seconds()`.
using expiry_generator = std::function<std::optional<expiry_info>(std::mt19937& engine, timestamp_destination destination)>;
/// Always returns disengaged optionals.
expiry_generator no_expiry_expiry_generator();
/// Utility class wrapping a randomly generated schema.
///
/// The schema is generated when the class is constructed.
/// The generation is deterministic, the same seed will generate the same schema.
class random_schema {
schema_ptr _schema;
private:
static data_model::mutation_description::key make_key(uint32_t n, value_generator& gen, schema::const_iterator_range_type columns,
size_t max_size_in_bytes);
data_model::mutation_description::key make_partition_key(uint32_t n, value_generator& gen) const;
data_model::mutation_description::key make_clustering_key(uint32_t n, value_generator& gen) const;
public:
/// Create a random schema.
///
/// Passing the same seed and spec will yield the same schema. Part of this
/// guarantee rests on the spec, which, if a custom one is used, should
/// make sure to honor this guarantee.
random_schema(uint32_t seed, random_schema_specification& spec);
schema_ptr schema() const {
return _schema;
}
sstring cql() const;
/// Create the generated schema as a table via CQL.
///
/// Along with all its dependencies, like UDTs.
/// The underlying schema_ptr instance is replaced with the one from the
/// local table instance.
future<> create_with_cql(cql_test_env& env);
/// Make a partition key which is n-th in some arbitrary sequence of keys.
///
/// There is no particular order for the keys, they're not in ring order.
/// This method is deterministic, the pair of the seed used to generate the
/// schema and `n` will map to the same generated value.
data_model::mutation_description::key make_pkey(uint32_t n);
/// Make n partition keys.
///
/// Keys are in ring order.
/// This method is deterministic, the pair of the seed used to generate the
/// schema and `n` will map to the same generated values.
std::vector<data_model::mutation_description::key> make_pkeys(size_t n);
/// Make a clustering key which is n-th in some arbitrary sequence of keys.
///
/// There is no particular order for the keys, they're not in clustering order.
/// This method is deterministic, the pair of the seed used to generate the
/// schema and `n` will map to the same generated value.
data_model::mutation_description::key make_ckey(uint32_t n);
/// Make up to n clustering keys.
///
/// Key are in clustering order.
/// This method is deterministic, the pair of the seed used to generate the
/// schema and `n` will map to the same generated values.
/// Fewer than n keys may be returned if the schema limits the clustering keys space.
std::vector<data_model::mutation_description::key> make_ckeys(size_t n);
data_model::mutation_description new_mutation(data_model::mutation_description::key pkey);
/// Make a new mutation with a key produced via `make_pkey(n)`.
data_model::mutation_description new_mutation(uint32_t n);
/// Set the partition tombstone
void set_partition_tombstone(std::mt19937& engine, data_model::mutation_description& md,
timestamp_generator ts_gen = default_timestamp_generator(),
expiry_generator exp_gen = no_expiry_expiry_generator());
void add_row(std::mt19937& engine, data_model::mutation_description& md, data_model::mutation_description::key ckey,
timestamp_generator ts_gen = default_timestamp_generator(),
expiry_generator exp_gen = no_expiry_expiry_generator());
/// Add a new row with a key produced via `make_ckey(n)`.
void add_row(std::mt19937& engine, data_model::mutation_description& md, uint32_t n, timestamp_generator ts_gen = default_timestamp_generator(),
expiry_generator exp_gen = no_expiry_expiry_generator());
void add_static_row(std::mt19937& engine, data_model::mutation_description& md, timestamp_generator ts_gen = default_timestamp_generator(),
expiry_generator exp_gen = no_expiry_expiry_generator());
void delete_range(
std::mt19937& engine,
data_model::mutation_description& md,
interval<data_model::mutation_description::key> range,
timestamp_generator ts_gen = default_timestamp_generator(),
expiry_generator exp_gen = no_expiry_expiry_generator());
};
/// Generate random mutations using the random schema.
///
/// `clustering_row_count_dist` and `range_tombstone_count_dist` will be used to
/// generate the respective counts for *each* partition. These params are
/// ignored if the schema has no clustering columns.
/// Mutations are returned in ring order. Does not contain duplicate partitions.
/// Futurized to avoid stalls.
future<utils::chunked_vector<mutation>> generate_random_mutations(
uint32_t seed,
tests::random_schema& random_schema,
timestamp_generator ts_gen = default_timestamp_generator(),
expiry_generator exp_gen = no_expiry_expiry_generator(),
std::uniform_int_distribution<size_t> partition_count_dist = std::uniform_int_distribution<size_t>(8, 16),
std::uniform_int_distribution<size_t> clustering_row_count_dist = std::uniform_int_distribution<size_t>(16, 128),
std::uniform_int_distribution<size_t> range_tombstone_count_dist = std::uniform_int_distribution<size_t>(4, 16));
future<utils::chunked_vector<mutation>> generate_random_mutations(
tests::random_schema& random_schema,
timestamp_generator ts_gen = default_timestamp_generator(),
expiry_generator exp_gen = no_expiry_expiry_generator(),
std::uniform_int_distribution<size_t> partition_count_dist = std::uniform_int_distribution<size_t>(8, 16),
std::uniform_int_distribution<size_t> clustering_row_count_dist = std::uniform_int_distribution<size_t>(16, 128),
std::uniform_int_distribution<size_t> range_tombstone_count_dist = std::uniform_int_distribution<size_t>(4, 16));
/// Generate exactly partition_count partitions. See the more general overload above.
future<utils::chunked_vector<mutation>> generate_random_mutations(tests::random_schema& random_schema, size_t partition_count);
} // namespace tests