From 540bd5e1abe6715fe538690c1e84a1c7795b7e3e Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Tue, 24 Sep 2024 12:01:54 -0400 Subject: [PATCH] sim: added run subcommand And a nice sim of random preferred content expressions. --- Annex/Sim.hs | 4 ++-- Command/Sim.hs | 47 ++++++++++++++++++++++++++------------ doc/git-annex-sim.mdwn | 6 +++++ doc/sims/randomwanted.mdwn | 13 +++++++++++ 4 files changed, 53 insertions(+), 17 deletions(-) create mode 100644 doc/sims/randomwanted.mdwn diff --git a/Annex/Sim.hs b/Annex/Sim.hs index d052bad763..42c3b8d437 100644 --- a/Annex/Sim.hs +++ b/Annex/Sim.hs @@ -531,7 +531,7 @@ applySimCommand' (CommandNotPresent _ _) _ _ = error "applySimCommand' CommandNo handleStep :: Bool -> Int -> Int -> SimState SimRepo -> Annex (SimState SimRepo) handleStep muststabilize startn n st - | n > 0 = do + | n >= 0 = do let (st', actions) = getactions unsyncactions st (st'', restactions) <- runoneaction actions st' if null restactions @@ -550,7 +550,7 @@ handleStep muststabilize startn n st | otherwise = checkstabalized st where runrest actions st' n' - | n' > 0 = do + | n' >= 0 = do (st'', restactions) <- runoneaction actions st' if null restactions then handleStep muststabilize startn n' st' diff --git a/Command/Sim.hs b/Command/Sim.hs index 5682577321..f14142b069 100644 --- a/Command/Sim.hs +++ b/Command/Sim.hs @@ -22,19 +22,21 @@ cmd = command "sim" SectionTesting paramCommand (withParams seek) seek :: CmdParams -> CommandSeek -seek ("start":[]) = start Nothing -seek ("start":simfile:[]) = start (Just simfile) -seek ("end":[]) = do - simdir <- fromRawFilePath <$> fromRepo gitAnnexSimDir - whenM (liftIO $ doesDirectoryExist simdir) $ do - liftIO $ removeDirectoryRecursive simdir - showLongNote $ UnquotedString "Sim ended." +seek ("start":[]) = startsim Nothing +seek ("start":simfile:[]) = startsim (Just simfile) +seek ("end":[]) = endsim seek ("show":[]) = do simdir <- fromRepo gitAnnexSimDir liftIO (restoreSim simdir) >>= \case Left err -> giveup err - Right st -> liftIO $ putStr $ generateSimFile $ - reverse $ simHistory st + Right st -> showsim st +seek ("run":simfile:[]) = startsim' (Just simfile) >>= cleanup + where + cleanup st = do + endsim + when (simFailed st) $ do + showsim st + giveup "Simulation shown above had errors." seek ps = case parseSimCommand ps of Left err -> giveup err Right simcmd -> do @@ -48,8 +50,16 @@ seek ps = case parseSimCommand ps of when (simFailed st' && not (simFailed st)) $ giveup "Simulation had errors." -start :: Maybe FilePath -> CommandSeek -start simfile = do +startsim :: Maybe FilePath -> CommandSeek +startsim simfile = startsim' simfile >>= cleanup + where + cleanup st = do + liftIO $ suspendSim st + when (simFailed st) $ + giveup "Simulation had errors." + +startsim' :: Maybe FilePath -> Annex (SimState SimRepo) +startsim' simfile = do simdir <- fromRawFilePath <$> fromRepo gitAnnexSimDir whenM (liftIO $ doesDirectoryExist simdir) $ giveup "A sim was previously started. Use `git-annex sim end` to stop it before starting a new one." @@ -68,12 +78,19 @@ start simfile = do repobyname <- mkGetExistingRepoByName createAnnexDirectory (toRawFilePath simdir) let st' = recordSeed st cs - st'' <- go st' repobyname cs - liftIO $ suspendSim st'' - when (simFailed st'') $ - giveup "Simulation had errors." + go st' repobyname cs go st _ [] = return st go st repobyname (c:cs) = do st' <- runSimCommand c repobyname st go st' repobyname cs + +endsim :: CommandSeek +endsim = do + simdir <- fromRawFilePath <$> fromRepo gitAnnexSimDir + whenM (liftIO $ doesDirectoryExist simdir) $ do + liftIO $ removeDirectoryRecursive simdir + showLongNote $ UnquotedString "Sim ended." + +showsim :: SimState SimRepo -> Annex () +showsim = liftIO . putStr . generateSimFile . reverse . simHistory diff --git a/doc/git-annex-sim.mdwn b/doc/git-annex-sim.mdwn index 665e91fa63..09505ee9d4 100644 --- a/doc/git-annex-sim.mdwn +++ b/doc/git-annex-sim.mdwn @@ -12,6 +12,8 @@ git annex sim show git annex sim end +git annex sim run my.sim + # DESCRIPTION This command simulates the behavior of git-annex in a network of @@ -32,6 +34,10 @@ as well as analyzing the results of the simulation. Use "git annex sim end" to finish the simulation, and clean up. +As a convenience, to run a sim from a file, and then stop it, use +"git-annex sim run". If there is a problem running the sim, it will be +shown before it is stopped. + # THE SIM FILE This text file is used to configure the simulation and also to report on diff --git a/doc/sims/randomwanted.mdwn b/doc/sims/randomwanted.mdwn new file mode 100644 index 0000000000..4896122346 --- /dev/null +++ b/doc/sims/randomwanted.mdwn @@ -0,0 +1,13 @@ +# This is a simulation of random preferred content expressions. +# git-annex sim run this in a loop to explore if an expression can fail to +# stabilize +init foo +init bar +connect foo <-> bar +addmulti 10 .x 100.0kB 10.0MB foo +addmulti 10 .y 100.0kB 10.0MB foo +randomwanted bar largerthan=1MB include=*.x anything present +randomwanted foo largerthan=1MB include=*.x anything present +# 40 is the maximum possible steps, in case bar wants to get all 20 files, +# and foo wants to drop them all +stepstable 40