Skip to content

A peer-to-peer stack for code collaboration

daemon: Extract machine store

Thomas Scholtes68cadf0db6d0
diff --git a/exe/Daemon.hs b/exe/Daemon.hs
index 88de77c..89f38da 100644
--- a/exe/Daemon.hs
+++ b/exe/Daemon.hs
@@ -18,11 +18,11 @@ import Options.Applicative
import Servant
import System.IO (BufferMode(..), hSetBuffering)
-import Radicle.Daemon.Common
import qualified Radicle.Daemon.HttpApi as Api
import Radicle.Daemon.Ipfs
import Radicle.Daemon.Logging
import Radicle.Daemon.MachineConfig
+import Radicle.Daemon.MachineStore
import Radicle.Daemon.Monad
import qualified Radicle.Ipfs as Ipfs
@@ -191,8 +191,8 @@ newMachine = do
id <- ipfs createMachine CouldNotCreateMachine
sub <- initDaemonSubscription id
logInfo "Created new IPFS machine" [("machine-id", getMachineId id)]
- m <- liftIO $ emptyMachine id Writer sub
- insertNewMachine id (Cached m)
+ m <- emptyMachine id Writer sub
+ insertMachine m
actAsWriter m
writeMachineConfig
pure (Api.NewResponse id)
@@ -268,8 +268,7 @@ send id (Api.SendRequest expressions) = do
-- into memory and start following its changes.
ensureMachineLoaded :: MachineId -> Daemon Machine
ensureMachineLoaded id = do
- msCMap <- asks machines
- m_ <- liftIO $ CMap.lookup id (getMachines msCMap)
+ m_ <- lookupMachine id
case m_ of
Nothing -> do
-- In this case we have not seen the machine before so we act as
@@ -309,9 +308,9 @@ loadMachine :: ReaderOrWriter -> MachineId -> Daemon Machine
loadMachine mode id = do
(idx, is) <- machineIpfs id $ machineInputsFrom id Nothing
sub <- initDaemonSubscription id
- m <- liftIO $ emptyMachine id mode sub
+ m <- emptyMachine id mode sub
(m', _) <- addInputs is idx m
- insertNewMachine id (Cached m')
+ insertMachine m'
pure m'
-- | Add inputs to a machine and update its state
@@ -353,42 +352,6 @@ bumpPolling id = do
modifyMachine id $ \m -> pure (m { machinePolling = highFreq }, () )
logDebug "Reset to high-frequency polling" [("machine-id", getMachineId id)]
--- | Insert a new machine into the cache.
-insertNewMachine :: MachineId -> CachedMachine -> Daemon ()
-insertNewMachine id m = do
- msCMap <- asks machines
- liftIO $ CMap.insert id m (getMachines msCMap)
-
--- | Modify a machine that is already in the cache. Errors if the
--- machine isn't in the cache already.
-modifyMachine :: forall a. MachineId -> (Machine -> Daemon (Machine, a)) -> Daemon a
-modifyMachine id f = do
- CachedMachines machineMap <- asks machines
- res <- CMap.modifyExistingValue id machineMap modCached
- case res of
- Nothing -> throwError (MachineError id MachineNotCached)
- Just a -> pure a
- where
- modCached :: CachedMachine -> Daemon (CachedMachine, a)
- modCached = \case
- UninitialisedReader _ -> throwError $ MachineError id MachineNotCached
- Cached m -> do
- (m', a) <- f m
- pure (Cached m', a)
-
-
-emptyMachine :: MachineId -> ReaderOrWriter -> TopicSubscription -> IO Machine
-emptyMachine id mode sub = do
- t <- Time.getSystemTime
- pure Machine{ machineId = id
- , machineState = pureEnv
- , machineLastIndex = emptyMachineEntryIndex
- , machineMode = mode
- , machineSubscription = sub
- , machineLastUpdated = t
- , machinePolling = highFreq
- }
-
-- ** Reader
-- | Load a machine in /reader mode/ and return it.
@@ -421,8 +384,7 @@ initReaderNoFail :: MachineId -> Daemon ()
initReaderNoFail id = catchError (initAsReader id $> ()) $ \err -> do
let (msg, infos) = displayError err
logError' "Could not initiate reader-mode machine on startup" $ ("init-error", msg) : infos
- t <- liftIO Time.getSystemTime
- insertNewMachine id (UninitialisedReader t)
+ insertUninitializedReader id
-- | Updates a (already initialised) machine in the cache with new inputs pulled
-- from IPFS.
@@ -503,11 +465,7 @@ writeInputs id inputs nonce = do
-- | Fetch and apply new inputs for all machines in reader mode.
poll :: Daemon ()
-poll = do
- msVar <- asks machines
- ms <- liftIO $ CMap.nonAtomicRead (getMachines msVar)
- _ <- Map.traverseWithKey pollMachine ms
- pure ()
+poll = traverseMachines pollMachine
where
pollMachine :: MachineId -> CachedMachine -> Daemon ()
pollMachine id = \case
@@ -578,8 +536,3 @@ lowFreqPollPeriod = 10 * 1000
-- a writer: 8 seconds.
ackWaitTime :: Milliseconds
ackWaitTime = 8 * 1000
-
--- | The amount of time a machine does high-frequency polling for
--- before it returns to low-frequency polling: 10 minutes.
-highFreq :: Polling
-highFreq = HighFreq (10 * 60 * 1000)
diff --git a/src/Radicle/Daemon/Common.hs b/src/Radicle/Daemon/Common.hs
deleted file mode 100644
index 3f65ad3..0000000
--- a/src/Radicle/Daemon/Common.hs
+++ /dev/null
@@ -1,52 +0,0 @@
-module Radicle.Daemon.Common
- ( ReaderOrWriter(..)
- , Polling(..)
- , Machine(..)
- , CachedMachine(..)
- , CachedMachines(..)
- ) where
-
-import Protolude hiding (log)
-
-import qualified Data.Aeson as A
-import qualified Data.Time.Clock.System as Time
-
-import Radicle
-import Radicle.Daemon.Ipfs
-import qualified Radicle.Internal.ConcurrentMap as CMap
-
--- | Indicates if the daemon is the writer or a reader for this machine.
-data ReaderOrWriter = Reader | Writer
- deriving (Generic)
-
-data Polling
- -- | Indicates that the daemon is performing high-frequency polling for this
- -- machine. Includes the number of milliseconds for how long this will
- -- continue being the case.
- = HighFreq Int64
- -- | Indicates that the daemon is only performing low-frequency polling for
- -- this machine.
- | LowFreq
-
-instance A.ToJSON ReaderOrWriter
-instance A.FromJSON ReaderOrWriter
-
--- A Cached machine with an initialised state and pubsub subscription.
-data Machine = Machine
- { machineId :: MachineId
- , machineState :: Bindings (PrimFns Identity)
- , machineLastIndex :: MachineEntryIndex
- , machineMode :: ReaderOrWriter
- , machineSubscription :: TopicSubscription
- , machineLastUpdated :: Time.SystemTime
- , machinePolling :: Polling
- } deriving (Generic)
-
-data CachedMachine
- -- | A reader which could not be initialised (probably due to IPNS name not
- -- resolving). Includes the time at which the last failed init was attempted.
- = UninitialisedReader Time.SystemTime
- -- | A cached machine with an active pubsub subscription.
- | Cached Machine
-
-newtype CachedMachines = CachedMachines { getMachines :: CMap.CMap MachineId CachedMachine }
diff --git a/src/Radicle/Daemon/MachineConfig.hs b/src/Radicle/Daemon/MachineConfig.hs
index 610c820..b89076a 100644
--- a/src/Radicle/Daemon/MachineConfig.hs
+++ b/src/Radicle/Daemon/MachineConfig.hs
@@ -14,8 +14,8 @@ import qualified Data.Aeson as A
import qualified Data.Map.Strict as Map
import System.Directory (doesFileExist)
-import Radicle.Daemon.Common
import Radicle.Daemon.Ipfs
+import Radicle.Daemon.MachineStore
import Radicle.Daemon.Monad
import qualified Radicle.Internal.ConcurrentMap as CMap
diff --git a/src/Radicle/Daemon/MachineStore.hs b/src/Radicle/Daemon/MachineStore.hs
new file mode 100644
index 0000000..39b6327
--- /dev/null
+++ b/src/Radicle/Daemon/MachineStore.hs
@@ -0,0 +1,137 @@
+-- | This module defines a 'Machine' as the in-memory representation of
+-- a Radicle State Machine and a 'MonadMachineStore' class that
+-- provides access to a set of machines in memory.
+module Radicle.Daemon.MachineStore
+ ( Machine(..)
+ , ReaderOrWriter(..)
+
+ , Polling(..)
+ , highFreq
+
+ , CachedMachine(..)
+ , CachedMachines(..)
+ , MonadMachineStore(..)
+ , modifyMachine
+ , insertMachine
+ , insertUninitializedReader
+ , emptyMachine
+ , traverseMachines
+ , lookupMachine
+ ) where
+
+import Protolude
+
+import Control.Monad.IO.Unlift
+import qualified Data.Aeson as A
+import qualified Data.Map.Strict as Map
+import qualified Data.Time.Clock.System as Time
+
+import Radicle
+import Radicle.Daemon.Error
+import Radicle.Daemon.Ipfs
+import qualified Radicle.Internal.ConcurrentMap as CMap
+
+-- | Indicates if the daemon is the writer or a reader for this machine.
+data ReaderOrWriter = Reader | Writer
+ deriving (Generic)
+
+instance A.ToJSON ReaderOrWriter
+instance A.FromJSON ReaderOrWriter
+
+
+data Polling
+ -- | Indicates that the daemon is performing high-frequency polling for this
+ -- machine. Includes the number of milliseconds for how long this will
+ -- continue being the case.
+ = HighFreq Int64
+ -- | Indicates that the daemon is only performing low-frequency polling for
+ -- this machine.
+ | LowFreq
+
+-- | The amount of time a machine does high-frequency polling for
+-- before it returns to low-frequency polling: 10 minutes.
+highFreq :: Polling
+highFreq = HighFreq (10 * 60 * 1000)
+
+
+-- A Cached machine with an initialised state and pubsub subscription.
+data Machine = Machine
+ { machineId :: MachineId
+ , machineState :: Bindings (PrimFns Identity)
+ , machineLastIndex :: MachineEntryIndex
+ , machineMode :: ReaderOrWriter
+ , machineSubscription :: TopicSubscription
+ , machineLastUpdated :: Time.SystemTime
+ , machinePolling :: Polling
+ } deriving (Generic)
+
+data CachedMachine
+ -- | A reader which could not be initialised (probably due to IPNS name not
+ -- resolving). Includes the time at which the last failed init was attempted.
+ = UninitialisedReader Time.SystemTime
+ -- | A cached machine with an active pubsub subscription.
+ | Cached Machine
+
+newtype CachedMachines = CachedMachines { getMachines :: CMap.CMap MachineId CachedMachine }
+
+-- * Machine access and modification
+
+class (MonadUnliftIO m, MonadError Error m) => MonadMachineStore m where
+ askMachines :: m CachedMachines
+
+
+-- | Modify a machine that is already in the store. Errors if the
+-- machine isn't in the store or is 'UninitializedReader'. Concurrent
+-- access to the same machine is serialized.
+modifyMachine :: forall a m. (MonadMachineStore m) => MachineId -> (Machine -> m (Machine, a)) -> m a
+modifyMachine id f = do
+ machines <- askMachines
+ res <- CMap.modifyExistingValue id (getMachines machines) modCached
+ case res of
+ Nothing -> throwError (MachineError id MachineNotCached)
+ Just a -> pure a
+ where
+ modCached :: CachedMachine -> m (CachedMachine, a)
+ modCached = \case
+ UninitialisedReader _ -> throwError $ MachineError id MachineNotCached
+ Cached m -> do
+ (m', a) <- f m
+ pure (Cached m', a)
+
+emptyMachine :: (MonadMachineStore m) => MachineId -> ReaderOrWriter -> TopicSubscription -> m Machine
+emptyMachine id mode sub = liftIO $ do
+ t <- Time.getSystemTime
+ pure Machine{ machineId = id
+ , machineState = pureEnv
+ , machineLastIndex = emptyMachineEntryIndex
+ , machineMode = mode
+ , machineSubscription = sub
+ , machineLastUpdated = t
+ , machinePolling = highFreq
+ }
+
+insertMachine :: (MonadMachineStore m) => Machine -> m ()
+insertMachine m = insertCachedMachine (machineId m) (Cached m)
+
+-- | Register a machine as a new 'UninitailizedReader' in the store.
+insertUninitializedReader :: (MonadMachineStore m) => MachineId -> m ()
+insertUninitializedReader id = do
+ t <- liftIO Time.getSystemTime
+ insertCachedMachine id (UninitialisedReader t)
+
+-- | Run @f@ sequentially for every machine in the store.
+traverseMachines :: (MonadMachineStore m) => (MachineId -> CachedMachine -> m b) -> m ()
+traverseMachines f = do
+ msVar <- askMachines
+ ms <- liftIO $ CMap.nonAtomicRead (getMachines msVar)
+ void $ Map.traverseWithKey f ms
+
+lookupMachine :: (MonadMachineStore m) => MachineId -> m (Maybe CachedMachine)
+lookupMachine id = do
+ msCMap <- askMachines
+ liftIO $ CMap.lookup id (getMachines msCMap)
+
+insertCachedMachine :: (MonadMachineStore m) => MachineId -> CachedMachine -> m ()
+insertCachedMachine id m = do
+ msCMap <- askMachines
+ liftIO $ CMap.insert id m (getMachines msCMap)
diff --git a/src/Radicle/Daemon/Monad.hs b/src/Radicle/Daemon/Monad.hs
index ced7d07..c4e50f6 100644
--- a/src/Radicle/Daemon/Monad.hs
+++ b/src/Radicle/Daemon/Monad.hs
@@ -21,9 +21,9 @@ import Control.Exception.Safe
import Control.Monad.Except
import Control.Monad.IO.Unlift
-import Radicle.Daemon.Common
import Radicle.Daemon.Error
import Radicle.Daemon.Logging
+import Radicle.Daemon.MachineStore
newtype Daemon a = Daemon (ExceptT Error (ReaderT Env IO) a)
@@ -48,6 +48,9 @@ liftExceptT makeError action = Daemon $ mapExceptT (lift . fmap (first makeError
instance MonadLog Daemon where
askLogLevel = asks logLevel
+instance MonadMachineStore Daemon where
+ askMachines = asks machines
+
-- * Environment
type FileLock = MVar ()