Repository navigation
Description
Describe the bug
By default, Pulsar Functions runtime extracts files of a Pulsar Function implementation jar file to a directory /tmp/pulsar-nar/[function-jar-file-name].jar-unpacked where the "function-jar-file-name" part is determined used a logic which doesn't produce unique file name.
When the Pulsar Function is installed from a URL source over http/https, the filename seems to always be the short name of the package for the implementation class.
For example for a function class com.github.lhotari.pulsar.RandomFilterFunction, the filename becomes pulsar.jar. This filename would be the same for all function classes in the same package, regardless of the implementation classname or it's version.
The logic is here:
Lines 548 to 572 in 3fcfb22
| private static String getDownloadFileName(FunctionDetails FunctionDetails, | |
| Function.PackageLocationMetaData packageLocation) { | |
| if (!org.apache.commons.lang.StringUtils.isEmpty(packageLocation.getOriginalFileName())) { | |
| return packageLocation.getOriginalFileName(); | |
| } | |
| String[] hierarchy = FunctionDetails.getClassName().split("\\."); | |
| String fileName; | |
| if (hierarchy.length <= 0) { | |
| fileName = FunctionDetails.getClassName(); | |
| } else if (hierarchy.length == 1) { | |
| fileName = hierarchy[0]; | |
| } else { | |
| fileName = hierarchy[hierarchy.length - 2]; | |
| } | |
| switch (FunctionDetails.getRuntime()) { | |
| case JAVA: | |
| return fileName + ".jar"; | |
| case PYTHON: | |
| return fileName + ".py"; | |
| case GO: | |
| return fileName + ".go"; | |
| default: | |
| throw new RuntimeException("Unknown runtime " + FunctionDetails.getRuntime()); | |
| } | |
| } |
The logic for extracting the Nar file is here:
pulsar/pulsar-common/src/main/java/org/apache/pulsar/common/nar/NarUnpacker.java
Lines 60 to 85 in c2e5ec6
| public static File unpackNar(final File nar, final File baseWorkingDirectory) throws IOException { | |
| final File narWorkingDirectory = new File(baseWorkingDirectory, nar.getName() + "-unpacked"); | |
| // if the working directory doesn't exist, unpack the nar | |
| if (!narWorkingDirectory.exists()) { | |
| unpack(nar, narWorkingDirectory, calculateMd5sum(nar)); | |
| } else { | |
| // the working directory does exist. Run MD5 sum against the nar | |
| // file and check if the nar has changed since it was deployed. | |
| final byte[] narMd5 = calculateMd5sum(nar); | |
| final File workingHashFile = new File(narWorkingDirectory, HASH_FILENAME); | |
| if (!workingHashFile.exists()) { | |
| FileUtils.deleteFile(narWorkingDirectory, true); | |
| unpack(nar, narWorkingDirectory, narMd5); | |
| } else { | |
| final byte[] hashFileContents = Files.readAllBytes(workingHashFile.toPath()); | |
| if (!Arrays.equals(hashFileContents, narMd5)) { | |
| log.info("Contents of nar {} have changed. Reloading.", nar.getAbsolutePath()); | |
| FileUtils.deleteFile(narWorkingDirectory, true); | |
| unpack(nar, narWorkingDirectory, narMd5); | |
| } | |
| } | |
| } | |
| return narWorkingDirectory; | |
| } |
As it can be seen from this code, it leads to collisions when there are multiple jar files with the same name.
Another problem can happen when 2 threads are in a race in extracting the nar file.
It can lead to an error message such as this one in the Pulsar Functions instance log file:
07:20:38.336 [public/default/random-1-2] INFO org.apache.pulsar.functions.instance.JavaInstanceRunnable - Load JAR: /tmp/pulsar_functions/public/default/random-1/2/pulsar.jar
07:20:38.378 [public/default/random-1-2] WARN org.apache.pulsar.common.nar.NarClassLoader - /tmp/pulsar-nar/pulsar.jar-unpacked does not contain META-INF/bundled-dependencies!
07:20:38.378 [public/default/random-1-2] INFO org.apache.pulsar.functions.instance.JavaInstanceRunnable - Initialize function class loader for function random-1 at function cache manager, functionClassLoader: org.apache.pulsar.common.nar.NarClassLoader[/tmp/pulsar-nar/pulsar.jar-unpacked]
07:20:38.381 [public/default/random-1-2] ERROR org.apache.pulsar.functions.instance.JavaInstanceRunnable - [public/default/random-1:2] Uncaught exception in Java Instance
java.lang.RuntimeException: User class must be in class path
at org.apache.pulsar.common.util.Reflections.createInstance(Reflections.java:112) ~[java-instance.jar:?]
at org.apache.pulsar.functions.instance.JavaInstanceRunnable.setup(JavaInstanceRunnable.java:197) ~[org.apache.pulsar-pulsar-functions-instance-2.7.2.1.1.0.jar:2.7.2.1.1.0]
at org.apache.pulsar.functions.instance.JavaInstanceRunnable.run(JavaInstanceRunnable.java:235) ~[org.apache.pulsar-pulsar-functions-instance-2.7.2.1.1.0.jar:2.7.2.1.1.0]
at java.lang.Thread.run(Thread.java:829) ~[?:?]
Caused by: java.lang.ClassNotFoundException: com.github.lhotari.pulsar.RandomFilterFunction
at java.net.URLClassLoader.findClass(URLClassLoader.java:471) ~[?:?]
at java.lang.ClassLoader.loadClass(ClassLoader.java:589) ~[?:?]
at java.lang.ClassLoader.loadClass(ClassLoader.java:522) ~[?:?]
at java.lang.Class.forName0(Native Method) ~[?:?]
at java.lang.Class.forName(Class.java:398) ~[?:?]
at org.apache.pulsar.common.util.Reflections.createInstance(Reflections.java:110) ~[java-instance.jar:?]
... 3 more
07:20:38.390 [public/default/random-1-2] INFO org.apache.pulsar.functions.instance.JavaInstanceRunnable - Closing instance
To Reproduce
Steps to reproduce the behavior:
Install a function with process runtime with parallelism > brokers/function workers. This can reproduce the race condition when /tmp/pulsar-nar doesn't get include the [function-jar-file-name].jar-unpacked directory. It might require a few attempts to reproduce the issue.
For example:
pulsar-admin functions create --tenant public --namespace default --name random-1 --parallelism 8 --inputs persistent://public/default/random-1-in --output persistent://amplitude/processing/random-2-in --jar https://github.com/lhotari/pulsar-issue-6054-repro/releases/download/v1.0/random-filter-function-1.0-SNAPSHOT.jar --ram 209715200 --classname com.github.lhotari.pulsar.RandomFilterFunction
Expected behavior
Pulsar Functions loading shouldn't mix different function implementations by extracting them to the same directory. Race conditions should also be handled when the same function is started with multiple instances on the same node.
Activity
[Issue 11340] Fix concurrency issues in NarUnpacker (#11343)
[Issue 11340] Fix concurrency issues in NarUnpacker (#11343)
[Issue 11340] Fix concurrency issues in NarUnpacker (apache#11343)