From 5cc97485fe65d17a70d017a2f42a060911206185 Mon Sep 17 00:00:00 2001
From: Agata Murawska <agatamurawska@google.com>
Date: Tue, 26 Jun 2012 14:39:36 +0200
Subject: [PATCH] Cleaner simulation of rebalance
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This replaces the 'if .. then .. else ..' with a large block of code in
'else' branch by separating this code into a separate function.

Signed-off-by: Agata Murawska <agatamurawska@google.com>
Reviewed-by: RenΓ© Nussbaumer <rn@google.com>
---
 htools/Ganeti/HTools/Program/Hcheck.hs | 46 ++++++++++++++------------
 1 file changed, 25 insertions(+), 21 deletions(-)

diff --git a/htools/Ganeti/HTools/Program/Hcheck.hs b/htools/Ganeti/HTools/Program/Hcheck.hs
index ede8794fd..04d9a41d4 100644
--- a/htools/Ganeti/HTools/Program/Hcheck.hs
+++ b/htools/Ganeti/HTools/Program/Hcheck.hs
@@ -207,31 +207,34 @@ perGroupChecks verbose machineread phase gl (gidx, (nl, il)) = do
   return groupstats
 
 -- | Use Hbal's iterateDepth to simulate group rebalance.
-simulateRebalance :: Options ->
-                     (Gdx, (Node.List, Instance.List)) ->
+executeSimulation :: Options -> Cluster.Table -> Double ->
+                     Gdx -> Node.List -> Instance.List ->
                      IO ( (Gdx, (Node.List, Instance.List)) )
-simulateRebalance opts (gidx, (nl, il)) = do
+executeSimulation opts ini_tbl min_cv gidx nl il = do
+  let imlen = maximum . map (length . Instance.alias) $ Container.elems il
+      nmlen = maximum . map (length . Node.alias) $ Container.elems nl
+
+  (fin_tbl, _) <- Hbal.iterateDepth False ini_tbl
+                                    (optMaxLength opts)
+                                    (optDiskMoves opts)
+                                    (optInstMoves opts)
+                                    nmlen imlen [] min_cv
+                                    (optMinGainLim opts) (optMinGain opts)
+                                    (optEvacMode opts)
+
+  let (Cluster.Table fin_nl fin_il _ _) = fin_tbl
+  return (gidx, (fin_nl, fin_il))
+
+-- | Simulate group rebalance if group's score is not good
+maybeSimulateGroupRebalance :: Options -> (Gdx, (Node.List, Instance.List))
+                               -> IO ((Gdx, (Node.List, Instance.List)))
+maybeSimulateGroupRebalance opts (gidx, (nl, il)) = do
   let ini_cv = Cluster.compCV nl
       ini_tbl = Cluster.Table nl il ini_cv []
       min_cv = optMinScore opts
-
-
   if (ini_cv < min_cv)
     then return (gidx, (nl, il))
-    else do
-      let imlen = maximum . map (length . Instance.alias) $ Container.elems il
-          nmlen = maximum . map (length . Node.alias) $ Container.elems nl
-
-      (fin_tbl, _) <- Hbal.iterateDepth False ini_tbl
-                                        (optMaxLength opts)
-                                        (optDiskMoves opts)
-                                        (optInstMoves opts)
-                                        nmlen imlen [] min_cv
-                                        (optMinGainLim opts) (optMinGain opts)
-                                        (optEvacMode opts)
-
-      let (Cluster.Table fin_nl fin_il _ _) = fin_tbl
-      return (gidx, (fin_nl, fin_il))
+    else executeSimulation opts ini_tbl min_cv gidx nl il
 
 -- | Decide whether to simulate rebalance.
 maybeSimulateRebalance :: Bool             -- ^ Whether to simulate rebalance
@@ -239,7 +242,7 @@ maybeSimulateRebalance :: Bool             -- ^ Whether to simulate rebalance
                        -> [(Gdx, (Node.List, Instance.List))] -- ^ Group data
                        -> IO([(Gdx, (Node.List, Instance.List))])
 maybeSimulateRebalance True opts cluster =
-    mapM (simulateRebalance opts) cluster
+    mapM (maybeSimulateGroupRebalance opts) cluster
 maybeSimulateRebalance False _ cluster = return cluster
 
 -- | Prints the final @OK@ marker in machine readable output.
@@ -269,7 +272,8 @@ main opts args = do
   let clusterstats = map sum (transpose groupsstats) :: [Int]
       needrebalance = clusterNeedsRebalance clusterstats
       canrebalance = length splitinstances == 0
-  printClusterStats verbose machineread Initial clusterstats needrebalance canrebalance
+  printClusterStats verbose machineread Initial clusterstats needrebalance
+                    canrebalance
 
   when nosimulation $ do
     unless (verbose == 0 || machineread) $
-- 
GitLab