root / src / Ganeti / HTools / Instance.hs @ 8106dd64
History | View | Annotate | Download (13.1 kB)
1 |
{-| Module describing an instance. |
---|---|
2 |
|
3 |
The instance data type holds very few fields, the algorithm |
4 |
intelligence is in the "Node" and "Cluster" modules. |
5 |
|
6 |
-} |
7 |
|
8 |
{- |
9 |
|
10 |
Copyright (C) 2009, 2010, 2011, 2012, 2013 Google Inc. |
11 |
|
12 |
This program is free software; you can redistribute it and/or modify |
13 |
it under the terms of the GNU General Public License as published by |
14 |
the Free Software Foundation; either version 2 of the License, or |
15 |
(at your option) any later version. |
16 |
|
17 |
This program is distributed in the hope that it will be useful, but |
18 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
19 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
20 |
General Public License for more details. |
21 |
|
22 |
You should have received a copy of the GNU General Public License |
23 |
along with this program; if not, write to the Free Software |
24 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
25 |
02110-1301, USA. |
26 |
|
27 |
-} |
28 |
|
29 |
module Ganeti.HTools.Instance |
30 |
( Instance(..) |
31 |
, Disk(..) |
32 |
, AssocList |
33 |
, List |
34 |
, create |
35 |
, isRunning |
36 |
, isOffline |
37 |
, notOffline |
38 |
, instanceDown |
39 |
, usesSecMem |
40 |
, applyIfOnline |
41 |
, setIdx |
42 |
, setName |
43 |
, setAlias |
44 |
, setPri |
45 |
, setSec |
46 |
, setBoth |
47 |
, setMovable |
48 |
, specOf |
49 |
, getTotalSpindles |
50 |
, instBelowISpec |
51 |
, instAboveISpec |
52 |
, instMatchesPolicy |
53 |
, shrinkByType |
54 |
, localStorageTemplates |
55 |
, hasSecondary |
56 |
, requiredNodes |
57 |
, allNodes |
58 |
, usesLocalStorage |
59 |
, mirrorType |
60 |
) where |
61 |
|
62 |
import Control.Monad (liftM2) |
63 |
|
64 |
import Ganeti.BasicTypes |
65 |
import qualified Ganeti.HTools.Types as T |
66 |
import qualified Ganeti.HTools.Container as Container |
67 |
import Ganeti.HTools.Nic (Nic) |
68 |
|
69 |
import Ganeti.Utils |
70 |
|
71 |
-- * Type declarations |
72 |
data Disk = Disk |
73 |
{ dskSize :: Int -- ^ Size in bytes |
74 |
, dskSpindles :: Maybe Int -- ^ Number of spindles |
75 |
} deriving (Show, Eq) |
76 |
|
77 |
-- | The instance type. |
78 |
data Instance = Instance |
79 |
{ name :: String -- ^ The instance name |
80 |
, alias :: String -- ^ The shortened name |
81 |
, mem :: Int -- ^ Memory of the instance |
82 |
, dsk :: Int -- ^ Total disk usage of the instance |
83 |
, disks :: [Disk] -- ^ Sizes of the individual disks |
84 |
, vcpus :: Int -- ^ Number of VCPUs |
85 |
, runSt :: T.InstanceStatus -- ^ Original run status |
86 |
, pNode :: T.Ndx -- ^ Original primary node |
87 |
, sNode :: T.Ndx -- ^ Original secondary node |
88 |
, idx :: T.Idx -- ^ Internal index |
89 |
, util :: T.DynUtil -- ^ Dynamic resource usage |
90 |
, movable :: Bool -- ^ Can and should the instance be moved? |
91 |
, autoBalance :: Bool -- ^ Is the instance auto-balanced? |
92 |
, diskTemplate :: T.DiskTemplate -- ^ The disk template of the instance |
93 |
, spindleUse :: Int -- ^ The numbers of used spindles |
94 |
, allTags :: [String] -- ^ List of all instance tags |
95 |
, exclTags :: [String] -- ^ List of instance exclusion tags |
96 |
, arPolicy :: T.AutoRepairPolicy -- ^ Instance's auto-repair policy |
97 |
, nics :: [Nic] -- ^ NICs of the instance |
98 |
} deriving (Show, Eq) |
99 |
|
100 |
instance T.Element Instance where |
101 |
nameOf = name |
102 |
idxOf = idx |
103 |
setAlias = setAlias |
104 |
setIdx = setIdx |
105 |
allNames n = [name n, alias n] |
106 |
|
107 |
-- | Check if instance is running. |
108 |
isRunning :: Instance -> Bool |
109 |
isRunning (Instance {runSt = T.Running}) = True |
110 |
isRunning (Instance {runSt = T.ErrorUp}) = True |
111 |
isRunning _ = False |
112 |
|
113 |
-- | Check if instance is offline. |
114 |
isOffline :: Instance -> Bool |
115 |
isOffline (Instance {runSt = T.StatusOffline}) = True |
116 |
isOffline _ = False |
117 |
|
118 |
|
119 |
-- | Helper to check if the instance is not offline. |
120 |
notOffline :: Instance -> Bool |
121 |
notOffline = not . isOffline |
122 |
|
123 |
-- | Check if instance is down. |
124 |
instanceDown :: Instance -> Bool |
125 |
instanceDown inst | isRunning inst = False |
126 |
instanceDown inst | isOffline inst = False |
127 |
instanceDown _ = True |
128 |
|
129 |
-- | Apply the function if the instance is online. Otherwise use |
130 |
-- the initial value |
131 |
applyIfOnline :: Instance -> (a -> a) -> a -> a |
132 |
applyIfOnline = applyIf . notOffline |
133 |
|
134 |
-- | Helper for determining whether an instance's memory needs to be |
135 |
-- taken into account for secondary memory reservation. |
136 |
usesSecMem :: Instance -> Bool |
137 |
usesSecMem inst = notOffline inst && autoBalance inst |
138 |
|
139 |
-- | Constant holding the local storage templates. |
140 |
-- |
141 |
-- /Note:/ Currently Ganeti only exports node total/free disk space |
142 |
-- for LVM-based storage; file-based storage is ignored in this model, |
143 |
-- so even though file-based storage uses in reality disk space on the |
144 |
-- node, in our model it won't affect it and we can't compute whether |
145 |
-- there is enough disk space for a file-based instance. Therefore we |
146 |
-- will treat this template as \'foreign\' storage. |
147 |
localStorageTemplates :: [T.DiskTemplate] |
148 |
localStorageTemplates = [ T.DTDrbd8, T.DTPlain ] |
149 |
|
150 |
-- | Constant holding the movable disk templates. |
151 |
-- |
152 |
-- This only determines the initial 'movable' state of the |
153 |
-- instance. Further the movable state can be restricted more due to |
154 |
-- user choices, etc. |
155 |
movableDiskTemplates :: [T.DiskTemplate] |
156 |
movableDiskTemplates = |
157 |
[ T.DTDrbd8 |
158 |
, T.DTBlock |
159 |
, T.DTSharedFile |
160 |
, T.DTGluster |
161 |
, T.DTRbd |
162 |
, T.DTExt |
163 |
] |
164 |
|
165 |
-- | A simple name for the int, instance association list. |
166 |
type AssocList = [(T.Idx, Instance)] |
167 |
|
168 |
-- | A simple name for an instance map. |
169 |
type List = Container.Container Instance |
170 |
|
171 |
-- * Initialization |
172 |
|
173 |
-- | Create an instance. |
174 |
-- |
175 |
-- Some parameters are not initialized by function, and must be set |
176 |
-- later (via 'setIdx' for example). |
177 |
create :: String -> Int -> Int -> [Disk] -> Int -> T.InstanceStatus |
178 |
-> [String] -> Bool -> T.Ndx -> T.Ndx -> T.DiskTemplate -> Int |
179 |
-> [Nic] -> Instance |
180 |
create name_init mem_init dsk_init disks_init vcpus_init run_init tags_init |
181 |
auto_balance_init pn sn dt su nics_init = |
182 |
Instance { name = name_init |
183 |
, alias = name_init |
184 |
, mem = mem_init |
185 |
, dsk = dsk_init |
186 |
, disks = disks_init |
187 |
, vcpus = vcpus_init |
188 |
, runSt = run_init |
189 |
, pNode = pn |
190 |
, sNode = sn |
191 |
, idx = -1 |
192 |
, util = T.baseUtil |
193 |
, movable = supportsMoves dt |
194 |
, autoBalance = auto_balance_init |
195 |
, diskTemplate = dt |
196 |
, spindleUse = su |
197 |
, allTags = tags_init |
198 |
, exclTags = [] |
199 |
, arPolicy = T.ArNotEnabled |
200 |
, nics = nics_init |
201 |
} |
202 |
|
203 |
-- | Changes the index. |
204 |
-- |
205 |
-- This is used only during the building of the data structures. |
206 |
setIdx :: Instance -- ^ The original instance |
207 |
-> T.Idx -- ^ New index |
208 |
-> Instance -- ^ The modified instance |
209 |
setIdx t i = t { idx = i } |
210 |
|
211 |
-- | Changes the name. |
212 |
-- |
213 |
-- This is used only during the building of the data structures. |
214 |
setName :: Instance -- ^ The original instance |
215 |
-> String -- ^ New name |
216 |
-> Instance -- ^ The modified instance |
217 |
setName t s = t { name = s, alias = s } |
218 |
|
219 |
-- | Changes the alias. |
220 |
-- |
221 |
-- This is used only during the building of the data structures. |
222 |
setAlias :: Instance -- ^ The original instance |
223 |
-> String -- ^ New alias |
224 |
-> Instance -- ^ The modified instance |
225 |
setAlias t s = t { alias = s } |
226 |
|
227 |
-- * Update functions |
228 |
|
229 |
-- | Changes the primary node of the instance. |
230 |
setPri :: Instance -- ^ the original instance |
231 |
-> T.Ndx -- ^ the new primary node |
232 |
-> Instance -- ^ the modified instance |
233 |
setPri t p = t { pNode = p } |
234 |
|
235 |
-- | Changes the secondary node of the instance. |
236 |
setSec :: Instance -- ^ the original instance |
237 |
-> T.Ndx -- ^ the new secondary node |
238 |
-> Instance -- ^ the modified instance |
239 |
setSec t s = t { sNode = s } |
240 |
|
241 |
-- | Changes both nodes of the instance. |
242 |
setBoth :: Instance -- ^ the original instance |
243 |
-> T.Ndx -- ^ new primary node index |
244 |
-> T.Ndx -- ^ new secondary node index |
245 |
-> Instance -- ^ the modified instance |
246 |
setBoth t p s = t { pNode = p, sNode = s } |
247 |
|
248 |
-- | Sets the movable flag on an instance. |
249 |
setMovable :: Instance -- ^ The original instance |
250 |
-> Bool -- ^ New movable flag |
251 |
-> Instance -- ^ The modified instance |
252 |
setMovable t m = t { movable = m } |
253 |
|
254 |
-- | Try to shrink the instance based on the reason why we can't |
255 |
-- allocate it. |
256 |
shrinkByType :: Instance -> T.FailMode -> Result Instance |
257 |
shrinkByType inst T.FailMem = let v = mem inst - T.unitMem |
258 |
in if v < T.unitMem |
259 |
then Bad "out of memory" |
260 |
else Ok inst { mem = v } |
261 |
shrinkByType inst T.FailDisk = |
262 |
let newdisks = [d {dskSize = dskSize d - T.unitDsk}| d <- disks inst] |
263 |
v = dsk inst - (length . disks $ inst) * T.unitDsk |
264 |
in if any (< T.unitDsk) $ map dskSize newdisks |
265 |
then Bad "out of disk" |
266 |
else Ok inst { dsk = v, disks = newdisks } |
267 |
shrinkByType inst T.FailCPU = let v = vcpus inst - T.unitCpu |
268 |
in if v < T.unitCpu |
269 |
then Bad "out of vcpus" |
270 |
else Ok inst { vcpus = v } |
271 |
shrinkByType inst T.FailSpindles = |
272 |
case disks inst of |
273 |
[Disk ds sp] -> case sp of |
274 |
Nothing -> Bad "No spindles, shouldn't have happened" |
275 |
Just sp' -> let v = sp' - T.unitSpindle |
276 |
in if v < T.unitSpindle |
277 |
then Bad "out of spindles" |
278 |
else Ok inst { disks = [Disk ds (Just v)] } |
279 |
d -> Bad $ "Expected one disk, but found " ++ show d |
280 |
shrinkByType _ f = Bad $ "Unhandled failure mode " ++ show f |
281 |
|
282 |
-- | Get the number of disk spindles |
283 |
getTotalSpindles :: Instance -> Maybe Int |
284 |
getTotalSpindles inst = |
285 |
foldr (liftM2 (+) . dskSpindles ) (Just 0) (disks inst) |
286 |
|
287 |
-- | Return the spec of an instance. |
288 |
specOf :: Instance -> T.RSpec |
289 |
specOf Instance { mem = m, dsk = d, vcpus = c, disks = dl } = |
290 |
let sp = case dl of |
291 |
[Disk _ (Just sp')] -> sp' |
292 |
_ -> 0 |
293 |
in T.RSpec { T.rspecCpu = c, T.rspecMem = m, |
294 |
T.rspecDsk = d, T.rspecSpn = sp } |
295 |
|
296 |
-- | Checks if an instance is smaller/bigger than a given spec. Returns |
297 |
-- OpGood for a correct spec, otherwise Bad one of the possible |
298 |
-- failure modes. |
299 |
instCompareISpec :: Ordering -> Instance-> T.ISpec -> Bool -> T.OpResult () |
300 |
instCompareISpec which inst ispec exclstor |
301 |
| which == mem inst `compare` T.iSpecMemorySize ispec = Bad T.FailMem |
302 |
| which `elem` map ((`compare` T.iSpecDiskSize ispec) . dskSize) |
303 |
(disks inst) = Bad T.FailDisk |
304 |
| which == vcpus inst `compare` T.iSpecCpuCount ispec = Bad T.FailCPU |
305 |
| exclstor && |
306 |
case getTotalSpindles inst of |
307 |
Nothing -> True |
308 |
Just sp_sum -> which == sp_sum `compare` T.iSpecSpindleUse ispec |
309 |
= Bad T.FailSpindles |
310 |
| not exclstor && which == spindleUse inst `compare` T.iSpecSpindleUse ispec |
311 |
= Bad T.FailSpindles |
312 |
| diskTemplate inst /= T.DTDiskless && |
313 |
which == length (disks inst) `compare` T.iSpecDiskCount ispec |
314 |
= Bad T.FailDiskCount |
315 |
| otherwise = Ok () |
316 |
|
317 |
-- | Checks if an instance is smaller than a given spec. |
318 |
instBelowISpec :: Instance -> T.ISpec -> Bool -> T.OpResult () |
319 |
instBelowISpec = instCompareISpec GT |
320 |
|
321 |
-- | Checks if an instance is bigger than a given spec. |
322 |
instAboveISpec :: Instance -> T.ISpec -> Bool -> T.OpResult () |
323 |
instAboveISpec = instCompareISpec LT |
324 |
|
325 |
-- | Checks if an instance matches a min/max specs pair |
326 |
instMatchesMinMaxSpecs :: Instance -> T.MinMaxISpecs -> Bool -> T.OpResult () |
327 |
instMatchesMinMaxSpecs inst minmax exclstor = do |
328 |
instAboveISpec inst (T.minMaxISpecsMinSpec minmax) exclstor |
329 |
instBelowISpec inst (T.minMaxISpecsMaxSpec minmax) exclstor |
330 |
|
331 |
-- | Checks if an instance matches any specs of a policy |
332 |
instMatchesSpecs :: Instance -> [T.MinMaxISpecs] -> Bool -> T.OpResult () |
333 |
-- Return Ok for no constraints, though this should never happen |
334 |
instMatchesSpecs _ [] _ = Ok () |
335 |
instMatchesSpecs inst minmaxes exclstor = |
336 |
-- The initial "Bad" should be always replaced by a real result |
337 |
foldr eithermatch (Bad T.FailInternal) minmaxes |
338 |
where eithermatch mm (Bad _) = instMatchesMinMaxSpecs inst mm exclstor |
339 |
eithermatch _ y@(Ok ()) = y |
340 |
|
341 |
-- | Checks if an instance matches a policy. |
342 |
instMatchesPolicy :: Instance -> T.IPolicy -> Bool -> T.OpResult () |
343 |
instMatchesPolicy inst ipol exclstor = do |
344 |
instMatchesSpecs inst (T.iPolicyMinMaxISpecs ipol) exclstor |
345 |
if diskTemplate inst `elem` T.iPolicyDiskTemplates ipol |
346 |
then Ok () |
347 |
else Bad T.FailDisk |
348 |
|
349 |
-- | Checks whether the instance uses a secondary node. |
350 |
-- |
351 |
-- /Note:/ This should be reconciled with @'sNode' == |
352 |
-- 'Node.noSecondary'@. |
353 |
hasSecondary :: Instance -> Bool |
354 |
hasSecondary = (== T.DTDrbd8) . diskTemplate |
355 |
|
356 |
-- | Computed the number of nodes for a given disk template. |
357 |
requiredNodes :: T.DiskTemplate -> Int |
358 |
requiredNodes T.DTDrbd8 = 2 |
359 |
requiredNodes _ = 1 |
360 |
|
361 |
-- | Computes all nodes of an instance. |
362 |
allNodes :: Instance -> [T.Ndx] |
363 |
allNodes inst = case diskTemplate inst of |
364 |
T.DTDrbd8 -> [pNode inst, sNode inst] |
365 |
_ -> [pNode inst] |
366 |
|
367 |
-- | Checks whether a given disk template uses local storage. |
368 |
usesLocalStorage :: Instance -> Bool |
369 |
usesLocalStorage = (`elem` localStorageTemplates) . diskTemplate |
370 |
|
371 |
-- | Checks whether a given disk template supported moves. |
372 |
supportsMoves :: T.DiskTemplate -> Bool |
373 |
supportsMoves = (`elem` movableDiskTemplates) |
374 |
|
375 |
-- | A simple wrapper over 'T.templateMirrorType'. |
376 |
mirrorType :: Instance -> T.MirrorType |
377 |
mirrorType = T.templateMirrorType . diskTemplate |