From 64d32be67d4ff2872698ec5e55147871c41b5a07 Mon Sep 17 00:00:00 2001 From: linb Date: Wed, 18 May 2016 14:04:11 +0800 Subject: [PATCH] Enhance getting valid router bindings When we get all router ids bound to edge, we now just search in nsxv_router_bindings. But sometime there maybe inconsistency and we have ghost router bindings left whose router db has actually been deleted. Following shared router backend ops would unexpectly break with these ghost router ids, worse itself also become a ghost after router db is deleted. The patch avoids it happend by double getting router ids also from Router db. Change-Id: If111ab8355102120d509c91e547fcde4cd309f16 --- vmware_nsx/plugins/nsx_v/vshield/edge_utils.py | 18 +++++++++++++++--- vmware_nsx/tests/unit/nsx_v/test_plugin.py | 6 +++--- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/vmware_nsx/plugins/nsx_v/vshield/edge_utils.py b/vmware_nsx/plugins/nsx_v/vshield/edge_utils.py index b84359ca51..a13a8a35d9 100644 --- a/vmware_nsx/plugins/nsx_v/vshield/edge_utils.py +++ b/vmware_nsx/plugins/nsx_v/vshield/edge_utils.py @@ -1474,13 +1474,25 @@ class EdgeManager(object): def get_routers_on_same_edge(self, context, router_id): edge_binding = nsxv_db.get_nsxv_router_binding( context.session, router_id) + router_ids = [] + valid_router_ids = [] if edge_binding: - return [ + router_ids = [ binding['router_id'] for binding in nsxv_db.get_nsxv_router_bindings_by_edge( context.session, edge_binding['edge_id'])] - else: - return [] + if router_ids: + valid_router_ids = self.plugin.get_routers( + context.elevated(), + filters={'id': router_ids}, + fields=['id']) + valid_router_ids = [ele['id'] for ele in valid_router_ids] + + if set(valid_router_ids) != set(router_ids): + LOG.error(_LE("Get invalid router bindings with " + "router ids: %s"), + str(set(router_ids) - set(valid_router_ids))) + return valid_router_ids def bind_router_on_available_edge( self, context, target_router_id, diff --git a/vmware_nsx/tests/unit/nsx_v/test_plugin.py b/vmware_nsx/tests/unit/nsx_v/test_plugin.py index bd1b02a07b..6f69cdecdf 100644 --- a/vmware_nsx/tests/unit/nsx_v/test_plugin.py +++ b/vmware_nsx/tests/unit/nsx_v/test_plugin.py @@ -3755,12 +3755,12 @@ class TestSharedRouterTestCase(L3NatTest, L3NatTestCaseBase, get_routers_on_same_edge( context.get_admin_context(), r1['router']['id'])) - self.assertEqual(routers_expected, routers_1) + self.assertEqual(set(routers_expected), set(routers_1)) routers_2 = (self.plugin_instance.edge_manager. get_routers_on_same_edge( context.get_admin_context(), r2['router']['id'])) - self.assertEqual(routers_expected, routers_2) + self.assertEqual(set(routers_expected), set(routers_2)) self._router_interface_action('remove', r1['router']['id'], s11['subnet']['id'], @@ -3821,7 +3821,7 @@ class TestSharedRouterTestCase(L3NatTest, L3NatTestCaseBase, get_routers_on_same_edge( context.get_admin_context(), r1['router']['id'])) - self.assertEqual(r1_expected, routers_1) + self.assertEqual(set(r1_expected), set(routers_1)) self._router_interface_action('add', r2['router']['id'], s13['subnet']['id'],