Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (6.3 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
    ) where
45

    
46
import qualified Ganeti.HTools.Types as T
47
import qualified Ganeti.HTools.Container as Container
48
import qualified Ganeti.Constants as C
49

    
50
-- * Type declarations
51

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

    
71
instance T.Element Instance where
72
    nameOf   = name
73
    idxOf    = idx
74
    setAlias = setAlias
75
    setIdx   = setIdx
76
    allNames n = [name n, alias n]
77

    
78
-- | Constant holding the running instance states.
79
runningStates :: [String]
80
runningStates = [C.inststRunning, C.inststErrorup]
81

    
82
-- | A simple name for the int, instance association list.
83
type AssocList = [(T.Idx, Instance)]
84

    
85
-- | A simple name for an instance map.
86
type List = Container.Container Instance
87

    
88
-- * Initialization
89

    
90
-- | Create an instance.
91
--
92
-- Some parameters are not initialized by function, and must be set
93
-- later (via 'setIdx' for example).
94
create :: String -> Int -> Int -> Int -> String
95
       -> [String] -> Bool -> T.Ndx -> T.Ndx -> T.DiskTemplate -> Instance
96
create name_init mem_init dsk_init vcpus_init run_init tags_init
97
       auto_balance_init pn sn dt =
98
    Instance { name = name_init
99
             , alias = name_init
100
             , mem = mem_init
101
             , dsk = dsk_init
102
             , vcpus = vcpus_init
103
             , running = run_init `elem` runningStates
104
             , runSt = run_init
105
             , pNode = pn
106
             , sNode = sn
107
             , idx = -1
108
             , util = T.baseUtil
109
             , tags = tags_init
110
             , movable = True
111
             , autoBalance = auto_balance_init
112
             , diskTemplate = dt
113
             }
114

    
115
-- | Changes the index.
116
--
117
-- This is used only during the building of the data structures.
118
setIdx :: Instance -- ^ The original instance
119
       -> T.Idx    -- ^ New index
120
       -> Instance -- ^ The modified instance
121
setIdx t i = t { idx = i }
122

    
123
-- | Changes the name.
124
--
125
-- This is used only during the building of the data structures.
126
setName :: Instance -- ^ The original instance
127
        -> String   -- ^ New name
128
        -> Instance -- ^ The modified instance
129
setName t s = t { name = s, alias = s }
130

    
131
-- | Changes the alias.
132
--
133
-- This is used only during the building of the data structures.
134
setAlias :: Instance -- ^ The original instance
135
         -> String   -- ^ New alias
136
         -> Instance -- ^ The modified instance
137
setAlias t s = t { alias = s }
138

    
139
-- * Update functions
140

    
141
-- | Changes the primary node of the instance.
142
setPri :: Instance  -- ^ the original instance
143
        -> T.Ndx    -- ^ the new primary node
144
        -> Instance -- ^ the modified instance
145
setPri t p = t { pNode = p }
146

    
147
-- | Changes the secondary node of the instance.
148
setSec :: Instance  -- ^ the original instance
149
        -> T.Ndx    -- ^ the new secondary node
150
        -> Instance -- ^ the modified instance
151
setSec t s = t { sNode = s }
152

    
153
-- | Changes both nodes of the instance.
154
setBoth :: Instance  -- ^ the original instance
155
         -> T.Ndx    -- ^ new primary node index
156
         -> T.Ndx    -- ^ new secondary node index
157
         -> Instance -- ^ the modified instance
158
setBoth t p s = t { pNode = p, sNode = s }
159

    
160
setMovable :: Instance -- ^ The original instance
161
           -> Bool     -- ^ New movable flag
162
           -> Instance -- ^ The modified instance
163
setMovable t m = t { movable = m }
164

    
165
-- | Try to shrink the instance based on the reason why we can't
166
-- allocate it.
167
shrinkByType :: Instance -> T.FailMode -> T.Result Instance
168
shrinkByType inst T.FailMem = let v = mem inst - T.unitMem
169
                              in if v < T.unitMem
170
                                 then T.Bad "out of memory"
171
                                 else T.Ok inst { mem = v }
172
shrinkByType inst T.FailDisk = let v = dsk inst - T.unitDsk
173
                               in if v < T.unitDsk
174
                                  then T.Bad "out of disk"
175
                                  else T.Ok inst { dsk = v }
176
shrinkByType inst T.FailCPU = let v = vcpus inst - T.unitCpu
177
                              in if v < T.unitCpu
178
                                 then T.Bad "out of vcpus"
179
                                 else T.Ok inst { vcpus = v }
180
shrinkByType _ f = T.Bad $ "Unhandled failure mode " ++ show f
181

    
182
-- | Return the spec of an instance.
183
specOf :: Instance -> T.RSpec
184
specOf Instance { mem = m, dsk = d, vcpus = c } =
185
    T.RSpec { T.rspecCpu = c, T.rspecMem = m, T.rspecDsk = d }