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 5c7c87cfb [paths] Simplify paths::cgroups2::containerId() logic.
5c7c87cfb is described below

commit 5c7c87cfbf334eb380f03e19b1daee620649ec37
Author: Jason Zhou <[email protected]>
AuthorDate: Fri Aug 9 18:01:09 2024 -0400

    [paths] Simplify paths::cgroups2::containerId() logic.
    
    Currently we are tokenizing the cgroup argument when we could be
    directly operating on the string using our strings utility helpers
    instead.
    
    This patch replaces the tokenizing logic to using the strings library.
    
    Review: https://reviews.apache.org/r/75160/
---
 src/slave/containerizer/mesos/paths.cpp | 29 +++++------------------------
 1 file changed, 5 insertions(+), 24 deletions(-)

diff --git a/src/slave/containerizer/mesos/paths.cpp 
b/src/slave/containerizer/mesos/paths.cpp
index 444ee0020..7fd4d7336 100644
--- a/src/slave/containerizer/mesos/paths.cpp
+++ b/src/slave/containerizer/mesos/paths.cpp
@@ -682,17 +682,16 @@ Option<ContainerID> containerId(
     const string& root,
     const string& cgroup)
 {
-  // Remove the trailing `/leaf`, if present.
-  string path = strings::remove(cgroup, "/leaf", strings::SUFFIX);
   // Remove leading or trailing slashes.
-  path = strings::trim(path, "/");
+  string path = strings::trim(cgroup, "/");
+  // Remove the trailing `/leaf`, if present.
+  path = strings::remove(path, "/leaf", strings::SUFFIX);
+  // Remove the leading root, if present
+  path = strings::remove(path, strings::trim(root, "/"), strings::PREFIX);
 
   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();
@@ -703,24 +702,6 @@ Option<ContainerID> containerId(
     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) {

Reply via email to