use of org.apache.flink.util.DynamicCodeLoadingException in project flink by apache.
the class CheckpointStorageLoader method fromConfig.
/**
* Loads the checkpoint storage from the configuration, from the parameter
* 'state.checkpoint-storage', as defined in {@link CheckpointingOptions#CHECKPOINT_STORAGE}.
*
* <p>The implementation can be specified either via their shortcut name, or via the class name
* of a {@link CheckpointStorageFactory}. If a CheckpointStorageFactory class name is specified,
* the factory is instantiated (via its zero-argument constructor) and its {@link
* CheckpointStorageFactory#createFromConfig(ReadableConfig, ClassLoader)} method is called.
*
* <p>Recognized shortcut names are '{@value #JOB_MANAGER_STORAGE_NAME}', and '{@value
* #FILE_SYSTEM_STORAGE_NAME}'.
*
* @param config The configuration to load the checkpoint storage from
* @param classLoader The class loader that should be used to load the checkpoint storage
* @param logger Optionally, a logger to log actions to (may be null)
* @return The instantiated checkpoint storage.
* @throws DynamicCodeLoadingException Thrown if a checkpoint storage factory is configured and
* the factory class was not found or the factory could not be instantiated
* @throws IllegalConfigurationException May be thrown by the CheckpointStorageFactory when
* creating / configuring the checkpoint storage in the factory
*/
public static Optional<CheckpointStorage> fromConfig(ReadableConfig config, ClassLoader classLoader, @Nullable Logger logger) throws IllegalStateException, DynamicCodeLoadingException {
Preconditions.checkNotNull(config, "config");
Preconditions.checkNotNull(classLoader, "classLoader");
final String storageName = config.get(CheckpointingOptions.CHECKPOINT_STORAGE);
if (storageName == null) {
if (logger != null) {
logger.debug("The configuration {} has not be set in the current" + " sessions flink-conf.yaml. Falling back to a default CheckpointStorage" + " type. Users are strongly encouraged explicitly set this configuration" + " so they understand how their applications are checkpointing" + " snapshots for fault-tolerance.", CheckpointingOptions.CHECKPOINT_STORAGE.key());
}
return Optional.empty();
}
switch(storageName.toLowerCase()) {
case JOB_MANAGER_STORAGE_NAME:
return Optional.of(createJobManagerCheckpointStorage(config, classLoader, logger));
case FILE_SYSTEM_STORAGE_NAME:
return Optional.of(createFileSystemCheckpointStorage(config, classLoader, logger));
default:
if (logger != null) {
logger.info("Loading state backend via factory '{}'", storageName);
}
CheckpointStorageFactory<?> factory;
try {
@SuppressWarnings("rawtypes") Class<? extends CheckpointStorageFactory> clazz = Class.forName(storageName, false, classLoader).asSubclass(CheckpointStorageFactory.class);
factory = clazz.newInstance();
} catch (ClassNotFoundException e) {
throw new DynamicCodeLoadingException("Cannot find configured state backend factory class: " + storageName, e);
} catch (ClassCastException | InstantiationException | IllegalAccessException e) {
throw new DynamicCodeLoadingException("The class configured under '" + CheckpointingOptions.CHECKPOINT_STORAGE.key() + "' is not a valid checkpoint storage factory (" + storageName + ')', e);
}
return Optional.of(factory.createFromConfig(config, classLoader));
}
}
use of org.apache.flink.util.DynamicCodeLoadingException in project flink by apache.
the class StateBackendLoadingTest method testLoadingFails.
// ------------------------------------------------------------------------
// Failures
// ------------------------------------------------------------------------
/**
* This test makes sure that failures properly manifest when the state backend could not be
* loaded.
*/
@Test
public void testLoadingFails() throws Exception {
final Configuration config = new Configuration();
// try a value that is neither recognized as a name, nor corresponds to a class
config.setString(backendKey, "does.not.exist");
try {
StateBackendLoader.fromApplicationOrConfigOrDefault(null, TernaryBoolean.UNDEFINED, config, cl, null);
fail("should fail with an exception");
} catch (DynamicCodeLoadingException ignored) {
// expected
}
// try a class that is not a factory
config.setString(backendKey, java.io.File.class.getName());
try {
StateBackendLoader.fromApplicationOrConfigOrDefault(null, TernaryBoolean.UNDEFINED, config, cl, null);
fail("should fail with an exception");
} catch (DynamicCodeLoadingException ignored) {
// expected
}
// a factory that fails
config.setString(backendKey, FailingFactory.class.getName());
try {
StateBackendLoader.fromApplicationOrConfigOrDefault(null, TernaryBoolean.UNDEFINED, config, cl, null);
fail("should fail with an exception");
} catch (IOException ignored) {
// expected
}
}
use of org.apache.flink.util.DynamicCodeLoadingException in project flink by apache.
the class CheckpointStorageLoaderTest method testLoadingFails.
@Test
public void testLoadingFails() throws Exception {
final Configuration config = new Configuration();
config.set(CheckpointingOptions.CHECKPOINT_STORAGE, "does.not.exist");
try {
CheckpointStorageLoader.load(null, null, new ModernStateBackend(), config, cl, log);
Assert.fail("should fail with exception");
} catch (DynamicCodeLoadingException e) {
// expected
}
// try a class that is not a factory
config.set(CheckpointingOptions.CHECKPOINT_STORAGE, java.io.File.class.getName());
try {
CheckpointStorageLoader.load(null, null, new ModernStateBackend(), config, cl, log);
Assert.fail("should fail with exception");
} catch (DynamicCodeLoadingException e) {
// expected
}
// try a factory that fails
config.set(CheckpointingOptions.CHECKPOINT_STORAGE, FailingFactory.class.getName());
try {
CheckpointStorageLoader.load(null, null, new ModernStateBackend(), config, cl, log);
Assert.fail("should fail with exception");
} catch (IllegalConfigurationException e) {
// expected
}
}
Aggregations