Set request context when polling vCenter tasks

There are various useful log messages which come when polling vCenter
tasks which are missing request context and thus are hard to correlate
to API requests.
This patch fixes this by passing the request context to the async
jobs which poll the vCenter tasks.

Change-Id: I8efbc343e5073d3deb18b5f87c378c271ea4d816
This commit is contained in:
Radoslav Gerganov 2018-08-07 12:26:34 +03:00
parent a2bcf80fbc
commit 44396c93b4
5 changed files with 25 additions and 6 deletions

View File

@ -29,6 +29,7 @@ netifaces==0.10.4
openstackdocstheme==1.18.1
oslo.concurrency==3.26.0
oslo.config==5.2.0
oslo.context==2.19.2
oslo.i18n==3.15.3
oslo.utils==3.33.0
pbr==2.0.0

View File

@ -24,6 +24,7 @@ in case of connection problems or server API call overload.
import logging
from oslo_concurrency import lockutils
from oslo_context import context
from oslo_utils import excutils
from oslo_utils import reflection
import six
@ -390,12 +391,13 @@ class VMwareAPISession(object):
:raises: VimException, VimFaultException, VimAttributeException,
VimSessionOverLoadException, VimConnectionException
"""
loop = loopingcall.FixedIntervalLoopingCall(self._poll_task, task)
ctx = context.get_current()
loop = loopingcall.FixedIntervalLoopingCall(self._poll_task, task, ctx)
evt = loop.start(self._task_poll_interval)
LOG.debug("Waiting for the task: %s to complete.", task)
return evt.wait()
def _poll_task(self, task):
def _poll_task(self, task, ctx):
"""Poll the given task until completion.
If the task completes successfully, the method returns the task info
@ -403,7 +405,9 @@ class VMwareAPISession(object):
exception is set in the event.
:param task: managed object reference of the task
:param ctx: request context for the corresponding task
"""
ctx.update_store()
try:
# we poll tasks too often, so skip logging the opID as it generates
# too much noise in the logs

View File

@ -24,6 +24,7 @@ import mock
import six
import suds
from oslo_context import context
from oslo_vmware import api
from oslo_vmware import exceptions
from oslo_vmware import pbm
@ -393,7 +394,10 @@ class VMwareAPISessionTest(base.TestCase):
'api')
self.assertEqual(fault_list, ex.fault_list)
def test_wait_for_task(self):
@mock.patch.object(context, 'get_current')
def test_wait_for_task(self, mock_curr_ctx):
ctx = mock.Mock()
mock_curr_ctx.return_value = ctx
api_session = self._create_api_session(True)
task_info_list = [('queued', 0), ('running', 40), ('success', 100)]
task_info_list_size = len(task_info_list)
@ -420,8 +424,11 @@ class VMwareAPISessionTest(base.TestCase):
skip_op_id=True)
self.assertEqual(task_info_list_size,
api_session.invoke_api.call_count)
mock_curr_ctx.assert_called_once()
self.assertEqual(3, ctx.update_store.call_count)
def test_wait_for_task_with_error_state(self):
@mock.patch.object(context, 'get_current')
def test_wait_for_task_with_error_state(self, mock_curr_ctx):
api_session = self._create_api_session(True)
task_info_list = [('queued', 0), ('running', 40), ('error', -1)]
task_info_list_size = len(task_info_list)
@ -446,8 +453,10 @@ class VMwareAPISessionTest(base.TestCase):
skip_op_id=True)
self.assertEqual(task_info_list_size,
api_session.invoke_api.call_count)
mock_curr_ctx.assert_called_once()
def test_wait_for_task_with_invoke_api_exception(self):
@mock.patch.object(context, 'get_current')
def test_wait_for_task_with_invoke_api_exception(self, mock_curr_ctx):
api_session = self._create_api_session(True)
api_session.invoke_api = mock.Mock(
side_effect=exceptions.VimException(None))
@ -461,6 +470,7 @@ class VMwareAPISessionTest(base.TestCase):
api_session.vim, task,
'info',
skip_op_id=True)
mock_curr_ctx.assert_called_once()
def test_wait_for_lease_ready(self):
api_session = self._create_api_session(True)
@ -551,9 +561,11 @@ class VMwareAPISessionTest(base.TestCase):
):
fake_task = mock.Mock()
fake_task.value = 'task-1'
ctx = mock.Mock()
self.assertRaises(expected_exception,
api_session._poll_task,
fake_task)
fake_task,
ctx)
def test_poll_task_well_known_exceptions(self):
for k, v in six.iteritems(exceptions._fault_classes_registry):

View File

@ -20,3 +20,4 @@ eventlet!=0.18.3,!=0.20.1,>=0.18.2 # MIT
requests>=2.14.2 # Apache-2.0
urllib3>=1.21.1 # MIT
oslo.concurrency>=3.26.0 # Apache-2.0
oslo.context>=2.19.2 # Apache-2.0

View File

@ -22,3 +22,4 @@ reno>=2.5.0 # Apache-2.0
bandit>=1.1.0 # Apache-2.0
ddt>=1.0.1 # MIT
oslo.context>=2.19.2 # Apache-2.0