Skip to content

Commit 982fdab

Browse files
committed
remote: prefix WorkerOp, add workerOp Serializer, test
1 parent 17992a4 commit 982fdab

File tree

4 files changed

+106
-81
lines changed

4 files changed

+106
-81
lines changed

hnix-store-remote/src/System/Nix/Store/Remote.hs

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ addToStore name source recursive repair = do
158158
Control.Monad.when (repair == RepairMode_DoRepair)
159159
$ error "repairing is not supported when building through the Nix daemon"
160160

161-
runOpArgsIO AddToStore $ \yield -> do
161+
runOpArgsIO WorkerOp_AddToStore $ \yield -> do
162162
yield $ Data.Serialize.Put.runPut $ do
163163
putText $ System.Nix.StorePath.unStorePathName name
164164
putBool
@@ -186,7 +186,7 @@ addTextToStore name text references' repair = do
186186
$ error "repairing is not supported when building through the Nix daemon"
187187

188188
storeDir <- getStoreDir
189-
runOpArgs AddTextToStore $ do
189+
runOpArgs WorkerOp_AddTextToStore $ do
190190
putText name
191191
putText text
192192
putPaths storeDir references'
@@ -195,30 +195,30 @@ addTextToStore name text references' repair = do
195195
addSignatures :: StorePath -> [ByteString] -> MonadStore ()
196196
addSignatures p signatures = do
197197
storeDir <- getStoreDir
198-
Control.Monad.void $ simpleOpArgs AddSignatures $ do
198+
Control.Monad.void $ simpleOpArgs WorkerOp_AddSignatures $ do
199199
putPath storeDir p
200200
putByteStrings signatures
201201

202202
addIndirectRoot :: StorePath -> MonadStore ()
203203
addIndirectRoot pn = do
204204
storeDir <- getStoreDir
205-
Control.Monad.void $ simpleOpArgs AddIndirectRoot $ putPath storeDir pn
205+
Control.Monad.void $ simpleOpArgs WorkerOp_AddIndirectRoot $ putPath storeDir pn
206206

207207
-- | Add temporary garbage collector root.
208208
--
209209
-- This root is removed as soon as the client exits.
210210
addTempRoot :: StorePath -> MonadStore ()
211211
addTempRoot pn = do
212212
storeDir <- getStoreDir
213-
Control.Monad.void $ simpleOpArgs AddTempRoot $ putPath storeDir pn
213+
Control.Monad.void $ simpleOpArgs WorkerOp_AddTempRoot $ putPath storeDir pn
214214

215215
-- | Build paths if they are an actual derivations.
216216
--
217217
-- If derivation output paths are already valid, do nothing.
218218
buildPaths :: HashSet StorePath -> BuildMode -> MonadStore ()
219219
buildPaths ps bm = do
220220
storeDir <- getStoreDir
221-
Control.Monad.void $ simpleOpArgs BuildPaths $ do
221+
Control.Monad.void $ simpleOpArgs WorkerOp_BuildPaths $ do
222222
putPaths storeDir ps
223223
putInt $ fromEnum bm
224224

@@ -229,7 +229,7 @@ buildDerivation
229229
-> MonadStore BuildResult
230230
buildDerivation p drv buildMode = do
231231
storeDir <- getStoreDir
232-
runOpArgs BuildDerivation $ do
232+
runOpArgs WorkerOp_BuildDerivation $ do
233233
putPath storeDir p
234234
putDerivation storeDir drv
235235
putEnum buildMode
@@ -247,7 +247,7 @@ deleteSpecific
247247
-> MonadStore GCResult
248248
deleteSpecific paths = do
249249
storeDir <- getStoreDir
250-
runOpArgs CollectGarbage $ do
250+
runOpArgs WorkerOp_CollectGarbage $ do
251251
putEnum GCAction_DeleteSpecific
252252
putPaths storeDir paths
253253
putBool False -- ignoreLiveness
@@ -265,12 +265,14 @@ deleteSpecific paths = do
265265
ensurePath :: StorePath -> MonadStore ()
266266
ensurePath pn = do
267267
storeDir <- getStoreDir
268-
Control.Monad.void $ simpleOpArgs EnsurePath $ putPath storeDir pn
268+
Control.Monad.void
269+
$ simpleOpArgs WorkerOp_EnsurePath
270+
$ putPath storeDir pn
269271

