raft-consensus-0.1.0.0: src/Distributed/Consensus/Raft/Implementation.hs
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE ExistentialQuantification #-}
module Distributed.Consensus.Raft.Implementation
( -- * Protocol implementation
Implementation (..),
Networking (..),
Persistence (..),
-- ** Helpers
nullTracer,
ephemeralPersistence,
-- * Types
LogEntry (..),
Command (..),
CommandResponse (..),
AppendEntries (..),
AppendEntriesResult (..),
InstallSnapshot (..),
InstallSnapshotResult (..),
ClusterMembershipRequest (..),
ClusterMembershipResult (..),
ClusterMembershipError (..),
AdminRequest,
Event (..),
RPC (..),
RPCResult (..),
EventContext (..),
RaftTrace (..),
)
where
import Data.Binary (Binary)
import Data.List.NonEmpty (NonEmpty)
import Data.Sequence (Seq)
import Distributed.Consensus.Raft.Domain (ClusterConfiguration (..), LogIndex, ReadOnlySequenceNum, Snapshot, SnapshotMetadata, StateMachineStep, Term)
import Distributed.Consensus.Raft.Domain.Admin (AdminRequest, AdminResponse)
import Distributed.Consensus.Raft.Domain.Client (ClientRequest, ClientRequestId, ClientResponse, ClientResult)
import Distributed.Consensus.Raft.Log (Log)
import GHC.Generics (Generic)
-- | Implementation of the Raft protocol.
--
-- There are four categories of things that need an implementation:
--
-- * Persistence: how are values persisted durably?
-- * State mechine: given an @entry@, how does the @state@ evolve?
-- * Networking: how are nodes communicating?
-- * Tracing: how is debugging information traced?
data Implementation entry node result state m = Implementation
{ persistence :: Persistence entry node state m,
stateMachine :: entry -> StateMachineStep result state,
networking :: Networking entry node result state m,
-- | Event tracer. This is mostly useful for testing and debugging.
--
-- You can turn tracing off using 'nullTracer'
tracer :: RaftTrace entry node result state -> m ()
}
-- | Ignore 'RaftTrace' events
nullTracer :: (Applicative m) => RaftTrace entry node result state -> m ()
nullTracer _ = pure ()
-- | Networking implementation.
--
-- See @raft-consensus-distributed-process@ for an implementation that fits
-- in the Cloud Haskell ecosystem.
data Networking entry node result state m = Networking
{ -- | Send a remote procedure call to another Raft node.
sendRPC :: node -> RPC entry node state -> m (),
-- | Send the result of a remote procedure call to another Raft node.
sendRPCResult :: node -> RPCResult node result -> m (),
-- | Send a response to a client.
sendClientResponse :: node -> ClientResponse node result -> m (),
-- | Send a response to an administrator.
sendAdminResponse :: node -> AdminResponse node -> m (),
-- | Receive a remote procedure call.
--
-- At the type implies, this call should /block/ until a message is ready.
receiveRPC :: m (RPC entry node state),
-- | Receive the result of a remote procedure call.
--
-- At the type implies, this call should /block/ until a message is ready.
receiveRPCResult :: m (RPCResult node result),
-- | Receive client requests.
--
-- This call should block until at least one request is available. If
-- your software stack allows for it, queueing requests allows
-- for pipelined processing which is much more efficient.
receiveClientRequests :: m (NonEmpty (ClientRequest entry node)),
-- | Receive a request from an administrator.
--
-- At the type implies, this call should /block/ until a message is ready.
receiveAdminRequest :: m (AdminRequest node)
}
-- | Persistence implementation.
--
-- This is broken out into its own type so that it can be provided cleanly by
-- third-party packages. For example, see the @raft-consensus-wal@ package.
data Persistence entry node state m = Persistence
{ -- | Read log entries, starting from and including, a given 'LogIndex'
--
-- Implementations should
readLogEntriesFrom :: node -> LogIndex -> m [(Term, LogIndex, LogEntry entry node)],
-- | Write log entries durably.
--
-- Note that the writes need to support overwriting. In the normal course
-- of running a Raft node, the log may need to be updated. This means that
-- a previously seen '(Term, LogIndex)' may be written again. Only the most recently
-- written entry should be surfaced by 'readLogEntriesFrom'.
--
-- This function also receives the node identification (of type @node@), which
-- allows to persist data in a shared location for multiple nodes
-- (e.g. in a remote database)
writeLogEntry :: node -> [(Term, LogIndex, LogEntry entry node)] -> m (),
-- Truncate log entries starting, and including, the provided log index.
--
-- Note that not all storage technologies support truncation. For example,
-- write-ahead logs cannot implement truncation. However, the goal is that
-- any "truncation" results in entries with this 'LogIndex' and later ones
-- to not be returned by 'readLogEntriesFrom'.
truncateLogEntriesFrom :: node -> LogIndex -> m (),
-- | Read the last persisted term. In case this is not known (for example, on a
-- first boot), then the term should be @0@.
readTerm :: node -> m Term,
-- | Write the current term durably.
--
-- This function also receives the node identification (of type @node@), which
-- allows to persist data in a shared location for multiple nodes
-- (e.g. in a remote database)
writeTerm :: node -> Term -> m (),
-- | Read the node for which /this/ node has voted, for a given term, if known.
readVotedFor :: node -> Term -> m (Maybe node),
-- | Write this node's vote for the current term, durably.
--
-- This function also receives the node identification (of type @node@), which
-- allows to persist data in a shared location for multiple nodes
-- (e.g. in a remote database)
writeVotedFor :: node -> Term -> Maybe node -> m (),
-- | Read the most recent snapshot, if one exists.
readSnapshot :: node -> m (Maybe (Snapshot node state)),
-- | Write a snapshot durably.
--
-- This function also receives the node identification (of type @node@), which
-- allows to persist data in a shared location for multiple nodes
-- (e.g. in a remote database)
writeSnapshot :: node -> Snapshot node state -> m (),
-- | Read the last persisted commit index. A default value of @0@ should be
-- returned if the commit index was never persisted
readCommitIndex :: node -> m LogIndex,
-- | Write the node's commit index. Note that this does
-- not need to be durable; storing the commit index is a startup performance
-- optimization.
writeCommitIndex :: node -> LogIndex -> m ()
}
-- | This 'Persistence' implementation does absolutely nothing.
--
-- Only use this for testing. If a node crashes and restarts without memory
-- of its state, it may vote again leading to multiple leaders in a single
-- cluster, which would be catastrophic.
--
-- An alternative would be for a node's identity (the 'node' type variable)
-- to never be re-used. In this case, it would be perfectly fine to restart
-- with an empty state.
ephemeralPersistence :: (Applicative m) => Persistence entry node state m
ephemeralPersistence =
Persistence
{ readLogEntriesFrom = \_ _ -> pure [],
writeLogEntry = \_ _ -> pure (),
truncateLogEntriesFrom = \_ _ -> pure (),
readTerm = \_ -> pure 0,
writeTerm = \_ _ -> pure (),
readVotedFor = \_ _ -> pure Nothing,
writeVotedFor = \_ _ _ -> pure (),
readSnapshot = \_ -> pure Nothing,
writeSnapshot = \_ _ -> pure (),
readCommitIndex = \_ -> pure 0,
writeCommitIndex = \_ _ -> pure ()
}
-- | A 'Command' comes from clients
data Command entry
= Command
!ClientRequestId
!entry
deriving (Eq, Show, Ord, Generic)
instance (Binary entry) => Binary (Command entry)
-- | A 'LogEntry' is anything which gets persisted
-- in the replicated log. This includes client commands,
-- byt also leadership changes (which are no-op entries)
-- and cluster membership changes.
data LogEntry entry node
= LogEntryCommand (Command entry)
| LogEntryLeaderChange
| LogEntryMembershipChange (ClusterConfiguration node)
deriving (Eq, Show, Ord, Generic)
instance (Binary node, Binary entry) => Binary (LogEntry entry node)
data CommandResponse node result
= MkCommandResponse
!result
!ClientRequestId
deriving (Eq, Show, Ord, Generic)
instance (Binary node, Binary result) => Binary (CommandResponse node result)
data AppendEntries entry node = AppendEntries
{ aeLeaderTerm :: !Term,
aeLeaderNode :: !node,
aePreviousLogIndex :: !LogIndex,
aePreviousLogTerm :: !Term,
aeEntries :: !(Seq (Term, LogEntry entry node)),
aeCommitIndex :: !LogIndex,
aeReadOnlySeqNum :: !ReadOnlySequenceNum
}
deriving (Eq, Show, Ord, Generic)
instance (Binary node, Binary entry) => Binary (AppendEntries entry node)
data AppendEntriesResult node result = AppendEntriesResult
{ aerCurrentTerm :: !Term,
aerNode :: !node,
aerMatch :: !Bool,
aerNewEntryLogIndex :: !LogIndex,
aerReadOnlySeqNum :: !ReadOnlySequenceNum
}
deriving (Eq, Ord, Show, Generic)
instance (Binary node, Binary result) => Binary (AppendEntriesResult node result)
data InstallSnapshot node state = InstallSnapshot
{ isLeaderTerm :: !Term,
isLeaderNode :: !node,
isSnapshot :: !(Snapshot node state)
}
deriving (Eq, Ord, Show, Generic)
instance (Binary node, Binary state) => Binary (InstallSnapshot node state)
data InstallSnapshotResult node = InstallSnapshotResult
{ isrTerm :: !Term,
isrNode :: !node,
isrSnapshotMetadata :: !SnapshotMetadata
}
deriving (Eq, Ord, Show, Generic)
instance (Binary node) => Binary (InstallSnapshotResult node)
data ClusterMembershipRequest node
= ClusterMembershipJoinRequest node
| ClusterMembershipLeaveRequest node
deriving (Eq, Ord, Show, Generic)
instance (Binary node) => Binary (ClusterMembershipRequest node)
data ClusterMembershipError node
= -- | carries the node that replied, not the leader
NoKnownLeader node
| OngoingClusterMembershipChange node
deriving (Eq, Show, Ord, Generic)
instance (Binary node) => Binary (ClusterMembershipError node)
data ClusterMembershipResult node
= ClusterMembershipJoinResult (Either (ClusterMembershipError node) node)
| ClusterMembershipLeaveResult (Either (ClusterMembershipError node) node)
deriving (Eq, Ord, Show, Generic)
instance (Binary node) => Binary (ClusterMembershipResult node)
data RPC entry node state
= AE (AppendEntries entry node)
| IS (InstallSnapshot node state)
| CM (ClusterMembershipRequest node)
| RequestVote
-- | Candidate term
Term
-- | Identification of the candidate requesting vote
node
-- | Index of candidate's last log entry
LogIndex
-- | Term of candidate's last log entry
Term
deriving (Eq, Ord, Show, Generic) -- For easy derivation of de/serialization
instance (Binary node, Binary entry, Binary state) => Binary (RPC entry node state)
data RPCResult node result
= AER (AppendEntriesResult node result)
| ISR (InstallSnapshotResult node)
| CMR (ClusterMembershipResult node)
| RequestVoteResult
-- | Voter node
node
-- | Current term, for leader to update itself
Term
-- | Whether vote was granted
Bool
deriving (Eq, Ord, Show, Generic) -- For easy derivation of de/serialization
instance (Binary node, Binary result) => Binary (RPCResult node result)
data Event entry node result state
= EventElectionTimeout
| EventHeartBeatTimeout
| EventIncomingClientRequest (NonEmpty (ClientRequest entry node))
| EventRPC (RPC entry node state)
| EventRPCResult (RPCResult node result)
| EventAdminRequest (AdminRequest node)
| EventSnapshotPersisted (Snapshot node state)
deriving (Eq, Show)
data EventContext node
= EventContext !Term !node
deriving (Eq)
instance (Show node) => Show (EventContext node) where
show (EventContext term node) = "[Term=" <> show term <> " | node=" <> show node <> "]"
data RaftTrace entry node result state
= StateRestored (EventContext node) (Maybe node) (Log (LogEntry entry node) node)
| LeaderElected (EventContext node)
| VotedFor (EventContext node) !Term node
| VoteRequestedBy (EventContext node) !Term node
| VoteGrantedFrom (EventContext node) node
| VoteDeniedFrom (EventContext node) node
| BecameCandidate (EventContext node)
| BecameFollower (EventContext node)
| BecameNonMember (EventContext node)
| EventReceived (EventContext node) (Event entry node result state)
| SplitElection (EventContext node)
| ElectionTriggered (EventContext node)
| -- | Command received by the leader node. If the command needs to be redirected
-- to another node, this event is not emitted
-- All Commands should have the same request ID
CommandReceived (EventContext node) !ClientRequestId entry
| CommandResultResponded (EventContext node) (CommandResponse node (ClientResult result))
| -- TODO: better traching of new joining/leaving cluster
MembershipChangeInitiated (EventContext node)
| MembershipChangeCompleted (EventContext node)
| AdminRequestReceived (EventContext node) (AdminRequest node)
| JoinedCluster (EventContext node)
| LeftCluster (EventContext node)
| CommitIndexIncreasedTo (EventContext node) !LogIndex
| LogEntryAppended (EventContext node) !Term !LogIndex (LogEntry entry node)
| ReadOnlyQueryQueued (EventContext node) !LogIndex entry
| LastLogIndexChangedTo (EventContext node) !LogIndex
| LogEntryApplied (EventContext node) !Term !LogIndex entry
| MembershipChangeApplied (EventContext node) (ClusterConfiguration node)
| -- | A leader was asked for a membership change that its committed
-- configuration already satisfies, so it did nothing.
MembershipChangeAlreadySettled (EventContext node) node
| LastAppliedIndexIncreasedTo (EventContext node) !LogIndex
| SnapshotApplied (EventContext node) !SnapshotMetadata
| GracefulShutdown (EventContext node)
| -- | The following event is emitted by the fault injector
-- in deterministic simulation tests. Users should not rely
-- on tracing this event in production.
Crashed node
deriving (Eq, Show)