Skip to content
This repository was archived by the owner on Aug 18, 2020. It is now read-only.

Commit ac9f0cb

Browse files
authored
Merge pull request #3202 from input-output-hk/mhuesch/CDEC-416-0
[CDEC-416] Move several modules from `infra` to `sinbin`
2 parents 909568d + b6cac56 commit ac9f0cb

File tree

35 files changed

+827
-761
lines changed

35 files changed

+827
-761
lines changed

block/src/Pos/Block/BlockWorkMode.hs

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ import Pos.Block.Types (LastKnownHeader, LastKnownHeaderTag,
2626
import Pos.Core.Context (HasPrimaryKey)
2727
import Pos.Infra.Communication.Protocol (Message)
2828
import Pos.Infra.Recovery.Info (MonadRecoveryInfo)
29-
import Pos.Infra.Shutdown.Class (HasShutdownContext)
30-
import Pos.Infra.StateLock (StateLock, StateLockMetrics)
31-
import Pos.Infra.Util.TimeWarp (CanJsonLog)
3229
import Pos.Security.Params (SecurityParams)
30+
import Pos.Sinbin.Shutdown.Class (HasShutdownContext)
31+
import Pos.Sinbin.StateLock (StateLock, StateLockMetrics)
3332
import Pos.Sinbin.Util.JsonLog.Events (MemPoolModifyReason)
33+
import Pos.Sinbin.Util.TimeWarp (CanJsonLog)
3434
import Pos.Txp (GenericTxpLocalData, MempoolExt, MonadTxpLocal,
3535
TxpHolderTag)
3636
import Pos.Update.Context (UpdateContext)

block/src/Pos/Block/Logic/Creation.hs

+3-3
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@ import Pos.DB.Class (MonadDBRead)
4949
import Pos.Delegation (DelegationVar, DlgPayload (..),
5050
ProxySKBlockInfo, clearDlgMemPool, getDlgMempool)
5151
import Pos.Exception (assertionFailed, reportFatalError)
52-
import Pos.Infra.StateLock (Priority (..), StateLock,
53-
StateLockMetrics, modifyStateLock)
54-
import Pos.Infra.Util.LogSafe (logInfoS)
5552
import Pos.Lrc (HasLrcContext)
5653
import Pos.Lrc.Context (lrcActionOnEpochReason)
5754
import qualified Pos.Lrc.DB as LrcDB
5855
import Pos.Sinbin.Reporting (HasMisbehaviorMetrics, reportError)
56+
import Pos.Sinbin.StateLock (Priority (..), StateLock,
57+
StateLockMetrics, modifyStateLock)
5958
import Pos.Sinbin.Util.JsonLog.Events (MemPoolModifyReason (..))
59+
import Pos.Sinbin.Util.LogSafe (logInfoS)
6060
import Pos.Ssc.Base (defaultSscPayload, stripSscPayload)
6161
import Pos.Ssc.Logic (sscGetLocalPayload)
6262
import Pos.Ssc.Mem (MonadSscMem)

block/src/Pos/Block/Lrc.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ import Pos.DB.Class (MonadDBRead, MonadGState)
3838
import qualified Pos.DB.GState.Stakes as GS (getRealStake, getRealTotalStake)
3939
import Pos.Delegation (getDelegators, isIssuerByAddressHash)
4040
import qualified Pos.GState.SanityCheck as DB (sanityCheckDB)
41-
import Pos.Infra.Util.TimeLimit (logWarningWaitLinear)
4241
import Pos.Lrc.Consumer (LrcConsumer (..))
4342
import Pos.Lrc.Consumers (allLrcConsumers)
4443
import Pos.Lrc.Context (LrcContext (lcLrcSync), LrcSyncData (..))
@@ -53,6 +52,7 @@ import Pos.Lrc.Types (RichmenStakes)
5352
import Pos.Sinbin.Reporting (HasMisbehaviorMetrics (..),
5453
MisbehaviorMetrics (..))
5554
import Pos.Sinbin.Slotting (MonadSlots)
55+
import Pos.Sinbin.Util.TimeLimit (logWarningWaitLinear)
5656
import Pos.Ssc (MonadSscMem, noReportNoSecretsForEpoch1,
5757
sscCalculateSeed)
5858
import Pos.Ssc.Message (SscMessageConstraints)

block/src/Pos/Block/Network/Logic.hs

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ import Pos.Infra.Diffusion.Types (Diffusion)
5050
import qualified Pos.Infra.Diffusion.Types as Diffusion
5151
(Diffusion (announceBlockHeader, requestTip))
5252
import Pos.Infra.Recovery.Info (recoveryInProgress)
53-
import Pos.Infra.StateLock (Priority (..), modifyStateLock)
54-
import Pos.Infra.Util.TimeWarp (CanJsonLog (..))
5553
import Pos.Sinbin.Reporting (HasMisbehaviorMetrics (..),
5654
MisbehaviorMetrics (..))
55+
import Pos.Sinbin.StateLock (Priority (..), modifyStateLock)
5756
import Pos.Sinbin.Util.JsonLog.Events (MemPoolModifyReason (..),
5857
jlAdoptedBlock)
58+
import Pos.Sinbin.Util.TimeWarp (CanJsonLog (..))
5959
import Pos.Util (buildListBounds, multilineBounds, _neLast)
6060
import Pos.Util.AssertMode (inAssertMode)
6161
import Pos.Util.Util (lensOf)

block/src/Pos/Block/Worker.hs

+3-3
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,6 @@ import qualified Pos.Infra.Diffusion.Types as Diffusion
5454
import Pos.Infra.Recovery.Info (getSyncStatus, getSyncStatusK,
5555
needTriggerRecovery, recoveryCommGuard)
5656
import Pos.Infra.Slotting (getSlotStartEmpatically, onNewSlot)
57-
import Pos.Infra.Util.LogSafe (logDebugS, logInfoS, logWarningS)
58-
import Pos.Infra.Util.TimeLimit (logWarningSWaitLinear)
59-
import Pos.Infra.Util.TimeWarp (CanJsonLog (..))
6057
import qualified Pos.Lrc.DB as LrcDB (getLeadersForEpoch)
6158
import Pos.Sinbin.Reporting (HasMisbehaviorMetrics,
6259
MetricMonitor (..), MetricMonitorState, noReportMonitor,
@@ -66,6 +63,9 @@ import Pos.Sinbin.Slotting (ActionTerminationPolicy (..),
6663
OnNewSlotParams (..), currentTimeSlotting,
6764
defaultOnNewSlotParams)
6865
import Pos.Sinbin.Util.JsonLog.Events (jlCreatedBlock)
66+
import Pos.Sinbin.Util.LogSafe (logDebugS, logInfoS, logWarningS)
67+
import Pos.Sinbin.Util.TimeLimit (logWarningSWaitLinear)
68+
import Pos.Sinbin.Util.TimeWarp (CanJsonLog (..))
6969

7070
import Pos.Update.DB (getAdoptedBVData)
7171

delegation/src/Pos/Delegation/Listeners.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ import Pos.Delegation.Logic (PskHeavyVerdict (..),
2424
processProxySKHeavy)
2525
import Pos.Infra.Communication.Protocol (Message)
2626
import Pos.Infra.Communication.Relay (DataMsg)
27-
import Pos.Infra.StateLock (StateLock)
2827
import Pos.Lrc.Context (HasLrcContext)
28+
import Pos.Sinbin.StateLock (StateLock)
2929
import Pos.Util (HasLens')
3030

3131
-- Message constraints we need to be defined.

delegation/src/Pos/Delegation/Logic/Mempool.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ import Pos.Delegation.Class (DlgMemPool, MonadDelegation,
3939
import Pos.Delegation.Logic.Common (DelegationStateAction,
4040
runDelegationStateAction)
4141
import Pos.Delegation.Types (DlgPayload (..), isRevokePsk)
42-
import Pos.Infra.StateLock (StateLock, withStateLockNoMetrics)
4342
import Pos.Lrc.Consumer.Delegation (getDlgRichmen)
4443
import Pos.Lrc.Context (HasLrcContext)
44+
import Pos.Sinbin.StateLock (StateLock, withStateLockNoMetrics)
4545
import Pos.Util (HasLens', microsecondsToUTC)
4646
import Pos.Util.Concurrent.PriorityLock (Priority (..))
4747

delegation/src/Pos/Delegation/Worker.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ import Pos.Delegation.Configuration (HasDlgConfiguration,
1818
import Pos.Delegation.Logic (DelegationStateAction,
1919
runDelegationStateAction)
2020
import Pos.Infra.Diffusion.Types (Diffusion)
21-
import Pos.Infra.Shutdown (HasShutdownContext)
2221
import Pos.Sinbin.Reporting (MonadReporting, reportOrLogE)
22+
import Pos.Sinbin.Shutdown (HasShutdownContext)
2323
import Pos.Util (microsecondsToUTC)
2424
import Pos.Util.LRU (filterLRU)
2525

infra/src/Pos/Infra/Shutdown.hs

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
module Pos.Infra.Shutdown
2-
( module Pos.Infra.Shutdown.Class
3-
, module Pos.Infra.Shutdown.Logic
4-
, module Pos.Infra.Shutdown.Types
2+
( module X
53
) where
64

7-
import Pos.Infra.Shutdown.Class
8-
import Pos.Infra.Shutdown.Logic
9-
import Pos.Infra.Shutdown.Types
5+
import Pos.Sinbin.Shutdown as X

infra/src/Pos/Infra/Shutdown/Class.hs

+2-13
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
1-
{-# LANGUAGE TypeFamilies #-}
2-
31
module Pos.Infra.Shutdown.Class
4-
( HasShutdownContext(..)
2+
( module X
53
) where
64

7-
import Universum
8-
9-
import Control.Lens.Lens (lens)
10-
import Pos.Infra.Shutdown.Types (ShutdownContext)
11-
12-
class HasShutdownContext ctx where
13-
shutdownContext :: Lens' ctx ShutdownContext
14-
15-
instance HasShutdownContext ShutdownContext where
16-
shutdownContext = lens identity (\_ x -> x)
5+
import Pos.Sinbin.Shutdown.Class as X

infra/src/Pos/Infra/Shutdown/Logic.hs

+2-21
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,5 @@
11
module Pos.Infra.Shutdown.Logic
2-
( triggerShutdown
3-
, waitForShutdown
2+
( module X
43
) where
54

6-
import Universum
7-
8-
import Control.Concurrent.STM (check, readTVar, writeTVar)
9-
import System.Wlog (WithLogger, logInfo)
10-
11-
import Pos.Infra.Shutdown.Class (HasShutdownContext (..))
12-
import Pos.Infra.Shutdown.Types (ShutdownContext (..),
13-
shdnIsTriggered)
14-
15-
triggerShutdown
16-
:: (MonadIO m, MonadReader ctx m, WithLogger m, HasShutdownContext ctx)
17-
=> m ()
18-
triggerShutdown = do
19-
logInfo "NODE SHUTDOWN TRIGGERED, WAITING FOR WORKERS TO TERMINATE"
20-
view (shutdownContext . shdnIsTriggered) >>= atomically . flip writeTVar True
21-
22-
-- | Wait for the shutdown var to be true.
23-
waitForShutdown :: ShutdownContext -> IO ()
24-
waitForShutdown (ShutdownContext v) = atomically (readTVar v >>= check)
5+
import Pos.Sinbin.Shutdown.Logic as X

infra/src/Pos/Infra/Shutdown/Types.hs

+2-14
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
1-
{-# LANGUAGE TemplateHaskell #-}
2-
31
module Pos.Infra.Shutdown.Types
4-
( ShutdownContext (..)
5-
, shdnIsTriggered
2+
( module X
63
) where
74

8-
import Universum
9-
10-
import Control.Lens (makeLenses)
11-
12-
data ShutdownContext = ShutdownContext
13-
{ _shdnIsTriggered :: !(TVar Bool)
14-
-- ^ If this flag is `True`, then workers should stop.
15-
}
16-
17-
makeLenses ''ShutdownContext
5+
import Pos.Sinbin.Shutdown.Types as X

infra/src/Pos/Infra/StateLock.hs

+2-155
Original file line numberDiff line numberDiff line change
@@ -1,158 +1,5 @@
1-
{-# LANGUAGE RankNTypes #-}
2-
{-# LANGUAGE TypeApplications #-}
3-
{-|
4-
Module: Pos.Infra.StateLock
5-
Description: A lock on the local state of a node
6-
7-
Provides a lock that is to be taken while modifying, or requiring a
8-
consistent view on, the local state (the database and mempool).
9-
10-
It collects metrics on how long a given action waits on the lock, and
11-
how long the action takes.
12-
-}
131
module Pos.Infra.StateLock
14-
( Priority (..)
15-
, StateLock (..)
16-
, newEmptyStateLock
17-
, newStateLock
18-
19-
, StateLockMetrics (..)
20-
, ignoreStateLockMetrics
21-
22-
, modifyStateLock
23-
, withStateLock
24-
, withStateLockNoMetrics
2+
( module X
253
) where
264

27-
import Universum
28-
29-
import Control.Monad.Catch (MonadMask)
30-
import Data.Aeson.Types (ToJSON (..), Value)
31-
import Data.Time.Units (Microsecond)
32-
import JsonLog (CanJsonLog (..))
33-
import Mockable (CurrentTime, Mockable, currentTime)
34-
import System.Mem (getAllocationCounter)
35-
import System.Wlog (LoggerNameBox, WithLogger, askLoggerName,
36-
usingLoggerName)
37-
38-
import Pos.Core (HeaderHash)
39-
import Pos.Util.Concurrent (modifyMVar, withMVar)
40-
import Pos.Util.Concurrent.PriorityLock (Priority (..), PriorityLock,
41-
newPriorityLock, withPriorityLock)
42-
import Pos.Util.Util (HasLens', lensOf)
43-
44-
45-
-- | A simple wrapper over 'MVar' which stores 'HeaderHash' (our
46-
-- current tip) and is taken whenever we want to update GState or
47-
-- other data dependent on GState.
48-
data StateLock = StateLock
49-
{ slTip :: !(MVar HeaderHash)
50-
, slLock :: !PriorityLock
51-
}
52-
53-
-- | Create empty (i. e. locked) 'StateLock'.
54-
newEmptyStateLock :: MonadIO m => m StateLock
55-
newEmptyStateLock = StateLock <$> newEmptyMVar <*> newPriorityLock
56-
57-
-- | Create a 'StateLock' with given tip.
58-
newStateLock :: MonadIO m => HeaderHash -> m StateLock
59-
newStateLock tip = StateLock <$> newMVar tip <*> newPriorityLock
60-
61-
-- | Effectful getters and setters for metrics related to the actions
62-
-- which use 'StateLock'.
63-
--
64-
-- TODO this should not be fixed at IO, if being able to mock features
65-
-- remains a goal. But we can't free it up right now because the current
66-
-- mockable system doesn't work well with ether.
67-
--
68-
-- TODO: make it type class if we want to use other things we have
69-
-- in application and to be more consistent with other code. @gromak
70-
data StateLockMetrics slr = StateLockMetrics
71-
{ -- | Called when a thread begins to wait to modify the mempool.
72-
-- Parameter is the reason for modifying the mempool.
73-
slmWait :: !(slr -> LoggerNameBox IO ())
74-
-- | Called when a thread is granted the lock on the mempool. Parameter
75-
-- indicates how long it waited.
76-
, slmAcquire :: !(slr -> Microsecond -> LoggerNameBox IO ())
77-
-- | Called when a thread is finished modifying the mempool and has
78-
-- released the lock. Parameters indicates time elapsed since acquiring
79-
-- the lock, and new mempool size.
80-
, slmRelease :: !(slr -> Microsecond -> Microsecond -> Int64 -> LoggerNameBox IO Value)
81-
}
82-
83-
-- | A 'StateLockMetrics' that never does any writes. Use it if you
84-
-- don't care about metrics.
85-
ignoreStateLockMetrics :: StateLockMetrics ()
86-
ignoreStateLockMetrics = StateLockMetrics
87-
{ slmWait = \_ -> pure ()
88-
, slmAcquire = \_ _ -> pure ()
89-
, slmRelease = \_ _ _ _ -> pure (toJSON ())
90-
}
91-
92-
type MonadStateLockBase ctx m
93-
= ( MonadIO m
94-
, MonadMask m
95-
, MonadReader ctx m
96-
, HasLens' ctx StateLock
97-
)
98-
99-
type MonadStateLock ctx slr m
100-
= ( MonadStateLockBase ctx m
101-
, WithLogger m
102-
, Mockable CurrentTime m
103-
, HasLens' ctx (StateLockMetrics slr)
104-
, CanJsonLog m
105-
)
106-
107-
-- | Run an action acquiring 'StateLock' lock. Argument of
108-
-- action is an old tip, result is put as a new tip.
109-
modifyStateLock :: forall ctx slr m a.
110-
MonadStateLock ctx slr m
111-
=> Priority
112-
-> slr
113-
-> (HeaderHash -> m (HeaderHash, a))
114-
-> m a
115-
modifyStateLock = stateLockHelper modifyMVar
116-
117-
-- | Run an action acquiring 'StateLock' lock without modifying tip.
118-
withStateLock ::
119-
MonadStateLock ctx slr m => Priority -> slr -> (HeaderHash -> m a) -> m a
120-
withStateLock = stateLockHelper withMVar
121-
122-
-- | Version of 'withStateLock' that does not gather metrics
123-
withStateLockNoMetrics ::
124-
MonadStateLockBase ctx m => Priority -> (HeaderHash -> m a) -> m a
125-
withStateLockNoMetrics prio action = do
126-
StateLock mvar prioLock <- view (lensOf @StateLock)
127-
withPriorityLock prioLock prio $ withMVar mvar action
128-
129-
stateLockHelper :: forall ctx slr m a b.
130-
MonadStateLock ctx slr m
131-
=> (MVar HeaderHash -> (HeaderHash -> m b) -> m a)
132-
-> Priority
133-
-> slr
134-
-> (HeaderHash -> m b)
135-
-> m a
136-
stateLockHelper doWithMVar prio reason action = do
137-
StateLock mvar prioLock <- view (lensOf @StateLock)
138-
StateLockMetrics {..} <- view (lensOf @(StateLockMetrics slr))
139-
lname <- askLoggerName
140-
liftIO . usingLoggerName lname $ slmWait reason
141-
timeBeginWait <- currentTime
142-
withPriorityLock prioLock prio $ doWithMVar mvar $ \hh -> do
143-
timeEndWait <- currentTime
144-
liftIO . usingLoggerName lname $
145-
slmAcquire reason (timeEndWait - timeBeginWait)
146-
timeBeginModify <- currentTime
147-
memBeginModify <- liftIO getAllocationCounter
148-
res <- action hh
149-
timeEndModify <- currentTime
150-
memEndModify <- liftIO getAllocationCounter
151-
json <- liftIO . usingLoggerName lname $ slmRelease
152-
reason
153-
(timeEndWait - timeBeginWait)
154-
(timeEndModify - timeBeginModify)
155-
-- counter counts "down" memory that has been allocated by the thread
156-
(memBeginModify - memEndModify)
157-
jsonLog json
158-
pure res
5+
import Pos.Sinbin.StateLock as X

0 commit comments

Comments
 (0)