From 903c3e1ffc0fe6e721a54354ed2be22af813dee3 Mon Sep 17 00:00:00 2001 From: LSmyrnaios Date: Fri, 15 Sep 2023 11:54:32 +0300 Subject: [PATCH] Add thread-safety when reading the bulkImportReport-files. --- .../urls_controller/controllers/BulkImportController.java | 4 ++++ .../java/eu/openaire/urls_controller/util/FileUtils.java | 6 +++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/main/java/eu/openaire/urls_controller/controllers/BulkImportController.java b/src/main/java/eu/openaire/urls_controller/controllers/BulkImportController.java index 51309b5..6aaad0f 100644 --- a/src/main/java/eu/openaire/urls_controller/controllers/BulkImportController.java +++ b/src/main/java/eu/openaire/urls_controller/controllers/BulkImportController.java @@ -224,6 +224,8 @@ public class BulkImportController { // Write the contents of the report-file to a string (efficiently!) and return the whole content as an HTTP-response. final StringBuilder stringBuilder = new StringBuilder(25_000); String line; + + FileUtils.fileAccessLock.lock(); try ( BufferedReader in = new BufferedReader(new InputStreamReader(Files.newInputStream(Paths.get(this.bulkImportReportLocation, bulkImportReportId + ".json"))), FileUtils.twentyFiveKb) ) { while ( (line = in.readLine()) != null ) stringBuilder.append(line).append("\n"); // The "readLine()" does not return the line-term char. @@ -234,6 +236,8 @@ public class BulkImportController { String errorMsg = "Failed to read the contents of report-file with ID: " + bulkImportReportId; logger.error(errorMsg, e); return ResponseEntity.internalServerError().body(errorMsg); // It's ok to give the file-path to the user, since the report already contains the file-path. + } finally { + FileUtils.fileAccessLock.unlock(); } String json = stringBuilder.toString().trim(); diff --git a/src/main/java/eu/openaire/urls_controller/util/FileUtils.java b/src/main/java/eu/openaire/urls_controller/util/FileUtils.java index bd5e571..88586cc 100644 --- a/src/main/java/eu/openaire/urls_controller/util/FileUtils.java +++ b/src/main/java/eu/openaire/urls_controller/util/FileUtils.java @@ -698,12 +698,12 @@ public class FileUtils { } - private static final Lock fileWriteLock = new ReentrantLock(true); + public static final Lock fileAccessLock = new ReentrantLock(true); public String writeToFile(String fileFullPath, String stringToWrite, boolean shouldLockThreads) { if ( shouldLockThreads ) // In case multiple threads write to the same file. for ex. during the bulk-import procedure. - fileWriteLock.lock(); + fileAccessLock.lock(); try ( BufferedWriter bufferedWriter = new BufferedWriter(Files.newBufferedWriter(Paths.get(fileFullPath)), halfMb) ) { @@ -714,7 +714,7 @@ public class FileUtils { return errorMsg; } finally { if ( shouldLockThreads ) - fileWriteLock.unlock(); + fileAccessLock.unlock(); } return null; }