2019-06-01 01:55:21 +03:00
|
|
|
module Vere.Worker where
|
|
|
|
|
|
|
|
import ClassyPrelude
|
|
|
|
import Control.Lens
|
|
|
|
import Data.Void
|
|
|
|
|
2019-06-01 03:21:44 +03:00
|
|
|
import System.Exit (ExitCode)
|
|
|
|
|
2019-06-01 01:55:21 +03:00
|
|
|
import Data.Noun
|
|
|
|
import Data.Noun.Atom
|
|
|
|
import Data.Noun.Jam
|
|
|
|
import Data.Noun.Poet
|
|
|
|
import Data.Noun.Pill
|
|
|
|
import Vere.Pier.Types
|
|
|
|
import System.Process
|
|
|
|
|
2019-06-19 03:04:57 +03:00
|
|
|
import qualified Urbit.Time as Time
|
|
|
|
|
2019-06-18 02:47:20 +03:00
|
|
|
import Data.ByteString (hGet)
|
|
|
|
import Data.ByteString.Unsafe (unsafeUseAsCString)
|
|
|
|
import Foreign.Ptr (castPtr)
|
|
|
|
import Foreign.Storable (peek)
|
|
|
|
|
|
|
|
import qualified Vere.Log as Log
|
|
|
|
|
2019-06-01 01:55:21 +03:00
|
|
|
data Worker = Worker
|
|
|
|
{ sendHandle :: Handle
|
|
|
|
, recvHandle :: Handle
|
|
|
|
, process :: ProcessHandle
|
|
|
|
|
|
|
|
-- , getInput :: STM (Writ ())
|
|
|
|
-- , onComputed :: Writ [Effect] -> STM ()
|
|
|
|
|
|
|
|
-- , onExit :: Worker -> IO ()
|
|
|
|
-- , task :: Async ()
|
|
|
|
}
|
|
|
|
|
2019-06-01 03:21:44 +03:00
|
|
|
|
2019-06-02 00:49:21 +03:00
|
|
|
|
2019-06-01 03:21:44 +03:00
|
|
|
--------------------------------------------------------------------------------
|
2019-06-01 01:55:21 +03:00
|
|
|
|
2019-06-02 00:49:21 +03:00
|
|
|
-- Think about how to handle process exit
|
|
|
|
-- Tear down subprocess on exit? (terminiteProcess)
|
2019-06-19 01:38:24 +03:00
|
|
|
startWorkerProcess :: IO Worker
|
|
|
|
startWorkerProcess =
|
2019-06-02 00:49:21 +03:00
|
|
|
do
|
|
|
|
(Just i, Just o, _, p) <- createProcess pSpec
|
2019-06-19 01:38:24 +03:00
|
|
|
pure (Worker i o p)
|
2019-06-02 00:49:21 +03:00
|
|
|
where
|
|
|
|
pSpec =
|
|
|
|
(proc "urbit-worker" []) { std_in = CreatePipe
|
|
|
|
, std_out = CreatePipe
|
|
|
|
}
|
2019-06-01 01:55:21 +03:00
|
|
|
|
2019-06-01 03:21:44 +03:00
|
|
|
kill :: Worker -> IO ExitCode
|
|
|
|
kill w = do
|
|
|
|
terminateProcess (process w)
|
|
|
|
waitForProcess (process w)
|
2019-06-01 01:55:21 +03:00
|
|
|
|
|
|
|
work :: Word64 -> Jam -> Atom
|
|
|
|
work id (Jam a) = jam $ toNoun (Cord "work", id, a)
|
|
|
|
|
2019-06-02 00:07:40 +03:00
|
|
|
newtype Job = Job Void
|
|
|
|
deriving newtype (Eq, Show, ToNoun, FromNoun)
|
2019-06-01 01:55:21 +03:00
|
|
|
|
|
|
|
type EventId = Word64
|
|
|
|
|
2019-06-01 03:21:44 +03:00
|
|
|
newtype Ship = Ship Word64 -- @p
|
2019-06-02 00:07:40 +03:00
|
|
|
deriving newtype (Eq, Ord, Show, ToNoun, FromNoun)
|
2019-06-01 01:55:21 +03:00
|
|
|
|
2019-06-02 00:07:40 +03:00
|
|
|
newtype ShipId = ShipId (Ship, Bool)
|
|
|
|
deriving newtype (Eq, Ord, Show, ToNoun, FromNoun)
|
2019-06-01 01:55:21 +03:00
|
|
|
|
2019-06-01 03:21:44 +03:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2019-06-02 00:07:40 +03:00
|
|
|
type Play = Nullable (EventId, Mug, ShipId)
|
2019-06-01 01:55:21 +03:00
|
|
|
|
|
|
|
data Plea
|
|
|
|
= Play Play
|
|
|
|
| Work EventId Mug Job
|
2019-06-19 03:04:57 +03:00
|
|
|
| Done EventId Mug [Eff]
|
2019-06-01 01:55:21 +03:00
|
|
|
| Stdr EventId Cord
|
|
|
|
| Slog EventId Word32 Tank
|
2019-06-01 03:21:44 +03:00
|
|
|
deriving (Eq, Show)
|
2019-06-01 01:55:21 +03:00
|
|
|
|
2019-06-02 00:07:40 +03:00
|
|
|
instance ToNoun Plea where
|
|
|
|
toNoun = \case
|
|
|
|
Play p -> toNoun (Cord "play", p)
|
|
|
|
Work i m j -> toNoun (Cord "work", i, m, j)
|
|
|
|
Done i m o -> toNoun (Cord "done", i, m, o)
|
|
|
|
Stdr i msg -> toNoun (Cord "stdr", i, msg)
|
|
|
|
Slog i p t -> toNoun (Cord "slog", i, p, t)
|
|
|
|
|
2019-06-01 03:21:44 +03:00
|
|
|
instance FromNoun Plea where
|
2019-06-02 00:07:40 +03:00
|
|
|
parseNoun n =
|
|
|
|
parseNoun n >>= \case
|
|
|
|
(Cord "play", p) -> parseNoun p <&> \p -> Play p
|
|
|
|
(Cord "work", w) -> parseNoun w <&> \(i, m, j) -> Work i m j
|
|
|
|
(Cord "done", d) -> parseNoun d <&> \(i, m, o) -> Done i m o
|
|
|
|
(Cord "stdr", r) -> parseNoun r <&> \(i, msg) -> Stdr i msg
|
|
|
|
(Cord "slog", s) -> parseNoun s <&> \(i, p, t) -> Slog i p t
|
|
|
|
(Cord tag , s) -> fail ("Invalid plea tag: " <> unpack (decodeUtf8 tag))
|
2019-06-01 03:21:44 +03:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
2019-06-01 01:55:21 +03:00
|
|
|
|
|
|
|
type CompletedEventId = Word64
|
|
|
|
type NextEventId = Word64
|
|
|
|
|
|
|
|
type WorkerState = (EventId, Mug)
|
|
|
|
|
2019-06-01 03:21:44 +03:00
|
|
|
type ReplacementEv = (EventId, Mug, Job)
|
2019-06-19 03:04:57 +03:00
|
|
|
type WorkResult = (EventId, Mug, [Eff])
|
2019-06-01 03:21:44 +03:00
|
|
|
type WorkerResp = (Either ReplacementEv WorkResult)
|
|
|
|
|
|
|
|
-- Exceptions ------------------------------------------------------------------
|
|
|
|
|
|
|
|
data WorkerExn
|
|
|
|
= BadComputeId EventId WorkResult
|
|
|
|
| BadReplacementId EventId ReplacementEv
|
|
|
|
| UnexpectedPlay EventId Play
|
|
|
|
| BadPleaAtom Atom
|
|
|
|
| BadPleaNoun Noun
|
2019-06-18 02:47:20 +03:00
|
|
|
| ReplacedEventDuringReplay EventId ReplacementEv
|
|
|
|
| WorkerConnectionClosed
|
2019-06-19 01:38:24 +03:00
|
|
|
| UnexpectedPleaOnNewShip Plea
|
|
|
|
| InvalidInitialPlea Plea
|
2019-06-01 03:21:44 +03:00
|
|
|
deriving (Show)
|
2019-06-01 01:55:21 +03:00
|
|
|
|
2019-06-01 03:21:44 +03:00
|
|
|
instance Exception WorkerExn
|
|
|
|
|
|
|
|
-- Utils -----------------------------------------------------------------------
|
2019-06-01 01:55:21 +03:00
|
|
|
|
|
|
|
printTank :: Word32 -> Tank -> IO ()
|
|
|
|
printTank pri t = print "tank"
|
|
|
|
|
2019-06-01 03:21:44 +03:00
|
|
|
guardExn :: Exception e => Bool -> e -> IO ()
|
|
|
|
guardExn ok = unless ok . throwIO
|
|
|
|
|
|
|
|
fromJustExn :: Exception e => Maybe a -> e -> IO a
|
|
|
|
fromJustExn Nothing exn = throwIO exn
|
|
|
|
fromJustExn (Just x) exn = pure x
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
2019-06-01 01:55:21 +03:00
|
|
|
|
2019-06-01 03:21:44 +03:00
|
|
|
sendAndRecv :: Worker -> EventId -> Atom -> IO WorkerResp
|
2019-06-01 01:55:21 +03:00
|
|
|
sendAndRecv w eventId event =
|
|
|
|
do
|
2019-06-01 03:21:44 +03:00
|
|
|
sendAtom w $ work eventId (Jam event)
|
2019-06-01 01:55:21 +03:00
|
|
|
loop
|
|
|
|
where
|
2019-06-01 03:21:44 +03:00
|
|
|
produce :: WorkResult -> IO WorkerResp
|
|
|
|
produce (i, m, o) = do
|
|
|
|
guardExn (i /= eventId) (BadComputeId eventId (i, m, o))
|
|
|
|
pure $ Right (i, m, o)
|
|
|
|
|
|
|
|
replace :: ReplacementEv -> IO WorkerResp
|
|
|
|
replace (i, m, j) = do
|
|
|
|
guardExn (i /= eventId) (BadReplacementId eventId (i, m, j))
|
2019-06-01 01:55:21 +03:00
|
|
|
pure (Left (i, m, j))
|
|
|
|
|
2019-06-01 03:21:44 +03:00
|
|
|
loop :: IO WorkerResp
|
|
|
|
loop = recvPlea w >>= \case
|
|
|
|
Play p -> throwIO (UnexpectedPlay eventId p)
|
|
|
|
Done i m o -> produce (i, m, o)
|
|
|
|
Work i m j -> replace (i, m, j)
|
|
|
|
Stdr _ cord -> print cord >> loop
|
|
|
|
Slog _ pri t -> printTank pri t >> loop
|
2019-06-01 01:55:21 +03:00
|
|
|
|
2019-06-19 01:38:24 +03:00
|
|
|
sendBootEvent :: LogIdentity -> Worker -> IO ()
|
|
|
|
sendBootEvent id w = do
|
|
|
|
sendAtom w $ jam $ toNoun (Cord "boot", id)
|
2019-06-18 02:47:20 +03:00
|
|
|
|
2019-06-01 01:55:21 +03:00
|
|
|
|
|
|
|
-- the ship is booted, but it is behind. shove events to the worker until it is
|
|
|
|
-- caught up.
|
2019-06-19 01:38:24 +03:00
|
|
|
replay :: Worker
|
|
|
|
-> WorkerState
|
|
|
|
-> LogIdentity
|
|
|
|
-> EventId
|
2019-06-01 01:55:21 +03:00
|
|
|
-> (EventId -> Word64 -> IO (Vector (EventId, Atom)))
|
2019-06-19 03:04:57 +03:00
|
|
|
-> IO (EventId, Mug)
|
2019-06-19 01:38:24 +03:00
|
|
|
replay w (wid, wmug) identity lastCommitedId getEvents = do
|
|
|
|
when (wid == 1) (sendBootEvent identity w)
|
2019-06-01 01:55:21 +03:00
|
|
|
|
2019-06-19 03:04:57 +03:00
|
|
|
vLast <- newIORef (wid, wmug)
|
|
|
|
loop vLast wid
|
|
|
|
readIORef vLast
|
2019-06-18 02:47:20 +03:00
|
|
|
where
|
|
|
|
-- Replay events in batches of 1000.
|
2019-06-19 03:04:57 +03:00
|
|
|
loop vLast curEvent = do
|
2019-06-18 02:47:20 +03:00
|
|
|
let toRead = min 1000 (1 + lastCommitedId - curEvent)
|
|
|
|
when (toRead > 0) do
|
|
|
|
events <- getEvents curEvent toRead
|
|
|
|
|
|
|
|
for_ events $ \(eventId, event) -> do
|
|
|
|
sendAndRecv w eventId event >>= \case
|
2019-06-19 03:04:57 +03:00
|
|
|
Left ev -> throwIO (ReplacedEventDuringReplay eventId ev)
|
|
|
|
Right (id, mug, _) -> writeIORef vLast (id, mug)
|
2019-06-01 01:55:21 +03:00
|
|
|
|
2019-06-19 03:04:57 +03:00
|
|
|
loop vLast (curEvent + toRead)
|
2019-06-01 01:55:21 +03:00
|
|
|
|
2019-06-19 01:38:24 +03:00
|
|
|
|
|
|
|
bootWorker :: Worker
|
|
|
|
-> LogIdentity
|
|
|
|
-> Pill
|
|
|
|
-> IO ()
|
|
|
|
bootWorker w identity pill =
|
|
|
|
do
|
|
|
|
recvPlea w >>= \case
|
|
|
|
Play Nil -> pure ()
|
|
|
|
x@(Play _) -> throwIO (UnexpectedPleaOnNewShip x)
|
|
|
|
x -> throwIO (InvalidInitialPlea x)
|
|
|
|
|
|
|
|
-- TODO: actually boot the pill
|
|
|
|
undefined
|
|
|
|
|
|
|
|
requestSnapshot w
|
|
|
|
|
|
|
|
-- Maybe return the current event id ? But we'll have to figure that out
|
|
|
|
-- later.
|
|
|
|
pure ()
|
|
|
|
|
|
|
|
resumeWorker :: Worker
|
|
|
|
-> LogIdentity
|
|
|
|
-> EventId
|
|
|
|
-> (EventId -> Word64 -> IO (Vector (EventId, Atom)))
|
2019-06-19 03:04:57 +03:00
|
|
|
-> IO (EventId, Mug)
|
2019-06-19 01:38:24 +03:00
|
|
|
resumeWorker w identity logLatestEventNumber eventFetcher =
|
2019-06-18 02:47:20 +03:00
|
|
|
do
|
|
|
|
ws@(eventId, mug) <- recvPlea w >>= \case
|
|
|
|
Play Nil -> pure (1, Mug 0)
|
|
|
|
Play (NotNil (e, m, _)) -> pure (e, m)
|
2019-06-19 01:38:24 +03:00
|
|
|
x -> throwIO (InvalidInitialPlea x)
|
2019-06-01 01:55:21 +03:00
|
|
|
|
2019-06-19 03:04:57 +03:00
|
|
|
r <- replay w ws identity logLatestEventNumber eventFetcher
|
2019-06-18 02:47:20 +03:00
|
|
|
|
|
|
|
requestSnapshot w
|
|
|
|
|
2019-06-19 03:04:57 +03:00
|
|
|
pure r
|
|
|
|
|
|
|
|
workerThread :: Worker -> STM Ovum -> (EventId, Mug) -> IO (Async ())
|
|
|
|
workerThread w getEvent (evendId, mug) = async $ forever do
|
|
|
|
ovum <- atomically $ getEvent
|
|
|
|
|
|
|
|
currentDate <- Time.now
|
|
|
|
|
|
|
|
let mat = jam (undefined (mug, currentDate, ovum))
|
2019-06-18 02:47:20 +03:00
|
|
|
|
2019-06-19 03:04:57 +03:00
|
|
|
undefined
|
|
|
|
|
|
|
|
-- Writ (eventId + 1) Nothing mat
|
|
|
|
-- -- assign a new event id.
|
|
|
|
-- -- assign a date
|
|
|
|
-- -- get current mug state
|
|
|
|
-- -- (jam [mug event])
|
|
|
|
-- sendAndRecv
|
2019-06-01 01:55:21 +03:00
|
|
|
|
2019-06-18 02:47:20 +03:00
|
|
|
requestSnapshot :: Worker -> IO ()
|
|
|
|
requestSnapshot w = undefined
|
2019-06-01 01:55:21 +03:00
|
|
|
|
|
|
|
-- The flow here is that we start the worker and then we receive a play event
|
|
|
|
-- with the current worker state:
|
|
|
|
--
|
|
|
|
-- <- [%play ...]
|
|
|
|
--
|
|
|
|
-- Base on this, the main flow is
|
|
|
|
--
|
|
|
|
|
|
|
|
-- [%work ] ->
|
|
|
|
-- <- [%slog]
|
|
|
|
-- <- [%slog]
|
|
|
|
-- <- [%slog]
|
|
|
|
-- <- [%work crash=tang]
|
|
|
|
-- [%work ] -> (replacement)
|
|
|
|
-- <- [%slog]
|
|
|
|
-- <- [%done]
|
2019-06-01 03:21:44 +03:00
|
|
|
-- [%work eventId mat]
|
2019-06-01 01:55:21 +03:00
|
|
|
|
|
|
|
-- response <- recvAtom w
|
|
|
|
|
|
|
|
|
2019-06-01 03:21:44 +03:00
|
|
|
-- Basic Send and Receive Operations -------------------------------------------
|
2019-06-01 01:55:21 +03:00
|
|
|
|
|
|
|
sendAtom :: Worker -> Atom -> IO ()
|
|
|
|
sendAtom w a = hPut (sendHandle w) (unpackAtom a)
|
|
|
|
|
|
|
|
atomBytes :: Iso' Atom ByteString
|
|
|
|
atomBytes = pill . pillBS
|
|
|
|
|
|
|
|
packAtom = view (from atomBytes)
|
|
|
|
|
|
|
|
unpackAtom :: Atom -> ByteString
|
|
|
|
unpackAtom = view atomBytes
|
|
|
|
|
|
|
|
recvLen :: Worker -> IO Word64
|
2019-06-18 02:47:20 +03:00
|
|
|
recvLen w = do
|
|
|
|
bs <- hGet (recvHandle w) 8
|
|
|
|
case length bs of
|
|
|
|
-- This is not big endian safe
|
|
|
|
8 -> unsafeUseAsCString bs (peek . castPtr)
|
|
|
|
_ -> throwIO WorkerConnectionClosed
|
2019-06-01 01:55:21 +03:00
|
|
|
|
|
|
|
recvBytes :: Worker -> Word64 -> IO ByteString
|
2019-06-18 02:47:20 +03:00
|
|
|
recvBytes w = hGet (recvHandle w) . fromIntegral
|
2019-06-01 01:55:21 +03:00
|
|
|
|
2019-06-02 00:07:40 +03:00
|
|
|
recvAtom :: Worker -> IO Atom
|
2019-06-01 01:55:21 +03:00
|
|
|
recvAtom w = do
|
|
|
|
len <- recvLen w
|
|
|
|
bs <- recvBytes w len
|
|
|
|
pure (packAtom bs)
|
|
|
|
|
2019-06-01 03:21:44 +03:00
|
|
|
recvPlea :: Worker -> IO Plea
|
2019-06-01 01:55:21 +03:00
|
|
|
recvPlea w = do
|
|
|
|
a <- recvAtom w
|
2019-06-01 03:21:44 +03:00
|
|
|
n <- fromJustExn (cue a) (BadPleaAtom a)
|
|
|
|
p <- fromJustExn (fromNoun n) (BadPleaNoun n)
|
|
|
|
pure p
|