Statistics
| Branch: | Tag: | Revision:

root / htools / Ganeti / HTools / Instance.hs @ 93c8a7b3

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 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
  , tags         :: [String]  -- ^ List of instance tags
82
  , diskTemplate :: T.DiskTemplate -- ^ The disk template of the instance
83
  , spindleUse   :: Int       -- ^ The numbers of used spindles
84
  } deriving (Show, Read, Eq)
85

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

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

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

    
104

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

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

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

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

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

    
136
-- | Constant holding the movable disk templates.
137
--
138
-- This only determines the initial 'movable' state of the
139
-- instance. Further the movable state can be restricted more due to
140
-- user choices, etc.
141
movableDiskTemplates :: [T.DiskTemplate]
142
movableDiskTemplates =
143
  [ T.DTDrbd8
144
  , T.DTBlock
145
  , T.DTSharedFile
146
  , T.DTRbd
147
  , T.DTExt
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
           , tags = tags_init
178
           , movable = supportsMoves dt
179
           , autoBalance = auto_balance_init
180
           , diskTemplate = dt
181
           , spindleUse = su
182
           }
183

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

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

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

    
208
-- * Update functions
209

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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