use of org.apache.pulsar.functions.utils.functions.Functions in project pulsar by apache.
the class LocalRunner method start.
public void start(boolean blocking) throws Exception {
List<RuntimeSpawner> local = new LinkedList<>();
synchronized (this) {
if (!running.compareAndSet(false, true)) {
throw new IllegalArgumentException("Pulsar Function local run already started!");
}
Runtime.getRuntime().addShutdownHook(shutdownHook);
Function.FunctionDetails functionDetails;
String userCodeFile = null;
int parallelism;
if (functionConfig != null) {
FunctionConfigUtils.inferMissingArguments(functionConfig, true);
parallelism = functionConfig.getParallelism();
if (functionConfig.getRuntime() == FunctionConfig.Runtime.JAVA) {
userCodeFile = functionConfig.getJar();
boolean isBuiltin = !StringUtils.isEmpty(functionConfig.getJar()) && functionConfig.getJar().startsWith(Utils.BUILTIN);
if (isBuiltin) {
WorkerConfig workerConfig = WorkerConfig.load(System.getenv("PULSAR_HOME") + "/conf/functions_worker.yml");
Functions functions = FunctionUtils.searchForFunctions(System.getenv("PULSAR_HOME") + workerConfig.getFunctionsDirectory().replaceFirst("^.", ""));
String functionType = functionConfig.getJar().replaceFirst("^builtin://", "");
userCodeFile = functions.getFunctions().get(functionType).toString();
}
if (Utils.isFunctionPackageUrlSupported(userCodeFile)) {
File file = FunctionCommon.extractFileFromPkgURL(userCodeFile);
userCodeClassLoader = FunctionConfigUtils.validate(functionConfig, file);
userCodeClassLoaderCreated = true;
} else if (userCodeFile != null) {
File file = new File(userCodeFile);
if (!file.exists()) {
throw new RuntimeException("User jar does not exist");
}
userCodeClassLoader = FunctionConfigUtils.validate(functionConfig, file);
userCodeClassLoaderCreated = true;
} else {
if (!(runtimeEnv == null || runtimeEnv == RuntimeEnv.THREAD)) {
throw new IllegalStateException("The jar property must be specified in FunctionConfig.");
}
FunctionConfigUtils.validateJavaFunction(functionConfig, Thread.currentThread().getContextClassLoader());
}
} else if (functionConfig.getRuntime() == FunctionConfig.Runtime.GO) {
userCodeFile = functionConfig.getGo();
} else if (functionConfig.getRuntime() == FunctionConfig.Runtime.PYTHON) {
userCodeFile = functionConfig.getPy();
} else {
throw new UnsupportedOperationException();
}
functionDetails = FunctionConfigUtils.convert(functionConfig, userCodeClassLoader != null ? userCodeClassLoader : Thread.currentThread().getContextClassLoader());
} else if (sourceConfig != null) {
inferMissingArguments(sourceConfig);
userCodeFile = sourceConfig.getArchive();
parallelism = sourceConfig.getParallelism();
ClassLoader builtInSourceClassLoader = userCodeFile != null ? isBuiltInSource(userCodeFile) : null;
if (builtInSourceClassLoader != null) {
functionDetails = SourceConfigUtils.convert(sourceConfig, SourceConfigUtils.validateAndExtractDetails(sourceConfig, builtInSourceClassLoader, true));
userCodeClassLoader = builtInSourceClassLoader;
} else if (userCodeFile != null && Utils.isFunctionPackageUrlSupported(userCodeFile)) {
File file = FunctionCommon.extractFileFromPkgURL(userCodeFile);
ClassLoader sourceClassLoader = FunctionCommon.getClassLoaderFromPackage(Function.FunctionDetails.ComponentType.SOURCE, sourceConfig.getClassName(), file, narExtractionDirectory);
functionDetails = SourceConfigUtils.convert(sourceConfig, SourceConfigUtils.validateAndExtractDetails(sourceConfig, sourceClassLoader, true));
userCodeClassLoader = sourceClassLoader;
userCodeClassLoaderCreated = true;
} else if (userCodeFile != null) {
File file = new File(userCodeFile);
if (!file.exists()) {
throw new RuntimeException("Source archive (" + userCodeFile + ") does not exist");
}
ClassLoader sourceClassLoader = FunctionCommon.getClassLoaderFromPackage(Function.FunctionDetails.ComponentType.SOURCE, sourceConfig.getClassName(), file, narExtractionDirectory);
functionDetails = SourceConfigUtils.convert(sourceConfig, SourceConfigUtils.validateAndExtractDetails(sourceConfig, sourceClassLoader, true));
userCodeClassLoader = sourceClassLoader;
userCodeClassLoaderCreated = true;
} else {
if (!(runtimeEnv == null || runtimeEnv == RuntimeEnv.THREAD)) {
throw new IllegalStateException("The archive property must be specified in SourceConfig.");
}
functionDetails = SourceConfigUtils.convert(sourceConfig, SourceConfigUtils.validateAndExtractDetails(sourceConfig, Thread.currentThread().getContextClassLoader(), true));
}
} else if (sinkConfig != null) {
inferMissingArguments(sinkConfig);
userCodeFile = sinkConfig.getArchive();
parallelism = sinkConfig.getParallelism();
ClassLoader builtInSinkClassLoader = userCodeFile != null ? isBuiltInSink(userCodeFile) : null;
if (builtInSinkClassLoader != null) {
functionDetails = SinkConfigUtils.convert(sinkConfig, SinkConfigUtils.validateAndExtractDetails(sinkConfig, builtInSinkClassLoader, true));
userCodeClassLoader = builtInSinkClassLoader;
} else if (Utils.isFunctionPackageUrlSupported(userCodeFile)) {
File file = FunctionCommon.extractFileFromPkgURL(userCodeFile);
ClassLoader sinkClassLoader = FunctionCommon.getClassLoaderFromPackage(Function.FunctionDetails.ComponentType.SINK, sinkConfig.getClassName(), file, narExtractionDirectory);
functionDetails = SinkConfigUtils.convert(sinkConfig, SinkConfigUtils.validateAndExtractDetails(sinkConfig, sinkClassLoader, true));
userCodeClassLoader = sinkClassLoader;
userCodeClassLoaderCreated = true;
} else if (userCodeFile != null) {
File file = new File(userCodeFile);
if (!file.exists()) {
throw new RuntimeException("Sink archive does not exist");
}
ClassLoader sinkClassLoader = FunctionCommon.getClassLoaderFromPackage(Function.FunctionDetails.ComponentType.SINK, sinkConfig.getClassName(), file, narExtractionDirectory);
functionDetails = SinkConfigUtils.convert(sinkConfig, SinkConfigUtils.validateAndExtractDetails(sinkConfig, sinkClassLoader, true));
userCodeClassLoader = sinkClassLoader;
userCodeClassLoaderCreated = true;
} else {
if (!(runtimeEnv == null || runtimeEnv == RuntimeEnv.THREAD)) {
throw new IllegalStateException("The archive property must be specified in SourceConfig.");
}
functionDetails = SinkConfigUtils.convert(sinkConfig, SinkConfigUtils.validateAndExtractDetails(sinkConfig, Thread.currentThread().getContextClassLoader(), true));
}
} else {
throw new IllegalArgumentException("Must specify Function, Source or Sink config");
}
if (System.getProperty(FunctionCacheEntry.JAVA_INSTANCE_JAR_PROPERTY) == null) {
System.setProperty(FunctionCacheEntry.JAVA_INSTANCE_JAR_PROPERTY, LocalRunner.class.getProtectionDomain().getCodeSource().getLocation().getFile());
}
AuthenticationConfig authConfig = AuthenticationConfig.builder().clientAuthenticationPlugin(clientAuthPlugin).clientAuthenticationParameters(clientAuthParams).useTls(useTls).tlsAllowInsecureConnection(tlsAllowInsecureConnection).tlsHostnameVerificationEnable(tlsHostNameVerificationEnabled).tlsTrustCertsFilePath(tlsTrustCertFilePath).build();
String serviceUrl = DEFAULT_SERVICE_URL;
if (brokerServiceUrl != null) {
serviceUrl = brokerServiceUrl;
}
if (webServiceUrl == null) {
webServiceUrl = DEFAULT_WEB_SERVICE_URL;
}
if ((sourceConfig != null || sinkConfig != null || functionConfig.getRuntime() == FunctionConfig.Runtime.JAVA) && (runtimeEnv == null || runtimeEnv == RuntimeEnv.THREAD)) {
// By default run java functions as threads
startThreadedMode(functionDetails, parallelism, instanceIdOffset, serviceUrl, stateStorageServiceUrl, authConfig, userCodeFile);
} else {
startProcessMode(functionDetails, parallelism, instanceIdOffset, serviceUrl, stateStorageServiceUrl, authConfig, userCodeFile);
}
local.addAll(spawners);
}
if (blocking) {
if (exitOnError) {
for (RuntimeSpawner spawner : local) {
spawner.join();
log.info("RuntimeSpawner quit because of", spawner.getRuntime().getDeathException());
}
close();
} else {
synchronized (this) {
while (running.get()) {
this.wait();
}
}
}
}
}
use of org.apache.pulsar.functions.utils.functions.Functions in project pulsar by yahoo.
the class ComponentImpl method getStreamingOutput.
private StreamingOutput getStreamingOutput(String pkgPath) {
final StreamingOutput streamingOutput = output -> {
if (pkgPath.startsWith(Utils.HTTP)) {
URL url = URI.create(pkgPath).toURL();
try (InputStream inputStream = url.openStream()) {
IOUtils.copy(inputStream, output);
}
} else if (pkgPath.startsWith(Utils.FILE)) {
URI url = URI.create(pkgPath);
File file = new File(url.getPath());
Files.copy(file.toPath(), output);
} else if (pkgPath.startsWith(Utils.BUILTIN) && !worker().getWorkerConfig().getUploadBuiltinSinksSources()) {
String sType = pkgPath.replaceFirst("^builtin://", "");
final String connectorsDir = worker().getWorkerConfig().getConnectorsDirectory();
log.warn("Processing package {} ; looking at the dir {}", pkgPath, connectorsDir);
Functions sinksOrSources = FunctionUtils.searchForFunctions(connectorsDir, true);
Path narPath = sinksOrSources.getFunctions().get(sType);
if (narPath == null) {
throw new IllegalStateException("Didn't find " + pkgPath + " in " + connectorsDir);
}
log.info("Loading {} from {}", pkgPath, narPath);
try (InputStream in = new FileInputStream(narPath.toString())) {
IOUtils.copy(in, output, 1024);
output.flush();
}
} else if (worker().getWorkerConfig().isFunctionsWorkerEnablePackageManagement()) {
try {
File file = downloadPackageFile(worker(), pkgPath);
try (InputStream in = new FileInputStream(file)) {
IOUtils.copy(in, output, 1024);
output.flush();
}
} catch (Exception e) {
log.error("Failed download package {} from packageMangment Service", pkgPath, e);
}
} else {
WorkerUtils.downloadFromBookkeeper(worker().getDlogNamespace(), output, pkgPath);
}
};
return streamingOutput;
}
use of org.apache.pulsar.functions.utils.functions.Functions in project pulsar by yahoo.
the class LocalRunner method start.
public void start(boolean blocking) throws Exception {
List<RuntimeSpawner> local = new LinkedList<>();
synchronized (this) {
if (!running.compareAndSet(false, true)) {
throw new IllegalArgumentException("Pulsar Function local run already started!");
}
Runtime.getRuntime().addShutdownHook(shutdownHook);
Function.FunctionDetails functionDetails;
String userCodeFile = null;
int parallelism;
if (functionConfig != null) {
FunctionConfigUtils.inferMissingArguments(functionConfig, true);
parallelism = functionConfig.getParallelism();
if (functionConfig.getRuntime() == FunctionConfig.Runtime.JAVA) {
userCodeFile = functionConfig.getJar();
boolean isBuiltin = !StringUtils.isEmpty(functionConfig.getJar()) && functionConfig.getJar().startsWith(Utils.BUILTIN);
if (isBuiltin) {
WorkerConfig workerConfig = WorkerConfig.load(System.getenv("PULSAR_HOME") + "/conf/functions_worker.yml");
Functions functions = FunctionUtils.searchForFunctions(System.getenv("PULSAR_HOME") + workerConfig.getFunctionsDirectory().replaceFirst("^.", ""));
String functionType = functionConfig.getJar().replaceFirst("^builtin://", "");
userCodeFile = functions.getFunctions().get(functionType).toString();
}
if (Utils.isFunctionPackageUrlSupported(userCodeFile)) {
File file = FunctionCommon.extractFileFromPkgURL(userCodeFile);
userCodeClassLoader = FunctionConfigUtils.validate(functionConfig, file);
userCodeClassLoaderCreated = true;
} else if (userCodeFile != null) {
File file = new File(userCodeFile);
if (!file.exists()) {
throw new RuntimeException("User jar does not exist");
}
userCodeClassLoader = FunctionConfigUtils.validate(functionConfig, file);
userCodeClassLoaderCreated = true;
} else {
if (!(runtimeEnv == null || runtimeEnv == RuntimeEnv.THREAD)) {
throw new IllegalStateException("The jar property must be specified in FunctionConfig.");
}
FunctionConfigUtils.validateJavaFunction(functionConfig, Thread.currentThread().getContextClassLoader());
}
} else if (functionConfig.getRuntime() == FunctionConfig.Runtime.GO) {
userCodeFile = functionConfig.getGo();
} else if (functionConfig.getRuntime() == FunctionConfig.Runtime.PYTHON) {
userCodeFile = functionConfig.getPy();
} else {
throw new UnsupportedOperationException();
}
functionDetails = FunctionConfigUtils.convert(functionConfig, userCodeClassLoader != null ? userCodeClassLoader : Thread.currentThread().getContextClassLoader());
} else if (sourceConfig != null) {
inferMissingArguments(sourceConfig);
userCodeFile = sourceConfig.getArchive();
parallelism = sourceConfig.getParallelism();
ClassLoader builtInSourceClassLoader = userCodeFile != null ? isBuiltInSource(userCodeFile) : null;
if (builtInSourceClassLoader != null) {
functionDetails = SourceConfigUtils.convert(sourceConfig, SourceConfigUtils.validateAndExtractDetails(sourceConfig, builtInSourceClassLoader, true));
userCodeClassLoader = builtInSourceClassLoader;
} else if (userCodeFile != null && Utils.isFunctionPackageUrlSupported(userCodeFile)) {
File file = FunctionCommon.extractFileFromPkgURL(userCodeFile);
ClassLoader sourceClassLoader = FunctionCommon.getClassLoaderFromPackage(Function.FunctionDetails.ComponentType.SOURCE, sourceConfig.getClassName(), file, narExtractionDirectory);
functionDetails = SourceConfigUtils.convert(sourceConfig, SourceConfigUtils.validateAndExtractDetails(sourceConfig, sourceClassLoader, true));
userCodeClassLoader = sourceClassLoader;
userCodeClassLoaderCreated = true;
} else if (userCodeFile != null) {
File file = new File(userCodeFile);
if (!file.exists()) {
throw new RuntimeException("Source archive (" + userCodeFile + ") does not exist");
}
ClassLoader sourceClassLoader = FunctionCommon.getClassLoaderFromPackage(Function.FunctionDetails.ComponentType.SOURCE, sourceConfig.getClassName(), file, narExtractionDirectory);
functionDetails = SourceConfigUtils.convert(sourceConfig, SourceConfigUtils.validateAndExtractDetails(sourceConfig, sourceClassLoader, true));
userCodeClassLoader = sourceClassLoader;
userCodeClassLoaderCreated = true;
} else {
if (!(runtimeEnv == null || runtimeEnv == RuntimeEnv.THREAD)) {
throw new IllegalStateException("The archive property must be specified in SourceConfig.");
}
functionDetails = SourceConfigUtils.convert(sourceConfig, SourceConfigUtils.validateAndExtractDetails(sourceConfig, Thread.currentThread().getContextClassLoader(), true));
}
} else if (sinkConfig != null) {
inferMissingArguments(sinkConfig);
userCodeFile = sinkConfig.getArchive();
parallelism = sinkConfig.getParallelism();
ClassLoader builtInSinkClassLoader = userCodeFile != null ? isBuiltInSink(userCodeFile) : null;
if (builtInSinkClassLoader != null) {
functionDetails = SinkConfigUtils.convert(sinkConfig, SinkConfigUtils.validateAndExtractDetails(sinkConfig, builtInSinkClassLoader, true));
userCodeClassLoader = builtInSinkClassLoader;
} else if (Utils.isFunctionPackageUrlSupported(userCodeFile)) {
File file = FunctionCommon.extractFileFromPkgURL(userCodeFile);
ClassLoader sinkClassLoader = FunctionCommon.getClassLoaderFromPackage(Function.FunctionDetails.ComponentType.SINK, sinkConfig.getClassName(), file, narExtractionDirectory);
functionDetails = SinkConfigUtils.convert(sinkConfig, SinkConfigUtils.validateAndExtractDetails(sinkConfig, sinkClassLoader, true));
userCodeClassLoader = sinkClassLoader;
userCodeClassLoaderCreated = true;
} else if (userCodeFile != null) {
File file = new File(userCodeFile);
if (!file.exists()) {
throw new RuntimeException("Sink archive does not exist");
}
ClassLoader sinkClassLoader = FunctionCommon.getClassLoaderFromPackage(Function.FunctionDetails.ComponentType.SINK, sinkConfig.getClassName(), file, narExtractionDirectory);
functionDetails = SinkConfigUtils.convert(sinkConfig, SinkConfigUtils.validateAndExtractDetails(sinkConfig, sinkClassLoader, true));
userCodeClassLoader = sinkClassLoader;
userCodeClassLoaderCreated = true;
} else {
if (!(runtimeEnv == null || runtimeEnv == RuntimeEnv.THREAD)) {
throw new IllegalStateException("The archive property must be specified in SourceConfig.");
}
functionDetails = SinkConfigUtils.convert(sinkConfig, SinkConfigUtils.validateAndExtractDetails(sinkConfig, Thread.currentThread().getContextClassLoader(), true));
}
} else {
throw new IllegalArgumentException("Must specify Function, Source or Sink config");
}
if (System.getProperty(FunctionCacheEntry.JAVA_INSTANCE_JAR_PROPERTY) == null) {
System.setProperty(FunctionCacheEntry.JAVA_INSTANCE_JAR_PROPERTY, LocalRunner.class.getProtectionDomain().getCodeSource().getLocation().getFile());
}
AuthenticationConfig authConfig = AuthenticationConfig.builder().clientAuthenticationPlugin(clientAuthPlugin).clientAuthenticationParameters(clientAuthParams).useTls(useTls).tlsAllowInsecureConnection(tlsAllowInsecureConnection).tlsHostnameVerificationEnable(tlsHostNameVerificationEnabled).tlsTrustCertsFilePath(tlsTrustCertFilePath).build();
String serviceUrl = DEFAULT_SERVICE_URL;
if (brokerServiceUrl != null) {
serviceUrl = brokerServiceUrl;
}
if (webServiceUrl == null) {
webServiceUrl = DEFAULT_WEB_SERVICE_URL;
}
if ((sourceConfig != null || sinkConfig != null || functionConfig.getRuntime() == FunctionConfig.Runtime.JAVA) && (runtimeEnv == null || runtimeEnv == RuntimeEnv.THREAD)) {
// By default run java functions as threads
startThreadedMode(functionDetails, parallelism, instanceIdOffset, serviceUrl, stateStorageServiceUrl, authConfig, userCodeFile);
} else {
startProcessMode(functionDetails, parallelism, instanceIdOffset, serviceUrl, stateStorageServiceUrl, authConfig, userCodeFile);
}
local.addAll(spawners);
}
if (blocking) {
if (exitOnError) {
for (RuntimeSpawner spawner : local) {
spawner.join();
log.info("RuntimeSpawner quit because of", spawner.getRuntime().getDeathException());
}
close();
} else {
synchronized (this) {
while (running.get()) {
this.wait();
}
}
}
}
}
use of org.apache.pulsar.functions.utils.functions.Functions in project incubator-pulsar by apache.
the class LocalRunner method start.
public void start(boolean blocking) throws Exception {
List<RuntimeSpawner> local = new LinkedList<>();
synchronized (this) {
if (!running.compareAndSet(false, true)) {
throw new IllegalArgumentException("Pulsar Function local run already started!");
}
Runtime.getRuntime().addShutdownHook(shutdownHook);
Function.FunctionDetails functionDetails;
String userCodeFile = null;
int parallelism;
if (functionConfig != null) {
FunctionConfigUtils.inferMissingArguments(functionConfig, true);
parallelism = functionConfig.getParallelism();
if (functionConfig.getRuntime() == FunctionConfig.Runtime.JAVA) {
userCodeFile = functionConfig.getJar();
boolean isBuiltin = !StringUtils.isEmpty(functionConfig.getJar()) && functionConfig.getJar().startsWith(Utils.BUILTIN);
if (isBuiltin) {
WorkerConfig workerConfig = WorkerConfig.load(System.getenv("PULSAR_HOME") + "/conf/functions_worker.yml");
Functions functions = FunctionUtils.searchForFunctions(System.getenv("PULSAR_HOME") + workerConfig.getFunctionsDirectory().replaceFirst("^.", ""));
String functionType = functionConfig.getJar().replaceFirst("^builtin://", "");
userCodeFile = functions.getFunctions().get(functionType).toString();
}
if (Utils.isFunctionPackageUrlSupported(userCodeFile)) {
File file = FunctionCommon.extractFileFromPkgURL(userCodeFile);
userCodeClassLoader = FunctionConfigUtils.validate(functionConfig, file);
userCodeClassLoaderCreated = true;
} else if (userCodeFile != null) {
File file = new File(userCodeFile);
if (!file.exists()) {
throw new RuntimeException("User jar does not exist");
}
userCodeClassLoader = FunctionConfigUtils.validate(functionConfig, file);
userCodeClassLoaderCreated = true;
} else {
if (!(runtimeEnv == null || runtimeEnv == RuntimeEnv.THREAD)) {
throw new IllegalStateException("The jar property must be specified in FunctionConfig.");
}
FunctionConfigUtils.validateJavaFunction(functionConfig, Thread.currentThread().getContextClassLoader());
}
} else if (functionConfig.getRuntime() == FunctionConfig.Runtime.GO) {
userCodeFile = functionConfig.getGo();
} else if (functionConfig.getRuntime() == FunctionConfig.Runtime.PYTHON) {
userCodeFile = functionConfig.getPy();
} else {
throw new UnsupportedOperationException();
}
functionDetails = FunctionConfigUtils.convert(functionConfig, userCodeClassLoader != null ? userCodeClassLoader : Thread.currentThread().getContextClassLoader());
} else if (sourceConfig != null) {
inferMissingArguments(sourceConfig);
userCodeFile = sourceConfig.getArchive();
parallelism = sourceConfig.getParallelism();
ClassLoader builtInSourceClassLoader = userCodeFile != null ? isBuiltInSource(userCodeFile) : null;
if (builtInSourceClassLoader != null) {
functionDetails = SourceConfigUtils.convert(sourceConfig, SourceConfigUtils.validateAndExtractDetails(sourceConfig, builtInSourceClassLoader, true));
userCodeClassLoader = builtInSourceClassLoader;
} else if (userCodeFile != null && Utils.isFunctionPackageUrlSupported(userCodeFile)) {
File file = FunctionCommon.extractFileFromPkgURL(userCodeFile);
ClassLoader sourceClassLoader = FunctionCommon.getClassLoaderFromPackage(Function.FunctionDetails.ComponentType.SOURCE, sourceConfig.getClassName(), file, narExtractionDirectory);
functionDetails = SourceConfigUtils.convert(sourceConfig, SourceConfigUtils.validateAndExtractDetails(sourceConfig, sourceClassLoader, true));
userCodeClassLoader = sourceClassLoader;
userCodeClassLoaderCreated = true;
} else if (userCodeFile != null) {
File file = new File(userCodeFile);
if (!file.exists()) {
throw new RuntimeException("Source archive (" + userCodeFile + ") does not exist");
}
ClassLoader sourceClassLoader = FunctionCommon.getClassLoaderFromPackage(Function.FunctionDetails.ComponentType.SOURCE, sourceConfig.getClassName(), file, narExtractionDirectory);
functionDetails = SourceConfigUtils.convert(sourceConfig, SourceConfigUtils.validateAndExtractDetails(sourceConfig, sourceClassLoader, true));
userCodeClassLoader = sourceClassLoader;
userCodeClassLoaderCreated = true;
} else {
if (!(runtimeEnv == null || runtimeEnv == RuntimeEnv.THREAD)) {
throw new IllegalStateException("The archive property must be specified in SourceConfig.");
}
functionDetails = SourceConfigUtils.convert(sourceConfig, SourceConfigUtils.validateAndExtractDetails(sourceConfig, Thread.currentThread().getContextClassLoader(), true));
}
} else if (sinkConfig != null) {
inferMissingArguments(sinkConfig);
userCodeFile = sinkConfig.getArchive();
parallelism = sinkConfig.getParallelism();
ClassLoader builtInSinkClassLoader = userCodeFile != null ? isBuiltInSink(userCodeFile) : null;
if (builtInSinkClassLoader != null) {
functionDetails = SinkConfigUtils.convert(sinkConfig, SinkConfigUtils.validateAndExtractDetails(sinkConfig, builtInSinkClassLoader, true));
userCodeClassLoader = builtInSinkClassLoader;
} else if (Utils.isFunctionPackageUrlSupported(userCodeFile)) {
File file = FunctionCommon.extractFileFromPkgURL(userCodeFile);
ClassLoader sinkClassLoader = FunctionCommon.getClassLoaderFromPackage(Function.FunctionDetails.ComponentType.SINK, sinkConfig.getClassName(), file, narExtractionDirectory);
functionDetails = SinkConfigUtils.convert(sinkConfig, SinkConfigUtils.validateAndExtractDetails(sinkConfig, sinkClassLoader, true));
userCodeClassLoader = sinkClassLoader;
userCodeClassLoaderCreated = true;
} else if (userCodeFile != null) {
File file = new File(userCodeFile);
if (!file.exists()) {
throw new RuntimeException("Sink archive does not exist");
}
ClassLoader sinkClassLoader = FunctionCommon.getClassLoaderFromPackage(Function.FunctionDetails.ComponentType.SINK, sinkConfig.getClassName(), file, narExtractionDirectory);
functionDetails = SinkConfigUtils.convert(sinkConfig, SinkConfigUtils.validateAndExtractDetails(sinkConfig, sinkClassLoader, true));
userCodeClassLoader = sinkClassLoader;
userCodeClassLoaderCreated = true;
} else {
if (!(runtimeEnv == null || runtimeEnv == RuntimeEnv.THREAD)) {
throw new IllegalStateException("The archive property must be specified in SourceConfig.");
}
functionDetails = SinkConfigUtils.convert(sinkConfig, SinkConfigUtils.validateAndExtractDetails(sinkConfig, Thread.currentThread().getContextClassLoader(), true));
}
} else {
throw new IllegalArgumentException("Must specify Function, Source or Sink config");
}
if (System.getProperty(FunctionCacheEntry.JAVA_INSTANCE_JAR_PROPERTY) == null) {
System.setProperty(FunctionCacheEntry.JAVA_INSTANCE_JAR_PROPERTY, LocalRunner.class.getProtectionDomain().getCodeSource().getLocation().getFile());
}
AuthenticationConfig authConfig = AuthenticationConfig.builder().clientAuthenticationPlugin(clientAuthPlugin).clientAuthenticationParameters(clientAuthParams).useTls(useTls).tlsAllowInsecureConnection(tlsAllowInsecureConnection).tlsHostnameVerificationEnable(tlsHostNameVerificationEnabled).tlsTrustCertsFilePath(tlsTrustCertFilePath).build();
String serviceUrl = DEFAULT_SERVICE_URL;
if (brokerServiceUrl != null) {
serviceUrl = brokerServiceUrl;
}
if (webServiceUrl == null) {
webServiceUrl = DEFAULT_WEB_SERVICE_URL;
}
if ((sourceConfig != null || sinkConfig != null || functionConfig.getRuntime() == FunctionConfig.Runtime.JAVA) && (runtimeEnv == null || runtimeEnv == RuntimeEnv.THREAD)) {
// By default run java functions as threads
startThreadedMode(functionDetails, parallelism, instanceIdOffset, serviceUrl, stateStorageServiceUrl, authConfig, userCodeFile);
} else {
startProcessMode(functionDetails, parallelism, instanceIdOffset, serviceUrl, stateStorageServiceUrl, authConfig, userCodeFile);
}
local.addAll(spawners);
}
if (blocking) {
if (exitOnError) {
for (RuntimeSpawner spawner : local) {
spawner.join();
log.info("RuntimeSpawner quit because of", spawner.getRuntime().getDeathException());
}
close();
} else {
synchronized (this) {
while (running.get()) {
this.wait();
}
}
}
}
}
use of org.apache.pulsar.functions.utils.functions.Functions in project incubator-pulsar by apache.
the class ComponentImpl method getStreamingOutput.
private StreamingOutput getStreamingOutput(String pkgPath) {
final StreamingOutput streamingOutput = output -> {
if (pkgPath.startsWith(Utils.HTTP)) {
URL url = URI.create(pkgPath).toURL();
try (InputStream inputStream = url.openStream()) {
IOUtils.copy(inputStream, output);
}
} else if (pkgPath.startsWith(Utils.FILE)) {
URI url = URI.create(pkgPath);
File file = new File(url.getPath());
Files.copy(file.toPath(), output);
} else if (pkgPath.startsWith(Utils.BUILTIN) && !worker().getWorkerConfig().getUploadBuiltinSinksSources()) {
String sType = pkgPath.replaceFirst("^builtin://", "");
final String connectorsDir = worker().getWorkerConfig().getConnectorsDirectory();
log.warn("Processing package {} ; looking at the dir {}", pkgPath, connectorsDir);
Functions sinksOrSources = FunctionUtils.searchForFunctions(connectorsDir, true);
Path narPath = sinksOrSources.getFunctions().get(sType);
if (narPath == null) {
throw new IllegalStateException("Didn't find " + pkgPath + " in " + connectorsDir);
}
log.info("Loading {} from {}", pkgPath, narPath);
try (InputStream in = new FileInputStream(narPath.toString())) {
IOUtils.copy(in, output, 1024);
output.flush();
}
} else if (worker().getWorkerConfig().isFunctionsWorkerEnablePackageManagement()) {
try {
File file = downloadPackageFile(worker(), pkgPath);
try (InputStream in = new FileInputStream(file)) {
IOUtils.copy(in, output, 1024);
output.flush();
}
} catch (Exception e) {
log.error("Failed download package {} from packageMangment Service", pkgPath, e);
}
} else {
WorkerUtils.downloadFromBookkeeper(worker().getDlogNamespace(), output, pkgPath);
}
};
return streamingOutput;
}
Aggregations