Skip to content

Commit

Permalink
feat(runtime): make debugger be able to request the log from the even…
Browse files Browse the repository at this point in the history
…t loop
  • Loading branch information
symbiont-stevan-andjelkovic committed Oct 1, 2021
1 parent fbf648d commit a777458
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 14 deletions.
22 changes: 21 additions & 1 deletion src/runtime-prototype/app/scheduler/Main.hs
Original file line number Diff line number Diff line change
@@ -1,8 +1,28 @@
{-# LANGUAGE CPP #-}

module Main where

import System.Environment (getArgs)

import qualified Scheduler

------------------------------------------------------------------------

#ifdef __BAZEL_BUILD__
import GitHash
-- When building with cabal we expect the git commit hash to be passed in via
-- CPP flags, i.e. `--ghc-option=-D__GIT_HASH__=\"X\"`.
#elif defined __GIT_HASH__
gitHash :: String
gitHash = __GIT_HASH__
#else
gitHash :: String
gitHash = "unknown"
#endif

main :: IO ()
main = Scheduler.main
main = do
as <- System.Environment.getArgs
if any (== "--version") as
then putStrLn gitHash
else Scheduler.main gitHash
23 changes: 22 additions & 1 deletion src/runtime-prototype/src/Debugger.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,25 @@
module Debugger where

import Control.Concurrent.Async
import System.FilePath
import System.IO
import System.Posix.Files

import StuntDouble

------------------------------------------------------------------------

readLog :: IO Log
readLog = do
let pipe = "/tmp" </> "scheduler-admin"
-- NOTE: We need to start reading the response before making the request to
-- dump the log, otherwise the response will be written to the void.
a <- async (withFile (pipe <> "-response") ReadWriteMode hGetLine)
appendFile pipe "AdminDumpLog\n"
s <- wait a
return (read s)

main :: IO ()
main = putStrLn "debugger"
main = do
l <- readLog
print l
6 changes: 3 additions & 3 deletions src/runtime-prototype/src/Scheduler.hs
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,8 @@ getDbPath = do
return (home </> ".detsys.db")
else throwIO e

main :: IO ()
main = do
main :: String -> IO ()
main version = do
let executorPort = 3001
executorRef = RemoteRef ("http://localhost:" ++ show executorPort ++ "/api/v1/event") 0
schedulerPort = 3005
Expand All @@ -272,6 +272,6 @@ main = do
now <- getCurrentTime realTime
lref <- spawn el (fakeScheduler executorRef) (initState now (makeSeed 0))
withHttpFrontend el lref schedulerPort $ \pid -> do
putStrLn ("Scheduler is listening on port: " ++ show schedulerPort)
putStrLn ("Scheduler (version " ++ version ++ ") is listening on port: " ++ show schedulerPort)
waitForEventLoopQuit el
cancel pid
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import System.Directory
import System.FilePath
import System.IO
import System.IO.Error
import System.Posix.Files

import StuntDouble.Envelope
import StuntDouble.Message
Expand All @@ -26,16 +25,18 @@ namedPipeAdminTransport fp name = do
-- than `transportReceive` is called.
let pipe = fp </> getEventLoopName name <> "-admin"
safeCreateNamedPipe pipe
safeCreateNamedPipe (pipe <> "-response")
h <- openFile pipe ReadWriteMode
hSetBuffering h LineBuffering
pid <- async (producer h queue)
return AdminTransport
{ adminTransportSend = \s -> withFile (pipe <> "-response") WriteMode $ \h' -> do
{ adminTransportSend = \s -> withFile (pipe <> "-response") ReadWriteMode $ \h' -> do
hSetBuffering h' LineBuffering
-- NOTE: We cannot write back the response on the same pipe as we got
-- the command on, because `adminTransportRecieve` which runs in a loop
-- will consume the response.
hPutStrLn h' s
putStrLn ("dumped log into " ++ pipe <> "-response")
, adminTransportReceive = atomically (flushTBQueue queue)
, adminTransportShutdown = do
adminCleanUpNamedPipe fp name
Expand All @@ -47,13 +48,15 @@ namedPipeAdminTransport fp name = do
l <- hGetLine h
case readMaybe l of
Just cmd -> atomically (writeTBQueue queue cmd)
Nothing -> return () -- XXX: Perhaps we should log something here?
Nothing ->
putStrLn ("namedPipeAdminTransport: unknown admin command: " ++ l)

adminCleanUpNamedPipe :: FilePath -> EventLoopName -> IO ()
adminCleanUpNamedPipe fp name =
catchJust
(\e -> if isDoesNotExistErrorType (ioeGetErrorType e)
then Just ()
else Nothing)
(removeFile (fp </> getEventLoopName name </> "admin"))
(removeFile (fp </> getEventLoopName name </> "admin") >>
removeFile (fp </> getEventLoopName name </> "admin-response"))
return
9 changes: 5 additions & 4 deletions src/runtime-prototype/src/StuntDouble/Log.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,19 @@ import StuntDouble.LogicalTime
------------------------------------------------------------------------

newtype Log = Log [Timestamped LogEntry]
deriving Show
deriving (Show, Read)

data Timestamped a = Timestamped a LogicalTimestamp Timestamp
deriving Show
deriving (Show, Read)

data TimestampedLogically a = TimestampedLogically a LogicalTimestamp
deriving Show
deriving (Show, Read)

data LogEntry
= LogSend LocalRef RemoteRef Message
| LogResumeContinuation RemoteRef LocalRef Message
deriving Show
deriving (Show, Read)

{-
= Spawned LocalRef
| Turn TurnData
Expand Down
2 changes: 1 addition & 1 deletion src/runtime-prototype/src/StuntDouble/Reference.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Data.String
------------------------------------------------------------------------

newtype LocalRef = LocalRef Int
deriving (Eq, Ord, Show)
deriving (Eq, Ord, Show, Read)

data RemoteRef = RemoteRef
{ address :: String
Expand Down

0 comments on commit a777458

Please sign in to comment.