Files
scylladb/raft/logical_clock.hh
Gleb Natapov e1ac1a61c9 raft: Implement log replication and leader election
This patch introduces partial RAFT implementation. It has only log
replication and leader election support. Snapshotting and configuration
change along with other, smaller features are not yet implemented.

The approach taken by this implementation is to have a deterministic
state machine coded in raft::fsm. What makes the FSM deterministic is
that it does not do any IO by itself. It only takes an input (which may
be a networking message, time tick or new append message), changes its
state and produce an output. The output contains the state that has
to be persisted, messages that need to be sent and entries that may
be applied (in that order). The input and output of the FSM is handled
by raft::server class. It uses raft::rpc interface to send and receive
messages and raft::storage interface to implement persistence.
2020-10-01 14:30:59 +03:00

73 lines
2.1 KiB
C++

/*
* Copyright (C) 2020 ScyllaDB
*/
/*
* This file is part of Scylla.
*
* Scylla is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Scylla is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Scylla. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <chrono>
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();
};
inline std::ostream& operator<<(std::ostream& os, const logical_clock::time_point& p) {
return os << (p - logical_clock::min()).count();
}
} // end of namespace raft
namespace std {
inline std::ostream& operator<<(std::ostream& os, const raft::logical_clock::duration& d) {
return os << d.count();
}
} // end of namespace std