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 2016/09/27 05:41:55 UTC

[5/8] mesos git commit: Factored out a 'cgroups::isolate()' function.

Factored out a 'cgroups::isolate()' function.

Cleans up code in LinuxLauncher. Adds a useful function for others to
use other places if necessary.

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


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

Branch: refs/heads/master
Commit: b7383c0d2cc3859ec4e589359e4b2eb3290b9d13
Parents: e92f3e6
Author: Benjamin Hindman <be...@gmail.com>
Authored: Sun Aug 21 21:28:33 2016 -0700
Committer: Benjamin Hindman <be...@gmail.com>
Committed: Mon Sep 26 22:41:21 2016 -0700

----------------------------------------------------------------------
 src/linux/cgroups.cpp                           | 27 ++++++++++
 src/linux/cgroups.hpp                           | 15 ++++++
 .../containerizer/mesos/linux_launcher.cpp      | 55 +++-----------------
 3 files changed, 50 insertions(+), 47 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/b7383c0d/src/linux/cgroups.cpp
----------------------------------------------------------------------
diff --git a/src/linux/cgroups.cpp b/src/linux/cgroups.cpp
index f3232c0..1475c8e 100644
--- a/src/linux/cgroups.cpp
+++ b/src/linux/cgroups.cpp
@@ -1074,6 +1074,33 @@ Try<Nothing> assign(const string& hierarchy, const string& cgroup, pid_t pid)
 }
 
 
+Try<Nothing> isolate(
+    const string& hierarchy,
+    const string& cgroup,
+    pid_t pid)
+{
+  // Create cgroup if necessary.
+  Try<bool> exists = cgroups::exists(hierarchy, cgroup);
+  if (exists.isError()) {
+    return Error("Failed to check existence of cgroup: " + exists.error());
+  }
+
+  if (!exists.get()) {
+    Try<Nothing> create = cgroups::create(hierarchy, cgroup, true);
+    if (create.isError()) {
+      return Error("Failed to create cgroup: " + create.error());
+    }
+  }
+
+  Try<Nothing> assign = cgroups::assign(hierarchy, cgroup, pid);
+  if (assign.isError()) {
+    return Error("Failed to assign process to cgroup: " + assign.error());
+  }
+
+  return Nothing();
+}
+
+
 namespace event {
 
 #ifndef EFD_SEMAPHORE

http://git-wip-us.apache.org/repos/asf/mesos/blob/b7383c0d/src/linux/cgroups.hpp
----------------------------------------------------------------------
diff --git a/src/linux/cgroups.hpp b/src/linux/cgroups.hpp
index 047d3ea..3c9d9e2 100644
--- a/src/linux/cgroups.hpp
+++ b/src/linux/cgroups.hpp
@@ -313,6 +313,21 @@ Try<Nothing> assign(
     pid_t pid);
 
 
+// Isolate a given process specified by it's 'pid' to a given cgroup
+// by both creating the cgroup (recursively) if it doesn't exist and
+// then assigning the process to that cgroup.
+//
+// @param   hierarchy   Path to the hierarchy root.
+// @param   cgroup      Path to the cgroup relative to the hierarchy root.
+// @param   pid         The pid of the given process.
+// @return  Nothing if the operation succeeds.
+//          Error if the operation fails.
+Try<Nothing> isolate(
+    const std::string& hierarchy,
+    const std::string& cgroup,
+    pid_t pid);
+
+
 namespace event {
 
 // Listen on an event notifier and return a future which will become ready when

http://git-wip-us.apache.org/repos/asf/mesos/blob/b7383c0d/src/slave/containerizer/mesos/linux_launcher.cpp
----------------------------------------------------------------------
diff --git a/src/slave/containerizer/mesos/linux_launcher.cpp b/src/slave/containerizer/mesos/linux_launcher.cpp
index 1f66220..19b54ae 100644
--- a/src/slave/containerizer/mesos/linux_launcher.cpp
+++ b/src/slave/containerizer/mesos/linux_launcher.cpp
@@ -220,47 +220,6 @@ Future<hashset<ContainerID>> LinuxLauncher::recover(
 }
 
 
-// A hook that is executed in the parent process. It attempts to move a process
-// into the freezer cgroup.
-//
-// NOTE: The child process is blocked by the hook infrastructure while
-// these hooks are executed.
-// NOTE: Returning an Error implies the child process will be killed.
-Try<Nothing> assignFreezerHierarchy(
-    pid_t child,
-    const string& hierarchy,
-    const string& cgroup)
-{
-  // Create a freezer cgroup for this container if necessary.
-  Try<bool> exists = cgroups::exists(hierarchy, cgroup);
-  if (exists.isError()) {
-    return Error("Failed to assign process to its freezer cgroup: "
-                 "Failed to check existence of freezer cgroup: " +
-                 exists.error());
-  }
-
-  if (!exists.get()) {
-    Try<Nothing> created = cgroups::create(hierarchy, cgroup);
-
-    if (created.isError()) {
-      return Error("Failed to assign process to its freezer cgroup: "
-                   "Failed to create freezer cgroup: " + created.error());
-    }
-  }
-
-  // Move the child into the freezer cgroup. Any grandchildren will
-  // also be contained in the cgroup.
-  Try<Nothing> assign = cgroups::assign(hierarchy, cgroup, child);
-
-  if (assign.isError()) {
-    return Error("Failed to assign process to its freezer cgroup: " +
-                 assign.error());
-  }
-
-  return Nothing();
-}
-
-
 Try<pid_t> LinuxLauncher::fork(
     const ContainerID& containerId,
     const string& path,
@@ -289,12 +248,14 @@ Try<pid_t> LinuxLauncher::fork(
         &systemd::mesos::extendLifetime));
   }
 
-  // Create parent Hook for moving child into freezer cgroup.
-  parentHooks.emplace_back(Subprocess::ParentHook(lambda::bind(
-      &assignFreezerHierarchy,
-      lambda::_1,
-      freezerHierarchy,
-      cgroup(containerId))));
+  // Create parent Hook for isolating child in a freezer cgroup (will
+  // also create the cgroup if necessary).
+  parentHooks.emplace_back(Subprocess::ParentHook([=](pid_t child) {
+    return cgroups::isolate(
+        freezerHierarchy,
+        cgroup(containerId),
+        child);
+  }));
 
   Try<Subprocess> child = subprocess(
       path,