1 {-| Module describing a node.
3 All updates are functional (copy-based) and return a new node with
9 Copyright (C) 2009 Google Inc.
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
28 module Ganeti.HTools.Node
29 ( Node(failN1, name, idx, t_mem, n_mem, f_mem, r_mem,
32 p_mem, p_dsk, p_rem, p_cpu,
33 plist, slist, offline)
37 -- ** Finalization after data loading
47 -- * Instance (re)location
60 import Text.Printf (printf)
62 import qualified Ganeti.HTools.Container as Container
63 import qualified Ganeti.HTools.Instance as Instance
64 import qualified Ganeti.HTools.PeerMap as PeerMap
66 import qualified Ganeti.HTools.Types as T
68 -- * Type declarations
71 data Node = Node { name :: String -- ^ The node name
72 , t_mem :: Double -- ^ Total memory (MiB)
73 , n_mem :: Int -- ^ Node memory (MiB)
74 , f_mem :: Int -- ^ Free memory (MiB)
75 , x_mem :: Int -- ^ Unaccounted memory (MiB)
76 , t_dsk :: Double -- ^ Total disk space (MiB)
77 , f_dsk :: Int -- ^ Free disk space (MiB)
78 , t_cpu :: Double -- ^ Total CPU count
79 , u_cpu :: Int -- ^ Used VCPU count
80 , plist :: [T.Idx]-- ^ List of primary instance indices
81 , slist :: [T.Idx]-- ^ List of secondary instance indices
82 , idx :: T.Ndx -- ^ Internal index for book-keeping
83 , peers :: PeerMap.PeerMap -- ^ Pnode to instance mapping
84 , failN1:: Bool -- ^ Whether the node has failed n1
85 , r_mem :: Int -- ^ Maximum memory needed for
86 -- failover by primaries of this node
87 , p_mem :: Double -- ^ Percent of free memory
88 , p_dsk :: Double -- ^ Percent of free disk
89 , p_rem :: Double -- ^ Percent of reserved memory
90 , p_cpu :: Double -- ^ Ratio of virtual to physical CPUs
91 , offline :: Bool -- ^ Whether the node should not be used
92 -- for allocations and skipped from
96 instance T.Element Node where
102 -- | A simple name for the int, node association list.
103 type AssocList = [(T.Ndx, Node)]
105 -- | A simple name for a node map.
106 type List = Container.Container Node
108 -- | Constant node index for a non-moveable instance.
112 -- * Initialization functions
114 -- | Create a new node.
116 -- The index and the peers maps are empty, and will be need to be
117 -- update later via the 'setIdx' and 'buildPeers' functions.
118 create :: String -> Double -> Int -> Int -> Double
119 -> Int -> Double -> Bool -> Node
120 create name_init mem_t_init mem_n_init mem_f_init
121 dsk_t_init dsk_f_init cpu_t_init offline_init =
136 peers = PeerMap.empty,
138 p_mem = (fromIntegral mem_f_init) / mem_t_init,
139 p_dsk = (fromIntegral dsk_f_init) / dsk_t_init,
142 offline = offline_init,
146 -- | Changes the index.
148 -- This is used only during the building of the data structures.
149 setIdx :: Node -> T.Ndx -> Node
150 setIdx t i = t {idx = i}
152 -- | Changes the name.
154 -- This is used only during the building of the data structures.
155 setName :: Node -> String -> Node
156 setName t s = t {name = s}
158 -- | Sets the offline attribute.
159 setOffline :: Node -> Bool -> Node
160 setOffline t val = t { offline = val }
162 -- | Sets the unnaccounted memory.
163 setXmem :: Node -> Int -> Node
164 setXmem t val = t { x_mem = val }
166 -- | Computes the maximum reserved memory for peers from a peer map.
167 computeMaxRes :: PeerMap.PeerMap -> PeerMap.Elem
168 computeMaxRes new_peers = PeerMap.maxElem new_peers
170 -- | Builds the peer map for a given node.
171 buildPeers :: Node -> Instance.List -> Node
174 (\i_idx -> let inst = Container.find i_idx il
175 in (Instance.pnode inst, Instance.mem inst))
177 pmap = PeerMap.accumArray (+) mdata
178 new_rmem = computeMaxRes pmap
179 new_failN1 = computeFailN1 new_rmem (f_mem t) (f_dsk t)
180 new_prem = (fromIntegral new_rmem) / (t_mem t)
181 in t {peers=pmap, failN1 = new_failN1, r_mem = new_rmem, p_rem = new_prem}
183 -- | Assigns an instance to a node as primary without other updates.
184 setPri :: Node -> T.Idx -> Node
185 setPri t idx = t { plist = idx:(plist t) }
187 -- | Assigns an instance to a node as secondary without other updates.
188 setSec :: Node -> T.Idx -> Node
189 setSec t idx = t { slist = idx:(slist t) }
191 -- | Add primary cpus to a node
192 addCpus :: Node -> Int -> Node
194 let new_count = (u_cpu t) + count
195 in t { u_cpu = new_count, p_cpu = (fromIntegral new_count) / (t_cpu t) }
197 -- * Update functions
199 -- | Sets the free memory.
200 setFmem :: Node -> Int -> Node
202 let new_n1 = computeFailN1 (r_mem t) new_mem (f_dsk t)
203 new_mp = (fromIntegral new_mem) / (t_mem t)
205 t { f_mem = new_mem, failN1 = new_n1, p_mem = new_mp }
207 -- | Given the rmem, free memory and disk, computes the failn1 status.
208 computeFailN1 :: Int -> Int -> Int -> Bool
209 computeFailN1 new_rmem new_mem new_dsk =
210 new_mem <= new_rmem || new_dsk <= 0
212 -- | Given the new free memory and disk, fail if any of them is below zero.
213 failHealth :: Int -> Int -> Bool
214 failHealth new_mem new_dsk = new_mem <= 0 || new_dsk <= 0
216 -- | Removes a primary instance.
217 removePri :: Node -> Instance.Instance -> Node
219 let iname = Instance.idx inst
220 new_plist = delete iname (plist t)
221 new_mem = f_mem t + Instance.mem inst
222 new_dsk = f_dsk t + Instance.dsk inst
223 new_mp = (fromIntegral new_mem) / (t_mem t)
224 new_dp = (fromIntegral new_dsk) / (t_dsk t)
225 new_failn1 = computeFailN1 (r_mem t) new_mem new_dsk
226 new_ucpu = (u_cpu t) - (Instance.vcpus inst)
227 new_rcpu = (fromIntegral new_ucpu) / (t_cpu t)
228 in t {plist = new_plist, f_mem = new_mem, f_dsk = new_dsk,
229 failN1 = new_failn1, p_mem = new_mp, p_dsk = new_dp,
230 u_cpu = new_ucpu, p_cpu = new_rcpu}
232 -- | Removes a secondary instance.
233 removeSec :: Node -> Instance.Instance -> Node
235 let iname = Instance.idx inst
236 pnode = Instance.pnode inst
237 new_slist = delete iname (slist t)
238 new_dsk = f_dsk t + Instance.dsk inst
240 old_peem = PeerMap.find pnode old_peers
241 new_peem = old_peem - (Instance.mem inst)
242 new_peers = PeerMap.add pnode new_peem old_peers
244 new_rmem = if old_peem < old_rmem then
247 computeMaxRes new_peers
248 new_prem = (fromIntegral new_rmem) / (t_mem t)
249 new_failn1 = computeFailN1 new_rmem (f_mem t) new_dsk
250 new_dp = (fromIntegral new_dsk) / (t_dsk t)
251 in t {slist = new_slist, f_dsk = new_dsk, peers = new_peers,
252 failN1 = new_failn1, r_mem = new_rmem, p_dsk = new_dp,
255 -- | Adds a primary instance.
256 addPri :: Node -> Instance.Instance -> Maybe Node
258 let iname = Instance.idx inst
259 new_mem = f_mem t - Instance.mem inst
260 new_dsk = f_dsk t - Instance.dsk inst
261 new_failn1 = computeFailN1 (r_mem t) new_mem new_dsk
262 new_ucpu = (u_cpu t) + (Instance.vcpus inst)
263 new_pcpu = (fromIntegral new_ucpu) / (t_cpu t)
265 if (failHealth new_mem new_dsk) || (new_failn1 && not (failN1 t)) then
268 let new_plist = iname:(plist t)
269 new_mp = (fromIntegral new_mem) / (t_mem t)
270 new_dp = (fromIntegral new_dsk) / (t_dsk t)
272 Just t {plist = new_plist, f_mem = new_mem, f_dsk = new_dsk,
273 failN1 = new_failn1, p_mem = new_mp, p_dsk = new_dp,
274 u_cpu = new_ucpu, p_cpu = new_pcpu}
276 -- | Adds a secondary instance.
277 addSec :: Node -> Instance.Instance -> T.Ndx -> Maybe Node
279 let iname = Instance.idx inst
282 new_dsk = f_dsk t - Instance.dsk inst
283 new_peem = PeerMap.find pdx old_peers + Instance.mem inst
284 new_peers = PeerMap.add pdx new_peem old_peers
285 new_rmem = max (r_mem t) new_peem
286 new_prem = (fromIntegral new_rmem) / (t_mem t)
287 new_failn1 = computeFailN1 new_rmem old_mem new_dsk in
288 if (failHealth old_mem new_dsk) || (new_failn1 && not (failN1 t)) then
291 let new_slist = iname:(slist t)
292 new_dp = (fromIntegral new_dsk) / (t_dsk t)
294 Just t {slist = new_slist, f_dsk = new_dsk,
295 peers = new_peers, failN1 = new_failn1,
296 r_mem = new_rmem, p_dsk = new_dp,
299 -- * Display functions
301 -- | String converter for the node list functionality.
302 list :: Int -> Node -> String
304 let pl = length $ plist t
305 sl = length $ slist t
315 imem = (truncate tmem) - nmem - xmem - fmem
318 then printf " - %-*s %57s %3d %3d"
319 mname (name t) "" pl sl
321 printf " %c %-*s %5.0f %5d %5d %5d %5d %5d %5.0f %5d\
322 \ %4.0f %4d %3d %3d %6.4f %6.4f %5.2f"
323 (if off then '-' else if fn then '*' else ' ')
324 mname (name t) tmem nmem imem xmem fmem (r_mem t)
325 ((t_dsk t) / 1024) ((f_dsk t) `div` 1024)