module Ecluse.Core.Mirror.State (
StateTarget,
stateTarget,
StateRevision,
ProjectMirrorRecord,
recordTarget,
recordPackage,
recordVersions,
recordEnvelope,
recordVersion,
StoredMirrorFragment,
fragmentDetails,
fragmentReadData,
MirrorVersion,
versionFiles,
versionFragments,
versionCompanions,
versionVerifiedIdentities,
CompanionState (..),
CompanionRef,
companionDigest,
companionSize,
VerifiedCompanionBytes,
companionBody,
MirrorStateFault (..),
StateRead (..),
StateWrite (..),
MirrorState (..),
StateCharge,
extendProjectRecord,
retainCompanionReference,
verifyCompanionBytes,
verifySourceCompanionBytes,
verifyRetainedCompanion,
checkMirrorCompletion,
MirrorCompletion (..),
MirrorCompletionFault (..),
encodeProjectRecord,
projectRecordWeight,
recordExpectedInventory,
retainProjectCompanion,
recordVerifiedArtifact,
) where
import Ecluse.Core.Mirror.State.Codec
import Ecluse.Core.Mirror.State.Format (MirrorStateFormat)
import Ecluse.Core.Mirror.State.Internal
import Ecluse.Core.Package (PackageDetails, PackageName)
import Ecluse.Core.Registry.MirrorUpload (MirrorInventory, MirrorReadData, VerifiedArtifactBytes, inventoryPackage)
import Ecluse.Core.Security (Limits)
import Ecluse.Core.Server.MemoryModel (saturatingCharge)
import Ecluse.Core.Version (Version)
fragmentDetails :: StoredMirrorFragment -> PackageDetails
fragmentDetails :: StoredMirrorFragment -> PackageDetails
fragmentDetails (StoredMirrorFragment PackageDetails
details MirrorReadData
_ NonEmpty ExpectedMirrorFile
_) = PackageDetails
details
fragmentReadData :: StoredMirrorFragment -> MirrorReadData
fragmentReadData :: StoredMirrorFragment -> MirrorReadData
fragmentReadData (StoredMirrorFragment PackageDetails
_ MirrorReadData
source NonEmpty ExpectedMirrorFile
_) = MirrorReadData
source
recordExpectedInventory :: MirrorStateFormat -> Limits -> StateCharge -> StateTarget -> MirrorState -> MirrorInventory -> IO (Either MirrorStateFault (StateRevision, ProjectMirrorRecord))
recordExpectedInventory :: MirrorStateFormat
-> Limits
-> StateCharge
-> StateTarget
-> MirrorState
-> MirrorInventory
-> IO
(Either MirrorStateFault (StateRevision, ProjectMirrorRecord))
recordExpectedInventory MirrorStateFormat
format Limits
limits StateCharge
charge StateTarget
target MirrorState
store MirrorInventory
inventory =
Limits
-> StateCharge
-> Int
-> MirrorState
-> PackageName
-> (Maybe ProjectMirrorRecord
-> Either MirrorStateFault ProjectMirrorRecord)
-> IO
(Either MirrorStateFault (StateRevision, ProjectMirrorRecord))
updateRecord Limits
limits StateCharge
charge (MirrorInventory -> Int
mirrorInventoryWeight MirrorInventory
inventory) MirrorState
store (MirrorInventory -> PackageName
inventoryPackage MirrorInventory
inventory) (MirrorStateFormat
-> Limits
-> StateTarget
-> MirrorInventory
-> Maybe ProjectMirrorRecord
-> Either MirrorStateFault ProjectMirrorRecord
extendProjectRecord MirrorStateFormat
format Limits
limits StateTarget
target MirrorInventory
inventory)
recordVerifiedArtifact :: Limits -> StateCharge -> MirrorState -> PackageName -> Version -> VerifiedArtifactBytes -> IO (Either MirrorStateFault (StateRevision, ProjectMirrorRecord))
recordVerifiedArtifact :: Limits
-> StateCharge
-> MirrorState
-> PackageName
-> Version
-> VerifiedArtifactBytes
-> IO
(Either MirrorStateFault (StateRevision, ProjectMirrorRecord))
recordVerifiedArtifact Limits
limits StateCharge
charge MirrorState
store PackageName
name Version
version VerifiedArtifactBytes
verified =
Limits
-> StateCharge
-> Int
-> MirrorState
-> PackageName
-> (Maybe ProjectMirrorRecord
-> Either MirrorStateFault ProjectMirrorRecord)
-> IO
(Either MirrorStateFault (StateRevision, ProjectMirrorRecord))
updateRecord Limits
limits StateCharge
charge Int
0 MirrorState
store PackageName
name (MirrorStateFault
-> Maybe ProjectMirrorRecord
-> Either MirrorStateFault ProjectMirrorRecord
forall l r. l -> Maybe r -> Either l r
maybeToRight (Text -> MirrorStateFault
StateInvalid Text
"missing expected inventory") (Maybe ProjectMirrorRecord
-> Either MirrorStateFault ProjectMirrorRecord)
-> (ProjectMirrorRecord
-> Either MirrorStateFault ProjectMirrorRecord)
-> Maybe ProjectMirrorRecord
-> Either MirrorStateFault ProjectMirrorRecord
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> Version
-> VerifiedArtifactBytes
-> ProjectMirrorRecord
-> Either MirrorStateFault ProjectMirrorRecord
retainVerifiedIdentity Version
version VerifiedArtifactBytes
verified)
retainProjectCompanion :: Limits -> StateCharge -> MirrorState -> PackageName -> Version -> VerifiedCompanionBytes -> IO (Either MirrorStateFault (StateRevision, ProjectMirrorRecord))
retainProjectCompanion :: Limits
-> StateCharge
-> MirrorState
-> PackageName
-> Version
-> VerifiedCompanionBytes
-> IO
(Either MirrorStateFault (StateRevision, ProjectMirrorRecord))
retainProjectCompanion Limits
limits StateCharge
charge MirrorState
store PackageName
name Version
version VerifiedCompanionBytes
bytes =
MirrorState
-> StateCharge
-> VerifiedCompanionBytes
-> IO (Either MirrorStateFault CompanionRef)
msRetainCompanion MirrorState
store StateCharge
charge VerifiedCompanionBytes
bytes IO (Either MirrorStateFault CompanionRef)
-> (Either MirrorStateFault CompanionRef
-> IO
(Either MirrorStateFault (StateRevision, ProjectMirrorRecord)))
-> IO
(Either MirrorStateFault (StateRevision, ProjectMirrorRecord))
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Left MirrorStateFault
fault -> Either MirrorStateFault (StateRevision, ProjectMirrorRecord)
-> IO
(Either MirrorStateFault (StateRevision, ProjectMirrorRecord))
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (MirrorStateFault
-> Either MirrorStateFault (StateRevision, ProjectMirrorRecord)
forall a b. a -> Either a b
Left MirrorStateFault
fault)
Right CompanionRef
reference -> Limits
-> StateCharge
-> Int
-> MirrorState
-> PackageName
-> (Maybe ProjectMirrorRecord
-> Either MirrorStateFault ProjectMirrorRecord)
-> IO
(Either MirrorStateFault (StateRevision, ProjectMirrorRecord))
updateRecord Limits
limits StateCharge
charge Int
0 MirrorState
store PackageName
name (MirrorStateFault
-> Maybe ProjectMirrorRecord
-> Either MirrorStateFault ProjectMirrorRecord
forall l r. l -> Maybe r -> Either l r
maybeToRight MirrorStateFault
StateMissingCompanion (Maybe ProjectMirrorRecord
-> Either MirrorStateFault ProjectMirrorRecord)
-> (ProjectMirrorRecord
-> Either MirrorStateFault ProjectMirrorRecord)
-> Maybe ProjectMirrorRecord
-> Either MirrorStateFault ProjectMirrorRecord
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> Version
-> VerifiedCompanionBytes
-> CompanionRef
-> ProjectMirrorRecord
-> Either MirrorStateFault ProjectMirrorRecord
retainCompanionReference Version
version VerifiedCompanionBytes
bytes CompanionRef
reference)
updateRecord :: Limits -> StateCharge -> Int -> MirrorState -> PackageName -> (Maybe ProjectMirrorRecord -> Either MirrorStateFault ProjectMirrorRecord) -> IO (Either MirrorStateFault (StateRevision, ProjectMirrorRecord))
updateRecord :: Limits
-> StateCharge
-> Int
-> MirrorState
-> PackageName
-> (Maybe ProjectMirrorRecord
-> Either MirrorStateFault ProjectMirrorRecord)
-> IO
(Either MirrorStateFault (StateRevision, ProjectMirrorRecord))
updateRecord Limits
limits StateCharge
charge Int
inputWeight MirrorState
store PackageName
name Maybe ProjectMirrorRecord
-> Either MirrorStateFault ProjectMirrorRecord
change = Int
-> MirrorStateFault
-> IO
(Either MirrorStateFault (StateRevision, ProjectMirrorRecord))
forall {t}.
(Eq t, Num t) =>
t
-> MirrorStateFault
-> IO
(Either MirrorStateFault (StateRevision, ProjectMirrorRecord))
go (Int
8 :: Int) MirrorStateFault
StateRetryExhausted
where
go :: t
-> MirrorStateFault
-> IO
(Either MirrorStateFault (StateRevision, ProjectMirrorRecord))
go t
0 MirrorStateFault
lastFault = Either MirrorStateFault (StateRevision, ProjectMirrorRecord)
-> IO
(Either MirrorStateFault (StateRevision, ProjectMirrorRecord))
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (MirrorStateFault
-> Either MirrorStateFault (StateRevision, ProjectMirrorRecord)
forall a b. a -> Either a b
Left MirrorStateFault
lastFault)
go t
attempts MirrorStateFault
_ =
MirrorState
-> StateCharge
-> PackageName
-> Maybe StateRevision
-> IO (Either MirrorStateFault StateRead)
msReadProject MirrorState
store StateCharge
charge PackageName
name Maybe StateRevision
forall a. Maybe a
Nothing IO (Either MirrorStateFault StateRead)
-> (Either MirrorStateFault StateRead
-> IO
(Either MirrorStateFault (StateRevision, ProjectMirrorRecord)))
-> IO
(Either MirrorStateFault (StateRevision, ProjectMirrorRecord))
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Left MirrorStateFault
fault -> Either MirrorStateFault (StateRevision, ProjectMirrorRecord)
-> IO
(Either MirrorStateFault (StateRevision, ProjectMirrorRecord))
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (MirrorStateFault
-> Either MirrorStateFault (StateRevision, ProjectMirrorRecord)
forall a b. a -> Either a b
Left MirrorStateFault
fault)
Right (StateUnchanged StateRevision
_) -> Either MirrorStateFault (StateRevision, ProjectMirrorRecord)
-> IO
(Either MirrorStateFault (StateRevision, ProjectMirrorRecord))
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (MirrorStateFault
-> Either MirrorStateFault (StateRevision, ProjectMirrorRecord)
forall a b. a -> Either a b
Left MirrorStateFault
StateRevisionMismatch)
Right StateRead
StateAbsent -> t
-> Maybe StateRevision
-> Maybe ProjectMirrorRecord
-> IO
(Either MirrorStateFault (StateRevision, ProjectMirrorRecord))
apply t
attempts Maybe StateRevision
forall a. Maybe a
Nothing Maybe ProjectMirrorRecord
forall a. Maybe a
Nothing
Right (StateLoaded StateRevision
revision ProjectMirrorRecord
record) -> t
-> Maybe StateRevision
-> Maybe ProjectMirrorRecord
-> IO
(Either MirrorStateFault (StateRevision, ProjectMirrorRecord))
apply t
attempts (StateRevision -> Maybe StateRevision
forall a. a -> Maybe a
Just StateRevision
revision) (ProjectMirrorRecord -> Maybe ProjectMirrorRecord
forall a. a -> Maybe a
Just ProjectMirrorRecord
record)
apply :: t
-> Maybe StateRevision
-> Maybe ProjectMirrorRecord
-> IO
(Either MirrorStateFault (StateRevision, ProjectMirrorRecord))
apply t
attempts Maybe StateRevision
revision Maybe ProjectMirrorRecord
previous = case (ProjectMirrorRecord -> Either MirrorStateFault Int)
-> Maybe ProjectMirrorRecord -> Either MirrorStateFault (Maybe Int)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Maybe a -> f (Maybe b)
traverse (Limits -> ProjectMirrorRecord -> Either MirrorStateFault Int
projectRecordWorkWeight Limits
limits) Maybe ProjectMirrorRecord
previous of
Left MirrorStateFault
fault -> Either MirrorStateFault (StateRevision, ProjectMirrorRecord)
-> IO
(Either MirrorStateFault (StateRevision, ProjectMirrorRecord))
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (MirrorStateFault
-> Either MirrorStateFault (StateRevision, ProjectMirrorRecord)
forall a b. a -> Either a b
Left MirrorStateFault
fault)
Right Maybe Int
retained -> do
StateCharge
charge (Integer -> Int
saturatingCharge (Integer
2 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* (Int -> Integer
forall a. Integral a => a -> Integer
toInteger Int
inputWeight Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer -> (Int -> Integer) -> Maybe Int -> Integer
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Integer
0 Int -> Integer
forall a. Integral a => a -> Integer
toInteger Maybe Int
retained) Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
4096))
case Maybe ProjectMirrorRecord
-> Either MirrorStateFault ProjectMirrorRecord
change Maybe ProjectMirrorRecord
previous of
Left MirrorStateFault
fault -> Either MirrorStateFault (StateRevision, ProjectMirrorRecord)
-> IO
(Either MirrorStateFault (StateRevision, ProjectMirrorRecord))
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (MirrorStateFault
-> Either MirrorStateFault (StateRevision, ProjectMirrorRecord)
forall a b. a -> Either a b
Left MirrorStateFault
fault)
Right ProjectMirrorRecord
updated -> case Maybe StateRevision
revision of
Just StateRevision
current | Maybe ProjectMirrorRecord
previous Maybe ProjectMirrorRecord -> Maybe ProjectMirrorRecord -> Bool
forall a. Eq a => a -> a -> Bool
== ProjectMirrorRecord -> Maybe ProjectMirrorRecord
forall a. a -> Maybe a
Just ProjectMirrorRecord
updated -> Either MirrorStateFault (StateRevision, ProjectMirrorRecord)
-> IO
(Either MirrorStateFault (StateRevision, ProjectMirrorRecord))
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((StateRevision, ProjectMirrorRecord)
-> Either MirrorStateFault (StateRevision, ProjectMirrorRecord)
forall a b. b -> Either a b
Right (StateRevision
current, ProjectMirrorRecord
updated))
Maybe StateRevision
_ ->
MirrorState
-> StateCharge
-> PackageName
-> Maybe StateRevision
-> ProjectMirrorRecord
-> IO (Either MirrorStateFault StateWrite)
msWriteProject MirrorState
store StateCharge
charge PackageName
name Maybe StateRevision
revision ProjectMirrorRecord
updated IO (Either MirrorStateFault StateWrite)
-> (Either MirrorStateFault StateWrite
-> IO
(Either MirrorStateFault (StateRevision, ProjectMirrorRecord)))
-> IO
(Either MirrorStateFault (StateRevision, ProjectMirrorRecord))
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Right (StateStored StateRevision
current) -> Either MirrorStateFault (StateRevision, ProjectMirrorRecord)
-> IO
(Either MirrorStateFault (StateRevision, ProjectMirrorRecord))
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((StateRevision, ProjectMirrorRecord)
-> Either MirrorStateFault (StateRevision, ProjectMirrorRecord)
forall a b. b -> Either a b
Right (StateRevision
current, ProjectMirrorRecord
updated))
Right StateWrite
StateConflict -> t
-> MirrorStateFault
-> IO
(Either MirrorStateFault (StateRevision, ProjectMirrorRecord))
go (t
attempts t -> t -> t
forall a. Num a => a -> a -> a
- t
1) MirrorStateFault
StateRetryExhausted
Left MirrorStateFault
fault -> t
-> MirrorStateFault
-> IO
(Either MirrorStateFault (StateRevision, ProjectMirrorRecord))
go (t
attempts t -> t -> t
forall a. Num a => a -> a -> a
- t
1) MirrorStateFault
fault