Jiří Moskovčák has uploaded a new change for review.

Change subject: refactor the brokerlink connect() code
......................................................................

refactor the brokerlink connect() code

- so it actually ends after a few attempts to connect
- throw a proper exception when the attempts are exhausted

Change-Id: I8875c0a19d61c5331a27c11bfa762177dd6a335b
Signed-off-by: Jiri Moskovcak <[email protected]>
---
M ovirt_hosted_engine_ha/agent/constants.py.in
M ovirt_hosted_engine_ha/lib/brokerlink.py
M ovirt_hosted_engine_ha/lib/exceptions.py
3 files changed, 23 insertions(+), 15 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-hosted-engine-ha 
refs/changes/40/30640/1

diff --git a/ovirt_hosted_engine_ha/agent/constants.py.in 
b/ovirt_hosted_engine_ha/agent/constants.py.in
index 56a0851..083e046 100644
--- a/ovirt_hosted_engine_ha/agent/constants.py.in
+++ b/ovirt_hosted_engine_ha/agent/constants.py.in
@@ -44,6 +44,7 @@
 MD_EXTENSION = '.metadata'
 LOCKSPACE_EXTENSION = '.lockspace'
 BROKER_CONNECTION_RETRIES = 10
+BROKER_CONNECTION_WAIT = 5
 HOST_ALIVE_TIMEOUT_SECS = 60
 ENGINE_RETRY_EXPIRATION_SECS = 600
 ENGINE_RETRY_MAX_ATTEMPTS = 3
diff --git a/ovirt_hosted_engine_ha/lib/brokerlink.py 
b/ovirt_hosted_engine_ha/lib/brokerlink.py
index 341eb12..b46f9cd 100644
--- a/ovirt_hosted_engine_ha/lib/brokerlink.py
+++ b/ovirt_hosted_engine_ha/lib/brokerlink.py
@@ -26,6 +26,7 @@
 from ..env import constants
 from ..lib.exceptions import DisconnectionError
 from ..lib.exceptions import RequestError
+from ..lib.exceptions import BrokerConnectionError
 from ..lib import util
 
 
@@ -38,7 +39,7 @@
         self._log = logging.getLogger("%s.BrokerLink" % __name__)
         self._socket = None
 
-    def connect(self, retries=0):
+    def connect(self, retries=0, wait=1):
         """
         Connect to the HA Broker.  Upon failure, reconnection attempts will
         be made approximately once per second until the specified number of
@@ -58,23 +59,25 @@
             self._socket = None
             raise
 
-        attempt = 0
-        while True:
+        for attempt in range(retries):
             try:
                 self._socket.connect(constants.BROKER_SOCKET_FILE)
+                break
             except socket.error as e:
-                if attempt < retries:
-                    self._log.info("Failed to connect to broker: %s", str(e))
-                    self._log.info("Retrying broker connection...")
-                    time.sleep(1)
-                    continue
-                else:
-                    self._log.error("Failed to connect to broker: %s", str(e))
-                    self._socket.close()
-                    self._socket = None
-                    raise
-            self._log.debug("Successfully connected")
-            break
+                self._log.info("Failed to connect to broker: %s", str(e))
+                self._log.info("Retrying broker connection in '{0}' seconds"
+                               .format(wait))
+                time.sleep(wait)
+        else:
+            error_msg = ("Failed to connect to broker, the number of "
+                         "errors has exceeded the limit ({0})"
+                         .format(retries))
+            self._log.error(error_msg)
+            self._socket.close()
+            self._socket = None
+            raise BrokerConnectionError(error_msg)
+
+        self._log.debug("Successfully connected")
 
     def is_connected(self):
         return self._socket is not None
diff --git a/ovirt_hosted_engine_ha/lib/exceptions.py 
b/ovirt_hosted_engine_ha/lib/exceptions.py
index 5eb6a83..119b33d 100644
--- a/ovirt_hosted_engine_ha/lib/exceptions.py
+++ b/ovirt_hosted_engine_ha/lib/exceptions.py
@@ -26,6 +26,10 @@
     pass
 
 
+class BrokerConnectionError(Exception):
+    pass
+
+
 class RequestError(Exception):
     pass
 


-- 
To view, visit http://gerrit.ovirt.org/30640
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I8875c0a19d61c5331a27c11bfa762177dd6a335b
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-hosted-engine-ha
Gerrit-Branch: ovirt-hosted-engine-ha-1.2
Gerrit-Owner: Jiří Moskovčák <[email protected]>
_______________________________________________
Engine-patches mailing list
[email protected]
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to