From dbdf1b894ff898bf4909789704120056677f3796 Mon Sep 17 00:00:00 2001 From: Mark Powers Date: Mon, 17 Feb 2025 22:20:50 +0000 Subject: [PATCH] Fix host randomization Previously, this was implemented using the shuffle method of the Random class without instantiating a Random object, which did not work. This changes swaps to the random.shuffle method, which works as expected. Closes-Bug: #2099927 Change-Id: Ib288091af9cc035ccb0535fbc1748c17bb3cb1e9 --- blazar/plugins/oshosts/host_plugin.py | 6 +++--- blazar/tests/plugins/oshosts/test_physical_host_plugin.py | 6 +++--- .../notes/fix-host-randomization-bcab5276ef6199e6.yaml | 5 +++++ 3 files changed, 11 insertions(+), 6 deletions(-) create mode 100644 releasenotes/notes/fix-host-randomization-bcab5276ef6199e6.yaml diff --git a/blazar/plugins/oshosts/host_plugin.py b/blazar/plugins/oshosts/host_plugin.py index 2bf5d227..dd6c5f9b 100644 --- a/blazar/plugins/oshosts/host_plugin.py +++ b/blazar/plugins/oshosts/host_plugin.py @@ -15,7 +15,7 @@ # under the License. import datetime -from random import Random +import random import retrying from novaclient import exceptions as nova_exceptions @@ -654,12 +654,12 @@ class PhysicalHostPlugin(base.BasePlugin, nova.NovaClientWrapper): allocated_host_ids.append(host['id']) if len(not_allocated_host_ids) >= int(min_host): if CONF[self.resource_type].randomize_host_selection: - Random.shuffle(not_allocated_host_ids) + random.shuffle(not_allocated_host_ids) return not_allocated_host_ids[:int(max_host)] all_host_ids = allocated_host_ids + not_allocated_host_ids if len(all_host_ids) >= int(min_host): if CONF[self.resource_type].randomize_host_selection: - Random.shuffle(all_host_ids) + random.shuffle(all_host_ids) return all_host_ids[:int(max_host)] else: return [] diff --git a/blazar/tests/plugins/oshosts/test_physical_host_plugin.py b/blazar/tests/plugins/oshosts/test_physical_host_plugin.py index 86ebf510..18b27d2e 100644 --- a/blazar/tests/plugins/oshosts/test_physical_host_plugin.py +++ b/blazar/tests/plugins/oshosts/test_physical_host_plugin.py @@ -2431,7 +2431,7 @@ class PhysicalHostPluginTestCase(tests.TestCase): self.addCleanup(CONF.clear_override, 'cleaning_time') self.assertEqual(['host1', 'host2', 'host3'], result) - @mock.patch.object(random.Random, "shuffle") + @mock.patch.object(random, "shuffle") def test_random_matching_hosts_not_allocated_hosts(self, mock_shuffle): def host_allocation_get_all_by_values(**kwargs): if kwargs['compute_host_id'] == 'host1': @@ -2465,7 +2465,7 @@ class PhysicalHostPluginTestCase(tests.TestCase): group=plugin.RESOURCE_TYPE) mock_shuffle.assert_called_once_with(['host2', 'host3']) - @mock.patch.object(random.Random, "shuffle") + @mock.patch.object(random, "shuffle") def test_random_matching_hosts_allocated_hosts(self, mock_shuffle): def host_allocation_get_all_by_values(**kwargs): if kwargs['compute_host_id'] == 'host1': @@ -2499,7 +2499,7 @@ class PhysicalHostPluginTestCase(tests.TestCase): group=plugin.RESOURCE_TYPE) mock_shuffle.assert_called_once_with(['host1', 'host2', 'host3']) - @mock.patch.object(random.Random, "shuffle") + @mock.patch.object(random, "shuffle") def test_random_matching_hosts_allocated_cleaning_time(self, mock_shuffle): def host_allocation_get_all_by_values(**kwargs): if kwargs['compute_host_id'] == 'host1': diff --git a/releasenotes/notes/fix-host-randomization-bcab5276ef6199e6.yaml b/releasenotes/notes/fix-host-randomization-bcab5276ef6199e6.yaml new file mode 100644 index 00000000..e6ed412f --- /dev/null +++ b/releasenotes/notes/fix-host-randomization-bcab5276ef6199e6.yaml @@ -0,0 +1,5 @@ +--- +fixes: + - | + Fixes functionality of host randomization feature. + `LP#2099927 `__