/
Exit.hs
152 lines (129 loc) · 5.47 KB
/
Exit.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Ouroboros.Consensus.Node.Exit
( -- * ExitFailure
ExitFailure
, exitReasontoExitFailure
-- * ExitReason
, ExitReason (..)
, toExitReason
) where
import Control.Exception (AsyncException (..), SomeException,
fromException)
import Control.Monad.Class.MonadAsync (ExceptionInLinkedThread (..))
import Ouroboros.Storage.ChainDB.API (ChainDbFailure (..))
import Ouroboros.Storage.FS.API.Types (FsError (..), FsErrorType (..))
import Ouroboros.Storage.ImmutableDB.Types (ImmutableDBError)
import qualified Ouroboros.Storage.ImmutableDB.Types as ImmDB
import Ouroboros.Storage.VolatileDB.Types (VolatileDBError)
import qualified Ouroboros.Storage.VolatileDB.Types as VolDB
import Ouroboros.Consensus.Node.DbMarker (DbMarkerError)
import Ouroboros.Consensus.Node.ProtocolInfo.Byron
(PBftLeaderCredentialsError)
{-------------------------------------------------------------------------------
ExitFailure
-------------------------------------------------------------------------------}
-- | The exit code to return when terminating with an exception.
--
-- To be used in the @ExitFailure@ constructor of 'System.Exit.ExitCode'.
--
-- Note that a node will never turn shut down itself, it is meant to run
-- forever, so it will always terminate with an 'ExitFailure'.
type ExitFailure = Int
-- | Convert an 'ExitReason' to an 'ExitFailure'.
exitReasontoExitFailure :: ExitReason -> ExitFailure
exitReasontoExitFailure = \case
-- Some action should be taken before restarting in the cases below.
ConfigurationError -> 3
WrongDatabase -> 4
DiskFull -> 5
InsufficientPermissions -> 6
NoNetwork -> 7
-- The node can simply be restarted in the cases below.
--
-- NOTE: Database corruption is handled automically: when the node is
-- restarted, it will do a full validation pass.
Killed -> 1
DatabaseCorruption -> 2
Other -> 2
{-------------------------------------------------------------------------------
ExitReason
-------------------------------------------------------------------------------}
-- | The reason of shutting down
data ExitReason =
-- ^ The node process was killed, by the @kill@ command, @CTRL-C@ or some
-- other means. This is normal way for a user to terminate the node
-- process. The node can simply be restarted.
Killed
-- | Something is wrong with the node configuration, the user should check it.
--
-- For example, for PBFT, it could be that the block signing key and the
-- delegation certificate do not match.
| ConfigurationError
-- | We were unable to open the database, probably the user is using the
-- wrong directory. See 'DbMarkerError' for details.
| WrongDatabase
-- | The disk is full, make some space before restarting the node.
| DiskFull
-- | The database folder doesn't have the right permissions.
| InsufficientPermissions
-- | There is a problem with the network connection, the user should
-- investigate.
--
-- TODO We're not yet returning this.
| NoNetwork
-- | Something went wrong with the database, restart the node with
-- recovery enabled.
| DatabaseCorruption
-- | Some exception was thrown. The node should just be restarted.
| Other
-- | Return the 'ExitReason' for the given 'SomeException'. Defaults to
-- 'Other'.
toExitReason :: SomeException -> ExitReason
toExitReason e
| Just (e' :: AsyncException) <- fromException e
= case e' of
ThreadKilled -> Killed
UserInterrupt -> Killed
_ -> Other
| Just (ExceptionInLinkedThread _ e') <- fromException e
= toExitReason e'
| Just (_ :: DbMarkerError) <- fromException e
= WrongDatabase
| Just (e' :: ChainDbFailure) <- fromException e
= case e' of
ImmDbFailure ue -> immDbUnexpectedError ue
VolDbFailure ue -> volDbUnexpectedError ue
LgrDbFailure fe -> fsError fe
_ -> DatabaseCorruption
| Just (_ :: PBftLeaderCredentialsError) <- fromException e
= ConfigurationError
-- The three exceptions below will always be wrapped in a
-- 'ChainDbFailure', but we include them just in case.
| Just (e' :: ImmutableDBError) <- fromException e
= case e' of
ImmDB.UnexpectedError ue -> immDbUnexpectedError ue
_ -> Other
| Just (e' :: VolatileDBError) <- fromException e
= case e' of
VolDB.UnexpectedError ue -> volDbUnexpectedError ue
_ -> Other
| Just (e' :: FsError) <- fromException e
= fsError e'
| otherwise
= Other
where
immDbUnexpectedError :: ImmDB.UnexpectedError -> ExitReason
immDbUnexpectedError = \case
ImmDB.FileSystemError fe -> fsError fe
_ -> DatabaseCorruption
volDbUnexpectedError :: VolDB.UnexpectedError -> ExitReason
volDbUnexpectedError = \case
VolDB.FileSystemError fe -> fsError fe
_ -> DatabaseCorruption
fsError :: FsError -> ExitReason
fsError FsError { fsErrorType } = case fsErrorType of
FsDeviceFull -> DiskFull
FsInsufficientPermissions -> InsufficientPermissions
_ -> DatabaseCorruption