Skip to content
This repository has been archived by the owner on Apr 1, 2024. It is now read-only.

Commit

Permalink
Pulsar Functions: detect .nar files and prevent spammy logs on functi…
Browse files Browse the repository at this point in the history
…ons boot (#13) (apache#12667)

(cherry picked from commit 515a69f)
  • Loading branch information
eolivelli committed Nov 9, 2021
1 parent 3cbc79d commit 64a63bb
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,17 @@
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;

/**
* A utility class containing a few useful static methods to do typical IO
* operations.
*
*/
@Slf4j
public class FileUtils {

public static final long MILLIS_BETWEEN_ATTEMPTS = 50L;
Expand Down Expand Up @@ -221,5 +225,21 @@ public static void sleepQuietly(final long millis) {
/* do nothing */
}
}

public static boolean mayBeANarArchive(File jarFile) {
try (ZipFile zipFile = new ZipFile(jarFile);) {
ZipEntry entry = zipFile.getEntry("META-INF/bundled-dependencies");
if (entry == null || !entry.isDirectory()) {
log.info("Jar file {} does not contain META-INF/bundled-dependencies, it is not a NAR file", jarFile);
return false;
} else {
log.info("Jar file {} contains META-INF/bundled-dependencies, it may be a NAR file", jarFile);
return true;
}
} catch (IOException err) {
log.info("Cannot safely detect if {} is a NAR archive", jarFile, err);
return true;
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import org.apache.pulsar.common.functions.ProducerConfig;
import org.apache.pulsar.common.util.ObjectMapperFactory;
import org.apache.pulsar.common.util.Reflections;
import org.apache.pulsar.common.nar.FileUtils;
import org.apache.pulsar.functions.api.Function;
import org.apache.pulsar.functions.api.Record;
import org.apache.pulsar.functions.api.StateStore;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.apache.pulsar.functions.runtime.thread;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
Expand All @@ -33,6 +34,7 @@
import org.apache.pulsar.client.admin.PulsarAdmin;
import org.apache.pulsar.client.api.ClientBuilder;
import org.apache.pulsar.client.api.PulsarClient;
import org.apache.pulsar.common.nar.FileUtils;
import org.apache.pulsar.functions.instance.InstanceConfig;
import org.apache.pulsar.functions.instance.InstanceUtils;
import org.apache.pulsar.functions.instance.stats.FunctionCollectorRegistry;
Expand Down Expand Up @@ -132,14 +134,24 @@ private static ClassLoader loadJars(String jarFile,
return Thread.currentThread().getContextClassLoader();
}
ClassLoader fnClassLoader;
try {
log.info("Load JAR: {}", jarFile);
// Let's first try to treat it as a nar archive
fnCache.registerFunctionInstanceWithArchive(
instanceConfig.getFunctionId(),
instanceConfig.getInstanceName(),
jarFile, narExtractionDirectory);
} catch (FileNotFoundException e) {
boolean loadedAsNar = false;
if (FileUtils.mayBeANarArchive(new File(jarFile))) {
try {
log.info("Trying Loading file as NAR file: {}", jarFile);
// Let's first try to treat it as a nar archive
fnCache.registerFunctionInstanceWithArchive(
instanceConfig.getFunctionId(),
instanceConfig.getInstanceName(),
jarFile, narExtractionDirectory);
loadedAsNar = true;
} catch (FileNotFoundException e) {
// this is usually like
// java.io.FileNotFoundException: /tmp/pulsar-nar/xxx.jar-unpacked/xxxxx/META-INF/MANIFEST.MF'
log.error("The file {} does not look like a .nar file", jarFile, e.toString());
}
}
if (!loadedAsNar) {
log.info("Load file as simple JAR file: {}", jarFile);
// create the function class loader
fnCache.registerFunctionInstance(
instanceConfig.getFunctionId(),
Expand Down

0 comments on commit 64a63bb

Please sign in to comment.