PrefetchCache, IcingaObject: speed up import list

...greatly improves REST API performance
This commit is contained in:
Thomas Gelf 2017-08-01 12:48:29 +02:00
parent 1d683f972d
commit 59d6291fcf
2 changed files with 35 additions and 2 deletions

View File

@ -7,6 +7,7 @@ use Icinga\Module\Director\CustomVariable\CustomVariable;
use Icinga\Module\Director\Db; use Icinga\Module\Director\Db;
use Icinga\Module\Director\Objects\IcingaObject; use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\Objects\IcingaTemplateResolver; use Icinga\Module\Director\Objects\IcingaTemplateResolver;
use Icinga\Module\Director\Resolver\TemplateTree;
/** /**
* Central prefetch cache * Central prefetch cache
@ -29,6 +30,8 @@ class PrefetchCache
protected $renderedVars = array(); protected $renderedVars = array();
protected $templateTrees = array();
public static function initialize(Db $db) public static function initialize(Db $db)
{ {
self::$instance = new static($db); self::$instance = new static($db);
@ -78,6 +81,11 @@ class PrefetchCache
return $this->templateResolver($object)->setObject($object)->fetchParents(); return $this->templateResolver($object)->setObject($object)->fetchParents();
} }
public function listImportNames(IcingaObject $object)
{
return $this->templateTree($object)->listParentNamesForObject($object);
}
/* Hint: not implemented, this happens in DbObject right now /* Hint: not implemented, this happens in DbObject right now
public function byObjectType($type) public function byObjectType($type)
{ {
@ -136,6 +144,19 @@ class PrefetchCache
return $this->groupsCaches[$key]; return $this->groupsCaches[$key];
} }
protected function templateTree(IcingaObject $object)
{
$key = $object->getShortTableName();
if (! array_key_exists($key, $this->templateTrees)) {
$this->templateTrees[$key] = new TemplateTree(
$key,
$object->getConnection()
);
}
return $this->templateTrees[$key];
}
public function __destruct() public function __destruct()
{ {
unset($this->groupsCaches); unset($this->groupsCaches);

View File

@ -18,6 +18,7 @@ use Icinga\Module\Director\IcingaConfig\IcingaConfig;
use Icinga\Module\Director\IcingaConfig\IcingaConfigRenderer; use Icinga\Module\Director\IcingaConfig\IcingaConfigRenderer;
use Icinga\Module\Director\IcingaConfig\IcingaConfigHelper as c; use Icinga\Module\Director\IcingaConfig\IcingaConfigHelper as c;
use Icinga\Module\Director\IcingaConfig\IcingaLegacyConfigHelper as c1; use Icinga\Module\Director\IcingaConfig\IcingaLegacyConfigHelper as c1;
use Icinga\Module\Director\Resolver\TemplateTree;
abstract class IcingaObject extends DbObject implements IcingaConfigRenderer abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
{ {
@ -130,6 +131,8 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
private $templateResolver; private $templateResolver;
protected static $tree;
/** /**
* @return Db * @return Db
*/ */
@ -2526,9 +2529,9 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
if ($this->supportsImports()) { if ($this->supportsImports()) {
if ($resolved) { if ($resolved) {
$props['imports'] = array(); $props['imports'] = [];
} else { } else {
$props['imports'] = $this->imports()->listImportNames(); $props['imports'] = $this->listImportNames();
} }
} }
@ -2587,6 +2590,15 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
return (object) $props; return (object) $props;
} }
public function listImportNames()
{
if (PrefetchCache::shouldBeUsed()) {
return PrefetchCache::instance()->listImportNames($this);
} else {
return $this->imports()->listImportNames();
}
}
protected function differsFromDefaultValue($key, $value) protected function differsFromDefaultValue($key, $value)
{ {
if (array_key_exists($key, $this->defaultProperties)) { if (array_key_exists($key, $this->defaultProperties)) {