Statistics
| Branch: | Tag: | Revision:

root / htools / Ganeti / HTools / Instance.hs @ 1496f5f3

History | View | Annotate | Download (10.4 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 139c0683 Iustin Pop
  } deriving (Show, Eq)
87 e4f08c46 Iustin Pop
88 262a08a2 Iustin Pop
instance T.Element Instance where
89 ebf38064 Iustin Pop
  nameOf   = name
90 ebf38064 Iustin Pop
  idxOf    = idx
91 ebf38064 Iustin Pop
  setAlias = setAlias
92 ebf38064 Iustin Pop
  setIdx   = setIdx
93 ebf38064 Iustin Pop
  allNames n = [name n, alias n]
94 262a08a2 Iustin Pop
95 8a8ed513 Agata Murawska
-- | Check if instance is running.
96 7959cbb9 Iustin Pop
isRunning :: Instance -> Bool
97 7959cbb9 Iustin Pop
isRunning (Instance {runSt = T.Running}) = True
98 7959cbb9 Iustin Pop
isRunning (Instance {runSt = T.ErrorUp}) = True
99 7959cbb9 Iustin Pop
isRunning _                              = False
100 a46f34d7 Iustin Pop
101 61bbbed7 Agata Murawska
-- | Check if instance is offline.
102 7959cbb9 Iustin Pop
isOffline :: Instance -> Bool
103 5e9deac0 Iustin Pop
isOffline (Instance {runSt = T.StatusOffline}) = True
104 5e9deac0 Iustin Pop
isOffline _                                    = False
105 61bbbed7 Agata Murawska
106 9cd6c325 Iustin Pop
107 9cd6c325 Iustin Pop
-- | Helper to check if the instance is not offline.
108 7959cbb9 Iustin Pop
notOffline :: Instance -> Bool
109 7959cbb9 Iustin Pop
notOffline = not . isOffline
110 9cd6c325 Iustin Pop
111 61bbbed7 Agata Murawska
-- | Check if instance is down.
112 61bbbed7 Agata Murawska
instanceDown :: Instance -> Bool
113 7959cbb9 Iustin Pop
instanceDown inst | isRunning inst = False
114 7959cbb9 Iustin Pop
instanceDown inst | isOffline inst = False
115 7959cbb9 Iustin Pop
instanceDown _                     = True
116 61bbbed7 Agata Murawska
117 61bbbed7 Agata Murawska
-- | Apply the function if the instance is online. Otherwise use
118 61bbbed7 Agata Murawska
-- the initial value
119 61bbbed7 Agata Murawska
applyIfOnline :: Instance -> (a -> a) -> a -> a
120 7959cbb9 Iustin Pop
applyIfOnline = applyIf . notOffline
121 61bbbed7 Agata Murawska
122 55bd1414 Iustin Pop
-- | Helper for determining whether an instance's memory needs to be
123 55bd1414 Iustin Pop
-- taken into account for secondary memory reservation.
124 55bd1414 Iustin Pop
usesSecMem :: Instance -> Bool
125 7959cbb9 Iustin Pop
usesSecMem inst = notOffline inst && autoBalance inst
126 55bd1414 Iustin Pop
127 8353b5e1 Iustin Pop
-- | Constant holding the local storage templates.
128 8353b5e1 Iustin Pop
--
129 8353b5e1 Iustin Pop
-- /Note:/ Currently Ganeti only exports node total/free disk space
130 8353b5e1 Iustin Pop
-- for LVM-based storage; file-based storage is ignored in this model,
131 8353b5e1 Iustin Pop
-- so even though file-based storage uses in reality disk space on the
132 8353b5e1 Iustin Pop
-- node, in our model it won't affect it and we can't compute whether
133 8353b5e1 Iustin Pop
-- there is enough disk space for a file-based instance. Therefore we
134 8353b5e1 Iustin Pop
-- will treat this template as \'foreign\' storage.
135 8353b5e1 Iustin Pop
localStorageTemplates :: [T.DiskTemplate]
136 8353b5e1 Iustin Pop
localStorageTemplates = [ T.DTDrbd8, T.DTPlain ]
137 8353b5e1 Iustin Pop
138 8353b5e1 Iustin Pop
-- | Constant holding the movable disk templates.
139 8353b5e1 Iustin Pop
--
140 8353b5e1 Iustin Pop
-- This only determines the initial 'movable' state of the
141 8353b5e1 Iustin Pop
-- instance. Further the movable state can be restricted more due to
142 8353b5e1 Iustin Pop
-- user choices, etc.
143 8353b5e1 Iustin Pop
movableDiskTemplates :: [T.DiskTemplate]
144 2c7b328c Iustin Pop
movableDiskTemplates =
145 2c7b328c Iustin Pop
  [ T.DTDrbd8
146 2c7b328c Iustin Pop
  , T.DTBlock
147 2c7b328c Iustin Pop
  , T.DTSharedFile
148 2c7b328c Iustin Pop
  , T.DTRbd
149 2c7b328c Iustin Pop
  ]
