From d23ee23f75aafa0ec6c6d4ab5a621e763b93eefe Mon Sep 17 00:00:00 2001
From: "Ruben S. Montero" <rsmontero@opennebula.org>
Date: Fri, 2 Sep 2016 13:58:33 +0200
Subject: [PATCH] Do not update disk monitor infor in the DB. Disk usage will
 be kept in the object and written to the DB the next time the VM is updated
 (presumably, in the next monitor message from probes). This will reduce DB
 I/O needs

(cherry picked from commit 9c929148fcda3c261aeb42d1440573473704bd0e)
---
 include/VirtualMachineManagerDriver.h  |  7 +++++--
 src/im/MonitorThread.cc                |  2 +-
 src/image/ImageManagerDriver.cc        |  2 +-
 src/vmm/VirtualMachineManagerDriver.cc | 25 ++++++++++++++++---------
 4 files changed, 23 insertions(+), 13 deletions(-)

diff --git a/include/VirtualMachineManagerDriver.h b/include/VirtualMachineManagerDriver.h
index c32b57c..c18e9ad 100644
--- a/include/VirtualMachineManagerDriver.h
+++ b/include/VirtualMachineManagerDriver.h
@@ -74,16 +74,19 @@ public:
      *
      * @param id VM id
      * @param monitor_str String returned by the poll driver call
+     * @param update_db write data to DB (or keep it in memory)
      */
-    static void process_poll(int id, const string &monitor_str);
+    static void process_poll(int id, const string &monitor_str, bool update_db);
 
     /**
      * Updates the VM with the information gathered by the drivers
      *
      * @param vm VM to update, must be locked
      * @param monitor_str String returned by the poll driver call
+     * @param update_db write data to DB (or keep it in memory)
      */
-    static void process_poll(VirtualMachine* vm, const string &monitor_str);
+    static void process_poll(VirtualMachine* vm, const string &monitor_str,
+        bool update_db);
 
     /**
      *  Check if action is supported for imported VMs
diff --git a/src/im/MonitorThread.cc b/src/im/MonitorThread.cc
index e79a896..9f2e2be 100644
--- a/src/im/MonitorThread.cc
+++ b/src/im/MonitorThread.cc
@@ -282,7 +282,7 @@ void MonitorThread::do_message()
                 continue;
             }
 
-            VirtualMachineManagerDriver::process_poll(vm, itm->second);
+            VirtualMachineManagerDriver::process_poll(vm, itm->second, true);
 
             vm->unlock();
         }
diff --git a/src/image/ImageManagerDriver.cc b/src/image/ImageManagerDriver.cc
index 64cc412..4770268 100644
--- a/src/image/ImageManagerDriver.cc
+++ b/src/image/ImageManagerDriver.cc
@@ -706,7 +706,7 @@ static void monitor_action(istringstream& is,
             continue;
         }
 
-        VirtualMachineManagerDriver::process_poll(vm_id, poll_info);
+        VirtualMachineManagerDriver::process_poll(vm_id, poll_info, false);
     }
 
     oss << "Datastore " << ds_name << " (" << id << ") successfully monitored.";
diff --git a/src/vmm/VirtualMachineManagerDriver.cc b/src/vmm/VirtualMachineManagerDriver.cc
index de51da7..1ffcaa7 100644
--- a/src/vmm/VirtualMachineManagerDriver.cc
+++ b/src/vmm/VirtualMachineManagerDriver.cc
@@ -645,7 +645,7 @@ void VirtualMachineManagerDriver::protocol(const string& message) const
             string monitor_str;
             getline(is, monitor_str);
 
-            process_poll(vm, monitor_str);
+            process_poll(vm, monitor_str, true);
         }
         else
         {
@@ -670,7 +670,8 @@ void VirtualMachineManagerDriver::protocol(const string& message) const
 
 void VirtualMachineManagerDriver::process_poll(
         int id,
-        const string &monitor_str)
+        const string &monitor_str,
+        bool update_db)
 {
     // Get the VM from the pool
     VirtualMachine* vm = Nebula::instance().get_vmpool()->get(id,true);
@@ -680,7 +681,7 @@ void VirtualMachineManagerDriver::process_poll(
         return;
     }
 
-    process_poll(vm, monitor_str);
+    process_poll(vm, monitor_str, update_db);
 
     vm->unlock();
 }
@@ -690,7 +691,8 @@ void VirtualMachineManagerDriver::process_poll(
 
 void VirtualMachineManagerDriver::process_poll(
         VirtualMachine* vm,
-        const string&   monitor_str)
+        const string&   monitor_str,
+        bool            update_db)
 {
     char state;
 
@@ -705,14 +707,19 @@ void VirtualMachineManagerDriver::process_poll(
 
     if (vm->get_state() == VirtualMachine::ACTIVE)
     {
-        if (vm->update_info(monitor_str) == 0)
+        int rc = vm->update_info(monitor_str);
+
+        if ( update_db )
         {
-            vmpool->update_history(vm);
+            if ( rc == 0)
+            {
+                vmpool->update_history(vm);
 
-            vmpool->update_monitoring(vm);
-        }
+                vmpool->update_monitoring(vm);
+            }
 
-        vmpool->update(vm);
+            vmpool->update(vm);
+        }
 
         VirtualMachineMonitorInfo &minfo = vm->get_info();
 
-- 
2.9.3

