Merge "Update HA router state if agent is not active"

This commit is contained in:
Jenkins 2016-01-24 18:35:14 +00:00 committed by Gerrit Code Review
commit 871efe51e5
2 changed files with 38 additions and 1 deletions

View File

@ -549,10 +549,32 @@ class L3_HA_NAT_db_mixin(l3_dvr_db.L3_NAT_with_dvr_db_mixin,
return query.all()
def _get_bindings_and_update_router_state_for_dead_agents(self, context,
router_id):
"""Return bindings. In case if dead agents were detected update router
states on this agent.
"""
with context.session.begin(subtransactions=True):
bindings = self.get_ha_router_port_bindings(context, [router_id])
dead_agents = [
binding.agent for binding in bindings
if binding.state == constants.HA_ROUTER_STATE_ACTIVE and
not binding.agent.is_active]
for dead_agent in dead_agents:
self.update_routers_states(
context, {router_id: constants.HA_ROUTER_STATE_STANDBY},
dead_agent.host)
if dead_agents:
return self.get_ha_router_port_bindings(context, [router_id])
return bindings
def get_l3_bindings_hosting_router_with_ha_states(
self, context, router_id):
"""Return a list of [(agent, ha_state), ...]."""
bindings = self.get_ha_router_port_bindings(context, [router_id])
bindings = self._get_bindings_and_update_router_state_for_dead_agents(
context, router_id)
return [(binding.agent, binding.state) for binding in bindings
if binding.agent is not None]

View File

@ -189,6 +189,21 @@ class L3HATestCase(L3HATestFramework):
self.admin_ctx, router['id'])
self.assertEqual([], bindings)
def test_get_l3_bindings_hosting_router_with_ha_states_active_and_dead(
self):
router = self._create_router()
self._bind_router(router['id'])
with mock.patch.object(agents_db.Agent, 'is_active',
new_callable=mock.PropertyMock,
return_value=False):
self.plugin.update_routers_states(
self.admin_ctx, {router['id']: 'active'}, self.agent1['host'])
bindings = (
self.plugin.get_l3_bindings_hosting_router_with_ha_states(
self.admin_ctx, router['id']))
agent_ids = [(agent[0]['id'], agent[1]) for agent in bindings]
self.assertIn((self.agent1['id'], 'standby'), agent_ids)
def test_ha_router_create(self):
router = self._create_router()
self.assertTrue(router['ha'])