Francesco Romani has uploaded a new change for review.

Change subject: WIP: single-threaded Guest Manager
......................................................................

WIP: single-threaded Guest Manager

This patch is still a proof of concept, and a live demo.
There are plenty of issues, e.g I'd really like to avoid
inheritance.

Change-Id: I96b96fef0c0c42fd45b76754da9c629865ab1954
Signed-off-by: Francesco Romani <[email protected]>
---
M mom/GuestManager.py
M mom/__init__.py
2 files changed, 92 insertions(+), 18 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/mom refs/changes/26/37826/1

diff --git a/mom/GuestManager.py b/mom/GuestManager.py
index 889c12b..bcc65ff 100644
--- a/mom/GuestManager.py
+++ b/mom/GuestManager.py
@@ -27,7 +27,7 @@
 GuestData = namedtuple('GuestData', ['guest', 'monitor'])
 
 
-class GuestManager(threading.Thread):
+class _GuestManager(threading.Thread):
     """
     The GuestManager thread maintains a list of currently active guests on the
     system.  When a new guest is discovered, a new GuestMonitor is spawned.
@@ -74,6 +74,9 @@
                 if domain_list is not None:
                     self._spawn_guest_monitors(domain_list)
                     self._check_guest_monitors(domain_list)
+
+                self._collect_from_guest_monitors()
+
                 time.sleep(interval)
             self._wait_for_guest_monitors()
         except Exception as e:
@@ -95,12 +98,62 @@
                 self.logger.error("Failed to get guest:%s information -- 
monitor "\
                     "can't start", id)
                 continue
-            guest = GuestMonitor(self.config, info, self.hypervisor_iface)
-            thread = GuestMonitorThread(info, guest)
-            thread.start()
-            if guest.isAlive():
+
+            guest, thread = self._create_monitor(info)
+
+            if guest.monitor.isAlive():
                 with self.guests_sem:
-                    self._register_guest(id, GuestData(guest, thread))
+                    self._register_guest(id, guest)
+
+    # to be filled
+
+    def _create_monitor(self, info):
+        """
+        must return GuestData
+        """
+        return GuestData(None, None)
+
+    def _wait_for_guest_monitors(self):
+        """
+        Wait for GuestMonitors to exit
+        """
+        pass
+
+    def _check_guest_monitors(self, domain_list):
+        """
+        Wait for GuestMonitors to exit
+        """
+
+    def _collect_from_guest_monitors(self):
+        """
+        Trigger data collection from all monitors
+        """
+        pass
+
+    # utilities
+
+    def _register_guest(self, uuid, guest):
+        if uuid not in self.guests:
+            self.guests[uuid] = guest
+        else:
+            del guest
+
+    def _unregister_guest(self, uuid):
+        if uuid in self.guests:
+            guest = self.guests.pop(uuid)
+            if guest.thread is not None:
+                guest.thread.terminate()
+
+
+class MultiThreadGuestManager(_GuestManager):
+    def __init__(self, config, hypervisor_iface):
+        _GuestManager.__init__(self, config, hypervisor_iface)
+
+    def _create_monitor(self, info):
+        guest = GuestMonitor(self.config, info, self.hypervisor_iface)
+        thread = GuestMonitorThread(info, guest)
+        thread.start()
+        return GuestData(guest, thread)
 
     def _wait_for_guest_monitors(self):
         """
@@ -134,14 +187,34 @@
                 elif uuid not in domain_list:
                     self._unregister_guest(uuid)
 
-    def _register_guest(self, uuid, guest):
-        if uuid not in self.guests:
-            self.guests[uuid] = guest
-        else:
-            del guest
 
-    def _unregister_guest(self, uuid):
-        if uuid in self.guests:
-            guest = self.guests.pop(uuid)
-            if guest.thread is not None:
-                guest.thread.terminate()
+class SingleThreadGuestManager(_GuestManager):
+    def __init__(self, config, hypervisor_iface):
+        _GuestManager.__init__(self, config, hypervisor_iface)
+
+    def _create_monitor(self, info):
+        return GuestData(
+            GuestMonitor(self.config, info, self.hypervisor_iface),
+            None)
+
+    def _check_guest_monitors(self, domain_list):
+        """
+        Check for stale and/or deceased guest monitors and remove them.
+        """
+        with self.guests_sem:
+            for uuid, guest in self.guests.items():
+                if uuid not in domain_list:
+                    self._unregister_guest(uuid)
+
+    def _collect_from_guest_monitors(self):
+        with self.guests_sem:
+            for uuid, guest in self.guests.items():
+                if guest.monitor.isAlive():
+                    guest.monitor.collect()
+
+
+def make(config, hypervisor_iface):
+    if config.getboolean('main', 'guest-manager-multi-thread'):
+        return MultiThreadGuestManager(config, hypervisor_iface)
+    else:
+        return SingleThreadGuestManager(config, hypervisor_iface)
diff --git a/mom/__init__.py b/mom/__init__.py
index 8335665..a2c2ad8 100644
--- a/mom/__init__.py
+++ b/mom/__init__.py
@@ -6,7 +6,7 @@
 import logging.handlers
 from mom.LogUtils import *
 from mom.HostMonitor import HostMonitor
-from mom.GuestManager import GuestManager
+from mom import GuestManager
 from mom.PolicyEngine import PolicyEngine
 from mom.RPCServer import RPCServer
 from mom.MOMFuncs import MOMFuncs, EXPORTED_ATTRIBUTE
@@ -31,6 +31,7 @@
     config.set('main', 'rpc-port', '-1')
     config.set('main', 'policy', '')
     config.set('main', 'policy-dir', '')
+    config.set('main', 'guest-manager-multi-thread', 'true')
     config.add_section('logging')
     config.set('logging', 'log', 'stdio')
     config.set('logging', 'verbosity', 'info')
@@ -89,7 +90,7 @@
             hypervisor_iface = self.get_hypervisor_interface()
             if not hypervisor_iface:
                 self.shutdown()
-            guest_manager = GuestManager(self.config, hypervisor_iface)
+            guest_manager = GuestManager.make(self.config, hypervisor_iface)
             guest_manager.start()
             policy_engine = PolicyEngine(self.config, hypervisor_iface,
                                          host_monitor, guest_manager)


-- 
To view, visit http://gerrit.ovirt.org/37826
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I96b96fef0c0c42fd45b76754da9c629865ab1954
Gerrit-PatchSet: 1
Gerrit-Project: mom
Gerrit-Branch: master
Gerrit-Owner: Francesco Romani <[email protected]>
_______________________________________________
Engine-patches mailing list
[email protected]
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to