From 18640d69547cc8dab1c9802d5cb20446a4e8386e Mon Sep 17 00:00:00 2001
From: Guido Trotter <ultrotter@google.com>
Date: Mon, 24 Nov 2008 09:47:32 +0000
Subject: [PATCH] IAllocator: subtract down instances from free mem

Currently free_memory just reports the amount of free ram, as seen by the
hypervisor. We adjust this amount by subtracting the memory for any instance
which is down, and the difference for any instance which is configured to have
more memory than the amount it's currently running at.

Reviewed-by: iustinp
---
 lib/cmdlib.py | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/lib/cmdlib.py b/lib/cmdlib.py
index 6807dcadc..c931bb755 100644
--- a/lib/cmdlib.py
+++ b/lib/cmdlib.py
@@ -5378,6 +5378,8 @@ class IAllocator(object):
 
     node_data = self.lu.rpc.call_node_info(node_list, cfg.GetVGName(),
                                            hypervisor)
+    node_iinfo = self.lu.rpc.call_all_instances_info(node_list,
+                       cluster_info.enabled_hypervisors)
     for nname in node_list:
       ninfo = cfg.GetNodeInfo(nname)
       if nname not in node_data or not isinstance(node_data[nname], dict):
@@ -5398,6 +5400,13 @@ class IAllocator(object):
       for iinfo, beinfo in i_list:
         if iinfo.primary_node == nname:
           i_p_mem += beinfo[constants.BE_MEMORY]
+          if iinfo.name not in node_iinfo[nname]:
+            i_used_mem = 0
+          else:
+            i_used_mem = int(node_iinfo[nname][iinfo.name]['memory'])
+          i_mem_diff = beinfo[constants.BE_MEMORY] - i_used_mem
+          remote_info['memory_free'] -= max(0, i_mem_diff)
+
           if iinfo.status == "up":
             i_p_up_mem += beinfo[constants.BE_MEMORY]
 
-- 
GitLab