.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.
This commit is contained in:
Li Weinan 2016-10-24 22:41:36 +08:00 committed by Boaz Leskes
parent f6f129b21f
commit d4e42b77a5
1 changed files with 6 additions and 1 deletions

View File

@ -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);
}
}
}
}