CGroupsCpuResourceHandlerImpl implements CpuResourceHandler { @Override public List<PrivilegedOperation> preStart(Container container) throws ResourceHandlerException { String cgroupId = container.getContainerId().toString(); cGroupsHandler.createCGroup(CPU, cgroupId); updateContainer(container); List<PrivilegedOperation> ret = new ArrayList<>(); ret.add(new PrivilegedOperation( PrivilegedOperation.OperationType.ADD_PID_TO_CGROUP, PrivilegedOperation.CGROUP_ARG_PREFIX + cGroupsHandler .getPathForCGroupTasks(CPU, cgroupId))); return ret; } 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 testPreStart() throws Exception { String id = "container_01_01"; String path = "test-path/" + id; ContainerId mockContainerId = mock(ContainerId.class); when(mockContainerId.toString()).thenReturn(id); Container mockContainer = mock(Container.class); when(mockContainer.getContainerId()).thenReturn(mockContainerId); when(mockCGroupsHandler .getPathForCGroupTasks(CGroupsHandler.CGroupController.CPU, id)) .thenReturn(path); when(mockContainer.getResource()).thenReturn(Resource.newInstance(1024, 2)); List<PrivilegedOperation> ret = cGroupsCpuResourceHandler.preStart(mockContainer); verify(mockCGroupsHandler, times(1)) .createCGroup(CGroupsHandler.CGroupController.CPU, id); verify(mockCGroupsHandler, times(1)) .updateCGroupParam(CGroupsHandler.CGroupController.CPU, id, CGroupsHandler.CGROUP_CPU_SHARES, String .valueOf(CGroupsCpuResourceHandlerImpl.CPU_DEFAULT_WEIGHT * 2)); verify(mockCGroupsHandler, never()) .updateCGroupParam(eq(CGroupsHandler.CGroupController.CPU), eq(id), eq(CGroupsHandler.CGROUP_CPU_PERIOD_US), anyString()); verify(mockCGroupsHandler, never()) .updateCGroupParam(eq(CGroupsHandler.CGroupController.CPU), eq(id), eq(CGroupsHandler.CGROUP_CPU_QUOTA_US), anyString()); Assert.assertNotNull(ret); Assert.assertEquals(1, ret.size()); PrivilegedOperation op = ret.get(0); Assert.assertEquals(PrivilegedOperation.OperationType.ADD_PID_TO_CGROUP, op.getOperationType()); List<String> args = op.getArguments(); Assert.assertEquals(1, args.size()); Assert.assertEquals(PrivilegedOperation.CGROUP_ARG_PREFIX + path, args.get(0)); }