You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by be...@apache.org on 2013/12/02 17:57:07 UTC

[13/14] git commit: Added 'Failure' as a helper for a failed Future.

Added 'Failure' as a helper for a failed Future.

Review: https://reviews.apache.org/r/15887


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/43e90f53
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/43e90f53
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/43e90f53

Branch: refs/heads/master
Commit: 43e90f53eba58716b1f255bdf8eb25ca797d9ab0
Parents: 51f81e2
Author: Benjamin Hindman <be...@gmail.com>
Authored: Tue Nov 26 22:50:02 2013 -0800
Committer: Benjamin Hindman <be...@gmail.com>
Committed: Sun Dec 1 23:17:16 2013 -0800

----------------------------------------------------------------------
 3rdparty/libprocess/include/process/future.hpp  | 28 ++++++++++++++++++++
 3rdparty/libprocess/include/process/shared.hpp  |  2 +-
 3rdparty/libprocess/src/process.cpp             | 17 +++++-------
 3rdparty/libprocess/src/tests/process_tests.cpp |  2 +-
 4 files changed, 36 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/43e90f53/3rdparty/libprocess/include/process/future.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/include/process/future.hpp b/3rdparty/libprocess/include/process/future.hpp
index 6d70d7d..e473b3d 100644
--- a/3rdparty/libprocess/include/process/future.hpp
+++ b/3rdparty/libprocess/include/process/future.hpp
@@ -18,6 +18,7 @@
 #include <process/pid.hpp>
 
 #include <stout/duration.hpp>
+#include <stout/error.hpp>
 #include <stout/option.hpp>
 #include <stout/preprocessor.hpp>
 
@@ -254,6 +255,33 @@ private:
 };
 
 
+// Helper for creating failed futures.
+struct _Failure
+{
+  _Failure(const std::string& _message) : message(_message) {}
+
+  template <typename T>
+  operator Future<T> () const
+  {
+    return Future<T>::failed(message);
+  }
+
+  const std::string message;
+};
+
+
+inline _Failure Failure(const std::string& message)
+{
+  return _Failure(message);
+}
+
+
+inline _Failure Failure(const Error& error)
+{
+  return _Failure(error.message);
+}
+
+
 // TODO(benh): Make Promise a subclass of Future?
 template <typename T>
 class Promise

http://git-wip-us.apache.org/repos/asf/mesos/blob/43e90f53/3rdparty/libprocess/include/process/shared.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/include/process/shared.hpp b/3rdparty/libprocess/include/process/shared.hpp
index b1b6234..8f5b59b 100644
--- a/3rdparty/libprocess/include/process/shared.hpp
+++ b/3rdparty/libprocess/include/process/shared.hpp
@@ -154,7 +154,7 @@ Future<Owned<T> > Shared<T>::own()
   }
 
   if (!__sync_bool_compare_and_swap(&data->owned, false, true)) {
-    return Future<Owned<T> >::failed("Ownership has already been transferred");
+    return Failure("Ownership has already been transferred");
   }
 
   Future<Owned<T> > future = data->promise.future();

http://git-wip-us.apache.org/repos/asf/mesos/blob/43e90f53/3rdparty/libprocess/src/process.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/src/process.cpp b/3rdparty/libprocess/src/process.cpp
index 0c816cc..2d193b1 100644
--- a/3rdparty/libprocess/src/process.cpp
+++ b/3rdparty/libprocess/src/process.cpp
@@ -3603,8 +3603,7 @@ Future<Response> decode(const string& buffer)
     for (size_t i = 0; i < responses.size(); ++i) {
       delete responses[i];
     }
-    return Future<Response>::failed(
-        "Failed to decode HTTP response:\n" + buffer + "\n");
+    return Failure("Failed to decode HTTP response:\n" + buffer + "\n");
   } else if (responses.size() > 1) {
     PLOG(ERROR) << "Received more than 1 HTTP Response";
   }
@@ -3625,13 +3624,12 @@ Future<Response> get(const UPID& upid, const string& path, const string& query)
   int s = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
 
   if (s < 0) {
-    return Future<Response>::failed(
-        string("Failed to create socket: ") + strerror(errno));
+    return Failure(string("Failed to create socket: ") + strerror(errno));
   }
 
   Try<Nothing> cloexec = os::cloexec(s);
   if (!cloexec.isSome()) {
-    return Future<Response>::failed("Failed to cloexec: " + cloexec.error());
+    return Failure("Failed to cloexec: " + cloexec.error());
   }
 
   sockaddr_in addr;
@@ -3642,8 +3640,7 @@ Future<Response> get(const UPID& upid, const string& path, const string& query)
 
   if (connect(s, (sockaddr*) &addr, sizeof(addr)) < 0) {
     os::close(s);
-    return Future<Response>::failed(
-        string("Failed to connect: ") + strerror(errno));
+    return Failure(string("Failed to connect: ") + strerror(errno));
   }
 
   std::ostringstream out;
@@ -3674,8 +3671,7 @@ Future<Response> get(const UPID& upid, const string& path, const string& query)
         continue;
       }
       os::close(s);
-      return Future<Response>::failed(
-          string("Failed to write: ") + strerror(errno));
+      return Failure(string("Failed to write: ") + strerror(errno));
     }
 
     remaining -= n;
@@ -3684,8 +3680,7 @@ Future<Response> get(const UPID& upid, const string& path, const string& query)
   Try<Nothing> nonblock = os::nonblock(s);
   if (!nonblock.isSome()) {
     os::close(s);
-    return Future<Response>::failed(
-        "Failed to set nonblock: " + nonblock.error());
+    return Failure("Failed to set nonblock: " + nonblock.error());
   }
 
   // Decode once the async read completes.

http://git-wip-us.apache.org/repos/asf/mesos/blob/43e90f53/3rdparty/libprocess/src/tests/process_tests.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/src/tests/process_tests.cpp b/3rdparty/libprocess/src/tests/process_tests.cpp
index 68300f3..b0fb5c2 100644
--- a/3rdparty/libprocess/src/tests/process_tests.cpp
+++ b/3rdparty/libprocess/src/tests/process_tests.cpp
@@ -158,7 +158,7 @@ Future<bool> readyFuture()
 
 Future<bool> failedFuture()
 {
-  return Future<bool>::failed("The value is not positive (or zero)");
+  return Failure("The value is not positive (or zero)");
 }