From 614be25a1c2d4d8e40e6b6d40a1914090f94b10f Mon Sep 17 00:00:00 2001 From: Michael Dombrowski Date: Thu, 5 Oct 2023 14:28:10 -0400 Subject: [PATCH] fix: catch ClosedByInterruptException and handle it at debug/info (#212) --- .../logmanager/CloudWatchAttemptLogsProcessor.java | 10 ++++++++++ .../aws/greengrass/logmanager/LogManagerService.java | 7 +++++-- .../com/aws/greengrass/logmanager/model/LogFile.java | 4 ++++ 3 files changed, 19 insertions(+), 2 deletions(-) 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/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; 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());