myriad/src/Myriad/Docker.hs

186 lines
7.4 KiB
Haskell
Raw Normal View History

2019-07-09 11:19:41 +02:00
module Myriad.Docker
( EvalResult(..)
, buildImage
, buildAllImages
, startCleanup
, setupContainer
, killContainer
2019-07-12 10:01:18 +02:00
, killContainers
2019-07-09 11:19:41 +02:00
, evalCode
) where
import Control.Monad.Reader
import qualified Data.Map.Strict as M
2019-07-12 10:01:18 +02:00
import Data.Snowflake
2019-07-09 11:19:41 +02:00
import Control.Concurrent.Async.Lifted
import Control.Concurrent.Lifted (fork, threadDelay)
import Control.Concurrent.MVar.Lifted
2019-07-09 11:19:41 +02:00
import Control.Concurrent.QSem.Lifted
import Control.Exception.Lifted
import System.Process.Typed
import Myriad.Core
import Myriad.Util
buildImage :: LanguageConfig -> MyriadIO ()
2019-07-09 11:19:41 +02:00
buildImage lang@LanguageConfig { name, concurrent } = do
2019-07-12 10:01:18 +02:00
logInfo ["Building image ", cvs $ imageName lang]
exec_ ["docker build -t ", imageName lang, " ./languages/", cvs name]
setupQSems
logInfo ["Built image ", cvs $ imageName lang]
MyriadConfig { prepareContainers } <- asks config
2019-07-09 11:19:41 +02:00
when_ prepareContainers $ setupContainer lang
2019-07-12 10:01:18 +02:00
where
setupQSems :: MyriadIO ()
setupQSems = do
Env { containerSems, evalSems } <- ask
csem <- newQSem 1 -- We only want one container to be set up at a time
esem <- newQSem $ fromIntegral concurrent
mapMVar containerSems $ M.insert name csem
mapMVar evalSems $ M.insert name esem
2019-07-09 11:19:41 +02:00
buildAllImages :: MyriadIO ()
2019-07-09 11:19:41 +02:00
buildAllImages = do
MyriadConfig { languages, buildConcurrently } <- asks config
if buildConcurrently
then forConcurrently_ languages buildImage
else forM_ languages buildImage
startCleanup :: MyriadIO ()
2019-07-09 11:19:41 +02:00
startCleanup = do
MyriadConfig { cleanupInterval } <- asks config
when_ (cleanupInterval > 0) do
let t = fromIntegral cleanupInterval * 60000000
fork $ timer t
where
timer :: Int -> MyriadIO ()
2019-07-09 11:19:41 +02:00
timer t = forever do
threadDelay t
2019-07-12 10:01:18 +02:00
n <- killContainers
logInfo ["Cleaned up ", cvs $ show n, " containers"]
2019-07-09 11:19:41 +02:00
timer t
setupContainer :: LanguageConfig -> MyriadIO ContainerName
2019-07-09 11:19:41 +02:00
setupContainer lang@LanguageConfig { name, memory, cpus } = do
2019-07-12 10:01:18 +02:00
cnts <- asks containers >>= readMVar
2019-07-09 11:19:41 +02:00
case cnts M.!? name of
2019-07-12 10:01:18 +02:00
Nothing -> setup
Just cnt -> pure cnt
where
setup :: MyriadIO ContainerName
setup = do
ref <- asks containers
2019-07-09 11:19:41 +02:00
cnt <- newContainerName lang
2019-07-12 10:01:18 +02:00
exec_
[ "docker run --rm --name="
, cvs cnt
-- User 1000 will be for setting up the environment
, " -u1000:1000 -w/tmp/ -dt --net=none --cpus="
, cvs cpus
, " -m="
, cvs memory
, " --memory-swap="
, cvs memory
, " "
, imageName lang
, " /bin/sh"
]
2019-07-09 11:19:41 +02:00
-- The `eval` directory is where all the eval work is done
-- 711 so that users can't traverse into other people's code
2019-07-12 10:01:18 +02:00
exec_ ["docker exec ", cnt, " mkdir eval"]
exec_ ["docker exec ", cnt, " chmod 711 eval"]
mapMVar ref $ M.insert name cnt
logInfo ["Started container ", cvs cnt]
2019-07-09 11:19:41 +02:00
pure cnt
2019-07-12 10:01:18 +02:00
killContainer :: Language -> MyriadIO Bool
2019-07-09 11:19:41 +02:00
killContainer lang = do
containers <- asks containers >>= readMVar
2019-07-09 11:19:41 +02:00
case containers M.!? lang of
Nothing -> pure False
Just cnt -> do
2019-07-12 10:01:18 +02:00
res <- kill cnt
2019-07-09 11:19:41 +02:00
case res of
2019-07-12 10:01:18 +02:00
Nothing -> pure True
Just err -> do
logError ["An exception occured when killing ", cvs cnt, ":\n", cvs $ show err]
2019-07-09 11:19:41 +02:00
pure False
2019-07-12 10:01:18 +02:00
where
kill :: ContainerName -> MyriadIO (Maybe SomeException)
kill cnt = do
ref <- asks containers
mapMVar ref $ M.delete lang
res <- try $ exec_ ["docker kill ", cnt]
case res of
Left err -> pure $ Just err
Right _ -> do
logInfo ["Killed container ", cvs cnt]
pure Nothing
2019-07-09 11:19:41 +02:00
2019-07-12 10:01:18 +02:00
killContainers :: MyriadIO [ContainerName]
killContainers = do
containers <- asks containers >>= readMVar
2019-07-12 10:01:18 +02:00
xs <- forConcurrently (M.toList containers) \(k, v) -> (v,) <$> killContainer k
2019-07-11 08:20:36 +02:00
pure . map fst $ filter snd xs
2019-07-09 11:19:41 +02:00
evalCode :: LanguageConfig -> Int -> String -> MyriadIO EvalResult
2019-07-12 10:01:18 +02:00
evalCode lang@LanguageConfig { name, timeout, retries } numRetries code = withContainer \cnt -> do
doneRef <- newMVar False -- For keeping track of if the evaluation is done, i.e. succeeded or timed out.
void . fork $ timer doneRef -- `race` could not have been used here since some evals can't be cancelled.
snowflakeGen <- asks snowflakeGen
snowflake <- liftIO $ nextSnowflake snowflakeGen
res <- try $ eval cnt snowflake
case res of
Left (SomeException err) -> do
void $ killContainer name
done <- readMVar doneRef
if done
-- If we find the eval is done from an exception, then it was timed out.
then do
logError ["Code timed out in container ", cvs cnt, ", evaluation ", cvs $ show snowflake]
pure EvalTimedOut
-- Otherwise, the container was killed from another eval, so we should retry.
else do
writeMVar doneRef True
if numRetries < fromIntegral retries
then do
logError ["An exception occured in ", cvs cnt, ", evaluation ", cvs $ show snowflake, ", retrying:\n", cvs $ show err]
evalCode lang (numRetries + 1) code
else do
logError ["An exception occured in ", cvs cnt, ", evaluation ", cvs $ show snowflake, ":\n", cvs $ show err]
pure EvalErrored
Right x -> do
writeMVar doneRef True
pure x
2019-07-09 11:19:41 +02:00
where
2019-07-12 10:01:18 +02:00
withContainer :: (ContainerName -> MyriadIO a) -> MyriadIO a
withContainer f = do
Env { containerSems, evalSems } <- ask
csem <- (M.! name) <$> readMVar containerSems
esem <- (M.! name) <$> readMVar evalSems
bracket_ (waitQSem esem) (signalQSem esem) do
cnt <- bracket_ (waitQSem csem) (signalQSem csem) $ setupContainer lang
f cnt
timer :: MVar Bool -> MyriadIO ()
2019-07-09 11:19:41 +02:00
timer doneRef = do
threadDelay $ fromIntegral timeout * 1000000
done <- readMVar doneRef
2019-07-09 11:19:41 +02:00
unless_ done do
2019-07-12 10:01:18 +02:00
writeMVar doneRef True
killContainer name
2019-07-09 11:19:41 +02:00
2019-07-12 10:01:18 +02:00
eval :: ContainerName -> Snowflake -> MyriadIO EvalResult
eval cnt snowflake = do
logInfo ["Running code in container ", cvs cnt, ", evaluation ", cvs $ show snowflake, ":\n", cvs code]
exec_ ["docker exec ", cvs cnt, " mkdir eval/", show snowflake]
exec_ ["docker exec ", cvs cnt, " chmod 777 eval/", show snowflake]
2019-07-09 11:19:41 +02:00
-- User 1001 will be used for the actual execution so that they can't access `eval` itself
let args = ["exec", "-u1001:1001", "-w/tmp/eval/" <> show snowflake, cnt, "/bin/sh", "/var/run/run.sh", code]
output <- readProcessInterleaved_ $ proc "docker" args
2019-07-12 10:01:18 +02:00
exec_ ["docker exec ", cnt, " rm -rf eval/", show snowflake]
logInfo ["Ran code in container ", cvs cnt, ", evaluation ", cvs $ show snowflake]
2019-07-09 11:19:41 +02:00
pure $ EvalOk output