Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / HTools / Instance.hs @ 55416810

History | View | Annotate | Download (10.5 kB)

1 e4f08c46 Iustin Pop
{-| Module describing an instance.
2 e4f08c46 Iustin Pop
3 e4f08c46 Iustin Pop
The instance data type holds very few fields, the algorithm
4 e4f08c46 Iustin Pop
intelligence is in the "Node" and "Cluster" modules.
5 e4f08c46 Iustin Pop
6 e4f08c46 Iustin Pop
-}
7 e2fa2baf Iustin Pop
8 e2fa2baf Iustin Pop
{-
9 e2fa2baf Iustin Pop
10 aa5b2f07 Iustin Pop
Copyright (C) 2009, 2010, 2011, 2012 Google Inc.
11 e2fa2baf Iustin Pop
12 e2fa2baf Iustin Pop
This program is free software; you can redistribute it and/or modify
13 e2fa2baf Iustin Pop
it under the terms of the GNU General Public License as published by
14 e2fa2baf Iustin Pop
the Free Software Foundation; either version 2 of the License, or
15 e2fa2baf Iustin Pop
(at your option) any later version.
16 e2fa2baf Iustin Pop
17 e2fa2baf Iustin Pop
This program is distributed in the hope that it will be useful, but
18 e2fa2baf Iustin Pop
WITHOUT ANY WARRANTY; without even the implied warranty of
19 e2fa2baf Iustin Pop
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20 e2fa2baf Iustin Pop
General Public License for more details.
21 e2fa2baf Iustin Pop
22 e2fa2baf Iustin Pop
You should have received a copy of the GNU General Public License
23 e2fa2baf Iustin Pop
along with this program; if not, write to the Free Software
24 e2fa2baf Iustin Pop
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 e2fa2baf Iustin Pop
02110-1301, USA.
26 e2fa2baf Iustin Pop
27 e2fa2baf Iustin Pop
-}
28 e2fa2baf Iustin Pop
29 181d4e04 Iustin Pop
module Ganeti.HTools.Instance
30 ebf38064 Iustin Pop
  ( Instance(..)
31 ebf38064 Iustin Pop
  , AssocList
32 ebf38064 Iustin Pop
  , List
33 ebf38064 Iustin Pop
  , create
34 7959cbb9 Iustin Pop
  , isRunning
35 7959cbb9 Iustin Pop
  , isOffline
36 7959cbb9 Iustin Pop
  , notOffline
37 ebf38064 Iustin Pop
  , instanceDown
38 55bd1414 Iustin Pop
  , usesSecMem
39 ebf38064 Iustin Pop
  , applyIfOnline
40 ebf38064 Iustin Pop
  , setIdx
41 ebf38064 Iustin Pop
  , setName
42 ebf38064 Iustin Pop
  , setAlias
43 ebf38064 Iustin Pop
  , setPri
44 ebf38064 Iustin Pop
  , setSec
45 ebf38064 Iustin Pop
  , setBoth
46 ebf38064 Iustin Pop
  , setMovable
47 ebf38064 Iustin Pop
  , specOf
48 aa5b2f07 Iustin Pop
  , instBelowISpec
49 aa5b2f07 Iustin Pop
  , instAboveISpec
50 aa5b2f07 Iustin Pop
  , instMatchesPolicy
51 ebf38064 Iustin Pop
  , shrinkByType
52 ebf38064 Iustin Pop
  , localStorageTemplates
53 ebf38064 Iustin Pop
  , hasSecondary
54 ebf38064 Iustin Pop
  , requiredNodes
55 ebf38064 Iustin Pop
  , allNodes
56 ebf38064 Iustin Pop
  , usesLocalStorage
57 fafd0773 Iustin Pop
  , mirrorType
58 ebf38064 Iustin Pop
  ) where
