Statistics
| Branch: | Tag: | Revision:

root / htools / Ganeti / HTools / Instance.hs @ 9cd6c325

History | View | Annotate | Download (10 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
  , instanceRunning
35
  , instanceOffline
36
  , instanceNotOffline
37
  , instanceDown
38
  , applyIfOnline
39
  , setIdx
40
  , setName
41
  , setAlias
42
  , setPri
43
  , setSec
44
  , setBoth
45
  , setMovable
46
  , specOf
47
  , instBelowISpec
48
  , instAboveISpec
49
  , instMatchesPolicy
50
  , shrinkByType
51
  , localStorageTemplates
52
  , hasSecondary
53
  , requiredNodes
54
  , allNodes
55
  , usesLocalStorage
56
  ) where
57

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

    
61
import Ganeti.HTools.Utils
62

    
63
-- * Type declarations
64

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

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

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

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

    
101

    
102
-- | Helper to check if the instance is not offline.
103
instanceNotOffline :: Instance -> Bool
104
instanceNotOffline = not . instanceOffline
105

    
106
-- | Check if instance is down.
107
instanceDown :: Instance -> Bool
108
instanceDown inst | instanceRunning inst = False
109
instanceDown inst | instanceOffline inst = False
110
instanceDown _                           = True
111

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

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

    
128
-- | Constant holding the movable disk templates.
129
--
130
-- This only determines the initial 'movable' state of the
131
-- instance. Further the movable state can be restricted more due to
132
-- user choices, etc.
133
movableDiskTemplates :: [T.DiskTemplate]
134
movableDiskTemplates = [ T.DTDrbd8, T.DTBlock, T.DTSharedFile ]
135

    
136
-- | A simple name for the int, instance association list.
137
type AssocList = [(T.Idx, Instance)]
138

    
139
-- | A simple name for an instance map.
140
type List = Container.Container Instance
141

    
142
-- * Initialization
143

    
144
-- | Create an instance.
145
--
146
-- Some parameters are not initialized by function, and must be set
147
-- later (via 'setIdx' for example).
148
create :: String -> Int -> Int -> Int -> T.InstanceStatus
149
       -> [String] -> Bool -> T.Ndx -> T.Ndx -> T.DiskTemplate -> Instance
150
create name_init mem_init dsk_init vcpus_init run_init tags_init
151
       auto_balance_init pn sn dt =
152
  Instance { name = name_init
153
           , alias = name_init
154
           , mem = mem_init
155
           , dsk = dsk_init
156
           , vcpus = vcpus_init
157
           , runSt = run_init
158
           , pNode = pn
159
           , sNode = sn
160
           , idx = -1
161
           , util = T.baseUtil
162
           , tags = tags_init
163
           , movable = supportsMoves dt
164
           , autoBalance = auto_balance_init
165
           , diskTemplate = dt
166
           }
167

    
168
-- | Changes the index.
169
--
170
-- This is used only during the building of the data structures.
171
setIdx :: Instance -- ^ The original instance
172
       -> T.Idx    -- ^ New index
173
       -> Instance -- ^ The modified instance
174
setIdx t i = t { idx = i }
175

    
176
-- | Changes the name.
177
--
178
-- This is used only during the building of the data structures.
179
setName :: Instance -- ^ The original instance
180
        -> String   -- ^ New name
181
        -> Instance -- ^ The modified instance
182
setName t s = t { name = s, alias = s }
183

    
184
-- | Changes the alias.
185
--
186
-- This is used only during the building of the data structures.
187
setAlias :: Instance -- ^ The original instance
188
         -> String   -- ^ New alias
189
         -> Instance -- ^ The modified instance
190
setAlias t s = t { alias = s }
191

    
192
-- * Update functions
193

    
194
-- | Changes the primary node of the instance.
195
setPri :: Instance  -- ^ the original instance
196
        -> T.Ndx    -- ^ the new primary node
197
        -> Instance -- ^ the modified instance
198
setPri t p = t { pNode = p }
199

    
200
-- | Changes the secondary node of the instance.
201
setSec :: Instance  -- ^ the original instance
202
        -> T.Ndx    -- ^ the new secondary node
203
        -> Instance -- ^ the modified instance
204
setSec t s = t { sNode = s }
205

    
206
-- | Changes both nodes of the instance.
207
setBoth :: Instance  -- ^ the original instance
208
         -> T.Ndx    -- ^ new primary node index
209
         -> T.Ndx    -- ^ new secondary node index
