Files
scylladb/raft/logical_clock.hh
Kefu Chai 3a8a3100af raft: add formatter for raft::logical_clock::time_point
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 a formatter for logical_clock::time_point, as fmt does not
  provide formatter for this time_point, as it is not a part of the
  standard library
* remove operator<<() for logical_clock::time_point, as its soly
  purpose is to generate the corresponding fmt::formatter when
  FMT_DEPRECATED_OSTREAM is defined.
* remove operator<<() for logical_clock::duration, as fmt provides
  a default implementation for formatting
  std::chrono::nanoseconds already, which uses `int64_t` as its rep
  template parameter as well.
* include "fmt/chrono.h" so that the source files including this
  header can have access the formatter without including it by
  themselves, this preserve the existing behavior which we have
  before removal of "operator<<()".

Refs #13245

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

Closes scylladb/scylladb#16263
2023-12-04 18:32:03 +02:00

57 lines
1.5 KiB
C++

/*
* Copyright (C) 2020-present ScyllaDB
*/
/*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#pragma once
#include <chrono>
#include <fmt/chrono.h>
namespace raft {
// Raft protocol state machine clock ticks at different speeds
// depending on the environment. A typical clock tick for
// a production system is 100ms, while a test system can
// tick it at the speed of the hardware clock.
//
// Every state machine has an own instance of logical clock,
// this enables tests when different state machines run at
// different clock speeds.
class logical_clock final {
public:
using rep = int64_t;
// There is no realistic period for a logical clock,
// just use the smallest period possible.
using period = std::chrono::nanoseconds::period;
using duration = std::chrono::duration<rep, period>;
using time_point = std::chrono::time_point<logical_clock, duration>;
static constexpr bool is_steady = true;
void advance(duration diff = duration{1}) {
_now += diff;
}
time_point now() const noexcept {
return _now;
}
static constexpr time_point min() {
return time_point(duration{0});
}
private:
time_point _now = min();
};
} // end of namespace raft
template <>
struct fmt::formatter<raft::logical_clock::time_point> {
constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
auto format(const raft::logical_clock::time_point& p, fmt::format_context& ctx) const {
return fmt::format_to(ctx.out(), "{}", (p - raft::logical_clock::min()).count());
}
};