LUCENE-6795: Make SystemInfoHandler more dynamic, add support for IBM J9 OperatingSystemMXBean

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1702656 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Uwe Schindler 2015-09-12 17:55:34 +00:00
parent d8b7f5a62c
commit f213b3bfdc
2 changed files with 48 additions and 55 deletions

View File

@ -17,6 +17,10 @@
package org.apache.solr.handler.admin; package org.apache.solr.handler.admin;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -30,11 +34,13 @@ import java.net.UnknownHostException;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.text.DecimalFormat; import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols; import java.text.DecimalFormatSymbols;
import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.Locale; import java.util.Locale;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.apache.lucene.LucenePackage; import org.apache.lucene.LucenePackage;
import org.apache.lucene.util.Constants;
import org.apache.solr.common.util.NamedList; import org.apache.solr.common.util.NamedList;
import org.apache.solr.common.util.SimpleOrderedMap; import org.apache.solr.common.util.SimpleOrderedMap;
import org.apache.solr.core.CoreContainer; import org.apache.solr.core.CoreContainer;
@ -160,75 +166,61 @@ public class SystemInfoHandler extends RequestHandlerBase
SimpleOrderedMap<Object> info = new SimpleOrderedMap<>(); SimpleOrderedMap<Object> info = new SimpleOrderedMap<>();
OperatingSystemMXBean os = ManagementFactory.getOperatingSystemMXBean(); OperatingSystemMXBean os = ManagementFactory.getOperatingSystemMXBean();
info.add(NAME, os.getName()); info.add(NAME, os.getName()); // add at least this one
info.add( "version", os.getVersion() );
info.add( "arch", os.getArch() );
info.add( "systemLoadAverage", os.getSystemLoadAverage());
// This is a public Oracle/OpenJDK extension, but may not be in other JDKs:
// com.sun.management.OperatingSystemMXBean
try { try {
final Class<? extends PlatformManagedObject> intf = Class.forName("com.sun.management.OperatingSystemMXBean") // add remaining ones dynamically using Java Beans API
.asSubclass(PlatformManagedObject.class); addMXBeanProperties(os, OperatingSystemMXBean.class, info);
final PlatformManagedObject bean = ManagementFactory.getPlatformMXBean(intf); } catch (IntrospectionException | ReflectiveOperationException e) {
if (bean != null) { log.warn("Unable to fetch properties of OperatingSystemMXBean.", e);
addMXBeanProperty( bean, intf, "committedVirtualMemorySize", info);
addMXBeanProperty( bean, intf, "freePhysicalMemorySize", info);
addMXBeanProperty( bean, intf, "freeSwapSpaceSize", info);
addMXBeanProperty( bean, intf, "processCpuTime", info);
addMXBeanProperty( bean, intf, "totalPhysicalMemorySize", info);
addMXBeanProperty( bean, intf, "totalSwapSpaceSize", info);
}
} catch (Exception e) {
// ignore
} }
// This is a public Oracle/OpenJDK extension, but may not be in other JDKs: // There are some additional beans we want to add (not available on all JVMs):
// com.sun.management.UnixOperatingSystemMXBean for (String clazz : Arrays.asList(
try { "com.sun.management.OperatingSystemMXBean",
final Class<? extends PlatformManagedObject> intf = Class.forName("com.sun.management.UnixOperatingSystemMXBean") "com.sun.management.UnixOperatingSystemMXBean",
.asSubclass(PlatformManagedObject.class); "com.ibm.lang.management.OperatingSystemMXBean"
final PlatformManagedObject bean = ManagementFactory.getPlatformMXBean(intf); )) {
if (bean != null) { try {
addMXBeanProperty( bean, intf, "openFileDescriptorCount", info ); final Class<? extends PlatformManagedObject> intf = Class.forName(clazz)
addMXBeanProperty( bean, intf, "maxFileDescriptorCount", info ); .asSubclass(PlatformManagedObject.class);
addMXBeanProperties(os, intf, info);
} catch (ClassNotFoundException e) {
// ignore
} catch (IntrospectionException | ReflectiveOperationException e) {
log.warn("Unable to fetch properties of JVM-specific OperatingSystemMXBean.", e);
} }
} catch (Exception e) {
// ignore
} }
// Try some command line things:
try { try {
if( !os.getName().toLowerCase(Locale.ROOT).startsWith( "windows" ) ) { if (!Constants.WINDOWS) {
// Try some command line things
info.add( "uname", execute( "uname -a" ) ); info.add( "uname", execute( "uname -a" ) );
info.add( "uptime", execute( "uptime" ) ); info.add( "uptime", execute( "uptime" ) );
} }
} catch( Exception ex ) { } catch( Exception ex ) {
log.warn("Unable to execute command line tools.", ex); log.warn("Unable to execute command line tools to get operating system properties.", ex);
} }
return info; return info;
} }
/** /**
* Try to run a getter function on a {@link PlatformManagedObject}. * Add all bean properties of a {@link PlatformManagedObject} to the given {@link NamedList}.
* <p> * <p>
* If you are running a OpenJDK/Oracle JVM, there are nice functions in: * If you are running a OpenJDK/Oracle JVM, there are nice properties in:
* {@code com.sun.management.UnixOperatingSystemMXBean} and * {@code com.sun.management.UnixOperatingSystemMXBean} and
* {@code com.sun.management.OperatingSystemMXBean} * {@code com.sun.management.OperatingSystemMXBean}
* <p>
* If the given bean does not have the property, it is handled like {@code null}
* and not added to the given named list.
*/ */
static void addMXBeanProperty(PlatformManagedObject obj, Class<? extends PlatformManagedObject> intf, static <T extends PlatformManagedObject> void addMXBeanProperties(T obj, Class<? extends T> intf, NamedList<Object> info)
String property, NamedList<Object> info) { throws IntrospectionException, ReflectiveOperationException {
try { if (intf.isInstance(obj)) {
final String method = "get" + Character.toUpperCase( property.charAt(0) ) + property.substring( 1 ); final BeanInfo beanInfo = Introspector.getBeanInfo(intf, intf.getSuperclass(), Introspector.IGNORE_ALL_BEANINFO);
final Object v = intf.getMethod( method ).invoke( intf.cast(obj) ); for (final PropertyDescriptor desc : beanInfo.getPropertyDescriptors()) {
if( v != null ) { final String name = desc.getName();
info.add( property, v ); final Object v = desc.getReadMethod().invoke(obj);
if(v != null && info.get(name) == null) {
info.add(name, v);
}
} }
} catch (Exception e) {
log.warn("Cannot get property '{}' of MXBean interface: {}", property, intf.getName());
} }
} }

