eolivelli commented on a change in pull request #11343:
URL: https://github.com/apache/pulsar/pull/11343#discussion_r671349866
##########
File path:
pulsar-common/src/main/java/org/apache/pulsar/common/nar/NarUnpacker.java
##########
@@ -58,43 +61,55 @@
* if unable to explode nar
*/
public static File unpackNar(final File nar, final File
baseWorkingDirectory) throws IOException {
- final File narWorkingDirectory = new File(baseWorkingDirectory,
nar.getName() + "-unpacked");
+ return doUnpackNar(nar, baseWorkingDirectory, null);
+ }
- // 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);
+ @VisibleForTesting
+ static File doUnpackNar(final File nar, final File baseWorkingDirectory,
Runnable extractCallback)
+ throws IOException {
+ File parentDirectory = new File(baseWorkingDirectory, nar.getName() +
"-unpacked");
+ if (!parentDirectory.exists()) {
+ parentDirectory.mkdirs();
+ }
+ String sha256Sum =
Base64.getUrlEncoder().withoutPadding().encodeToString(calculateSha256Sum(nar));
+ // ensure that one process can extract the files
+ File lockFile = new File(parentDirectory, "." + sha256Sum + ".lock");
+ // prevent OverlappingFileLockException by ensuring that one thread
tries to create a lock in this JVM
+ Object localLock =
CURRENT_JVM_FILE_LOCKS.computeIfAbsent(lockFile.getAbsolutePath(), key -> new
Object());
+ synchronized (localLock) {
+ // create file lock that ensures that other processes
+ // using the same lock file don't execute concurrently
+ try (FileChannel channel = new RandomAccessFile(lockFile,
"rw").getChannel();
+ FileLock lock = channel.lock()) {
+ File narWorkingDirectory = new File(parentDirectory,
sha256Sum);
+ if (narWorkingDirectory.mkdir()) {
+ try {
+ log.info("Extracting {} to {}", nar,
narWorkingDirectory);
+ if (extractCallback != null) {
+ extractCallback.run();
+ }
+ unpack(nar, narWorkingDirectory);
+ } catch (IOException e) {
+ log.error("There was a problem extracting the nar
file. Deleting {} to clean up state.",
Review comment:
shall we retry, using another directory ?
##########
File path:
pulsar-common/src/test/java/org/apache/pulsar/common/nar/NarUnpackerTest.java
##########
@@ -0,0 +1,160 @@
+/**
+ * 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.nar;
+
+import static org.junit.Assert.assertTrue;
+import static org.testng.Assert.assertEquals;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipOutputStream;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang3.SystemUtils;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+@Slf4j
+public class NarUnpackerTest {
+ File sampleZipFile;
+ File extractDirectory;
+
+ @BeforeMethod
+ public void createSampleZipFile() throws IOException {
+ sampleZipFile = Files.createTempFile("sample", ".zip").toFile();
+ try (ZipOutputStream out = new ZipOutputStream(new
FileOutputStream(sampleZipFile))) {
+ for (int i = 0; i < 10000; i++) {
+ ZipEntry e = new ZipEntry("hello" + i + ".txt");
+ out.putNextEntry(e);
+ byte[] msg = "hello world!".getBytes(StandardCharsets.UTF_8);
+ out.write(msg, 0, msg.length);
+ out.closeEntry();
+ }
+ }
+ extractDirectory =
Files.createTempDirectory("nar_unpack_dir").toFile();
+ }
+
+ @AfterMethod(alwaysRun = true)
+ void deleteSampleZipFile() throws IOException {
+ if (sampleZipFile != null) {
+ sampleZipFile.delete();
+ }
+ if (extractDirectory != null) {
+ FileUtils.deleteFile(extractDirectory, true);
+ }
+ }
+
+ @Test
+ void shouldExtractFilesOnceInSameProcess() throws InterruptedException {
+ int threads = 20;
+ CountDownLatch countDownLatch = new CountDownLatch(threads);
+ AtomicInteger exceptionCounter = new AtomicInteger();
+ AtomicInteger extractCounter = new AtomicInteger();
+ for (int i = 0; i < threads; i++) {
+ new Thread(() -> {
+ try {
+ NarUnpacker.doUnpackNar(sampleZipFile, extractDirectory,
extractCounter::incrementAndGet);
+ } catch (Exception e) {
+ e.printStackTrace();
+ exceptionCounter.incrementAndGet();
+ } finally {
+ countDownLatch.countDown();
+ }
+ }).start();
+ }
+ assertTrue(countDownLatch.await(30, TimeUnit.SECONDS));
+ assertEquals(exceptionCounter.get(), 0);
+ assertEquals(extractCounter.get(), 1);
+ }
+
+ public static class NarUnpackerWorker {
+ public static void main(String[] args) {
+ File sampleZipFile = new File(args[0]);
+ File extractDirectory = new File(args[1]);
+ AtomicInteger extractCounter = new AtomicInteger();
+ try {
+ NarUnpacker.doUnpackNar(sampleZipFile, extractDirectory,
extractCounter::incrementAndGet);
+ if (extractCounter.get() == 1) {
+ System.exit(101);
+ } else if (extractCounter.get() == 0) {
+ System.exit(100);
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ System.exit(99);
+ }
+ }
+ }
+
+ @Test
+ void shouldExtractFilesOnceInDifferentProcess() throws
InterruptedException {
Review comment:
I am not sure it is worth to add such test.
it is quite heavyweight (especially for CI, but also for people that are
running the tests for instance during a release validation) and we are not sure
it reproduces consistently the problem.
I suggest to drop this test but if you feel strong to keep it then keep it
##########
File path:
pulsar-common/src/test/java/org/apache/pulsar/common/nar/NarUnpackerTest.java
##########
@@ -0,0 +1,160 @@
+/**
+ * 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.nar;
+
+import static org.junit.Assert.assertTrue;
+import static org.testng.Assert.assertEquals;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipOutputStream;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang3.SystemUtils;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+@Slf4j
+public class NarUnpackerTest {
+ File sampleZipFile;
+ File extractDirectory;
+
+ @BeforeMethod
+ public void createSampleZipFile() throws IOException {
+ sampleZipFile = Files.createTempFile("sample", ".zip").toFile();
+ try (ZipOutputStream out = new ZipOutputStream(new
FileOutputStream(sampleZipFile))) {
+ for (int i = 0; i < 10000; i++) {
+ ZipEntry e = new ZipEntry("hello" + i + ".txt");
+ out.putNextEntry(e);
+ byte[] msg = "hello world!".getBytes(StandardCharsets.UTF_8);
+ out.write(msg, 0, msg.length);
+ out.closeEntry();
+ }
+ }
+ extractDirectory =
Files.createTempDirectory("nar_unpack_dir").toFile();
+ }
+
+ @AfterMethod(alwaysRun = true)
+ void deleteSampleZipFile() throws IOException {
+ if (sampleZipFile != null) {
+ sampleZipFile.delete();
+ }
+ if (extractDirectory != null) {
+ FileUtils.deleteFile(extractDirectory, true);
+ }
+ }
+
+ @Test
+ void shouldExtractFilesOnceInSameProcess() throws InterruptedException {
+ int threads = 20;
+ CountDownLatch countDownLatch = new CountDownLatch(threads);
+ AtomicInteger exceptionCounter = new AtomicInteger();
+ AtomicInteger extractCounter = new AtomicInteger();
+ for (int i = 0; i < threads; i++) {
+ new Thread(() -> {
+ try {
+ NarUnpacker.doUnpackNar(sampleZipFile, extractDirectory,
extractCounter::incrementAndGet);
+ } catch (Exception e) {
+ e.printStackTrace();
Review comment:
nit: use logger ?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]