Statistics
| Branch: | Tag: | Revision:

root / htools / Ganeti / HTools / Instance.hs @ 2f907bad

History | View | Annotate | Download (10.5 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 qualified Ganeti.HTools.Types as T
61
import qualified Ganeti.HTools.Container as Container
62

    
63
import Ganeti.HTools.Utils
64

    
65
-- * Type declarations
66

    
67
-- | The instance type.
68
data Instance = Instance
69
  { name         :: String    -- ^ The instance name
70
  , alias        :: String    -- ^ The shortened name
71
  , mem          :: Int       -- ^ Memory of the instance
72
  , dsk          :: Int       -- ^ Disk size of instance
73
  , vcpus        :: Int       -- ^ Number of VCPUs
74
  , runSt        :: T.InstanceStatus -- ^ Original run status
75
  , pNode        :: T.Ndx     -- ^ Original primary node
76
  , sNode        :: T.Ndx     -- ^ Original secondary node
77
  , idx          :: T.Idx     -- ^ Internal index
78
  , util         :: T.DynUtil -- ^ Dynamic resource usage
79
  , movable      :: Bool      -- ^ Can and should the instance be moved?
80
  , autoBalance  :: Bool      -- ^ Is the instance auto-balanced?
81
  , diskTemplate :: T.DiskTemplate -- ^ The disk template of the instance
82
  , spindleUse   :: Int       -- ^ The numbers of used spindles
83
  , allTags      :: [String]  -- ^ List of all instance tags
84
  , exclTags     :: [String]  -- ^ List of instance exclusion tags
85
  } deriving (Show, Read, Eq)
86

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

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

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

    
105

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

    
110
-- | Check if instance is down.
111
instanceDown :: Instance -> Bool
112
instanceDown inst | isRunning inst = False
113
instanceDown inst | isOffline inst = False
114
instanceDown _                     = True
115

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

    
121
-- | Helper for determining whether an instance's memory needs to be
122
-- taken into account for secondary memory reservation.
123
usesSecMem :: Instance -> Bool
124
usesSecMem inst = notOffline inst && autoBalance inst
125

    
126
-- | Constant holding the local storage templates.
127
--
128
-- /Note:/ Currently Ganeti only exports node total/free disk space
129
-- for LVM-based storage; file-based storage is ignored in this model,
130
-- so even though file-based storage uses in reality disk space on the
131
-- node, in our model it won't affect it and we can't compute whether
132
-- there is enough disk space for a file-based instance. Therefore we
133
-- will treat this template as \'foreign\' storage.
134
localStorageTemplates :: [T.DiskTemplate]
135
localStorageTemplates = [ T.DTDrbd8, T.DTPlain ]
136

    
137
-- | Constant holding the movable disk templates.
138
--
139
-- This only determines the initial 'movable' state of the
140
-- instance. Further the movable state can be restricted more due to
141
-- user choices, etc.
142
movableDiskTemplates :: [T.DiskTemplate]
143
movableDiskTemplates =
144
  [ T.DTDrbd8
145
  , T.DTBlock
146
  , T.DTSharedFile
147
  , T.DTRbd
148
  ]
149

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

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

    
156
-- * Initialization
157

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

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

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

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

    
209
-- * Update functions
210

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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