Blame SOURCES/0054-v2v-o-rhv-upload-Log-full-imageio-response-on-failur.patch

97ae69
From ed2835440ba7d1a4bc4f9aeb92a14c644522bbfd Mon Sep 17 00:00:00 2001
97ae69
From: "Richard W.M. Jones" <rjones@redhat.com>
97ae69
Date: Tue, 5 Jun 2018 13:27:43 +0100
97ae69
Subject: [PATCH] v2v: -o rhv-upload: Log full imageio response on failure.
97ae69
97ae69
Thanks: Nir Soffer
97ae69
(cherry picked from commit 831a75cd11c5a87e40fccdadcb62353f6a4d5a72)
97ae69
---
97ae69
 v2v/rhv-upload-plugin.py | 69 ++++++++++++++++++++++++----------------
97ae69
 1 file changed, 42 insertions(+), 27 deletions(-)
97ae69
97ae69
diff --git a/v2v/rhv-upload-plugin.py b/v2v/rhv-upload-plugin.py
97ae69
index 9ad354b84..7c5084efd 100644
97ae69
--- a/v2v/rhv-upload-plugin.py
97ae69
+++ b/v2v/rhv-upload-plugin.py
97ae69
@@ -227,6 +227,32 @@ def can_flush(h):
97ae69
 def get_size(h):
97ae69
     return params['disk_size']
97ae69
 
97ae69
+# Any unexpected HTTP response status from the server will end up
97ae69
+# calling this function which logs the full error, pauses the
97ae69
+# transfer, sets the failed state, and raises a RuntimeError
97ae69
+# exception.
97ae69
+def request_failed(h, r, msg):
97ae69
+    # Setting the failed flag in the handle causes the disk to be
97ae69
+    # cleaned up on close.
97ae69
+    h['failed'] = True
97ae69
+    h['transfer_service'].pause()
97ae69
+
97ae69
+    status = r.status
97ae69
+    reason = r.reason
97ae69
+    try:
97ae69
+        body = r.read()
97ae69
+    except EnvironmentError as e:
97ae69
+        body = "(Unable to read response body: %s)" % e
97ae69
+
97ae69
+    # Log the full error if we're verbose.
97ae69
+    debug("unexpected response from imageio server:")
97ae69
+    debug(msg)
97ae69
+    debug("%d: %s" % (status, reason))
97ae69
+    debug(body)
97ae69
+
97ae69
+    # Only a short error is included in the exception.
97ae69
+    raise RuntimeError("%s: %d %s: %r", msg, status, reason, body[:200])
97ae69
+
97ae69
 # For documentation see:
97ae69
 # https://github.com/oVirt/ovirt-imageio/blob/master/docs/random-io.md
97ae69
 # For examples of working code to read/write from the server, see:
97ae69
@@ -247,16 +273,14 @@ def pread(h, count, offset):
97ae69
     r = http.getresponse()
97ae69
     # 206 = HTTP Partial Content.
97ae69
     if r.status != 206:
97ae69
-        h['transfer_service'].pause()
97ae69
-        h['failed'] = True
97ae69
-        raise RuntimeError("could not read sector (%d, %d): %d: %s" %
97ae69
-                           (offset, count, r.status, r.reason))
97ae69
+        request_failed(h, r,
97ae69
+                       "could not read sector offset %d size %d" %
97ae69
+                       (offset, count))
97ae69
     return r.read()
97ae69
 
97ae69
 def pwrite(h, buf, offset):
97ae69
     http = h['http']
97ae69
     transfer = h['transfer']
97ae69
-    transfer_service = h['transfer_service']
97ae69
 
97ae69
     count = len(buf)
97ae69
     h['highestwrite'] = max(h['highestwrite'], offset+count)
97ae69
@@ -274,15 +298,13 @@ def pwrite(h, buf, offset):
97ae69
 
97ae69
     r = http.getresponse()