59 e4f08c46 Iustin Pop
60 01e52493 Iustin Pop
import Ganeti.BasicTypes
61 262a08a2 Iustin Pop
import qualified Ganeti.HTools.Types as T
62 262a08a2 Iustin Pop
import qualified Ganeti.HTools.Container as Container
63 262a08a2 Iustin Pop
64 26d62e4c Iustin Pop
import Ganeti.Utils
65 61bbbed7 Agata Murawska
66 9188aeef Iustin Pop
-- * Type declarations
67 9188aeef Iustin Pop
68 525bfb36 Iustin Pop
-- | The instance type.
69 c352b0a9 Iustin Pop
data Instance = Instance
70 ebf38064 Iustin Pop
  { name         :: String    -- ^ The instance name
71 ebf38064 Iustin Pop
  , alias        :: String    -- ^ The shortened name
72 ebf38064 Iustin Pop
  , mem          :: Int       -- ^ Memory of the instance
73 ebf38064 Iustin Pop
  , dsk          :: Int       -- ^ Disk size of instance
74 ebf38064 Iustin Pop
  , vcpus        :: Int       -- ^ Number of VCPUs
75 ebf38064 Iustin Pop
  , runSt        :: T.InstanceStatus -- ^ Original run status
76 ebf38064 Iustin Pop
  , pNode        :: T.Ndx     -- ^ Original primary node
77 ebf38064 Iustin Pop
  , sNode        :: T.Ndx     -- ^ Original secondary node
78 ebf38064 Iustin Pop
  , idx          :: T.Idx     -- ^ Internal index
79 ebf38064 Iustin Pop
  , util         :: T.DynUtil -- ^ Dynamic resource usage
80 ebf38064 Iustin Pop
  , movable      :: Bool      -- ^ Can and should the instance be moved?
81 ebf38064 Iustin Pop
  , autoBalance  :: Bool      -- ^ Is the instance auto-balanced?
82 ebf38064 Iustin Pop
  , diskTemplate :: T.DiskTemplate -- ^ The disk template of the instance
83 ec629280 René Nussbaumer
  , spindleUse   :: Int       -- ^ The numbers of used spindles
84 2f907bad Dato Simó
  , allTags      :: [String]  -- ^ List of all instance tags
85 2f907bad Dato Simó
  , exclTags     :: [String]  -- ^ List of instance exclusion tags
86 23594127 Dato Simó
  , arPolicy     :: T.AutoRepairPolicy -- ^ Instance's auto-repair policy
87 139c0683 Iustin Pop
  } deriving (Show, Eq)
88 e4f08c46 Iustin Pop
89 262a08a2 Iustin Pop
instance T.Element Instance where
90 ebf38064 Iustin Pop
  nameOf   = name
91 ebf38064 Iustin Pop
  idxOf    = idx
92 ebf38064 Iustin Pop
  setAlias = setAlias
93 ebf38064 Iustin Pop
  setIdx   = setIdx
94 ebf38064 Iustin Pop
  allNames n = [name n, alias n]
