From 29d55eeaddae7759dd228e8c81436b6c66b569aa Mon Sep 17 00:00:00 2001 From: Michael Dombrowski Date: Thu, 5 Oct 2023 08:27:33 -0400 Subject: [PATCH 1/2] fix: catch ClosedByInterruptException and handle it at debug/info --- .../logmanager/CloudWatchAttemptLogsProcessor.java | 10 ++++++++++ .../com/aws/greengrass/logmanager/model/LogFile.java | 4 ++++ 2 files changed, 14 insertions(+) diff --git a/src/main/java/com/aws/greengrass/logmanager/CloudWatchAttemptLogsProcessor.java b/src/main/java/com/aws/greengrass/logmanager/CloudWatchAttemptLogsProcessor.java index 12da41c9..a55975eb 100644 --- a/src/main/java/com/aws/greengrass/logmanager/CloudWatchAttemptLogsProcessor.java +++ b/src/main/java/com/aws/greengrass/logmanager/CloudWatchAttemptLogsProcessor.java @@ -28,6 +28,7 @@ import java.io.IOException; import java.io.InputStreamReader; import java.nio.channels.Channels; +import java.nio.channels.ClosedByInterruptException; import java.nio.channels.SeekableByteChannel; import java.nio.charset.StandardCharsets; import java.nio.file.Files; @@ -187,12 +188,21 @@ public CloudWatchAttempt processLogFiles(ComponentLogFileInformation componentLo // Need to read more lines until we get a complete log line. Let's add this to the SB. data.append(partialLogLine); + } catch (ClosedByInterruptException e) { + Thread.currentThread().interrupt(); + logger.atInfo().log("Interrupted while reading log"); + componentLogFileInformation.getLogFileInformationList().remove(0); + break; } catch (IOException e) { logger.atError().cause(e).log("Unable to read file {}", logFile.getAbsolutePath()); componentLogFileInformation.getLogFileInformationList().remove(0); break; } } + } catch (ClosedByInterruptException e) { + Thread.currentThread().interrupt(); + logger.atInfo().log("Interrupted while reading log"); + componentLogFileInformation.getLogFileInformationList().remove(0); } catch (IOException e) { // File probably does not exist. logger.atError().cause(e).log("Unable to read file {}", logFile.getAbsolutePath()); diff --git a/src/main/java/com/aws/greengrass/logmanager/model/LogFile.java b/src/main/java/com/aws/greengrass/logmanager/model/LogFile.java index 993cb5ca..1df55a20 100644 --- a/src/main/java/com/aws/greengrass/logmanager/model/LogFile.java +++ b/src/main/java/com/aws/greengrass/logmanager/model/LogFile.java @@ -11,6 +11,7 @@ import java.io.IOException; import java.io.InputStream; import java.net.URI; +import java.nio.channels.ClosedByInterruptException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; @@ -89,6 +90,9 @@ private String readBytesToString() { } catch (FileNotFoundException e) { // The file may be deleted as expected. logger.atDebug().cause(e).log("The file {} does not exist", this.getAbsolutePath()); + } catch (ClosedByInterruptException e) { + Thread.currentThread().interrupt(); + logger.atDebug().log("Interrupted while getting log file hash"); } catch (IOException e) { // File may not exist logger.atError().cause(e).log("Unable to read file {}", this.getAbsolutePath()); From 8b3e8d719dd370359f4c9d774a21c2218110195e Mon Sep 17 00:00:00 2001 From: Michael Dombrowski Date: Thu, 5 Oct 2023 08:34:11 -0400 Subject: [PATCH 2/2] fix: interrupt processLogsAndUpload --- .../com/aws/greengrass/logmanager/LogManagerService.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/aws/greengrass/logmanager/LogManagerService.java b/src/main/java/com/aws/greengrass/logmanager/LogManagerService.java index 04243f47..372da17d 100644 --- a/src/main/java/com/aws/greengrass/logmanager/LogManagerService.java +++ b/src/main/java/com/aws/greengrass/logmanager/LogManagerService.java @@ -681,9 +681,9 @@ private void updatelastComponentUploadedLogFile(Map lastCompone * It will then get all the log files which have not yet been uploaded to the cloud. This is done by checking * the last uploaded log file time for that component. */ - @SuppressWarnings("PMD.CollapsibleIfStatements") + @SuppressWarnings({"PMD.CollapsibleIfStatements", "PMD.PrematureDeclaration"}) private void processLogsAndUpload() throws InterruptedException { - while (true) { + while (!Thread.currentThread().isInterrupted()) { //TODO: this is only done for passing the current text. But in practise, we don`t need to intentionally // sleep here. if (!isCurrentlyUploading.compareAndSet(false, true)) { @@ -702,6 +702,9 @@ private void processLogsAndUpload() throws InterruptedException { try { LogFileGroup logFileGroup = LogFileGroup.create(componentLogConfiguration, lastUploadedLogFileTimeMs, workDir); + if (Thread.currentThread().isInterrupted()) { + return; + } if (logFileGroup.getLogFiles().isEmpty()) { continue;