CGroupsMemoryResourceHandlerImpl implements MemoryResourceHandler { @Override public List<PrivilegedOperation> bootstrap(Configuration conf) throws ResourceHandlerException { boolean pmemEnabled = conf.getBoolean(YarnConfiguration.NM_PMEM_CHECK_ENABLED, YarnConfiguration.DEFAULT_NM_PMEM_CHECK_ENABLED); boolean vmemEnabled = conf.getBoolean(YarnConfiguration.NM_VMEM_CHECK_ENABLED, YarnConfiguration.DEFAULT_NM_VMEM_CHECK_ENABLED); if (pmemEnabled || vmemEnabled) { String msg = "The default YARN physical and/or virtual memory health" + " checkers as well as the CGroups memory controller are enabled. " + "If you wish to use the Cgroups memory controller, please turn off" + " the default physical/virtual memory checkers by setting " + YarnConfiguration.NM_PMEM_CHECK_ENABLED + " and " + YarnConfiguration.NM_VMEM_CHECK_ENABLED + " to false."; throw new ResourceHandlerException(msg); } this.cGroupsHandler.mountCGroupController(MEMORY); swappiness = conf .getInt(YarnConfiguration.NM_MEMORY_RESOURCE_CGROUPS_SWAPPINESS, YarnConfiguration.DEFAULT_NM_MEMORY_RESOURCE_CGROUPS_SWAPPINESS); if (swappiness < 0 || swappiness > 100) { throw new ResourceHandlerException( "Illegal value '" + swappiness + "' for " + YarnConfiguration.NM_MEMORY_RESOURCE_CGROUPS_SWAPPINESS + ". Value must be between 0 and 100."); } float softLimitPerc = conf.getFloat( YarnConfiguration.NM_MEMORY_RESOURCE_CGROUPS_SOFT_LIMIT_PERCENTAGE, YarnConfiguration.DEFAULT_NM_MEMORY_RESOURCE_CGROUPS_SOFT_LIMIT_PERCENTAGE); softLimit = softLimitPerc / 100.0f; if (softLimitPerc < 0.0f || softLimitPerc > 100.0f) { throw new ResourceHandlerException( "Illegal value '" + softLimitPerc + "' " + YarnConfiguration.NM_MEMORY_RESOURCE_CGROUPS_SOFT_LIMIT_PERCENTAGE + ". Value must be between 0 and 100."); } return null; } CGroupsMemoryResourceHandlerImpl(CGroupsHandler cGroupsHandler); @Override List<PrivilegedOperation> bootstrap(Configuration conf); @Override List<PrivilegedOperation> reacquireContainer(ContainerId containerId); @Override List<PrivilegedOperation> preStart(Container container); @Override List<PrivilegedOperation> postComplete(ContainerId containerId); @Override List<PrivilegedOperation> teardown();  }
@Test public void testBootstrap() throws Exception { Configuration conf = new YarnConfiguration(); conf.setBoolean(YarnConfiguration.NM_PMEM_CHECK_ENABLED, false); conf.setBoolean(YarnConfiguration.NM_VMEM_CHECK_ENABLED, false); List<PrivilegedOperation> ret = cGroupsMemoryResourceHandler.bootstrap(conf); verify(mockCGroupsHandler, times(1)) .mountCGroupController(CGroupsHandler.CGroupController.MEMORY); Assert.assertNull(ret); Assert.assertEquals("Default swappiness value incorrect", 0, cGroupsMemoryResourceHandler.getSwappiness()); conf.setBoolean(YarnConfiguration.NM_PMEM_CHECK_ENABLED, true); try { cGroupsMemoryResourceHandler.bootstrap(conf); Assert.fail("Pmem check should not be allowed to run with cgroups"); } catch(ResourceHandlerException re) { } conf.setBoolean(YarnConfiguration.NM_PMEM_CHECK_ENABLED, false); conf.setBoolean(YarnConfiguration.NM_VMEM_CHECK_ENABLED, true); try { cGroupsMemoryResourceHandler.bootstrap(conf); Assert.fail("Vmem check should not be allowed to run with cgroups"); } catch(ResourceHandlerException re) { } }