]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
rgw: handle errors via exceptions in RGWStreamIOEngine.
authorRadoslaw Zarzynski <rzarzynski@mirantis.com>
Fri, 5 Aug 2016 12:48:06 +0000 (14:48 +0200)
committerRadoslaw Zarzynski <rzarzynski@mirantis.com>
Fri, 21 Oct 2016 20:57:19 +0000 (22:57 +0200)
Signed-off-by: Radoslaw Zarzynski <rzarzynski@mirantis.com>
src/rgw/rgw_asio_client.cc
src/rgw/rgw_asio_client.h
src/rgw/rgw_civetweb.cc
src/rgw/rgw_civetweb.h
src/rgw/rgw_client_io.h
src/rgw/rgw_client_io_decoimpl.h
src/rgw/rgw_fcgi.cc
src/rgw/rgw_fcgi.h
src/rgw/rgw_lib.h
src/rgw/rgw_loadgen.h

index 679b3078aef527d8332f45a3f8e6fbfcda020213..42251ae1d6be65ab568e8f51776a1abd3c8a78e2 100644 (file)
@@ -84,7 +84,7 @@ int RGWAsioClientIO::write_data(const char* const buf, const int len)
   auto bytes = boost::asio::write(socket, boost::asio::buffer(buf, len), ec);
   if (ec) {
     derr << "write_data failed with " << ec.message() << dendl;
-    return -ec.value();
+    throw RGWStreamIOEngine::Exception(-ec.value());
   }
   return bytes;
 }
index 11ae0443d4fe8dddffc7c82dd5e48fba6e28031b..1f0dbbcdfdbd61bfc5e2f8f50e8d9e51e74e90e1 100644 (file)
@@ -53,7 +53,7 @@ class RGWAsioClientIO : public RGWStreamIOEngine {
   int complete_header() override;
   int send_content_length(uint64_t len) override;
 
-  RGWEnv& get_env() override {
+  RGWEnv& get_env() noexcept override {
     return env;
   }
 };
index e9e0b0d852bbf675703e3b39c240cfd77e633497..1ef15641fd2870addee095b14c76ba7ae2aa8a31 100644 (file)
 
 int RGWCivetWeb::write_data(const char *buf, int len)
 {
-  int r = mg_write(conn, buf, len);
-  if (r == 0) {
+  const int ret = mg_write(conn, buf, len);
+  if (ret == 0) {
     /* didn't send anything, error out */
-    return -EIO;
+    throw RGWStreamIOEngine::Exception(-EIO);
+  } else if (ret < 0) {
+    throw RGWStreamIOEngine::Exception(ret);
   }
-  return r;
+  return ret;
 }
 
 RGWCivetWeb::RGWCivetWeb(mg_connection* const conn, const int port)
@@ -32,7 +34,11 @@ RGWCivetWeb::RGWCivetWeb(mg_connection* const conn, const int port)
 
 int RGWCivetWeb::read_data(char *buf, int len)
 {
-  return mg_read(conn, buf, len);
+  const int ret = mg_read(conn, buf, len);
+  if (ret < 0) {
+    throw RGWStreamIOEngine::Exception(ret);
+  }
+  return ret;
 }
 
 void RGWCivetWeb::flush()
@@ -109,12 +115,25 @@ void RGWCivetWeb::init_env(CephContext *cct)
   }
 }
 
+template <class... Args>
+static inline std::size_t safe_mg_printf(Args&&... args)
+{
+  const int ret = mg_printf(std::forward<Args>(args)...);
+  if (ret == 0) {
+    /* didn't send anything, error out */
+    throw RGWStreamIOEngine::Exception(-EIO);
+  } else if (ret < 0) {
+    throw RGWStreamIOEngine::Exception(ret);
+  }
+  return static_cast<std::size_t>(ret);
+}
+
 int RGWCivetWeb::send_status(int status, const char *status_name)
 {
   mg_set_http_status(conn, status);
 
-  return mg_printf(conn, "HTTP/1.1 %d %s\r\n", status,
-                   status_name ? status_name : "");
+  return safe_mg_printf(conn, "HTTP/1.1 %d %s\r\n", status,
+                        status_name ? status_name : "");
 }
 
 int RGWCivetWeb::send_100_continue()
@@ -161,5 +180,5 @@ int RGWCivetWeb::complete_header()
 
 int RGWCivetWeb::send_content_length(uint64_t len)
 {
-  return mg_printf(conn, "Content-Length: %" PRIu64 "\r\n", len);
+  return safe_mg_printf(conn, "Content-Length: %" PRIu64 "\r\n", len);
 }
index b2bf41239e1aa6ce48d479320a9d67fbdde3b360..0072d99c306f362424d31d3dec5bc570e355de4a 100644 (file)
@@ -35,7 +35,7 @@ public:
 
   void flush() override;
 
-  RGWEnv& get_env() override {
+  RGWEnv& get_env() noexcept override {
     return env;
   }
 
index 89c3123cb4d60201ec9862a7a6b35f46e1593bdd..afb8461ce9156abbe7a88e5f760473761eebbcbd 100644 (file)
@@ -4,6 +4,7 @@
 #ifndef CEPH_RGW_CLIENT_IO_H
 #define CEPH_RGW_CLIENT_IO_H
 
+#include <exception>
 #include <string>
 #include <streambuf>
 #include <istream>
@@ -21,7 +22,7 @@ public:
   virtual ~RGWClientIO() {}
 
   void init(CephContext *cct);
-  virtual RGWEnv& get_env() = 0;
+  virtual RGWEnv& get_env() noexcept = 0;
   virtual int complete_request() = 0;
 }; /* RGWClient IO */
 
