220 lines
7.5 KiB
Python
220 lines
7.5 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
|
|
|
# Copyright (C) 2012 Yahoo! Inc. All Rights Reserved.
|
|
#
|
|
# 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 threading
|
|
|
|
from concurrent import futures
|
|
|
|
from taskflow.engines.action_engine import graph_action
|
|
from taskflow.engines.action_engine import task_action
|
|
from taskflow.engines import base
|
|
|
|
from taskflow import exceptions as exc
|
|
from taskflow.openstack.common import excutils
|
|
from taskflow.openstack.common import uuidutils
|
|
from taskflow import states
|
|
from taskflow import storage as t_storage
|
|
|
|
from taskflow.utils import flow_utils
|
|
from taskflow.utils import lock_utils
|
|
from taskflow.utils import misc
|
|
from taskflow.utils import reflection
|
|
from taskflow.utils import threading_utils
|
|
|
|
|
|
class ActionEngine(base.EngineBase):
|
|
"""Generic action-based engine.
|
|
|
|
Converts the flow to recursive structure of actions.
|
|
"""
|
|
_graph_action = None
|
|
|
|
def __init__(self, flow, flow_detail, backend, conf):
|
|
super(ActionEngine, self).__init__(flow, flow_detail, backend, conf)
|
|
self._failures = {} # task uuid => failure
|
|
self._root = None
|
|
self._lock = threading.RLock()
|
|
self._state_lock = threading.RLock()
|
|
self.notifier = misc.TransitionNotifier()
|
|
self.task_notifier = misc.TransitionNotifier()
|
|
|
|
def _revert(self, current_failure=None):
|
|
self._change_state(states.REVERTING)
|
|
try:
|
|
state = self._root.revert(self)
|
|
except Exception:
|
|
with excutils.save_and_reraise_exception():
|
|
self._change_state(states.FAILURE)
|
|
|
|
self._change_state(state)
|
|
if state == states.SUSPENDED:
|
|
return
|
|
misc.Failure.reraise_if_any(self._failures.values())
|
|
if current_failure:
|
|
current_failure.reraise()
|
|
|
|
def __str__(self):
|
|
return "%s: %s" % (reflection.get_class_name(self), id(self))
|
|
|
|
def suspend(self):
|
|
self._change_state(states.SUSPENDING)
|
|
|
|
def get_graph(self):
|
|
self.compile()
|
|
return self._root.graph
|
|
|
|
@lock_utils.locked
|
|
def run(self):
|
|
self.compile()
|
|
external_provides = set(self.storage.fetch_all().keys())
|
|
missing = self._flow.requires - external_provides
|
|
if missing:
|
|
raise exc.MissingDependencies(self._flow, sorted(missing))
|
|
|
|
if self._failures:
|
|
self._revert()
|
|
else:
|
|
self._run()
|
|
|
|
def _run(self):
|
|
self._change_state(states.RUNNING)
|
|
try:
|
|
state = self._root.execute(self)
|
|
except Exception:
|
|
self._change_state(states.FAILURE)
|
|
self._revert(misc.Failure())
|
|
else:
|
|
self._change_state(state)
|
|
|
|
@lock_utils.locked(lock='_state_lock')
|
|
def _change_state(self, state):
|
|
old_state = self.storage.get_flow_state()
|
|
if not states.check_flow_transition(old_state, state):
|
|
return
|
|
self.storage.set_flow_state(state)
|
|
try:
|
|
flow_uuid = self._flow.uuid
|
|
except AttributeError:
|
|
# NOTE(harlowja): if the flow was just a single task, then it will
|
|
# not itself have a uuid, but the constructed flow_detail will.
|
|
if self._flow_detail is not None:
|
|
flow_uuid = self._flow_detail.uuid
|
|
else:
|
|
flow_uuid = None
|
|
details = dict(engine=self,
|
|
flow_name=self._flow.name,
|
|
flow_uuid=flow_uuid,
|
|
old_state=old_state)
|
|
self.notifier.notify(state, details)
|
|
|
|
def on_task_state_change(self, task_action, state, result=None):
|
|
if isinstance(result, misc.Failure):
|
|
self._failures[task_action.uuid] = result
|
|
details = dict(engine=self,
|
|
task_name=task_action.name,
|
|
task_uuid=task_action.uuid,
|
|
result=result)
|
|
self.task_notifier.notify(state, details)
|
|
|
|
def compile(self):
|
|
if self._root is not None:
|
|
return
|
|
|
|
assert self._graph_action is not None, ('Graph action class must be'
|
|
' specified')
|
|
self._change_state(states.RESUMING) # does nothing in PENDING state
|
|
task_graph = flow_utils.flatten(self._flow)
|
|
self._root = self._graph_action(task_graph)
|
|
loaded_failures = {}
|
|
|
|
for task in task_graph.nodes_iter():
|
|
try:
|
|
task_id = self.storage.get_uuid_by_name(task.name)
|
|
except exc.NotFound:
|
|
task_id = uuidutils.generate_uuid()
|
|
task_version = misc.get_version_string(task)
|
|
self.storage.add_task(task_name=task.name, uuid=task_id,
|
|
task_version=task_version)
|
|
try:
|
|
result = self.storage.get(task_id)
|
|
except exc.NotFound:
|
|
result = None
|
|
|
|
if isinstance(result, misc.Failure):
|
|
# NOTE(imelnikov): old failure may have exc_info which
|
|
# might get lost during serialization, so we preserve
|
|
# old failure object if possible.
|
|
old_failure = self._failures.get(task_id, None)
|
|
if result.matches(old_failure):
|
|
loaded_failures[task_id] = old_failure
|
|
else:
|
|
loaded_failures[task_id] = result
|
|
|
|
self.storage.set_result_mapping(task_id, task.save_as)
|
|
self._root.add(task, task_action.TaskAction(task, task_id))
|
|
self._failures = loaded_failures
|
|
self._change_state(states.SUSPENDED) # does nothing in PENDING state
|
|
|
|
@property
|
|
def is_running(self):
|
|
return self.storage.get_flow_state() == states.RUNNING
|
|
|
|
@property
|
|
def is_reverting(self):
|
|
return self.storage.get_flow_state() == states.REVERTING
|
|
|
|
|
|
class SingleThreadedActionEngine(ActionEngine):
|
|
# This one attempts to run in a serial manner.
|
|
_graph_action = graph_action.SequentialGraphAction
|
|
_storage_cls = t_storage.Storage
|
|
|
|
|
|
class MultiThreadedActionEngine(ActionEngine):
|
|
# This one attempts to run in a parallel manner.
|
|
_graph_action = graph_action.ParallelGraphAction
|
|
_storage_cls = t_storage.ThreadSafeStorage
|
|
|
|
def __init__(self, flow, flow_detail, backend, conf):
|
|
super(MultiThreadedActionEngine, self).__init__(
|
|
flow, flow_detail, backend, conf)
|
|
self._executor = conf.get('executor', None)
|
|
|
|
@lock_utils.locked
|
|
def run(self):
|
|
if self._executor is None:
|
|
self._executor = futures.ThreadPoolExecutor(
|
|
threading_utils.get_optimal_thread_count())
|
|
owns_executor = True
|
|
else:
|
|
owns_executor = False
|
|
|
|
try:
|
|
ActionEngine.run(self)
|
|
finally:
|
|
# Don't forget to shutdown the executor!!
|
|
if owns_executor:
|
|
try:
|
|
self._executor.shutdown(wait=True)
|
|
finally:
|
|
self._executor = None
|
|
|
|
@property
|
|
def executor(self):
|
|
return self._executor
|