Node operations: take into account auto_balance
[ganeti-local] / htools / Ganeti / HTools / Instance.hs
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
49 -- * Type declarations
50
51 -- | The instance type
52 data Instance = Instance
53     { name         :: String    -- ^ The instance name
54     , alias        :: String    -- ^ The shortened name
55     , mem          :: Int       -- ^ Memory of the instance
56     , dsk          :: Int       -- ^ Disk size of instance
57     , vcpus        :: Int       -- ^ Number of VCPUs
58     , running      :: Bool      -- ^ Is the instance running?
59     , runSt        :: String    -- ^ Original (text) run status
60     , pNode        :: T.Ndx     -- ^ Original primary node
61     , sNode        :: T.Ndx     -- ^ Original secondary node
62     , idx          :: T.Idx     -- ^ Internal index
63     , util         :: T.DynUtil -- ^ Dynamic resource usage
64     , movable      :: Bool      -- ^ Can the instance be moved?
65     , auto_balance :: Bool      -- ^ Is the instance auto-balanced?
66     , tags         :: [String]  -- ^ List of instance tags
67     } deriving (Show, Read)
68
69 instance T.Element Instance where
70     nameOf   = name
71     idxOf    = idx
72     setAlias = setAlias
73     setIdx   = setIdx
74     allNames n = [name n, alias n]
75
76 -- | Running instance states.
77 runningStates :: [String]
78 runningStates = ["running", "ERROR_up"]
79
80 -- | A simple name for the int, instance association list.
81 type AssocList = [(T.Idx, Instance)]
82
83 -- | A simple name for an instance map.
84 type List = Container.Container Instance
85
86 -- * Initialization
87
88 -- | Create an instance.
89 --
90 -- Some parameters are not initialized by function, and must be set
91 -- later (via 'setIdx' for example).
92 create :: String -> Int -> Int -> Int -> String
93        -> [String] -> Bool -> T.Ndx -> T.Ndx -> Instance
94 create name_init mem_init dsk_init vcpus_init run_init tags_init
95        auto_balance_init pn sn =
96     Instance { name = name_init
97              , alias = name_init
98              , mem = mem_init
99              , dsk = dsk_init
100              , vcpus = vcpus_init
101              , running = run_init `elem` runningStates
102              , runSt = run_init
103              , pNode = pn
104              , sNode = sn
105              , idx = -1
106              , util = T.baseUtil
107              , tags = tags_init
108              , movable = True
109              , auto_balance = auto_balance_init
110              }
111
112 -- | Changes the index.
113 --
114 -- This is used only during the building of the data structures.
115 setIdx :: Instance -- ^ The original instance
116        -> T.Idx    -- ^ New index
117        -> Instance -- ^ The modified instance
118 setIdx t i = t { idx = i }
119
120 -- | Changes the name.
121 --
122 -- This is used only during the building of the data structures.
123 setName :: Instance -- ^ The original instance
124         -> String   -- ^ New name
125         -> Instance -- ^ The modified instance
126 setName t s = t { name = s, alias = s }
127
128 -- | Changes the alias.
129 --
130 -- This is used only during the building of the data structures.
131 setAlias :: Instance -- ^ The original instance
132          -> String   -- ^ New alias
133          -> Instance -- ^ The modified instance
134 setAlias t s = t { alias = s }
135
136 -- * Update functions
137
138 -- | Changes the primary node of the instance.
139 setPri :: Instance  -- ^ the original instance
140         -> T.Ndx    -- ^ the new primary node
141         -> Instance -- ^ the modified instance
142 setPri t p = t { pNode = p }
143
144 -- | Changes the secondary node of the instance.
145 setSec :: Instance  -- ^ the original instance
146         -> T.Ndx    -- ^ the new secondary node
147         -> Instance -- ^ the modified instance
148 setSec t s = t { sNode = s }
149
150 -- | Changes both nodes of the instance.
151 setBoth :: Instance  -- ^ the original instance
152          -> T.Ndx    -- ^ new primary node index
153          -> T.Ndx    -- ^ new secondary node index
154          -> Instance -- ^ the modified instance
155 setBoth t p s = t { pNode = p, sNode = s }
156
157 setMovable :: Instance -- ^ The original instance
158            -> Bool     -- ^ New movable flag
159            -> Instance -- ^ The modified instance
160 setMovable t m = t { movable = m }
161
162 -- | Try to shrink the instance based on the reason why we can't
163 -- allocate it.
164 shrinkByType :: Instance -> T.FailMode -> T.Result Instance
165 shrinkByType inst T.FailMem = let v = mem inst - T.unitMem
166                               in if v < T.unitMem
167                                  then T.Bad "out of memory"
168                                  else T.Ok inst { mem = v }
169 shrinkByType inst T.FailDisk = let v = dsk inst - T.unitDsk
170                                in if v < T.unitDsk
171                                   then T.Bad "out of disk"
172                                   else T.Ok inst { dsk = v }
173 shrinkByType inst T.FailCPU = let v = vcpus inst - T.unitCpu
174                               in if v < T.unitCpu
175                                  then T.Bad "out of vcpus"
176                                  else T.Ok inst { vcpus = v }
177 shrinkByType _ f = T.Bad $ "Unhandled failure mode " ++ show f
178
179 -- | Return the spec of an instance.
180 specOf :: Instance -> T.RSpec
181 specOf Instance { mem = m, dsk = d, vcpus = c } =
182     T.RSpec { T.rspecCpu = c, T.rspecMem = m, T.rspecDsk = d }