Skip to content

Commit

Permalink
fix: catch ClosedByInterruptException and handle it at debug/info (#212)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeDombo authored Oct 5, 2023
1 parent bd631de commit 614be25
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -681,9 +681,9 @@ private void updatelastComponentUploadedLogFile(Map<String, Instant> 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)) {
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit 614be25

Please sign in to comment.