From d4e42b77a501711b4ab6debdc7e461711abad145 Mon Sep 17 00:00:00 2001 From: Li Weinan Date: Mon, 24 Oct 2016 22:41:36 +0800 Subject: [PATCH] .es_temp_file remains after system crash, causing it not to start again #21007 When system starts, it creates a temporary file named .es_temp_file to ensure the data directories are writable. If system crashes after creating the .es_temp_file but before deleting this file, next time the system will not be able to start because the Files.createFile(resolve) will throw an exception if the file already exists. --- .../main/java/org/elasticsearch/env/NodeEnvironment.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/elasticsearch/env/NodeEnvironment.java b/core/src/main/java/org/elasticsearch/env/NodeEnvironment.java index f3e1f2fb24d..401f3f12f4b 100644 --- a/core/src/main/java/org/elasticsearch/env/NodeEnvironment.java +++ b/core/src/main/java/org/elasticsearch/env/NodeEnvironment.java @@ -1002,11 +1002,16 @@ public final class NodeEnvironment implements Closeable { private static void tryWriteTempFile(Path path) throws IOException { if (Files.exists(path)) { Path resolve = path.resolve(".es_temp_file"); + boolean tempFileCreated = false; try { Files.createFile(resolve); - Files.deleteIfExists(resolve); + tempFileCreated = true; } catch (IOException ex) { throw new IOException("failed to write in data directory [" + path + "] write permission is required", ex); + } finally { + if (tempFileCreated) { + Files.deleteIfExists(resolve); + } } } }