From f72e89fcfe60161b6396daef6ed7547bdfd05308 Mon Sep 17 00:00:00 2001 From: Alejo Sanchez Date: Mon, 1 Mar 2021 10:11:27 -0400 Subject: [PATCH] raft: replication test: parametrize drop_replication Pass drop_replication down instead of keeping it global. Signed-off-by: Alejo Sanchez --- test/raft/replication_test.cc | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/test/raft/replication_test.cc b/test/raft/replication_test.cc index b6feb4ae26..118f5a83da 100644 --- a/test/raft/replication_test.cc +++ b/test/raft/replication_test.cc @@ -76,8 +76,6 @@ int rand() { return dist(gen); } -bool drop_replication = false; - class sm_value_impl { public: sm_value_impl() {}; @@ -273,8 +271,9 @@ public: class rpc : public raft::rpc { static std::unordered_map net; raft::server_id _id; + bool _drop_replication; public: - rpc(raft::server_id id) : _id(id) { + rpc(raft::server_id id, bool drop_replication) : _id(id), _drop_replication(drop_replication) { net[_id] = this; } virtual future<> send_snapshot(raft::server_id id, const raft::install_snapshot& snap) { @@ -285,14 +284,14 @@ public: return net[id]->_client->apply_snapshot(_id, std::move(snap)); } virtual future<> send_append_entries(raft::server_id id, const raft::append_request& append_request) { - if (is_disconnected(id) || is_disconnected(_id) || (drop_replication && !(rand() % 5))) { + if (is_disconnected(id) || is_disconnected(_id) || (_drop_replication && !(rand() % 5))) { return make_ready_future<>(); } net[id]->_client->append_entries(_id, append_request); return make_ready_future<>(); } virtual future<> send_append_entries_reply(raft::server_id id, const raft::append_reply& reply) { - if (is_disconnected(id) || is_disconnected(_id) || (drop_replication && !(rand() % 5))) { + if (is_disconnected(id) || is_disconnected(_id) || (_drop_replication && !(rand() % 5))) { return make_ready_future<>(); } net[id]->_client->append_entries_reply(_id, std::move(reply)); @@ -326,12 +325,12 @@ enum class sm_type { std::pair, state_machine*> create_raft_server(raft::server_id uuid, state_machine::apply_fn apply, initial_state state, - size_t apply_entries, sm_type type) { + size_t apply_entries, sm_type type, bool drop_replication) { sm_value val = (type == sm_type::HASH) ? sm_value(std::make_unique()) : sm_value(std::make_unique()); auto sm = std::make_unique(uuid, std::move(apply), std::move(val), apply_entries); auto& rsm = *sm; - auto mrpc = std::make_unique(uuid); + auto mrpc = std::make_unique(uuid, drop_replication); auto mpersistence = std::make_unique(uuid, state); auto fd = seastar::make_shared(uuid); @@ -341,7 +340,8 @@ create_raft_server(raft::server_id uuid, state_machine::apply_fn apply, initial_ return std::make_pair(std::move(raft), &rsm); } -future, state_machine*>>> create_cluster(std::vector states, state_machine::apply_fn apply, size_t apply_entries, sm_type type) { +future, state_machine*>>> create_cluster(std::vector states, state_machine::apply_fn apply, size_t apply_entries, sm_type type, + bool drop_replication) { raft::configuration config; std::vector, state_machine*>> rafts; @@ -355,7 +355,8 @@ future, state_machine*>>> cr auto& s = states[i].address; states[i].snapshot.config = config; snapshots[s.id] = states[i].snp_value; - auto& raft = *rafts.emplace_back(create_raft_server(s.id, apply, states[i], apply_entries, type)).first; + auto& raft = *rafts.emplace_back(create_raft_server(s.id, apply, states[i], apply_entries, + type, drop_replication)).first; co_await raft.start(); } @@ -464,7 +465,7 @@ void restart_tickers(std::vector>& tickers) { } // Run test case (name, nodes, leader, initial logs, updates) -future run_test(test_case test) { +future run_test(test_case test, bool drop_replication = false) { std::vector states(test.nodes); // Server initial states tlogger.debug("running test {}:", test.name); @@ -510,7 +511,8 @@ future run_test(test_case test) { } } - auto rafts = co_await create_cluster(states, apply_changes, test.total_values, test.type); + auto rafts = co_await create_cluster(states, apply_changes, test.total_values, test.type, + drop_replication); // Tickers for servers std::vector> tickers(test.nodes); @@ -799,10 +801,10 @@ int main(int argc, char* argv[]) { }; return app.run(argc, argv, [&replication_tests, &app] () -> future { - drop_replication = app.configuration()["drop-replication"].as(); + bool drop_replication = app.configuration()["drop-replication"].as(); for (auto test: replication_tests) { - if (co_await run_test(test) != 0) { + if (co_await run_test(test, drop_replication) != 0) { tlogger.error("Test {} failed", test.name); co_return 1; // Fail }