Statistics
| Branch: | Tag: | Revision:

root / htools / Ganeti / HTools / Instance.hs @ a8038349

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, Read, 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.AdminOffline}) = 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
  ]
150

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

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

    
157
-- * Initialization
158

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

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

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

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

    
210
-- * Update functions
211

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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