1 {-| Module describing a node.
3 All updates are functional (copy-based) and return a new node with
9 Copyright (C) 2009, 2010 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
33 -- ** Finalization after data loading
48 -- * Instance (re)location
59 , conflictingPrimaries
72 import Data.List hiding (group)
73 import qualified Data.Map as Map
74 import qualified Data.Foldable as Foldable
75 import Data.Ord (comparing)
76 import Text.Printf (printf)
78 import qualified Ganeti.HTools.Container as Container
79 import qualified Ganeti.HTools.Instance as Instance
80 import qualified Ganeti.HTools.PeerMap as P
82 import qualified Ganeti.HTools.Types as T
84 -- * Type declarations
87 type TagMap = Map.Map String Int
91 { name :: String -- ^ The node name
92 , alias :: String -- ^ The shortened name (for display purposes)
93 , tMem :: Double -- ^ Total memory (MiB)
94 , nMem :: Int -- ^ Node memory (MiB)
95 , fMem :: Int -- ^ Free memory (MiB)
96 , xMem :: Int -- ^ Unaccounted memory (MiB)
97 , tDsk :: Double -- ^ Total disk space (MiB)
98 , fDsk :: Int -- ^ Free disk space (MiB)
99 , tCpu :: Double -- ^ Total CPU count
100 , uCpu :: Int -- ^ Used VCPU count
101 , pList :: [T.Idx] -- ^ List of primary instance indices
102 , sList :: [T.Idx] -- ^ List of secondary instance indices
103 , idx :: T.Ndx -- ^ Internal index for book-keeping
104 , peers :: P.PeerMap -- ^ Pnode to instance mapping
105 , failN1 :: Bool -- ^ Whether the node has failed n1
106 , rMem :: Int -- ^ Maximum memory needed for failover by
107 -- primaries of this node
108 , pMem :: Double -- ^ Percent of free memory
109 , pDsk :: Double -- ^ Percent of free disk
110 , pRem :: Double -- ^ Percent of reserved memory
111 , pCpu :: Double -- ^ Ratio of virtual to physical CPUs
112 , mDsk :: Double -- ^ Minimum free disk ratio
113 , mCpu :: Double -- ^ Max ratio of virt-to-phys CPUs
114 , loDsk :: Int -- ^ Autocomputed from mDsk low disk
116 , hiCpu :: Int -- ^ Autocomputed from mCpu high cpu
118 , offline :: Bool -- ^ Whether the node should not be used
119 -- for allocations and skipped from score
121 , utilPool :: T.DynUtil -- ^ Total utilisation capacity
122 , utilLoad :: T.DynUtil -- ^ Sum of instance utilisation
123 , pTags :: TagMap -- ^ Map of primary instance tags and their count
124 , group :: T.GroupID -- ^ The node's group (as UUID)
125 } deriving (Show, Eq)
127 instance T.Element Node where
132 allNames n = [name n, alias n]
134 -- | A simple name for the int, node association list.
135 type AssocList = [(T.Ndx, Node)]
137 -- | A simple name for a node map.
138 type List = Container.Container Node
140 -- | A simple name for an allocation element (here just for logistic
142 type AllocElement = (List, Instance.Instance, [Node], T.Score)
144 -- | Constant node index for a non-moveable instance.
148 -- * Helper functions
150 -- | Add a tag to a tagmap
151 addTag :: TagMap -> String -> TagMap
152 addTag t s = Map.insertWith (+) s 1 t
154 -- | Add multiple tags
155 addTags :: TagMap -> [String] -> TagMap
156 addTags = foldl' addTag
158 -- | Adjust or delete a tag from a tagmap
159 delTag :: TagMap -> String -> TagMap
160 delTag t s = Map.update (\v -> if v > 1
165 -- | Remove multiple tags
166 delTags :: TagMap -> [String] -> TagMap
167 delTags = foldl' delTag
169 -- | Check if we can add a list of tags to a tagmap
170 rejectAddTags :: TagMap -> [String] -> Bool
171 rejectAddTags t = any (`Map.member` t)
173 -- | Check how many primary instances have conflicting tags. The
174 -- algorithm to compute this is to sum the count of all tags, then
175 -- subtract the size of the tag map (since each tag has at least one,
176 -- non-conflicting instance); this is equivalent to summing the
177 -- values in the tag map minus one.
178 conflictingPrimaries :: Node -> Int
179 conflictingPrimaries (Node { pTags = t }) = Foldable.sum t - Map.size t
181 -- * Initialization functions
183 -- | Create a new node.
185 -- The index and the peers maps are empty, and will be need to be
186 -- update later via the 'setIdx' and 'buildPeers' functions.
187 create :: String -> Double -> Int -> Int -> Double
188 -> Int -> Double -> Bool -> String -> Node
189 create name_init mem_t_init mem_n_init mem_f_init
190 dsk_t_init dsk_f_init cpu_t_init offline_init group_init =
191 Node { name = name_init
206 , pMem = fromIntegral mem_f_init / mem_t_init
207 , pDsk = fromIntegral dsk_f_init / dsk_t_init
210 , offline = offline_init
212 , mDsk = T.defReservedDiskRatio
213 , mCpu = T.defVcpuRatio
214 , loDsk = mDskToloDsk T.defReservedDiskRatio dsk_t_init
215 , hiCpu = mCpuTohiCpu T.defVcpuRatio cpu_t_init
216 , utilPool = T.baseUtil
217 , utilLoad = T.zeroUtil
222 -- | Conversion formula from mDsk\/tDsk to loDsk
223 mDskToloDsk :: Double -> Double -> Int
224 mDskToloDsk mval tdsk = floor (mval * tdsk)
226 -- | Conversion formula from mCpu\/tCpu to hiCpu
227 mCpuTohiCpu :: Double -> Double -> Int
228 mCpuTohiCpu mval tcpu = floor (mval * tcpu)
230 -- | Changes the index.
232 -- This is used only during the building of the data structures.
233 setIdx :: Node -> T.Ndx -> Node
234 setIdx t i = t {idx = i}
236 -- | Changes the alias.
238 -- This is used only during the building of the data structures.
239 setAlias :: Node -> String -> Node
240 setAlias t s = t { alias = s }
242 -- | Sets the offline attribute.
243 setOffline :: Node -> Bool -> Node
244 setOffline t val = t { offline = val }
246 -- | Sets the unnaccounted memory.
247 setXmem :: Node -> Int -> Node
248 setXmem t val = t { xMem = val }
250 -- | Sets the max disk usage ratio
251 setMdsk :: Node -> Double -> Node
252 setMdsk t val = t { mDsk = val, loDsk = mDskToloDsk val (tDsk t) }
254 -- | Sets the max cpu usage ratio
255 setMcpu :: Node -> Double -> Node
256 setMcpu t val = t { mCpu = val, hiCpu = mCpuTohiCpu val (tCpu t) }
258 -- | Computes the maximum reserved memory for peers from a peer map.
259 computeMaxRes :: P.PeerMap -> P.Elem
260 computeMaxRes = P.maxElem
262 -- | Builds the peer map for a given node.
263 buildPeers :: Node -> Instance.List -> Node
266 (\i_idx -> let inst = Container.find i_idx il
267 in (Instance.pNode inst, Instance.mem inst))
269 pmap = P.accumArray (+) mdata
270 new_rmem = computeMaxRes pmap
271 new_failN1 = fMem t <= new_rmem
272 new_prem = fromIntegral new_rmem / tMem t
273 in t {peers=pmap, failN1 = new_failN1, rMem = new_rmem, pRem = new_prem}
275 -- | Assigns an instance to a node as primary and update the used VCPU
276 -- count, utilisation data and tags map.
277 setPri :: Node -> Instance.Instance -> Node
278 setPri t inst = t { pList = Instance.idx inst:pList t
280 , pCpu = fromIntegral new_count / tCpu t
281 , utilLoad = utilLoad t `T.addUtil` Instance.util inst
282 , pTags = addTags (pTags t) (Instance.tags inst)
284 where new_count = uCpu t + Instance.vcpus inst
286 -- | Assigns an instance to a node as secondary without other updates.
287 setSec :: Node -> Instance.Instance -> Node
288 setSec t inst = t { sList = Instance.idx inst:sList t
289 , utilLoad = old_load { T.dskWeight = T.dskWeight old_load +
290 T.dskWeight (Instance.util inst) }
292 where old_load = utilLoad t
294 -- * Update functions
296 -- | Sets the free memory.
297 setFmem :: Node -> Int -> Node
299 let new_n1 = new_mem <= rMem t
300 new_mp = fromIntegral new_mem / tMem t
301 in t { fMem = new_mem, failN1 = new_n1, pMem = new_mp }
303 -- | Removes a primary instance.
304 removePri :: Node -> Instance.Instance -> Node
306 let iname = Instance.idx inst
307 new_plist = delete iname (pList t)
308 new_mem = fMem t + Instance.mem inst
309 new_dsk = fDsk t + Instance.dsk inst
310 new_mp = fromIntegral new_mem / tMem t
311 new_dp = fromIntegral new_dsk / tDsk t
312 new_failn1 = new_mem <= rMem t
313 new_ucpu = uCpu t - Instance.vcpus inst
314 new_rcpu = fromIntegral new_ucpu / tCpu t
315 new_load = utilLoad t `T.subUtil` Instance.util inst
316 in t { pList = new_plist, fMem = new_mem, fDsk = new_dsk
317 , failN1 = new_failn1, pMem = new_mp, pDsk = new_dp
318 , uCpu = new_ucpu, pCpu = new_rcpu, utilLoad = new_load
319 , pTags = delTags (pTags t) (Instance.tags inst) }
321 -- | Removes a secondary instance.
322 removeSec :: Node -> Instance.Instance -> Node
324 let iname = Instance.idx inst
325 pnode = Instance.pNode inst
326 new_slist = delete iname (sList t)
327 new_dsk = fDsk t + Instance.dsk inst
329 old_peem = P.find pnode old_peers
330 new_peem = old_peem - Instance.mem inst
331 new_peers = if new_peem > 0
332 then P.add pnode new_peem old_peers
333 else P.remove pnode old_peers
335 new_rmem = if old_peem < old_rmem
337 else computeMaxRes new_peers
338 new_prem = fromIntegral new_rmem / tMem t
339 new_failn1 = fMem t <= new_rmem
340 new_dp = fromIntegral new_dsk / tDsk t
341 old_load = utilLoad t
342 new_load = old_load { T.dskWeight = T.dskWeight old_load -
343 T.dskWeight (Instance.util inst) }
344 in t { sList = new_slist, fDsk = new_dsk, peers = new_peers
345 , failN1 = new_failn1, rMem = new_rmem, pDsk = new_dp
346 , pRem = new_prem, utilLoad = new_load }
348 -- | Adds a primary instance (basic version).
349 addPri :: Node -> Instance.Instance -> T.OpResult Node
350 addPri = addPriEx False
352 -- | Adds a primary instance (extended version).
353 addPriEx :: Bool -- ^ Whether to override the N+1 and
354 -- other /soft/ checks, useful if we
355 -- come from a worse status
357 -> Node -- ^ The target node
358 -> Instance.Instance -- ^ The instance to add
359 -> T.OpResult Node -- ^ The result of the operation,
360 -- either the new version of the node
362 addPriEx force t inst =
363 let iname = Instance.idx inst
364 new_mem = fMem t - Instance.mem inst
365 new_dsk = fDsk t - Instance.dsk inst
366 new_failn1 = new_mem <= rMem t
367 new_ucpu = uCpu t + Instance.vcpus inst
368 new_pcpu = fromIntegral new_ucpu / tCpu t
369 new_dp = fromIntegral new_dsk / tDsk t
371 new_load = utilLoad t `T.addUtil` Instance.util inst
372 inst_tags = Instance.tags inst
376 _ | new_mem <= 0 -> T.OpFail T.FailMem
377 | new_dsk <= 0 -> T.OpFail T.FailDisk
378 | mDsk t > new_dp && strict -> T.OpFail T.FailDisk
379 | new_failn1 && not (failN1 t) && strict -> T.OpFail T.FailMem
380 | l_cpu >= 0 && l_cpu < new_pcpu && strict -> T.OpFail T.FailCPU
381 | rejectAddTags old_tags inst_tags -> T.OpFail T.FailTags
383 let new_plist = iname:pList t
384 new_mp = fromIntegral new_mem / tMem t
385 r = t { pList = new_plist, fMem = new_mem, fDsk = new_dsk
386 , failN1 = new_failn1, pMem = new_mp, pDsk = new_dp
387 , uCpu = new_ucpu, pCpu = new_pcpu
388 , utilLoad = new_load
389 , pTags = addTags old_tags inst_tags }
392 -- | Adds a secondary instance (basic version).
393 addSec :: Node -> Instance.Instance -> T.Ndx -> T.OpResult Node
394 addSec = addSecEx False
396 -- | Adds a secondary instance (extended version).
397 addSecEx :: Bool -> Node -> Instance.Instance -> T.Ndx -> T.OpResult Node
398 addSecEx force t inst pdx =
399 let iname = Instance.idx inst
402 new_dsk = fDsk t - Instance.dsk inst
403 new_peem = P.find pdx old_peers + Instance.mem inst
404 new_peers = P.add pdx new_peem old_peers
405 new_rmem = max (rMem t) new_peem
406 new_prem = fromIntegral new_rmem / tMem t
407 new_failn1 = old_mem <= new_rmem
408 new_dp = fromIntegral new_dsk / tDsk t
409 old_load = utilLoad t
410 new_load = old_load { T.dskWeight = T.dskWeight old_load +
411 T.dskWeight (Instance.util inst) }
414 _ | new_dsk <= 0 -> T.OpFail T.FailDisk
415 | mDsk t > new_dp && strict -> T.OpFail T.FailDisk
416 | Instance.mem inst >= old_mem && strict -> T.OpFail T.FailMem
417 | new_failn1 && not (failN1 t) && strict -> T.OpFail T.FailMem
419 let new_slist = iname:sList t
420 r = t { sList = new_slist, fDsk = new_dsk
421 , peers = new_peers, failN1 = new_failn1
422 , rMem = new_rmem, pDsk = new_dp
423 , pRem = new_prem, utilLoad = new_load }
428 -- | Computes the amount of available disk on a given node
429 availDisk :: Node -> Int
437 -- | Computes the amount of available memory on a given node
438 availMem :: Node -> Int
446 -- | Computes the amount of available memory on a given node
447 availCpu :: Node -> Int
455 -- * Display functions
457 showField :: Node -> String -> String
460 "idx" -> printf "%4d" $ idx t
463 "status" -> if offline t then "-"
464 else if failN1 t then "*" else " "
465 "tmem" -> printf "%5.0f" $ tMem t
466 "nmem" -> printf "%5d" $ nMem t
467 "xmem" -> printf "%5d" $ xMem t
468 "fmem" -> printf "%5d" $ fMem t
469 "imem" -> printf "%5d" imem
470 "rmem" -> printf "%5d" $ rMem t
471 "amem" -> printf "%5d" $ fMem t - rMem t
472 "tdsk" -> printf "%5.0f" $ tDsk t / 1024
473 "fdsk" -> printf "%5d" $ fDsk t `div` 1024
474 "tcpu" -> printf "%4.0f" $ tCpu t
475 "ucpu" -> printf "%4d" $ uCpu t
476 "pcnt" -> printf "%3d" $ length (pList t)
477 "scnt" -> printf "%3d" $ length (sList t)
478 "plist" -> show $ pList t
479 "slist" -> show $ sList t
480 "pfmem" -> printf "%6.4f" $ pMem t
481 "pfdsk" -> printf "%6.4f" $ pDsk t
482 "rcpu" -> printf "%5.2f" $ pCpu t
483 "cload" -> printf "%5.3f" uC
484 "mload" -> printf "%5.3f" uM
485 "dload" -> printf "%5.3f" uD
486 "nload" -> printf "%5.3f" uN
487 "ptags" -> intercalate "," . map (\(k, v) -> printf "%s=%d" k v) .
489 "peermap" -> show $ peers t
490 "group.uuid" -> group t
493 T.DynUtil { T.cpuWeight = uC, T.memWeight = uM,
494 T.dskWeight = uD, T.netWeight = uN } = utilLoad t
495 imem = truncate (tMem t) - nMem t - xMem t - fMem t
497 -- | Returns the header and numeric propery of a field
498 showHeader :: String -> (String, Bool)
501 "idx" -> ("Index", True)
502 "name" -> ("Name", False)
503 "fqdn" -> ("Name", False)
504 "status" -> ("F", False)
505 "tmem" -> ("t_mem", True)
506 "nmem" -> ("n_mem", True)
507 "xmem" -> ("x_mem", True)
508 "fmem" -> ("f_mem", True)
509 "imem" -> ("i_mem", True)
510 "rmem" -> ("r_mem", True)
511 "amem" -> ("a_mem", True)
512 "tdsk" -> ("t_dsk", True)
513 "fdsk" -> ("f_dsk", True)
514 "tcpu" -> ("pcpu", True)
515 "ucpu" -> ("vcpu", True)
516 "pcnt" -> ("pcnt", True)
517 "scnt" -> ("scnt", True)
518 "plist" -> ("primaries", True)
519 "slist" -> ("secondaries", True)
520 "pfmem" -> ("p_fmem", True)
521 "pfdsk" -> ("p_fdsk", True)
522 "rcpu" -> ("r_cpu", True)
523 "cload" -> ("lCpu", True)
524 "mload" -> ("lMem", True)
525 "dload" -> ("lDsk", True)
526 "nload" -> ("lNet", True)
527 "ptags" -> ("PrimaryTags", False)
528 "peermap" -> ("PeerMap", False)
529 "group.uuid" -> ("GroupUUID", False)
530 _ -> (T.unknownField, False)
532 -- | String converter for the node list functionality.
533 list :: [String] -> Node -> [String]
534 list fields t = map (showField t) fields
537 defaultFields :: [String]
539 [ "status", "name", "tmem", "nmem", "imem", "xmem", "fmem"
540 , "rmem", "tdsk", "fdsk", "tcpu", "ucpu", "pcnt", "scnt"
541 , "pfmem", "pfdsk", "rcpu"
542 , "cload", "mload", "dload", "nload" ]
544 -- | Split a list of nodes into a list of (node group UUID, list of
546 computeGroups :: [Node] -> [(T.GroupID, [Node])]
547 computeGroups nodes =
548 let nodes' = sortBy (comparing group) nodes
549 nodes'' = groupBy (\a b -> group a == group b) nodes'
550 in map (\nl -> (group (head nl), nl)) nodes''