95 262a08a2 Iustin Pop
96 8a8ed513 Agata Murawska
-- | Check if instance is running.
97 7959cbb9 Iustin Pop
isRunning :: Instance -> Bool
98 7959cbb9 Iustin Pop
isRunning (Instance {runSt = T.Running}) = True
99 7959cbb9 Iustin Pop
isRunning (Instance {runSt = T.ErrorUp}) = True
100 7959cbb9 Iustin Pop
isRunning _                              = False
101 a46f34d7 Iustin Pop
102 61bbbed7 Agata Murawska
-- | Check if instance is offline.
103 7959cbb9 Iustin Pop
isOffline :: Instance -> Bool
104 5e9deac0 Iustin Pop
isOffline (Instance {runSt = T.StatusOffline}) = True
105 5e9deac0 Iustin Pop
isOffline _                                    = False
106 61bbbed7 Agata Murawska
107 9cd6c325 Iustin Pop
108 9cd6c325 Iustin Pop
-- | Helper to check if the instance is not offline.
109 7959cbb9 Iustin Pop
notOffline :: Instance -> Bool
110 7959cbb9 Iustin Pop
notOffline = not . isOffline
111 9cd6c325 Iustin Pop
112 61bbbed7 Agata Murawska
-- | Check if instance is down.
113 61bbbed7 Agata Murawska
instanceDown :: Instance -> Bool
114 7959cbb9 Iustin Pop
instanceDown inst | isRunning inst = False
115 7959cbb9 Iustin Pop
instanceDown inst | isOffline inst = False
116 7959cbb9 Iustin Pop
instanceDown _                     = True
117 61bbbed7 Agata Murawska
118 61bbbed7 Agata Murawska
-- | Apply the function if the instance is online. Otherwise use
119 61bbbed7 Agata Murawska
-- the initial value
120 61bbbed7 Agata Murawska
applyIfOnline :: Instance -> (a -> a) -> a -> a
121 7959cbb9 Iustin Pop
applyIfOnline = applyIf . notOffline
122 61bbbed7 Agata Murawska
123 55bd1414 Iustin Pop
-- | Helper for determining whether an instance's memory needs to be
124 55bd1414 Iustin Pop
-- taken into account for secondary memory reservation.
125 55bd1414 Iustin Pop
usesSecMem :: Instance -> Bool
126 7959cbb9 Iustin Pop
usesSecMem inst = notOffline inst && autoBalance inst
127 55bd1414 Iustin Pop
128 8353b5e1 Iustin Pop
-- | Constant holding the local storage templates.
129 8353b5e1 Iustin Pop
--
130 8353b5e1 Iustin Pop
-- /Note:/ Currently Ganeti only exports node total/free disk space
131 8353b5e1 Iustin Pop
-- for LVM-based storage; file-based storage is ignored in this model,
132 8353b5e1 Iustin Pop
-- so even though file-based storage uses in reality disk space on the
133 8353b5e1 Iustin Pop
-- node, in our model it won't affect it and we can't compute whether
134 8353b5e1 Iustin Pop
-- there is enough disk space for a file-based instance. Therefore we
135 8353b5e1 Iustin Pop
-- will treat this template as \'foreign\' storage.
136 8353b5e1 Iustin Pop
localStorageTemplates :: [T.DiskTemplate]
137 8353b5e1 Iustin Pop
localStorageTemplates = [ T.DTDrbd8, T.DTPlain ]
138 8353b5e1 Iustin Pop
139 8353b5e1 Iustin Pop
-- | Constant holding the movable disk templates.
140 8353b5e1 Iustin Pop
--
141 8353b5e1 Iustin Pop
-- This only determines the initial 'movable' state of the
142 8353b5e1 Iustin Pop
-- instance. Further the movable state can be restricted more due to
143 8353b5e1 Iustin Pop
-- user choices, etc.
144 8353b5e1 Iustin Pop
movableDiskTemplates :: [T.DiskTemplate]
145 2c7b328c Iustin Pop
movableDiskTemplates =
146 2c7b328c Iustin Pop
  [ T.DTDrbd8
147 2c7b328c Iustin Pop
  , T.DTBlock
148 2c7b328c Iustin Pop
  , T.DTSharedFile
149 2c7b328c Iustin Pop
  , T.DTRbd
150 277a2ec9 Constantinos Venetsanopoulos
  , T.DTExt
151 2c7b328c Iustin Pop
  ]
152 8353b5e1 Iustin Pop
153 9188aeef Iustin Pop
-- | A simple name for the int, instance association list.
154 608efcce Iustin Pop
type AssocList = [(T.Idx, Instance)]
155 040afc35 Iustin Pop
156 9188aeef Iustin Pop
-- | A simple name for an instance map.
157 262a08a2 Iustin Pop
type List = Container.Container Instance
158 262a08a2 Iustin Pop
159 9188aeef Iustin Pop
-- * Initialization
160 9188aeef Iustin Pop
161 9188aeef Iustin Pop
-- | Create an instance.
162 9188aeef Iustin Pop
--
163 9188aeef Iustin Pop
-- Some parameters are not initialized by function, and must be set
164 9188aeef Iustin Pop
-- later (via 'setIdx' for example).
165 7dd14211 Agata Murawska
create :: String -> Int -> Int -> Int -> T.InstanceStatus
166 981bb5cf René Nussbaumer
       -> [String] -> Bool -> T.Ndx -> T.Ndx -> T.DiskTemplate -> Int
