4ba3e91bb782585d448f553b4bd165af841c6183
[ganeti-github.git] / src / Ganeti / HTools / Instance.hs
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 , AssocList
32 , List
33 , create
34 , isRunning
35 , isOffline
36 , notOffline
37 , instanceDown
38 , usesSecMem
39 , applyIfOnline
40 , setIdx
41 , setName
42 , setAlias
43 , setPri
44 , setSec
45 , setBoth
46 , setMovable
47 , specOf
48 , instBelowISpec
49 , instAboveISpec
50 , instMatchesPolicy
51 , shrinkByType
52 , localStorageTemplates
53 , hasSecondary
54 , requiredNodes
55 , allNodes
56 , usesLocalStorage
57 , mirrorType
58 ) where
59
60 import Ganeti.BasicTypes
61 import qualified Ganeti.HTools.Types as T
62 import qualified Ganeti.HTools.Container as Container
63
64 import Ganeti.Utils
65
66 -- * Type declarations
67
68 -- | The instance type.
69 data Instance = Instance
70 { name :: String -- ^ The instance name
71 , alias :: String -- ^ The shortened name
72 , mem :: Int -- ^ Memory of the instance
73 , dsk :: Int -- ^ Total disk usage of the instance
74 , disks :: [Int] -- ^ Sizes of the individual disks
75 , vcpus :: Int -- ^ Number of VCPUs
76 , runSt :: T.InstanceStatus -- ^ Original run status
77 , pNode :: T.Ndx -- ^ Original primary node
78 , sNode :: T.Ndx -- ^ Original secondary node
79 , idx :: T.Idx -- ^ Internal index
80 , util :: T.DynUtil -- ^ Dynamic resource usage
81 , movable :: Bool -- ^ Can and should the instance be moved?
82 , autoBalance :: Bool -- ^ Is the instance auto-balanced?
83 , diskTemplate :: T.DiskTemplate -- ^ The disk template of the instance
84 , spindleUse :: Int -- ^ The numbers of used spindles
85 , allTags :: [String] -- ^ List of all instance tags
86 , exclTags :: [String] -- ^ List of instance exclusion tags
87 , arPolicy :: T.AutoRepairPolicy -- ^ Instance's auto-repair policy
88 } deriving (Show, Eq)
89
90 instance T.Element Instance where
91 nameOf = name
92 idxOf = idx
93 setAlias = setAlias
94 setIdx = setIdx
95 allNames n = [name n, alias n]
96
97 -- | Check if instance is running.
98 isRunning :: Instance -> Bool
99 isRunning (Instance {runSt = T.Running}) = True
100 isRunning (Instance {runSt = T.ErrorUp}) = True
101 isRunning _ = False
102
103 -- | Check if instance is offline.
104 isOffline :: Instance -> Bool
105 isOffline (Instance {runSt = T.StatusOffline}) = True
106 isOffline _ = False
107
108
109 -- | Helper to check if the instance is not offline.
110 notOffline :: Instance -> Bool
111 notOffline = not . isOffline
112
113 -- | Check if instance is down.
114 instanceDown :: Instance -> Bool
115 instanceDown inst | isRunning inst = False
116 instanceDown inst | isOffline inst = False
117 instanceDown _ = True
118
119 -- | Apply the function if the instance is online. Otherwise use
120 -- the initial value
121 applyIfOnline :: Instance -> (a -> a) -> a -> a
122 applyIfOnline = applyIf . notOffline
123
124 -- | Helper for determining whether an instance's memory needs to be
125 -- taken into account for secondary memory reservation.
126 usesSecMem :: Instance -> Bool
127 usesSecMem inst = notOffline inst && autoBalance inst
128
129 -- | Constant holding the local storage templates.
130 --
131 -- /Note:/ Currently Ganeti only exports node total/free disk space
132 -- for LVM-based storage; file-based storage is ignored in this model,
133 -- so even though file-based storage uses in reality disk space on the
134 -- node, in our model it won't affect it and we can't compute whether
135 -- there is enough disk space for a file-based instance. Therefore we
136 -- will treat this template as \'foreign\' storage.
137 localStorageTemplates :: [T.DiskTemplate]
138 localStorageTemplates = [ T.DTDrbd8, T.DTPlain ]
139
140 -- | Constant holding the movable disk templates.
141 --
142 -- This only determines the initial 'movable' state of the
143 -- instance. Further the movable state can be restricted more due to
144 -- user choices, etc.
145 movableDiskTemplates :: [T.DiskTemplate]
146 movableDiskTemplates =
147 [ T.DTDrbd8
148 , T.DTBlock
149 , T.DTSharedFile
150 , T.DTRbd
151 , T.DTExt
152 ]
153
154 -- | A simple name for the int, instance association list.
155 type AssocList = [(T.Idx, Instance)]
156
157 -- | A simple name for an instance map.
158 type List = Container.Container Instance
159
160 -- * Initialization
161
162 -- | Create an instance.
163 --
164 -- Some parameters are not initialized by function, and must be set
165 -- later (via 'setIdx' for example).
166 create :: String -> Int -> Int -> [Int] -> Int -> T.InstanceStatus
167 -> [String] -> Bool -> T.Ndx -> T.Ndx -> T.DiskTemplate -> Int
168 -> Instance
169 create name_init mem_init dsk_init disks_init vcpus_init run_init tags_init
170 auto_balance_init pn sn dt su =
171 Instance { name = name_init
172 , alias = name_init
173 , mem = mem_init
174 , dsk = dsk_init
175 , disks = disks_init
176 , vcpus = vcpus_init
177 , runSt = run_init
178 , pNode = pn
179 , sNode = sn
180 , idx = -1
181 , util = T.baseUtil
182 , movable = supportsMoves dt
183 , autoBalance = auto_balance_init
184 , diskTemplate = dt
185 , spindleUse = su
186 , allTags = tags_init
187 , exclTags = []
188 , arPolicy = T.ArNotEnabled
189 }
190
191 -- | Changes the index.
192 --
193 -- This is used only during the building of the data structures.
194 setIdx :: Instance -- ^ The original instance
195 -> T.Idx -- ^ New index
196 -> Instance -- ^ The modified instance
197 setIdx t i = t { idx = i }
198
199 -- | Changes the name.
200 --
201 -- This is used only during the building of the data structures.
202 setName :: Instance -- ^ The original instance
203 -> String -- ^ New name
204 -> Instance -- ^ The modified instance
205 setName t s = t { name = s, alias = s }
206
207 -- | Changes the alias.
208 --
209 -- This is used only during the building of the data structures.
210 setAlias :: Instance -- ^ The original instance
211 -> String -- ^ New alias
212 -> Instance -- ^ The modified instance
213 setAlias t s = t { alias = s }
214
215 -- * Update functions
216
217 -- | Changes the primary node of the instance.
218 setPri :: Instance -- ^ the original instance
219 -> T.Ndx -- ^ the new primary node
220 -> Instance -- ^ the modified instance
221 setPri t p = t { pNode = p }
222
223 -- | Changes the secondary node of the instance.
224 setSec :: Instance -- ^ the original instance
225 -> T.Ndx -- ^ the new secondary node
226 -> Instance -- ^ the modified instance
227 setSec t s = t { sNode = s }
228
229 -- | Changes both nodes of the instance.
230 setBoth :: Instance -- ^ the original instance
231 -> T.Ndx -- ^ new primary node index
232 -> T.Ndx -- ^ new secondary node index
233 -> Instance -- ^ the modified instance
234 setBoth t p s = t { pNode = p, sNode = s }
235
236 -- | Sets the movable flag on an instance.
237 setMovable :: Instance -- ^ The original instance
238 -> Bool -- ^ New movable flag
239 -> Instance -- ^ The modified instance
240 setMovable t m = t { movable = m }
241
242 -- | Try to shrink the instance based on the reason why we can't
243 -- allocate it.
244 shrinkByType :: Instance -> T.FailMode -> Result Instance
245 shrinkByType inst T.FailMem = let v = mem inst - T.unitMem
246 in if v < T.unitMem
247 then Bad "out of memory"
248 else Ok inst { mem = v }
249 shrinkByType inst T.FailDisk = let v = dsk inst - T.unitDsk
250 in if v < T.unitDsk
251 then Bad "out of disk"
252 else Ok inst { dsk = v }
253 shrinkByType inst T.FailCPU = let v = vcpus inst - T.unitCpu
254 in if v < T.unitCpu
255 then Bad "out of vcpus"
256 else Ok inst { vcpus = v }
257 shrinkByType _ f = Bad $ "Unhandled failure mode " ++ show f
258
259 -- | Return the spec of an instance.
260 specOf :: Instance -> T.RSpec
261 specOf Instance { mem = m, dsk = d, vcpus = c } =
262 T.RSpec { T.rspecCpu = c, T.rspecMem = m, T.rspecDsk = d }
263
264 -- | Checks if an instance is smaller than a given spec. Returns
265 -- OpGood for a correct spec, otherwise Bad one of the possible
266 -- failure modes.
267 instBelowISpec :: Instance -> T.ISpec -> T.OpResult ()
268 instBelowISpec inst ispec
269 | mem inst > T.iSpecMemorySize ispec = Bad T.FailMem
270 | any (> T.iSpecDiskSize ispec) (disks inst) = Bad T.FailDisk
271 | vcpus inst > T.iSpecCpuCount ispec = Bad T.FailCPU
272 | otherwise = Ok ()
273
274 -- | Checks if an instance is bigger than a given spec.
275 instAboveISpec :: Instance -> T.ISpec -> T.OpResult ()
276 instAboveISpec inst ispec
277 | mem inst < T.iSpecMemorySize ispec = Bad T.FailMem
278 | any (< T.iSpecDiskSize ispec) (disks inst) = Bad T.FailDisk
279 | vcpus inst < T.iSpecCpuCount ispec = Bad T.FailCPU
280 | otherwise = Ok ()
281
282 -- | Checks if an instance matches a min/max specs pair
283 instMatchesMinMaxSpecs :: Instance -> T.MinMaxISpecs -> T.OpResult ()
284 instMatchesMinMaxSpecs inst minmax = do
285 instAboveISpec inst (T.minMaxISpecsMinSpec minmax)
286 instBelowISpec inst (T.minMaxISpecsMaxSpec minmax)
287
288 -- | Checks if an instance matches any specs of a policy
289 instMatchesSpecs :: Instance -> [T.MinMaxISpecs] -> T.OpResult ()
290 -- Return Ok for no constraints, though this should never happen
291 instMatchesSpecs _ [] = Ok ()
292 instMatchesSpecs inst (minmax:minmaxes) =
293 foldr eithermatch (instMatchesMinMaxSpecs inst minmax) minmaxes
294 where eithermatch mm (Bad _) = instMatchesMinMaxSpecs inst mm
295 eithermatch _ y@(Ok ()) = y
296 -- # See 04f231771
297
298 -- | Checks if an instance matches a policy.
299 instMatchesPolicy :: Instance -> T.IPolicy -> T.OpResult ()
300 instMatchesPolicy inst ipol = do
301 instMatchesSpecs inst $ T.iPolicyMinMaxISpecs ipol
302 if diskTemplate inst `elem` T.iPolicyDiskTemplates ipol
303 then Ok ()
304 else Bad T.FailDisk
305
306 -- | Checks whether the instance uses a secondary node.
307 --
308 -- /Note:/ This should be reconciled with @'sNode' ==
309 -- 'Node.noSecondary'@.
310 hasSecondary :: Instance -> Bool
311 hasSecondary = (== T.DTDrbd8) . diskTemplate
312
313 -- | Computed the number of nodes for a given disk template.
314 requiredNodes :: T.DiskTemplate -> Int
315 requiredNodes T.DTDrbd8 = 2
316 requiredNodes _ = 1
317
318 -- | Computes all nodes of an instance.
319 allNodes :: Instance -> [T.Ndx]
320 allNodes inst = case diskTemplate inst of
321 T.DTDrbd8 -> [pNode inst, sNode inst]
322 _ -> [pNode inst]
323
324 -- | Checks whether a given disk template uses local storage.
325 usesLocalStorage :: Instance -> Bool
326 usesLocalStorage = (`elem` localStorageTemplates) . diskTemplate
327
328 -- | Checks whether a given disk template supported moves.
329 supportsMoves :: T.DiskTemplate -> Bool
330 supportsMoves = (`elem` movableDiskTemplates)
331
332 -- | A simple wrapper over 'T.templateMirrorType'.
333 mirrorType :: Instance -> T.MirrorType
334 mirrorType = T.templateMirrorType . diskTemplate