1 {-| Module describing an instance.
3 The instance data type holds very few fields, the algorithm
4 intelligence is in the "Node" and "Cluster" modules.
10 Copyright (C) 2009, 2010, 2011 Google Inc.
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.
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.
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
29 module Ganeti.HTools.Instance
44 , localStorageTemplates
51 import qualified Ganeti.HTools.Types as T
52 import qualified Ganeti.HTools.Container as Container
53 import qualified Ganeti.Constants as C
55 -- * Type declarations
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)
76 instance T.Element Instance where
81 allNames n = [name n, alias n]
83 -- | Constant holding the running instance states.
84 runningStates :: [String]
85 runningStates = [C.inststRunning, C.inststErrorup]
87 -- | Constant holding the local storage templates.
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 ]
98 -- | Constant holding the movable disk templates.
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 ]
106 -- | A simple name for the int, instance association list.
107 type AssocList = [(T.Idx, Instance)]
109 -- | A simple name for an instance map.
110 type List = Container.Container Instance
114 -- | Create an instance.
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
127 , running = run_init `elem` runningStates
134 , movable = supportsMoves dt
135 , autoBalance = auto_balance_init
139 -- | Changes the index.
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 }
147 -- | Changes the name.
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 }
155 -- | Changes the alias.
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 }
163 -- * Update functions
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 }
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 }
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 }
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 }
190 -- | Try to shrink the instance based on the reason why we can't
192 shrinkByType :: Instance -> T.FailMode -> T.Result Instance
193 shrinkByType inst T.FailMem = let v = mem inst - 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
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
203 then T.Bad "out of vcpus"
204 else T.Ok inst { vcpus = v }
205 shrinkByType _ f = T.Bad $ "Unhandled failure mode " ++ show f
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 }
212 -- | Checks whether the instance uses a secondary node.
214 -- /Note:/ This should be reconciled with @'sNode' ==
215 -- 'Node.noSecondary'@.
216 hasSecondary :: Instance -> Bool
217 hasSecondary = (== T.DTDrbd8) . diskTemplate
219 -- | Computed the number of nodes for a given disk template.
220 requiredNodes :: T.DiskTemplate -> Int
221 requiredNodes T.DTDrbd8 = 2
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]
230 -- | Checks whether a given disk template uses local storage.
231 usesLocalStorage :: Instance -> Bool
232 usesLocalStorage = (`elem` localStorageTemplates) . diskTemplate
234 -- | Checks whether a given disk template supported moves.
235 supportsMoves :: T.DiskTemplate -> Bool
236 supportsMoves = (`elem` movableDiskTemplates)