Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / HTools / Types.hs @ 6d3d13ab

History | View | Annotate | Download (13.9 kB)

1 e9aaa3c6 Iustin Pop
{-# LANGUAGE TemplateHaskell #-}
2 e9aaa3c6 Iustin Pop
3 e4c5beaf Iustin Pop
{-| Some common types.
4 e4c5beaf Iustin Pop
5 e4c5beaf Iustin Pop
-}
6 e4c5beaf Iustin Pop
7 e2fa2baf Iustin Pop
{-
8 e2fa2baf Iustin Pop
9 e8fa4ff6 Iustin Pop
Copyright (C) 2009, 2010, 2011, 2012 Google Inc.
10 e2fa2baf Iustin Pop
11 e2fa2baf Iustin Pop
This program is free software; you can redistribute it and/or modify
12 e2fa2baf Iustin Pop
it under the terms of the GNU General Public License as published by
13 e2fa2baf Iustin Pop
the Free Software Foundation; either version 2 of the License, or
14 e2fa2baf Iustin Pop
(at your option) any later version.
15 e2fa2baf Iustin Pop
16 e2fa2baf Iustin Pop
This program is distributed in the hope that it will be useful, but
17 e2fa2baf Iustin Pop
WITHOUT ANY WARRANTY; without even the implied warranty of
18 e2fa2baf Iustin Pop
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 e2fa2baf Iustin Pop
General Public License for more details.
20 e2fa2baf Iustin Pop
21 e2fa2baf Iustin Pop
You should have received a copy of the GNU General Public License
22 e2fa2baf Iustin Pop
along with this program; if not, write to the Free Software
23 e2fa2baf Iustin Pop
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 e2fa2baf Iustin Pop
02110-1301, USA.
25 e2fa2baf Iustin Pop
26 e2fa2baf Iustin Pop
-}
27 e2fa2baf Iustin Pop
28 e4c5beaf Iustin Pop
module Ganeti.HTools.Types
29 ebf38064 Iustin Pop
  ( Idx
30 ebf38064 Iustin Pop
  , Ndx
31 ebf38064 Iustin Pop
  , Gdx
32 ebf38064 Iustin Pop
  , NameAssoc
33 ebf38064 Iustin Pop
  , Score
34 ebf38064 Iustin Pop
  , Weight
35 ebf38064 Iustin Pop
  , GroupID
36 f3f76ccc Iustin Pop
  , defaultGroupID
37 ebf38064 Iustin Pop
  , AllocPolicy(..)
38 ebf38064 Iustin Pop
  , allocPolicyFromRaw
39 ebf38064 Iustin Pop
  , allocPolicyToRaw
40 ebf38064 Iustin Pop
  , InstanceStatus(..)
41 ebf38064 Iustin Pop
  , instanceStatusFromRaw
42 ebf38064 Iustin Pop
  , instanceStatusToRaw
43 ebf38064 Iustin Pop
  , RSpec(..)
44 76a20994 Iustin Pop
  , AllocInfo(..)
45 80d7d8a1 Iustin Pop
  , AllocStats
46 ebf38064 Iustin Pop
  , DynUtil(..)
47 ebf38064 Iustin Pop
  , zeroUtil
48 ebf38064 Iustin Pop
  , baseUtil
49 ebf38064 Iustin Pop
  , addUtil
50 ebf38064 Iustin Pop
  , subUtil
51 ebf38064 Iustin Pop
  , defReservedDiskRatio
52 ebf38064 Iustin Pop
  , unitMem
53 ebf38064 Iustin Pop
  , unitCpu
54 ebf38064 Iustin Pop
  , unitDsk
55 ebf38064 Iustin Pop
  , unknownField
56 ebf38064 Iustin Pop
  , Placement
57 ebf38064 Iustin Pop
  , IMove(..)
58 ebf38064 Iustin Pop
  , DiskTemplate(..)
59 ebf38064 Iustin Pop
  , diskTemplateToRaw
60 ebf38064 Iustin Pop
  , diskTemplateFromRaw
61 b906d7c1 Iustin Pop
  , MirrorType(..)
62 b906d7c1 Iustin Pop
  , templateMirrorType
63 ebf38064 Iustin Pop
  , MoveJob
64 ebf38064 Iustin Pop
  , JobSet
65 ebf38064 Iustin Pop
  , Element(..)
66 ebf38064 Iustin Pop
  , FailMode(..)
67 ebf38064 Iustin Pop
  , FailStats
68 a8038349 Iustin Pop
  , OpResult
69 ebf38064 Iustin Pop
  , opToResult
70 ebf38064 Iustin Pop
  , EvacMode(..)
71 a07343b2 Iustin Pop
  , ISpec(..)
72 a07343b2 Iustin Pop
  , IPolicy(..)
73 d02f941e Iustin Pop
  , defIPolicy
74 304f9292 Iustin Pop
  , rspecFromISpec
75 3e77a36c Dato Simó
  , AutoRepairType(..)
76 3e77a36c Dato Simó
  , autoRepairTypeToRaw
77 3e77a36c Dato Simó
  , autoRepairTypeFromRaw
78 3e77a36c Dato Simó
  , AutoRepairResult(..)
79 3e77a36c Dato Simó
  , autoRepairResultToRaw
80 3e77a36c Dato Simó
  , autoRepairResultFromRaw
81 7ec2f76b Dato Simó
  , AutoRepairPolicy(..)
82 7ec2f76b Dato Simó
  , AutoRepairSuspendTime(..)
83 7ec2f76b Dato Simó
  , AutoRepairData(..)
84 7ec2f76b Dato Simó
  , AutoRepairStatus(..)
85 ebf38064 Iustin Pop
  ) where
86 e4c5beaf Iustin Pop
87 2d0ca2c5 Iustin Pop
import qualified Data.Map as M
88 7ec2f76b Dato Simó
import System.Time (ClockTime)
89 2d0ca2c5 Iustin Pop
90 2e5eb96a Iustin Pop
import qualified Ganeti.Constants as C
91 e9aaa3c6 Iustin Pop
import qualified Ganeti.THH as THH
92 0c37d1e4 Iustin Pop
import Ganeti.BasicTypes
93 5e9deac0 Iustin Pop
import Ganeti.Types
94 2e5eb96a Iustin Pop
95 9188aeef Iustin Pop
-- | The instance index type.
96 608efcce Iustin Pop
type Idx = Int
97 608efcce Iustin Pop
98 9188aeef Iustin Pop
-- | The node index type.
99 608efcce Iustin Pop
type Ndx = Int
100 608efcce Iustin Pop
101 0dc1bf87 Iustin Pop
-- | The group index type.
102 0dc1bf87 Iustin Pop
type Gdx = Int
103 0dc1bf87 Iustin Pop
104 9188aeef Iustin Pop
-- | The type used to hold name-to-idx mappings.
105 2d0ca2c5 Iustin Pop
type NameAssoc = M.Map String Int
106 e4c5beaf Iustin Pop
107 92e32d76 Iustin Pop
-- | A separate name for the cluster score type.
108 92e32d76 Iustin Pop
type Score = Double
109 92e32d76 Iustin Pop
110 2180829f Iustin Pop
-- | A separate name for a weight metric.
111 2180829f Iustin Pop
type Weight = Double
112 2180829f Iustin Pop
113 0dc1bf87 Iustin Pop
-- | The Group UUID type.
114 c4d98278 Iustin Pop
type GroupID = String
115 c4d98278 Iustin Pop
116 f3f76ccc Iustin Pop
-- | Default group UUID (just a string, not a real UUID).
117 f3f76ccc Iustin Pop
defaultGroupID :: GroupID
118 f3f76ccc Iustin Pop
defaultGroupID = "00000000-0000-0000-0000-000000000000"
119 f3f76ccc Iustin Pop
120 b906d7c1 Iustin Pop
-- | Mirroring type.
121 b906d7c1 Iustin Pop
data MirrorType = MirrorNone     -- ^ No mirroring/movability
122 b906d7c1 Iustin Pop
                | MirrorInternal -- ^ DRBD-type mirroring
123 b906d7c1 Iustin Pop
                | MirrorExternal -- ^ Shared-storage type mirroring
124 139c0683 Iustin Pop
                  deriving (Eq, Show)
125 b906d7c1 Iustin Pop
126 b906d7c1 Iustin Pop
-- | Correspondence between disk template and mirror type.
127 b906d7c1 Iustin Pop
templateMirrorType :: DiskTemplate -> MirrorType
128 b906d7c1 Iustin Pop
templateMirrorType DTDiskless   = MirrorExternal
129 b906d7c1 Iustin Pop
templateMirrorType DTFile       = MirrorNone
130 b906d7c1 Iustin Pop
templateMirrorType DTSharedFile = MirrorExternal
131 b906d7c1 Iustin Pop
templateMirrorType DTPlain      = MirrorNone
132 b906d7c1 Iustin Pop
templateMirrorType DTBlock      = MirrorExternal
133 b906d7c1 Iustin Pop
templateMirrorType DTDrbd8      = MirrorInternal
134 b906d7c1 Iustin Pop
templateMirrorType DTRbd        = MirrorExternal
135 277a2ec9 Constantinos Venetsanopoulos
templateMirrorType DTExt        = MirrorExternal
136 b906d7c1 Iustin Pop
137 1f9066c0 Iustin Pop
-- | The resource spec type.
138 1f9066c0 Iustin Pop
data RSpec = RSpec
139 ebf38064 Iustin Pop
  { rspecCpu  :: Int  -- ^ Requested VCPUs
140 ebf38064 Iustin Pop
  , rspecMem  :: Int  -- ^ Requested memory
141 ebf38064 Iustin Pop
  , rspecDsk  :: Int  -- ^ Requested disk
142 139c0683 Iustin Pop
  } deriving (Show, Eq)
143 1f9066c0 Iustin Pop
144 76a20994 Iustin Pop
-- | Allocation stats type. This is used instead of 'RSpec' (which was
145 76a20994 Iustin Pop
-- used at first), because we need to track more stats. The actual
146 76a20994 Iustin Pop
-- data can refer either to allocated, or available, etc. values
147 76a20994 Iustin Pop
-- depending on the context. See also
148 76a20994 Iustin Pop
-- 'Cluster.computeAllocationDelta'.
149 76a20994 Iustin Pop
data AllocInfo = AllocInfo
150 76a20994 Iustin Pop
  { allocInfoVCpus :: Int    -- ^ VCPUs
151 76a20994 Iustin Pop
  , allocInfoNCpus :: Double -- ^ Normalised CPUs
152 76a20994 Iustin Pop
  , allocInfoMem   :: Int    -- ^ Memory
153 76a20994 Iustin Pop
  , allocInfoDisk  :: Int    -- ^ Disk
154 139c0683 Iustin Pop
  } deriving (Show, Eq)
155 a07343b2 Iustin Pop
156 80d7d8a1 Iustin Pop
-- | Currently used, possibly to allocate, unallocable.
157 80d7d8a1 Iustin Pop
type AllocStats = (AllocInfo, AllocInfo, AllocInfo)
158 80d7d8a1 Iustin Pop
159 a07343b2 Iustin Pop
-- | Instance specification type.
160 a07343b2 Iustin Pop
$(THH.buildObject "ISpec" "iSpec"
161 9cda4d6a Iustin Pop
  [ THH.renameField "MemorySize" $ THH.simpleField C.ispecMemSize    [t| Int |]
162 9cda4d6a Iustin Pop
  , THH.renameField "CpuCount"   $ THH.simpleField C.ispecCpuCount   [t| Int |]
163 9cda4d6a Iustin Pop
  , THH.renameField "DiskSize"   $ THH.simpleField C.ispecDiskSize   [t| Int |]
164 9cda4d6a Iustin Pop
  , THH.renameField "DiskCount"  $ THH.simpleField C.ispecDiskCount  [t| Int |]
165 9cda4d6a Iustin Pop
  , THH.renameField "NicCount"   $ THH.simpleField C.ispecNicCount   [t| Int |]
166 9cda4d6a Iustin Pop
  , THH.renameField "SpindleUse" $ THH.simpleField C.ispecSpindleUse [t| Int |]
167 a07343b2 Iustin Pop
  ])
168 a07343b2 Iustin Pop
169 d02f941e Iustin Pop
-- | The default minimum ispec.
170 d02f941e Iustin Pop
defMinISpec :: ISpec
171 d02f941e Iustin Pop
defMinISpec = ISpec { iSpecMemorySize = C.ipolicyDefaultsMinMemorySize
172 d02f941e Iustin Pop
                    , iSpecCpuCount   = C.ipolicyDefaultsMinCpuCount
173 d02f941e Iustin Pop
                    , iSpecDiskSize   = C.ipolicyDefaultsMinDiskSize
174 d02f941e Iustin Pop
                    , iSpecDiskCount  = C.ipolicyDefaultsMinDiskCount
175 d02f941e Iustin Pop
                    , iSpecNicCount   = C.ipolicyDefaultsMinNicCount
176 d953a965 René Nussbaumer
                    , iSpecSpindleUse = C.ipolicyDefaultsMinSpindleUse
177 d02f941e Iustin Pop
                    }
178 d02f941e Iustin Pop
179 d02f941e Iustin Pop
-- | The default standard ispec.
180 d02f941e Iustin Pop
defStdISpec :: ISpec
181 d02f941e Iustin Pop
defStdISpec = ISpec { iSpecMemorySize = C.ipolicyDefaultsStdMemorySize
182 d02f941e Iustin Pop
                    , iSpecCpuCount   = C.ipolicyDefaultsStdCpuCount
183 d02f941e Iustin Pop
                    , iSpecDiskSize   = C.ipolicyDefaultsStdDiskSize
184 d02f941e Iustin Pop
                    , iSpecDiskCount  = C.ipolicyDefaultsStdDiskCount
185 d02f941e Iustin Pop
                    , iSpecNicCount   = C.ipolicyDefaultsStdNicCount
186 d953a965 René Nussbaumer
                    , iSpecSpindleUse = C.ipolicyDefaultsStdSpindleUse
187 d02f941e Iustin Pop
                    }
188 d02f941e Iustin Pop
189 d02f941e Iustin Pop
-- | The default max ispec.
190 d02f941e Iustin Pop
defMaxISpec :: ISpec
191 d02f941e Iustin Pop
defMaxISpec = ISpec { iSpecMemorySize = C.ipolicyDefaultsMaxMemorySize
192 d02f941e Iustin Pop
                    , iSpecCpuCount   = C.ipolicyDefaultsMaxCpuCount
193 d02f941e Iustin Pop
                    , iSpecDiskSize   = C.ipolicyDefaultsMaxDiskSize
194 d02f941e Iustin Pop
                    , iSpecDiskCount  = C.ipolicyDefaultsMaxDiskCount
195 d02f941e Iustin Pop
                    , iSpecNicCount   = C.ipolicyDefaultsMaxNicCount
196 d953a965 René Nussbaumer
                    , iSpecSpindleUse = C.ipolicyDefaultsMaxSpindleUse
197 d02f941e Iustin Pop
                    }
198 d02f941e Iustin Pop
199 a07343b2 Iustin Pop
-- | Instance policy type.
200 a07343b2 Iustin Pop
$(THH.buildObject "IPolicy" "iPolicy"
201 cd79cd83 Iustin Pop
  [ THH.renameField "StdSpec" $ THH.simpleField C.ispecsStd [t| ISpec |]
202 cd79cd83 Iustin Pop
  , THH.renameField "MinSpec" $ THH.simpleField C.ispecsMin [t| ISpec |]
203 cd79cd83 Iustin Pop
  , THH.renameField "MaxSpec" $ THH.simpleField C.ispecsMax [t| ISpec |]
204 fc42a408 Iustin Pop
  , THH.renameField "DiskTemplates" $
205 cd79cd83 Iustin Pop
      THH.simpleField C.ipolicyDts [t| [DiskTemplate] |]
206 e8fa4ff6 Iustin Pop
  , THH.renameField "VcpuRatio" $
207 cd79cd83 Iustin Pop
      THH.simpleField C.ipolicyVcpuRatio [t| Double |]
208 c22d4dd4 Iustin Pop
  , THH.renameField "SpindleRatio" $
209 c22d4dd4 Iustin Pop
      THH.simpleField C.ipolicySpindleRatio [t| Double |]
210 a07343b2 Iustin Pop
  ])
211 a07343b2 Iustin Pop
212 304f9292 Iustin Pop
-- | Converts an ISpec type to a RSpec one.
213 304f9292 Iustin Pop
rspecFromISpec :: ISpec -> RSpec
214 304f9292 Iustin Pop
rspecFromISpec ispec = RSpec { rspecCpu = iSpecCpuCount ispec
215 304f9292 Iustin Pop
                             , rspecMem = iSpecMemorySize ispec
216 304f9292 Iustin Pop
                             , rspecDsk = iSpecDiskSize ispec
217 304f9292 Iustin Pop
                             }
218 304f9292 Iustin Pop
219 d02f941e Iustin Pop
-- | The default instance policy.
220 d02f941e Iustin Pop
defIPolicy :: IPolicy
221 d02f941e Iustin Pop
defIPolicy = IPolicy { iPolicyStdSpec = defStdISpec
222 d02f941e Iustin Pop
                     , iPolicyMinSpec = defMinISpec
223 d02f941e Iustin Pop
                     , iPolicyMaxSpec = defMaxISpec
224 fc42a408 Iustin Pop
                     -- hardcoding here since Constants.hs exports the
225 fc42a408 Iustin Pop
                     -- string values, not the actual type; and in
226 fc42a408 Iustin Pop
                     -- htools, we are mostly looking at DRBD
227 810d051f Iustin Pop
                     , iPolicyDiskTemplates = [minBound..maxBound]
228 e8fa4ff6 Iustin Pop
                     , iPolicyVcpuRatio = C.ipolicyDefaultsVcpuRatio
229 c22d4dd4 Iustin Pop
                     , iPolicySpindleRatio = C.ipolicyDefaultsSpindleRatio
230 d02f941e Iustin Pop
                     }
231 d02f941e Iustin Pop
232 2180829f Iustin Pop
-- | The dynamic resource specs of a machine (i.e. load or load
233 2180829f Iustin Pop
-- capacity, as opposed to size).
234 2180829f Iustin Pop
data DynUtil = DynUtil
235 ebf38064 Iustin Pop
  { cpuWeight :: Weight -- ^ Standardised CPU usage
236 ebf38064 Iustin Pop
  , memWeight :: Weight -- ^ Standardised memory load
237 ebf38064 Iustin Pop
  , dskWeight :: Weight -- ^ Standardised disk I\/O usage
238 ebf38064 Iustin Pop
  , netWeight :: Weight -- ^ Standardised network usage
239 139c0683 Iustin Pop
  } deriving (Show, Eq)
240 2180829f Iustin Pop
241 525bfb36 Iustin Pop
-- | Initial empty utilisation.
242 2180829f Iustin Pop
zeroUtil :: DynUtil
243 2180829f Iustin Pop
zeroUtil = DynUtil { cpuWeight = 0, memWeight = 0
244 2180829f Iustin Pop
                   , dskWeight = 0, netWeight = 0 }
245 2180829f Iustin Pop
246 525bfb36 Iustin Pop
-- | Base utilisation (used when no actual utilisation data is
247 525bfb36 Iustin Pop
-- supplied).
248 ee9724b9 Iustin Pop
baseUtil :: DynUtil
249 ee9724b9 Iustin Pop
baseUtil = DynUtil { cpuWeight = 1, memWeight = 1
250 ee9724b9 Iustin Pop
                   , dskWeight = 1, netWeight = 1 }
251 ee9724b9 Iustin Pop
252 525bfb36 Iustin Pop
-- | Sum two utilisation records.
253 2180829f Iustin Pop
addUtil :: DynUtil -> DynUtil -> DynUtil
254 2180829f Iustin Pop
addUtil (DynUtil a1 a2 a3 a4) (DynUtil b1 b2 b3 b4) =
255 ebf38064 Iustin Pop
  DynUtil (a1+b1) (a2+b2) (a3+b3) (a4+b4)
256 2180829f Iustin Pop
257 525bfb36 Iustin Pop
-- | Substracts one utilisation record from another.
258 2180829f Iustin Pop
subUtil :: DynUtil -> DynUtil -> DynUtil
259 2180829f Iustin Pop
subUtil (DynUtil a1 a2 a3 a4) (DynUtil b1 b2 b3 b4) =
260 ebf38064 Iustin Pop
  DynUtil (a1-b1) (a2-b2) (a3-b3) (a4-b4)
261 2180829f Iustin Pop
262 66dac8e0 Iustin Pop
-- | The description of an instance placement. It contains the
263 66dac8e0 Iustin Pop
-- instance index, the new primary and secondary node, the move being
264 66dac8e0 Iustin Pop
-- performed and the score of the cluster after the move.
265 66dac8e0 Iustin Pop
type Placement = (Idx, Ndx, Ndx, IMove, Score)
266 92e32d76 Iustin Pop
267 525bfb36 Iustin Pop
-- | An instance move definition.
268 92e32d76 Iustin Pop
data IMove = Failover                -- ^ Failover the instance (f)
269 0c8cef35 Iustin Pop
           | FailoverToAny Ndx       -- ^ Failover to a random node
270 0c8cef35 Iustin Pop
                                     -- (fa:np), for shared storage
271 92e32d76 Iustin Pop
           | ReplacePrimary Ndx      -- ^ Replace primary (f, r:np, f)
272 92e32d76 Iustin Pop
           | ReplaceSecondary Ndx    -- ^ Replace secondary (r:ns)
273 92e32d76 Iustin Pop
           | ReplaceAndFailover Ndx  -- ^ Replace secondary, failover (r:np, f)
274 92e32d76 Iustin Pop
           | FailoverAndReplace Ndx  -- ^ Failover, replace secondary (f, r:ns)
275 139c0683 Iustin Pop
             deriving (Show)
276 92e32d76 Iustin Pop
277 0e8ae201 Iustin Pop
-- | Formatted solution output for one move (involved nodes and
278 525bfb36 Iustin Pop
-- commands.
279 924f9c16 Iustin Pop
type MoveJob = ([Ndx], Idx, IMove, [String])
280 0e8ae201 Iustin Pop
281 525bfb36 Iustin Pop
-- | Unknown field in table output.
282 82ea2874 Iustin Pop
unknownField :: String
283 82ea2874 Iustin Pop
unknownField = "<unknown field>"
284 82ea2874 Iustin Pop
285 525bfb36 Iustin Pop
-- | A list of command elements.
286 0e8ae201 Iustin Pop
type JobSet = [MoveJob]
287 0e8ae201 Iustin Pop
288 f4c0b8c5 Iustin Pop
-- | Default max disk usage ratio.
289 f4c0b8c5 Iustin Pop
defReservedDiskRatio :: Double
290 f4c0b8c5 Iustin Pop
defReservedDiskRatio = 0
291 f4c0b8c5 Iustin Pop
292 1e3dccc8 Iustin Pop
-- | Base memory unit.
293 1e3dccc8 Iustin Pop
unitMem :: Int
294 1e3dccc8 Iustin Pop
unitMem = 64
295 1e3dccc8 Iustin Pop
296 1e3dccc8 Iustin Pop
-- | Base disk unit.
297 1e3dccc8 Iustin Pop
unitDsk :: Int
298 1e3dccc8 Iustin Pop
unitDsk = 256
299 1e3dccc8 Iustin Pop
300 1e3dccc8 Iustin Pop
-- | Base vcpus unit.
301 1e3dccc8 Iustin Pop
unitCpu :: Int
302 1e3dccc8 Iustin Pop
unitCpu = 1
303 1e3dccc8 Iustin Pop
304 525bfb36 Iustin Pop
-- | Reason for an operation's falure.
305 f2280553 Iustin Pop
data FailMode = FailMem  -- ^ Failed due to not enough RAM
306 f2280553 Iustin Pop
              | FailDisk -- ^ Failed due to not enough disk
307 f2280553 Iustin Pop
              | FailCPU  -- ^ Failed due to not enough CPU capacity
308 f2280553 Iustin Pop
              | FailN1   -- ^ Failed due to not passing N1 checks
309 5f0b9579 Iustin Pop
              | FailTags -- ^ Failed due to tag exclusion
310 139c0683 Iustin Pop
                deriving (Eq, Enum, Bounded, Show)
311 f2280553 Iustin Pop
312 525bfb36 Iustin Pop
-- | List with failure statistics.
313 478df686 Iustin Pop
type FailStats = [(FailMode, Int)]
314 478df686 Iustin Pop
315 525bfb36 Iustin Pop
-- | Either-like data-type customized for our failure modes.
316 a30b473c Iustin Pop
--
317 a30b473c Iustin Pop
-- The failure values for this monad track the specific allocation
318 a30b473c Iustin Pop
-- failures, so this is not a general error-monad (compare with the
319 a30b473c Iustin Pop
-- 'Result' data type). One downside is that this type cannot encode a
320 a8038349 Iustin Pop
-- generic failure mode, hence our way to build a FailMode from string
321 a8038349 Iustin Pop
-- will instead raise an exception.
322 a8038349 Iustin Pop
type OpResult = GenericResult FailMode
323 f2280553 Iustin Pop
324 a8038349 Iustin Pop
-- | 'FromString' instance for 'FailMode' designed to catch unintended
325 a8038349 Iustin Pop
-- use as a general monad.
326 a8038349 Iustin Pop
instance FromString FailMode where
327 a8038349 Iustin Pop
  mkFromString v = error $ "Programming error: OpResult used as generic monad"
328 a8038349 Iustin Pop
                           ++ v
329 f2280553 Iustin Pop
330 a30b473c Iustin Pop
-- | Conversion from 'OpResult' to 'Result'.
331 a30b473c Iustin Pop
opToResult :: OpResult a -> Result a
332 a8038349 Iustin Pop
opToResult (Bad f) = Bad $ show f
333 a8038349 Iustin Pop
opToResult (Ok v) = Ok v
334 a30b473c Iustin Pop
335 9188aeef Iustin Pop
-- | A generic class for items that have updateable names and indices.
336 497e30a1 Iustin Pop
class Element a where
337 ebf38064 Iustin Pop
  -- | Returns the name of the element
338 ebf38064 Iustin Pop
  nameOf  :: a -> String
339 ebf38064 Iustin Pop
  -- | Returns all the known names of the element
340 ebf38064 Iustin Pop
  allNames :: a -> [String]
341 ebf38064 Iustin Pop
  -- | Returns the index of the element
342 ebf38064 Iustin Pop
  idxOf   :: a -> Int
343 ebf38064 Iustin Pop
  -- | Updates the alias of the element
344 ebf38064 Iustin Pop
  setAlias :: a -> String -> a
345 ebf38064 Iustin Pop
  -- | Compute the alias by stripping a given suffix (domain) from
346 ebf38064 Iustin Pop
  -- the name
347 ebf38064 Iustin Pop
  computeAlias :: String -> a -> a
348 ebf38064 Iustin Pop
  computeAlias dom e = setAlias e alias
349 ebf38064 Iustin Pop
    where alias = take (length name - length dom) name
350 ebf38064 Iustin Pop
          name = nameOf e
351 ebf38064 Iustin Pop
  -- | Updates the index of the element
352 ebf38064 Iustin Pop
  setIdx  :: a -> Int -> a
353 1fe412bb Iustin Pop
354 1fe412bb Iustin Pop
-- | The iallocator node-evacuate evac_mode type.
355 e9aaa3c6 Iustin Pop
$(THH.declareSADT "EvacMode"
356 ebf38064 Iustin Pop
       [ ("ChangePrimary",   'C.iallocatorNevacPri)
357 ebf38064 Iustin Pop
       , ("ChangeSecondary", 'C.iallocatorNevacSec)
358 ebf38064 Iustin Pop
       , ("ChangeAll",       'C.iallocatorNevacAll)
359 ebf38064 Iustin Pop
       ])
360 e9aaa3c6 Iustin Pop
$(THH.makeJSONInstance ''EvacMode)
361 3e77a36c Dato Simó
362 3e77a36c Dato Simó
-- | The repair modes for the auto-repair tool.
363 3e77a36c Dato Simó
$(THH.declareSADT "AutoRepairType"
364 3e77a36c Dato Simó
       -- Order is important here: from least destructive to most.
365 3e77a36c Dato Simó
       [ ("ArFixStorage", 'C.autoRepairFixStorage)
366 3e77a36c Dato Simó
       , ("ArMigrate",    'C.autoRepairMigrate)
367 3e77a36c Dato Simó
       , ("ArFailover",   'C.autoRepairFailover)
368 3e77a36c Dato Simó
       , ("ArReinstall",  'C.autoRepairReinstall)
369 3e77a36c Dato Simó
       ])
370 3e77a36c Dato Simó
371 3e77a36c Dato Simó
-- | The possible auto-repair results.
372 3e77a36c Dato Simó
$(THH.declareSADT "AutoRepairResult"
373 3e77a36c Dato Simó
       [ ("ArSuccess", 'C.autoRepairSuccess)
374 3e77a36c Dato Simó
       , ("ArFailure", 'C.autoRepairFailure)
375 3e77a36c Dato Simó
       , ("ArEnoperm", 'C.autoRepairEnoperm)
376 3e77a36c Dato Simó
       ])
377 7ec2f76b Dato Simó
378 7ec2f76b Dato Simó
-- | The possible auto-repair policy for a given instance.
379 7ec2f76b Dato Simó
data AutoRepairPolicy
380 7ec2f76b Dato Simó
  = ArEnabled AutoRepairType          -- ^ Auto-repair explicitly enabled
381 7ec2f76b Dato Simó
  | ArSuspended AutoRepairSuspendTime -- ^ Suspended temporarily, or forever
382 7ec2f76b Dato Simó
  | ArNotEnabled                      -- ^ Auto-repair not explicitly enabled
383 7ec2f76b Dato Simó
  deriving (Eq, Show)
384 7ec2f76b Dato Simó
385 7ec2f76b Dato Simó
-- | The suspend timeout for 'ArSuspended'.
386 7ec2f76b Dato Simó
data AutoRepairSuspendTime = Forever         -- ^ Permanently suspended
387 7ec2f76b Dato Simó
                           | Until ClockTime -- ^ Suspended up to a certain time
388 7ec2f76b Dato Simó
                           deriving (Eq, Show)
389 7ec2f76b Dato Simó
390 7ec2f76b Dato Simó
-- | The possible auto-repair states for any given instance.
391 7ec2f76b Dato Simó
data AutoRepairStatus
392 7ec2f76b Dato Simó
  = ArHealthy                      -- ^ No problems detected with the instance
393 7ec2f76b Dato Simó
  | ArNeedsRepair AutoRepairData   -- ^ Instance has problems, no action taken
394 7ec2f76b Dato Simó
  | ArPendingRepair AutoRepairData -- ^ Repair jobs ongoing for the instance
395 7ec2f76b Dato Simó
  | ArFailedRepair AutoRepairData  -- ^ Some repair jobs for the instance failed
396 7ec2f76b Dato Simó
397 7ec2f76b Dato Simó
-- | The data accompanying a repair operation (future, pending, or failed).
398 7ec2f76b Dato Simó
data AutoRepairData = AutoRepairData { arType :: AutoRepairType
399 7ec2f76b Dato Simó
                                     , arUuid :: String
400 7ec2f76b Dato Simó
                                     , arTime :: ClockTime
401 7ec2f76b Dato Simó
                                     , arJobs :: [JobId]
402 7ec2f76b Dato Simó
                                     , arResult :: Maybe AutoRepairResult
403 7ec2f76b Dato Simó
                                     }