Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / HTools / Instance.hs @ 3add7574

History | View | Annotate | Download (10.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 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       -- ^ 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, Eq)
87

    
88
instance T.Element Instance where
89
  nameOf   = name
90
  idxOf    = idx
91
  setAlias = setAlias
92
  setIdx   = setIdx
93
  allNames n = [name n, alias n]
94

    
95
-- | Check if instance is running.
96
isRunning :: Instance -> Bool
97
isRunning (Instance {runSt = T.Running}) = True
98
isRunning (Instance {runSt = T.ErrorUp}) = True
99
isRunning _                              = False
100

    
101
-- | Check if instance is offline.
102
isOffline :: Instance -> Bool
103
isOffline (Instance {runSt = T.StatusOffline}) = True
104
isOffline _                                    = False
105

    
106

    
107
-- | Helper to check if the instance is not offline.
108
notOffline :: Instance -> Bool
109
notOffline = not . isOffline
110

    
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
116

    
117
-- | Apply the function if the instance is online. Otherwise use
118
-- the initial value
119
applyIfOnline :: Instance -> (a -> a) -> a -> a
120
applyIfOnline = applyIf . notOffline
121

    
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
126

    
127
-- | Constant holding the local storage templates.
128
--
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 ]
137

    
138
-- | Constant holding the movable disk templates.
139
--
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 =
145
  [ T.DTDrbd8
146
  , T.DTBlock
147
  , T.DTSharedFile
148
  , T.DTRbd
149
  , T.DTExt
150
  ]
151

    
152
-- | A simple name for the int, instance association list.
153
type AssocList = [(T.Idx, Instance)]
154

    
155
-- | A simple name for an instance map.
156
type List = Container.Container Instance
157

    
158
-- * Initialization
159

    
160
-- | Create an instance.
161
--
162
-- Some parameters are not initialized by function, and must be set
163
-- later (via 'setIdx' for example).
164
create :: String -> Int -> Int -> Int -> T.InstanceStatus
165
       -> [String] -> Bool -> T.Ndx -> T.Ndx -> T.DiskTemplate -> Int
166
       -> Instance
167
create name_init mem_init dsk_init vcpus_init run_init tags_init
168
       auto_balance_init pn sn dt su =
169
  Instance { name = name_init
170
           , alias = name_init
171
           , mem = mem_init
172
           , dsk = dsk_init
173
           , vcpus = vcpus_init
174
           , runSt = run_init
175
           , pNode = pn
176
           , sNode = sn
177
           , idx = -1
178
           , util = T.baseUtil
179
           , movable = supportsMoves dt
180
           , autoBalance = auto_balance_init
181
           , diskTemplate = dt
182
           , spindleUse = su
183
           , allTags = tags_init
184
           , exclTags = []
185
           }
186

    
187
-- | Changes the index.
188
--
189
-- This is used only during the building of the data structures.
190
setIdx :: Instance -- ^ The original instance
191
       -> T.Idx    -- ^ New index
192
       -> Instance -- ^ The modified instance
193
setIdx t i = t { idx = i }
194

    
195
-- | Changes the name.
196
--
197
-- This is used only during the building of the data structures.
198
setName :: Instance -- ^ The original instance
199
        -> String   -- ^ New name
200
        -> Instance -- ^ The modified instance
201
setName t s = t { name = s, alias = s }
202

    
203
-- | Changes the alias.
204
--
205
-- This is used only during the building of the data structures.
206
setAlias :: Instance -- ^ The original instance
207
         -> String   -- ^ New alias
208
         -> Instance -- ^ The modified instance
209
setAlias t s = t { alias = s }
210

    
211
-- * Update functions
212

    
213
-- | Changes the primary node of the instance.
214
setPri :: Instance  -- ^ the original instance
215
        -> T.Ndx    -- ^ the new primary node
216
        -> Instance -- ^ the modified instance
217
setPri t p = t { pNode = p }
218

    
219
-- | Changes the secondary node of the instance.
220
setSec :: Instance  -- ^ the original instance
221
        -> T.Ndx    -- ^ the new secondary node
222
        -> Instance -- ^ the modified instance
223
setSec t s = t { sNode = s }
224

    
225
-- | Changes both nodes of the instance.
226
setBoth :: Instance  -- ^ the original instance
227
         -> T.Ndx    -- ^ new primary node index
228
         -> T.Ndx    -- ^ new secondary node index
229
         -> Instance -- ^ the modified instance
