2019-07-16 03:01:45 +03:00
|
|
|
{-# OPTIONS_GHC -Wwarn #-}
|
|
|
|
|
2019-08-01 08:16:02 +03:00
|
|
|
module Vere.Pier (booted, resumed, pier, runPersist, runCompute) where
|
2019-05-30 23:19:26 +03:00
|
|
|
|
2019-07-16 03:01:45 +03:00
|
|
|
import UrbitPrelude
|
2019-07-24 04:34:16 +03:00
|
|
|
|
|
|
|
import Arvo
|
2019-05-30 23:19:26 +03:00
|
|
|
import Vere.Pier.Types
|
2019-08-01 08:48:08 +03:00
|
|
|
import System.Random
|
2019-07-19 03:52:53 +03:00
|
|
|
|
2019-07-21 04:29:39 +03:00
|
|
|
import System.Directory (createDirectoryIfMissing)
|
|
|
|
import System.Posix.Files (ownerModes, setFileMode)
|
2019-08-01 05:34:14 +03:00
|
|
|
import Vere.Ames (ames)
|
|
|
|
import Vere.Behn (behn)
|
2019-08-08 01:24:02 +03:00
|
|
|
import Vere.Http.Server (serv)
|
2019-07-21 04:29:39 +03:00
|
|
|
import Vere.Log (EventLog)
|
2019-08-01 05:34:14 +03:00
|
|
|
import Vere.Serf (Serf, SerfState(..), doJob)
|
2019-06-19 01:38:24 +03:00
|
|
|
|
2019-07-16 03:01:45 +03:00
|
|
|
import qualified System.Entropy as Ent
|
2019-07-21 22:56:18 +03:00
|
|
|
import qualified Urbit.Time as Time
|
2019-07-16 03:01:45 +03:00
|
|
|
import qualified Vere.Log as Log
|
|
|
|
import qualified Vere.Serf as Serf
|
2019-05-30 23:19:26 +03:00
|
|
|
|
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-07-21 22:56:18 +03:00
|
|
|
_ioDrivers = [] :: [IODriver]
|
2019-06-18 02:47:20 +03:00
|
|
|
|
2019-08-15 05:42:48 +03:00
|
|
|
setupPierDirectory :: FilePath -> IO ()
|
|
|
|
setupPierDirectory shipPath = do
|
2019-07-21 04:29:39 +03:00
|
|
|
for_ ["put", "get", "log", "chk"] $ \seg -> do
|
|
|
|
let pax = shipPath <> "/.urb/" <> seg
|
|
|
|
createDirectoryIfMissing True pax
|
|
|
|
setFileMode pax ownerModes
|
|
|
|
|
2019-06-18 02:47:20 +03:00
|
|
|
|
2019-07-21 22:56:18 +03:00
|
|
|
-- Load pill into boot sequence. -----------------------------------------------
|
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)
|
2019-08-14 03:52:59 +03:00
|
|
|
preKern ent = [ EvBlip $ BlipEvTerm $ TermEvBoot (1,()) (Fake (who ident))
|
|
|
|
, EvBlip $ BlipEvArvo $ ArvoEvWhom () ship
|
|
|
|
, EvBlip $ BlipEvArvo $ ArvoEvWack () ent
|
2019-07-16 03:01:45 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2019-07-21 22:56:18 +03:00
|
|
|
-- Write a batch of jobs into the event log ------------------------------------
|
2019-07-19 03:52:53 +03:00
|
|
|
|
|
|
|
writeJobs :: EventLog -> Vector Job -> IO ()
|
|
|
|
writeJobs log !jobs = do
|
|
|
|
expect <- Log.nextEv log
|
|
|
|
events <- fmap fromList $ traverse fromJob (zip [expect..] $ toList jobs)
|
|
|
|
Log.appendEvents log events
|
2019-07-17 08:32:36 +03:00
|
|
|
where
|
2019-07-21 22:56:18 +03:00
|
|
|
fromJob :: (EventId, Job) -> IO ByteString
|
2019-07-21 04:29:39 +03:00
|
|
|
fromJob (expectedId, job) = do
|
|
|
|
guard (expectedId == jobId job)
|
2019-07-21 22:56:18 +03:00
|
|
|
pure $ jamBS $ jobPayload job
|
2019-07-21 04:29:39 +03:00
|
|
|
|
|
|
|
jobPayload :: Job -> Noun
|
|
|
|
jobPayload (RunNok (LifeCyc _ m n)) = toNoun (m, n)
|
|
|
|
jobPayload (DoWork (Work _ m d o)) = toNoun (m, d, o)
|
2019-07-19 03:52:53 +03:00
|
|
|
|
2019-06-29 04:46:33 +03:00
|
|
|
|
2019-07-21 22:56:18 +03:00
|
|
|
-- Boot a new ship. ------------------------------------------------------------
|
|
|
|
|
2019-07-22 00:24:07 +03:00
|
|
|
booted :: FilePath -> FilePath -> Serf.Flags -> Ship
|
|
|
|
-> Acquire (Serf, EventLog, SerfState)
|
2019-08-14 03:52:59 +03:00
|
|
|
booted pillPath pierPath flags ship = do
|
2019-08-15 05:42:48 +03:00
|
|
|
putStrLn "LOADING PILL"
|
|
|
|
|
2019-07-21 22:56:18 +03:00
|
|
|
pill <- liftIO $ loadFile @Pill pillPath >>= \case
|
2019-08-14 03:52:59 +03:00
|
|
|
Left l -> error (show l) -- TODO Throw a real exception.
|
2019-07-21 22:56:18 +03:00
|
|
|
Right p -> pure p
|
|
|
|
|
2019-08-15 05:42:48 +03:00
|
|
|
putStrLn "PILL LOADED"
|
|
|
|
|
2019-07-21 22:56:18 +03:00
|
|
|
seq@(BootSeq ident x y) <- liftIO $ generateBootSeq ship pill
|
|
|
|
|
2019-08-15 05:42:48 +03:00
|
|
|
putStrLn "BootSeq Computed"
|
|
|
|
|
|
|
|
liftIO (setupPierDirectory pierPath)
|
|
|
|
|
|
|
|
putStrLn "Directory Setup"
|
|
|
|
|
2019-08-14 03:52:59 +03:00
|
|
|
log <- Log.new (pierPath <> "/.urb/log") ident
|
2019-08-15 05:42:48 +03:00
|
|
|
|
|
|
|
putStrLn "Event Log Initialized"
|
|
|
|
|
2019-08-14 03:52:59 +03:00
|
|
|
serf <- Serf.run (Serf.Config pierPath flags)
|
2019-07-21 22:56:18 +03:00
|
|
|
|
2019-08-15 05:42:48 +03:00
|
|
|
putStrLn "Serf Started"
|
|
|
|
|
2019-07-21 22:56:18 +03:00
|
|
|
liftIO $ do
|
|
|
|
(events, serfSt) <- Serf.bootFromSeq serf seq
|
2019-08-15 05:42:48 +03:00
|
|
|
putStrLn "Boot Sequence completed"
|
2019-07-21 22:56:18 +03:00
|
|
|
Serf.snapshot serf serfSt
|
2019-08-15 05:42:48 +03:00
|
|
|
putStrLn "Snapshot taken"
|
2019-07-21 22:56:18 +03:00
|
|
|
writeJobs log (fromList events)
|
2019-08-15 05:42:48 +03:00
|
|
|
putStrLn "Events written"
|
2019-07-21 22:56:18 +03:00
|
|
|
pure (serf, log, serfSt)
|
|
|
|
|
|
|
|
|
|
|
|
-- Resume an existing ship. ----------------------------------------------------
|
|
|
|
|
2019-08-14 03:52:59 +03:00
|
|
|
resumed :: FilePath -> Serf.Flags
|
|
|
|
-> Acquire (Serf, EventLog, SerfState)
|
2019-07-22 00:24:07 +03:00
|
|
|
resumed top flags = do
|
2019-07-21 22:56:18 +03:00
|
|
|
log <- Log.existing (top <> "/.urb/log")
|
2019-07-22 00:24:07 +03:00
|
|
|
serf <- Serf.run (Serf.Config top flags)
|
2019-07-21 22:56:18 +03:00
|
|
|
serfSt <- liftIO (Serf.replay serf log)
|
|
|
|
|
|
|
|
liftIO (Serf.snapshot serf serfSt)
|
|
|
|
|
|
|
|
pure (serf, log, serfSt)
|
|
|
|
|
|
|
|
|
2019-06-29 04:46:33 +03:00
|
|
|
-- Run Pier --------------------------------------------------------------------
|
|
|
|
|
2019-08-08 01:24:02 +03:00
|
|
|
pier :: FilePath
|
|
|
|
-> Maybe Port
|
2019-08-01 05:34:14 +03:00
|
|
|
-> (Serf, EventLog, SerfState)
|
2019-08-01 08:16:02 +03:00
|
|
|
-> Acquire ()
|
2019-08-08 01:24:02 +03:00
|
|
|
pier pierPath mPort (serf, log, ss) = do
|
2019-08-01 05:34:14 +03:00
|
|
|
computeQ <- newTQueueIO :: Acquire (TQueue Ev)
|
|
|
|
persistQ <- newTQueueIO :: Acquire (TQueue (Job, FX))
|
|
|
|
executeQ <- newTQueueIO :: Acquire (TQueue FX)
|
2019-06-01 01:55:21 +03:00
|
|
|
|
2019-08-13 08:56:31 +03:00
|
|
|
inst <- liftIO (KingId . UV . fromIntegral <$> randomIO @Word16)
|
2019-08-01 08:48:08 +03:00
|
|
|
|
|
|
|
let ship = who (Log.identity log)
|
2019-06-19 01:38:24 +03:00
|
|
|
|
2019-08-01 05:34:14 +03:00
|
|
|
let (bootEvents, startDrivers) =
|
2019-08-08 01:24:02 +03:00
|
|
|
drivers pierPath inst ship mPort (writeTQueue computeQ)
|
2019-06-19 01:38:24 +03:00
|
|
|
|
2019-08-01 05:34:14 +03:00
|
|
|
liftIO $ atomically $ for_ bootEvents (writeTQueue computeQ)
|
2019-06-19 01:38:24 +03:00
|
|
|
|
2019-08-01 08:16:02 +03:00
|
|
|
tExe <- startDrivers >>= router (readTQueue executeQ)
|
2019-08-01 05:34:14 +03:00
|
|
|
tDisk <- runPersist log persistQ (writeTQueue executeQ)
|
|
|
|
tCpu <- runCompute serf ss (readTQueue computeQ) (writeTQueue persistQ)
|
2019-07-20 06:00:23 +03:00
|
|
|
|
2019-08-01 08:16:02 +03:00
|
|
|
-- Wait for something to die.
|
2019-07-20 06:00:23 +03:00
|
|
|
|
2019-08-01 08:16:02 +03:00
|
|
|
let ded = asum [ death "effect thread" tExe
|
|
|
|
, death "persist thread" tDisk
|
|
|
|
, death "compute thread" tCpu
|
|
|
|
]
|
|
|
|
|
|
|
|
atomically ded >>= \case
|
|
|
|
Left (txt, exn) -> print ("Somthing died", txt, exn)
|
|
|
|
Right tag -> print ("something simply exited", tag)
|
|
|
|
|
|
|
|
death :: Text -> Async () -> STM (Either (Text, SomeException) Text)
|
|
|
|
death tag tid = do
|
|
|
|
waitCatchSTM tid <&> \case
|
|
|
|
Left exn -> Left (tag, exn)
|
|
|
|
Right () -> Right tag
|
2019-07-21 22:56:18 +03:00
|
|
|
|
2019-08-01 05:34:14 +03:00
|
|
|
-- Start All Drivers -----------------------------------------------------------
|
2019-07-21 22:56:18 +03:00
|
|
|
|
2019-08-01 05:34:14 +03:00
|
|
|
data Drivers = Drivers
|
|
|
|
{ dAmes :: EffCb AmesEf
|
|
|
|
, dBehn :: EffCb BehnEf
|
|
|
|
, dHttpClient :: EffCb HttpClientEf
|
|
|
|
, dHttpServer :: EffCb HttpServerEf
|
|
|
|
, dNewt :: EffCb NewtEf
|
|
|
|
, dSync :: EffCb SyncEf
|
|
|
|
, dTerm :: EffCb TermEf
|
|
|
|
}
|
2019-07-21 22:56:18 +03:00
|
|
|
|
2019-08-08 01:24:02 +03:00
|
|
|
drivers :: FilePath
|
|
|
|
-> KingId
|
2019-08-01 05:34:14 +03:00
|
|
|
-> Ship
|
|
|
|
-> Maybe Port
|
|
|
|
-> (Ev -> STM ())
|
|
|
|
-> ([Ev], Acquire Drivers)
|
2019-08-08 01:24:02 +03:00
|
|
|
drivers pierPath inst who mPort plan =
|
2019-08-01 05:34:14 +03:00
|
|
|
(initialEvents, runDrivers)
|
|
|
|
where
|
|
|
|
(behnBorn, runBehn) = behn inst plan
|
|
|
|
(amesBorn, runAmes) = ames inst who mPort plan
|
2019-08-08 01:24:02 +03:00
|
|
|
(httpBorn, runHttp) = serv pierPath inst plan
|
|
|
|
initialEvents = mconcat [behnBorn, amesBorn, httpBorn]
|
2019-08-01 05:34:14 +03:00
|
|
|
runDrivers = do
|
|
|
|
dNewt <- runAmes
|
|
|
|
dBehn <- runBehn
|
2019-08-01 08:16:02 +03:00
|
|
|
dAmes <- pure $ const $ pure ()
|
|
|
|
dHttpClient <- pure $ const $ pure ()
|
2019-08-08 01:24:02 +03:00
|
|
|
dHttpServer <- runHttp
|
2019-08-01 08:16:02 +03:00
|
|
|
dSync <- pure $ const $ pure ()
|
|
|
|
dTerm <- pure $ const $ pure ()
|
2019-08-01 05:34:14 +03:00
|
|
|
pure (Drivers{..})
|
|
|
|
|
|
|
|
|
|
|
|
-- Route Effects to Drivers ----------------------------------------------------
|
|
|
|
|
|
|
|
router :: STM FX -> Drivers -> Acquire (Async ())
|
|
|
|
router waitFx Drivers{..} = mkAcquire start cancel
|
|
|
|
where
|
|
|
|
start = async $ forever $ do
|
|
|
|
fx <- atomically waitFx
|
2019-08-01 08:16:02 +03:00
|
|
|
for_ fx $ \ef -> do
|
|
|
|
putStrLn ("[EFFECT]\n" <> pack (ppShow ef) <> "\n\n")
|
|
|
|
case ef of
|
|
|
|
GoodParse (EfVega _ _) -> error "TODO"
|
|
|
|
GoodParse (EfExit _ _) -> error "TODO"
|
|
|
|
GoodParse (EfVane (VEAmes ef)) -> dAmes ef
|
|
|
|
GoodParse (EfVane (VEBehn ef)) -> dBehn ef
|
|
|
|
GoodParse (EfVane (VEBoat ef)) -> dSync ef
|
|
|
|
GoodParse (EfVane (VEClay ef)) -> dSync ef
|
|
|
|
GoodParse (EfVane (VEHttpClient ef)) -> dHttpClient ef
|
|
|
|
GoodParse (EfVane (VEHttpServer ef)) -> dHttpServer ef
|
|
|
|
GoodParse (EfVane (VENewt ef)) -> dNewt ef
|
|
|
|
GoodParse (EfVane (VESync ef)) -> dSync ef
|
|
|
|
GoodParse (EfVane (VETerm ef)) -> dTerm ef
|
|
|
|
FailParse n -> pPrint n
|
2019-07-21 22:56:18 +03:00
|
|
|
|
2019-08-01 05:34:14 +03:00
|
|
|
|
|
|
|
-- Compute Thread --------------------------------------------------------------
|
|
|
|
|
|
|
|
runCompute :: Serf -> SerfState -> STM Ev -> ((Job, FX) -> STM ())
|
|
|
|
-> Acquire (Async ())
|
|
|
|
runCompute serf ss getEvent putResult =
|
|
|
|
mkAcquire (async (go ss)) cancel
|
|
|
|
where
|
|
|
|
go :: SerfState -> IO ()
|
|
|
|
go ss = do
|
|
|
|
ev <- atomically getEvent
|
2019-08-01 08:16:02 +03:00
|
|
|
putStrLn ("[EVENT]\n" <> pack (ppShow ev) <> "\n\n")
|
2019-08-01 05:34:14 +03:00
|
|
|
wen <- Time.now
|
|
|
|
eId <- pure (ssNextEv ss)
|
|
|
|
mug <- pure (ssLastMug ss)
|
|
|
|
|
|
|
|
(job', ss', fx) <- doJob serf (DoWork (Work eId mug wen ev))
|
|
|
|
atomically (putResult (job', fx))
|
|
|
|
go ss'
|
2019-07-21 22:56:18 +03:00
|
|
|
|
|
|
|
|
2019-07-20 06:00:23 +03:00
|
|
|
-- Persist Thread --------------------------------------------------------------
|
|
|
|
|
|
|
|
data PersistExn = BadEventId EventId EventId
|
|
|
|
deriving Show
|
|
|
|
|
|
|
|
instance Exception PersistExn where
|
|
|
|
displayException (BadEventId expected got) =
|
|
|
|
unlines [ "Out-of-order event id send to persist thread."
|
|
|
|
, "\tExpected " <> show expected <> " but got " <> show got
|
|
|
|
]
|
|
|
|
|
|
|
|
runPersist :: EventLog
|
2019-08-01 05:34:14 +03:00
|
|
|
-> TQueue (Job, FX)
|
|
|
|
-> (FX -> STM ())
|
|
|
|
-> Acquire (Async ())
|
|
|
|
runPersist log inpQ out =
|
2019-07-20 06:00:23 +03:00
|
|
|
mkAcquire runThread cancelWait
|
|
|
|
where
|
|
|
|
cancelWait :: Async () -> IO ()
|
|
|
|
cancelWait tid = cancel tid >> wait tid
|
|
|
|
|
|
|
|
runThread :: IO (Async ())
|
|
|
|
runThread = asyncBound $ forever $ do
|
2019-07-21 22:56:18 +03:00
|
|
|
writs <- atomically getBatchFromQueue
|
2019-08-01 05:34:14 +03:00
|
|
|
events <- validateJobsAndGetBytes (toNullable writs)
|
2019-07-20 06:00:23 +03:00
|
|
|
Log.appendEvents log events
|
2019-08-01 05:34:14 +03:00
|
|
|
atomically $ for_ writs $ \(_,fx) -> out fx
|
2019-07-20 06:00:23 +03:00
|
|
|
|
2019-08-01 05:34:14 +03:00
|
|
|
validateJobsAndGetBytes :: [(Job, FX)] -> IO (Vector ByteString)
|
|
|
|
validateJobsAndGetBytes writs = do
|
2019-07-20 06:00:23 +03:00
|
|
|
expect <- Log.nextEv log
|
|
|
|
fmap fromList
|
|
|
|
$ for (zip [expect..] writs)
|
2019-08-01 05:34:14 +03:00
|
|
|
$ \(expectedId, (j, fx)) -> do
|
|
|
|
unless (expectedId == jobId j) $
|
|
|
|
throwIO (BadEventId expectedId (jobId j))
|
|
|
|
case j of
|
|
|
|
RunNok _ ->
|
|
|
|
error "This shouldn't happen here!"
|
|
|
|
DoWork (Work eId mug wen ev) ->
|
|
|
|
pure $ jamBS $ toNoun (mug, wen, ev)
|
|
|
|
|
|
|
|
getBatchFromQueue :: STM (NonNull [(Job, FX)])
|
2019-07-20 06:00:23 +03:00
|
|
|
getBatchFromQueue =
|
|
|
|
readTQueue inpQ >>= go . singleton
|
|
|
|
where
|
|
|
|
go acc =
|
|
|
|
tryReadTQueue inpQ >>= \case
|
|
|
|
Nothing -> pure (reverse acc)
|
|
|
|
Just item -> go (item <| acc)
|