Skip to content

Commit

Permalink
Covered the read of logfile content with try-resource to avoid to kee…
Browse files Browse the repository at this point in the history
…p a file descriptor open that later prohibited access to the process itself. Also added clean shutdown of LogManager before deleting log files used by log.

Fixes elastic#11399
  • Loading branch information
andsel committed Dec 12, 2019
1 parent 7f5aa18 commit 71e702c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public void setUp() throws IOException {

@After
public void tearDown() throws IOException {
LogManager.shutdown();
LogTestUtils.deleteLogFile("logstash-deprecation.log");
LogTestUtils.reloadLogConfiguration();
}
Expand Down
33 changes: 5 additions & 28 deletions logstash-core/src/test/java/org/logstash/log/LogTestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import org.apache.logging.log4j.core.LoggerContext;

import java.io.IOException;
import java.nio.file.FileSystemException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.junit.Assert.assertTrue;

Expand All @@ -19,7 +19,9 @@ static String loadLogFileContent(String logfileName) throws IOException {
.getPath(System.getProperty("user.dir"), System.getProperty("ls.logs"), logfileName);

assertTrue("Log [" + path.toString() + "] file MUST exists", Files.exists(path));
return Files.lines(path).collect(Collectors.joining());
try (Stream<String> lines = Files.lines(path)) {
return lines.collect(Collectors.joining());
}
}

static void reloadLogConfiguration() {
Expand All @@ -30,31 +32,6 @@ static void reloadLogConfiguration() {
static void deleteLogFile(String logfileName) throws IOException {
Path path = FileSystems.getDefault()
.getPath(System.getProperty("user.dir"), System.getProperty("ls.logs"), logfileName);
pollingDelete(path, 5, TimeUnit.SECONDS);
}

static void pollingDelete(Path path, int sleep, TimeUnit timeUnit) throws IOException {
final int maxRetries = 5;
int retries = 0;
do {
try {
Files.deleteIfExists(path);
break;
} catch (FileSystemException fsex) {
System.out.println("FS access error while deleting, " + fsex.getReason() + " deleting: " + fsex.getOtherFile());
}

try {
Thread.sleep(timeUnit.toMillis(sleep));
} catch (InterruptedException e) {
// follows up
Thread.currentThread().interrupt();
break;
}

retries++;
} while (retries < maxRetries);

assertTrue("Exhausted 5 retries to delete the file: " + path,retries < maxRetries);
Files.deleteIfExists(path);
}
}

0 comments on commit 71e702c

Please sign in to comment.