150 8353b5e1 Iustin Pop
151 9188aeef Iustin Pop
-- | A simple name for the int, instance association list.
152 608efcce Iustin Pop
type AssocList = [(T.Idx, Instance)]
153 040afc35 Iustin Pop
154 9188aeef Iustin Pop
-- | A simple name for an instance map.
155 262a08a2 Iustin Pop
type List = Container.Container Instance
156 262a08a2 Iustin Pop
157 9188aeef Iustin Pop
-- * Initialization
158 9188aeef Iustin Pop
159 9188aeef Iustin Pop
-- | Create an instance.
160 9188aeef Iustin Pop
--
161 9188aeef Iustin Pop
-- Some parameters are not initialized by function, and must be set
162 9188aeef Iustin Pop
-- later (via 'setIdx' for example).
163 7dd14211 Agata Murawska
create :: String -> Int -> Int -> Int -> T.InstanceStatus
164 981bb5cf René Nussbaumer
       -> [String] -> Bool -> T.Ndx -> T.Ndx -> T.DiskTemplate -> Int
165 981bb5cf René Nussbaumer
       -> Instance
166 c352b0a9 Iustin Pop
create name_init mem_init dsk_init vcpus_init run_init tags_init
167 981bb5cf René Nussbaumer
       auto_balance_init pn sn dt su =
168 ebf38064 Iustin Pop
  Instance { name = name_init
169 ebf38064 Iustin Pop
           , alias = name_init
170 ebf38064 Iustin Pop
           , mem = mem_init
171 ebf38064 Iustin Pop
           , dsk = dsk_init
172 ebf38064 Iustin Pop
           , vcpus = vcpus_init
173 ebf38064 Iustin Pop
           , runSt = run_init
174 ebf38064 Iustin Pop
           , pNode = pn
175 ebf38064 Iustin Pop
           , sNode = sn
176 ebf38064 Iustin Pop
           , idx = -1
177 ebf38064 Iustin Pop
           , util = T.baseUtil
178 ebf38064 Iustin Pop
           , movable = supportsMoves dt
179 ebf38064 Iustin Pop
           , autoBalance = auto_balance_init
180 ebf38064 Iustin Pop
           , diskTemplate = dt
181 ec629280 René Nussbaumer
           , spindleUse = su
182 2f907bad Dato Simó
           , allTags = tags_init
183 2f907bad Dato Simó
           , exclTags = []
184 ebf38064 Iustin Pop
           }
185 e4f08c46 Iustin Pop
186 9188aeef Iustin Pop
-- | Changes the index.
187 9188aeef Iustin Pop
--
188 9188aeef Iustin Pop
-- This is used only during the building of the data structures.
189 903a7d46 Iustin Pop
setIdx :: Instance -- ^ The original instance
190 903a7d46 Iustin Pop
       -> T.Idx    -- ^ New index
191 903a7d46 Iustin Pop
       -> Instance -- ^ The modified instance
192 9188aeef Iustin Pop
setIdx t i = t { idx = i }
193 9188aeef Iustin Pop
194 9188aeef Iustin Pop
-- | Changes the name.
195 9188aeef Iustin Pop
--
196 9188aeef Iustin Pop
-- This is used only during the building of the data structures.
197 9188aeef Iustin Pop
setName :: Instance -- ^ The original instance
198 9188aeef Iustin Pop
        -> String   -- ^ New name
199 903a7d46 Iustin Pop
        -> Instance -- ^ The modified instance
200 8bcdde0c Iustin Pop
setName t s = t { name = s, alias = s }
201 8bcdde0c Iustin Pop
202 8bcdde0c Iustin Pop
-- | Changes the alias.
203 8bcdde0c Iustin Pop
--
204 8bcdde0c Iustin Pop
-- This is used only during the building of the data structures.
205 8bcdde0c Iustin Pop
setAlias :: Instance -- ^ The original instance
206 8bcdde0c Iustin Pop
         -> String   -- ^ New alias
207 8bcdde0c Iustin Pop
         -> Instance -- ^ The modified instance
