From 5e15f460c8ff22e3508191f349b946582568da53 Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Mon, 25 May 2009 19:31:03 +0100
Subject: [PATCH] hail: Implement non-mirrored instance allocation
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This patch implements non-mirrored instance allocation, by allocating as
secondary node β€œnoSecondary”.
---
 Ganeti/HTools/Cluster.hs | 20 +++++++++++++++-----
 hail.hs                  | 24 +++++++++++++-----------
 2 files changed, 28 insertions(+), 16 deletions(-)

diff --git a/Ganeti/HTools/Cluster.hs b/Ganeti/HTools/Cluster.hs
index 75f7ef996..9c59616b2 100644
--- a/Ganeti/HTools/Cluster.hs
+++ b/Ganeti/HTools/Cluster.hs
@@ -34,7 +34,8 @@ module Ganeti.HTools.Cluster
     , compCV
     , printStats
     -- * IAllocator functions
-    , allocateOn
+    , allocateOnSingle
+    , allocateOnPair
     ) where
 
 import Data.List
@@ -409,10 +410,19 @@ applyMove nl inst (FailoverAndReplace new_sdx) =
                  Container.addTwo old_sdx new_p old_pdx int_p nl
     in (new_nl, Instance.setBoth inst old_sdx new_sdx, old_sdx, new_sdx)
 
-allocateOn nl inst new_pdx new_sdx =
-    let
-        tgt_p = Container.find new_pdx nl
-        tgt_s = Container.find new_sdx nl
+allocateOnSingle :: NodeList -> Instance.Instance -> Node.Node
+                 -> (Maybe NodeList, Instance.Instance)
+allocateOnSingle nl inst p =
+    let new_pdx = Node.idx p
+        new_nl = Node.addPri p inst >>= \new_p ->
+                 return $ Container.add new_pdx new_p nl
+    in (new_nl, Instance.setBoth inst new_pdx Node.noSecondary)
+
+allocateOnPair :: NodeList -> Instance.Instance -> Node.Node -> Node.Node
+               -> (Maybe NodeList, Instance.Instance)
+allocateOnPair nl inst tgt_p tgt_s =
+    let new_pdx = Node.idx tgt_p
+        new_sdx = Node.idx tgt_s
         new_nl = do -- Maybe monad
           new_p <- Node.addPri tgt_p inst
           new_s <- Node.addSec tgt_s inst new_pdx
diff --git a/hail.hs b/hail.hs
index 23dca5d33..ef9b589a9 100644
--- a/hail.hs
+++ b/hail.hs
@@ -21,7 +21,6 @@ import qualified Ganeti.HTools.Node as Node
 import qualified Ganeti.HTools.Instance as Instance
 import qualified Ganeti.HTools.CLI as CLI
 import Ganeti.HTools.IAlloc
-import Ganeti.HTools.Utils
 import Ganeti.HTools.Types
 
 -- | Command line options structure.
@@ -120,17 +119,20 @@ tryAlloc :: (Monad m) =>
          -> Instance.Instance
          -> Int
          -> m [(Maybe NodeList, [Node.Node])]
-tryAlloc nl il inst 2 =
+tryAlloc nl _ inst 2 =
     let all_nodes = Container.elems nl
         all_pairs = liftM2 (,) all_nodes all_nodes
         ok_pairs = filter (\(x, y) -> Node.idx x /= Node.idx y) all_pairs
-        sols1 = map (\(p, s) -> let pdx = Node.idx p
-                                    sdx = Node.idx s
-                                    (mnl, _) = Cluster.allocateOn nl
-                                               inst pdx sdx
-                                in (mnl, [p, s])
-                     ) ok_pairs
-    in return sols1
+        sols = map (\(p, s) ->
+                        (fst $ Cluster.allocateOnPair nl inst p s, [p, s]))
+               ok_pairs
+    in return sols
+
+tryAlloc nl _ inst 1 =
+    let all_nodes = Container.elems nl
+        sols = map (\p -> (fst $ Cluster.allocateOnSingle nl inst p, [p]))
+               all_nodes
+    in return sols
 
 tryAlloc _ _ _ reqn = fail $ "Unsupported number of alllocation \
                              \destinations required (" ++ (show reqn) ++
@@ -180,8 +182,8 @@ processResults sols =
         sols'' = sortBy (compare `on` fst) sols'
         (best, w) = head sols''
         (worst, l) = last sols''
-        info = printf "Valid results: %d, best score: %.8f (nodes %s), \
-                      \worst score: %.8f (nodes %s)" (length sols'')
+        info = printf "Valid results: %d, best score: %.8f for node(s) %s, \
+                      \worst score: %.8f for node(s) %s" (length sols'')
                       best (intercalate "/" . map Node.name $ w)
                       worst (intercalate "/" . map Node.name $ l)
     in return (info, w)
-- 
GitLab