270272
-- | Find garbage collector roots.
271273
findRoots :: MonadStore (Map ByteString StorePath)
272274
findRoots = do
273-
runOp FindRoots
275+
runOp WorkerOp_FindRoots
274276
sd <- getStoreDir
275277
res <-
276278
getSocketIncremental
@@ -292,7 +294,7 @@ findRoots = do
292294
isValidPathUncached :: StorePath -> MonadStore Bool
293295
isValidPathUncached p = do
294296
storeDir <- getStoreDir
295-
simpleOpArgs IsValidPath $ putPath storeDir p
297+
simpleOpArgs WorkerOp_IsValidPath $ putPath storeDir p
296298

297299
-- | Query valid paths from set, optionally try to use substitutes.
298300
queryValidPaths
@@ -301,26 +303,26 @@ queryValidPaths
301303
-> MonadStore (HashSet StorePath)
302304
queryValidPaths ps substitute = do
303305
storeDir <- getStoreDir
304-
runOpArgs QueryValidPaths $ do
306+
runOpArgs WorkerOp_QueryValidPaths $ do
305307
putPaths storeDir ps
306308
putBool $ substitute == SubstituteMode_DoSubstitute
307309
sockGetPaths
308310

309311
queryAllValidPaths :: MonadStore (HashSet StorePath)
310312
queryAllValidPaths = do
311-
runOp QueryAllValidPaths
313+
runOp WorkerOp_QueryAllValidPaths
312314
sockGetPaths
313315

314316
querySubstitutablePaths :: HashSet StorePath -> MonadStore (HashSet StorePath)
315317
querySubstitutablePaths ps = do
316318
storeDir <- getStoreDir
317-
runOpArgs QuerySubstitutablePaths $ putPaths storeDir ps
319+
runOpArgs WorkerOp_QuerySubstitutablePaths $ putPaths storeDir ps
318320
sockGetPaths
319321

320322
queryPathInfoUncached :: StorePath -> MonadStore (Metadata StorePath)
321323
queryPathInfoUncached path = do
322324
storeDir <- getStoreDir
323-
runOpArgs QueryPathInfo $ do
325+
runOpArgs WorkerOp_QueryPathInfo $ do
324326
putPath storeDir path
325327

326328
valid <- sockGetBool
@@ -369,30 +371,30 @@ queryPathInfoUncached path = do
369371
queryReferrers :: StorePath -> MonadStore (HashSet StorePath)
370372
queryReferrers p = do
371373
storeDir <- getStoreDir
372-
runOpArgs QueryReferrers $ putPath storeDir p
374+
runOpArgs WorkerOp_QueryReferrers $ putPath storeDir p
373375
sockGetPaths
374376

375377
queryValidDerivers :: StorePath -> MonadStore (HashSet StorePath)
376378
queryValidDerivers p = do
377379
storeDir <- getStoreDir
378-
runOpArgs QueryValidDerivers $ putPath storeDir p
380+
runOpArgs WorkerOp_QueryValidDerivers $ putPath storeDir p
379381
sockGetPaths
380382

381383
queryDerivationOutputs :: StorePath -> MonadStore (HashSet StorePath)
382384
queryDerivationOutputs p = do
383385
storeDir <- getStoreDir
384-
runOpArgs QueryDerivationOutputs $ putPath storeDir p
386+
runOpArgs WorkerOp_QueryDerivationOutputs $ putPath storeDir p
385387
sockGetPaths
386388

387389
queryDerivationOutputNames :: StorePath -> MonadStore (HashSet StorePath)
388390
queryDerivationOutputNames p = do
389391
storeDir <- getStoreDir
390-
runOpArgs QueryDerivationOutputNames $ putPath storeDir p
392+
runOpArgs WorkerOp_QueryDerivationOutputNames $ putPath storeDir p
391393
sockGetPaths
392394