230
setBoth t p s = t { pNode = p, sNode = s }
231

    
232
-- | Sets the movable flag on an instance.
233
setMovable :: Instance -- ^ The original instance
234
           -> Bool     -- ^ New movable flag
235
           -> Instance -- ^ The modified instance
236
setMovable t m = t { movable = m }
237

    
238
-- | Try to shrink the instance based on the reason why we can't
239
-- allocate it.
240
shrinkByType :: Instance -> T.FailMode -> Result Instance
241
shrinkByType inst T.FailMem = let v = mem inst - T.unitMem
242
                              in if v < T.unitMem
243
                                 then Bad "out of memory"
244
                                 else Ok inst { mem = v }
245
shrinkByType inst T.FailDisk = let v = dsk inst - T.unitDsk
246
                               in if v < T.unitDsk
247
                                  then Bad "out of disk"
248
                                  else Ok inst { dsk = v }
249
shrinkByType inst T.FailCPU = let v = vcpus inst - T.unitCpu
250
                              in if v < T.unitCpu
251
                                 then Bad "out of vcpus"
252
                                 else Ok inst { vcpus = v }
253
shrinkByType _ f = Bad $ "Unhandled failure mode " ++ show f
254

    
255
-- | Return the spec of an instance.
256
specOf :: Instance -> T.RSpec
257
specOf Instance { mem = m, dsk = d, vcpus = c } =
258
  T.RSpec { T.rspecCpu = c, T.rspecMem = m, T.rspecDsk = d }
259

    
260
-- | Checks if an instance is smaller than a given spec. Returns
261
-- OpGood for a correct spec, otherwise Bad one of the possible
262
-- failure modes.
263
instBelowISpec :: Instance -> T.ISpec -> T.OpResult ()
264
instBelowISpec inst ispec
265
  | mem inst > T.iSpecMemorySize ispec = Bad T.FailMem
266
  | dsk inst > T.iSpecDiskSize ispec   = Bad T.FailDisk
267
  | vcpus inst > T.iSpecCpuCount ispec = Bad T.FailCPU
268
  | otherwise = Ok ()
269

    
270
-- | Checks if an instance is bigger than a given spec.
271
instAboveISpec :: Instance -> T.ISpec -> T.OpResult ()
272
instAboveISpec inst ispec
273
  | mem inst < T.iSpecMemorySize ispec = Bad T.FailMem
274
  | dsk inst < T.iSpecDiskSize ispec   = Bad T.FailDisk
275
  | vcpus inst < T.iSpecCpuCount ispec = Bad T.FailCPU
276
  | otherwise = Ok ()
277

    
278
-- | Checks if an instance matches a policy.
279
instMatchesPolicy :: Instance -> T.IPolicy -> T.OpResult ()
280
instMatchesPolicy inst ipol = do
281
  instAboveISpec inst (T.iPolicyMinSpec ipol)
282
  instBelowISpec inst (T.iPolicyMaxSpec ipol)
283
  if diskTemplate inst `elem` T.iPolicyDiskTemplates ipol
284
    then Ok ()
285
    else Bad T.FailDisk
286

    
287
-- | Checks whether the instance uses a secondary node.
288
--
289
-- /Note:/ This should be reconciled with @'sNode' ==
290
-- 'Node.noSecondary'@.
291
hasSecondary :: Instance -> Bool
292
hasSecondary = (== T.DTDrbd8) . diskTemplate
293

    
294
-- | Computed the number of nodes for a given disk template.
295
requiredNodes :: T.DiskTemplate -> Int
296
requiredNodes T.DTDrbd8 = 2
297
requiredNodes _         = 1
298

    
299
-- | Computes all nodes of an instance.
300
allNodes :: Instance -> [T.Ndx]
301
allNodes inst = case diskTemplate inst of
302
                  T.DTDrbd8 -> [pNode inst, sNode inst]
303
                  _ -> [pNode inst]
304

    
305
-- | Checks whether a given disk template uses local storage.
306
usesLocalStorage :: Instance -> Bool
307
usesLocalStorage = (`elem` localStorageTemplates) . diskTemplate
308

    
309
-- | Checks whether a given disk template supported moves.
310
supportsMoves :: T.DiskTemplate -> Bool
311
supportsMoves = (`elem` movableDiskTemplates)
312

    
313
-- | A simple wrapper over 'T.templateMirrorType'.
314
mirrorType :: Instance -> T.MirrorType
315
mirrorType = T.templateMirrorType . diskTemplate