root / htools / Ganeti / HTools / Types.hs @ 304f9292
History | View | Annotate | Download (11 kB)
1 |
{-# LANGUAGE TemplateHaskell #-} |
---|---|
2 |
|
3 |
{-| Some common types. |
4 |
|
5 |
-} |
6 |
|
7 |
{- |
8 |
|
9 |
Copyright (C) 2009, 2010, 2011 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 |
, InstanceStatus(..) |
41 |
, instanceStatusFromRaw |
42 |
, instanceStatusToRaw |
43 |
, RSpec(..) |
44 |
, DynUtil(..) |
45 |
, zeroUtil |
46 |
, baseUtil |
47 |
, addUtil |
48 |
, subUtil |
49 |
, defVcpuRatio |
50 |
, defReservedDiskRatio |
51 |
, unitMem |
52 |
, unitCpu |
53 |
, unitDsk |
54 |
, unknownField |
55 |
, Placement |
56 |
, IMove(..) |
57 |
, DiskTemplate(..) |
58 |
, diskTemplateToRaw |
59 |
, diskTemplateFromRaw |
60 |
, MoveJob |
61 |
, JobSet |
62 |
, Result(..) |
63 |
, isOk |
64 |
, isBad |
65 |
, eitherToResult |
66 |
, annotateResult |
67 |
, Element(..) |
68 |
, FailMode(..) |
69 |
, FailStats |
70 |
, OpResult(..) |
71 |
, opToResult |
72 |
, connTimeout |
73 |
, queryTimeout |
74 |
, EvacMode(..) |
75 |
, ISpec(..) |
76 |
, IPolicy(..) |
77 |
, defIPolicy |
78 |
, rspecFromISpec |
79 |
) where |
80 |
|
81 |
import qualified Data.Map as M |
82 |
import Text.JSON (makeObj, readJSON, showJSON) |
83 |
|
84 |
import qualified Ganeti.Constants as C |
85 |
import qualified Ganeti.THH as THH |
86 |
import Ganeti.BasicTypes |
87 |
import Ganeti.HTools.JSON |
88 |
|
89 |
-- | The instance index type. |
90 |
type Idx = Int |
91 |
|
92 |
-- | The node index type. |
93 |
type Ndx = Int |
94 |
|
95 |
-- | The group index type. |
96 |
type Gdx = Int |
97 |
|
98 |
-- | The type used to hold name-to-idx mappings. |
99 |
type NameAssoc = M.Map String Int |
100 |
|
101 |
-- | A separate name for the cluster score type. |
102 |
type Score = Double |
103 |
|
104 |
-- | A separate name for a weight metric. |
105 |
type Weight = Double |
106 |
|
107 |
-- | The Group UUID type. |
108 |
type GroupID = String |
109 |
|
110 |
-- | Default group UUID (just a string, not a real UUID). |
111 |
defaultGroupID :: GroupID |
112 |
defaultGroupID = "00000000-0000-0000-0000-000000000000" |
113 |
|
114 |
-- | The Group allocation policy type. |
115 |
-- |
116 |
-- Note that the order of constructors is important as the automatic |
117 |
-- Ord instance will order them in the order they are defined, so when |
118 |
-- changing this data type be careful about the interaction with the |
119 |
-- desired sorting order. |
120 |
$(THH.declareSADT "AllocPolicy" |
121 |
[ ("AllocPreferred", 'C.allocPolicyPreferred) |
122 |
, ("AllocLastResort", 'C.allocPolicyLastResort) |
123 |
, ("AllocUnallocable", 'C.allocPolicyUnallocable) |
124 |
]) |
125 |
$(THH.makeJSONInstance ''AllocPolicy) |
126 |
|
127 |
-- | The Instance real state type. |
128 |
$(THH.declareSADT "InstanceStatus" |
129 |
[ ("AdminDown", 'C.inststAdmindown) |
130 |
, ("AdminOffline", 'C.inststAdminoffline) |
131 |
, ("ErrorDown", 'C.inststErrordown) |
132 |
, ("ErrorUp", 'C.inststErrorup) |
133 |
, ("NodeDown", 'C.inststNodedown) |
134 |
, ("NodeOffline", 'C.inststNodeoffline) |
135 |
, ("Running", 'C.inststRunning) |
136 |
, ("WrongNode", 'C.inststWrongnode) |
137 |
]) |
138 |
$(THH.makeJSONInstance ''InstanceStatus) |
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, Read, Eq) |
146 |
|
147 |
|
148 |
-- | Instance specification type. |
149 |
$(THH.buildObject "ISpec" "iSpec" |
150 |
[ THH.renameField "MemorySize" $ THH.simpleField "memory-size" [t| Int |] |
151 |
, THH.renameField "CpuCount" $ THH.simpleField "cpu-count" [t| Int |] |
152 |
, THH.renameField "DiskSize" $ THH.simpleField "disk-size" [t| Int |] |
153 |
, THH.renameField "DiskCount" $ THH.simpleField "disk-count" [t| Int |] |
154 |
, THH.renameField "NicCount" $ THH.simpleField "nic-count" [t| Int |] |
155 |
]) |
156 |
|
157 |
-- | The default minimum ispec. |
158 |
defMinISpec :: ISpec |
159 |
defMinISpec = ISpec { iSpecMemorySize = C.ipolicyDefaultsMinMemorySize |
160 |
, iSpecCpuCount = C.ipolicyDefaultsMinCpuCount |
161 |
, iSpecDiskSize = C.ipolicyDefaultsMinDiskSize |
162 |
, iSpecDiskCount = C.ipolicyDefaultsMinDiskCount |
163 |
, iSpecNicCount = C.ipolicyDefaultsMinNicCount |
164 |
} |
165 |
|
166 |
-- | The default standard ispec. |
167 |
defStdISpec :: ISpec |
168 |
defStdISpec = ISpec { iSpecMemorySize = C.ipolicyDefaultsStdMemorySize |
169 |
, iSpecCpuCount = C.ipolicyDefaultsStdCpuCount |
170 |
, iSpecDiskSize = C.ipolicyDefaultsStdDiskSize |
171 |
, iSpecDiskCount = C.ipolicyDefaultsStdDiskCount |
172 |
, iSpecNicCount = C.ipolicyDefaultsStdNicCount |
173 |
} |
174 |
|
175 |
-- | The default max ispec. |
176 |
defMaxISpec :: ISpec |
177 |
defMaxISpec = ISpec { iSpecMemorySize = C.ipolicyDefaultsMaxMemorySize |
178 |
, iSpecCpuCount = C.ipolicyDefaultsMaxCpuCount |
179 |
, iSpecDiskSize = C.ipolicyDefaultsMaxDiskSize |
180 |
, iSpecDiskCount = C.ipolicyDefaultsMaxDiskCount |
181 |
, iSpecNicCount = C.ipolicyDefaultsMaxNicCount |
182 |
} |
183 |
|
184 |
-- | Instance policy type. |
185 |
$(THH.buildObject "IPolicy" "iPolicy" |
186 |
[ THH.renameField "StdSpec" $ THH.simpleField "std" [t| ISpec |] |
187 |
, THH.renameField "MinSpec" $ THH.simpleField "min" [t| ISpec |] |
188 |
, THH.renameField "MaxSpec" $ THH.simpleField "max" [t| ISpec |] |
189 |
]) |
190 |
|
191 |
-- | Converts an ISpec type to a RSpec one. |
192 |
rspecFromISpec :: ISpec -> RSpec |
193 |
rspecFromISpec ispec = RSpec { rspecCpu = iSpecCpuCount ispec |
194 |
, rspecMem = iSpecMemorySize ispec |
195 |
, rspecDsk = iSpecDiskSize ispec |
196 |
} |
197 |
|
198 |
-- | The default instance policy. |
199 |
defIPolicy :: IPolicy |
200 |
defIPolicy = IPolicy { iPolicyStdSpec = defStdISpec |
201 |
, iPolicyMinSpec = defMinISpec |
202 |
, iPolicyMaxSpec = defMaxISpec |
203 |
} |
204 |
|
205 |
-- | The dynamic resource specs of a machine (i.e. load or load |
206 |
-- capacity, as opposed to size). |
207 |
data DynUtil = DynUtil |
208 |
{ cpuWeight :: Weight -- ^ Standardised CPU usage |
209 |
, memWeight :: Weight -- ^ Standardised memory load |
210 |
, dskWeight :: Weight -- ^ Standardised disk I\/O usage |
211 |
, netWeight :: Weight -- ^ Standardised network usage |
212 |
} deriving (Show, Read, Eq) |
213 |
|
214 |
-- | Initial empty utilisation. |
215 |
zeroUtil :: DynUtil |
216 |
zeroUtil = DynUtil { cpuWeight = 0, memWeight = 0 |
217 |
, dskWeight = 0, netWeight = 0 } |
218 |
|
219 |
-- | Base utilisation (used when no actual utilisation data is |
220 |
-- supplied). |
221 |
baseUtil :: DynUtil |
222 |
baseUtil = DynUtil { cpuWeight = 1, memWeight = 1 |
223 |
, dskWeight = 1, netWeight = 1 } |
224 |
|
225 |
-- | Sum two utilisation records. |
226 |
addUtil :: DynUtil -> DynUtil -> DynUtil |
227 |
addUtil (DynUtil a1 a2 a3 a4) (DynUtil b1 b2 b3 b4) = |
228 |
DynUtil (a1+b1) (a2+b2) (a3+b3) (a4+b4) |
229 |
|
230 |
-- | Substracts one utilisation record from another. |
231 |
subUtil :: DynUtil -> DynUtil -> DynUtil |
232 |
subUtil (DynUtil a1 a2 a3 a4) (DynUtil b1 b2 b3 b4) = |
233 |
DynUtil (a1-b1) (a2-b2) (a3-b3) (a4-b4) |
234 |
|
235 |
-- | The description of an instance placement. It contains the |
236 |
-- instance index, the new primary and secondary node, the move being |
237 |
-- performed and the score of the cluster after the move. |
238 |
type Placement = (Idx, Ndx, Ndx, IMove, Score) |
239 |
|
240 |
-- | An instance move definition. |
241 |
data IMove = Failover -- ^ Failover the instance (f) |
242 |
| ReplacePrimary Ndx -- ^ Replace primary (f, r:np, f) |
243 |
| ReplaceSecondary Ndx -- ^ Replace secondary (r:ns) |
244 |
| ReplaceAndFailover Ndx -- ^ Replace secondary, failover (r:np, f) |
245 |
| FailoverAndReplace Ndx -- ^ Failover, replace secondary (f, r:ns) |
246 |
deriving (Show, Read) |
247 |
|
248 |
-- | Instance disk template type. |
249 |
$(THH.declareSADT "DiskTemplate" |
250 |
[ ("DTDiskless", 'C.dtDiskless) |
251 |
, ("DTFile", 'C.dtFile) |
252 |
, ("DTSharedFile", 'C.dtSharedFile) |
253 |
, ("DTPlain", 'C.dtPlain) |
254 |
, ("DTBlock", 'C.dtBlock) |
255 |
, ("DTDrbd8", 'C.dtDrbd8) |
256 |
]) |
257 |
$(THH.makeJSONInstance ''DiskTemplate) |
258 |
|
259 |
-- | Formatted solution output for one move (involved nodes and |
260 |
-- commands. |
261 |
type MoveJob = ([Ndx], Idx, IMove, [String]) |
262 |
|
263 |
-- | Unknown field in table output. |
264 |
unknownField :: String |
265 |
unknownField = "<unknown field>" |
266 |
|
267 |
-- | A list of command elements. |
268 |
type JobSet = [MoveJob] |
269 |
|
270 |
-- | Connection timeout (when using non-file methods). |
271 |
connTimeout :: Int |
272 |
connTimeout = 15 |
273 |
|
274 |
-- | The default timeout for queries (when using non-file methods). |
275 |
queryTimeout :: Int |
276 |
queryTimeout = 60 |
277 |
|
278 |
-- | Default vcpu-to-pcpu ratio (randomly chosen value). |
279 |
defVcpuRatio :: Double |
280 |
defVcpuRatio = 64 |
281 |
|
282 |
-- | Default max disk usage ratio. |
283 |
defReservedDiskRatio :: Double |
284 |
defReservedDiskRatio = 0 |
285 |
|
286 |
-- | Base memory unit. |
287 |
unitMem :: Int |
288 |
unitMem = 64 |
289 |
|
290 |
-- | Base disk unit. |
291 |
unitDsk :: Int |
292 |
unitDsk = 256 |
293 |
|
294 |
-- | Base vcpus unit. |
295 |
unitCpu :: Int |
296 |
unitCpu = 1 |
297 |
|
298 |
-- | Reason for an operation's falure. |
299 |
data FailMode = FailMem -- ^ Failed due to not enough RAM |
300 |
| FailDisk -- ^ Failed due to not enough disk |
301 |
| FailCPU -- ^ Failed due to not enough CPU capacity |
302 |
| FailN1 -- ^ Failed due to not passing N1 checks |
303 |
| FailTags -- ^ Failed due to tag exclusion |
304 |
deriving (Eq, Enum, Bounded, Show, Read) |
305 |
|
306 |
-- | List with failure statistics. |
307 |
type FailStats = [(FailMode, Int)] |
308 |
|
309 |
-- | Either-like data-type customized for our failure modes. |
310 |
-- |
311 |
-- The failure values for this monad track the specific allocation |
312 |
-- failures, so this is not a general error-monad (compare with the |
313 |
-- 'Result' data type). One downside is that this type cannot encode a |
314 |
-- generic failure mode, hence 'fail' for this monad is not defined |
315 |
-- and will cause an exception. |
316 |
data OpResult a = OpFail FailMode -- ^ Failed operation |
317 |
| OpGood a -- ^ Success operation |
318 |
deriving (Show, Read) |
319 |
|
320 |
instance Monad OpResult where |
321 |
(OpGood x) >>= fn = fn x |
322 |
(OpFail y) >>= _ = OpFail y |
323 |
return = OpGood |
324 |
|
325 |
-- | Conversion from 'OpResult' to 'Result'. |
326 |
opToResult :: OpResult a -> Result a |
327 |
opToResult (OpFail f) = Bad $ show f |
328 |
opToResult (OpGood v) = Ok v |
329 |
|
330 |
-- | A generic class for items that have updateable names and indices. |
331 |
class Element a where |
332 |
-- | Returns the name of the element |
333 |
nameOf :: a -> String |
334 |
-- | Returns all the known names of the element |
335 |
allNames :: a -> [String] |
336 |
-- | Returns the index of the element |
337 |
idxOf :: a -> Int |
338 |
-- | Updates the alias of the element |
339 |
setAlias :: a -> String -> a |
340 |
-- | Compute the alias by stripping a given suffix (domain) from |
341 |
-- the name |
342 |
computeAlias :: String -> a -> a |
343 |
computeAlias dom e = setAlias e alias |
344 |
where alias = take (length name - length dom) name |
345 |
name = nameOf e |
346 |
-- | Updates the index of the element |
347 |
setIdx :: a -> Int -> a |
348 |
|
349 |
-- | The iallocator node-evacuate evac_mode type. |
350 |
$(THH.declareSADT "EvacMode" |
351 |
[ ("ChangePrimary", 'C.iallocatorNevacPri) |
352 |
, ("ChangeSecondary", 'C.iallocatorNevacSec) |
353 |
, ("ChangeAll", 'C.iallocatorNevacAll) |
354 |
]) |
355 |
$(THH.makeJSONInstance ''EvacMode) |