summaryrefslogtreecommitdiffstats
path: root/jdisc_http_service
diff options
context:
space:
mode:
authorValerij Fredriksen <freva@users.noreply.github.com>2021-02-11 16:32:29 +0100
committerGitHub <noreply@github.com>2021-02-11 16:32:29 +0100
commit792e5313aa8499b0c3f60420674510c4a215cabe (patch)
tree2019a3abc01947288fc710c4569a41c61c4ca1c3 /jdisc_http_service
parente118411cf06630f083e12aff00f9719e33cb99f4 (diff)
parent71d1f1cacabdf56036849e7b4e63d1ee1dda01b5 (diff)
Merge pull request #16485 from vespa-engine/freva/rotate-atomically
Atomic rotation in LogFileHandler
Diffstat (limited to 'jdisc_http_service')
-rw-r--r--jdisc_http_service/src/main/java/com/yahoo/container/logging/LogFileHandler.java32
1 files changed, 28 insertions, 4 deletions
diff --git a/jdisc_http_service/src/main/java/com/yahoo/container/logging/LogFileHandler.java b/jdisc_http_service/src/main/java/com/yahoo/container/logging/LogFileHandler.java
index b2d7dd80b7f..6ee78b94401 100644
--- a/jdisc_http_service/src/main/java/com/yahoo/container/logging/LogFileHandler.java
+++ b/jdisc_http_service/src/main/java/com/yahoo/container/logging/LogFileHandler.java
@@ -18,6 +18,7 @@ import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
+import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.Optional;
import java.util.concurrent.BlockingQueue;
@@ -400,9 +401,8 @@ class LogFileHandler <LOGTYPE> {
private static void runCompressionZstd(NativeIO nativeIO, Path oldFile) {
try {
Path compressedFile = Paths.get(oldFile.toString() + ".zst");
- Files.createFile(compressedFile);
int bufferSize = 0x400000; // 4M
- try (FileOutputStream fileOut = new FileOutputStream(compressedFile.toFile());
+ try (FileOutputStream fileOut = AtomicFileOutputStream.create(compressedFile);
ZstdOuputStream out = new ZstdOuputStream(fileOut, bufferSize);
FileInputStream in = new FileInputStream(oldFile.toFile())) {
pageFriendlyTransfer(nativeIO, out, fileOut.getFD(), in, bufferSize);
@@ -420,7 +420,7 @@ class LogFileHandler <LOGTYPE> {
private static void runCompressionGzip(NativeIO nativeIO, Path oldFile) {
try {
Path gzippedFile = Paths.get(oldFile.toString() + ".gz");
- try (FileOutputStream fileOut = new FileOutputStream(gzippedFile.toFile());
+ try (FileOutputStream fileOut = AtomicFileOutputStream.create(gzippedFile);
GZIPOutputStream compressor = new GZIPOutputStream(fileOut, 0x100000);
FileInputStream inputStream = new FileInputStream(oldFile.toFile())) {
pageFriendlyTransfer(nativeIO, compressor, fileOut.getFD(), inputStream, 0x400000);
@@ -527,5 +527,29 @@ class LogFileHandler <LOGTYPE> {
}
}
}
-}
+ private static class AtomicFileOutputStream extends FileOutputStream {
+ private final Path path;
+ private final Path tmpPath;
+ private volatile boolean closed = false;
+
+ private AtomicFileOutputStream(Path path, Path tmpPath) throws FileNotFoundException {
+ super(tmpPath.toFile());
+ this.path = path;
+ this.tmpPath = tmpPath;
+ }
+
+ @Override
+ public synchronized void close() throws IOException {
+ super.close();
+ if (!closed) {
+ Files.move(tmpPath, path, StandardCopyOption.ATOMIC_MOVE);
+ closed = true;
+ }
+ }
+
+ private static AtomicFileOutputStream create(Path path) throws FileNotFoundException {
+ return new AtomicFileOutputStream(path, path.resolveSibling("." + path.getFileName() + ".tmp"));
+ }
+ }
+}