Greg Padgett has uploaded a new change for review.

Change subject: agent, broker: more robust handling of socket errors
......................................................................

agent, broker: more robust handling of socket errors

Undetected socket disconnections could occur in some circumstances which
would cause the agent to stop functioning.  This improves detection of
these errors and allows the agent to attempt reconnection.

Change-Id: Id5a676f47fae83db6fd0b75bd943f6a7325a8e8f
Signed-off-by: Greg Padgett <gpadg...@redhat.com>
---
M ovirt_hosted_engine_ha/agent/brokerlink.py
M ovirt_hosted_engine_ha/broker/listener.py
M ovirt_hosted_engine_ha/lib/util.py
3 files changed, 30 insertions(+), 7 deletions(-)


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

diff --git a/ovirt_hosted_engine_ha/agent/brokerlink.py 
b/ovirt_hosted_engine_ha/agent/brokerlink.py
index 025181d..f72e43f 100644
--- a/ovirt_hosted_engine_ha/agent/brokerlink.py
+++ b/ovirt_hosted_engine_ha/agent/brokerlink.py
@@ -41,7 +41,8 @@
             self._socket.connect(constants.BROKER_SOCKET_FILE)
         except socket.error as e:
             self._log.error("Failed to connect to broker: %s", str(e))
-            self._socket.close()
+            if self._socket:
+                self._socket.close()
             self._socket = None
             raise
 
@@ -85,7 +86,7 @@
         except Exception as e:
             self._log.error("Exception getting monitor status: %s", str(e))
             raise RequestError("Failed to get monitor status: {0}"
-                               .format(response))
+                               .format(str(e)))
         self._log.info("Success, status %s", response)
         return response
 
@@ -150,8 +151,8 @@
             raise DisconnectionError("Not connected to broker")
 
         self._log.debug("Sending request: %s", request)
-        self._socket.sendall(request + "\n")
         try:
+            util.socket_sendline(self._socket, self._log, request)
             response = util.socket_readline(self._socket, self._log)
         except DisconnectionError:
             self._log.error("Connection closed")
diff --git a/ovirt_hosted_engine_ha/broker/listener.py 
b/ovirt_hosted_engine_ha/broker/listener.py
index cc64039..4dcdf52 100644
--- a/ovirt_hosted_engine_ha/broker/listener.py
+++ b/ovirt_hosted_engine_ha/broker/listener.py
@@ -138,7 +138,7 @@
                 except RequestError as e:
                     response = "failure " + format(str(e))
                 self._log.debug("Response: %s", response)
-                self.request.sendall(response + "\n")
+                util.socket_sendline(self.request, self._log, response)
             except socket.timeout:
                 pass
             except socket.error as e:
diff --git a/ovirt_hosted_engine_ha/lib/util.py 
b/ovirt_hosted_engine_ha/lib/util.py
index df8534a..3b513b6 100644
--- a/ovirt_hosted_engine_ha/lib/util.py
+++ b/ovirt_hosted_engine_ha/lib/util.py
@@ -23,6 +23,7 @@
 
 import errno
 import os
+import socket
 
 from .exceptions import DisconnectionError
 
@@ -38,10 +39,18 @@
 def socket_readline(sock, log):
     """
     Reads from a socket until newline is received.  Returns string read
-    (without trailing newline), or raises DisconnectionError.
+    (without trailing newline), or raises either DisconnectionError on
+    disconnect or socket.timeout on timeout.
     """
-    sockfile = sock.makefile()
-    msg = sockfile.readline()
+    try:
+        sockfile = sock.makefile()
+        msg = sockfile.readline()
+    except socket.timeout:
+        raise
+    except IOError as e:
+        log.debug("Connection closed while reading from socket: %s", str(e))
+        raise DisconnectionError("Connection closed")
+
     if len(msg) == 0:
         log.debug("Connection closed while reading from socket")
         raise DisconnectionError("Connection closed")
@@ -50,6 +59,19 @@
         return msg
 
 
+def socket_sendline(sock, log, data):
+    """
+    Writes data to a socket, appending a newline.  Returns normally, or
+    raises DisconnectionError if the write could not be completed.
+    """
+    try:
+        sock.sendall(data + "\n")
+    except IOError as e:
+        log.debug("Connection closed while writing to socket: %s", str(e))
+        raise DisconnectionError("Connection closed")
+    return
+
+
 def to_bool(string):
     first = str(string).lower()[:1]
     if first in ('t', 'y', '1'):


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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Id5a676f47fae83db6fd0b75bd943f6a7325a8e8f
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-hosted-engine-ha
Gerrit-Branch: master
Gerrit-Owner: Greg Padgett <gpadg...@redhat.com>
_______________________________________________
Engine-patches mailing list
Engine-patches@ovirt.org
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to