Add agent timestamp to "_log_heartbeat" method

When an agent reports the state, the timestamp is sent along with the
agent status. This timestamp now is logged if "log_agent_heartbeats" is
activated.

Change-Id: Ifc88dfb3041aa07b197f395172b69399796ba46a
Related-Bug: #1799555
This commit is contained in:
Rodolfo Alonso Hernandez 2019-06-14 16:15:16 +00:00
parent 76754e06f5
commit b76321f1d8
3 changed files with 28 additions and 16 deletions

View File

@ -356,18 +356,20 @@ class AgentDbMixin(ext_agent.AgentPluginBase, AgentAvailabilityZoneMixin):
"""
return candidate_hosts
def _log_heartbeat(self, state, agent_db, agent_conf):
def _log_heartbeat(self, state, agent_db, agent_conf, agent_timestamp):
if agent_conf.get('log_agent_heartbeats'):
delta = timeutils.utcnow() - agent_db.heartbeat_timestamp
LOG.info("Heartbeat received from %(type)s agent on "
"host %(host)s, uuid %(uuid)s after %(delta)s",
"host %(host)s, uuid %(uuid)s after %(delta)s, sent at "
"%(agent_timestamp)s",
{'type': agent_db.agent_type,
'host': agent_db.host,
'uuid': state.get('uuid'),
'delta': delta})
'delta': delta,
'agent_timestamp': agent_timestamp})
@db_api.retry_if_session_inactive()
def create_or_update_agent(self, context, agent_state):
def create_or_update_agent(self, context, agent_state, agent_timestamp):
"""Registers new agent in the database or updates existing.
Returns tuple of agent status and state.
@ -405,7 +407,8 @@ class AgentDbMixin(ext_agent.AgentPluginBase, AgentAvailabilityZoneMixin):
if agent_state.get('start_flag'):
res['started_at'] = current_time
greenthread.sleep(0)
self._log_heartbeat(agent_state, agent, configurations_dict)
self._log_heartbeat(agent_state, agent, configurations_dict,
agent_timestamp)
agent.update_fields(res)
agent.update()
event_type = events.AFTER_UPDATE
@ -420,7 +423,8 @@ class AgentDbMixin(ext_agent.AgentPluginBase, AgentAvailabilityZoneMixin):
greenthread.sleep(0)
agent.create()
event_type = events.AFTER_CREATE
self._log_heartbeat(agent_state, agent, configurations_dict)
self._log_heartbeat(agent_state, agent, configurations_dict,
agent_timestamp)
status = agent_consts.AGENT_NEW
greenthread.sleep(0)
@ -513,7 +517,7 @@ class AgentExtRpcCallback(object):
if not self.plugin:
self.plugin = directory.get_plugin()
agent_status, agent_state = self.plugin.create_or_update_agent(
context, agent_state)
context, agent_state, time)
self._update_local_agent_resource_versions(context, agent_state)
return agent_status

View File

@ -72,7 +72,7 @@ def _register_agent(agent, plugin=None):
if not plugin:
plugin = FakePlugin()
admin_context = context.get_admin_context()
plugin.create_or_update_agent(admin_context, agent)
plugin.create_or_update_agent(admin_context, agent, timeutils.utcnow())
return plugin._get_agent_by_type_and_host(
admin_context, agent['agent_type'], agent['host'])

View File

@ -126,15 +126,19 @@ class TestAgentsDbMixin(TestAgentsDbBase):
self.assertEqual(value, result[field], field)
def test_create_or_update_agent_new_entry(self):
self.plugin.create_or_update_agent(self.context, self.agent_status)
self.plugin.create_or_update_agent(self.context, self.agent_status,
timeutils.utcnow())
agent = self.plugin.get_agents(self.context)[0]
self._assert_ref_fields_are_equal(self.agent_status, agent)
def test_create_or_update_agent_existing_entry(self):
self.plugin.create_or_update_agent(self.context, self.agent_status)
self.plugin.create_or_update_agent(self.context, self.agent_status)
self.plugin.create_or_update_agent(self.context, self.agent_status)
self.plugin.create_or_update_agent(self.context, self.agent_status,
timeutils.utcnow())
self.plugin.create_or_update_agent(self.context, self.agent_status,
timeutils.utcnow())
self.plugin.create_or_update_agent(self.context, self.agent_status,
timeutils.utcnow())
agents = self.plugin.get_agents(self.context)
self.assertEqual(len(agents), 1)
@ -147,11 +151,13 @@ class TestAgentsDbMixin(TestAgentsDbBase):
status['configurations'] = {'log_agent_heartbeats': True}
with mock.patch.object(agents_db.LOG, 'info') as info:
self.plugin.create_or_update_agent(self.context, status)
self.plugin.create_or_update_agent(self.context, status,
timeutils.utcnow())
self.assertTrue(info.called)
status['configurations'] = {'log_agent_heartbeats': False}
info.reset_mock()
self.plugin.create_or_update_agent(self.context, status)
self.plugin.create_or_update_agent(self.context, status,
timeutils.utcnow())
self.assertFalse(info.called)
def test_create_or_update_agent_concurrent_insert(self):
@ -172,14 +178,16 @@ class TestAgentsDbMixin(TestAgentsDbBase):
with mock.patch('neutron.objects.db.api.create_object') as add_mock:
add_mock.side_effect = create_obj_side_effect
self.plugin.create_or_update_agent(self.context, self.agent_status)
self.plugin.create_or_update_agent(self.context, self.agent_status,
'any_timestamp')
self.assertEqual(add_mock.call_count, 2,
"Agent entry creation hasn't been retried")
def test_create_or_update_agent_disable_new_agents(self):
cfg.CONF.set_override('enable_new_agents', False)
self.plugin.create_or_update_agent(self.context, self.agent_status)
self.plugin.create_or_update_agent(self.context, self.agent_status,
'any_timestamp')
agent = self.plugin.get_agents(self.context)[0]
self.assertFalse(agent['admin_state_up'])