Files
scylladb/test/raft/replication.cc
Kefu Chai 372a4d1b79 treewide: do not define FMT_DEPRECATED_OSTREAM
since we do not rely on FMT_DEPRECATED_OSTREAM to define the
fmt::formatter for us anymore, let's stop defining `FMT_DEPRECATED_OSTREAM`.

in this change,

* utils: drop the range formatters in to_string.hh and to_string.c, as
  we don't use them anymore. and the tests for them in
  test/boost/string_format_test.cc are removed accordingly.
* utils: use fmt to print chunk_vector and small_vector. as
  we are not able to print the elements using operator<< anymore
  after switching to {fmt} formatters.
* test/boost: specialize fmt::details::is_std_string_like<bytes>
  due to a bug in {fmt} v9, {fmt} fails to format a range whose
  element type is `basic_sstring<uint8_t>`, as it considers it
  as a string-like type, but `basic_sstring<uint8_t>`'s char type
  is signed char, not char. this issue does not exist in {fmt} v10,
  so, in this change, we add a workaround to explicitly specialize
  the type trait to assure that {fmt} format this type using its
  `fmt::formatter` specialization instead of trying to format it
  as a string. also, {fmt}'s generic ranges formatter calls the
  pair formatter's `set_brackets()` and `set_separator()` methods
  when printing the range, but operator<< based formatter does not
  provide these method, we have to include this change in the change
  switching to {fmt}, otherwise the change specializing
  `fmt::details::is_std_string_like<bytes>` won't compile.
* test/boost: in tests, we use `BOOST_REQUIRE_EQUAL()` and its friends
  for comparing values. but without the operator<< based formatters,
  Boost.Test would not be able to print them. after removing
  the homebrew formatters, we need to use the generic
  `boost_test_print_type()` helper to do this job. so we are
  including `test_utils.hh` in tests so that we can print
  the formattable types.
* treewide: add "#include "utils/to_string.hh" where
  `fmt::formatter<optional<>>` is used.
* configure.py: do not define FMT_DEPRECATED_OSTREAM
* cmake: do not define FMT_DEPRECATED_OSTREAM

Refs #13245

Signed-off-by: Kefu Chai <kefu.chai@scylladb.com>
2024-04-19 22:57:36 +08:00

94 lines
2.9 KiB
C++

/*
* Copyright (C) 2021-present ScyllaDB
*/
/*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#undef SEASTAR_TESTING_MAIN
#include "utils/to_string.hh"
#include "replication.hh"
seastar::logger tlogger("test");
seastar::semaphore snapshot_sync(0);
// application of a snapshot with that id will be delayed until snapshot_sync is signaled
raft::snapshot_id delay_apply_snapshot{utils::UUID(0, 0xdeadbeaf)};
// sending of a snapshot with that id will be delayed until snapshot_sync is signaled
raft::snapshot_id delay_send_snapshot{utils::UUID(0xdeadbeaf, 0)};
std::vector<raft::server_id> to_raft_id_vec(std::vector<node_id> nodes) noexcept {
std::vector<raft::server_id> ret;
for (auto node: nodes) {
ret.push_back(raft::server_id{to_raft_uuid(node.id)});
}
return ret;
}
raft::server_address_set address_set(std::vector<node_id> nodes) noexcept {
return address_set(to_raft_id_vec(nodes));
}
raft::config_member_set config_set(std::vector<node_id> nodes) noexcept {
return config_set(to_raft_id_vec(nodes));
}
size_t test_case::get_first_val() {
// Count existing leader snap index and entries, if present
size_t first_val = 0;
if (initial_leader < initial_states.size()) {
first_val += initial_states[initial_leader].le.size();
}
if (initial_leader < initial_snapshots.size()) {
first_val = initial_snapshots[initial_leader].snap.idx;
}
return first_val;
}
std::mt19937 random_generator() noexcept {
auto& gen = seastar::testing::local_random_engine;
return std::mt19937(gen());
}
int rand() noexcept {
static thread_local std::uniform_int_distribution<int> dist(0, std::numeric_limits<uint8_t>::max());
static thread_local auto gen = random_generator();
return dist(gen);
}
size_t apply_changes(raft::server_id id, const std::vector<raft::command_cref>& commands,
lw_shared_ptr<hasher_int> hasher) {
size_t entries = 0;
tlogger.debug("sm::apply_changes[{}] got {} entries", id, commands.size());
for (auto&& d : commands) {
auto is = ser::as_input_stream(d);
int n = ser::deserialize(is, boost::type<int>());
if (n != dummy_command) {
entries++;
hasher->update(n); // running hash (values and snapshots)
tlogger.debug("{}: apply_changes {}", id, n);
}
}
return entries;
}
std::vector<raft::log_entry> create_log(std::vector<log_entry> list, unsigned start_idx) {
std::vector<raft::log_entry> log;
unsigned i = start_idx;
for (auto e : list) {
if (std::holds_alternative<int>(e.data)) {
log.push_back(raft::log_entry{raft::term_t(e.term), raft::index_t(i++),
create_command(std::get<int>(e.data))});
} else {
log.push_back(raft::log_entry{raft::term_t(e.term), raft::index_t(i++),
std::get<raft::configuration>(e.data)});
}
}
return log;
}