Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (10.1 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
  ) where
58

    
59
import qualified Ganeti.HTools.Types as T
60
import qualified Ganeti.HTools.Container as Container
61

    
62
import Ganeti.HTools.Utils
63

    
64
-- * Type declarations
65

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

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

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

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

    
102

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

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

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

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

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

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

    
147
-- | A simple name for the int, instance association list.
148
type AssocList = [(T.Idx, Instance)]
149

    
150
-- | A simple name for an instance map.
151
type List = Container.Container Instance
152

    
153
-- * Initialization
154

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

    
179
-- | Changes the index.
180
--
181
-- This is used only during the building of the data structures.
182
setIdx :: Instance -- ^ The original instance
183
       -> T.Idx    -- ^ New index
184
       -> Instance -- ^ The modified instance
185
setIdx t i = t { idx = i }
186

    
187
-- | Changes the name.
188
--
189
-- This is used only during the building of the data structures.
190
setName :: Instance -- ^ The original instance
191
        -> String   -- ^ New name
192
        -> Instance -- ^ The modified instance
193
setName t s = t { name = s, alias = s }
194

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

    
203
-- * Update functions
204

    
205
-- | Changes the primary node of the instance.
206
setPri :: Instance  -- ^ the original instance
207
        -> T.Ndx    -- ^ the new primary node
208
        -> Instance -- ^ the modified instance
209
setPri t p = t { pNode = p }
210

    
211
-- | Changes the secondary node of the instance.
212
setSec :: Instance  -- ^ the original instance
213
        -> T.Ndx    -- ^ the new secondary node
214
        -> Instance -- ^ the modified instance
215
setSec t s = t { sNode = s }
216

    
217
-- | Changes both nodes of the instance.
218
setBoth :: Instance  -- ^ the original instance
219
         -> T.Ndx    -- ^ new primary node index
220
         -> T.Ndx    -- ^ new secondary node index
221
         -> Instance -- ^ the modified instance
222
setBoth t p s = t { pNode = p, sNode = s }
223

    
224
-- | Sets the movable flag on an instance.
225
setMovable :: Instance -- ^ The original instance
226
           -> Bool     -- ^ New movable flag
227
           -> Instance -- ^ The modified instance
228
setMovable t m = t { movable = m }
229

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

    
247
-- | Return the spec of an instance.
248
specOf :: Instance -> T.RSpec
249
specOf Instance { mem = m, dsk = d, vcpus = c } =
250
  T.RSpec { T.rspecCpu = c, T.rspecMem = m, T.rspecDsk = d }
251

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

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

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

    
279
-- | Checks whether the instance uses a secondary node.
280
--
281
-- /Note:/ This should be reconciled with @'sNode' ==
282
-- 'Node.noSecondary'@.
283
hasSecondary :: Instance -> Bool
284
hasSecondary = (== T.DTDrbd8) . diskTemplate
285

    
286
-- | Computed the number of nodes for a given disk template.
287
requiredNodes :: T.DiskTemplate -> Int
288
requiredNodes T.DTDrbd8 = 2
289
requiredNodes _         = 1
290

    
291
-- | Computes all nodes of an instance.
292
allNodes :: Instance -> [T.Ndx]
293
allNodes inst = case diskTemplate inst of
294
                  T.DTDrbd8 -> [pNode inst, sNode inst]
295
                  _ -> [pNode inst]
296

    
297
-- | Checks whether a given disk template uses local storage.
298
usesLocalStorage :: Instance -> Bool
299
usesLocalStorage = (`elem` localStorageTemplates) . diskTemplate
300

    
301
-- | Checks whether a given disk template supported moves.
302
supportsMoves :: T.DiskTemplate -> Bool
303
supportsMoves = (`elem` movableDiskTemplates)