Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ public Optional<T> createOptionalPluginInstance(PluginIdentifier pluginIdentifie

@Override
public Optional<T> createOptionalPluginInstance(
PluginIdentifier pluginIdentifier, Collection<URL> pluginJars) {
PluginIdentifier pluginIdentifier, Collection<URL> additionalPluginJars) {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
T pluginInstance = loadPluginInstance(pluginIdentifier, classLoader);
if (pluginInstance != null) {
Expand All @@ -230,19 +230,14 @@ public Optional<T> createOptionalPluginInstance(
// use current thread classloader to avoid different classloader load same class
// error.
addURLToClassLoaderConsumer.accept(classLoader, pluginJarPaths.get());
addURLToClassLoaderConsumer.accept(classLoader, (List<URL>) pluginJars);
addURLToClassLoaderConsumer.accept(classLoader, (List<URL>) additionalPluginJars);
} catch (Exception e) {
log.warn(
"can't load jar use current thread classloader, use URLClassLoader instead now."
+ " message: "
+ e.getMessage());
URL[] urls = new URL[pluginJars.size() + 1];
int i = 0;
for (URL pluginJar : pluginJars) {
urls[i++] = pluginJar;
}
urls =
Stream.concat(Arrays.stream(urls), pluginJarPaths.get().stream())
URL[] urls =
Stream.concat(additionalPluginJars.stream(), pluginJarPaths.get().stream())
.distinct()
.toArray(URL[]::new);
classLoader =
Expand All @@ -262,8 +257,9 @@ public Optional<T> createOptionalPluginInstance(
}

@Override
public T createPluginInstance(PluginIdentifier pluginIdentifier, Collection<URL> pluginJars) {
Optional<T> instance = createOptionalPluginInstance(pluginIdentifier, pluginJars);
public T createPluginInstance(
PluginIdentifier pluginIdentifier, Collection<URL> additionalPluginJars) {
Optional<T> instance = createOptionalPluginInstance(pluginIdentifier, additionalPluginJars);
if (instance.isPresent()) {
return instance.get();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,11 @@ public interface PluginDiscovery<T> {
* Get plugin instance by plugin identifier.
*
* @param pluginIdentifier plugin identifier.
* @param pluginJars used to help plugin load
* @param additionalPluginJars additional jars supplied by the caller to help the plugin load,
* as opposed to the plugin jars discovered from the plugin directory.
* @return plugin instance. If not found, throw IllegalArgumentException.
*/
T createPluginInstance(PluginIdentifier pluginIdentifier, Collection<URL> pluginJars);
T createPluginInstance(PluginIdentifier pluginIdentifier, Collection<URL> additionalPluginJars);

/**
* Get plugin instance by plugin identifier.
Expand All @@ -80,11 +81,12 @@ public interface PluginDiscovery<T> {
* Get plugin instance by plugin identifier.
*
* @param pluginIdentifier plugin identifier.
* @param pluginJars used to help plugin load
* @param additionalPluginJars additional jars supplied by the caller to help the plugin load,
* as opposed to the plugin jars discovered from the plugin directory.
* @return plugin instance. If not found, return Optional.empty().
*/
Optional<T> createOptionalPluginInstance(
PluginIdentifier pluginIdentifier, Collection<URL> pluginJars);
PluginIdentifier pluginIdentifier, Collection<URL> additionalPluginJars);

/**
* Get all plugin instances.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@

package org.apache.seatunnel.plugin.discovery;

import org.apache.seatunnel.shade.com.typesafe.config.Config;
import org.apache.seatunnel.shade.com.typesafe.config.ConfigFactory;

import org.apache.seatunnel.api.common.PluginIdentifier;
import org.apache.seatunnel.api.common.PluginIdentifierInterface;
import org.apache.seatunnel.common.config.Common;
import org.apache.seatunnel.common.config.DeployMode;
import org.apache.seatunnel.common.constants.PluginType;
Expand All @@ -27,15 +31,28 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URLClassLoader;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystemException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;

public class AbstractPluginDiscoveryTest {

private String originSeatunnelHome = null;
private DeployMode originMode = null;
private ClassLoader originContextClassLoader = null;
private static final String seatunnelHome;
private static final Path fallbackWorkDir =
Paths.get(System.getProperty("user.dir"), "target", "fallback-plugin");

static {
String rootModuleDir = "seatunnel-plugin-discovery";
Expand All @@ -59,6 +76,7 @@ public void before() {
Common.setDeployMode(DeployMode.CLIENT);
originSeatunnelHome = Common.getSeaTunnelHome();
Common.setSeaTunnelHome(seatunnelHome);
originContextClassLoader = Thread.currentThread().getContextClassLoader();
}

@Test
Expand All @@ -72,9 +90,113 @@ public void testGetAllPlugins() {
Assertions.assertEquals(35, sinkPlugins.size());
}

/**
* When the context classloader is not a {@link URLClassLoader}, the default consumer throws
* {@link UnsupportedOperationException} (JDK 9+ application classloader behaves like that) and
* the discovery must fall back to building its own {@link URLClassLoader}. The fallback array
* used to contain a trailing {@code null} which made the {@code URLClassLoader} constructor
* throw a {@link NullPointerException}, so no plugin could ever be loaded through it.
*/
@Test
public void testCreateOptionalPluginInstanceFallbackWithoutUrlClassLoader() throws Exception {
Path pluginDir = fallbackWorkDir.resolve("connectors");
Files.createDirectories(pluginDir);

// The plugin jar only has to exist so that discovery resolves a plugin jar path.
Path fallbackPluginJar = pluginDir.resolve("connector-fallback.jar");
try (JarOutputStream ignored =
new JarOutputStream(Files.newOutputStream(fallbackPluginJar))) {
// A valid but empty jar is enough, the service descriptor is provided below.
}

// The additional jar carries the service descriptor while the implementation class
// itself is visible from the test classloader.
Path additionalJar = fallbackWorkDir.resolve("fallback-additional.jar");
try (OutputStream outputStream = Files.newOutputStream(additionalJar);
JarOutputStream jarOutputStream = new JarOutputStream(outputStream)) {
jarOutputStream.putNextEntry(
new JarEntry("META-INF/services/" + FallbackPlugin.class.getName()));
jarOutputStream.write(
FallbackPluginImpl.class.getName().getBytes(StandardCharsets.UTF_8));
jarOutputStream.closeEntry();
}

Config pluginMappingConfig =
ConfigFactory.parseString("seatunnel { source { Fallback = connector-fallback } }");
TestPluginDiscovery pluginDiscovery =
new TestPluginDiscovery(pluginDir, pluginMappingConfig);

ClassLoader plainClassLoader =
new ClassLoader(Thread.currentThread().getContextClassLoader()) {};
Assertions.assertFalse(plainClassLoader instanceof URLClassLoader);
Thread.currentThread().setContextClassLoader(plainClassLoader);
try {
Optional<FallbackPlugin> plugin =
pluginDiscovery.createOptionalPluginInstance(
PluginIdentifier.of(
"seatunnel", PluginType.SOURCE.getType(), "Fallback"),
Collections.singletonList(additionalJar.toUri().toURL()));
Assertions.assertTrue(plugin.isPresent());
Assertions.assertEquals(FallbackPluginImpl.class, plugin.get().getClass());
Assertions.assertEquals("Fallback", plugin.get().getPluginName());
} finally {
Thread.currentThread().setContextClassLoader(originContextClassLoader);
}
}

@AfterEach
public void after() {
public void after() throws IOException {
Thread.currentThread().setContextClassLoader(originContextClassLoader);
Common.setSeaTunnelHome(originSeatunnelHome);
Common.setDeployMode(originMode);
deleteIfPossible(fallbackWorkDir.resolve("connectors").resolve("connector-fallback.jar"));
deleteIfPossible(fallbackWorkDir.resolve("fallback-additional.jar"));
deleteIfPossible(fallbackWorkDir.resolve("connectors"));
deleteIfPossible(fallbackWorkDir);
}

/**
* Best-effort removal of the artifacts written by {@link
* #testCreateOptionalPluginInstanceFallbackWithoutUrlClassLoader()}.
*
* <p>On Windows a jar can not be deleted while a {@link URLClassLoader} still holds it open.
* The fallback loader is created locally in {@code AbstractPluginDiscovery} and is not exposed,
* so the test can not close it and {@link Files#deleteIfExists(Path)} fails with "The process
* cannot access the file because it is being used by another process". The artifacts only live
* under {@code target/}, are recreated on every run and are removed by {@code mvn clean}, so a
* leftover jar is harmless. Only that Windows file lock is ignored here, any other {@link
* IOException} is still propagated.
*/
private static void deleteIfPossible(Path path) throws IOException {
try {
Files.deleteIfExists(path);
} catch (FileSystemException e) {
// Expected on Windows while the fallback classloader keeps the jar open, see above.
}
}

/** Test plugin interface, acts as the plugin base class of the test discovery. */
public interface FallbackPlugin extends PluginIdentifierInterface {}

/** Test plugin implementation loaded through the fallback URLClassLoader. */
public static class FallbackPluginImpl implements FallbackPlugin {

@Override
public String getPluginName() {
return "Fallback";
}
}

/** Plugin discovery used to exercise the fallback of the default URL consumer. */
static class TestPluginDiscovery extends AbstractPluginDiscovery<FallbackPlugin> {

TestPluginDiscovery(Path pluginDir, Config pluginMappingConfig) {
super(pluginDir, pluginMappingConfig);
}

@Override
protected Class<FallbackPlugin> getPluginBaseClass() {
return FallbackPlugin.class;
}
}
}