This is an automated email from the ASF dual-hosted git repository.

bmahler pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mesos.git


The following commit(s) were added to refs/heads/master by this push:
     new 87e377e0e [cgroups2] Prevent containerId from prepending cgroup root 
during recovery.
87e377e0e is described below

commit 87e377e0e6b04441bfe9108e9a06e0f7e3608887
Author: Jason Zhou <[email protected]>
AuthorDate: Fri Aug 9 13:55:42 2024 -0400

    [cgroups2] Prevent containerId from prepending cgroup root during recovery.
    
    Currently, CgroupsIsolatorTest.ROOT_CGROUPS_PERF_PerfForward fails
    during recovery because it cannot create the directory for the recovered
    container. This happens because the original containerId, when
    recovered, includes the cgroup root. The function that converts a cgroup
    to a containerId does not ignore the cgroup root, even though we do not
    expect it to be included.
    
    To fix this, we will remove the first token of the cgroup if it matches
    the cgroup root. This will prevent attaching an extraneous cgroup root
    to the containerId when parsing it from a cgroup.
    
    Review: https://reviews.apache.org/r/75156/
---
 src/slave/containerizer/mesos/paths.cpp            | 22 ++++++++++++++++++++++
 src/slave/containerizer/mesos/paths.hpp            |  3 +++
 .../mesos_containerizer_paths_tests.cpp            | 15 +++++++++++++++
 3 files changed, 40 insertions(+)

diff --git a/src/slave/containerizer/mesos/paths.cpp 
b/src/slave/containerizer/mesos/paths.cpp
index 126950279..444ee0020 100644
--- a/src/slave/containerizer/mesos/paths.cpp
+++ b/src/slave/containerizer/mesos/paths.cpp
@@ -690,15 +690,37 @@ Option<ContainerID> containerId(
   vector<string> tokens =
     strings::tokenize(path, stringify(os::PATH_SEPARATOR));
 
+  vector<string> root_tokens =
+    strings::tokenize(root, stringify(os::PATH_SEPARATOR));
+
   if (tokens.size() == 0) {
     // Root.
     return None();
   }
+
   if (tokens.back() == "agent") {
     // Mesos Agent.
     return None();
   }
 
+  bool starts_with_root = [&]() {
+    if (tokens.size() < root_tokens.size()) {
+      return false;
+    }
+
+    for (int i = 0; i < root_tokens.size(); ++i) {
+      if (root_tokens[i] != tokens[i]) {
+        return false;
+      }
+    }
+
+    return true;
+  }();
+
+  if (starts_with_root) {
+    tokens.erase(tokens.begin(), tokens.begin() + root_tokens.size());
+  }
+
   Option<ContainerID> current;
   foreach (const string& token, tokens) {
     if (token == CGROUP_SEPARATOR) {
diff --git a/src/slave/containerizer/mesos/paths.hpp 
b/src/slave/containerizer/mesos/paths.hpp
index 6acd72363..8bf36a271 100644
--- a/src/slave/containerizer/mesos/paths.hpp
+++ b/src/slave/containerizer/mesos/paths.hpp
@@ -326,6 +326,9 @@ std::string container(
 //
 // Leaf paths (which end in `/leaf`) and non-leaf paths will resolve to the
 // same container id.
+// If the passed cgroup includes the root, it will be removed.
+// e.g. if root is root/mesos, and the cgroup is root/mesos/id1, the result
+// will be id1
 Option<ContainerID> containerId(
     const std::string& root,
     const std::string& cgroup);
diff --git a/src/tests/containerizer/mesos_containerizer_paths_tests.cpp 
b/src/tests/containerizer/mesos_containerizer_paths_tests.cpp
index 45afc8465..dbe64784f 100644
--- a/src/tests/containerizer/mesos_containerizer_paths_tests.cpp
+++ b/src/tests/containerizer/mesos_containerizer_paths_tests.cpp
@@ -196,6 +196,21 @@ TEST(MesosContainerizerPathsTest, 
CGROUPS2_Cgroups2ParsePaths)
   EXPECT_SOME_ID_EQ(
       id1,
       cgroups2::containerId("mesos", cgroups2::container("mesos", id1, true)));
+
+  EXPECT_SOME_ID_EQ(
+      id1,
+      cgroups2::containerId(
+          "root", cgroups2::container("root", id1, true)));
+
+  EXPECT_SOME_ID_EQ(
+      id1,
+      cgroups2::containerId(
+          "test/root", cgroups2::container("test/root", id1, true)));
+
+  EXPECT_SOME_ID_EQ(
+      id3,
+      cgroups2::containerId(
+          "test/root", "test/root/id1/mesos/id2/mesos/id3"));
 }
 
 } // namespace tests {

Reply via email to