{-# LANGUAGE ImportQualifiedPost #-}
module Control.Distributed.Process.Raft.Admin
( -- * Interface
withSession,
AdminSession,
-- ** Session information
Raft.Admin.sessionId,
Raft.Admin.lowestUnacknowledgedSequenceNum,
Raft.Admin.setLeader,
-- ** Creating commands
leaveCluster,
joinCluster,
getClusterConfiguration,
shutDown,
-- ** Sending commands
sendRequest,
SessionError (..),
-- * Other exports
Microseconds,
Raft.Admin.AdminCommand (..),
AdminCommandResult (..),
)
where
import Control.Concurrent.STM (atomically, newTQueueIO, readTQueue, writeTQueue)
import Control.Distributed.Process
( NodeId,
Process,
exit,
getSelfNode,
link,
match,
matchUnknown,
nsendRemote,
receiveWait,
register,
spawnLocal,
)
import Control.Distributed.Process.Raft.Instances ()
import Control.Monad (forever)
import Control.Monad.IO.Class (liftIO)
import Distributed.Consensus.Raft.Admin
( AdminCommandResult,
AdminImplementation (..),
AdminRequest,
AdminSession,
Microseconds,
SessionError (..),
)
import Distributed.Consensus.Raft.Admin qualified as Raft.Admin
-- | Open an admin session, and run an action with a handle to this session.
--
-- It is perfectly safe, and encouraged, to send separate requests in
-- separate threads. However, you should call 'withSession' only once.
withSession ::
-- | Some member of the Raft cluster
NodeId ->
(AdminSession NodeId Process -> Process b) ->
Process b
withSession clusterMember f = do
recvQueue <- liftIO newTQueueIO
mailPid <-
spawnLocal $
forever $
receiveWait
[ match $ liftIO . atomically . writeTQueue recvQueue,
matchUnknown $ pure ()
]
link mailPid
register adminMailboxProcessName mailPid
thisNodeId <- getSelfNode
Raft.Admin.withSession thisNodeId clusterMember (impl recvQueue) f <* exit mailPid "Completed"
where
impl recvQueue =
AdminImplementation
{ sendAdminRequest = (`nsendRemote` adminMailboxProcessName),
receiveAdminResponse = liftIO $ atomically $ readTQueue recvQueue,
generateAdminSessionId = Raft.Admin.randomSessionId
}
-- | This value needs to be kept in sync with 'Control.Distributed.Process.Raft'.
-- The alternative would be a tiny shared library...
adminMailboxProcessName :: String
adminMailboxProcessName = "raft-consensus-admin-mailbox"
-- | Send an admin request to a Raft cluster, and wait for its result.
--
-- See 'Raft.Admin.sendRequest' for details.
sendRequest ::
AdminSession NodeId Process ->
-- | Maximum time to wait for a response.
Microseconds ->
-- | Node to command. If @Nothing@, the command is sent to
-- the leader of the cluster
Maybe NodeId ->
AdminRequest NodeId ->
Process
( Either
SessionError
(AdminCommandResult NodeId)
)
sendRequest = Raft.Admin.sendRequest
-- | Command that a node join a Raft cluster.
--
-- This has no effect if the node is already a member of the
-- cluster.
joinCluster ::
AdminSession NodeId Process ->
-- | Node to join
NodeId ->
Process (AdminRequest NodeId)
joinCluster = Raft.Admin.joinCluster
-- | Command that a node leave the Raft cluster.
--
-- Note that this node will not shut down unless the 'shutDown'
-- command is also sent to it.
leaveCluster ::
AdminSession NodeId Process ->
Process (AdminRequest NodeId)
leaveCluster = Raft.Admin.leaveCluster
-- | Ask a node for the cluster configuration it has committed.
--
-- This is the only way for an admin to tell whether a 'joinCluster' or
-- 'leaveCluster' has actually taken effect.
getClusterConfiguration ::
AdminSession NodeId Process ->
Process (AdminRequest NodeId)
getClusterConfiguration = Raft.Admin.getClusterConfiguration
-- | Request that a node shuts down.
shutDown ::
AdminSession NodeId Process ->
Process (AdminRequest NodeId)
shutDown = Raft.Admin.shutDown