From 1997de3b77ea521bb1b57d067a0076593e93b7c9 Mon Sep 17 00:00:00 2001 From: Surya Seetharaman Date: Wed, 22 Aug 2018 14:02:59 +0200 Subject: [PATCH] Add scatter-gather-single-cell utility This patch adds the method to query a single cell using the scatter-gather utility. Although we do have the target_cell function to target a specific cell, in situations where we need to handle a down cell (like for nova show), it is advantageous to have this wrapper which in turn only calls the scatter-gather-cells utility. Change-Id: I31f2ab499aed795812a99b116cec3663760297e3 --- nova/context.py | 23 +++++++++++++++++++++++ nova/tests/unit/test_context.py | 16 ++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/nova/context.py b/nova/context.py index 0b345ab829f1..59830ff3dc1e 100644 --- a/nova/context.py +++ b/nova/context.py @@ -512,6 +512,29 @@ def scatter_gather_skip_cell0(context, fn, *args, **kwargs): fn, *args, **kwargs) +def scatter_gather_single_cell(context, cell_mapping, fn, *args, **kwargs): + """Target the provided cell and return its results or sentinels in case of + failure. + + The first parameter in the signature of the function to call for each cell + should be of type RequestContext. + + :param context: The RequestContext for querying cells + :param cell_mapping: The CellMapping to target + :param fn: The function to call for each cell + :param args: The args for the function to call for each cell, not including + the RequestContext + :param kwargs: The kwargs for the function to call for this cell + :returns: A dict {cell_uuid: result} containing the joined results. The + did_not_respond_sentinel will be returned if the cell did not + respond within the timeout. The raised_exception_sentinel will + be returned if the call to the cell raised an exception. The + exception will be logged. + """ + return scatter_gather_cells(context, [cell_mapping], CELL_TIMEOUT, fn, + *args, **kwargs) + + def scatter_gather_all_cells(context, fn, *args, **kwargs): """Target all cells in parallel and return their results. diff --git a/nova/tests/unit/test_context.py b/nova/tests/unit/test_context.py index c3984462e39e..ac40f9a7cf66 100644 --- a/nova/tests/unit/test_context.py +++ b/nova/tests/unit/test_context.py @@ -466,3 +466,19 @@ class ContextTestCase(test.NoDBTestCase): mock_scatter.assert_called_once_with( ctxt, [mapping1], 60, objects.InstanceList.get_by_filters, filters, sort_dir='foo') + + @mock.patch('nova.context.scatter_gather_cells') + def test_scatter_gather_single_cell(self, mock_scatter): + ctxt = context.get_context() + mapping0 = objects.CellMapping(database_connection='fake://db0', + transport_url='none:///', + uuid=objects.CellMapping.CELL0_UUID) + + filters = {'deleted': False} + context.scatter_gather_single_cell(ctxt, mapping0, + objects.InstanceList.get_by_filters, filters, sort_dir='foo') + + mock_scatter.assert_called_once_with( + ctxt, [mapping0], context.CELL_TIMEOUT, + objects.InstanceList.get_by_filters, filters, + sort_dir='foo')