From 4e8a86c6a11810c7b6bb573561efff3ebf3c63e6 Mon Sep 17 00:00:00 2001 From: Kamil Braun Date: Thu, 21 Oct 2021 14:44:27 +0200 Subject: [PATCH] test: raft: randomized_nemesis_test: persistence_proxy: store a shared pointer to `persistence` We want the test to be able to reuse `persistence` even after `persistence_proxy` is destroyed for simulating server restarts. We'll do it by having the test keep a shared pointer to `persistence`. To do that, instead of storing `persistence` by value and constructing it inside `persistence_proxy`, store it by `lw_shared_ptr` which is taken through the constructor (so `persistence` itself is now constructed outside of `persistence_proxy`). --- test/raft/randomized_nemesis_test.cc | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/test/raft/randomized_nemesis_test.cc b/test/raft/randomized_nemesis_test.cc index e20de7d199..9302b45f83 100644 --- a/test/raft/randomized_nemesis_test.cc +++ b/test/raft/randomized_nemesis_test.cc @@ -794,25 +794,21 @@ public: template class persistence_proxy : public raft::persistence { snapshots_t& _snapshots; - ::persistence _persistence; + lw_shared_ptr<::persistence> _persistence; public: - // If this is the first server of a cluster, it must be initialized with a singleton configuration - // containing opnly this server's ID which must be also provided here as `init_config_id`. - // Otherwise it must be initialized with an empty configuration (it will be added to the cluster - // through a configuration change) and `init_config_id` must be `nullopt`. - persistence_proxy(snapshots_t& snaps, std::optional init_config_id, State init_state) + persistence_proxy(snapshots_t& snaps, lw_shared_ptr<::persistence> persistence) : _snapshots(snaps) - , _persistence(std::move(init_config_id), std::move(init_state)) + , _persistence(std::move(persistence)) {} virtual future<> store_term_and_vote(raft::term_t term, raft::server_id vote) override { - _persistence.store_term_and_vote(term, vote); + _persistence->store_term_and_vote(term, vote); co_return; } virtual future> load_term_and_vote() override { - co_return _persistence.load_term_and_vote(); + co_return _persistence->load_term_and_vote(); } // Stores not only the snapshot descriptor but also the corresponding snapshot. @@ -820,28 +816,28 @@ public: auto it = _snapshots.find(snap.id); assert(it != _snapshots.end()); - _persistence.store_snapshot(snap, it->second, preserve_log_entries); + _persistence->store_snapshot(snap, it->second, preserve_log_entries); co_return; } // Loads not only the snapshot descriptor but also the corresponding snapshot. virtual future load_snapshot_descriptor() override { - auto [snap, state] = _persistence.load_snapshot(); + auto [snap, state] = _persistence->load_snapshot(); _snapshots.insert_or_assign(snap.id, std::move(state)); co_return snap; } virtual future<> store_log_entries(const std::vector& entries) override { - _persistence.store_log_entries(entries); + _persistence->store_log_entries(entries); co_return; } virtual future load_log() override { - co_return _persistence.load_log(); + co_return _persistence->load_log(); } virtual future<> truncate_log(raft::index_t idx) override { - _persistence.truncate_log(idx); + _persistence->truncate_log(idx); co_return; } @@ -1103,7 +1099,8 @@ public: auto snapshots = std::make_unique>(); auto sm = std::make_unique>(*snapshots); auto rpc_ = std::make_unique>(id, *snapshots, std::move(send_rpc)); - auto persistence_ = std::make_unique>(*snapshots, first_server ? std::optional{id} : std::nullopt, M::init); + auto persistence_ = std::make_unique>(*snapshots, + make_lw_shared>(first_server ? std::optional{id} : std::nullopt, M::init)); auto& sm_ref = *sm; auto& rpc_ref = *rpc_;