From 608efcce95d93c1228f526c5f3ed192650b6f2b7 Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Wed, 27 May 2009 20:25:09 +0100
Subject: [PATCH] Add type synonyms for the node/instance indices

This is a first step towards full datatype renaming. That requires more
changes, so at first we only want to document clearly what is a node
index, what is an instance index, and what is a plain Int.
---
 Ganeti/HTools/Cluster.hs  | 20 ++++++++++----------
 Ganeti/HTools/IAlloc.hs   |  2 +-
 Ganeti/HTools/Instance.hs | 20 ++++++++++----------
 Ganeti/HTools/Loader.hs   |  8 ++++----
 Ganeti/HTools/Node.hs     | 20 ++++++++++----------
 Ganeti/HTools/PeerMap.hs  |  4 +++-
 Ganeti/HTools/Rapi.hs     |  2 +-
 Ganeti/HTools/Text.hs     |  2 +-
 Ganeti/HTools/Types.hs    |  6 ++++++
 hail.hs                   |  6 +++---
 10 files changed, 49 insertions(+), 41 deletions(-)

diff --git a/Ganeti/HTools/Cluster.hs b/Ganeti/HTools/Cluster.hs
index fd0ef1f92..1bf652427 100644
--- a/Ganeti/HTools/Cluster.hs
+++ b/Ganeti/HTools/Cluster.hs
@@ -52,7 +52,7 @@ import Ganeti.HTools.Utils
 type Score = Double
 
 -- | The description of an instance placement.
