|
@@ -44,6 +44,7 @@ import org.apache.hadoop.yarn.server.nodemanager.containermanager.resourceplugin
|
|
|
import org.apache.hadoop.yarn.server.nodemanager.containermanager.runtime.ContainerRuntimeConstants;
|
|
|
import org.apache.hadoop.yarn.server.nodemanager.recovery.NMMemoryStateStoreService;
|
|
|
import org.apache.hadoop.yarn.server.nodemanager.recovery.NMStateStoreService;
|
|
|
+import org.apache.hadoop.yarn.server.nodemanager.webapp.dao.NMDeviceResourceInfo;
|
|
|
import org.apache.hadoop.yarn.util.resource.ResourceUtils;
|
|
|
import org.apache.hadoop.yarn.util.resource.TestResourceUtils;
|
|
|
import org.junit.After;
|
|
@@ -517,6 +518,72 @@ public class TestDevicePluginAdapter {
|
|
|
return c;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Ensure correct return value generated.
|
|
|
+ * */
|
|
|
+ @Test
|
|
|
+ public void testNMResourceInfoRESTAPI() throws IOException, YarnException {
|
|
|
+ NodeManager.NMContext context = mock(NodeManager.NMContext.class);
|
|
|
+ NMStateStoreService storeService = mock(NMStateStoreService.class);
|
|
|
+ when(context.getNMStateStore()).thenReturn(storeService);
|
|
|
+ doNothing().when(storeService).storeAssignedResources(isA(Container.class),
|
|
|
+ isA(String.class),
|
|
|
+ isA(ArrayList.class));
|
|
|
+
|
|
|
+ // Init scheduler manager
|
|
|
+ DeviceMappingManager dmm = new DeviceMappingManager(context);
|
|
|
+
|
|
|
+ ResourcePluginManager rpm = mock(ResourcePluginManager.class);
|
|
|
+ when(rpm.getDeviceMappingManager()).thenReturn(dmm);
|
|
|
+
|
|
|
+ // Init an plugin
|
|
|
+ MyPlugin plugin = new MyPlugin();
|
|
|
+ MyPlugin spyPlugin = spy(plugin);
|
|
|
+ String resourceName = MyPlugin.RESOURCE_NAME;
|
|
|
+ // Init an adapter for the plugin
|
|
|
+ DevicePluginAdapter adapter = new DevicePluginAdapter(
|
|
|
+ resourceName,
|
|
|
+ spyPlugin, dmm);
|
|
|
+ // Bootstrap, adding device
|
|
|
+ adapter.initialize(context);
|
|
|
+ adapter.createResourceHandler(context,
|
|
|
+ mockCGroupsHandler, mockPrivilegedExecutor);
|
|
|
+ adapter.getDeviceResourceHandler().bootstrap(conf);
|
|
|
+ int size = dmm.getAvailableDevices(resourceName);
|
|
|
+ Assert.assertEquals(3, size);
|
|
|
+
|
|
|
+ // A container c1 requests 1 device
|
|
|
+ Container c1 = mockContainerWithDeviceRequest(0,
|
|
|
+ resourceName,
|
|
|
+ 1, false);
|
|
|
+ // preStart
|
|
|
+ adapter.getDeviceResourceHandler().preStart(c1);
|
|
|
+ // check book keeping
|
|
|
+ Assert.assertEquals(2,
|
|
|
+ dmm.getAvailableDevices(resourceName));
|
|
|
+ Assert.assertEquals(1,
|
|
|
+ dmm.getAllUsedDevices().get(resourceName).size());
|
|
|
+ Assert.assertEquals(3,
|
|
|
+ dmm.getAllAllowedDevices().get(resourceName).size());
|
|
|
+ // get REST return value
|
|
|
+ NMDeviceResourceInfo response =
|
|
|
+ (NMDeviceResourceInfo) adapter.getNMResourceInfo();
|
|
|
+ Assert.assertEquals(1, response.getAssignedDevices().size());
|
|
|
+ Assert.assertEquals(3, response.getTotalDevices().size());
|
|
|
+ Device device = response.getAssignedDevices().get(0).getDevice();
|
|
|
+ String cId = response.getAssignedDevices().get(0).getContainerId();
|
|
|
+ Assert.assertTrue(dmm.getAllAllowedDevices().get(resourceName)
|
|
|
+ .contains(device));
|
|
|
+ Assert.assertTrue(dmm.getAllUsedDevices().get(resourceName)
|
|
|
+ .containsValue(ContainerId.fromString(cId)));
|
|
|
+ //finish container
|
|
|
+ adapter.getDeviceResourceHandler().postComplete(getContainerId(0));
|
|
|
+ response =
|
|
|
+ (NMDeviceResourceInfo) adapter.getNMResourceInfo();
|
|
|
+ Assert.assertEquals(0, response.getAssignedDevices().size());
|
|
|
+ Assert.assertEquals(3, response.getTotalDevices().size());
|
|
|
+ }
|
|
|
+
|
|
|
private static ContainerId getContainerId(int id) {
|
|
|
return ContainerId.newContainerId(ApplicationAttemptId
|
|
|
.newInstance(ApplicationId.newInstance(1234L, 1), 1), id);
|