Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

Commit

Permalink
[CAD-2178] The retryCount from the /errors endpoint is not correctly …
Browse files Browse the repository at this point in the history
…incremented.
  • Loading branch information
ksaric committed Dec 1, 2020
1 parent 03d477f commit 4983433
Show file tree
Hide file tree
Showing 10 changed files with 244 additions and 99 deletions.
1 change: 1 addition & 0 deletions smash-servant-types/smash-servant-types.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ library
, swagger2
, text
, time
, quiet
, wai

default-language: Haskell2010
Expand Down
20 changes: 16 additions & 4 deletions smash-servant-types/src/Cardano/SMASH/DBSync/Db/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE GeneralisedNewtypeDeriving #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE DerivingVia #-}

module Cardano.SMASH.DBSync.Db.Types where

Expand All @@ -17,13 +18,16 @@ import Cardano.Api.Typed hiding (PoolId)
import qualified Data.ByteString.Base16 as B16
import qualified Data.ByteString.Char8 as BSC

import Quiet (Quiet (..))

-- | The stake pool identifier. It is the hash of the stake pool operator's
-- vkey.
--
-- It may be rendered as hex or as bech32 using the @pool@ prefix.
--
newtype PoolId = PoolId { getPoolId :: Text }
deriving stock (Eq, Show, Ord, Generic)
deriving stock (Eq, Ord, Generic)
deriving Show via (Quiet PoolId)
deriving newtype PersistField

instance ToJSON PoolId where
Expand Down Expand Up @@ -71,7 +75,8 @@ parsePoolId poolId =
-- It may be rendered as hex.
--
newtype PoolMetadataHash = PoolMetadataHash { getPoolMetadataHash :: Text }
deriving stock (Eq, Show, Ord, Generic)
deriving stock (Eq, Ord, Generic)
deriving Show via (Quiet PoolMetadataHash)
deriving newtype PersistField

instance ToJSON PoolMetadataHash where
Expand All @@ -87,6 +92,11 @@ instance FromJSON PoolMetadataHash where
poolHash <- o .: "poolHash"
return $ PoolMetadataHash poolHash

-- Converting the basic type to a strong one.
-- Presumes the user knows what he is doing, NOT TYPE SAFE!
bytestringToPoolMetaHash :: ByteString -> PoolMetadataHash
bytestringToPoolMetaHash bs = PoolMetadataHash . decodeUtf8 . B16.encode $ bs

-- | The stake pool metadata. It is JSON format. This type represents it in
-- its raw original form. The hash of this content is the 'PoolMetadataHash'.
newtype PoolMetadataRaw = PoolMetadataRaw { getPoolMetadata :: Text }
Expand All @@ -95,12 +105,14 @@ newtype PoolMetadataRaw = PoolMetadataRaw { getPoolMetadata :: Text }

-- | The pool url wrapper so we have some additional safety.
newtype PoolUrl = PoolUrl { getPoolUrl :: Text }
deriving stock (Eq, Show, Ord, Generic)
deriving stock (Eq, Ord, Generic)
deriving Show via (Quiet PoolUrl)
deriving newtype PersistField

-- | The ticker name wrapper so we have some additional safety.
newtype TickerName = TickerName { getTickerName :: Text }
deriving stock (Eq, Show, Ord, Generic)
deriving stock (Eq, Ord, Generic)
deriving Show via (Quiet TickerName)
deriving newtype PersistField

instance ToJSON TickerName where
Expand Down
1 change: 1 addition & 0 deletions smash-servant-types/src/Cardano/SMASH/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module Cardano.SMASH.Types
, PoolId (..)
, PoolUrl (..)
, PoolMetadataHash (..)
, bytestringToPoolMetaHash
, PoolMetadataRaw (..)
, TickerName (..)
-- * Wrapper
Expand Down
4 changes: 4 additions & 0 deletions smash/smash.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ test-suite smash-test
build-depends:
base >=4.7 && <5
, cardano-prelude
, iohk-monitoring
, containers
, ed25519
, hspec
Expand All @@ -176,6 +177,7 @@ test-suite smash-test
, smash
, smash-servant-types
, tree-diff
, transformers-except

