Repository: mesos
Updated Branches:
  refs/heads/master 02013593c -> 1ba5153ee


Supported provisioner listContainers() to be recursive.

This patch supports collecting all containerIds (all containers in the
nested hierarchy) recursively.

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


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

Branch: refs/heads/master
Commit: 1ba5153ee4d0a0075b51debab72903bba561c6b7
Parents: 0201359
Author: Gilbert Song <songzihao1...@gmail.com>
Authored: Thu Sep 8 16:05:43 2016 -0700
Committer: Jie Yu <yujie....@gmail.com>
Committed: Thu Sep 8 16:43:39 2016 -0700

----------------------------------------------------------------------
 .../containerizer/mesos/provisioner/paths.cpp   | 77 ++++++++++++++------
 1 file changed, 55 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/1ba5153e/src/slave/containerizer/mesos/provisioner/paths.cpp
----------------------------------------------------------------------
diff --git a/src/slave/containerizer/mesos/provisioner/paths.cpp 
b/src/slave/containerizer/mesos/provisioner/paths.cpp
index c0ad2cd..68bc1b4 100644
--- a/src/slave/containerizer/mesos/provisioner/paths.cpp
+++ b/src/slave/containerizer/mesos/provisioner/paths.cpp
@@ -106,35 +106,68 @@ string getContainerRootfsDir(
 Try<hashset<ContainerID>> listContainers(
     const string& provisionerDir)
 {
-  hashset<ContainerID> results;
+  lambda::function<Try<hashset<ContainerID>>(
+      const string&,
+      const Option<ContainerID>&)> helper;
+
+  // This helper lists all child (or grand child) containers under
+  // 'containersDir' whose parent (or grand parent) is the given
+  // 'parentContainerId'.
+  //
+  // NOTE: The lambda is used here because we do not want the
+  // 'parentContainerId' to be in the public function.
+  helper = [&helper](
+      const string& containersDir,
+      const Option<ContainerID>& parentContainerId)
+    -> Try<hashset<ContainerID>> {
+    // This is the termination condition for the recursion.
+    if (!os::exists(containersDir)) {
+      return hashset<ContainerID>();
+    }
 
-  string containersDir = getContainersDir(provisionerDir);
-  if (!os::exists(containersDir)) {
-    // No container has been created yet.
-    return results;
-  }
+    Try<list<string>> containerIds = os::ls(containersDir);
+    if (containerIds.isError()) {
+      return Error(
+          "Unable to list the containers under directory: '" +
+          containersDir + "': " + containerIds.error());
+    }
 
-  Try<list<string>> containerIds = os::ls(containersDir);
-  if (containerIds.isError()) {
-    return Error("Unable to list the containers directory: " +
-                 containerIds.error());
-  }
+    hashset<ContainerID> results;
 
-  foreach (const string& entry, containerIds.get()) {
-    string containerPath = path::join(containersDir, entry);
+    foreach (const string& entry, containerIds.get()) {
+      const string containerPath = path::join(containersDir, entry);
 
-    if (!os::stat::isdir(containerPath)) {
-      LOG(WARNING) << "Ignoring unexpected container entry at: "
-                   << containerPath;
-      continue;
+      if (!os::stat::isdir(containerPath)) {
+        LOG(WARNING) << "Ignoring unexpected container entry at "
+                     << "'" << containerPath << "' when listing "
+                     << "containers in provisioner";
+        continue;
+      }
+
+      ContainerID containerId;
+      containerId.set_value(entry);
+
+      if (parentContainerId.isSome()) {
+        containerId.mutable_parent()->CopyFrom(parentContainerId.get());
+      }
+
+      results.insert(containerId);
+
+      Try<hashset<ContainerID>> children = helper(
+          getContainersDir(containerPath),
+          containerId);
+
+      if (children.isError()) {
+        return Error("Failed to list child containers: " + children.error());
+      }
+
+      results.insert(children->begin(), children->end());
     }
 
-    ContainerID containerId;
-    containerId.set_value(entry);
-    results.insert(containerId);
-  }
+    return results;
+  };
 
-  return results;
+  return helper(getContainersDir(provisionerDir), None());
 }
 
 

Reply via email to