lhotari commented on a change in pull request #11343:
URL: https://github.com/apache/pulsar/pull/11343#discussion_r671358327



##########
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:
       I think it's better not to retry. This would most likely happen when 
there's no remaining disk space.




-- 
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]


Reply via email to