208 8bcdde0c Iustin Pop
setAlias t s = t { alias = s }
209 9188aeef Iustin Pop
210 9188aeef Iustin Pop
-- * Update functions
211 9188aeef Iustin Pop
212 e4f08c46 Iustin Pop
-- | Changes the primary node of the instance.
213 fd934a28 Iustin Pop
setPri :: Instance  -- ^ the original instance
214 608efcce Iustin Pop
        -> T.Ndx    -- ^ the new primary node
215 e4f08c46 Iustin Pop
        -> Instance -- ^ the modified instance
216 2060348b Iustin Pop
setPri t p = t { pNode = p }
217 e4f08c46 Iustin Pop
218 e4f08c46 Iustin Pop
-- | Changes the secondary node of the instance.
219 fd934a28 Iustin Pop
setSec :: Instance  -- ^ the original instance
220 608efcce Iustin Pop
        -> T.Ndx    -- ^ the new secondary node
221 e4f08c46 Iustin Pop
        -> Instance -- ^ the modified instance
222 2060348b Iustin Pop
setSec t s = t { sNode = s }
223 e4f08c46 Iustin Pop
224 e4f08c46 Iustin Pop
-- | Changes both nodes of the instance.
225 fd934a28 Iustin Pop
setBoth :: Instance  -- ^ the original instance
226 608efcce Iustin Pop
         -> T.Ndx    -- ^ new primary node index
227 608efcce Iustin Pop
         -> T.Ndx    -- ^ new secondary node index
228 e4f08c46 Iustin Pop
         -> Instance -- ^ the modified instance
229 2060348b Iustin Pop
setBoth t p s = t { pNode = p, sNode = s }
230 c8db97e5 Iustin Pop
231 179c0828 Iustin Pop
-- | Sets the movable flag on an instance.
232 a182df55 Iustin Pop
setMovable :: Instance -- ^ The original instance
233 a182df55 Iustin Pop
           -> Bool     -- ^ New movable flag
234 a182df55 Iustin Pop
           -> Instance -- ^ The modified instance
235 a182df55 Iustin Pop
setMovable t m = t { movable = m }
236 a182df55 Iustin Pop
237 c8db97e5 Iustin Pop
-- | Try to shrink the instance based on the reason why we can't
238 c8db97e5 Iustin Pop
-- allocate it.
239 01e52493 Iustin Pop
shrinkByType :: Instance -> T.FailMode -> Result Instance
240 1e3dccc8 Iustin Pop
shrinkByType inst T.FailMem = let v = mem inst - T.unitMem
241 1e3dccc8 Iustin Pop
                              in if v < T.unitMem
242 01e52493 Iustin Pop
                                 then Bad "out of memory"
243 01e52493 Iustin Pop
                                 else Ok inst { mem = v }
244 1e3dccc8 Iustin Pop
shrinkByType inst T.FailDisk = let v = dsk inst - T.unitDsk
245 1e3dccc8 Iustin Pop
                               in if v < T.unitDsk
246 01e52493 Iustin Pop
                                  then Bad "out of disk"
247 01e52493 Iustin Pop
                                  else Ok inst { dsk = v }
248 1e3dccc8 Iustin Pop
shrinkByType inst T.FailCPU = let v = vcpus inst - T.unitCpu
249 1e3dccc8 Iustin Pop
                              in if v < T.unitCpu
250 01e52493 Iustin Pop
                                 then Bad "out of vcpus"
251 01e52493 Iustin Pop
                                 else Ok inst { vcpus = v }
252 01e52493 Iustin Pop
shrinkByType _ f = Bad $ "Unhandled failure mode " ++ show f
253 83ad1f3c Iustin Pop
254 83ad1f3c Iustin Pop
-- | Return the spec of an instance.
255 83ad1f3c Iustin Pop
specOf :: Instance -> T.RSpec
256 83ad1f3c Iustin Pop
specOf Instance { mem = m, dsk = d, vcpus = c } =
257 ebf38064 Iustin Pop
  T.RSpec { T.rspecCpu = c, T.rspecMem = m, T.rspecDsk = d }
258 a10a476a Iustin Pop
259 aa5b2f07 Iustin Pop
-- | Checks if an instance is smaller than a given spec. Returns
260 a8038349 Iustin Pop
-- OpGood for a correct spec, otherwise Bad one of the possible
261 aa5b2f07 Iustin Pop
-- failure modes.
262 aa5b2f07 Iustin Pop
instBelowISpec :: Instance -> T.ISpec -> T.OpResult ()
263 aa5b2f07 Iustin Pop
instBelowISpec inst ispec
264 a8038349 Iustin Pop
  | mem inst > T.iSpecMemorySize ispec = Bad T.FailMem
