1 {-| Module describing an instance.
3 The instance data type holds very few fields, the algorithm
4 intelligence is in the "Node" and "Cluster" modules.
10 Copyright (C) 2009, 2010, 2011, 2012 Google Inc.
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.
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.
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
29 module Ganeti.HTools.Instance
52 , localStorageTemplates
60 import Ganeti.BasicTypes
61 import qualified Ganeti.HTools.Types as T
62 import qualified Ganeti.HTools.Container as Container
66 -- * Type declarations
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 -- ^ Disk size of instance
74 , vcpus :: Int -- ^ Number of VCPUs
75 , runSt :: T.InstanceStatus -- ^ Original run status
76 , pNode :: T.Ndx -- ^ Original primary node
77 , sNode :: T.Ndx -- ^ Original secondary node
78 , idx :: T.Idx -- ^ Internal index
79 , util :: T.DynUtil -- ^ Dynamic resource usage
80 , movable :: Bool -- ^ Can and should the instance be moved?
81 , autoBalance :: Bool -- ^ Is the instance auto-balanced?
82 , diskTemplate :: T.DiskTemplate -- ^ The disk template of the instance
83 , spindleUse :: Int -- ^ The numbers of used spindles
84 , allTags :: [String] -- ^ List of all instance tags
85 , exclTags :: [String] -- ^ List of instance exclusion tags
86 } deriving (Show, Read, Eq)
88 instance T.Element Instance where
93 allNames n = [name n, alias n]
95 -- | Check if instance is running.
96 isRunning :: Instance -> Bool
97 isRunning (Instance {runSt = T.Running}) = True
98 isRunning (Instance {runSt = T.ErrorUp}) = True
101 -- | Check if instance is offline.
102 isOffline :: Instance -> Bool
103 isOffline (Instance {runSt = T.AdminOffline}) = True
107 -- | Helper to check if the instance is not offline.
108 notOffline :: Instance -> Bool
109 notOffline = not . isOffline
111 -- | Check if instance is down.
112 instanceDown :: Instance -> Bool
113 instanceDown inst | isRunning inst = False
114 instanceDown inst | isOffline inst = False
115 instanceDown _ = True
117 -- | Apply the function if the instance is online. Otherwise use
119 applyIfOnline :: Instance -> (a -> a) -> a -> a
120 applyIfOnline = applyIf . notOffline
122 -- | Helper for determining whether an instance's memory needs to be
123 -- taken into account for secondary memory reservation.
124 usesSecMem :: Instance -> Bool
125 usesSecMem inst = notOffline inst && autoBalance inst
127 -- | Constant holding the local storage templates.
129 -- /Note:/ Currently Ganeti only exports node total/free disk space
130 -- for LVM-based storage; file-based storage is ignored in this model,
131 -- so even though file-based storage uses in reality disk space on the
132 -- node, in our model it won't affect it and we can't compute whether
133 -- there is enough disk space for a file-based instance. Therefore we
134 -- will treat this template as \'foreign\' storage.
135 localStorageTemplates :: [T.DiskTemplate]
136 localStorageTemplates = [ T.DTDrbd8, T.DTPlain ]
138 -- | Constant holding the movable disk templates.
140 -- This only determines the initial 'movable' state of the
141 -- instance. Further the movable state can be restricted more due to
142 -- user choices, etc.
143 movableDiskTemplates :: [T.DiskTemplate]
144 movableDiskTemplates =
151 -- | A simple name for the int, instance association list.
152 type AssocList = [(T.Idx, Instance)]
154 -- | A simple name for an instance map.
155 type List = Container.Container Instance
159 -- | Create an instance.
161 -- Some parameters are not initialized by function, and must be set
162 -- later (via 'setIdx' for example).
163 create :: String -> Int -> Int -> Int -> T.InstanceStatus
164 -> [String] -> Bool -> T.Ndx -> T.Ndx -> T.DiskTemplate -> Int
166 create name_init mem_init dsk_init vcpus_init run_init tags_init
167 auto_balance_init pn sn dt su =
168 Instance { name = name_init
178 , movable = supportsMoves dt
179 , autoBalance = auto_balance_init
182 , allTags = tags_init
186 -- | Changes the index.
188 -- This is used only during the building of the data structures.
189 setIdx :: Instance -- ^ The original instance
190 -> T.Idx -- ^ New index
191 -> Instance -- ^ The modified instance
192 setIdx t i = t { idx = i }
194 -- | Changes the name.
196 -- This is used only during the building of the data structures.
197 setName :: Instance -- ^ The original instance
198 -> String -- ^ New name
199 -> Instance -- ^ The modified instance
200 setName t s = t { name = s, alias = s }
202 -- | Changes the alias.
204 -- This is used only during the building of the data structures.
205 setAlias :: Instance -- ^ The original instance
206 -> String -- ^ New alias
207 -> Instance -- ^ The modified instance
208 setAlias t s = t { alias = s }
210 -- * Update functions
212 -- | Changes the primary node of the instance.
213 setPri :: Instance -- ^ the original instance
214 -> T.Ndx -- ^ the new primary node
215 -> Instance -- ^ the modified instance
216 setPri t p = t { pNode = p }
218 -- | Changes the secondary node of the instance.
219 setSec :: Instance -- ^ the original instance
220 -> T.Ndx -- ^ the new secondary node
221 -> Instance -- ^ the modified instance
222 setSec t s = t { sNode = s }
224 -- | Changes both nodes of the instance.
225 setBoth :: Instance -- ^ the original instance
226 -> T.Ndx -- ^ new primary node index
227 -> T.Ndx -- ^ new secondary node index
228 -> Instance -- ^ the modified instance
229 setBoth t p s = t { pNode = p, sNode = s }
231 -- | Sets the movable flag on an instance.
232 setMovable :: Instance -- ^ The original instance
233 -> Bool -- ^ New movable flag
234 -> Instance -- ^ The modified instance
235 setMovable t m = t { movable = m }
237 -- | Try to shrink the instance based on the reason why we can't
239 shrinkByType :: Instance -> T.FailMode -> Result Instance
240 shrinkByType inst T.FailMem = let v = mem inst - T.unitMem
242 then Bad "out of memory"
243 else Ok inst { mem = v }
244 shrinkByType inst T.FailDisk = let v = dsk inst - T.unitDsk
246 then Bad "out of disk"
247 else Ok inst { dsk = v }
248 shrinkByType inst T.FailCPU = let v = vcpus inst - T.unitCpu
250 then Bad "out of vcpus"
251 else Ok inst { vcpus = v }
252 shrinkByType _ f = Bad $ "Unhandled failure mode " ++ show f
254 -- | Return the spec of an instance.
255 specOf :: Instance -> T.RSpec
256 specOf Instance { mem = m, dsk = d, vcpus = c } =
257 T.RSpec { T.rspecCpu = c, T.rspecMem = m, T.rspecDsk = d }
259 -- | Checks if an instance is smaller than a given spec. Returns
260 -- OpGood for a correct spec, otherwise Bad one of the possible
262 instBelowISpec :: Instance -> T.ISpec -> T.OpResult ()
263 instBelowISpec inst ispec
264 | mem inst > T.iSpecMemorySize ispec = Bad T.FailMem
265 | dsk inst > T.iSpecDiskSize ispec = Bad T.FailDisk
266 | vcpus inst > T.iSpecCpuCount ispec = Bad T.FailCPU
269 -- | Checks if an instance is bigger than a given spec.
270 instAboveISpec :: Instance -> T.ISpec -> T.OpResult ()
271 instAboveISpec inst ispec
272 | mem inst < T.iSpecMemorySize ispec = Bad T.FailMem
273 | dsk inst < T.iSpecDiskSize ispec = Bad T.FailDisk
274 | vcpus inst < T.iSpecCpuCount ispec = Bad T.FailCPU
277 -- | Checks if an instance matches a policy.
278 instMatchesPolicy :: Instance -> T.IPolicy -> T.OpResult ()
279 instMatchesPolicy inst ipol = do
280 instAboveISpec inst (T.iPolicyMinSpec ipol)
281 instBelowISpec inst (T.iPolicyMaxSpec ipol)
282 if diskTemplate inst `elem` T.iPolicyDiskTemplates ipol
286 -- | Checks whether the instance uses a secondary node.
288 -- /Note:/ This should be reconciled with @'sNode' ==
289 -- 'Node.noSecondary'@.
290 hasSecondary :: Instance -> Bool
291 hasSecondary = (== T.DTDrbd8) . diskTemplate
293 -- | Computed the number of nodes for a given disk template.
294 requiredNodes :: T.DiskTemplate -> Int
295 requiredNodes T.DTDrbd8 = 2
298 -- | Computes all nodes of an instance.
299 allNodes :: Instance -> [T.Ndx]
300 allNodes inst = case diskTemplate inst of
301 T.DTDrbd8 -> [pNode inst, sNode inst]
304 -- | Checks whether a given disk template uses local storage.
305 usesLocalStorage :: Instance -> Bool
306 usesLocalStorage = (`elem` localStorageTemplates) . diskTemplate
308 -- | Checks whether a given disk template supported moves.
309 supportsMoves :: T.DiskTemplate -> Bool
310 supportsMoves = (`elem` movableDiskTemplates)
312 -- | A simple wrapper over 'T.templateMirrorType'.
313 mirrorType :: Instance -> T.MirrorType
314 mirrorType = T.templateMirrorType . diskTemplate