-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Master Issue: #6003 ### Motivation This pull request implements the possibility to add builtin functions (in the same way of the build in connectors). The builtin function must include a `pulsar-io.yml` file with the following content ```yml name: <function-name> description: <function-desciption> functionClass: <function-class> ``` e.g. ```yml name: test-function description: test function description functionClass: it.oncode.pulsar.functions.TestFunction ``` it is possible to create a builtin function in the same way of the builtin sinks/sources. Example in scala ```scala val functionConfigBuilder: FunctionConfigBuilder = FunctionConfig.builder() val function = functionConfigBuilder .tenant("public") .namespace("default") .jar("builtin://test-function") .name("test-function-name") .className("it.oncode.pulsar.functions.TestFunction") .inputs(Seq("channel_in").asJava) .output("channel_out") .runtime(FunctionConfig.Runtime.JAVA) .build() Pulsar.admin.functions .createFunction(function, null) ``` Function folder to be specified in the `conf/functions_worker.yml` conf file e.g. `functionsDirectory: ./functions` Function package must be in `*.nar` format like for source/sink connectors ### Modifications I modified the `pulsar-function-utils`, `pulsar-functions-worker` and `pulsar-common` modules on the basis of the built in connectors implementation. Also `Function.proto` has been modified in order to include the `builtin` property #### What this MR does not include - modification of pulsar-admin to fetch the available buildin functions - the related documentation This is a feature that is critical for us, I think we could open an issue for the remaining points and consider to merge this PR.
- Loading branch information
Showing
19 changed files
with
364 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
pulsar-common/src/main/java/org/apache/pulsar/common/functions/FunctionDefinition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.pulsar.common.functions; | ||
|
||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
/** | ||
* Basic information about a Pulsar function. | ||
*/ | ||
@Data | ||
@NoArgsConstructor | ||
public class FunctionDefinition { | ||
|
||
/** | ||
* The name of the function type. | ||
*/ | ||
private String name; | ||
|
||
/** | ||
* Description to be used for user help. | ||
*/ | ||
private String description; | ||
|
||
/** | ||
* The class name for the function implementation. | ||
* | ||
* <p>If not defined, it will be assumed this function cannot act as a data. | ||
*/ | ||
private String functionClass; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
...ctions/utils/src/main/java/org/apache/pulsar/functions/utils/functions/FunctionUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.pulsar.functions.utils.functions; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.DirectoryStream; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.Collections; | ||
|
||
import lombok.experimental.UtilityClass; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.pulsar.common.functions.FunctionDefinition; | ||
import org.apache.pulsar.common.nar.NarClassLoader; | ||
import org.apache.pulsar.common.util.ObjectMapperFactory; | ||
import org.apache.pulsar.functions.utils.Exceptions; | ||
import org.apache.pulsar.functions.api.Function; | ||
|
||
|
||
@UtilityClass | ||
@Slf4j | ||
public class FunctionUtils { | ||
|
||
private static final String PULSAR_IO_SERVICE_NAME = "pulsar-io.yaml"; | ||
|
||
/** | ||
* Extract the Pulsar Function class from a functionctor archive. | ||
*/ | ||
public static String getFunctionClass(ClassLoader classLoader) throws IOException { | ||
NarClassLoader ncl = (NarClassLoader) classLoader; | ||
String configStr = ncl.getServiceDefinition(PULSAR_IO_SERVICE_NAME); | ||
|
||
FunctionDefinition conf = ObjectMapperFactory.getThreadLocalYaml().readValue(configStr, | ||
FunctionDefinition.class); | ||
if (StringUtils.isEmpty(conf.getFunctionClass())) { | ||
throw new IOException( | ||
String.format("The '%s' functionctor does not provide a function implementation", conf.getName())); | ||
} | ||
|
||
try { | ||
// Try to load source class and check it implements Function interface | ||
Class functionClass = ncl.loadClass(conf.getFunctionClass()); | ||
if (!(Function.class.isAssignableFrom(functionClass))) { | ||
throw new IOException( | ||
"Class " + conf.getFunctionClass() + " does not implement interface " + Function.class.getName()); | ||
} | ||
} catch (Throwable t) { | ||
Exceptions.rethrowIOException(t); | ||
} | ||
|
||
return conf.getFunctionClass(); | ||
} | ||
|
||
public static FunctionDefinition getFunctionDefinition(String narPath) throws IOException { | ||
try (NarClassLoader ncl = NarClassLoader.getFromArchive(new File(narPath), Collections.emptySet())) { | ||
String configStr = ncl.getServiceDefinition(PULSAR_IO_SERVICE_NAME); | ||
return ObjectMapperFactory.getThreadLocalYaml().readValue(configStr, FunctionDefinition.class); | ||
} | ||
} | ||
|
||
public static Functions searchForFunctions(String functionsDirectory) throws IOException { | ||
Path path = Paths.get(functionsDirectory).toAbsolutePath(); | ||
log.info("Searching for functions in {}", path); | ||
|
||
Functions functions = new Functions(); | ||
|
||
if (!path.toFile().exists()) { | ||
log.warn("Functions archive directory not found"); | ||
return functions; | ||
} | ||
|
||
try (DirectoryStream<Path> stream = Files.newDirectoryStream(path, "*.nar")) { | ||
for (Path archive : stream) { | ||
try { | ||
FunctionDefinition cntDef = FunctionUtils.getFunctionDefinition(archive.toString()); | ||
log.info("Found function {} from {}", cntDef, archive); | ||
log.error(cntDef.getName()); | ||
log.error(cntDef.getFunctionClass()); | ||
if (!StringUtils.isEmpty(cntDef.getFunctionClass())) { | ||
functions.functions.put(cntDef.getName(), archive); | ||
} | ||
|
||
functions.functionsDefinitions.add(cntDef); | ||
} catch (Throwable t) { | ||
log.warn("Failed to load function from {}", archive, t); | ||
} | ||
} | ||
} | ||
|
||
Collections.sort(functions.functionsDefinitions, | ||
(c1, c2) -> String.CASE_INSENSITIVE_ORDER.compare(c1.getName(), c2.getName())); | ||
|
||
return functions; | ||
} | ||
} |
Oops, something went wrong.