265 a8038349 Iustin Pop
  | dsk inst > T.iSpecDiskSize ispec   = Bad T.FailDisk
266 a8038349 Iustin Pop
  | vcpus inst > T.iSpecCpuCount ispec = Bad T.FailCPU
267 a8038349 Iustin Pop
  | otherwise = Ok ()
268 aa5b2f07 Iustin Pop
269 aa5b2f07 Iustin Pop
-- | Checks if an instance is bigger than a given spec.
270 aa5b2f07 Iustin Pop
instAboveISpec :: Instance -> T.ISpec -> T.OpResult ()
271 aa5b2f07 Iustin Pop
instAboveISpec inst ispec
272 a8038349 Iustin Pop
  | mem inst < T.iSpecMemorySize ispec = Bad T.FailMem
273 a8038349 Iustin Pop
  | dsk inst < T.iSpecDiskSize ispec   = Bad T.FailDisk
274 a8038349 Iustin Pop
  | vcpus inst < T.iSpecCpuCount ispec = Bad T.FailCPU
275 a8038349 Iustin Pop
  | otherwise = Ok ()
276 aa5b2f07 Iustin Pop
277 aa5b2f07 Iustin Pop
-- | Checks if an instance matches a policy.
278 aa5b2f07 Iustin Pop
instMatchesPolicy :: Instance -> T.IPolicy -> T.OpResult ()
279 aa5b2f07 Iustin Pop
instMatchesPolicy inst ipol = do
280 aa5b2f07 Iustin Pop
  instAboveISpec inst (T.iPolicyMinSpec ipol)
281 aa5b2f07 Iustin Pop
  instBelowISpec inst (T.iPolicyMaxSpec ipol)
282 5b11f8db Iustin Pop
  if diskTemplate inst `elem` T.iPolicyDiskTemplates ipol
283 a8038349 Iustin Pop
    then Ok ()
284 a8038349 Iustin Pop
    else Bad T.FailDisk
285 aa5b2f07 Iustin Pop
286 8353b5e1 Iustin Pop
-- | Checks whether the instance uses a secondary node.
287 8353b5e1 Iustin Pop
--
288 8353b5e1 Iustin Pop
-- /Note:/ This should be reconciled with @'sNode' ==
289 8353b5e1 Iustin Pop
-- 'Node.noSecondary'@.
290 8353b5e1 Iustin Pop
hasSecondary :: Instance -> Bool
291 8353b5e1 Iustin Pop
hasSecondary = (== T.DTDrbd8) . diskTemplate
292 8353b5e1 Iustin Pop
293 179c0828 Iustin Pop
-- | Computed the number of nodes for a given disk template.
294 a10a476a Iustin Pop
requiredNodes :: T.DiskTemplate -> Int
295 a10a476a Iustin Pop
requiredNodes T.DTDrbd8 = 2
296 a10a476a Iustin Pop
requiredNodes _         = 1
297 d254d6ce Iustin Pop
298 d254d6ce Iustin Pop
-- | Computes all nodes of an instance.
299 d254d6ce Iustin Pop
allNodes :: Instance -> [T.Ndx]
300 d254d6ce Iustin Pop
allNodes inst = case diskTemplate inst of
301 d254d6ce Iustin Pop
                  T.DTDrbd8 -> [pNode inst, sNode inst]
302 d254d6ce Iustin Pop
                  _ -> [pNode inst]
303 8353b5e1 Iustin Pop
304 8353b5e1 Iustin Pop
-- | Checks whether a given disk template uses local storage.
305 8353b5e1 Iustin Pop
usesLocalStorage :: Instance -> Bool
306 8353b5e1 Iustin Pop
usesLocalStorage = (`elem` localStorageTemplates) . diskTemplate
307 8353b5e1 Iustin Pop
308 8353b5e1 Iustin Pop
-- | Checks whether a given disk template supported moves.
309 8353b5e1 Iustin Pop
supportsMoves :: T.DiskTemplate -> Bool
310 8353b5e1 Iustin Pop
supportsMoves = (`elem` movableDiskTemplates)
311 fafd0773 Iustin Pop
312 fafd0773 Iustin Pop
-- | A simple wrapper over 'T.templateMirrorType'.
313 fafd0773 Iustin Pop
mirrorType :: Instance -> T.MirrorType
314 fafd0773 Iustin Pop
mirrorType = T.templateMirrorType . diskTemplate