167 981bb5cf René Nussbaumer
       -> Instance
168 c352b0a9 Iustin Pop
create name_init mem_init dsk_init vcpus_init run_init tags_init
169 981bb5cf René Nussbaumer
       auto_balance_init pn sn dt su =
170 ebf38064 Iustin Pop
  Instance { name = name_init
171 ebf38064 Iustin Pop
           , alias = name_init
172 ebf38064 Iustin Pop
           , mem = mem_init
173 ebf38064 Iustin Pop
           , dsk = dsk_init
174 ebf38064 Iustin Pop
           , vcpus = vcpus_init
175 ebf38064 Iustin Pop
           , runSt = run_init
176 ebf38064 Iustin Pop
           , pNode = pn
177 ebf38064 Iustin Pop
           , sNode = sn
178 ebf38064 Iustin Pop
           , idx = -1
179 ebf38064 Iustin Pop
           , util = T.baseUtil
180 ebf38064 Iustin Pop
           , movable = supportsMoves dt
181 ebf38064 Iustin Pop
           , autoBalance = auto_balance_init
182 ebf38064 Iustin Pop
           , diskTemplate = dt
183 ec629280 René Nussbaumer
           , spindleUse = su
184 2f907bad Dato Simó
           , allTags = tags_init
185 2f907bad Dato Simó
           , exclTags = []
186 23594127 Dato Simó
           , arPolicy = T.ArNotEnabled
187 ebf38064 Iustin Pop
           }
188 e4f08c46 Iustin Pop
189 9188aeef Iustin Pop
-- | Changes the index.
190 9188aeef Iustin Pop
--
191 9188aeef Iustin Pop
-- This is used only during the building of the data structures.
192 903a7d46 Iustin Pop
setIdx :: Instance -- ^ The original instance
193 903a7d46 Iustin Pop
       -> T.Idx    -- ^ New index
194 903a7d46 Iustin Pop
       -> Instance -- ^ The modified instance
195 9188aeef Iustin Pop
setIdx t i = t { idx = i }
196 9188aeef Iustin Pop
197 9188aeef Iustin Pop
-- | Changes the name.
198 9188aeef Iustin Pop
--
199 9188aeef Iustin Pop
-- This is used only during the building of the data structures.
200 9188aeef Iustin Pop
setName :: Instance -- ^ The original instance
201 9188aeef Iustin Pop
        -> String   -- ^ New name
202 903a7d46 Iustin Pop
        -> Instance -- ^ The modified instance
203 8bcdde0c Iustin Pop
setName t s = t { name = s, alias = s }
204 8bcdde0c Iustin Pop
205 8bcdde0c Iustin Pop
-- | Changes the alias.
206 8bcdde0c Iustin Pop
--
207 8bcdde0c Iustin Pop
-- This is used only during the building of the data structures.
208 8bcdde0c Iustin Pop
setAlias :: Instance -- ^ The original instance
209 8bcdde0c Iustin Pop
         -> String   -- ^ New alias
210 8bcdde0c Iustin Pop
         -> Instance -- ^ The modified instance
211 8bcdde0c Iustin Pop
setAlias t s = t { alias = s }
212 9188aeef Iustin Pop
213 9188aeef Iustin Pop
-- * Update functions
214 9188aeef Iustin Pop
215 e4f08c46 Iustin Pop
-- | Changes the primary node of the instance.
216 fd934a28 Iustin Pop
setPri :: Instance  -- ^ the original instance
217 608efcce Iustin Pop
        -> T.Ndx    -- ^ the new primary node
218 e4f08c46 Iustin Pop
        -> Instance -- ^ the modified instance
219 2060348b Iustin Pop
setPri t p = t { pNode = p }
220 e4f08c46 Iustin Pop
221 e4f08c46 Iustin Pop
-- | Changes the secondary node of the instance.
222 fd934a28 Iustin Pop
setSec :: Instance  -- ^ the original instance
223 608efcce Iustin Pop
        -> T.Ndx    -- ^ the new secondary node
