htools: split tryMGAlloc in two
authorIustin Pop <iustin@google.com>
Wed, 6 Jul 2011 22:44:21 +0000 (00:44 +0200)
committerIustin Pop <iustin@google.com>
Fri, 15 Jul 2011 13:10:55 +0000 (15:10 +0200)
Currently, this function does both identify the best group for an
instance, and format the results (partially). The former functionality
is useful in other contexts too, so let's split the function in two.

Signed-off-by: Iustin Pop <iustin@google.com>
Reviewed-by: Guido Trotter <ultrotter@google.com>

htools/Ganeti/HTools/Cluster.hs

index ecd6c91..ce5afc9 100644 (file)
@@ -748,14 +748,14 @@ sortMGResults gl sols =
                                (extractScore . head . asSolutions) sol)
     in sortBy (comparing solScore) sols
 
--- | Try to allocate an instance on a multi-group cluster.
-tryMGAlloc :: Group.List           -- ^ The group list
-           -> Node.List            -- ^ The node list
-           -> Instance.List        -- ^ The instance list
-           -> Instance.Instance    -- ^ The instance to allocate
-           -> Int                  -- ^ Required number of nodes
-           -> Result AllocSolution -- ^ Possible solution list
-tryMGAlloc mggl mgnl mgil inst cnt =
+-- | Finds the best group for an instance on a multi-group cluster.
+findBestAllocGroup :: Group.List           -- ^ The group list
+                   -> Node.List            -- ^ The node list
+                   -> Instance.List        -- ^ The instance list
+                   -> Instance.Instance    -- ^ The instance to allocate
+                   -> Int                  -- ^ Required number of nodes
+                   -> Result (Gdx, AllocSolution, [String])
+findBestAllocGroup mggl mgnl mgil inst cnt =
   let groups = splitCluster mgnl mgil
       sols = map (\(gid, (nl, il)) ->
                    (gid, genAllocNodes mggl nl cnt False >>=
@@ -767,9 +767,21 @@ tryMGAlloc mggl mgnl mgil inst cnt =
   in if null sortedSols
      then Bad $ intercalate ", " all_msgs
      else let (final_group, final_sol) = head sortedSols
-              final_name = Group.name $ Container.find final_group mggl
-              selmsg = "Selected group: " ++  final_name
-          in Ok $ final_sol { asLog = selmsg:all_msgs }
+          in return (final_group, final_sol, all_msgs)
+
+-- | Try to allocate an instance on a multi-group cluster.
+tryMGAlloc :: Group.List           -- ^ The group list
+           -> Node.List            -- ^ The node list
+           -> Instance.List        -- ^ The instance list
+           -> Instance.Instance    -- ^ The instance to allocate
+           -> Int                  -- ^ Required number of nodes
+           -> Result AllocSolution -- ^ Possible solution list
+tryMGAlloc mggl mgnl mgil inst cnt = do
+  (best_group, solution, all_msgs) <-
+      findBestAllocGroup mggl mgnl mgil inst cnt
+  let group_name = Group.name $ Container.find best_group mggl
+      selmsg = "Selected group: " ++ group_name
+  return $ solution { asLog = selmsg:all_msgs }
 
 -- | Try to relocate an instance on the cluster.
 tryReloc :: (Monad m) =>