Statistics
| Branch: | Tag: | Revision:

root / htools / Ganeti / HTools / Instance.hs @ 981bb5cf

History | View | Annotate | Download (10.2 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
  , spindleUsage :: Int       -- ^ The numbers of used spindles
83
  } deriving (Show, Read, Eq)
84

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

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

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

    
103

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

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

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

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

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

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

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

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

    
154
-- * Initialization
155

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

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

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

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

    
206
-- * Update functions
207

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

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

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

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

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

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

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

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

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

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

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

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

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

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