Decoupled Goal from Strategy
In this changeset, I decoupled the notion of Goal from the Strategy by making it a distinct object. Goals are plugins that can be loaded just like for the strategies. Partially Implements: blueprint efficacy-indicator Change-Id: I4378dccd508170b305aa968843228bbc8af78895
This commit is contained in:
26
watcher/decision_engine/goal/__init__.py
Normal file
26
watcher/decision_engine/goal/__init__.py
Normal file
@@ -0,0 +1,26 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
# Copyright (c) 2016 b<>com
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
# implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from watcher.decision_engine.goal import goals
|
||||
|
||||
Dummy = goals.Dummy
|
||||
ServerConsolidation = goals.ServerConsolidation
|
||||
ThermalOptimization = goals.ThermalOptimization
|
||||
Unclassified = goals.Unclassified
|
||||
WorkloadBalancing = goals.WorkloadBalancing
|
||||
|
||||
__all__ = ("Dummy", "ServerConsolidation", "ThermalOptimization",
|
||||
"Unclassified", "WorkloadBalancing", )
|
||||
62
watcher/decision_engine/goal/base.py
Normal file
62
watcher/decision_engine/goal/base.py
Normal file
@@ -0,0 +1,62 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
# Copyright (c) 2016 b<>com
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
# implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import abc
|
||||
import six
|
||||
|
||||
from oslo_log import log
|
||||
|
||||
from watcher.common.loader import loadable
|
||||
|
||||
LOG = log.getLogger(__name__)
|
||||
|
||||
|
||||
@six.add_metaclass(abc.ABCMeta)
|
||||
class Goal(loadable.Loadable):
|
||||
|
||||
def __init__(self, config):
|
||||
super(Goal, self).__init__(config)
|
||||
self.name = self.get_name()
|
||||
self.display_name = self.get_display_name()
|
||||
|
||||
@classmethod
|
||||
@abc.abstractmethod
|
||||
def get_name(cls):
|
||||
"""Name of the goal: should be identical to the related entry point"""
|
||||
raise NotImplementedError()
|
||||
|
||||
@classmethod
|
||||
@abc.abstractmethod
|
||||
def get_display_name(cls):
|
||||
"""The goal display name for the goal"""
|
||||
raise NotImplementedError()
|
||||
|
||||
@classmethod
|
||||
@abc.abstractmethod
|
||||
def get_translatable_display_name(cls):
|
||||
"""The translatable msgid of the goal"""
|
||||
# Note(v-francoise): Defined here to be used as the translation key for
|
||||
# other services
|
||||
raise NotImplementedError()
|
||||
|
||||
@classmethod
|
||||
def get_config_opts(cls):
|
||||
"""Defines the configuration options to be associated to this loadable
|
||||
|
||||
:return: A list of configuration options relative to this Loadable
|
||||
:rtype: list of :class:`oslo_config.cfg.Opt` instances
|
||||
"""
|
||||
return []
|
||||
93
watcher/decision_engine/goal/goals.py
Normal file
93
watcher/decision_engine/goal/goals.py
Normal file
@@ -0,0 +1,93 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
# Copyright (c) 2016 b<>com
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
# implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from watcher._i18n import _
|
||||
from watcher.decision_engine.goal import base
|
||||
|
||||
|
||||
class Dummy(base.Goal):
|
||||
|
||||
@classmethod
|
||||
def get_name(cls):
|
||||
return "dummy"
|
||||
|
||||
@classmethod
|
||||
def get_display_name(cls):
|
||||
return _("Dummy goal")
|
||||
|
||||
@classmethod
|
||||
def get_translatable_display_name(cls):
|
||||
return "Dummy goal"
|
||||
|
||||
|
||||
class Unclassified(base.Goal):
|
||||
|
||||
@classmethod
|
||||
def get_name(cls):
|
||||
return "unclassified"
|
||||
|
||||
@classmethod
|
||||
def get_display_name(cls):
|
||||
return _("Unclassified")
|
||||
|
||||
@classmethod
|
||||
def get_translatable_display_name(cls):
|
||||
return "Unclassified"
|
||||
|
||||
|
||||
class ServerConsolidation(base.Goal):
|
||||
|
||||
@classmethod
|
||||
def get_name(cls):
|
||||
return "server_consolidation"
|
||||
|
||||
@classmethod
|
||||
def get_display_name(cls):
|
||||
return _("Server consolidation")
|
||||
|
||||
@classmethod
|
||||
def get_translatable_display_name(cls):
|
||||
return "Server consolidation"
|
||||
|
||||
|
||||
class ThermalOptimization(base.Goal):
|
||||
|
||||
@classmethod
|
||||
def get_name(cls):
|
||||
return "thermal_optimization"
|
||||
|
||||
@classmethod
|
||||
def get_display_name(cls):
|
||||
return _("Thermal optimization")
|
||||
|
||||
@classmethod
|
||||
def get_translatable_display_name(cls):
|
||||
return "Thermal optimization"
|
||||
|
||||
|
||||
class WorkloadBalancing(base.Goal):
|
||||
|
||||
@classmethod
|
||||
def get_name(cls):
|
||||
return "workload_balancing"
|
||||
|
||||
@classmethod
|
||||
def get_display_name(cls):
|
||||
return _("Workload balancing")
|
||||
|
||||
@classmethod
|
||||
def get_translatable_display_name(cls):
|
||||
return "Workload balancing"
|
||||
@@ -27,3 +27,9 @@ class DefaultStrategyLoader(default.DefaultLoader):
|
||||
def __init__(self):
|
||||
super(DefaultStrategyLoader, self).__init__(
|
||||
namespace='watcher_strategies')
|
||||
|
||||
|
||||
class DefaultGoalLoader(default.DefaultLoader):
|
||||
def __init__(self):
|
||||
super(DefaultGoalLoader, self).__init__(
|
||||
namespace='watcher_goals')
|
||||
@@ -19,7 +19,7 @@ from oslo_log import log
|
||||
|
||||
from watcher._i18n import _
|
||||
from watcher.common import exception
|
||||
from watcher.decision_engine.strategy.loading import default
|
||||
from watcher.decision_engine.loading import default
|
||||
from watcher.decision_engine.strategy.selection import base
|
||||
|
||||
LOG = log.getLogger(__name__)
|
||||
|
||||
@@ -42,6 +42,7 @@ import six
|
||||
from watcher._i18n import _
|
||||
from watcher.common import clients
|
||||
from watcher.common.loader import loadable
|
||||
from watcher.decision_engine.loading import default as loading
|
||||
from watcher.decision_engine.solution import default
|
||||
from watcher.decision_engine.strategy.common import level
|
||||
|
||||
@@ -93,18 +94,10 @@ class BaseStrategy(loadable.Loadable):
|
||||
raise NotImplementedError()
|
||||
|
||||
@classmethod
|
||||
@abc.abstractmethod
|
||||
def get_goal_display_name(cls):
|
||||
"""The translated display name related to the goal of the strategy"""
|
||||
raise NotImplementedError()
|
||||
|
||||
@classmethod
|
||||
@abc.abstractmethod
|
||||
def get_translatable_goal_display_name(cls):
|
||||
"""The translatable msgid related to the goal of the strategy"""
|
||||
# Note(v-francoise): Defined here to be used as the translation key for
|
||||
# other services
|
||||
raise NotImplementedError()
|
||||
def get_goal(cls):
|
||||
"""The goal the strategy achieves"""
|
||||
goal_loader = loading.DefaultGoalLoader()
|
||||
return goal_loader.load(cls.get_goal_name())
|
||||
|
||||
@classmethod
|
||||
def get_config_opts(cls):
|
||||
@@ -140,7 +133,7 @@ class BaseStrategy(loadable.Loadable):
|
||||
self._solution = s
|
||||
|
||||
@property
|
||||
def id(self):
|
||||
def name(self):
|
||||
return self._name
|
||||
|
||||
@property
|
||||
@@ -169,15 +162,7 @@ class DummyBaseStrategy(BaseStrategy):
|
||||
|
||||
@classmethod
|
||||
def get_goal_name(cls):
|
||||
return "DUMMY"
|
||||
|
||||
@classmethod
|
||||
def get_goal_display_name(cls):
|
||||
return _("Dummy goal")
|
||||
|
||||
@classmethod
|
||||
def get_translatable_goal_display_name(cls):
|
||||
return "Dummy goal"
|
||||
return "dummy"
|
||||
|
||||
|
||||
@six.add_metaclass(abc.ABCMeta)
|
||||
@@ -192,15 +177,7 @@ class UnclassifiedStrategy(BaseStrategy):
|
||||
|
||||
@classmethod
|
||||
def get_goal_name(cls):
|
||||
return "UNCLASSIFIED"
|
||||
|
||||
@classmethod
|
||||
def get_goal_display_name(cls):
|
||||
return _("Unclassified")
|
||||
|
||||
@classmethod
|
||||
def get_translatable_goal_display_name(cls):
|
||||
return "Unclassified"
|
||||
return "unclassified"
|
||||
|
||||
|
||||
@six.add_metaclass(abc.ABCMeta)
|
||||
@@ -208,15 +185,7 @@ class ServerConsolidationBaseStrategy(BaseStrategy):
|
||||
|
||||
@classmethod
|
||||
def get_goal_name(cls):
|
||||
return "SERVER_CONSOLIDATION"
|
||||
|
||||
@classmethod
|
||||
def get_goal_display_name(cls):
|
||||
return _("Server consolidation")
|
||||
|
||||
@classmethod
|
||||
def get_translatable_goal_display_name(cls):
|
||||
return "Server consolidation"
|
||||
return "server_consolidation"
|
||||
|
||||
|
||||
@six.add_metaclass(abc.ABCMeta)
|
||||
@@ -224,15 +193,7 @@ class ThermalOptimizationBaseStrategy(BaseStrategy):
|
||||
|
||||
@classmethod
|
||||
def get_goal_name(cls):
|
||||
return "THERMAL_OPTIMIZATION"
|
||||
|
||||
@classmethod
|
||||
def get_goal_display_name(cls):
|
||||
return _("Thermal optimization")
|
||||
|
||||
@classmethod
|
||||
def get_translatable_goal_display_name(cls):
|
||||
return "Thermal optimization"
|
||||
return "thermal_optimization"
|
||||
|
||||
|
||||
@six.add_metaclass(abc.ABCMeta)
|
||||
@@ -240,7 +201,7 @@ class WorkloadStabilizationBaseStrategy(BaseStrategy):
|
||||
|
||||
@classmethod
|
||||
def get_goal_name(cls):
|
||||
return "WORKLOAD_BALANCING"
|
||||
return "workload_balancing"
|
||||
|
||||
@classmethod
|
||||
def get_goal_display_name(cls):
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
#
|
||||
from oslo_log import log
|
||||
|
||||
from watcher._i18n import _LE, _LI, _LW
|
||||
from watcher._i18n import _, _LE, _LI, _LW
|
||||
from watcher.common import exception as wexc
|
||||
from watcher.decision_engine.model import resource
|
||||
from watcher.decision_engine.model import vm_state
|
||||
|
||||
@@ -123,15 +123,15 @@ class WorkloadStabilization(base.WorkloadStabilizationBaseStrategy):
|
||||
|
||||
@classmethod
|
||||
def get_name(cls):
|
||||
return "WORKLOAD_BALANCING"
|
||||
return "workload_stabilization"
|
||||
|
||||
@classmethod
|
||||
def get_display_name(cls):
|
||||
return _("Workload balancing")
|
||||
return _("Workload stabilization")
|
||||
|
||||
@classmethod
|
||||
def get_translatable_display_name(cls):
|
||||
return "Workload balancing"
|
||||
return "Workload stabilization"
|
||||
|
||||
@property
|
||||
def ceilometer(self):
|
||||
|
||||
@@ -20,15 +20,19 @@ from oslo_log import log
|
||||
|
||||
from watcher._i18n import _LE, _LI
|
||||
from watcher.common import context
|
||||
from watcher.decision_engine.strategy.loading import default
|
||||
from watcher.decision_engine.loading import default
|
||||
from watcher import objects
|
||||
|
||||
LOG = log.getLogger(__name__)
|
||||
|
||||
GoalMapping = collections.namedtuple('GoalMapping', ['name', 'display_name'])
|
||||
GoalMapping = collections.namedtuple(
|
||||
'GoalMapping', ['name', 'display_name'])
|
||||
StrategyMapping = collections.namedtuple(
|
||||
'StrategyMapping', ['name', 'goal_name', 'display_name'])
|
||||
|
||||
IndicatorSpec = collections.namedtuple(
|
||||
'IndicatorSpec', ['name', 'description', 'unit', 'schema'])
|
||||
|
||||
|
||||
class Syncer(object):
|
||||
"""Syncs all available goals and strategies with the Watcher DB"""
|
||||
@@ -52,22 +56,26 @@ class Syncer(object):
|
||||
|
||||
@property
|
||||
def available_goals(self):
|
||||
"""Goals loaded from DB"""
|
||||
if self._available_goals is None:
|
||||
self._available_goals = objects.Goal.list(self.ctx)
|
||||
return self._available_goals
|
||||
|
||||
@property
|
||||
def available_strategies(self):
|
||||
"""Strategies loaded from DB"""
|
||||
if self._available_strategies is None:
|
||||
self._available_strategies = objects.Strategy.list(self.ctx)
|
||||
return self._available_strategies
|
||||
|
||||
@property
|
||||
def available_goals_map(self):
|
||||
"""Mapping of goals loaded from DB"""
|
||||
if self._available_goals_map is None:
|
||||
self._available_goals_map = {
|
||||
GoalMapping(
|
||||
name=g.name, display_name=g.display_name): g
|
||||
name=g.name,
|
||||
display_name=g.display_name): g
|
||||
for g in self.available_goals
|
||||
}
|
||||
return self._available_goals_map
|
||||
@@ -109,9 +117,7 @@ class Syncer(object):
|
||||
|
||||
def _sync_goal(self, goal_map):
|
||||
goal_name = goal_map.name
|
||||
goal_display_name = goal_map.display_name
|
||||
goal_mapping = dict()
|
||||
|
||||
# Goals that are matching by name with the given discovered goal name
|
||||
matching_goals = [g for g in self.available_goals
|
||||
if g.name == goal_name]
|
||||
@@ -120,7 +126,7 @@ class Syncer(object):
|
||||
if stale_goals or not matching_goals:
|
||||
goal = objects.Goal(self.ctx)
|
||||
goal.name = goal_name
|
||||
goal.display_name = goal_display_name
|
||||
goal.display_name = goal_map.display_name
|
||||
goal.create()
|
||||
LOG.info(_LI("Goal %s created"), goal_name)
|
||||
|
||||
@@ -134,7 +140,6 @@ class Syncer(object):
|
||||
|
||||
def _sync_strategy(self, strategy_map):
|
||||
strategy_name = strategy_map.name
|
||||
strategy_display_name = strategy_map.display_name
|
||||
goal_name = strategy_map.goal_name
|
||||
strategy_mapping = dict()
|
||||
|
||||
@@ -148,7 +153,7 @@ class Syncer(object):
|
||||
if stale_strategies or not matching_strategies:
|
||||
strategy = objects.Strategy(self.ctx)
|
||||
strategy.name = strategy_name
|
||||
strategy.display_name = strategy_display_name
|
||||
strategy.display_name = strategy_map.display_name
|
||||
strategy.goal_id = objects.Goal.get_by_name(self.ctx, goal_name).id
|
||||
strategy.create()
|
||||
LOG.info(_LI("Strategy %s created"), strategy_name)
|
||||
@@ -253,14 +258,18 @@ class Syncer(object):
|
||||
strategies_map = {}
|
||||
goals_map = {}
|
||||
discovered_map = {"goals": goals_map, "strategies": strategies_map}
|
||||
goal_loader = default.DefaultGoalLoader()
|
||||
implemented_goals = goal_loader.list_available()
|
||||
|
||||
strategy_loader = default.DefaultStrategyLoader()
|
||||
implemented_strategies = strategy_loader.list_available()
|
||||
|
||||
for _, strategy_cls in implemented_strategies.items():
|
||||
goals_map[strategy_cls.get_goal_name()] = GoalMapping(
|
||||
name=strategy_cls.get_goal_name(),
|
||||
display_name=strategy_cls.get_translatable_goal_display_name())
|
||||
for _, goal_cls in implemented_goals.items():
|
||||
goals_map[goal_cls.get_name()] = GoalMapping(
|
||||
name=goal_cls.get_name(),
|
||||
display_name=goal_cls.get_translatable_display_name())
|
||||
|
||||
for _, strategy_cls in implemented_strategies.items():
|
||||
strategies_map[strategy_cls.get_name()] = StrategyMapping(
|
||||
name=strategy_cls.get_name(),
|
||||
goal_name=strategy_cls.get_goal_name(),
|
||||
@@ -269,13 +278,21 @@ class Syncer(object):
|
||||
return discovered_map
|
||||
|
||||
def _soft_delete_stale_goals(self, goal_map, matching_goals):
|
||||
goal_name = goal_map.name
|
||||
"""Soft delete the stale goals
|
||||
|
||||
:param goal_map: discovered goal map
|
||||
:type goal_map: :py:class:`~.GoalMapping` instance
|
||||
:param matching_goals: list of DB goals matching the goal_map
|
||||
:type matching_goals: list of :py:class:`~.objects.Goal` instances
|
||||
:returns: A list of soft deleted DB goals (subset of matching goals)
|
||||
:rtype: list of :py:class:`~.objects.Goal` instances
|
||||
"""
|
||||
goal_display_name = goal_map.display_name
|
||||
goal_name = goal_map.name
|
||||
|
||||
stale_goals = []
|
||||
for matching_goal in matching_goals:
|
||||
if (matching_goal.display_name == goal_display_name and
|
||||
matching_goal.strategy_id not in self.strategy_mapping):
|
||||
if matching_goal.display_name == goal_display_name:
|
||||
LOG.info(_LI("Goal %s unchanged"), goal_name)
|
||||
else:
|
||||
LOG.info(_LI("Goal %s modified"), goal_name)
|
||||
|
||||
Reference in New Issue
Block a user