-type Placement = (Int, Int, Int, Score)
+type Placement = (Idx, Ndx, Ndx, Score)
 
 {- | A cluster solution described as the solution delta and the list
 of placements.
@@ -72,10 +72,10 @@ data Removal = Removal Node.List [Instance.Instance]
 
 -- | An instance move definition
 data IMove = Failover                -- ^ Failover the instance (f)
-           | ReplacePrimary Int      -- ^ Replace primary (f, r:np, f)
-           | ReplaceSecondary Int    -- ^ Replace secondary (r:ns)
-           | ReplaceAndFailover Int  -- ^ Replace secondary, failover (r:np, f)
-           | FailoverAndReplace Int  -- ^ Failover, replace secondary (f, r:ns)
+           | ReplacePrimary Ndx      -- ^ Replace primary (f, r:np, f)
+           | ReplaceSecondary Ndx    -- ^ Replace secondary (r:ns)
+           | ReplaceAndFailover Ndx  -- ^ Replace secondary, failover (r:np, f)
+           | FailoverAndReplace Ndx  -- ^ Failover, replace secondary (f, r:ns)
              deriving (Show)
 
 -- | The complete state for the balancing solution
@@ -238,7 +238,7 @@ computeRemovals nl bad_instances depth =
 -- Second phase functions
 
 -- | Single-node relocation cost
-nodeDelta :: Int -> Int -> Int -> Int
+nodeDelta :: Ndx -> Ndx -> Ndx -> Int
 nodeDelta i p s =
     if i == p || i == s then
         0
@@ -333,7 +333,7 @@ checkPlacement nl victims current current_delta prev_sol max_delta =
 
 -- | Apply a move
 applyMove :: Node.List -> Instance.Instance
-          -> IMove -> (Maybe Node.List, Instance.Instance, Int, Int)
+          -> IMove -> (Maybe Node.List, Instance.Instance, Ndx, Ndx)
 -- Failover (f)
 applyMove nl inst Failover =
     let old_pdx = Instance.pnode inst
@@ -451,7 +451,7 @@ checkSingleStep ini_tbl target cur_tbl move =
 -- | Given the status of the current secondary as a valid new node
 -- and the current candidate target node,
 -- generate the possible moves for a instance.
-possibleMoves :: Bool -> Int -> [IMove]
+possibleMoves :: Bool -> Ndx -> [IMove]
 possibleMoves True tdx =
     [ReplaceSecondary tdx,
      ReplaceAndFailover tdx,
@@ -463,7 +463,7 @@ possibleMoves False tdx =
      ReplaceAndFailover tdx]
 
 -- | Compute the best move for a given instance.
-checkInstanceMove :: [Int]             -- Allowed target node indices
+checkInstanceMove :: [Ndx]             -- Allowed target node indices
                   -> Table             -- Original table
                   -> Instance.Instance -- Instance to move
                   -> Table             -- Best new table for this instance
@@ -482,7 +482,7 @@ checkInstanceMove nodes_idx ini_tbl target =
       foldl' (checkSingleStep ini_tbl target) aft_failover all_moves
 
 -- | Compute the best next move.
-checkMove :: [Int]               -- ^ Allowed target node indices
+checkMove :: [Ndx]               -- ^ Allowed target node indices
           -> Table               -- ^ The current solution
           -> [Instance.Instance] -- ^ List of instances still to move
           -> Table               -- ^ The new solution
diff --git a/Ganeti/HTools/IAlloc.hs b/Ganeti/HTools/IAlloc.hs
index 901f2d4ed..c45bdcd27 100644
--- a/Ganeti/HTools/IAlloc.hs
+++ b/Ganeti/HTools/IAlloc.hs
@@ -26,7 +26,7 @@ import Ganeti.HTools.Types
 
 data RqType
     = Allocate Instance.Instance Int
-    | Relocate Int Int [Int]
+    | Relocate Idx Int [Ndx]
     deriving (Show)
 
 data Request = Request RqType Node.List Instance.List String
diff --git a/Ganeti/HTools/Instance.hs b/Ganeti/HTools/Instance.hs
index f0d77983b..cdb292043 100644
--- a/Ganeti/HTools/Instance.hs
+++ b/Ganeti/HTools/Instance.hs
@@ -15,9 +15,9 @@ data Instance = Instance { name :: String   -- ^ the instance name
                          , running :: Bool  -- ^ whether the instance
                                             -- is running
                          , run_st :: String -- ^ original (text) run status
-                         , pnode :: Int     -- ^ original primary node
-                         , snode :: Int     -- ^ original secondary node
-                         , idx :: Int       -- ^ internal index for
+                         , pnode :: T.Ndx   -- ^ original primary node
+                         , snode :: T.Ndx   -- ^ original secondary node
+                         , idx :: T.Idx     -- ^ internal index for
                                             -- book-keeping
                          } deriving (Show)
 
@@ -28,12 +28,12 @@ instance T.Element Instance where
     setIdx  = setIdx
 
 -- | A simple name for the int, instance association list
-type AssocList = [(Int, Instance)]
+type AssocList = [(T.Idx, Instance)]
 
 -- | A simple name for an instance map
 type List = Container.Container Instance
 
-create :: String -> Int -> Int -> String -> Int -> Int -> Instance
+create :: String -> Int -> Int -> String -> T.Ndx -> T.Ndx -> Instance
 create name_init mem_init dsk_init run_init pn sn =
     Instance {
           name = name_init,
@@ -51,27 +51,27 @@ create name_init mem_init dsk_init run_init pn sn =
 
 -- | Changes the primary node of the instance.
 setPri :: Instance  -- ^ the original instance
-        -> Int      -- ^ the new primary node
+        -> T.Ndx    -- ^ the new primary node
         -> Instance -- ^ the modified instance
 setPri t p = t { pnode = p }
 
 -- | Changes the secondary node of the instance.
 setSec :: Instance  -- ^ the original instance
-        -> Int      -- ^ the new secondary node
+        -> T.Ndx    -- ^ the new secondary node
         -> Instance -- ^ the modified instance
 setSec t s = t { snode = s }
 
 -- | Changes both nodes of the instance.
 setBoth :: Instance  -- ^ the original instance
-         -> Int      -- ^ new primary node index
-         -> Int      -- ^ new secondary node index
+         -> T.Ndx    -- ^ new primary node index
+         -> T.Ndx    -- ^ new secondary node index
          -> Instance -- ^ the modified instance
 setBoth t p s = t { pnode = p, snode = s }
 
 -- | Changes the index.
 -- This is used only during the building of the data structures.
 setIdx :: Instance  -- ^ the original instance
-        -> Int      -- ^ new index
+        -> T.Idx    -- ^ new index
         -> Instance -- ^ the modified instance
 setIdx t i = t { idx = i }
 
diff --git a/Ganeti/HTools/Loader.hs b/Ganeti/HTools/Loader.hs
index 525cb64dd..1720bcb6f 100644
--- a/Ganeti/HTools/Loader.hs
+++ b/Ganeti/HTools/Loader.hs
@@ -23,7 +23,7 @@ import qualified Ganeti.HTools.Node as Node
 import Ganeti.HTools.Types
 
 -- | Lookups a node into an assoc list
-lookupNode :: (Monad m) => NameAssoc -> String -> String -> m Int
+lookupNode :: (Monad m) => [(String, Ndx)] -> String -> String -> m Ndx
 lookupNode ktn inst node =
     case lookup node ktn of
       Nothing -> fail $ "Unknown node '" ++ node ++ "' for instance " ++ inst
@@ -37,9 +37,9 @@ assignIndices =
           . zip [0..]
 
 -- | For each instance, add its index to its primary and secondary nodes
-fixNodes :: [(Int, Node.Node)]
-         -> [(Int, Instance.Instance)]
-         -> [(Int, Node.Node)]
+fixNodes :: [(Ndx, Node.Node)]
+         -> [(Idx, Instance.Instance)]
+         -> [(Ndx, Node.Node)]
 fixNodes nl il =
     foldl' (\accu (idx, inst) ->
                 let
diff --git a/Ganeti/HTools/Node.hs b/Ganeti/HTools/Node.hs
index c1e4ad30c..c95f7f96d 100644
--- a/Ganeti/HTools/Node.hs
+++ b/Ganeti/HTools/Node.hs
@@ -48,9 +48,9 @@ data Node = Node { name  :: String -- ^ the node name
                  , x_mem :: Int    -- ^ unaccounted memory (MiB)
                  , t_dsk :: Double -- ^ total disk space (MiB)
                  , f_dsk :: Int    -- ^ free disk space (MiB)
-                 , plist :: [Int]  -- ^ list of primary instance indices
-                 , slist :: [Int]  -- ^ list of secondary instance indices
-                 , idx :: Int      -- ^ internal index for book-keeping
+                 , plist :: [T.Idx]-- ^ list of primary instance indices
+                 , slist :: [T.Idx]-- ^ list of secondary instance indices
+                 , idx :: T.Ndx    -- ^ internal index for book-keeping
                  , peers :: PeerMap.PeerMap -- ^ pnode to instance mapping
                  , failN1:: Bool   -- ^ whether the node has failed n1
                  , r_mem :: Int    -- ^ maximum memory needed for
@@ -70,13 +70,13 @@ instance T.Element Node where
     setIdx = setIdx
 
 -- | A simple name for the int, node association list
-type AssocList = [(Int, Node)]
+type AssocList = [(T.Ndx, Node)]
 
 -- | A simple name for a node map
 type List = Container.Container Node
 
 -- | Constant node index for a non-moveable instance
-noSecondary :: Int
+noSecondary :: T.Ndx
 noSecondary = -1
 
 {- | Create a new node.
@@ -111,7 +111,7 @@ create name_init mem_t_init mem_n_init mem_f_init
 
 -- | Changes the index.
 -- This is used only during the building of the data structures.
-setIdx :: Node -> Int -> Node
+setIdx :: Node -> T.Ndx -> Node
 setIdx t i = t {idx = i}
 
 -- | Changes the name
@@ -148,7 +148,7 @@ computeMaxRes :: PeerMap.PeerMap -> PeerMap.Elem
 computeMaxRes new_peers = PeerMap.maxElem new_peers
 
 -- | Builds the peer map for a given node.
-buildPeers :: Node -> Container.Container Instance.Instance -> Int -> Node
+buildPeers :: Node -> Instance.List -> Int -> Node
 buildPeers t il num_nodes =
     let mdata = map
                 (\i_idx -> let inst = Container.find i_idx il
@@ -214,7 +214,7 @@ addPri t inst =
                 failN1 = new_failn1, p_mem = new_mp, p_dsk = new_dp}
 
 -- | Adds a secondary instance.
-addSec :: Node -> Instance.Instance -> Int -> Maybe Node
+addSec :: Node -> Instance.Instance -> T.Ndx -> Maybe Node
 addSec t inst pdx =
     let iname = Instance.idx inst
         old_peers = peers t
@@ -237,11 +237,11 @@ addSec t inst pdx =
                 p_rem = new_prem}
 
 -- | Add a primary instance to a node without other updates
-setPri :: Node -> Int -> Node
+setPri :: Node -> T.Idx -> Node
 setPri t idx = t { plist = idx:(plist t) }
 
 -- | Add a secondary instance to a node without other updates
-setSec :: Node -> Int -> Node
+setSec :: Node -> T.Idx -> Node
 setSec t idx = t { slist = idx:(slist t) }
 
 -- | String converter for the node list functionality.
diff --git a/Ganeti/HTools/PeerMap.hs b/Ganeti/HTools/PeerMap.hs
index 173c8c992..9c142e917 100644
--- a/Ganeti/HTools/PeerMap.hs
+++ b/Ganeti/HTools/PeerMap.hs
@@ -26,7 +26,9 @@ import Data.List
 import Data.Function
 import Data.Ord
 
-type Key = Int
+import Ganeti.HTools.Types
+
+type Key = Ndx
 type Elem = Int
 type PeerMap = [(Key, Elem)]
 
diff --git a/Ganeti/HTools/Rapi.hs b/Ganeti/HTools/Rapi.hs
index 5f028c3b5..bdf2dffb0 100644
--- a/Ganeti/HTools/Rapi.hs
+++ b/Ganeti/HTools/Rapi.hs
@@ -51,7 +51,7 @@ getNodes body = do
   nlist <- mapM parseNode arr
   return nlist
 
-parseInstance :: [(String, Int)]
+parseInstance :: [(String, Ndx)]
               -> JSObject JSValue
               -> Result (String, Instance.Instance)
 parseInstance ktn a = do
diff --git a/Ganeti/HTools/Text.hs b/Ganeti/HTools/Text.hs
index 0c8715c68..19826cce9 100644
--- a/Ganeti/HTools/Text.hs
+++ b/Ganeti/HTools/Text.hs
@@ -44,7 +44,7 @@ loadNode s = fail $ "Invalid/incomplete node data: '" ++ (show s) ++ "'"
 
 -- | Load an instance from a field list
 loadInst :: (Monad m) =>
-            [(String, Int)] -> [String] -> m (String, Instance.Instance)
+            [(String, Ndx)] -> [String] -> m (String, Instance.Instance)
 loadInst ktn (name:mem:dsk:status:pnode:snode:[]) = do
   pidx <- lookupNode ktn name pnode
   sidx <- (if null snode then return Node.noSecondary
diff --git a/Ganeti/HTools/Types.hs b/Ganeti/HTools/Types.hs
index a0a83afce..116b3eee1 100644
--- a/Ganeti/HTools/Types.hs
+++ b/Ganeti/HTools/Types.hs
@@ -5,6 +5,12 @@
 module Ganeti.HTools.Types
     where
 
+-- | The instance index type
+type Idx = Int
+
+-- | The node index type
+type Ndx = Int
+
 -- | The type used to hold idx-to-name mappings
 type NameList = [(Int, String)]
 
diff --git a/hail.hs b/hail.hs
index 9d436f9d8..69348d125 100644
--- a/hail.hs
+++ b/hail.hs
@@ -85,15 +85,15 @@ tryAlloc _ _ _ reqn = fail $ "Unsupported number of alllocation \
 tryReloc :: (Monad m) =>
             Node.List
          -> Instance.List
+         -> Idx
          -> Int
-         -> Int
-         -> [Int]
+         -> [Ndx]
          -> m [(Maybe Node.List, [Node.Node])]
 tryReloc nl il xid 1 ex_idx =
     let all_nodes = getOnline nl
         inst = Container.find xid il
         ex_idx' = (Instance.pnode inst):ex_idx
-        valid_nodes = filter (not . flip elem ex_idx' . idxOf) all_nodes
+        valid_nodes = filter (not . flip elem ex_idx' . Node.idx) all_nodes
         valid_idxes = map Node.idx valid_nodes
         sols1 = map (\x -> let (mnl, _, _, _) =
                                     Cluster.applyMove nl inst
-- 
GitLab