View File

@ -19,6 +19,7 @@ package org.apache.solr.handler.admin;
import java.lang.management.ManagementFactory; import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean; import java.lang.management.OperatingSystemMXBean;
import java.util.Arrays;
import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.LuceneTestCase;
import org.apache.solr.common.util.SimpleOrderedMap; import org.apache.solr.common.util.SimpleOrderedMap;
@ -26,7 +27,7 @@ import org.apache.solr.common.util.SimpleOrderedMap;
public class SystemInfoHandlerTest extends LuceneTestCase { public class SystemInfoHandlerTest extends LuceneTestCase {
public void testMagickGetter() { public void testMagickGetter() throws Exception {
OperatingSystemMXBean os = ManagementFactory.getOperatingSystemMXBean(); OperatingSystemMXBean os = ManagementFactory.getOperatingSystemMXBean();
@ -36,14 +37,14 @@ public class SystemInfoHandlerTest extends LuceneTestCase {
info.add( "version", os.getVersion() ); info.add( "version", os.getVersion() );
info.add( "arch", os.getArch() ); info.add( "arch", os.getArch() );
// make another using addMXBeanProperty() // make another using addMXBeanProperties()
SimpleOrderedMap<Object> info2 = new SimpleOrderedMap<>(); SimpleOrderedMap<Object> info2 = new SimpleOrderedMap<>();
SystemInfoHandler.addMXBeanProperty( os, OperatingSystemMXBean.class, "name", info2 ); SystemInfoHandler.addMXBeanProperties( os, OperatingSystemMXBean.class, info2 );
SystemInfoHandler.addMXBeanProperty( os, OperatingSystemMXBean.class, "version", info2 );
SystemInfoHandler.addMXBeanProperty( os, OperatingSystemMXBean.class, "arch", info2 );
// make sure they got the same thing // make sure they got the same thing
assertEquals( info.toString(), info2.toString() ); for (String p : Arrays.asList("name", "version", "arch")) {
assertEquals(info.get(p), info2.get(p));
}
} }
} }