More code reorganizations
[ganeti-local] / Ganeti / HTools / Node.hs
index 3ecf685..c5856fb 100644 (file)
@@ -16,6 +16,7 @@ module Ganeti.HTools.Node
     , setIdx
     , setOffline
     , setXmem
+    , setFmem
     -- * Instance (re)location
     , removePri
     , removeSec
@@ -25,6 +26,9 @@ module Ganeti.HTools.Node
     , setSec
     -- * Formatting
     , list
+    -- * Misc stuff
+    , AssocList
+    , noSecondary
     ) where
 
 import Data.List
@@ -34,8 +38,6 @@ import qualified Ganeti.HTools.Container as Container
 import qualified Ganeti.HTools.Instance as Instance
 import qualified Ganeti.HTools.PeerMap as PeerMap
 
-import Ganeti.HTools.Utils
-
 data Node = Node { t_mem :: Double -- ^ total memory (MiB)
                  , n_mem :: Int    -- ^ node memory (MiB)
                  , f_mem :: Int    -- ^ free memory (MiB)
@@ -57,14 +59,22 @@ data Node = Node { t_mem :: Double -- ^ total memory (MiB)
                                    -- score computations
   } deriving (Show)
 
+-- | A simple name for the int, node association list
+type AssocList = [(Int, Node)]
+
+-- | Constant node index for a non-moveable instance
+noSecondary :: Int
+noSecondary = -1
+
 {- | Create a new node.
 
 The index and the peers maps are empty, and will be need to be update
 later via the 'setIdx' and 'buildPeers' functions.
 
 -}
-create :: Double -> Int -> Int -> Double -> Int -> Node
-create mem_t_init mem_n_init mem_f_init dsk_t_init dsk_f_init =
+create :: Double -> Int -> Int -> Double -> Int -> Bool -> Node
+create mem_t_init mem_n_init mem_f_init dsk_t_init dsk_f_init
+       offline_init =
     Node
     {
       t_mem = mem_t_init,
@@ -81,7 +91,7 @@ create mem_t_init mem_n_init mem_f_init dsk_t_init dsk_f_init =
       p_mem = (fromIntegral mem_f_init) / mem_t_init,
       p_dsk = (fromIntegral dsk_f_init) / dsk_t_init,
       p_rem = 0,
-      offline = False,
+      offline = offline_init,
       x_mem = 0
     }
 
@@ -98,6 +108,14 @@ setOffline t val = t { offline = val }
 setXmem :: Node -> Int -> Node
 setXmem t val = t { x_mem = val }
 
+-- | Sets the free memory
+setFmem :: Node -> Int -> Node
+setFmem t new_mem =
+    let new_n1 = computeFailN1 (r_mem t) new_mem (f_dsk t)
+        new_mp = (fromIntegral new_mem) / (t_mem t)
+    in
+      t { f_mem = new_mem, failN1 = new_n1, p_mem = new_mp }
+
 -- | Given the rmem, free memory and disk, computes the failn1 status.
 computeFailN1 :: Int -> Int -> Int -> Bool
 computeFailN1 new_rmem new_mem new_dsk =
@@ -217,10 +235,15 @@ list mname n t =
         dp = p_dsk t
         off = offline t
         fn = failN1 t
+        tmem = t_mem t
+        nmem = n_mem t
+        xmem = x_mem t
+        fmem = f_mem t
+        imem = (truncate tmem) - nmem - xmem - fmem
     in
-      printf " %c %-*s %5.0f %5d %5d %5d %5.0f %5d %3d %3d %.5f %.5f"
+      printf " %c %-*s %5.0f %5d %5d %5d %5d %5d %5.0f %5d %3d %3d %.5f %.5f"
                  (if off then '-' else if fn then '*' else ' ')
-                 mname n (t_mem t) (n_mem t) (f_mem t) (r_mem t)
+                 mname n tmem nmem imem xmem fmem (r_mem t)
                  ((t_dsk t) / 1024) ((f_dsk t) `div` 1024)
                  (length pl) (length sl)
                  mp dp