97ae69
     if r.status != 200:
97ae69
-        transfer_service.pause()
97ae69
-        h['failed'] = True
97ae69
-        raise RuntimeError("could not write sector (%d, %d): %d: %s" %
97ae69
-                           (offset, count, r.status, r.reason))
97ae69
+        request_failed(h, r,
97ae69
+                       "could not write sector offset %d size %d" %
97ae69
+                       (offset, count))
97ae69
 
97ae69
 def zero(h, count, offset, may_trim):
97ae69
     http = h['http']
97ae69
     transfer = h['transfer']
97ae69
-    transfer_service = h['transfer_service']
97ae69
 
97ae69
     # Unlike the trim and flush calls, there is no 'can_zero' method
97ae69
     # so nbdkit could call this even if the server doesn't support
97ae69
@@ -304,10 +326,9 @@ def zero(h, count, offset, may_trim):
97ae69
 
97ae69
     r = http.getresponse()
97ae69
     if r.status != 200:
97ae69
-        transfer_service.pause()
97ae69
-        h['failed'] = True
97ae69
-        raise RuntimeError("could not zero sector (%d, %d): %d: %s" %
97ae69
-                           (offset, count, r.status, r.reason))
97ae69
+        request_failed(h, r,
97ae69
+                       "could not zero sector offset %d size %d" %
97ae69
+                       (offset, count))
97ae69
 
97ae69
 def emulate_zero(h, count, offset):
97ae69
     # qemu-img convert starts by trying to zero/trim the whole device.
97ae69
@@ -332,15 +353,13 @@ def emulate_zero(h, count, offset):
97ae69
 
97ae69
         r = http.getresponse()
97ae69
         if r.status != 200:
97ae69
-            transfer_service.pause()
97ae69
-            h['failed'] = True
97ae69
-            raise RuntimeError("could not write zeroes (%d, %d): %d: %s" %
97ae69
-                               (offset, count, r.status, r.reason))
97ae69
+            request_failed(h, r,
97ae69
+                           "could not write zeroes offset %d size %d" %
97ae69
+                           (offset, count))
97ae69
 
97ae69
 def trim(h, count, offset):
97ae69
     http = h['http']
97ae69
     transfer = h['transfer']
97ae69
-    transfer_service = h['transfer_service']
97ae69
 
97ae69
     # Construct the JSON request for trimming.
97ae69
     buf = json.dumps({'op': "trim",
97ae69
@@ -355,15 +374,13 @@ def trim(h, count, offset):
97ae69
 
97ae69
     r = http.getresponse()
97ae69
     if r.status != 200:
97ae69
-        transfer_service.pause()
97ae69
-        h['failed'] = True
97ae69
-        raise RuntimeError("could not trim sector (%d, %d): %d: %s" %
97ae69
-                           (offset, count, r.status, r.reason))
97ae69
+        request_failed(h, r,
97ae69
+                       "could not trim sector offset %d size %d" %
97ae69
+                       (offset, count))
97ae69
 
97ae69
 def flush(h):
97ae69
     http = h['http']
97ae69
     transfer = h['transfer']
97ae69
-    transfer_service = h['transfer_service']
97ae69
 
97ae69
     # Construct the JSON request for flushing.
97ae69
     buf = json.dumps({'op': "flush"}).encode()
97ae69
@@ -375,9 +392,7 @@ def flush(h):
97ae69
 
97ae69
     r = http.getresponse()
97ae69
     if r.status != 200:
97ae69
-        transfer_service.pause()
97ae69
-        h['failed'] = True
97ae69
-        raise RuntimeError("could not flush: %d: %s" % (r.status, r.reason))
97ae69
+        request_failed(h, r, "could not flush")
97ae69
 
97ae69
 def delete_disk_on_failure(h):
97ae69
     disk_service = h['disk_service']
97ae69
-- 
97ae69
2.17.1
97ae69