htools: add a function for computing evacuated nodes
authorIustin Pop <iustin@google.com>
Mon, 4 Jul 2011 21:41:52 +0000 (23:41 +0200)
committerIustin Pop <iustin@google.com>
Thu, 14 Jul 2011 09:05:58 +0000 (11:05 +0200)
The new IAllocator interface is based on instances, not nodes, so we
need to backtrack and compute on which nodes we can't allocate
instances during the current operation. This patch adds a function for
computing this node set.

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

htools/Ganeti/HTools/Cluster.hs

index 901c981..1cc6117 100644 (file)
@@ -74,6 +74,7 @@ module Ganeti.HTools.Cluster
     ) where
 
 import Data.Function (on)
+import qualified Data.IntSet as IntSet
 import Data.List
 import Data.Ord (comparing)
 import Text.Printf (printf)
@@ -1103,3 +1104,25 @@ associateIdxs :: [Idx] -- ^ Instance indices to be split/associated
 associateIdxs idxs =
     map (\(gdx, (nl, il)) ->
              (gdx, (nl, il, filter (`Container.member` il) idxs)))
+
+-- | Compute the list of nodes that are to be evacuated, given a list
+-- of instances and an evacuation mode.
+nodesToEvacuate :: Instance.List -- ^ The cluster-wide instance list
+                -> EvacMode      -- ^ The evacuation mode we're using
+                -> [Idx]         -- ^ List of instance indices being evacuated
+                -> IntSet.IntSet -- ^ Set of node indices
+nodesToEvacuate il mode =
+    IntSet.delete Node.noSecondary .
+    foldl' (\ns idx ->
+                let i = Container.find idx il
+                    pdx = Instance.pNode i
+                    sdx = Instance.sNode i
+                    dt = Instance.diskTemplate i
+                    withSecondary = case dt of
+                                      DTDrbd8 -> IntSet.insert sdx ns
+                                      _ -> ns
+                in case mode of
+                     ChangePrimary   -> IntSet.insert pdx ns
+                     ChangeSecondary -> withSecondary
+                     ChangeAll       -> IntSet.insert pdx withSecondary
+           ) IntSet.empty