[icinga-checkins] icinga.org: icingaweb2-module-director/feature/icinga-legacy-config-13049: IcingaServiceSet: Try to prefetch related services

git at icinga.org git at icinga.org
Tue Nov 15 16:44:49 CET 2016


Module: icingaweb2-module-director
Branch: feature/icinga-legacy-config-13049
Commit: 205768a14ff2a6e46debf3b6712c20f6bffd3200
URL:    https://git.icinga.org/?p=icingaweb2-module-director.git;a=commit;h=205768a14ff2a6e46debf3b6712c20f6bffd3200

Author: Markus Frosch <markus.frosch at icinga.com>
Date:   Tue Nov 15 16:37:19 2016 +0100

IcingaServiceSet: Try to prefetch related services

Query optimization...

refs #13049

---

 library/Director/Objects/IcingaServiceSet.php |   56 ++++++++++++++++++-------
 1 file changed, 41 insertions(+), 15 deletions(-)

diff --git a/library/Director/Objects/IcingaServiceSet.php b/library/Director/Objects/IcingaServiceSet.php
index 4251a26..4486040 100644
--- a/library/Director/Objects/IcingaServiceSet.php
+++ b/library/Director/Objects/IcingaServiceSet.php
@@ -4,6 +4,7 @@ namespace Icinga\Module\Director\Objects;
 
 use Icinga\Data\Filter\Filter;
 use Icinga\Exception\IcingaException;
+use Icinga\Module\Director\Db\Cache\PrefetchCache;
 use Icinga\Module\Director\IcingaConfig\IcingaConfig;
 
 
@@ -34,6 +35,8 @@ class IcingaServiceSet extends IcingaObject
         'host' => 'IcingaHost',
     );
 
+    protected static $fetchedServices = null;
+
     public function isDisabled()
     {
         return false;
@@ -80,31 +83,54 @@ class IcingaServiceSet extends IcingaObject
         }
     }
 
+    protected function fetchServices(IcingaServiceSet $set)
+    {
+        if (PrefetchCache::shouldBeUsed()) {
+            if (static::$fetchedServices === null) {
+                $services = IcingaObject::loadAllByType('service', $this->getConnection());
+                static::$fetchedServices = array();
+
+                foreach ($services as $service) {
+                    if ($service->service_set_id === $set->id) {
+                        if (! array_key_exists($set->id, static::$fetchedServices)) {
+                            static::$fetchedServices[$set->id] = array();
+                        }
+                        static::$fetchedServices[$set->id][] = $service;
+                    }
+                }
+            }
+
+            if (array_key_exists($set->id, static::$fetchedServices)) {
+                return static::$fetchedServices[$set->id];
+            }
+            else {
+                return array();
+            }
+        }
+        else {
+            $db = $this->getConnection();
+            $query = $db->select()
+                ->where('service_set_id', $set->id);
+
+            return IcingaService::loadAll($db, $query);
+        }
+    }
+
     protected function getServiceObjectsForSet(IcingaServiceSet $set)
     {
         if ($set->get('id') === null) {
             return array();
         }
 
-        $connection = $this->getConnection();
-        $db = $this->getDb();
-        $ids = $db->fetchCol(
-            $db->select()->from('icinga_service', 'id')
-                ->where('service_set_id = ?', $set->get('id'))
-        );
+        $services = $this->fetchServices($set);
 
-        $services = array();
-        foreach ($ids as $id) {
-            $service = IcingaService::load(array(
-                'id' => $id,
-                'object_type' => 'template'
-            ), $connection);
+        $result = array();
+        foreach ($services as $service) {
             $service->set('service_set', null);
-
-            $services[$service->getObjectName()] = $service;
+            $result[$service->getObjectName()] = $service;
         }
 
-        return $services;
+        return $result;
     }
 
     public function renderToConfig(IcingaConfig $config)



More information about the icinga-checkins mailing list