root / src / Ganeti / HTools / Instance.hs @ 5a13489b
History | View | Annotate | Download (12.4 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.DTRbd |
161 |
, T.DTExt |
162 |
] |
163 |
|
164 |
-- | A simple name for the int, instance association list. |
165 |
type AssocList = [(T.Idx, Instance)] |
166 |
|
167 |
-- | A simple name for an instance map. |
168 |
type List = Container.Container Instance |
169 |
|
170 |
-- * Initialization |
171 |
|
172 |
-- | Create an instance. |
173 |
-- |
174 |
-- Some parameters are not initialized by function, and must be set |
175 |
-- later (via 'setIdx' for example). |
176 |
create :: String -> Int -> Int -> [Disk] -> Int -> T.InstanceStatus |
177 |
-> [String] -> Bool -> T.Ndx -> T.Ndx -> T.DiskTemplate -> Int |
178 |
-> [Nic] -> Instance |
179 |
create name_init mem_init dsk_init disks_init vcpus_init run_init tags_init |
180 |
auto_balance_init pn sn dt su nics_init = |
181 |
Instance { name = name_init |
182 |
, alias = name_init |
183 |
, mem = mem_init |
184 |
, dsk = dsk_init |
185 |
, disks = disks_init |
186 |
, vcpus = vcpus_init |
187 |
, runSt = run_init |
188 |
, pNode = pn |
189 |
, sNode = sn |
190 |
, idx = -1 |
191 |
, util = T.baseUtil |
192 |
, movable = supportsMoves dt |
193 |
, autoBalance = auto_balance_init |
194 |
, diskTemplate = dt |
195 |
, spindleUse = su |
196 |
, allTags = tags_init |
197 |
, exclTags = [] |
198 |
, arPolicy = T.ArNotEnabled |
199 |
, nics = nics_init |
200 |
} |
201 |
|
202 |
-- | Changes the index. |
203 |
-- |
204 |
-- This is used only during the building of the data structures. |
205 |
setIdx :: Instance -- ^ The original instance |
206 |
-> T.Idx -- ^ New index |
207 |
-> Instance -- ^ The modified instance |
208 |
setIdx t i = t { idx = i } |
209 |
|
210 |
-- | Changes the name. |
211 |
-- |
212 |
-- This is used only during the building of the data structures. |
213 |
setName :: Instance -- ^ The original instance |
214 |
-> String -- ^ New name |
215 |
-> Instance -- ^ The modified instance |
216 |
setName t s = t { name = s, alias = s } |
217 |
|
218 |
-- | Changes the alias. |
219 |
-- |
220 |
-- This is used only during the building of the data structures. |
221 |
setAlias :: Instance -- ^ The original instance |
222 |
-> String -- ^ New alias |
223 |
-> Instance -- ^ The modified instance |
224 |
setAlias t s = t { alias = s } |
225 |
|
226 |
-- * Update functions |
227 |
|
228 |
-- | Changes the primary node of the instance. |
229 |
setPri :: Instance -- ^ the original instance |
230 |
-> T.Ndx -- ^ the new primary node |
231 |
-> Instance -- ^ the modified instance |
232 |
setPri t p = t { pNode = p } |
233 |
|
234 |
-- | Changes the secondary node of the instance. |
235 |
setSec :: Instance -- ^ the original instance |
236 |
-> T.Ndx -- ^ the new secondary node |
237 |
-> Instance -- ^ the modified instance |
238 |
setSec t s = t { sNode = s } |
239 |
|
240 |
-- | Changes both nodes of the instance. |
241 |
setBoth :: Instance -- ^ the original instance |
242 |
-> T.Ndx -- ^ new primary node index |
243 |
-> T.Ndx -- ^ new secondary node index |
244 |
-> Instance -- ^ the modified instance |
245 |
setBoth t p s = t { pNode = p, sNode = s } |
246 |
|
247 |
-- | Sets the movable flag on an instance. |
248 |
setMovable :: Instance -- ^ The original instance |
249 |
-> Bool -- ^ New movable flag |
250 |
-> Instance -- ^ The modified instance |
251 |
setMovable t m = t { movable = m } |
252 |
|
253 |
-- | Try to shrink the instance based on the reason why we can't |
254 |
-- allocate it. |
255 |
shrinkByType :: Instance -> T.FailMode -> Result Instance |
256 |
shrinkByType inst T.FailMem = let v = mem inst - T.unitMem |
257 |
in if v < T.unitMem |
258 |
then Bad "out of memory" |
259 |
else Ok inst { mem = v } |
260 |
shrinkByType inst T.FailDisk = let v = dsk inst - T.unitDsk |
261 |
in if v < T.unitDsk |
262 |
then Bad "out of disk" |
263 |
else Ok inst { dsk = v } |
264 |
shrinkByType inst T.FailCPU = let v = vcpus inst - T.unitCpu |
265 |
in if v < T.unitCpu |
266 |
then Bad "out of vcpus" |
267 |
else Ok inst { vcpus = v } |
268 |
shrinkByType _ f = Bad $ "Unhandled failure mode " ++ show f |
269 |
|
270 |
-- | Get the number of disk spindles |
271 |
getTotalSpindles :: Instance -> Maybe Int |
272 |
getTotalSpindles inst = |
273 |
foldr (liftM2 (+) . dskSpindles ) (Just 0) (disks inst) |
274 |
|
275 |
-- | Return the spec of an instance. |
276 |
specOf :: Instance -> T.RSpec |
277 |
specOf Instance { mem = m, dsk = d, vcpus = c } = |
278 |
T.RSpec { T.rspecCpu = c, T.rspecMem = m, T.rspecDsk = d } |
279 |
|
280 |
-- | Checks if an instance is smaller/bigger than a given spec. Returns |
281 |
-- OpGood for a correct spec, otherwise Bad one of the possible |
282 |
-- failure modes. |
283 |
instCompareISpec :: Ordering -> Instance-> T.ISpec -> Bool -> T.OpResult () |
284 |
instCompareISpec which inst ispec exclstor |
285 |
| which == mem inst `compare` T.iSpecMemorySize ispec = Bad T.FailMem |
286 |
| which `elem` map ((`compare` T.iSpecDiskSize ispec) . dskSize) |
287 |
(disks inst) = Bad T.FailDisk |
288 |
| which == vcpus inst `compare` T.iSpecCpuCount ispec = Bad T.FailCPU |
289 |
| exclstor && |
290 |
case getTotalSpindles inst of |
291 |
Nothing -> True |
292 |
Just sp_sum -> which == sp_sum `compare` T.iSpecSpindleUse ispec |
293 |
= Bad T.FailSpindles |
294 |
| not exclstor && which == spindleUse inst `compare` T.iSpecSpindleUse ispec |
295 |
= Bad T.FailSpindles |
296 |
| diskTemplate inst /= T.DTDiskless && |
297 |
which == length (disks inst) `compare` T.iSpecDiskCount ispec |
298 |
= Bad T.FailDiskCount |
299 |
| otherwise = Ok () |
300 |
|
301 |
-- | Checks if an instance is smaller than a given spec. |
302 |
instBelowISpec :: Instance -> T.ISpec -> Bool -> T.OpResult () |
303 |
instBelowISpec = instCompareISpec GT |
304 |
|
305 |
-- | Checks if an instance is bigger than a given spec. |
306 |
instAboveISpec :: Instance -> T.ISpec -> Bool -> T.OpResult () |
307 |
instAboveISpec = instCompareISpec LT |
308 |
|
309 |
-- | Checks if an instance matches a min/max specs pair |
310 |
instMatchesMinMaxSpecs :: Instance -> T.MinMaxISpecs -> Bool -> T.OpResult () |
311 |
instMatchesMinMaxSpecs inst minmax exclstor = do |
312 |
instAboveISpec inst (T.minMaxISpecsMinSpec minmax) exclstor |
313 |
instBelowISpec inst (T.minMaxISpecsMaxSpec minmax) exclstor |
314 |
|
315 |
-- | Checks if an instance matches any specs of a policy |
316 |
instMatchesSpecs :: Instance -> [T.MinMaxISpecs] -> Bool -> T.OpResult () |
317 |
-- Return Ok for no constraints, though this should never happen |
318 |
instMatchesSpecs _ [] _ = Ok () |
319 |
instMatchesSpecs inst minmaxes exclstor = |
320 |
-- The initial "Bad" should be always replaced by a real result |
321 |
foldr eithermatch (Bad T.FailInternal) minmaxes |
322 |
where eithermatch mm (Bad _) = instMatchesMinMaxSpecs inst mm exclstor |
323 |
eithermatch _ y@(Ok ()) = y |
324 |
|
325 |
-- | Checks if an instance matches a policy. |
326 |
instMatchesPolicy :: Instance -> T.IPolicy -> Bool -> T.OpResult () |
327 |
instMatchesPolicy inst ipol exclstor = do |
328 |
instMatchesSpecs inst (T.iPolicyMinMaxISpecs ipol) exclstor |
329 |
if diskTemplate inst `elem` T.iPolicyDiskTemplates ipol |
330 |
then Ok () |
331 |
else Bad T.FailDisk |
332 |
|
333 |
-- | Checks whether the instance uses a secondary node. |
334 |
-- |
335 |
-- /Note:/ This should be reconciled with @'sNode' == |
336 |
-- 'Node.noSecondary'@. |
337 |
hasSecondary :: Instance -> Bool |
338 |
hasSecondary = (== T.DTDrbd8) . diskTemplate |
339 |
|
340 |
-- | Computed the number of nodes for a given disk template. |
341 |
requiredNodes :: T.DiskTemplate -> Int |
342 |
requiredNodes T.DTDrbd8 = 2 |
343 |
requiredNodes _ = 1 |
344 |
|
345 |
-- | Computes all nodes of an instance. |
346 |
allNodes :: Instance -> [T.Ndx] |
347 |
allNodes inst = case diskTemplate inst of |
348 |
T.DTDrbd8 -> [pNode inst, sNode inst] |
349 |
_ -> [pNode inst] |
350 |
|
351 |
-- | Checks whether a given disk template uses local storage. |
352 |
usesLocalStorage :: Instance -> Bool |
353 |
usesLocalStorage = (`elem` localStorageTemplates) . diskTemplate |
354 |
|
355 |
-- | Checks whether a given disk template supported moves. |
356 |
supportsMoves :: T.DiskTemplate -> Bool |
357 |
supportsMoves = (`elem` movableDiskTemplates) |
358 |
|
359 |
-- | A simple wrapper over 'T.templateMirrorType'. |
360 |
mirrorType :: Instance -> T.MirrorType |
361 |
mirrorType = T.templateMirrorType . diskTemplate |