Statistics
| Branch: | Tag: | Revision:

root / htools / Ganeti / HTools / Instance.hs @ 8353b5e1

History | View | Annotate | Download (8.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 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
    , setIdx
35
    , setName
36
    , setAlias
37
    , setPri
38
    , setSec
39
    , setBoth
40
    , setMovable
41
    , specOf
42
    , shrinkByType
43
    , runningStates
44
    , localStorageTemplates
45
    , hasSecondary
46
    , requiredNodes
47
    , allNodes
48
    , usesLocalStorage
49
    ) where
50

    
51
import qualified Ganeti.HTools.Types as T
52
import qualified Ganeti.HTools.Container as Container
53
import qualified Ganeti.Constants as C
54

    
55
-- * Type declarations
56

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

    
76
instance T.Element Instance where
77
    nameOf   = name
78
    idxOf    = idx
79
    setAlias = setAlias
80
    setIdx   = setIdx
81
    allNames n = [name n, alias n]
82

    
83
-- | Constant holding the running instance states.
84
runningStates :: [String]
85
runningStates = [C.inststRunning, C.inststErrorup]
86

    
87
-- | Constant holding the local storage templates.
88
--
89
-- /Note:/ Currently Ganeti only exports node total/free disk space
90
-- for LVM-based storage; file-based storage is ignored in this model,
91
-- so even though file-based storage uses in reality disk space on the
92
-- node, in our model it won't affect it and we can't compute whether
93
-- there is enough disk space for a file-based instance. Therefore we
94
-- will treat this template as \'foreign\' storage.
95
localStorageTemplates :: [T.DiskTemplate]
96
localStorageTemplates = [ T.DTDrbd8, T.DTPlain ]
97

    
98
-- | Constant holding the movable disk templates.
99
--
100
-- This only determines the initial 'movable' state of the
101
-- instance. Further the movable state can be restricted more due to
102
-- user choices, etc.
103
movableDiskTemplates :: [T.DiskTemplate]
104
movableDiskTemplates = [ T.DTDrbd8, T.DTBlock, T.DTSharedFile ]
105

    
106
-- | A simple name for the int, instance association list.
107
type AssocList = [(T.Idx, Instance)]
108

    
109
-- | A simple name for an instance map.
110
type List = Container.Container Instance
111

    
112
-- * Initialization
113

    
114
-- | Create an instance.
115
--
116
-- Some parameters are not initialized by function, and must be set
117
-- later (via 'setIdx' for example).
118
create :: String -> Int -> Int -> Int -> String
119
       -> [String] -> Bool -> T.Ndx -> T.Ndx -> T.DiskTemplate -> Instance
120
create name_init mem_init dsk_init vcpus_init run_init tags_init
121
       auto_balance_init pn sn dt =
122
    Instance { name = name_init
123
             , alias = name_init
124
             , mem = mem_init
125
             , dsk = dsk_init
126
             , vcpus = vcpus_init
127
             , running = run_init `elem` runningStates
128
             , runSt = run_init
129
             , pNode = pn
130
             , sNode = sn
131
             , idx = -1
132
             , util = T.baseUtil
133
             , tags = tags_init
134
             , movable = supportsMoves dt
135
             , autoBalance = auto_balance_init
136
             , diskTemplate = dt
137
             }
138

    
139
-- | Changes the index.
140
--
141
-- This is used only during the building of the data structures.
142
setIdx :: Instance -- ^ The original instance
143
       -> T.Idx    -- ^ New index
144
       -> Instance -- ^ The modified instance
145
setIdx t i = t { idx = i }
146

    
147
-- | Changes the name.
148
--
149
-- This is used only during the building of the data structures.
150
setName :: Instance -- ^ The original instance
151
        -> String   -- ^ New name
152
        -> Instance -- ^ The modified instance
153
setName t s = t { name = s, alias = s }
154

    
155
-- | Changes the alias.
156
--
157
-- This is used only during the building of the data structures.
158
setAlias :: Instance -- ^ The original instance
159
         -> String   -- ^ New alias
160
         -> Instance -- ^ The modified instance
161
setAlias t s = t { alias = s }
162

    
163
-- * Update functions
164

    
165
-- | Changes the primary node of the instance.
166
setPri :: Instance  -- ^ the original instance
167
        -> T.Ndx    -- ^ the new primary node
168
        -> Instance -- ^ the modified instance
169
setPri t p = t { pNode = p }
170

    
171
-- | Changes the secondary node of the instance.
172
setSec :: Instance  -- ^ the original instance
173
        -> T.Ndx    -- ^ the new secondary node
174
        -> Instance -- ^ the modified instance
175
setSec t s = t { sNode = s }
176

    
177
-- | Changes both nodes of the instance.
178
setBoth :: Instance  -- ^ the original instance
179
         -> T.Ndx    -- ^ new primary node index
180
         -> T.Ndx    -- ^ new secondary node index
181
         -> Instance -- ^ the modified instance
182
setBoth t p s = t { pNode = p, sNode = s }
183

    
184
-- | Sets the movable flag on an instance.
185
setMovable :: Instance -- ^ The original instance
186
           -> Bool     -- ^ New movable flag
187
           -> Instance -- ^ The modified instance
188
setMovable t m = t { movable = m }
189

    
190
-- | Try to shrink the instance based on the reason why we can't
191
-- allocate it.
192
shrinkByType :: Instance -> T.FailMode -> T.Result Instance
193
shrinkByType inst T.FailMem = let v = mem inst - T.unitMem
194
                              in if v < T.unitMem
195
                                 then T.Bad "out of memory"
196
                                 else T.Ok inst { mem = v }
197
shrinkByType inst T.FailDisk = let v = dsk inst - T.unitDsk
198
                               in if v < T.unitDsk
199
                                  then T.Bad "out of disk"
200
                                  else T.Ok inst { dsk = v }
201
shrinkByType inst T.FailCPU = let v = vcpus inst - T.unitCpu
202
                              in if v < T.unitCpu
203
                                 then T.Bad "out of vcpus"
204
                                 else T.Ok inst { vcpus = v }
205
shrinkByType _ f = T.Bad $ "Unhandled failure mode " ++ show f
206

    
207
-- | Return the spec of an instance.
208
specOf :: Instance -> T.RSpec
209
specOf Instance { mem = m, dsk = d, vcpus = c } =
210
    T.RSpec { T.rspecCpu = c, T.rspecMem = m, T.rspecDsk = d }
211

    
212
-- | Checks whether the instance uses a secondary node.
213
--
214
-- /Note:/ This should be reconciled with @'sNode' ==
215
-- 'Node.noSecondary'@.
216
hasSecondary :: Instance -> Bool
217
hasSecondary = (== T.DTDrbd8) . diskTemplate
218

    
219
-- | Computed the number of nodes for a given disk template.
220
requiredNodes :: T.DiskTemplate -> Int
221
requiredNodes T.DTDrbd8 = 2
222
requiredNodes _         = 1
223

    
224
-- | Computes all nodes of an instance.
225
allNodes :: Instance -> [T.Ndx]
226
allNodes inst = case diskTemplate inst of
227
                  T.DTDrbd8 -> [pNode inst, sNode inst]
228
                  _ -> [pNode inst]
229

    
230
-- | Checks whether a given disk template uses local storage.
231
usesLocalStorage :: Instance -> Bool
232
usesLocalStorage = (`elem` localStorageTemplates) . diskTemplate
233

    
234
-- | Checks whether a given disk template supported moves.
235
supportsMoves :: T.DiskTemplate -> Bool
236
supportsMoves = (`elem` movableDiskTemplates)