Skip to content

Commit

Permalink
raft: add fmt::formatter for raft tracker types
Browse files Browse the repository at this point in the history
before this change, we rely on the default-generated fmt::formatter
created from operator<<, but fmt v10 dropped the default-generated
formatter.

in this change, we define formatters for

* raft::election_tracker
* raft::votes
* raft::vote_result

and drop their operator<<:s.

Refs scylladb#13245

Signed-off-by: Kefu Chai <kefu.chai@scylladb.com>

Closes scylladb#17670
  • Loading branch information
tchaikov authored and denesb committed Mar 8, 2024
1 parent 44bbf2e commit 079d701
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 25 deletions.
44 changes: 23 additions & 21 deletions raft/tracker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -256,36 +256,38 @@ vote_result votes::tally_votes() const {
return _current.tally_votes();
}

std::ostream& operator<<(std::ostream& os, const election_tracker& v) {
os << "responded: " << v._responded.size() << ", ";
os << "granted: " << v._granted;
return os;
}
} // end of namespace raft

auto fmt::formatter<raft::election_tracker>::format(const raft::election_tracker& v, fmt::format_context& ctx) const
-> decltype(ctx.out()) {
return fmt::format_to(ctx.out(), "responded: {}, granted: {}",
v._responded.size(), v._granted);
}

std::ostream& operator<<(std::ostream& os, const votes& v) {
os << "current: " << v._current << std::endl;
auto fmt::formatter<raft::votes>::format(const raft::votes& v, fmt::format_context& ctx) const
-> decltype(ctx.out()) {
auto out = ctx.out();
out = fmt::format_to(out, "current: {}\n", v._current);
if (v._previous) {
os << "previous: " << v._previous.value() << std::endl;
out = fmt::format_to(out, "previous: {}\n", v._previous.value());
}
return os;
return out;
}

std::ostream& operator<<(std::ostream& os, const vote_result& v) {
static const char *n;
auto fmt::formatter<raft::vote_result>::format(const raft::vote_result& v, fmt::format_context& ctx) const
-> decltype(ctx.out()) {
std::string_view name;
using enum raft::vote_result;
switch (v) {
case vote_result::UNKNOWN:
n = "UNKNOWN";
case UNKNOWN:
name = "UNKNOWN";
break;
case vote_result::WON:
n = "WON";
case WON:
name = "WON";
break;
case vote_result::LOST:
n = "LOST";
case LOST:
name = "LOST";
break;
}
os << n;
return os;
return formatter<std::string_view>::format(name, ctx);
}

} // end of namespace raft
18 changes: 14 additions & 4 deletions raft/tracker.hh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#pragma once

#include <seastar/core/condition-variable.hh>
#include <fmt/core.h>
#include "raft.hh"

namespace raft {
Expand Down Expand Up @@ -145,8 +146,6 @@ enum class vote_result {
LOST,
};

std::ostream& operator<<(std::ostream& os, const vote_result& v);

// State of election in a single quorum
class election_tracker {
// All eligible voters
Expand Down Expand Up @@ -182,7 +181,7 @@ public:
auto unknown = _suffrage.size() - _responded.size();
return _granted + unknown >= quorum ? vote_result::UNKNOWN : vote_result::LOST;
}
friend std::ostream& operator<<(std::ostream& os, const election_tracker& v);
friend fmt::formatter<election_tracker>;
};

// Candidate's state specific to election
Expand All @@ -202,8 +201,19 @@ public:
void register_vote(server_id from, bool granted);
vote_result tally_votes() const;

friend std::ostream& operator<<(std::ostream& os, const votes& v);
friend fmt::formatter<votes>;
};

} // namespace raft

template <> struct fmt::formatter<raft::election_tracker> : fmt::formatter<std::string_view> {
auto format(const raft::election_tracker& v, fmt::format_context& ctx) const -> decltype(ctx.out());
};

template <> struct fmt::formatter<raft::votes> : fmt::formatter<std::string_view> {
auto format(const raft::votes& v, fmt::format_context& ctx) const -> decltype(ctx.out());
};

template <> struct fmt::formatter<raft::vote_result> : fmt::formatter<std::string_view> {
auto format(const raft::vote_result& v, fmt::format_context& ctx) const -> decltype(ctx.out());
};
10 changes: 10 additions & 0 deletions test/raft/fsm_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,20 @@

#define BOOST_TEST_MODULE raft

#include "raft/tracker.hh"
#include "test/raft/helpers.hh"

using namespace raft;

namespace raft {

std::ostream& boost_test_print_type(std::ostream& os, const vote_result& v) {
fmt::print(os, "{}", v);
return os;
}

}

BOOST_AUTO_TEST_CASE(test_votes) {
auto id1 = id();

Expand Down

0 comments on commit 079d701

Please sign in to comment.