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