default-language: Haskell2010
default-extensions:
Expand Down Expand Up @@ -214,6 +216,8 @@ test-suite db-spec-test
, smash-servant-types
, iohk-monitoring
, tree-diff
, time
, transformers-except

default-language: Haskell2010
default-extensions:
Expand Down
1 change: 1 addition & 0 deletions smash/src/Cardano/SMASH/DBSyncPlugin.hs
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,4 @@ insertPoolRegister dataLayer tracer params = do

liftIO . logInfo tracer $ "Inserted pool register."
pure ()

17 changes: 10 additions & 7 deletions smash/src/Cardano/SMASH/FetchQueue.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Cardano.SMASH.FetchQueue
( FetchQueue -- opaque
, PoolFetchRetry (..)
, Retry -- opaque
, Retry (..)
, retryCount
, emptyFetchQueue
, lenFetchQueue
Expand All @@ -10,6 +10,7 @@ module Cardano.SMASH.FetchQueue
, partitionFetchQueue
, newRetry
, nextRetry
, countedRetry
) where


Expand All @@ -20,7 +21,8 @@ import qualified Data.Map.Strict as Map
import Data.Time.Clock.POSIX (POSIXTime)

import Cardano.SMASH.DBSync.Db.Schema (PoolMetadataReferenceId)
import Cardano.SMASH.DBSync.Db.Types (PoolId)
import Cardano.SMASH.DBSync.Db.Types (PoolId, PoolMetadataHash,
PoolUrl)

import Cardano.SMASH.FetchQueue.Retry

Expand All @@ -29,15 +31,16 @@ import Cardano.SMASH.FetchQueue.Retry
-- Figuring out how to use an existing priority queue for this task would be more time
-- consuming that writing this from scratch.

newtype FetchQueue = FetchQueue (Map Text PoolFetchRetry)
newtype FetchQueue = FetchQueue (Map PoolUrl PoolFetchRetry)
deriving (Show)

data PoolFetchRetry = PoolFetchRetry
{ pfrReferenceId :: !PoolMetadataReferenceId
, pfrPoolIdWtf :: !PoolId
, pfrPoolUrl :: !Text
, pfrPoolMDHash :: !ByteString
, pfrPoolUrl :: !PoolUrl
, pfrPoolMDHash :: !PoolMetadataHash
, pfrRetry :: !Retry
}
} deriving (Show)

emptyFetchQueue :: FetchQueue
emptyFetchQueue = FetchQueue mempty
Expand All @@ -52,7 +55,7 @@ insertFetchQueue :: [PoolFetchRetry] -> FetchQueue -> FetchQueue
insertFetchQueue xs (FetchQueue mp) =
FetchQueue $ Map.union mp (Map.fromList $ map build xs)
where
build :: PoolFetchRetry -> (Text, PoolFetchRetry)
build :: PoolFetchRetry -> (PoolUrl, PoolFetchRetry)
build pfr = (pfrPoolUrl pfr, pfr)

partitionFetchQueue :: FetchQueue -> POSIXTime -> ([PoolFetchRetry], FetchQueue)
Expand Down
9 changes: 9 additions & 0 deletions smash/src/Cardano/SMASH/FetchQueue/Retry.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Cardano.SMASH.FetchQueue.Retry
( Retry (..)
, newRetry
, nextRetry
, countedRetry
) where


Expand Down Expand Up @@ -32,6 +33,14 @@ newRetry now =
, retryCount = 0
}

countedRetry :: Retry -> Retry
countedRetry retry =
Retry
{ retryWhen = retryWhen retry
, retryNext = retryNext retry
, retryCount = retryCount retry + 1
}

-- Update a Retry with an exponential (* 3) backoff.
nextRetry :: POSIXTime -> Retry -> Retry
nextRetry now r =
Expand Down
Loading

0 comments on commit 4983433

Please sign in to comment.