sailesh1993 / rpms / cloud-init

Forked from rpms/cloud-init a year ago
Clone
42024e
From 3ee42e6e6ca51b3fd0b6461f707d62c89d54e227 Mon Sep 17 00:00:00 2001
42024e
From: Johnson Shi <Johnson.Shi@microsoft.com>
42024e
Date: Thu, 25 Mar 2021 07:20:10 -0700
42024e
Subject: [PATCH 2/7] Azure helper: Ensure Azure http handler sleeps between
42024e
 retries (#842)
42024e
42024e
RH-Author: Eduardo Otubo <otubo@redhat.com>
42024e
RH-MergeRequest: 18: Add support for userdata on Azure from IMDS
42024e
RH-Commit: [2/7] 65672cdfe2265f32e6d3c440ba5a8accafdb6ca6 (otubo/cloud-init-src)
42024e
RH-Bugzilla: 2042351
42024e
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
42024e
RH-Acked-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
42024e
42024e
Ensure that the Azure helper's http handler sleeps a fixed duration
42024e
between retry failure attempts. The http handler will sleep a fixed
42024e
duration between failed attempts regardless of whether the attempt
42024e
failed due to (1) request timing out or (2) instant failure (no
42024e
timeout).
42024e
42024e
Due to certain platform issues, the http request to the Azure endpoint
42024e
may instantly fail without reaching the http timeout duration. Without
42024e
sleeping a fixed duration in between retry attempts, the http handler
42024e
will loop through the max retry attempts quickly. This causes the
42024e
communication between cloud-init and the Azure platform to be less
42024e
resilient due to the short total duration if there is no sleep in
42024e
between retries.
42024e
---
42024e
 cloudinit/sources/helpers/azure.py                   |  2 ++
42024e
 tests/unittests/test_datasource/test_azure_helper.py | 11 +++++++++--
42024e
 2 files changed, 11 insertions(+), 2 deletions(-)
42024e
42024e
diff --git a/cloudinit/sources/helpers/azure.py b/cloudinit/sources/helpers/azure.py
42024e
index d3055d08..03e7156b 100755
42024e
--- a/cloudinit/sources/helpers/azure.py
42024e
+++ b/cloudinit/sources/helpers/azure.py
42024e
@@ -303,6 +303,7 @@ def http_with_retries(url, **kwargs) -> str:
42024e
 
42024e
     max_readurl_attempts = 240
42024e
     default_readurl_timeout = 5
42024e
+    sleep_duration_between_retries = 5
42024e
     periodic_logging_attempts = 12
42024e
 
42024e
     if 'timeout' not in kwargs:
42024e
@@ -338,6 +339,7 @@ def http_with_retries(url, **kwargs) -> str:
42024e
                     'attempt %d with exception: %s' %
42024e
                     (url, attempt, e),
42024e
                     logger_func=LOG.debug)
42024e
+            time.sleep(sleep_duration_between_retries)
42024e
 
42024e
     raise exc
42024e
 
42024e
diff --git a/tests/unittests/test_datasource/test_azure_helper.py b/tests/unittests/test_datasource/test_azure_helper.py
42024e
index b8899807..63482c6c 100644
42024e
--- a/tests/unittests/test_datasource/test_azure_helper.py
42024e
+++ b/tests/unittests/test_datasource/test_azure_helper.py
42024e
@@ -384,6 +384,7 @@ class TestAzureHelperHttpWithRetries(CiTestCase):
42024e
 
42024e
     max_readurl_attempts = 240
42024e
     default_readurl_timeout = 5
42024e
+    sleep_duration_between_retries = 5
42024e
     periodic_logging_attempts = 12
42024e
 
42024e
     def setUp(self):
42024e
@@ -394,8 +395,8 @@ class TestAzureHelperHttpWithRetries(CiTestCase):
42024e
         self.m_readurl = patches.enter_context(
42024e
             mock.patch.object(
42024e
                 azure_helper.url_helper, 'readurl', mock.MagicMock()))
42024e
-        patches.enter_context(
42024e
-            mock.patch.object(azure_helper.time, 'sleep', mock.MagicMock()))
42024e
+        self.m_sleep = patches.enter_context(
42024e
+            mock.patch.object(azure_helper.time, 'sleep', autospec=True))
42024e
 
42024e
     def test_http_with_retries(self):
42024e
         self.m_readurl.return_value = 'TestResp'
42024e
@@ -438,6 +439,12 @@ class TestAzureHelperHttpWithRetries(CiTestCase):
42024e
             self.m_readurl.call_count,
42024e
             self.periodic_logging_attempts + 1)
42024e
 
42024e
+        # Ensure that cloud-init did sleep between each failed request
42024e
+        self.assertEqual(
42024e
+            self.m_sleep.call_count,
42024e
+            self.periodic_logging_attempts)
42024e
+        self.m_sleep.assert_called_with(self.sleep_duration_between_retries)
42024e
+
42024e
     def test_http_with_retries_long_delay_logs_periodic_failure_msg(self):
42024e
         self.m_readurl.side_effect = \
42024e
             [SentinelException] * self.periodic_logging_attempts + \
42024e
-- 
42024e
2.27.0
42024e