@@ -46,6 +47,19 @@ protected:
   virtual int write_data(const char *buf, int len) = 0;
 
 public:
+  class Exception : public std::exception {
+    int err;
+
+  public:
+    Exception(const int err)
+      : err(err) {
+    }
+
+    int value() {
+      return err;
+    }
+  };
+
   virtual int send_status(int status, const char *status_name) = 0;
   virtual int send_100_continue() = 0;
   virtual int complete_header() = 0;
@@ -106,7 +120,7 @@ public:
 
   std::string grab_aws4_sha256_hash();
 
-  RGWEnv& get_env() override {
+  RGWEnv& get_env() noexcept override {
     return env;
   }
 
@@ -135,17 +149,31 @@ class RGWStreamIOLegacyWrapper : public RGWStreamIO {
     return *engine;
   }
 
+#define EXCPT_TO_RC(code)                                       \
+  try {                                                         \
+    return code;                                                \
+  } catch (RGWStreamIOEngine::Exception& e) {                   \
+    return e.value();                                           \
+  }
+
+#define EXCPT_TO_VOID(code)                                     \
+  try {                                                         \
+    return code;                                                \
+  } catch (RGWStreamIOEngine::Exception& e) {                   \
+    return;                                                     \
+  }
+
 protected:
   void init_env(CephContext *cct) override {
-    return get_decoratee().init_env(cct);
+    EXCPT_TO_VOID(get_decoratee().init_env(cct));
   }
 
   int read_data(char* const buf, const int max) override {
-    return get_decoratee().read_data(buf, max);
+    EXCPT_TO_RC(get_decoratee().read_data(buf, max));
   }
 
   int write_data(const char* const buf, const int len) override {
-    return get_decoratee().write_data(buf, len);
+    EXCPT_TO_RC(get_decoratee().write_data(buf, len));
   }
 
 public:
@@ -154,31 +182,31 @@ public:
   }
 
   int send_status(const int status, const char* const status_name) override {
-    return get_decoratee().send_status(status, status_name);
+    EXCPT_TO_RC(get_decoratee().send_status(status, status_name));
   }
 
   int send_100_continue() override {
-    return get_decoratee().send_100_continue();
+    EXCPT_TO_RC(get_decoratee().send_100_continue());
   }
 
   int send_content_length(const uint64_t len) override {
-    return get_decoratee().send_content_length(len);
+    EXCPT_TO_RC(get_decoratee().send_content_length(len));
   }
 
   int complete_header() override {
-    return get_decoratee().complete_header();
+    EXCPT_TO_RC(get_decoratee().complete_header());
   }
 
   void flush() override {
-    return get_decoratee().flush();
+    EXCPT_TO_VOID(get_decoratee().flush());
   }
 
-  RGWEnv& get_env() override {
+  RGWEnv& get_env() noexcept override {
     return get_decoratee().get_env();
   }
 
   int complete_request() override {
-    return get_decoratee().complete_request();
+    EXCPT_TO_RC(get_decoratee().complete_request());
   }
 };
 
index 08a392cc8d538f1c7b1f53d8bd8397da548c5330..ebb48820d032e9c2f3d9b0c7599a5556b1790ea6 100644 (file)
@@ -79,7 +79,7 @@ public:
     return get_decoratee().flush();
   }
 
-  RGWEnv& get_env() override {
+  RGWEnv& get_env() noexcept override {
     return get_decoratee().get_env();
   }
 
index 95f945d7c496080249872d4a760c279c660be46d..5b59f717b5bec48e24d2fdcdda35d4dd876cd4be 100644 (file)
@@ -7,12 +7,20 @@
 
 int RGWFCGX::write_data(const char* const buf, const int len)
 {
-  return FCGX_PutStr(buf, len, fcgx->out);
+  const auto ret = FCGX_PutStr(buf, len, fcgx->out);
+  if (ret < 0) {
+    throw RGWStreamIOEngine::Exception(ret);
+  }
+  return ret;
 }
 
 int RGWFCGX::read_data(char* const buf, const int len)
 {
-  return FCGX_GetStr(buf, len, fcgx->in);
+  const auto ret = FCGX_GetStr(buf, len, fcgx->in);
+  if (ret < 0) {
+    throw RGWStreamIOEngine::Exception(ret);
+  }
+  return ret;
 }
 
 void RGWFCGX::flush()
index 046dbb53e1f5cf708d9ff897f0906c116fc0be09..4f235714e341f3980b63a87eac4e810313a4f7e8 100644 (file)
@@ -32,7 +32,7 @@ public:
 
   void flush();
 
-  RGWEnv& get_env() override {
+  RGWEnv& get_env() noexcept override {
     return env;
   }
 
index c937d475120e10d3047f1179bc5d9477ce559226..c10f66915c5959d45e2554180064bac3c961ae57 100644 (file)
@@ -77,7 +77,7 @@ namespace rgw {
     int complete_header();
     int send_content_length(uint64_t len);
 
-    RGWEnv& get_env() override {
+    RGWEnv& get_env() noexcept override {
       return env;
     }
 
index 1dec5c86a5016283fafb3129ec547a1b8eb434e6..173e493b122cbe9bf9f5fedeec821aa1e2cb648e 100644 (file)
@@ -55,7 +55,7 @@ public:
 
   void flush();
 
-  RGWEnv& get_env() override {
+  RGWEnv& get_env() noexcept override {
     return env;
   }