def test_update_and_get_cluster_lock_second_host_no_lock(self):
     ret = ovsvapp_db.update_and_get_cluster_lock("fake_vcenter", "fake_cluster")
     self.assertEqual("1", ret)
     with mock.patch("networking_vsphere.db.ovsvapp_db." "LOG.info") as info_log:
         ret_1 = ovsvapp_db.update_and_get_cluster_lock("fake_vcenter", "fake_cluster")
     self.assertEqual("0", ret_1)
     self.assertTrue(info_log.called)
 def test_update_and_get_cluster_lock_threshold_reached(self):
     ret = ovsvapp_db.update_and_get_cluster_lock("fake_vcenter", "fake_cluster")
     self.assertEqual("1", ret)
     ovsvapp_db.set_cluster_threshold("fake_vcenter", "fake_cluster")
     with mock.patch("networking_vsphere.db.ovsvapp_db." "LOG.warn") as warn_log:
         ret_1 = ovsvapp_db.update_and_get_cluster_lock("fake_vcenter", "fake_cluster")
     self.assertEqual("-1", ret_1)
     self.assertTrue(warn_log.called)
 def test_update_and_get_cluster_lock_second_host_no_lock(self):
     ret = ovsvapp_db.update_and_get_cluster_lock('fake_vcenter',
                                                  'fake_cluster')
     self.assertEqual('1', ret)
     with mock.patch('networking_vsphere.db.ovsvapp_db.'
                     'LOG.info') as info_log:
         ret_1 = ovsvapp_db.update_and_get_cluster_lock(
             'fake_vcenter', 'fake_cluster')
     self.assertEqual('0', ret_1)
     self.assertTrue(info_log.called)
예제 #4
0
 def test_update_and_get_cluster_lock_second_host_no_lock(self):
     ret = ovsvapp_db.update_and_get_cluster_lock('fake_vcenter',
                                                  'fake_cluster')
     self.assertEqual('1', ret)
     with mock.patch('networking_vsphere.db.ovsvapp_db.'
                     'LOG.info') as info_log:
         ret_1 = ovsvapp_db.update_and_get_cluster_lock('fake_vcenter',
                                                        'fake_cluster')
     self.assertEqual('0', ret_1)
     self.assertTrue(info_log.called)
 def test_update_and_get_cluster_lock_threshold_reached(self):
     ret = ovsvapp_db.update_and_get_cluster_lock('fake_vcenter',
                                                  'fake_cluster')
     self.assertEqual('1', ret)
     ovsvapp_db.set_cluster_threshold('fake_vcenter', 'fake_cluster')
     with mock.patch('networking_vsphere.db.ovsvapp_db.'
                     'LOG.warning') as warn_log:
         ret_1 = ovsvapp_db.update_and_get_cluster_lock(
             'fake_vcenter', 'fake_cluster')
     self.assertEqual('-1', ret_1)
     self.assertTrue(warn_log.called)
예제 #6
0
 def test_update_and_get_cluster_lock_threshold_reached(self):
     ret = ovsvapp_db.update_and_get_cluster_lock('fake_vcenter',
                                                  'fake_cluster')
     self.assertEqual('1', ret)
     ovsvapp_db.set_cluster_threshold('fake_vcenter', 'fake_cluster')
     with mock.patch('networking_vsphere.db.ovsvapp_db.'
                     'LOG.warning') as warn_log:
         ret_1 = ovsvapp_db.update_and_get_cluster_lock('fake_vcenter',
                                                        'fake_cluster')
     self.assertEqual('-1', ret_1)
     self.assertTrue(warn_log.called)
    def monitor_agent_state(self):
        """Thread to monitor agent state.

        Represents a thread which maintains list of active
        and inactive agents based on the heartbeat recorded.
        """
        # Do nothing until plugin is initialized.
        if not self.plugin:
            status = self.get_plugin_and_initialize()
            if not status:
                LOG.warning(_LW("Plugin not defined...returning!"))
                return
        if not self.agent_ext_support:
            LOG.warning(_LW("Agent extension is not loaded by plugin."))
            return
        try:
            self.agents = self.plugin.get_agents(
                self.context,
                filters={'agent_type': [ovsvapp_const.AGENT_TYPE_OVSVAPP]})
        except Exception:
            LOG.exception(_LE("Unable to get agent list."))
            return
        for agent in self.agents:
            agent_time_stamp = agent['heartbeat_timestamp']
            agent_id = agent['id']
            status = timeutils.is_older_than(agent_time_stamp,
                                             cfg.CONF.agent_down_time * 2)
            LOG.debug("For ovsvapp_agent %(agent)s agent_state %(state)s.",
                      {'agent': agent, 'state': status})
            try:
                agent_config = agent['configurations']
                if not status:
                    if agent_id not in self.active_agents:
                        self.active_agents.append(agent_id)
                        self.update_agent_state(agent_id, True)
                    if agent_id in self.inactive_agents:
                        LOG.info(_LI("Removing agent: %s from inactive "
                                     "agent list."), agent_id)
                        self.inactive_agents.remove(agent_id)
                        ovsvapp_db.reset_cluster_threshold(
                            agent_config['vcenter_id'],
                            agent_config['cluster_id']
                        )
                else:
                    if not agent['admin_state_up']:
                        # This agent is already handled in earlier run or by
                        # another Neutron server. Just update the cache and
                        # proceed further.
                        if agent_id not in self.inactive_agents:
                            LOG.info(_LI("Moving agent: %s from active to "
                                         "inactive."), agent_id)
                            self.inactive_agents.append(agent_id)
                        if agent_id in self.active_agents:
                            self.active_agents.remove(agent_id)
                        continue
                    if self.update_agent_state(agent_id, False):
                        # Got the ownership for mitigating this agent.
                        if agent_id in self.active_agents:
                            self.active_agents.remove(agent_id)
                        if self.check_ovsvapp_data_path(agent):
                            continue
                        cluster_status = (
                            ovsvapp_db.update_and_get_cluster_lock(
                                agent_config['vcenter_id'],
                                agent_config['cluster_id']))
                        if cluster_status == ovsvapp_db.SUCCESS:
                            # Got the cluster lock for mitigating this agent.
                            self.threadpool.spawn_n(self.process_ovsvapp_agent,
                                                    agent)
                            LOG.info(_LI("Spawned a thread for processing "
                                         "OVSvApp Agent %s."), agent['id'])
                            if agent_id not in self.inactive_agents:
                                LOG.info(_LI("Moving agent: %s from active to "
                                             "inactive."), agent_id)
                                self.inactive_agents.append(agent_id)
                        elif cluster_status == ovsvapp_db.RETRY:
                            self.update_agent_state(agent['id'], True)
                            LOG.debug("Will retry the agent %s in the next "
                                      "iteration.", agent['id'])
                        elif cluster_status == ovsvapp_db.GIVE_UP:
                            self.update_agent_state(agent['id'], True)
                            LOG.debug("Threshold already reached. Will retry "
                                      "the agent %s in the next run",
                                      agent['id'])
            except Exception:
                LOG.exception(_LE("Exception occurred in"
                                  "monitor_agent_state."))