Search in sources :

Example 1 with ExternalResourceDriverFactory

use of org.apache.flink.api.common.externalresource.ExternalResourceDriverFactory in project flink by apache.

the class ExternalResourceUtils method externalResourceDriversFromConfig.

/**
 * Instantiate the {@link ExternalResourceDriver ExternalResourceDrivers} for all of enabled
 * external resources. {@link ExternalResourceDriver ExternalResourceDrivers} are mapped to its
 * resource name.
 */
@VisibleForTesting
static Map<String, ExternalResourceDriver> externalResourceDriversFromConfig(Configuration config, PluginManager pluginManager) {
    final Set<String> resourceSet = getExternalResourceSet(config);
    if (resourceSet.isEmpty()) {
        return Collections.emptyMap();
    }
    final Iterator<ExternalResourceDriverFactory> factoryIterator = pluginManager.load(ExternalResourceDriverFactory.class);
    final Map<String, ExternalResourceDriverFactory> externalResourceFactories = new HashMap<>();
    factoryIterator.forEachRemaining(externalResourceDriverFactory -> externalResourceFactories.put(externalResourceDriverFactory.getClass().getName(), externalResourceDriverFactory));
    final Map<String, ExternalResourceDriver> externalResourceDrivers = new HashMap<>();
    for (String resourceName : resourceSet) {
        final ConfigOption<String> driverClassOption = key(ExternalResourceOptions.getExternalResourceDriverFactoryConfigOptionForResource(resourceName)).stringType().noDefaultValue();
        final String driverFactoryClassName = config.getString(driverClassOption);
        if (StringUtils.isNullOrWhitespaceOnly(driverFactoryClassName)) {
            LOG.warn("Could not find driver class name for {}. Please make sure {} is configured.", resourceName, driverClassOption.key());
            continue;
        }
        ExternalResourceDriverFactory externalResourceDriverFactory = externalResourceFactories.get(driverFactoryClassName);
        if (externalResourceDriverFactory != null) {
            DelegatingConfiguration delegatingConfiguration = new DelegatingConfiguration(config, ExternalResourceOptions.getExternalResourceParamConfigPrefixForResource(resourceName));
            try {
                externalResourceDrivers.put(resourceName, externalResourceDriverFactory.createExternalResourceDriver(delegatingConfiguration));
                LOG.info("Add external resources driver for {}.", resourceName);
            } catch (Exception e) {
                LOG.warn("Could not instantiate driver with factory {} for {}. {}", driverFactoryClassName, resourceName, e);
            }
        } else {
            LOG.warn("Could not find factory class {} for {}.", driverFactoryClassName, resourceName);
        }
    }
    return externalResourceDrivers;
}
Also used : DelegatingConfiguration(org.apache.flink.configuration.DelegatingConfiguration) HashMap(java.util.HashMap) ExternalResourceDriverFactory(org.apache.flink.api.common.externalresource.ExternalResourceDriverFactory) ExternalResourceDriver(org.apache.flink.api.common.externalresource.ExternalResourceDriver) VisibleForTesting(org.apache.flink.annotation.VisibleForTesting)

Aggregations

HashMap (java.util.HashMap)1 VisibleForTesting (org.apache.flink.annotation.VisibleForTesting)1 ExternalResourceDriver (org.apache.flink.api.common.externalresource.ExternalResourceDriver)1 ExternalResourceDriverFactory (org.apache.flink.api.common.externalresource.ExternalResourceDriverFactory)1 DelegatingConfiguration (org.apache.flink.configuration.DelegatingConfiguration)1