From 998bde082017df2e556c7286d27c9542d7ddea99 Mon Sep 17 00:00:00 2001 From: kimchy Date: Sat, 6 Nov 2010 22:47:09 +0200 Subject: [PATCH] Add an option to display max_open_files, by setting -Des.max-open-files to `true`, closes #483. --- .../elasticsearch/bootstrap/Bootstrap.java | 6 ++++ .../common/io/FileSystemUtils.java | 32 ++++++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/bootstrap/Bootstrap.java b/modules/elasticsearch/src/main/java/org/elasticsearch/bootstrap/Bootstrap.java index a3d9e0a01a5..d7e213d626b 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/bootstrap/Bootstrap.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/bootstrap/Bootstrap.java @@ -25,6 +25,7 @@ import org.elasticsearch.common.Classes; import org.elasticsearch.common.collect.Tuple; import org.elasticsearch.common.inject.CreationException; import org.elasticsearch.common.inject.spi.Message; +import org.elasticsearch.common.io.FileSystemUtils; import org.elasticsearch.common.jline.ANSI; import org.elasticsearch.common.jna.Natives; import org.elasticsearch.common.logging.ESLogger; @@ -155,6 +156,11 @@ public class Bootstrap { System.exit(3); } + if (System.getProperty("es.max-open-files", "false").equals("true")) { + ESLogger logger = Loggers.getLogger(Bootstrap.class); + logger.info("max_open_files [{}]", FileSystemUtils.maxOpenFiles(new File(tuple.v2().workFile(), "open_files"))); + } + String stage = "Initialization"; try { if (!foreground) { diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/common/io/FileSystemUtils.java b/modules/elasticsearch/src/main/java/org/elasticsearch/common/io/FileSystemUtils.java index dc06d330433..d1e9072f4fa 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/common/io/FileSystemUtils.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/common/io/FileSystemUtils.java @@ -21,12 +21,42 @@ package org.elasticsearch.common.io; import java.io.*; import java.nio.channels.FileChannel; +import java.util.ArrayList; +import java.util.List; /** - * @author kimchy (Shay Banon) + * @author kimchy (shay.banon) */ public class FileSystemUtils { + public static int maxOpenFiles(File testDir) { + boolean dirCreated = false; + if (!testDir.exists()) { + dirCreated = true; + testDir.mkdirs(); + } + List files = new ArrayList(); + try { + while (true) { + files.add(new RandomAccessFile(new File(testDir, "tmp" + files.size()), "rw")); + } + } catch (IOException ioe) { + int i = 0; + for (RandomAccessFile raf : files) { + try { + raf.close(); + } catch (IOException e) { + // ignore + } + new File(testDir, "tmp" + i++).delete(); + } + if (dirCreated) { + deleteRecursively(testDir); + } + } + return files.size(); + } + public static boolean deleteRecursively(File root) { return deleteRecursively(root, true); }