224 e4f08c46 Iustin Pop
        -> Instance -- ^ the modified instance
225 2060348b Iustin Pop
setSec t s = t { sNode = s }
226 e4f08c46 Iustin Pop
227 e4f08c46 Iustin Pop
-- | Changes both nodes of the instance.
228 fd934a28 Iustin Pop
setBoth :: Instance  -- ^ the original instance
229 608efcce Iustin Pop
         -> T.Ndx    -- ^ new primary node index
230 608efcce Iustin Pop
         -> T.Ndx    -- ^ new secondary node index
231 e4f08c46 Iustin Pop
         -> Instance -- ^ the modified instance
232 2060348b Iustin Pop
setBoth t p s = t { pNode = p, sNode = s }
233 c8db97e5 Iustin Pop
234 179c0828 Iustin Pop
-- | Sets the movable flag on an instance.
235 a182df55 Iustin Pop
setMovable :: Instance -- ^ The original instance
236 a182df55 Iustin Pop
           -> Bool     -- ^ New movable flag
237 a182df55 Iustin Pop
           -> Instance -- ^ The modified instance
238 a182df55 Iustin Pop
setMovable t m = t { movable = m }
239 a182df55 Iustin Pop
240 c8db97e5 Iustin Pop
-- | Try to shrink the instance based on the reason why we can't
241 c8db97e5 Iustin Pop
-- allocate it.
242 01e52493 Iustin Pop
shrinkByType :: Instance -> T.FailMode -> Result Instance
243 1e3dccc8 Iustin Pop
shrinkByType inst T.FailMem = let v = mem inst - T.unitMem
244 1e3dccc8 Iustin Pop
                              in if v < T.unitMem
245 01e52493 Iustin Pop
                                 then Bad "out of memory"
246 01e52493 Iustin Pop
                                 else Ok inst { mem = v }
247 1e3dccc8 Iustin Pop
shrinkByType inst T.FailDisk = let v = dsk inst - T.unitDsk
248 1e3dccc8 Iustin Pop
                               in if v < T.unitDsk
249 01e52493 Iustin Pop
                                  then Bad "out of disk"
250 01e52493 Iustin Pop
                                  else Ok inst { dsk = v }
251 1e3dccc8 Iustin Pop
shrinkByType inst T.FailCPU = let v = vcpus inst - T.unitCpu
252 1e3dccc8 Iustin Pop
                              in if v < T.unitCpu
253 01e52493 Iustin Pop
                                 then Bad "out of vcpus"
254 01e52493 Iustin Pop
                                 else Ok inst { vcpus = v }
255 01e52493 Iustin Pop
shrinkByType _ f = Bad $ "Unhandled failure mode " ++ show f
256 83ad1f3c Iustin Pop
257 83ad1f3c Iustin Pop
-- | Return the spec of an instance.
258 83ad1f3c Iustin Pop
specOf :: Instance -> T.RSpec
259 83ad1f3c Iustin Pop
specOf Instance { mem = m, dsk = d, vcpus = c } =
260 ebf38064 Iustin Pop
  T.RSpec { T.rspecCpu = c, T.rspecMem = m, T.rspecDsk = d }
261 a10a476a Iustin Pop
262 aa5b2f07 Iustin Pop
-- | Checks if an instance is smaller than a given spec. Returns
263 a8038349 Iustin Pop
-- OpGood for a correct spec, otherwise Bad one of the possible
264 aa5b2f07 Iustin Pop
-- failure modes.
265 aa5b2f07 Iustin Pop
instBelowISpec :: Instance -> T.ISpec -> T.OpResult ()
266 aa5b2f07 Iustin Pop
instBelowISpec inst ispec
267 a8038349 Iustin Pop
  | mem inst > T.iSpecMemorySize ispec = Bad T.FailMem
268 a8038349 Iustin Pop
  | dsk inst > T.iSpecDiskSize ispec   = Bad T.FailDisk
269 a8038349 Iustin Pop
  | vcpus inst > T.iSpecCpuCount ispec = Bad T.FailCPU
270 a8038349 Iustin Pop
  | otherwise = Ok ()
