Skip to content

Commit d443f8e

Browse files
Zuulopenstack-gerrit
authored andcommitted
Merge "Transport context to all threads"
2 parents 3e57422 + 646fc51 commit d443f8e

File tree

4 files changed

+37
-29
lines changed

4 files changed

+37
-29
lines changed

nova/compute/manager.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8913,7 +8913,8 @@ def live_migration(self, context, dest, instance, block_migration,
89138913
# in order to be able to track and abort it in the future.
89148914
self._waiting_live_migrations[instance.uuid] = (None, None)
89158915
try:
8916-
future = self._live_migration_executor.submit(
8916+
future = nova.utils.pass_context(
8917+
self._live_migration_executor.submit,
89178918
self._do_live_migration, context, dest, instance,
89188919
block_migration, migration, migrate_data)
89198920
self._waiting_live_migrations[instance.uuid] = (migration, future)
@@ -10197,7 +10198,9 @@ def query_driver_power_state_and_sync():
1019710198
else:
1019810199
LOG.debug('Triggering sync for uuid %s', uuid)
1019910200
self._syncs_in_progress[uuid] = True
10200-
self._sync_power_pool.spawn_n(_sync, db_instance)
10201+
nova.utils.pass_context(self._sync_power_pool.spawn_n,
10202+
_sync,
10203+
db_instance)
1020110204

1020210205
def _query_driver_power_state_and_sync(self, context, db_instance):
1020310206
if db_instance.task_state is not None:

nova/conductor/manager.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2096,8 +2096,8 @@ def skipped_host(context, host, image_ids):
20962096
skipped_host(target_ctxt, host, image_ids)
20972097
continue
20982098

2099-
fetch_pool.spawn_n(wrap_cache_images, target_ctxt, host,
2100-
image_ids)
2099+
utils.pass_context(fetch_pool.spawn_n, wrap_cache_images,
2100+
target_ctxt, host, image_ids)
21012101

21022102
# Wait until all those things finish
21032103
fetch_pool.waitall()

nova/tests/unit/compute/test_compute_mgr.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9648,9 +9648,15 @@ def test_get_serial_console(self, mock_console_obj, mock_elevated):
96489648
self.assertEqual(driver_console.get_connection_info.return_value,
96499649
console)
96509650

9651+
@mock.patch('nova.utils.pass_context')
96519652
@mock.patch('nova.compute.manager.ComputeManager.'
96529653
'_do_live_migration')
9653-
def _test_max_concurrent_live(self, mock_lm):
9654+
def _test_max_concurrent_live(self, mock_lm, mock_pass_context):
9655+
# pass_context wraps the function, which doesn't work with a mock
9656+
# So we simply mock it too
9657+
def _mock_pass_context(runner, func, *args, **kwargs):
9658+
return runner(func, *args, **kwargs)
9659+
mock_pass_context.side_effect = _mock_pass_context
96549660

96559661
@mock.patch('nova.objects.Migration.save')
96569662
def _do_it(mock_mig_save):

nova/utils.py

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -632,15 +632,13 @@ def _serialize_profile_info():
632632
return trace_info
633633

634634

635-
def spawn(func, *args, **kwargs):
636-
"""Passthrough method for eventlet.spawn.
637-
638-
This utility exists so that it can be stubbed for testing without
639-
interfering with the service spawns.
635+
def pass_context(runner, func, *args, **kwargs):
636+
"""Generalised passthrough method
640637
641-
It will also grab the context from the threadlocal store and add it to
642-
the store on the new thread. This allows for continuity in logging the
643-
context when using this method to spawn a new thread.
638+
It will grab the context from the threadlocal store and add it to
639+
the store on the runner. This allows for continuity in logging the
640+
context when using this method to spawn a new thread through the
641+
runner function
644642
"""
645643
_context = common_context.get_current()
646644
profiler_info = _serialize_profile_info()
@@ -655,11 +653,11 @@ def context_wrapper(*args, **kwargs):
655653
profiler.init(**profiler_info)
656654
return func(*args, **kwargs)
657655

658-
return eventlet.spawn(context_wrapper, *args, **kwargs)
656+
return runner(context_wrapper, *args, **kwargs)
659657

660658

661-
def spawn_n(func, *args, **kwargs):
662-
"""Passthrough method for eventlet.spawn_n.
659+
def spawn(func, *args, **kwargs):
660+
"""Passthrough method for eventlet.spawn.
663661
664662
This utility exists so that it can be stubbed for testing without
665663
interfering with the service spawns.
@@ -668,25 +666,26 @@ def spawn_n(func, *args, **kwargs):
668666
the store on the new thread. This allows for continuity in logging the
669667
context when using this method to spawn a new thread.
670668
"""
671-
_context = common_context.get_current()
672-
profiler_info = _serialize_profile_info()
673669

674-
@functools.wraps(func)
675-
def context_wrapper(*args, **kwargs):
676-
# NOTE: If update_store is not called after spawn_n it won't be
677-
# available for the logger to pull from threadlocal storage.
678-
if _context is not None:
679-
_context.update_store()
680-
if profiler_info and profiler:
681-
profiler.init(**profiler_info)
682-
func(*args, **kwargs)
670+
return pass_context(eventlet.spawn, func, *args, **kwargs)
671+
672+
673+
def spawn_n(func, *args, **kwargs):
674+
"""Passthrough method for eventlet.spawn_n.
675+
676+
This utility exists so that it can be stubbed for testing without
677+
interfering with the service spawns.
683678
684-
eventlet.spawn_n(context_wrapper, *args, **kwargs)
679+
It will also grab the context from the threadlocal store and add it to
680+
the store on the new thread. This allows for continuity in logging the
681+
context when using this method to spawn a new thread.
682+
"""
683+
pass_context(eventlet.spawn_n, func, *args, **kwargs)
685684

686685

687686
def tpool_execute(func, *args, **kwargs):
688687
"""Run func in a native thread"""
689-
tpool.execute(func, *args, **kwargs)
688+
return pass_context(tpool.execute, func, *args, **kwargs)
690689

691690

692691
def is_none_string(val):

0 commit comments

Comments
 (0)