393395
queryPathFromHashPart :: StorePathHashPart -> MonadStore StorePath
394396
queryPathFromHashPart storePathHash = do
395-
runOpArgs QueryPathFromHashPart
397+
runOpArgs WorkerOp_QueryPathFromHashPart
396398
$ putText
397399
$ System.Nix.StorePath.storePathHashPartToText storePathHash
398400
sockGetPath
@@ -408,7 +410,7 @@ queryMissing
408410
)
409411
queryMissing ps = do
410412
storeDir <- getStoreDir
411-
runOpArgs QueryMissing $ putPaths storeDir ps
413+
runOpArgs WorkerOp_QueryMissing $ putPaths storeDir ps
412414

413415
willBuild <- sockGetPaths
414416
willSubstitute <- sockGetPaths
@@ -418,13 +420,13 @@ queryMissing ps = do
418420
pure (willBuild, willSubstitute, unknown, downloadSize', narSize')
419421

420422
optimiseStore :: MonadStore ()
421-
optimiseStore = Control.Monad.void $ simpleOp OptimiseStore
423+
optimiseStore = Control.Monad.void $ simpleOp WorkerOp_OptimiseStore
422424

423425
syncWithGC :: MonadStore ()
424-
syncWithGC = Control.Monad.void $ simpleOp SyncWithGC
426+
syncWithGC = Control.Monad.void $ simpleOp WorkerOp_SyncWithGC
425427

426428
-- returns True on errors
427429
verifyStore :: CheckMode -> RepairMode -> MonadStore Bool
428-
verifyStore check repair = simpleOpArgs VerifyStore $ do
430+
verifyStore check repair = simpleOpArgs WorkerOp_VerifyStore $ do
429431
putBool $ check == CheckMode_DoCheck
430432
putBool $ repair == RepairMode_DoRepair

hnix-store-remote/src/System/Nix/Store/Remote/Serializer.hs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ module System.Nix.Store.Remote.Serializer
6666
-- * Handshake
6767
, HandshakeSError(..)
6868
, workerMagic
69+
-- * Worker protocol
70+
, workerOp
6971
) where
7072

7173
import Control.Monad.Except (MonadError, throwError, )
@@ -922,3 +924,9 @@ workerMagic = Serializer
922924
$ word64ToWorkerMagic c
923925
, putS = putS int . workerMagicToWord64
924926
}
927+
928+
-- * Worker protocol
929+
930+
workerOp :: NixSerializer r SError WorkerOp
931+
workerOp = enum
932+