271 aa5b2f07 Iustin Pop
272 aa5b2f07 Iustin Pop
-- | Checks if an instance is bigger than a given spec.
273 aa5b2f07 Iustin Pop
instAboveISpec :: Instance -> T.ISpec -> T.OpResult ()
274 aa5b2f07 Iustin Pop
instAboveISpec inst ispec
275 a8038349 Iustin Pop
  | mem inst < T.iSpecMemorySize ispec = Bad T.FailMem
276 a8038349 Iustin Pop
  | dsk inst < T.iSpecDiskSize ispec   = Bad T.FailDisk
277 a8038349 Iustin Pop
  | vcpus inst < T.iSpecCpuCount ispec = Bad T.FailCPU
278 a8038349 Iustin Pop
  | otherwise = Ok ()
279 aa5b2f07 Iustin Pop
280 aa5b2f07 Iustin Pop
-- | Checks if an instance matches a policy.
281 aa5b2f07 Iustin Pop
instMatchesPolicy :: Instance -> T.IPolicy -> T.OpResult ()
282 aa5b2f07 Iustin Pop
instMatchesPolicy inst ipol = do
283 aa5b2f07 Iustin Pop
  instAboveISpec inst (T.iPolicyMinSpec ipol)
284 aa5b2f07 Iustin Pop
  instBelowISpec inst (T.iPolicyMaxSpec ipol)
285 5b11f8db Iustin Pop
  if diskTemplate inst `elem` T.iPolicyDiskTemplates ipol
286 a8038349 Iustin Pop
    then Ok ()
287 a8038349 Iustin Pop
    else Bad T.FailDisk
288 aa5b2f07 Iustin Pop
289 8353b5e1 Iustin Pop
-- | Checks whether the instance uses a secondary node.
290 8353b5e1 Iustin Pop
--
291 8353b5e1 Iustin Pop
-- /Note:/ This should be reconciled with @'sNode' ==
292 8353b5e1 Iustin Pop
-- 'Node.noSecondary'@.
293 8353b5e1 Iustin Pop
hasSecondary :: Instance -> Bool
294 8353b5e1 Iustin Pop
hasSecondary = (== T.DTDrbd8) . diskTemplate
295 8353b5e1 Iustin Pop
296 179c0828 Iustin Pop
-- | Computed the number of nodes for a given disk template.
297 a10a476a Iustin Pop
requiredNodes :: T.DiskTemplate -> Int
298 a10a476a Iustin Pop
requiredNodes T.DTDrbd8 = 2
299 a10a476a Iustin Pop
requiredNodes _         = 1
300 d254d6ce Iustin Pop
301 d254d6ce Iustin Pop
-- | Computes all nodes of an instance.
302 d254d6ce Iustin Pop
allNodes :: Instance -> [T.Ndx]
303 d254d6ce Iustin Pop
allNodes inst = case diskTemplate inst of
304 d254d6ce Iustin Pop
                  T.DTDrbd8 -> [pNode inst, sNode inst]
305 d254d6ce Iustin Pop
                  _ -> [pNode inst]
306 8353b5e1 Iustin Pop
307 8353b5e1 Iustin Pop
-- | Checks whether a given disk template uses local storage.
308 8353b5e1 Iustin Pop
usesLocalStorage :: Instance -> Bool
309 8353b5e1 Iustin Pop
usesLocalStorage = (`elem` localStorageTemplates) . diskTemplate
310 8353b5e1 Iustin Pop
311 8353b5e1 Iustin Pop
-- | Checks whether a given disk template supported moves.
312 8353b5e1 Iustin Pop
supportsMoves :: T.DiskTemplate -> Bool
313 8353b5e1 Iustin Pop
supportsMoves = (`elem` movableDiskTemplates)
314 fafd0773 Iustin Pop
315 fafd0773 Iustin Pop
-- | A simple wrapper over 'T.templateMirrorType'.
316 fafd0773 Iustin Pop
mirrorType :: Instance -> T.MirrorType
317 fafd0773 Iustin Pop
mirrorType = T.templateMirrorType . diskTemplate