Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix][broker] Fix incomplete NAR file extraction which prevents broker from starting #23274

Merged
merged 3 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
Expand Down Expand Up @@ -86,19 +88,32 @@ static File doUnpackNar(final File nar, final File baseWorkingDirectory, Runnabl
try (FileChannel channel = new RandomAccessFile(lockFile, "rw").getChannel();
FileLock lock = channel.lock()) {
File narWorkingDirectory = new File(parentDirectory, md5Sum);
if (narWorkingDirectory.mkdir()) {
if (!narWorkingDirectory.exists()) {
File narExtractionTempDirectory = new File(parentDirectory, md5Sum + ".tmp");
if (narExtractionTempDirectory.exists()) {
FileUtils.deleteFile(narExtractionTempDirectory, true);
}
if (!narExtractionTempDirectory.mkdir()) {
throw new IOException("Cannot create " + narExtractionTempDirectory);
}
try {
log.info("Extracting {} to {}", nar, narWorkingDirectory);
log.info("Extracting {} to {}", nar, narExtractionTempDirectory);
if (extractCallback != null) {
extractCallback.run();
}
unpack(nar, narWorkingDirectory);
unpack(nar, narExtractionTempDirectory);
} catch (IOException e) {
log.error("There was a problem extracting the nar file. Deleting {} to clean up state.",
narWorkingDirectory, e);
FileUtils.deleteFile(narWorkingDirectory, true);
narExtractionTempDirectory, e);
try {
FileUtils.deleteFile(narExtractionTempDirectory, true);
} catch (IOException e2) {
log.error("Failed to delete temporary directory {}", narExtractionTempDirectory, e2);
}
throw e;
}
Files.move(narExtractionTempDirectory.toPath(), narWorkingDirectory.toPath(),
StandardCopyOption.ATOMIC_MOVE);
}
return narWorkingDirectory;
}
Expand Down Expand Up @@ -166,7 +181,7 @@ private static void makeFile(final InputStream inputStream, final File file) thr
* @throws IOException
* if cannot read file
*/
private static byte[] calculateMd5sum(final File file) throws IOException {
protected static byte[] calculateMd5sum(final File file) throws IOException {
try (final FileInputStream inputStream = new FileInputStream(file)) {
// codeql[java/weak-cryptographic-algorithm] - md5 is sufficient for this use case
final MessageDigest md5 = MessageDigest.getInstance("md5");
Expand All @@ -184,4 +199,4 @@ private static byte[] calculateMd5sum(final File file) throws IOException {
throw new IllegalArgumentException(nsae);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,30 @@ public static void main(String[] args) {
}
}

@Test
void shouldReExtractWhenUnpackedDirectoryIsMissing() throws InterruptedException {
CountDownLatch countDownLatch = new CountDownLatch(1);
AtomicInteger exceptionCounter = new AtomicInteger();
lhotari marked this conversation as resolved.
Show resolved Hide resolved
AtomicInteger extractCounter = new AtomicInteger();

new Thread(() -> {
try {
File narWorkingDirectory = NarUnpacker.doUnpackNar(sampleZipFile, extractDirectory, extractCounter::incrementAndGet);
FileUtils.deleteFile(narWorkingDirectory, true);
NarUnpacker.doUnpackNar(sampleZipFile, extractDirectory, extractCounter::incrementAndGet);
} catch (Exception e) {
log.error("Unpacking failed", e);
exceptionCounter.incrementAndGet();
lhotari marked this conversation as resolved.
Show resolved Hide resolved
} finally {
countDownLatch.countDown();
}
}).start();

assertTrue(countDownLatch.await(30, TimeUnit.SECONDS));
assertEquals(exceptionCounter.get(), 0);
assertEquals(extractCounter.get(), 2);
}

@Test
void shouldExtractFilesOnceInDifferentProcess() throws InterruptedException {
int processes = 5;
Expand Down