use of org.apache.pulsar.common.nar.NarClassLoader in project pulsar by apache.
the class SourceApiV3ResourceTest method testUpdateSourceWithUrl.
@Test
public void testUpdateSourceWithUrl() throws Exception {
Configurator.setRootLevel(Level.DEBUG);
String filePackageUrl = getPulsarIOCassandraNar().toURI().toString();
SourceConfig sourceConfig = new SourceConfig();
sourceConfig.setTopicName(outputTopic);
sourceConfig.setSerdeClassName(outputSerdeClassName);
sourceConfig.setTenant(tenant);
sourceConfig.setNamespace(namespace);
sourceConfig.setName(source);
sourceConfig.setClassName(TWITTER_FIRE_HOSE);
sourceConfig.setParallelism(parallelism);
when(mockedManager.containsFunction(eq(tenant), eq(namespace), eq(source))).thenReturn(true);
mockStatic(ConnectorUtils.class, c -> {
});
mockStatic(ClassLoaderUtils.class, c -> {
});
mockStatic(FunctionCommon.class, ctx -> {
ctx.when(() -> FunctionCommon.extractFileFromPkgURL(any())).thenCallRealMethod();
ctx.when(() -> FunctionCommon.getClassLoaderFromPackage(any(), any(), any(), any())).thenCallRealMethod();
ctx.when(() -> FunctionCommon.getSourceType(argThat(clazz -> clazz.getName().equals(TWITTER_FIRE_HOSE)))).thenReturn(String.class);
ctx.when(() -> FunctionCommon.extractNarClassLoader(any(), any())).thenReturn(narClassLoader);
});
this.mockedFunctionMetaData = FunctionMetaData.newBuilder().setFunctionDetails(createDefaultFunctionDetails()).build();
when(mockedManager.getFunctionMetaData(any(), any(), any())).thenReturn(mockedFunctionMetaData);
resource.updateSource(tenant, namespace, source, null, null, filePackageUrl, sourceConfig, null, null, null);
}
use of org.apache.pulsar.common.nar.NarClassLoader in project pulsar by apache.
the class SinkApiV3ResourceTest method testRegisterSinkSuccessK8sNoUpload.
/*
Externally managed runtime,
uploadBuiltinSinksSources == false
Make sure uploadFileToBookkeeper is not called
*/
@Test
public void testRegisterSinkSuccessK8sNoUpload() throws Exception {
mockedWorkerService.getWorkerConfig().setUploadBuiltinSinksSources(false);
mockStatic(WorkerUtils.class, ctx -> {
ctx.when(() -> WorkerUtils.uploadFileToBookkeeper(anyString(), any(File.class), any(Namespace.class))).thenThrow(new RuntimeException("uploadFileToBookkeeper triggered"));
});
NarClassLoader mockedClassLoader = mock(NarClassLoader.class);
mockStatic(FunctionCommon.class, ctx -> {
ctx.when(() -> FunctionCommon.getSinkType(any())).thenReturn(String.class);
ctx.when(() -> FunctionCommon.getClassLoaderFromPackage(any(), any(), any(), any())).thenCallRealMethod();
ctx.when(() -> FunctionCommon.isFunctionCodeBuiltin(any())).thenReturn(true);
ctx.when(() -> FunctionCommon.isFunctionCodeBuiltin(any())).thenReturn(true);
ctx.when(() -> FunctionCommon.extractNarClassLoader(any(), any())).thenReturn(mockedClassLoader);
});
ConnectorsManager mockedConnManager = mock(ConnectorsManager.class);
Connector connector = Connector.builder().classLoader(mockedClassLoader).build();
when(mockedConnManager.getConnector("cassandra")).thenReturn(connector);
when(mockedWorkerService.getConnectorsManager()).thenReturn(mockedConnManager);
when(mockedRuntimeFactory.externallyManaged()).thenReturn(true);
when(mockedManager.containsFunction(eq(tenant), eq(namespace), eq(sink))).thenReturn(false);
SinkConfig sinkConfig = createDefaultSinkConfig();
sinkConfig.setArchive("builtin://cassandra");
try (FileInputStream inputStream = new FileInputStream(getPulsarIOCassandraNar())) {
resource.registerSink(tenant, namespace, sink, inputStream, mockedFormData, null, sinkConfig, null, null);
}
}
use of org.apache.pulsar.common.nar.NarClassLoader in project pulsar by apache.
the class ProxyExtensionsUtils method load.
/**
* Load the extension according to the handler definition.
*
* @param metadata the extension definition.
* @return
*/
static ProxyExtensionWithClassLoader load(ProxyExtensionMetadata metadata, String narExtractionDirectory) throws IOException {
final File narFile = metadata.getArchivePath().toAbsolutePath().toFile();
NarClassLoader ncl = NarClassLoaderBuilder.builder().narFile(narFile).parentClassLoader(ProxyExtension.class.getClassLoader()).extractionDirectory(narExtractionDirectory).build();
ProxyExtensionDefinition phDef = getProxyExtensionDefinition(ncl);
if (StringUtils.isBlank(phDef.getExtensionClass())) {
throw new IOException("extension `" + phDef.getName() + "` does NOT provide a protocol" + " handler implementation");
}
try {
Class extensionClass = ncl.loadClass(phDef.getExtensionClass());
Object extension = extensionClass.newInstance();
if (!(extension instanceof ProxyExtension)) {
throw new IOException("Class " + phDef.getExtensionClass() + " does not implement extension interface");
}
ProxyExtension ph = (ProxyExtension) extension;
return new ProxyExtensionWithClassLoader(ph, ncl);
} catch (Throwable t) {
rethrowIOException(t);
return null;
}
}
use of org.apache.pulsar.common.nar.NarClassLoader in project pulsar by apache.
the class ProxyExtensionWithClassLoaderTest method testWrapper.
@Test
public void testWrapper() throws Exception {
ProxyExtension h = mock(ProxyExtension.class);
NarClassLoader loader = mock(NarClassLoader.class);
ProxyExtensionWithClassLoader wrapper = new ProxyExtensionWithClassLoader(h, loader);
String protocol = "kafka";
when(h.extensionName()).thenReturn(protocol);
assertEquals(protocol, wrapper.extensionName());
verify(h, times(1)).extensionName();
when(h.accept(eq(protocol))).thenReturn(true);
assertTrue(wrapper.accept(protocol));
verify(h, times(1)).accept(same(protocol));
ProxyConfiguration conf = new ProxyConfiguration();
wrapper.initialize(conf);
verify(h, times(1)).initialize(same(conf));
ProxyService service = mock(ProxyService.class);
wrapper.start(service);
verify(h, times(1)).start(service);
}
use of org.apache.pulsar.common.nar.NarClassLoader in project pulsar by apache.
the class ProxyExtensionWithClassLoaderTest method testClassLoaderSwitcher.
@Test
public void testClassLoaderSwitcher() throws Exception {
NarClassLoader loader = mock(NarClassLoader.class);
String protocol = "test-protocol";
ProxyExtension h = new ProxyExtension() {
@Override
public String extensionName() {
assertEquals(Thread.currentThread().getContextClassLoader(), loader);
return protocol;
}
@Override
public boolean accept(String protocol) {
assertEquals(Thread.currentThread().getContextClassLoader(), loader);
return true;
}
@Override
public void initialize(ProxyConfiguration conf) throws Exception {
assertEquals(Thread.currentThread().getContextClassLoader(), loader);
throw new Exception("test exception");
}
@Override
public void start(ProxyService service) {
assertEquals(Thread.currentThread().getContextClassLoader(), loader);
}
@Override
public Map<InetSocketAddress, ChannelInitializer<SocketChannel>> newChannelInitializers() {
assertEquals(Thread.currentThread().getContextClassLoader(), loader);
return null;
}
@Override
public void close() {
assertEquals(Thread.currentThread().getContextClassLoader(), loader);
}
};
ProxyExtensionWithClassLoader wrapper = new ProxyExtensionWithClassLoader(h, loader);
ClassLoader curClassLoader = Thread.currentThread().getContextClassLoader();
assertEquals(wrapper.extensionName(), protocol);
assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
assertTrue(wrapper.accept(protocol));
assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
ProxyConfiguration conf = new ProxyConfiguration();
expectThrows(Exception.class, () -> wrapper.initialize(conf));
assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
ProxyService service = mock(ProxyService.class);
wrapper.start(service);
assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
assertNull(wrapper.newChannelInitializers());
assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
wrapper.close();
assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
}
Aggregations