remote, add Types.GC, use in deleteSpecific

This commit is contained in:
sorki 2023-11-30 06:53:40 +01:00
parent 598cb89fbd
commit 9cd7d3597a
8 changed files with 69 additions and 16 deletions

View File

@ -84,6 +84,7 @@ library
, System.Nix.Store.Remote.Socket
, System.Nix.Store.Remote.Types
, System.Nix.Store.Remote.Types.Activity
, System.Nix.Store.Remote.Types.GC
, System.Nix.Store.Remote.Types.CheckMode
, System.Nix.Store.Remote.Types.Logger
, System.Nix.Store.Remote.Types.ProtoVersion

View File

@ -169,11 +169,11 @@ buildDerivation p drv buildMode = do
-- | Delete store paths
deleteSpecific
:: HashSet StorePath -- ^ Paths to delete
-> MonadStore (HashSet StorePath, Word64) -- ^ (Paths deleted, Bytes freed)
-> MonadStore GCResult
deleteSpecific paths = do
storeDir <- getStoreDir
runOpArgs CollectGarbage $ do
putEnum GCDeleteSpecific
putEnum GCAction_DeleteSpecific
putPaths storeDir paths
putBool False -- ignoreLiveness
putInt (maxBound :: Word64) -- maxFreedBytes
@ -181,10 +181,11 @@ deleteSpecific paths = do
putInt (0::Int)
putInt (0::Int)
getSocketIncremental $ do
deletedPaths <- getPathsOrFail storeDir
bytesFreed <- getInt
gcResult_deletedPaths <- getPathsOrFail storeDir
gcResult_bytesFreed <- getInt
-- TODO: who knows
_ :: Int <- getInt
pure (deletedPaths, bytesFreed)
pure GCResult{..}
ensurePath :: StorePath -> MonadStore ()
ensurePath pn = do

View File

@ -152,11 +152,3 @@ runStoreOpts' sockFamily sockAddr storeRootDir code =
$ (`runReaderT` sock)
$ (`runStateT` (Nothing, []))
$ runExceptT (greet >> code)
data GCAction
= GCReturnLive
| GCReturnDead
| GCDeleteDead
| GCDeleteSpecific
deriving (Eq, Show, Enum)

View File

@ -61,6 +61,12 @@ instance Serialize BuildResult where
putTime startTime
putTime stopTime
-- * GCAction
--
instance Serialize GCAction where
get = getEnum
put = putEnum
-- * ProtoVersion
-- protoVersion_major & 0xFF00

View File

@ -1,5 +1,6 @@
module System.Nix.Store.Remote.Types
( module System.Nix.Store.Remote.Types.Activity
, module System.Nix.Store.Remote.Types.GC
, module System.Nix.Store.Remote.Types.CheckMode
, module System.Nix.Store.Remote.Types.Logger
, module System.Nix.Store.Remote.Types.ProtoVersion
@ -10,6 +11,7 @@ module System.Nix.Store.Remote.Types
) where
import System.Nix.Store.Remote.Types.Activity
import System.Nix.Store.Remote.Types.GC
import System.Nix.Store.Remote.Types.CheckMode
import System.Nix.Store.Remote.Types.Logger
import System.Nix.Store.Remote.Types.ProtoVersion

View File

@ -0,0 +1,45 @@
{-|
Description : Garbage collection actions / options
Maintainer : srk <srk@48.io>
|-}
module System.Nix.Store.Remote.Types.GC (
GCAction(..)
, GCOptions(..)
, GCResult(..)
) where
import Data.HashSet (HashSet)
import Data.Word (Word64)
import GHC.Generics (Generic)
import System.Nix.StorePath (StorePath)
data GCAction
= GCAction_ReturnLive -- ^ Return the set of paths reachable from roots (closure)
| GCAction_ReturnDead -- ^ Return unreachable paths
| GCAction_DeleteDead -- ^ Delete unreachable paths
| GCAction_DeleteSpecific -- ^ Delete specified paths
deriving (Bounded, Eq, Enum, Generic, Ord, Show)
-- | Garbage collector operation options
data GCOptions = GCOptions
{ -- | Operation
gcOptions_operation :: GCAction
-- | If set, then reachability from the roots is ignored (unused)
, gcOptions_ignoreLiveness :: Bool
-- | Paths to delete for @GCAction_DeleteSpecific@
, gcOptions_pathsToDelete :: HashSet StorePath
-- | Stop after `gcOptions_maxFreed` bytes have been freed
, gcOptions_maxFreed :: Integer
} deriving (Eq, Generic, Ord, Show)
data GCResult = GCResult
{ -- | Depending on the action, the GC roots,
-- or the paths that would be or have been deleted
gcResult_deletedPaths :: HashSet StorePath
-- | The number of bytes that would be or was freed for
--
-- - @GCAction_ReturnDead@
-- - @GCAction_DeleteDead@
-- - @GCAction_DeleteSpecific@
, gcResult_bytesFreed :: Word64
} deriving (Eq, Generic, Ord, Show)

View File

@ -285,7 +285,7 @@ spec_protocol = Hspec.around withNixDaemon $
liftIO $ forM_ tempRootList $ \entry -> do
removeFile $ mconcat [ tempRootsDir, "/", entry ]
(deletedPaths, deletedBytes) <- deleteSpecific (HS.fromList [path])
deletedPaths `shouldBe` HS.fromList [path]
deletedBytes `shouldBe` 4
GCResult{..} <- deleteSpecific (HS.fromList [path])
gcResult_deletedPaths `shouldBe` HS.fromList [path]
gcResult_bytesFreed `shouldBe` 4

View File

@ -156,6 +156,12 @@ spec = parallel $ do
it' "ResolvesToAlreadyValid" BuildStatus_ResolvesToAlreadyValid 13
it' "NoSubstituters" BuildStatus_NoSubstituters 14
describe "GCAction enum order matches Nix" $ do
it' "ReturnLive" GCAction_ReturnLive 0
it' "ReturnDead" GCAction_ReturnDead 1
it' "DeleteDead" GCAction_DeleteDead 2
it' "DeleteSpecific" GCAction_DeleteSpecific 3
describe "Logger" $ do
describe "Activity enum order matches Nix" $ do
it' "CopyPath" Activity_CopyPath 100