hnix-store-remote/src/System/Nix/Store/Remote/Types/WorkerOp.hs

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -7,51 +7,51 @@ module System.Nix.Store.Remote.Types.WorkerOp
77
-- This type has gaps filled in so that the GHC builtin
88
-- Enum instance lands on the right values.
99
data WorkerOp
10-
= Reserved_0__ -- 0
11-
| IsValidPath -- 1
12-
| Reserved_2__ -- 2
13-
| HasSubstitutes -- 3
14-
| QueryPathHash -- 4 // obsolete
15-
| QueryReferences -- 5 // obsolete
16-
| QueryReferrers -- 6
17-
| AddToStore -- 7
18-
| AddTextToStore -- 8 // obsolete since 1.25, Nix 3.0. Use wopAddToStore
19-
| BuildPaths -- 9
20-
| EnsurePath -- 10 0xa
21-
| AddTempRoot -- 11 0xb
22-
| AddIndirectRoot -- 12 0xc
23-
| SyncWithGC -- 13 0xd
24-
| FindRoots -- 14 0xe
25-
| Reserved_15__ -- 15 0xf
26-
| ExportPath -- 16 0x10 // obsolete
27-
| Reserved_17__ -- 17 0x11
28-
| QueryDeriver -- 18 0x12 // obsolete
29-
| SetOptions -- 19 0x13
30-
| CollectGarbage -- 20 0x14
31-
| QuerySubstitutablePathInfo -- 21 0x15
32-
| QueryDerivationOutputs -- 22 0x16 // obsolete
33-
| QueryAllValidPaths -- 23 0x17
34-
| QueryFailedPaths -- 24 0x18
35-
| ClearFailedPaths -- 25 0x19
36-
| QueryPathInfo -- 26 0x1a
37-
| ImportPaths -- 27 0x1b // obsolete
38-
| QueryDerivationOutputNames -- 28 0x1c // obsolete
39-
| QueryPathFromHashPart -- 29 0x1d
40-
| QuerySubstitutablePathInfos -- 30 0x1e
41-
| QueryValidPaths -- 31 0x1f
42-
| QuerySubstitutablePaths -- 32 0x20
43-
| QueryValidDerivers -- 33 0x21
44-
| OptimiseStore -- 34 0x22
45-
| VerifyStore -- 35 0x23
46-
| BuildDerivation -- 36 0x24
47-
| AddSignatures -- 37 0x25
48-
| NarFromPath -- 38 0x26
49-
| AddToStoreNar -- 39 0x27
50-
| QueryMissing -- 40 0x28
51-
| QueryDerivationOutputMap -- 41 0x29
52-
| RegisterDrvOutput -- 42 0x2a
53-
| QueryRealisation -- 43 0x2b
54-
| AddMultipleToStore -- 44 0x2c
55-
| AddBuildLog -- 45 0x2d
56-
| BuildPathsWithResults -- 46 0x2e
10+
= WorkerOp_Reserved_0__ -- 0
11+
| WorkerOp_IsValidPath -- 1
12+
| WorkerOp_Reserved_2__ -- 2
13+
| WorkerOp_HasSubstitutes -- 3
14+
| WorkerOp_QueryPathHash -- 4 // obsolete
15+
| WorkerOp_QueryReferences -- 5 // obsolete
16+
| WorkerOp_QueryReferrers -- 6
17+
| WorkerOp_AddToStore -- 7
18+
| WorkerOp_AddTextToStore -- 8 // obsolete since 1.25, Nix 3.0. Use wopAddToStore
19+
| WorkerOp_BuildPaths -- 9
20+
| WorkerOp_EnsurePath -- 10 0xa
21+
| WorkerOp_AddTempRoot -- 11 0xb
22+
| WorkerOp_AddIndirectRoot -- 12 0xc
23+
| WorkerOp_SyncWithGC -- 13 0xd
24+
| WorkerOp_FindRoots -- 14 0xe
25+
| WorkerOp_Reserved_15__ -- 15 0xf
26+
| WorkerOp_ExportPath -- 16 0x10 // obsolete
27+
| WorkerOp_Reserved_17__ -- 17 0x11
28+
| WorkerOp_QueryDeriver -- 18 0x12 // obsolete
29+
| WorkerOp_SetOptions -- 19 0x13
30+
| WorkerOp_CollectGarbage -- 20 0x14
31+
| WorkerOp_QuerySubstitutablePathInfo -- 21 0x15
32+
| WorkerOp_QueryDerivationOutputs -- 22 0x16 // obsolete
33+
| WorkerOp_QueryAllValidPaths -- 23 0x17
34+
| WorkerOp_QueryFailedPaths -- 24 0x18
35+
| WorkerOp_ClearFailedPaths -- 25 0x19
36+
| WorkerOp_QueryPathInfo -- 26 0x1a
37+
| WorkerOp_ImportPaths -- 27 0x1b // obsolete
38+
| WorkerOp_QueryDerivationOutputNames -- 28 0x1c // obsolete
39+
| WorkerOp_QueryPathFromHashPart -- 29 0x1d
40+
| WorkerOp_QuerySubstitutablePathInfos -- 30 0x1e
41+
| WorkerOp_QueryValidPaths -- 31 0x1f
42+
| WorkerOp_QuerySubstitutablePaths -- 32 0x20
43+
| WorkerOp_QueryValidDerivers -- 33 0x21
44+
| WorkerOp_OptimiseStore -- 34 0x22
45+
| WorkerOp_VerifyStore -- 35 0x23
46+
| WorkerOp_BuildDerivation -- 36 0x24
47+
| WorkerOp_AddSignatures -- 37 0x25
48+
| WorkerOp_NarFromPath -- 38 0x26
49+
| WorkerOp_AddToStoreNar -- 39 0x27
50+
| WorkerOp_QueryMissing -- 40 0x28
51+
| WorkerOp_QueryDerivationOutputMap -- 41 0x29
52+
| WorkerOp_RegisterDrvOutput -- 42 0x2a
53+
| WorkerOp_QueryRealisation -- 43 0x2b
54+
| WorkerOp_AddMultipleToStore -- 44 0x2c
55+
| WorkerOp_AddBuildLog -- 45 0x2d
56+
| WorkerOp_BuildPathsWithResults -- 46 0x2e
5757
deriving (Bounded, Eq, Enum, Ord, Show, Read)

