2019-07-16 03:01:45 +03:00
|
|
|
{-# OPTIONS_GHC -Wwarn #-}
|
|
|
|
|
2019-05-30 23:19:26 +03:00
|
|
|
module Vere.Pier where
|
|
|
|
|
2019-07-16 03:01:45 +03:00
|
|
|
import UrbitPrelude
|
2019-06-19 01:38:24 +03:00
|
|
|
|
2019-05-30 23:19:26 +03:00
|
|
|
import Vere.Pier.Types
|
2019-06-19 01:38:24 +03:00
|
|
|
|
2019-07-16 03:01:45 +03:00
|
|
|
import qualified System.Entropy as Ent
|
|
|
|
import qualified Vere.Log as Log
|
2019-07-16 05:20:23 +03:00
|
|
|
import qualified Vere.Persist as Persist
|
2019-07-16 03:01:45 +03:00
|
|
|
import qualified Vere.Serf as Serf
|
2019-05-30 23:19:26 +03:00
|
|
|
|
2019-07-12 22:18:14 +03:00
|
|
|
import Vere.Serf (EventId, Serf)
|
2019-06-18 02:47:20 +03:00
|
|
|
|
|
|
|
|
2019-06-25 04:10:41 +03:00
|
|
|
--------------------------------------------------------------------------------
|
2019-06-18 02:47:20 +03:00
|
|
|
|
2019-06-25 04:10:41 +03:00
|
|
|
ioDrivers = [] :: [IODriver]
|
2019-06-18 02:47:20 +03:00
|
|
|
|
|
|
|
|
2019-07-16 03:01:45 +03:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
genEntropy :: IO Word512
|
|
|
|
genEntropy = fromIntegral . view (from atomBytes) <$> Ent.getEntropy 64
|
|
|
|
|
|
|
|
generateBootSeq :: Ship -> Pill -> IO BootSeq
|
|
|
|
generateBootSeq ship Pill{..} = do
|
|
|
|
ent <- genEntropy
|
|
|
|
let ovums = preKern ent <> pKernelOvums <> pUserspaceOvums
|
|
|
|
pure $ BootSeq ident pBootFormulas ovums
|
|
|
|
where
|
|
|
|
ident = LogIdentity ship True (fromIntegral $ length pBootFormulas)
|
|
|
|
preKern ent = [ Ovum (Path ["", "term", "1"]) (Boot $ Fake $ who ident)
|
|
|
|
, Ovum (Path ["", "arvo"]) (Whom ship)
|
|
|
|
, Ovum (Path ["", "arvo"]) (Wack ent)
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2019-06-25 04:10:41 +03:00
|
|
|
--------------------------------------------------------------------------------
|
2019-06-19 01:38:24 +03:00
|
|
|
|
2019-06-25 04:10:41 +03:00
|
|
|
-- This is called to make a freshly booted pier. It assigns an identity to an
|
|
|
|
-- event log and takes a chill pill.
|
2019-07-16 05:20:23 +03:00
|
|
|
boot :: FilePath -> FilePath -> Ship -> IO (Serf, EventLog, EventId, Mug)
|
|
|
|
boot pillPath top ship = do
|
|
|
|
let logPath = top <> "/.urb/log"
|
2019-05-30 23:19:26 +03:00
|
|
|
|
2019-07-16 05:20:23 +03:00
|
|
|
pill <- loadFile @Pill pillPath >>= \case
|
|
|
|
Left l -> error (show l)
|
|
|
|
Right p -> pure p
|
|
|
|
|
|
|
|
|
|
|
|
seq@(BootSeq ident _ _) <- generateBootSeq ship pill
|
|
|
|
|
|
|
|
Log.wipeEvents logPath
|
2019-06-25 04:10:41 +03:00
|
|
|
log <- Log.open logPath
|
2019-05-30 23:19:26 +03:00
|
|
|
|
2019-07-16 05:20:23 +03:00
|
|
|
Log.writeIdent log ident
|
2019-05-30 23:19:26 +03:00
|
|
|
|
2019-07-17 02:14:46 +03:00
|
|
|
serf <- Serf.startSerfProcess top
|
|
|
|
(events, serfSt) <- Serf.bootFromSeq serf seq
|
2019-07-16 05:20:23 +03:00
|
|
|
|
2019-07-17 02:14:46 +03:00
|
|
|
Serf.requestSnapshot serf serfSt
|
|
|
|
Serf.shutdownAndKill serf 0
|
2019-07-16 05:20:23 +03:00
|
|
|
|
|
|
|
Persist.writeEvents log events
|
|
|
|
|
|
|
|
(eId, atom) : _ <- evaluate (reverse events)
|
|
|
|
Just (mug, _::Noun) <- evaluate (atom ^? atomBytes . _Cue >>= fromNoun)
|
2019-06-18 02:47:20 +03:00
|
|
|
|
2019-07-16 05:20:23 +03:00
|
|
|
pure (serf, log, eId, mug)
|
2019-06-19 01:38:24 +03:00
|
|
|
|
2019-06-25 04:10:41 +03:00
|
|
|
{-
|
|
|
|
What we really want to do is write the log identity and then do
|
|
|
|
normal startup, but writeIdent requires a full log state
|
|
|
|
including input/output queues.
|
|
|
|
-}
|
|
|
|
resume :: FilePath -> IO (Serf, EventLog, EventId, Mug)
|
|
|
|
resume top = do
|
|
|
|
log <- Log.open (top <> "/.urb/log")
|
|
|
|
ident <- Log.readIdent log
|
|
|
|
lastEv <- Log.latestEventNumber log
|
|
|
|
serf <- Serf.startSerfProcess top
|
|
|
|
(e, m) <- Serf.replay serf ident lastEv (Log.readEvents log)
|
2019-06-19 01:38:24 +03:00
|
|
|
|
2019-06-25 04:10:41 +03:00
|
|
|
pure (serf, log, e, m)
|
2019-06-19 01:38:24 +03:00
|
|
|
|
2019-06-29 04:46:33 +03:00
|
|
|
|
|
|
|
-- Run Pier --------------------------------------------------------------------
|
|
|
|
|
2019-06-25 04:10:41 +03:00
|
|
|
{-
|
|
|
|
performCommonPierStartup :: Serf.Serf
|
2019-06-19 03:04:57 +03:00
|
|
|
-> TQueue Ovum
|
2019-06-19 01:38:24 +03:00
|
|
|
-> TQueue (Writ [Eff])
|
|
|
|
-> TQueue (Writ [Eff])
|
|
|
|
-> LogState
|
|
|
|
-> IO Pier
|
2019-06-25 04:10:41 +03:00
|
|
|
performCommonPierStartup serf computeQ persistQ releaseQ logState = do
|
2019-06-19 01:38:24 +03:00
|
|
|
for ioDrivers $ \x -> do
|
|
|
|
bootMessage <- bornEvent x
|
2019-06-25 04:10:41 +03:00
|
|
|
atomically $ writeTQueue computeQ bootMessage
|
2019-06-01 01:55:21 +03:00
|
|
|
|
2019-06-19 01:38:24 +03:00
|
|
|
driverThreads <- for ioDrivers $ \x -> do
|
2019-06-25 04:10:41 +03:00
|
|
|
startDriver x (writeTQueue computeQ)
|
2019-06-19 01:38:24 +03:00
|
|
|
|
|
|
|
-- TODO: Don't do a bunch of extra work; we send all events to all drivers
|
|
|
|
portingThread <- async $ do
|
|
|
|
forever $ do
|
2019-06-25 04:10:41 +03:00
|
|
|
r <- atomically (readTQueue releaseQ)
|
2019-06-19 01:38:24 +03:00
|
|
|
for_ driverThreads $ \(_, k) ->
|
|
|
|
for_ (payload r) $ \eff ->
|
|
|
|
k eff
|
|
|
|
|
2019-06-25 04:10:41 +03:00
|
|
|
Serf.workerThread serf (readTQueue computeQ) undefined
|
2019-06-19 01:38:24 +03:00
|
|
|
|
|
|
|
pure (Pier{..})
|
2019-06-25 04:10:41 +03:00
|
|
|
-}
|