Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix agent memory usage check #2903

Merged
merged 5 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions azurelinuxagent/ga/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ def __init__(self):
self._heartbeat_id = str(uuid.uuid4()).upper()
self._heartbeat_counter = 0

self._last_check_memory_usage = datetime.min
self._initial_attempt_check_memory_usage = True
self._last_check_memory_usage_time = time.time()
self._check_memory_usage_last_error_report = datetime.min

# VM Size is reported via the heartbeat, default it here.
Expand Down Expand Up @@ -1016,8 +1017,11 @@ def _check_agent_memory_usage(self):
"""
try:
if conf.get_enable_agent_memory_usage_check() and self._extensions_summary.converged:
if self._last_check_memory_usage == datetime.min or datetime.utcnow() >= (self._last_check_memory_usage + UpdateHandler.CHECK_MEMORY_USAGE_PERIOD):
self._last_check_memory_usage = datetime.utcnow()
# we delay first attempt memory usage check, so that current agent won't get blacklisted due to multiple restarts(because of memory limit reach) too frequently
if (self._initial_attempt_check_memory_usage and time.time() - self._last_check_memory_usage_time > CHILD_LAUNCH_INTERVAL) or \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why the change from utcnow() to time()? - the former has the advantage that it formats nicely when, for example, running the code under the debugger

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing specific, I just came across the idea. Let me update if it has advantage

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, it is not a big issue.

(not self._initial_attempt_check_memory_usage and time.time() - self._last_check_memory_usage_time > conf.get_cgroup_check_period()):
self._last_check_memory_usage_time = time.time()
self._initial_attempt_check_memory_usage = False
CGroupConfigurator.get_instance().check_agent_memory_usage()
except AgentMemoryExceededException as exception:
msg = "Check on agent memory usage:\n{0}".format(ustr(exception))
Expand Down
13 changes: 11 additions & 2 deletions tests/ga/test_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -2378,7 +2378,7 @@ def test_check_agent_memory_usage_raises_exit_exception(self, patch_add_event, p
with patch('azurelinuxagent.common.conf.get_enable_agent_memory_usage_check', return_value=True):
with self.assertRaises(ExitException) as context_manager:
update_handler = get_update_handler()

update_handler._last_check_memory_usage_time = time.time() - 24 * 60
update_handler._check_agent_memory_usage()
self.assertEqual(1, patch_add_event.call_count)
self.assertTrue(any("Check on agent memory usage" in call_args[0]
Expand All @@ -2393,7 +2393,7 @@ def test_check_agent_memory_usage_fails(self, patch_add_event, patch_warn, *_):
with patch("azurelinuxagent.ga.cgroupconfigurator.CGroupConfigurator._Impl.check_agent_memory_usage", side_effect=Exception()):
with patch('azurelinuxagent.common.conf.get_enable_agent_memory_usage_check', return_value=True):
update_handler = get_update_handler()

update_handler._last_check_memory_usage_time = time.time() - 24 * 60
update_handler._check_agent_memory_usage()
self.assertTrue(any("Error checking the agent's memory usage" in call_args[0]
for call_args in patch_warn.call_args),
Expand All @@ -2409,6 +2409,15 @@ def test_check_agent_memory_usage_fails(self, patch_add_event, patch_warn, *_):
add_events[0]["message"],
"The error message is not correct when memory usage check failed")

@patch("azurelinuxagent.ga.cgroupconfigurator.CGroupConfigurator._Impl.check_agent_memory_usage")
@patch("azurelinuxagent.ga.update.add_event")
def test_check_agent_memory_usage_not_called(self, patch_add_event, patch_memory_usage, *_):
# This test ensures that agent not called immediately on startup, instead waits for CHILD_LAUNCH_INTERVAL
with patch('azurelinuxagent.common.conf.get_enable_agent_memory_usage_check', return_value=True):
update_handler = get_update_handler()
update_handler._check_agent_memory_usage()
self.assertEqual(0, patch_memory_usage.call_count)
self.assertEqual(0, patch_add_event.call_count)

class GoalStateIntervalTestCase(AgentTestCase):
def test_initial_goal_state_period_should_default_to_goal_state_period(self):
Expand Down
Loading