210
         -> Instance -- ^ the modified instance
211
setBoth t p s = t { pNode = p, sNode = s }
212

    
213
-- | Sets the movable flag on an instance.
214
setMovable :: Instance -- ^ The original instance
215
           -> Bool     -- ^ New movable flag
216
           -> Instance -- ^ The modified instance
217
setMovable t m = t { movable = m }
218

    
219
-- | Try to shrink the instance based on the reason why we can't
220
-- allocate it.
221
shrinkByType :: Instance -> T.FailMode -> T.Result Instance
222
shrinkByType inst T.FailMem = let v = mem inst - T.unitMem
223
                              in if v < T.unitMem
224
                                 then T.Bad "out of memory"
225
                                 else T.Ok inst { mem = v }
226
shrinkByType inst T.FailDisk = let v = dsk inst - T.unitDsk
227
                               in if v < T.unitDsk
228
                                  then T.Bad "out of disk"
229
                                  else T.Ok inst { dsk = v }
230
shrinkByType inst T.FailCPU = let v = vcpus inst - T.unitCpu
231
                              in if v < T.unitCpu
232
                                 then T.Bad "out of vcpus"
233
                                 else T.Ok inst { vcpus = v }
234
shrinkByType _ f = T.Bad $ "Unhandled failure mode " ++ show f
235

    
236
-- | Return the spec of an instance.
237
specOf :: Instance -> T.RSpec
238
specOf Instance { mem = m, dsk = d, vcpus = c } =
239
  T.RSpec { T.rspecCpu = c, T.rspecMem = m, T.rspecDsk = d }
240

    
241
-- | Checks if an instance is smaller than a given spec. Returns
242
-- OpGood for a correct spec, otherwise OpFail one of the possible
243
-- failure modes.
244
instBelowISpec :: Instance -> T.ISpec -> T.OpResult ()
245
instBelowISpec inst ispec
246
  | mem inst > T.iSpecMemorySize ispec = T.OpFail T.FailMem
247
  | dsk inst > T.iSpecDiskSize ispec   = T.OpFail T.FailDisk
248
  | vcpus inst > T.iSpecCpuCount ispec = T.OpFail T.FailCPU
249
  | otherwise = T.OpGood ()
250

    
251
-- | Checks if an instance is bigger than a given spec.
252
instAboveISpec :: Instance -> T.ISpec -> T.OpResult ()
253
instAboveISpec inst ispec
254
  | mem inst < T.iSpecMemorySize ispec = T.OpFail T.FailMem
255
  | dsk inst < T.iSpecDiskSize ispec   = T.OpFail T.FailDisk
256
  | vcpus inst < T.iSpecCpuCount ispec = T.OpFail T.FailCPU
257
  | otherwise = T.OpGood ()
258

    
259
-- | Checks if an instance matches a policy.
260
instMatchesPolicy :: Instance -> T.IPolicy -> T.OpResult ()
261
instMatchesPolicy inst ipol = do
262
  instAboveISpec inst (T.iPolicyMinSpec ipol)
263
  instBelowISpec inst (T.iPolicyMaxSpec ipol)
264
  if (diskTemplate inst `elem` T.iPolicyDiskTemplates ipol)
265
    then T.OpGood ()
266
    else T.OpFail T.FailDisk
267

    
268
-- | Checks whether the instance uses a secondary node.
269
--
270
-- /Note:/ This should be reconciled with @'sNode' ==
271
-- 'Node.noSecondary'@.
272
hasSecondary :: Instance -> Bool
273
hasSecondary = (== T.DTDrbd8) . diskTemplate
274

    
275
-- | Computed the number of nodes for a given disk template.
276
requiredNodes :: T.DiskTemplate -> Int
277
requiredNodes T.DTDrbd8 = 2
278
requiredNodes _         = 1
279

    
280
-- | Computes all nodes of an instance.
281
allNodes :: Instance -> [T.Ndx]
282
allNodes inst = case diskTemplate inst of
283
                  T.DTDrbd8 -> [pNode inst, sNode inst]
284
                  _ -> [pNode inst]
285

    
286
-- | Checks whether a given disk template uses local storage.
287
usesLocalStorage :: Instance -> Bool
288
usesLocalStorage = (`elem` localStorageTemplates) . diskTemplate
289

    
290
-- | Checks whether a given disk template supported moves.
291
supportsMoves :: T.DiskTemplate -> Bool
292
supportsMoves = (`elem` movableDiskTemplates)