]> git.apps.os.sepia.ceph.com Git - teuthology.git/commitdiff
Remote.reconnect(): Use a default timeout of 30s 1862/head
authorZack Cerza <zack@redhat.com>
Fri, 30 Jun 2023 18:29:52 +0000 (12:29 -0600)
committerZack Cerza <zack@redhat.com>
Fri, 30 Jun 2023 18:53:22 +0000 (12:53 -0600)
And rewrite with safe_while.

Signed-off-by: Zack Cerza <zack@redhat.com>
teuthology/orchestra/remote.py

index af5e139f7b33bced5b9c71ffa69405b3e6c7c41b..64d1cbb11d6796aed82f43dde11d1a0c18f9eb05 100644 (file)
@@ -4,6 +4,7 @@ Support for paramiko remote objects.
 
 import teuthology.lock.query
 import teuthology.lock.util
+from teuthology.contextutil import safe_while
 from teuthology.orchestra import run
 from teuthology.orchestra import connection
 from teuthology.orchestra import console
@@ -13,7 +14,6 @@ from teuthology import misc
 from teuthology.exceptions import CommandFailedError
 from teuthology.misc import host_shortname
 import errno
-import time
 import re
 import logging
 from io import BytesIO
@@ -386,7 +386,7 @@ class Remote(RemoteShell):
         self.ssh = connection.connect(**args)
         return self.ssh
 
-    def reconnect(self, timeout=None, socket_timeout=None, sleep_time=30):
+    def reconnect(self, timeout=30, socket_timeout=None):
         """
         Attempts to re-establish connection. Returns True for success; False
         for failure.
@@ -395,18 +395,15 @@ class Remote(RemoteShell):
             self.ssh.close()
         if not timeout:
             return self._reconnect(timeout=socket_timeout)
-        start_time = time.time()
-        elapsed_time = lambda: time.time() - start_time
-        while elapsed_time() < timeout:
-            success = self._reconnect(timeout=socket_timeout)
-            if success:
-                log.info(f"Successfully reconnected to host '{self.name}'")
-                break
-            # Don't let time_remaining be < 0
-            time_remaining = max(0, timeout - elapsed_time())
-            sleep_val = min(time_remaining, sleep_time)
-            time.sleep(sleep_val)
-        return success
+        action = "reconnect to {self.shortname}"
+        with safe_while(action=action, timeout=timeout, increment=3, _raise=False) as proceed:
+            success = False
+            while proceed():
+                success = self._reconnect(timeout=socket_timeout)
+                if success:
+                    log.info(f"Successfully reconnected to host '{self.name}'")
+                    return success
+            return success
 
     def _reconnect(self, timeout=None):
         log.info(f"Trying to reconnect to host '{self.name}'")