ResourcePluginManager { public void initializePluggableDevicePlugins(Context context, Configuration configuration, Map<String, ResourcePlugin> pluginMap) throws YarnRuntimeException, ClassNotFoundException { LOG.info("The pluggable device framework enabled," + "trying to load the vendor plugins"); if (null == deviceMappingManager) { LOG.debug("DeviceMappingManager initialized."); deviceMappingManager = new DeviceMappingManager(context); } String[] pluginClassNames = configuration.getStrings( YarnConfiguration.NM_PLUGGABLE_DEVICE_FRAMEWORK_DEVICE_CLASSES); if (null == pluginClassNames) { throw new YarnRuntimeException("Null value found in configuration: " + YarnConfiguration.NM_PLUGGABLE_DEVICE_FRAMEWORK_DEVICE_CLASSES); } for (String pluginClassName : pluginClassNames) { Class<?> pluginClazz = Class.forName(pluginClassName); if (!DevicePlugin.class.isAssignableFrom(pluginClazz)) { throw new YarnRuntimeException("Class: " + pluginClassName + " not instance of " + DevicePlugin.class.getCanonicalName()); } checkInterfaceCompatibility(DevicePlugin.class, pluginClazz); DevicePlugin dpInstance = (DevicePlugin) ReflectionUtils.newInstance( pluginClazz, configuration); DeviceRegisterRequest request = null; try { request = dpInstance.getRegisterRequestInfo(); } catch (Exception e) { throw new YarnRuntimeException("Exception thrown from plugin's" + " getRegisterRequestInfo:" + e.getMessage()); } String resourceName = request.getResourceName(); if (pluginMap.containsKey(resourceName)) { throw new YarnRuntimeException(resourceName + " already registered! Please change resource type name" + " or configure correct resource type name" + " in resource-types.xml for " + pluginClassName); } if (!isConfiguredResourceName(resourceName)) { throw new YarnRuntimeException(resourceName + " is not configured inside " + YarnConfiguration.RESOURCE_TYPES_CONFIGURATION_FILE + " , please configure it first"); } LOG.info("New resource type: {} registered successfully by {}", resourceName, pluginClassName); DevicePluginAdapter pluginAdapter = new DevicePluginAdapter( resourceName, dpInstance, deviceMappingManager); LOG.info("Adapter of {} created. Initializing..", pluginClassName); try { pluginAdapter.initialize(context); } catch (YarnException e) { throw new YarnRuntimeException("Adapter of " + pluginClassName + " init failed!"); } LOG.info("Adapter of {} init success!", pluginClassName); pluginMap.put(request.getResourceName(), pluginAdapter); if (dpInstance instanceof DevicePluginScheduler) { checkInterfaceCompatibility(DevicePluginScheduler.class, pluginClazz); LOG.info( "{} can schedule {} devices." + "Added as preferred device plugin scheduler", pluginClassName, resourceName); deviceMappingManager.addDevicePluginScheduler( resourceName, (DevicePluginScheduler) dpInstance); } } }  void initialize(Context context); void initializePluggableDevicePlugins(Context context,
      Configuration configuration,
      Map<String, ResourcePlugin> pluginMap); @VisibleForTesting // Check if the implemented interfaces' signature is compatible void checkInterfaceCompatibility(Class<?> expectedClass,
      Class<?> actualClass); @VisibleForTesting boolean isConfiguredResourceName(String resourceName); @VisibleForTesting void setDeviceMappingManager(
      DeviceMappingManager deviceMappingManager); DeviceMappingManager getDeviceMappingManager(); void cleanup(); synchronized Map<String, ResourcePlugin> getNameToPlugins();  }
@Test(timeout = 30000) public void testInitializationWithPluggableDeviceFrameworkEnabled2() throws ClassNotFoundException { ResourcePluginManager rpm = new ResourcePluginManager(); ResourcePluginManager rpmSpy = spy(rpm); nm = new ResourcePluginMockNM(rpmSpy); boolean fail = false; try { conf.setBoolean(YarnConfiguration.NM_PLUGGABLE_DEVICE_FRAMEWORK_ENABLED, true); nm.init(conf); nm.start(); } catch (YarnRuntimeException e) { fail = true; } catch (Exception ignored) { } verify(rpmSpy).initializePluggableDevicePlugins( any(Context.class), any(Configuration.class), anyMap()); assertThat(fail).isTrue(); }