diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/linux/resources/CGroupsHandlerImpl.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/linux/resources/CGroupsHandlerImpl.java index 050d0a80306..1b2c7804bde 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/linux/resources/CGroupsHandlerImpl.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/linux/resources/CGroupsHandlerImpl.java @@ -87,9 +87,10 @@ class CGroupsHandlerImpl implements CGroupsHandler { CGroupsHandlerImpl(Configuration conf, PrivilegedOperationExecutor privilegedOperationExecutor, String mtab) throws ResourceHandlerException { + // Remove leading and trialing slash(es) this.cGroupPrefix = conf.get(YarnConfiguration. NM_LINUX_CONTAINER_CGROUPS_HIERARCHY, "/hadoop-yarn") - .replaceAll("^/", "").replaceAll("$/", ""); + .replaceAll("^/+", "").replaceAll("/+$", ""); this.enableCGroupMount = conf.getBoolean(YarnConfiguration. NM_LINUX_CONTAINER_CGROUPS_MOUNT, false); this.cGroupMountPath = conf.get(YarnConfiguration. diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/test/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/linux/resources/TestCGroupsHandlerImpl.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/test/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/linux/resources/TestCGroupsHandlerImpl.java index ea6fb529a37..70badaf4ef4 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/test/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/linux/resources/TestCGroupsHandlerImpl.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/test/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/linux/resources/TestCGroupsHandlerImpl.java @@ -598,4 +598,42 @@ public class TestCGroupsHandlerImpl { FileUtils.deleteQuietly(cpu); } } + + // Remove leading and trailing slashes + @Test + public void testCgroupsHierarchySetting() throws ResourceHandlerException { + YarnConfiguration conf = new YarnConfiguration(); + conf.set(YarnConfiguration.NM_LINUX_CONTAINER_CGROUPS_MOUNT_PATH, tmpPath); + conf.set(YarnConfiguration.NM_LINUX_CONTAINER_CGROUPS_HIERARCHY, + "/hadoop-yarn"); + CGroupsHandlerImpl cGroupsHandler = new CGroupsHandlerImpl(conf, null); + String expectedRelativePath = "hadoop-yarn/c1"; + Assert.assertEquals(expectedRelativePath, + cGroupsHandler.getRelativePathForCGroup("c1")); + + conf.set(YarnConfiguration.NM_LINUX_CONTAINER_CGROUPS_HIERARCHY, + "hadoop-yarn"); + cGroupsHandler = new CGroupsHandlerImpl(conf, null); + Assert.assertEquals(expectedRelativePath, + cGroupsHandler.getRelativePathForCGroup("c1")); + + conf.set(YarnConfiguration.NM_LINUX_CONTAINER_CGROUPS_HIERARCHY, + "hadoop-yarn/"); + cGroupsHandler = new CGroupsHandlerImpl(conf, null); + Assert.assertEquals(expectedRelativePath, + cGroupsHandler.getRelativePathForCGroup("c1")); + + conf.set(YarnConfiguration.NM_LINUX_CONTAINER_CGROUPS_HIERARCHY, + "//hadoop-yarn//"); + cGroupsHandler = new CGroupsHandlerImpl(conf, null); + Assert.assertEquals(expectedRelativePath, + cGroupsHandler.getRelativePathForCGroup("c1")); + + expectedRelativePath = "hadoop-yarn/root/c1"; + conf.set(YarnConfiguration.NM_LINUX_CONTAINER_CGROUPS_HIERARCHY, + "//hadoop-yarn/root//"); + cGroupsHandler = new CGroupsHandlerImpl(conf, null); + Assert.assertEquals(expectedRelativePath, + cGroupsHandler.getRelativePathForCGroup("c1")); + } } \ No newline at end of file