mesos-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ji...@apache.org
Subject [1/3] mesos git commit: Added containerizer helper method 'getContainerIds()'.
Date Tue, 27 Sep 2016 03:51:57 GMT
Repository: mesos
Updated Branches:
  refs/heads/master 9faf6a091 -> d1cb408f1


Added containerizer helper method 'getContainerIds()'.

This is a helper function to collect all containerIds (include top
level and nested) from the runtime directory.

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


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

Branch: refs/heads/master
Commit: 67135e455b437af4af9753758a1d4829022ec324
Parents: 9faf6a0
Author: Gilbert Song <songzihao1990@gmail.com>
Authored: Mon Sep 26 20:10:47 2016 -0700
Committer: Jie Yu <yujie.jay@gmail.com>
Committed: Mon Sep 26 20:15:10 2016 -0700

----------------------------------------------------------------------
 src/slave/containerizer/mesos/paths.cpp | 63 ++++++++++++++++++++++++++++
 src/slave/containerizer/mesos/paths.hpp |  8 ++++
 2 files changed, 71 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/67135e45/src/slave/containerizer/mesos/paths.cpp
----------------------------------------------------------------------
diff --git a/src/slave/containerizer/mesos/paths.cpp b/src/slave/containerizer/mesos/paths.cpp
index faa588a..768cb99 100644
--- a/src/slave/containerizer/mesos/paths.cpp
+++ b/src/slave/containerizer/mesos/paths.cpp
@@ -14,12 +14,15 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
+#include <stout/lambda.hpp>
 #include <stout/os.hpp>
 #include <stout/path.hpp>
 
 #include "slave/containerizer/mesos/paths.hpp"
 
+using std::list;
 using std::string;
+using std::vector;
 
 namespace mesos {
 namespace internal {
@@ -117,6 +120,66 @@ Result<int> getContainerStatus(
   return None();
 }
 
+
+Try<vector<ContainerID>> getContainerIds(const string& runtimeDir)
+{
+  lambda::function<Try<vector<ContainerID>>(const Option<ContainerID>&)>
helper;
+
+  helper = [&helper, &runtimeDir](const Option<ContainerID>& parentContainerId)
+    -> Try<vector<ContainerID>> {
+    // Loop through each container at the path, if it exists.
+    const string path = path::join(
+        parentContainerId.isSome()
+          ? getRuntimePath(runtimeDir, parentContainerId.get())
+          : runtimeDir,
+        CONTAINER_DIRECTORY);
+
+    if (!os::exists(path)) {
+      return vector<ContainerID>();
+    }
+
+    Try<list<string>> entries = os::ls(path);
+    if (entries.isError()) {
+      return Error("Failed to list '" + path + "': " + entries.error());
+    }
+
+    // The order always guarantee that a parent container is inserted
+    // before its child containers. This is necessary for constructing
+    // the hashmap 'containers_' in 'Containerizer::recover()'.
+    vector<ContainerID> containers;
+
+    foreach (const string& entry, entries.get()) {
+      // We're not expecting anything else but directories here
+      // representing each container.
+      CHECK(os::stat::isdir(path::join(path, entry)));
+
+      // TODO(benh): Validate that the entry looks like a ContainerID?
+      ContainerID container;
+      container.set_value(entry);
+
+      if (parentContainerId.isSome()) {
+        container.mutable_parent()->CopyFrom(parentContainerId.get());
+      }
+
+      containers.push_back(container);
+
+      // Now recursively build the list of nested containers.
+      Try<vector<ContainerID>> children = helper(container);
+      if (children.isError()) {
+        return Error(children.error());
+      }
+
+      if (!children->empty()) {
+        containers.insert(containers.end(), children->begin(), children->end());
+      }
+    }
+
+    return containers;
+  };
+
+  return helper(None());
+}
+
 } // namespace paths {
 } // namespace containerizer {
 } // namespace slave {

http://git-wip-us.apache.org/repos/asf/mesos/blob/67135e45/src/slave/containerizer/mesos/paths.hpp
----------------------------------------------------------------------
diff --git a/src/slave/containerizer/mesos/paths.hpp b/src/slave/containerizer/mesos/paths.hpp
index c47fd8c..373905b 100644
--- a/src/slave/containerizer/mesos/paths.hpp
+++ b/src/slave/containerizer/mesos/paths.hpp
@@ -74,6 +74,14 @@ Result<int> getContainerStatus(
     const std::string& runtimeDir,
     const ContainerID& containerId);
 
+
+// The helper method to list all container IDs (including nested
+// containers) from the container runtime directory. The order of
+// returned vector is a result of pre-ordering walk (i.e., parent
+// is inserted before its children).
+Try<std::vector<ContainerID>> getContainerIds(
+    const std::string& runtimeDir);
+
 } // namespace paths {
 } // namespace containerizer {
 } // namespace slave {


Mime
View raw message