Move some functions to service_utils

This moves some utility functions to service_utils
from stack_lock.

Change-Id: Iebc39a7f7b15dd5eea4be3cd3fdaea22607dd72e
This commit is contained in:
Rabi Mishra 2016-05-11 09:55:17 +05:30
parent 2c05a61328
commit ef4cf621fb
7 changed files with 35 additions and 28 deletions

View File

@ -13,8 +13,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import uuid
from oslo_utils import timeutils
from heat.rpc import listener_client
SERVICE_KEYS = (
SERVICE_ID,
SERVICE_HOST,
@ -70,3 +74,12 @@ def format_service(service):
SERVICE_STATUS: status
}
return result
def engine_alive(context, engine_id):
return listener_client.EngineListenerClient(
engine_id).is_alive(context)
def generate_engine_id():
return str(uuid.uuid4())

View File

@ -360,7 +360,7 @@ class EngineService(service.Service):
self.manage_thread_grp.add_thread(create_watch_tasks)
def start(self):
self.engine_id = stack_lock.StackLock.generate_engine_id()
self.engine_id = service_utils.generate_engine_id()
if self.thread_group_mgr is None:
self.thread_group_mgr = ThreadGroupManager()
self.listener = EngineListener(self.host, self.engine_id,
@ -1145,7 +1145,7 @@ class EngineService(service.Service):
self.thread_group_mgr.send(current_stack.id, cancel_message)
# Another active engine has the lock
elif stack_lock.StackLock.engine_alive(cnxt, engine_id):
elif service_utils.engine_alive(cnxt, engine_id):
cancel_result = self._remote_call(
cnxt, engine_id, self.listener.SEND,
stack_identity=stack_identity, message=cancel_message)
@ -1375,7 +1375,7 @@ class EngineService(service.Service):
self.thread_group_mgr.stop(stack.id)
# Another active engine has the lock
elif stack_lock.StackLock.engine_alive(cnxt, acquire_result):
elif service_utils.engine_alive(cnxt, acquire_result):
stop_result = self._remote_call(
cnxt, acquire_result, self.listener.STOP_STACK,
stack_identity=stack_identity)

View File

@ -12,7 +12,6 @@
# under the License.
import contextlib
import uuid
from oslo_log import log as logging
from oslo_utils import excutils
@ -20,9 +19,10 @@ from oslo_utils import excutils
from heat.common import exception
from heat.common.i18n import _LI
from heat.common.i18n import _LW
from heat.common import service_utils
from heat.objects import stack as stack_object
from heat.objects import stack_lock as stack_lock_object
from heat.rpc import listener_client
LOG = logging.getLogger(__name__)
@ -34,15 +34,6 @@ class StackLock(object):
self.engine_id = engine_id
self.listener = None
@staticmethod
def engine_alive(context, engine_id):
return listener_client.EngineListenerClient(
engine_id).is_alive(context)
@staticmethod
def generate_engine_id():
return str(uuid.uuid4())
def get_engine_id(self):
return stack_lock_object.StackLock.get_engine_id(self.stack_id)
@ -72,7 +63,7 @@ class StackLock(object):
tenant_safe=False,
show_deleted=True)
if (lock_engine_id == self.engine_id or
self.engine_alive(self.context, lock_engine_id)):
service_utils.engine_alive(self.context, lock_engine_id)):
LOG.debug("Lock on stack %(stack)s is owned by engine "
"%(engine)s" % {'stack': self.stack_id,
'engine': lock_engine_id})

View File