hnix-store-remote/tests/NixSerializerSpec.hs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import Crypto.Hash (MD5, SHA1, SHA256, SHA512)
66
import Data.Dependent.Sum (DSum((:=>)))
77
import Data.Fixed (Uni)
88
import Data.Time (NominalDiffTime)
9-
import Test.Hspec (Expectation, Spec, describe, parallel, shouldBe)
9+
import Data.Word (Word64)
10+
import Test.Hspec (Expectation, Spec, describe, it, parallel, shouldBe)
1011
import Test.Hspec.QuickCheck (prop)
1112
import Test.QuickCheck (Gen, arbitrary, forAll, suchThat)
1213
import Test.QuickCheck.Instances ()
@@ -23,7 +24,7 @@ import System.Nix.StorePath (StoreDir)
2324
import System.Nix.StorePath.Metadata (Metadata(..))
2425
import System.Nix.Store.Remote.Arbitrary ()
2526
import System.Nix.Store.Remote.Serializer
26-
import System.Nix.Store.Remote.Types (ErrorInfo(..), Logger(..), ProtoVersion(..), Trace(..))
27+
import System.Nix.Store.Remote.Types (ErrorInfo(..), Logger(..), ProtoVersion(..), Trace(..), WorkerOp(..))
2728

2829
-- | Test for roundtrip using @NixSerializer@
2930
roundtripSReader
@@ -154,11 +155,25 @@ spec = parallel $ do
154155
$ \pv ->
155156
forAll (arbitrary `suchThat` errorInfoIf (protoVersion_minor pv >= 26))
156157
$ roundtripSReader logger pv
157-
where
158-
errorInfoIf True (Logger_Error (Right x)) = noJust0s x
159-
errorInfoIf False (Logger_Error (Left _)) = True
160-
errorInfoIf _ (Logger_Error _) = False
161-
errorInfoIf _ _ = True
162-
noJust0s ErrorInfo{..} =
163-
errorInfoPosition /= Just 0
164-
&& all ((/= Just 0) . tracePosition) errorInfoTraces
158+
159+
describe "Enums" $ do
160+
let it' name constr value =
161+
it name
162+
$ (runP enum () constr)
163+
`shouldBe`
164+
(runP (int @Word64) () value)
165+
166+
describe "WorkerOp enum order matches Nix" $ do
167+
it' "IsValidPath" WorkerOp_IsValidPath 1
168+
it' "BuildPathsWithResults" WorkerOp_BuildPathsWithResults 46
169+
170+
errorInfoIf :: Bool -> Logger -> Bool
171+
errorInfoIf True (Logger_Error (Right x)) = noJust0s x
172+
where
173+
noJust0s :: ErrorInfo -> Bool
174+
noJust0s ErrorInfo{..} =
175+
errorInfoPosition /= Just 0
176+
&& all ((/= Just 0) . tracePosition) errorInfoTraces
177+
errorInfoIf False (Logger_Error (Left _)) = True
178+
errorInfoIf _ (Logger_Error _) = False
179+
errorInfoIf _ _ = True

0 commit comments

Comments
 (0)