Made HDFS::rm asynchronous.

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


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

Branch: refs/heads/master
Commit: c08d3488bcb3b849d96f6466b40d37e55a480a79
Parents: 3424b1f
Author: Jie Yu <[email protected]>
Authored: Mon Dec 14 11:42:25 2015 -0800
Committer: Jie Yu <[email protected]>
Committed: Tue Dec 15 10:55:33 2015 -0800

----------------------------------------------------------------------
 src/cli/execute.cpp |  9 ++++++---
 src/hdfs/hdfs.cpp   | 33 ++++++++++++++++++++++++---------
 src/hdfs/hdfs.hpp   |  2 +-
 3 files changed, 31 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/c08d3488/src/cli/execute.cpp
----------------------------------------------------------------------
diff --git a/src/cli/execute.cpp b/src/cli/execute.cpp
index eef3b91..fec2ad4 100644
--- a/src/cli/execute.cpp
+++ b/src/cli/execute.cpp
@@ -395,9 +395,12 @@ int main(int argc, char** argv)
            << (exists.isFailed() ? exists.failure() : "discarded") << endl;
       return EXIT_FAILURE;
     } else if (exists.get() && flags.overwrite) {
-      Try<Nothing> rm = hdfs.get()->rm(path);
-      if (rm.isError()) {
-        cerr << "Failed to remove existing file: " << rm.error() << endl;
+      Future<Nothing> rm = hdfs.get()->rm(path);
+      rm.await();
+
+      if (!rm.isReady()) {
+        cerr << "Failed to remove existing file: "
+             << (rm.isFailed() ? rm.failure() : "discarded") << endl;
         return EXIT_FAILURE;
       }
     } else if (exists.get()) {

http://git-wip-us.apache.org/repos/asf/mesos/blob/c08d3488/src/hdfs/hdfs.cpp
----------------------------------------------------------------------
diff --git a/src/hdfs/hdfs.cpp b/src/hdfs/hdfs.cpp
index 1038cc3..ed7ab63 100644
--- a/src/hdfs/hdfs.cpp
+++ b/src/hdfs/hdfs.cpp
@@ -204,20 +204,35 @@ Try<Bytes> HDFS::du(const string& _path)
 }
 
 
-Try<Nothing> HDFS::rm(const string& path)
+Future<Nothing> HDFS::rm(const string& path)
 {
-  Try<string> command = strings::format(
-      "%s fs -rm '%s'", hadoop, absolutePath(path));
+  Try<Subprocess> s = subprocess(
+      hadoop,
+      {"hadoop", "fs", "-rm", absolutePath(path)},
+      Subprocess::PATH("/dev/null"),
+      Subprocess::PIPE(),
+      Subprocess::PIPE());
 
-  CHECK_SOME(command);
+  if (s.isError()) {
+    return Failure("Failed to execute the subprocess: " + s.error());
+  }
 
-  Try<string> out = os::shell(command.get());
+  return result(s.get())
+    .then([](const CommandResult& result) -> Future<Nothing> {
+      if (result.status.isNone()) {
+        return Failure("Failed to reap the subprocess");
+      }
 
-  if (out.isError()) {
-    return Error(out.error());
-  }
+      if (result.status.get() != 0) {
+        return Failure(
+            "Unexpected result from the subprocess: "
+            "status='" + stringify(result.status.get()) + "', " +
+            "stdout='" + result.out + "', " +
+            "stderr='" + result.err + "'");
+      }
 
-  return Nothing();
+      return Nothing();
+    });
 }
 
 

http://git-wip-us.apache.org/repos/asf/mesos/blob/c08d3488/src/hdfs/hdfs.hpp
----------------------------------------------------------------------
diff --git a/src/hdfs/hdfs.hpp b/src/hdfs/hdfs.hpp
index aa44903..13ec02c 100644
--- a/src/hdfs/hdfs.hpp
+++ b/src/hdfs/hdfs.hpp
@@ -51,7 +51,7 @@ public:
 
   process::Future<bool> exists(const std::string& path);
   Try<Bytes> du(const std::string& path);
-  Try<Nothing> rm(const std::string& path);
+  process::Future<Nothing> rm(const std::string& path);
   Try<Nothing> copyFromLocal(const std::string& from, const std::string& to);
   Try<Nothing> copyToLocal(const std::string& from, const std::string& to);
 

Reply via email to