@ -222,7 +222,7 @@ class ServiceEngineTest(common.HeatTestCase):
return_value=mock.Mock())
@mock.patch('heat.common.messaging.get_rpc_client',
return_value=mock.Mock())
@mock.patch('heat.engine.stack_lock.StackLock.generate_engine_id',
@mock.patch('heat.common.service_utils.generate_engine_id',
return_value='sample-uuid')
@mock.patch('heat.engine.service.ThreadGroupManager',
return_value=mock.Mock())
@ -259,7 +259,7 @@ class ServiceEngineTest(common.HeatTestCase):
return_value=mock.Mock())
@mock.patch('heat.common.messaging.get_rpc_client',
return_value=mock.Mock())
@mock.patch('heat.engine.stack_lock.StackLock.generate_engine_id',
@mock.patch('heat.common.service_utils.generate_engine_id',
return_value=mock.Mock())
@mock.patch('heat.engine.service.ThreadGroupManager',
return_value=mock.Mock())
@ -389,7 +389,7 @@ class ServiceEngineTest(common.HeatTestCase):
return_value=mock.Mock())
@mock.patch('heat.common.messaging.get_rpc_client',
return_value=mock.Mock())
@mock.patch('heat.engine.stack_lock.StackLock.generate_engine_id',
@mock.patch('heat.common.service_utils.generate_engine_id',
return_value=mock.Mock())
@mock.patch('heat.engine.service.ThreadGroupManager',
return_value=mock.Mock())

View File

@ -14,6 +14,7 @@ import mock
from oslo_messaging.rpc import dispatcher
from heat.common import exception
from heat.common import service_utils
from heat.engine import service
from heat.engine import stack as parser
from heat.engine import stack_lock
@ -124,7 +125,7 @@ class StackDeleteTest(common.HeatTestCase):
@mock.patch.object(parser.Stack, 'load')
@mock.patch.object(stack_lock.StackLock, 'try_acquire')
@mock.patch.object(stack_lock.StackLock, 'engine_alive')
@mock.patch.object(service_utils, 'engine_alive')
def test_stack_delete_other_engine_active_lock_failed(self, mock_alive,
mock_try, mock_load):
OTHER_ENGINE = "other-engine-fake-uuid"
@ -157,7 +158,7 @@ class StackDeleteTest(common.HeatTestCase):
@mock.patch.object(parser.Stack, 'load')
@mock.patch.object(stack_lock.StackLock, 'try_acquire')
@mock.patch.object(stack_lock.StackLock, 'engine_alive')
@mock.patch.object(service_utils, 'engine_alive')
@mock.patch.object(stack_lock.StackLock, 'acquire')
def test_stack_delete_other_engine_active_lock_succeeded(
self, mock_acquire, mock_alive, mock_try, mock_load):
@ -191,7 +192,7 @@ class StackDeleteTest(common.HeatTestCase):
@mock.patch.object(parser.Stack, 'load')
@mock.patch.object(stack_lock.StackLock, 'try_acquire')
@mock.patch.object(stack_lock.StackLock, 'engine_alive')
@mock.patch.object(service_utils, 'engine_alive')
@mock.patch.object(stack_lock.StackLock, 'acquire')
def test_stack_delete_other_dead_engine_active_lock(
self, mock_acquire, mock_alive, mock_try, mock_load):

View File

@ -20,6 +20,7 @@ import six
from heat.common import exception
from heat.common import messaging
from heat.common import service_utils
from heat.common import template_format
from heat.engine.clients.os import glance
from heat.engine.clients.os import nova
@ -438,7 +439,7 @@ class ServiceStackUpdateTest(common.HeatTestCase):
self.patchobject(stack.Stack, 'load', return_value=stk)
self.patchobject(stack_lock.StackLock, 'try_acquire',
return_value=str(uuid.uuid4()))
self.patchobject(stack_lock.StackLock, 'engine_alive',
self.patchobject(service_utils, 'engine_alive',
return_value=True)
self.man.listener = mock.Mock()
self.man.listener.SEND = 'send'

View File

@ -14,6 +14,7 @@
import mock
from heat.common import exception
from heat.common import service_utils
from heat.engine import stack_lock
from heat.objects import stack as stack_object
from heat.objects import stack_lock as stack_lock_object
@ -26,7 +27,7 @@ class StackLockTest(common.HeatTestCase):
super(StackLockTest, self).setUp()
self.context = utils.dummy_context()
self.stack_id = "aae01f2d-52ae-47ac-8a0d-3fde3d220fea"
self.engine_id = stack_lock.StackLock.generate_engine_id()
self.engine_id = service_utils.generate_engine_id()
stack = mock.MagicMock()
stack.id = self.stack_id
stack.name = "test_stack"
@ -74,7 +75,7 @@ class StackLockTest(common.HeatTestCase):
slock = stack_lock.StackLock(self.context, self.stack_id,
self.engine_id)
self.patchobject(slock, 'engine_alive', return_value=False)
self.patchobject(service_utils, 'engine_alive', return_value=False)
slock.acquire()
mock_create.assert_called_once_with(self.stack_id, self.engine_id)
@ -88,7 +89,7 @@ class StackLockTest(common.HeatTestCase):
slock = stack_lock.StackLock(self.context, self.stack_id,
self.engine_id)
self.patchobject(slock, 'engine_alive', return_value=True)
self.patchobject(service_utils, 'engine_alive', return_value=True)
self.assertRaises(exception.ActionInProgress, slock.acquire)
self.mock_get_by_id.assert_called_once_with(
self.context,
@ -108,7 +109,7 @@ class StackLockTest(common.HeatTestCase):
slock = stack_lock.StackLock(self.context, self.stack_id,
self.engine_id)
self.patchobject(slock, 'engine_alive', return_value=False)
self.patchobject(service_utils, 'engine_alive', return_value=False)
self.assertRaises(exception.ActionInProgress, slock.acquire)
self.mock_get_by_id.assert_called_once_with(
self.context,
@ -130,7 +131,7 @@ class StackLockTest(common.HeatTestCase):
slock = stack_lock.StackLock(self.context, self.stack_id,
self.engine_id)
self.patchobject(slock, 'engine_alive', return_value=False)
self.patchobject(service_utils, 'engine_alive', return_value=False)
slock.acquire()
mock_create.assert_has_calls(
@ -148,7 +149,7 @@ class StackLockTest(common.HeatTestCase):
slock = stack_lock.StackLock(self.context, self.stack_id,
self.engine_id)
self.patchobject(slock, 'engine_alive', return_value=False)
self.patchobject(service_utils, 'engine_alive', return_value=False)
self.assertRaises(exception.ActionInProgress, slock.acquire)
self.mock_get_by_id.assert_called_with(
self.context,