Skip to content

Extracted Pulsar Function jar file content in /tmp/pulsar-nar directory gets corrupted or overridden by different content #11340

Closed

Description

@lhotari

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:

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:

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

  1. added
    type/bug
    on Jul 16, 2021
  2. added a commit that references this issue on Jul 22, 2021
    dbf3138
  3. added a commit that references this issue on Jul 23, 2021
    d1eb106
  4. added a commit that references this issue on Mar 18, 2022
    9022291
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type/bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions