Skip to content

Order requests from a tarball based on the sequence folder #3979

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions booster/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ executables:
- bz2
- casing
- clock
- containers
- directory
- extra
- filepath
Expand Down
31 changes: 25 additions & 6 deletions booster/tools/rpc-client/RpcClient.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import Data.ByteString.Lazy.Char8 qualified as BS
import Data.Char (isDigit, toLower, toUpper)
import Data.Int (Int64)
import Data.List.Extra
import Data.Map qualified as Map
import Data.Maybe (isNothing, mapMaybe)
import Data.Text qualified as Text
import Data.Text.IO qualified as Text
Expand Down Expand Up @@ -515,14 +516,17 @@ runTarball common (Just sock) tarFile keepGoing runOnly compareDetails = do
withTempDir $ \tmp -> withLogLevel common.logLevel $ do
-- unpack relevant tar files (rpc_* directories only)
logInfo_ $ unwords ["unpacking json files from tarball", tarFile, "into", tmp]
jsonFiles <-
liftIO $ Tar.foldEntries (unpackIfRpc tmp) (pure []) throwAnyError checked
(jsonFiles, sequenceMap) <-
liftIO $ Tar.foldEntries (unpackIfRpc tmp) (pure mempty) throwAnyError checked
logInfo_ $ "RPC data:" <> show jsonFiles
logInfo_ $ "Sequence data:" <> show sequenceMap

-- we should not rely on the requests being returned in a sorted order and
-- should therefore sort them explicitly
let requests = sort $ mapMaybe (stripSuffix "_request.json") jsonFiles
let requests = mapMaybe (stripSuffix "_request.json") $ sortBy (compareSequence sequenceMap) jsonFiles
successMsg = if compareDetails then "matches expected" else "has expected type"

logInfo_ $ "Requests to be executed:" <> show (map (<> "_request.json") requests)
results <-
forM requests $ \r -> do
mbError <- runRequest skt tmp jsonFiles r
Expand All @@ -542,8 +546,19 @@ runTarball common (Just sock) tarFile keepGoing runOnly compareDetails = do
throwAnyError :: Either Tar.FormatError Tar.FileNameError -> IO a
throwAnyError = either throwIO throwIO

compareSequence :: Ord a => Ord b => Map.Map a b -> a -> a -> Ordering
compareSequence seqMap a b = case (Map.lookup a seqMap, Map.lookup b seqMap) of
(Nothing, Nothing) -> compare a b
(Just{}, Nothing) -> LT
(Nothing, Just{}) -> GT
(Just a', Just b') -> compare a' b'

-- unpack all */*.json files into dir and return their names
unpackIfRpc :: FilePath -> Tar.Entry -> IO [FilePath] -> IO [FilePath]
unpackIfRpc ::
FilePath ->
Tar.Entry ->
IO ([FilePath], Map.Map FilePath Int) ->
IO ([FilePath], Map.Map FilePath Int)
unpackIfRpc tmpDir entry acc = do
case splitFileName (Tar.entryPath entry) of
-- unpack all directories "<something>" containing "*.json" files
Expand All @@ -562,8 +577,12 @@ runTarball common (Just sock) tarFile keepGoing runOnly compareDetails = do
-- current tarballs do not have dir entries, create dir here
createDirectoryIfMissing True $ tmpDir </> dir
BS.writeFile (tmpDir </> newPath) bs
(newPath :) <$> acc
| otherwise ->
(first (newPath :)) <$> acc
| "sequence" `isInfixOf` dir
, Just (idx :: Int) <- readMaybe file
, Tar.NormalFile bs _size <- Tar.entryContent entry ->
(second $ Map.insert (BS.unpack bs) idx) <$> acc
| otherwise -> do
-- skip anything else
acc

Expand Down
Loading