raft: operator<<(ostream&, ...) implementation for server_address and configuration
Useful for debugging. Had to make `configuration` constructor explicit. Otherwise the `operator<<` implementation for `configuration` would implicitly convert the `server_address` to `configuration` when trying to output it, causing infinite recursion. Removed implicit uses of the constructor.
This commit is contained in:
12
raft/raft.cc
12
raft/raft.cc
@@ -6,9 +6,21 @@
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
#include "raft.hh"
|
||||
#include "to_string.hh"
|
||||
|
||||
namespace raft {
|
||||
|
||||
seastar::logger logger("raft");
|
||||
|
||||
std::ostream& operator<<(std::ostream& os, const raft::server_address& addr) {
|
||||
return os << addr.id;
|
||||
}
|
||||
|
||||
std::ostream& operator<<(std::ostream& os, const raft::configuration& cfg) {
|
||||
if (cfg.previous.empty()) {
|
||||
return os << cfg.current;
|
||||
}
|
||||
return os << format("{}->{}", cfg.previous, cfg.current);
|
||||
}
|
||||
|
||||
} // end of namespace raft
|
||||
|
||||
@@ -63,6 +63,8 @@ struct server_address {
|
||||
bool operator<(const server_address& rhs) const {
|
||||
return id < rhs.id;
|
||||
}
|
||||
|
||||
friend std::ostream& operator<<(std::ostream&, const server_address&);
|
||||
};
|
||||
|
||||
} // end of namespace raft
|
||||
@@ -105,7 +107,7 @@ struct configuration {
|
||||
}
|
||||
}
|
||||
|
||||
configuration(server_address_set current_arg = {}, server_address_set previous_arg = {})
|
||||
explicit configuration(server_address_set current_arg = {}, server_address_set previous_arg = {})
|
||||
: current(std::move(current_arg)), previous(std::move(previous_arg)) {
|
||||
if (current.count(server_address{server_id()}) || previous.count(server_address{server_id()})) {
|
||||
throw std::invalid_argument("raft::configuration: id zero is not supported");
|
||||
@@ -195,6 +197,8 @@ struct configuration {
|
||||
assert(is_joint());
|
||||
previous.clear();
|
||||
}
|
||||
|
||||
friend std::ostream& operator<<(std::ostream&, const configuration&);
|
||||
};
|
||||
|
||||
struct log_entry {
|
||||
@@ -270,7 +274,7 @@ struct snapshot_descriptor {
|
||||
index_t idx = index_t(0);
|
||||
term_t term = term_t(0);
|
||||
// The committed configuration in the snapshot
|
||||
configuration config;
|
||||
configuration config{};
|
||||
// Id of the snapshot.
|
||||
snapshot_id id;
|
||||
};
|
||||
|
||||
@@ -2403,12 +2403,6 @@ std::ostream& operator<<(std::ostream& os, const AppendReg::ret& r) {
|
||||
return os << format("ret{{{}, {}}}", r.x, r.prev);
|
||||
}
|
||||
|
||||
namespace raft {
|
||||
std::ostream& operator<<(std::ostream& os, const raft::server_address& a) {
|
||||
return os << a.id;
|
||||
}
|
||||
}
|
||||
|
||||
SEASTAR_TEST_CASE(basic_generator_test) {
|
||||
using op_type = operation::invocable<operation::either_of<
|
||||
raft_call<AppendReg>,
|
||||
|
||||
@@ -375,7 +375,7 @@ RAFT_TEST_CASE(rpc_configuration_truncate_restore_from_snp, (test_case{
|
||||
.log = { raft::log_entry{raft::term_t(1), raft::index_t(1),
|
||||
config{.curr = {node_id{0},node_id{1},node_id{2},node_id{3}},
|
||||
.prev = {node_id{0},node_id{1},node_id{2}}}}},
|
||||
.snapshot = {.config = address_set({node_id{0},node_id{1},node_id{2}})
|
||||
.snapshot = {.config = raft::configuration{address_set({node_id{0},node_id{1},node_id{2}})}
|
||||
}
|
||||
}},
|
||||
// A should see {A, B, C, D} as RPC config since
|
||||
@@ -472,7 +472,7 @@ RAFT_TEST_CASE(rpc_configuration_truncate_restore_from_log, (test_case{
|
||||
},
|
||||
},
|
||||
// all nodes in snapshot config {A, B, C, D} (original)
|
||||
.snapshot = {.config = address_set({node_id{0},node_id{1},node_id{2},node_id{3}})
|
||||
.snapshot = {.config = raft::configuration{address_set({node_id{0},node_id{1},node_id{2},node_id{3}})}
|
||||
}
|
||||
}},
|
||||
|
||||
@@ -522,7 +522,7 @@ RAFT_TEST_CASE(rpc_configuration_truncate_restore_from_log, (test_case{
|
||||
},
|
||||
},
|
||||
// all nodes in snapshot config {A, B, C, D} (original)
|
||||
.snapshot = {.config = address_set({node_id{0},node_id{1},node_id{2},node_id{3}})
|
||||
.snapshot = {.config = raft::configuration{address_set({node_id{0},node_id{1},node_id{2},node_id{3}})}
|
||||
}
|
||||
}},
|
||||
|
||||
|
||||
Reference in New Issue
Block a user