Allow disabling of sigar via settings

Sigar can only be disabled by removing the binaries. This is tricky for our
tests and might cause a lot of trouble if a user wants or needs to do it.
This commit allows to disable sigar with a simple boolean flag in the settings.

Closes #9582
This commit is contained in:
Simon Willnauer 2015-05-22 09:16:23 +02:00
parent 19de7039a3
commit ada98ba0c4
4 changed files with 36 additions and 28 deletions

View File

@ -38,6 +38,7 @@ import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.env.Environment; import org.elasticsearch.env.Environment;
import org.elasticsearch.monitor.jvm.JvmInfo; import org.elasticsearch.monitor.jvm.JvmInfo;
import org.elasticsearch.monitor.process.JmxProcessProbe; import org.elasticsearch.monitor.process.JmxProcessProbe;
import org.elasticsearch.monitor.sigar.SigarService;
import org.elasticsearch.node.Node; import org.elasticsearch.node.Node;
import org.elasticsearch.node.NodeBuilder; import org.elasticsearch.node.NodeBuilder;
import org.elasticsearch.node.internal.InternalSettingsPreparer; import org.elasticsearch.node.internal.InternalSettingsPreparer;
@ -85,7 +86,8 @@ public class Bootstrap {
} }
/** initialize native resources */ /** initialize native resources */
public static void initializeNatives(boolean mlockAll, boolean ctrlHandler) { public static void initializeNatives(boolean mlockAll, boolean ctrlHandler, boolean loadSigar) {
final ESLogger logger = Loggers.getLogger(Bootstrap.class);
// mlockall if requested // mlockall if requested
if (mlockAll) { if (mlockAll) {
if (Constants.WINDOWS) { if (Constants.WINDOWS) {
@ -98,7 +100,7 @@ public class Bootstrap {
// check if the user is running as root, and bail // check if the user is running as root, and bail
if (Natives.definitelyRunningAsRoot()) { if (Natives.definitelyRunningAsRoot()) {
if (Boolean.parseBoolean(System.getProperty("es.insecure.allow.root"))) { if (Boolean.parseBoolean(System.getProperty("es.insecure.allow.root"))) {
Loggers.getLogger(Bootstrap.class).warn("running as ROOT user. this is a bad idea!"); logger.warn("running as ROOT user. this is a bad idea!");
} else { } else {
throw new RuntimeException("don't run elasticsearch as root."); throw new RuntimeException("don't run elasticsearch as root.");
} }
@ -110,9 +112,7 @@ public class Bootstrap {
@Override @Override
public boolean handle(int code) { public boolean handle(int code) {
if (CTRL_CLOSE_EVENT == code) { if (CTRL_CLOSE_EVENT == code) {
ESLogger logger = Loggers.getLogger(Bootstrap.class);
logger.info("running graceful exit on windows"); logger.info("running graceful exit on windows");
Bootstrap.INSTANCE.stop(); Bootstrap.INSTANCE.stop();
return true; return true;
} }
@ -128,12 +128,16 @@ public class Bootstrap {
// we've already logged this. // we've already logged this.
} }
if (loadSigar) {
// initialize sigar explicitly // initialize sigar explicitly
try { try {
Sigar.load(); Sigar.load();
Loggers.getLogger(Bootstrap.class).trace("sigar libraries loaded successfully"); logger.trace("sigar libraries loaded successfully");
} catch (Throwable t) { } catch (Throwable t) {
Loggers.getLogger(Bootstrap.class).trace("failed to load sigar libraries", t); logger.trace("failed to load sigar libraries", t);
}
} else {
logger.trace("sigar not loaded, disabled via settings");
} }
// init lucene random seed. it will use /dev/urandom where available: // init lucene random seed. it will use /dev/urandom where available:
@ -142,7 +146,8 @@ public class Bootstrap {
private void setup(boolean addShutdownHook, Settings settings, Environment environment) throws Exception { private void setup(boolean addShutdownHook, Settings settings, Environment environment) throws Exception {
initializeNatives(settings.getAsBoolean("bootstrap.mlockall", false), initializeNatives(settings.getAsBoolean("bootstrap.mlockall", false),
settings.getAsBoolean("bootstrap.ctrlhandler", true)); settings.getAsBoolean("bootstrap.ctrlhandler", true),
settings.getAsBoolean("bootstrap.sigar", true));
if (addShutdownHook) { if (addShutdownHook) {
Runtime.getRuntime().addShutdownHook(new Thread() { Runtime.getRuntime().addShutdownHook(new Thread() {

View File

@ -34,8 +34,8 @@ public class SigarService extends AbstractComponent {
@Inject @Inject
public SigarService(Settings settings) { public SigarService(Settings settings) {
super(settings); super(settings);
Sigar sigar = null; Sigar sigar = null;
if (settings.getAsBoolean("bootstrap.sigar", true)) {
try { try {
sigar = new Sigar(); sigar = new Sigar();
// call it to make sure the library was loaded // call it to make sure the library was loaded
@ -53,6 +53,7 @@ public class SigarService extends AbstractComponent {
} }
} }
} }
}
this.sigar = sigar; this.sigar = sigar;
} }

View File

@ -49,7 +49,7 @@ public class BootstrapForTesting {
static { static {
// just like bootstrap, initialize natives, then SM // just like bootstrap, initialize natives, then SM
Bootstrap.initializeNatives(true, true); Bootstrap.initializeNatives(true, true, true);
// make sure java.io.tmpdir exists always (in case code uses it in a static initializer) // make sure java.io.tmpdir exists always (in case code uses it in a static initializer)
Path javaTmpDir = PathUtils.get(Objects.requireNonNull(System.getProperty("java.io.tmpdir"), Path javaTmpDir = PathUtils.get(Objects.requireNonNull(System.getProperty("java.io.tmpdir"),

View File

@ -90,6 +90,7 @@ import org.elasticsearch.indices.breaker.CircuitBreakerService;
import org.elasticsearch.indices.breaker.HierarchyCircuitBreakerService; import org.elasticsearch.indices.breaker.HierarchyCircuitBreakerService;
import org.elasticsearch.indices.fielddata.cache.IndicesFieldDataCache; import org.elasticsearch.indices.fielddata.cache.IndicesFieldDataCache;
import org.elasticsearch.indices.recovery.RecoverySettings; import org.elasticsearch.indices.recovery.RecoverySettings;
import org.elasticsearch.monitor.sigar.SigarService;
import org.elasticsearch.node.Node; import org.elasticsearch.node.Node;
import org.elasticsearch.node.service.NodeService; import org.elasticsearch.node.service.NodeService;
import org.elasticsearch.plugins.PluginsService; import org.elasticsearch.plugins.PluginsService;
@ -277,6 +278,7 @@ public final class InternalTestCluster extends TestCluster {
builder.put("path.data", dataPath.toString()); builder.put("path.data", dataPath.toString());
} }
} }
builder.put("bootstrap.sigar", rarely());
builder.put("path.home", baseDir); builder.put("path.home", baseDir);
builder.put("path.repo", baseDir.resolve("repos")); builder.put("path.repo", baseDir.resolve("repos"));
builder.put("transport.tcp.port", BASE_PORT + "-" + (BASE_PORT+100)); builder.put("transport.tcp.port", BASE_PORT + "-" + (BASE_PORT+100));