diff options
author | Harald Musum <musum@verizonmedia.com> | 2020-10-15 23:23:39 +0200 |
---|---|---|
committer | Harald Musum <musum@verizonmedia.com> | 2020-10-15 23:23:39 +0200 |
commit | b455cafb3d9c70a5ad8598a2f0e7a9d6eee5e83c (patch) | |
tree | f6df19cdc4ca22031ac210e5858463ad1b7aed59 | |
parent | 56f9d32c4d5ef275a128d8f5cb1025f7834ddd58 (diff) |
Try to avoid race when creating directory
If e.g. several containers try to create this directory at the same time
there will be unnecessary warnings in log
-rw-r--r-- | vespalog/src/main/java/com/yahoo/log/LogFileDb.java | 11 |
1 files changed, 6 insertions, 5 deletions
diff --git a/vespalog/src/main/java/com/yahoo/log/LogFileDb.java b/vespalog/src/main/java/com/yahoo/log/LogFileDb.java index 22678994346..d9ca8f4653e 100644 --- a/vespalog/src/main/java/com/yahoo/log/LogFileDb.java +++ b/vespalog/src/main/java/com/yahoo/log/LogFileDb.java @@ -1,4 +1,4 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.log; import static java.nio.charset.StandardCharsets.UTF_8; @@ -6,6 +6,7 @@ import static java.nio.file.StandardOpenOption.APPEND; import static java.nio.file.StandardOpenOption.CREATE; import java.io.File; +import java.io.IOException; import java.io.OutputStream; import java.nio.file.Files; import java.nio.file.Path; @@ -32,10 +33,10 @@ public class LogFileDb { private static OutputStream metaFile() throws java.io.IOException { String fn = getDefaults().underVespaHome(DBDIR + "logfiles." + dayStamp()); File dir = new File(fn).getParentFile(); - if (!dir.exists()) { - if (!dir.mkdirs()) { - System.err.println("Failed creating logfiledb directory '" + dir.getPath() + "'."); - } + try { + Files.createDirectories(dir.toPath()); + } catch (IOException e) { + System.err.println("Failed creating logfiledb directory '" + dir.getPath() + "': " + e.getMessage()); } Path path = Paths.get(fn); return Files.newOutputStream(path, CREATE, APPEND); |