CGroupsCpuResourceHandlerImpl implements CpuResourceHandler { @Override public List<PrivilegedOperation> bootstrap(Configuration conf) throws ResourceHandlerException { return bootstrap( ResourceCalculatorPlugin.getResourceCalculatorPlugin(null, conf), conf); } CGroupsCpuResourceHandlerImpl(CGroupsHandler cGroupsHandler); @Override List<PrivilegedOperation> bootstrap(Configuration conf); @InterfaceAudience.Private static boolean cpuLimitsExist(String path); @VisibleForTesting @InterfaceAudience.Private static int[] getOverallLimits(float yarnProcessors); @Override List<PrivilegedOperation> preStart(Container container); @Override List<PrivilegedOperation> reacquireContainer(ContainerId containerId); @Override List<PrivilegedOperation> updateContainer(Container container); @Override List<PrivilegedOperation> postComplete(ContainerId containerId); @Override List<PrivilegedOperation> teardown();  }
@Test public void testBootstrap() throws Exception { Configuration conf = new YarnConfiguration(); List<PrivilegedOperation> ret = cGroupsCpuResourceHandler.bootstrap(plugin, conf); verify(mockCGroupsHandler, times(1)) .initializeCGroupController(CGroupsHandler.CGroupController.CPU); verify(mockCGroupsHandler, times(0)) .updateCGroupParam(CGroupsHandler.CGroupController.CPU, "", CGroupsHandler.CGROUP_CPU_PERIOD_US, ""); verify(mockCGroupsHandler, times(0)) .updateCGroupParam(CGroupsHandler.CGroupController.CPU, "", CGroupsHandler.CGROUP_CPU_QUOTA_US, ""); Assert.assertNull(ret); }
@Test public void testBootstrapLimits() throws Exception { Configuration conf = new YarnConfiguration(); int cpuPerc = 80; conf.setInt(YarnConfiguration.NM_RESOURCE_PERCENTAGE_PHYSICAL_CPU_LIMIT, cpuPerc); int period = (CGroupsCpuResourceHandlerImpl.MAX_QUOTA_US * 100) / (cpuPerc * numProcessors); List<PrivilegedOperation> ret = cGroupsCpuResourceHandler.bootstrap(plugin, conf); verify(mockCGroupsHandler, times(1)) .initializeCGroupController(CGroupsHandler.CGroupController.CPU); verify(mockCGroupsHandler, times(1)) .updateCGroupParam(CGroupsHandler.CGroupController.CPU, "", CGroupsHandler.CGROUP_CPU_PERIOD_US, String.valueOf(period)); verify(mockCGroupsHandler, times(1)) .updateCGroupParam(CGroupsHandler.CGroupController.CPU, "", CGroupsHandler.CGROUP_CPU_QUOTA_US, String.valueOf(CGroupsCpuResourceHandlerImpl.MAX_QUOTA_US)); Assert.assertNull(ret); }
@Test public void testBootstrapExistingLimits() throws Exception { File existingLimit = new File(CGroupsHandler.CGroupController.CPU.getName() + "." + CGroupsHandler.CGROUP_CPU_QUOTA_US); try { FileUtils.write(existingLimit, "10000"); when(mockCGroupsHandler .getPathForCGroup(CGroupsHandler.CGroupController.CPU, "")) .thenReturn("."); Configuration conf = new YarnConfiguration(); List<PrivilegedOperation> ret = cGroupsCpuResourceHandler.bootstrap(plugin, conf); verify(mockCGroupsHandler, times(1)) .initializeCGroupController(CGroupsHandler.CGroupController.CPU); verify(mockCGroupsHandler, times(1)) .updateCGroupParam(CGroupsHandler.CGroupController.CPU, "", CGroupsHandler.CGROUP_CPU_QUOTA_US, "-1"); Assert.assertNull(ret); } finally { FileUtils.deleteQuietly(existingLimit); } }