Statistics
| Branch: | Tag: | Revision:

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

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

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

    
60
import Ganeti.HTools.Utils
61

    
62
-- * Type declarations
63

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

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

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

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

    
100
-- | Check if instance is down.
101
instanceDown :: Instance -> Bool
102
instanceDown inst | instanceRunning inst = False
103
instanceDown inst | instanceOffline inst = False
104
instanceDown _                           = True
105

    
106
-- | Apply the function if the instance is online. Otherwise use
107
-- the initial value
108
applyIfOnline :: Instance -> (a -> a) -> a -> a
109
applyIfOnline = applyIf . not . instanceOffline
110

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

    
122
-- | Constant holding the movable disk templates.
123
--
124
-- This only determines the initial 'movable' state of the
125
-- instance. Further the movable state can be restricted more due to
126
-- user choices, etc.
127
movableDiskTemplates :: [T.DiskTemplate]
128
movableDiskTemplates = [ T.DTDrbd8, T.DTBlock, T.DTSharedFile ]
129

    
130
-- | A simple name for the int, instance association list.
131
type AssocList = [(T.Idx, Instance)]
132

    
133
-- | A simple name for an instance map.
134
type List = Container.Container Instance
135

    
136
-- * Initialization
137

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

    
162
-- | Changes the index.
163
--
164
-- This is used only during the building of the data structures.
165
setIdx :: Instance -- ^ The original instance
166
       -> T.Idx    -- ^ New index
167
       -> Instance -- ^ The modified instance
168
setIdx t i = t { idx = i }
169

    
170
-- | Changes the name.
171
--
172
-- This is used only during the building of the data structures.
173
setName :: Instance -- ^ The original instance
174
        -> String   -- ^ New name
175
        -> Instance -- ^ The modified instance
176
setName t s = t { name = s, alias = s }
177

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

    
186
-- * Update functions
187

    
188
-- | Changes the primary node of the instance.
189
setPri :: Instance  -- ^ the original instance
190
        -> T.Ndx    -- ^ the new primary node
191
        -> Instance -- ^ the modified instance
192
setPri t p = t { pNode = p }
193

    
194
-- | Changes the secondary node of the instance.
195
setSec :: Instance  -- ^ the original instance
196
        -> T.Ndx    -- ^ the new secondary node
197
        -> Instance -- ^ the modified instance
198
setSec t s = t { sNode = s }
199

    
200
-- | Changes both nodes of the instance.
201
setBoth :: Instance  -- ^ the original instance
202
         -> T.Ndx    -- ^ new primary node index
203
         -> T.Ndx    -- ^ new secondary node index
204
         -> Instance -- ^ the modified instance
205
setBoth t p s = t { pNode = p, sNode = s }
206

    
207
-- | Sets the movable flag on an instance.
208
setMovable :: Instance -- ^ The original instance
209
           -> Bool     -- ^ New movable flag
210
           -> Instance -- ^ The modified instance
211
setMovable t m = t { movable = m }
212

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

    
230
-- | Return the spec of an instance.
231
specOf :: Instance -> T.RSpec
232
specOf Instance { mem = m, dsk = d, vcpus = c } =
233
  T.RSpec { T.rspecCpu = c, T.rspecMem = m, T.rspecDsk = d }
234

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

    
245
-- | Checks if an instance is bigger than a given spec.
246
instAboveISpec :: Instance -> T.ISpec -> T.OpResult ()
247
instAboveISpec inst ispec
248
  | mem inst < T.iSpecMemorySize ispec = T.OpFail T.FailMem
249
  | dsk inst < T.iSpecDiskSize ispec   = T.OpFail T.FailDisk
250
  | vcpus inst < T.iSpecCpuCount ispec = T.OpFail T.FailCPU
251
  | otherwise = T.OpGood ()
252

    
253
-- | Checks if an instance matches a policy.
254
instMatchesPolicy :: Instance -> T.IPolicy -> T.OpResult ()
255
instMatchesPolicy inst ipol = do
256
  instAboveISpec inst (T.iPolicyMinSpec ipol)
257
  instBelowISpec inst (T.iPolicyMaxSpec ipol)
258
  if (diskTemplate inst `elem` T.iPolicyDiskTemplates ipol)
259
    then T.OpGood ()
260
    else T.OpFail T.FailDisk
261

    
262
-- | Checks whether the instance uses a secondary node.
263
--
264
-- /Note:/ This should be reconciled with @'sNode' ==
265
-- 'Node.noSecondary'@.
266
hasSecondary :: Instance -> Bool
267
hasSecondary = (== T.DTDrbd8) . diskTemplate
268

    
269
-- | Computed the number of nodes for a given disk template.
270
requiredNodes :: T.DiskTemplate -> Int
271
requiredNodes T.DTDrbd8 = 2
272
requiredNodes _         = 1
273

    
274
-- | Computes all nodes of an instance.
275
allNodes :: Instance -> [T.Ndx]
276
allNodes inst = case diskTemplate inst of
277
                  T.DTDrbd8 -> [pNode inst, sNode inst]
278
                  _ -> [pNode inst]
279

    
280
-- | Checks whether a given disk template uses local storage.
281
usesLocalStorage :: Instance -> Bool
282
usesLocalStorage = (`elem` localStorageTemplates) . diskTemplate
283

    
284
-- | Checks whether a given disk template supported moves.
285
supportsMoves :: T.DiskTemplate -> Bool
286
supportsMoves = (`elem` movableDiskTemplates)