Apache Lucene open-source search software
SOLR-9805 Don't use FileDescriptorRatioGauge - internally it uses reflection and doesn't work under Java 9. Instead use this opportunity to implement a more detailed OperatingSystemMetricSet. Add a unit test. Simplify some of the metric names.
Andrzej Bialecki80462df86efa
diff --git a/solr/core/src/java/org/apache/solr/metrics/OperatingSystemMetricSet.java b/solr/core/src/java/org/apache/solr/metrics/OperatingSystemMetricSet.javanew file mode 100644index 00000000000..b26386a2a1d--- /dev/null+++ b/solr/core/src/java/org/apache/solr/metrics/OperatingSystemMetricSet.java@@ -0,0 +1,80 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package org.apache.solr.metrics;++import javax.management.JMException;+import javax.management.MBeanServer;+import javax.management.ObjectName;+import java.lang.invoke.MethodHandles;+import java.util.HashMap;+import java.util.Map;++import com.codahale.metrics.JmxAttributeGauge;+import com.codahale.metrics.Metric;+import com.codahale.metrics.MetricSet;+import org.slf4j.Logger;+import org.slf4j.LoggerFactory;++/**+ * This is an extended replacement for {@link com.codahale.metrics.jvm.FileDescriptorRatioGauge}+ * - that class uses reflection and doesn't work under Java 9. We can also get much more+ * information about OS environment once we have to go through MBeanServer anyway.+ */+public class OperatingSystemMetricSet implements MetricSet {+ private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());++ /** Metric names - these correspond to known numeric MBean attributes. Depending on the OS and+ * Java implementation only some of them may be actually present.+ */+ public static final String[] METRICS = {+ "AvailableProcessors",+ "CommittedVirtualMemorySize",+ "FreePhysicalMemorySize",+ "FreeSwapSpaceSize",+ "MaxFileDescriptorCount",+ "OpenFileDescriptorCount",+ "ProcessCpuLoad",+ "ProcessCpuTime",+ "SystemLoadAverage",+ "TotalPhysicalMemorySize",+ "TotalSwapSpaceSize"+ };++ private final MBeanServer mBeanServer;++ public OperatingSystemMetricSet(MBeanServer mBeanServer) {+ this.mBeanServer = mBeanServer;+ }++ @Override+ public Map<String, Metric> getMetrics() {+ final Map<String, Metric> metrics = new HashMap<>();++ try {+ final ObjectName on = new ObjectName("java.lang:type=OperatingSystem");+ // verify that it exists+ mBeanServer.getMBeanInfo(on);+ for (String metric : METRICS) {+ metrics.put(metric, new JmxAttributeGauge(mBeanServer, on, metric));+ }+ } catch (JMException ignored) {+ log.debug("Unable to load OperatingSystem MBean", ignored);+ }++ return metrics;+ }+}diff --git a/solr/core/src/java/org/apache/solr/servlet/SolrDispatchFilter.java b/solr/core/src/java/org/apache/solr/servlet/SolrDispatchFilter.javaindex dbc4b35244a..a411bb34f2b 100644--- a/solr/core/src/java/org/apache/solr/servlet/SolrDispatchFilter.java+++ b/solr/core/src/java/org/apache/solr/servlet/SolrDispatchFilter.java@@ -49,7 +49,6 @@ import java.util.regex.Pattern;import com.codahale.metrics.jvm.BufferPoolMetricSet;import com.codahale.metrics.jvm.ClassLoadingGaugeSet;-import com.codahale.metrics.jvm.FileDescriptorRatioGauge;import com.codahale.metrics.jvm.GarbageCollectorMetricSet;import com.codahale.metrics.jvm.MemoryUsageGaugeSet;import com.codahale.metrics.jvm.ThreadStatesGaugeSet;@@ -69,6 +68,7 @@ import org.apache.solr.core.SolrCore;import org.apache.solr.core.SolrInfoMBean;import org.apache.solr.core.SolrResourceLoader;import org.apache.solr.core.SolrXmlConfig;+import org.apache.solr.metrics.OperatingSystemMetricSet;import org.apache.solr.metrics.SolrMetricManager;import org.apache.solr.request.SolrRequestInfo;import org.apache.solr.security.AuthenticationPlugin;@@ -187,9 +187,9 @@ public class SolrDispatchFilter extends BaseSolrFilter {SolrMetricManager metricManager = cores.getMetricManager();try {String registry = SolrMetricManager.getRegistryName(SolrInfoMBean.Group.jvm);- metricManager.registerAll(registry, new BufferPoolMetricSet(platformMBeanServer), true, "bufferPools");- metricManager.registerAll(registry, new ClassLoadingGaugeSet(), true, "classLoading");- metricManager.register(registry, new FileDescriptorRatioGauge(), true, "fileDescriptorRatio");+ metricManager.registerAll(registry, new BufferPoolMetricSet(platformMBeanServer), true, "buffers");+ metricManager.registerAll(registry, new ClassLoadingGaugeSet(), true, "classes");+ metricManager.registerAll(registry, new OperatingSystemMetricSet(platformMBeanServer), true, "os");metricManager.registerAll(registry, new GarbageCollectorMetricSet(), true, "gc");metricManager.registerAll(registry, new MemoryUsageGaugeSet(), true, "memory");metricManager.registerAll(registry, new ThreadStatesGaugeSet(), true, "threads"); // todo should we use CachedThreadStatesGaugeSet instead?diff --git a/solr/core/src/test/org/apache/solr/metrics/JvmMetricsTest.java b/solr/core/src/test/org/apache/solr/metrics/JvmMetricsTest.javanew file mode 100644index 00000000000..77c4e1aa1c4--- /dev/null+++ b/solr/core/src/test/org/apache/solr/metrics/JvmMetricsTest.java@@ -0,0 +1,67 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package org.apache.solr.metrics;++import javax.management.MBeanServer;+import java.lang.management.ManagementFactory;+import java.util.Map;++import com.codahale.metrics.Gauge;+import com.codahale.metrics.Metric;+import org.apache.solr.SolrJettyTestBase;+import org.junit.BeforeClass;+import org.junit.Test;++/**+ * Test {@link OperatingSystemMetricSet} and proper JVM metrics registration.+ */+public class JvmMetricsTest extends SolrJettyTestBase {++ @BeforeClass+ public static void beforeTest() throws Exception {+ createJetty(legacyExampleCollection1SolrHome());+ }++ @Test+ public void testOperatingSystemMetricsSet() throws Exception {+ MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();+ OperatingSystemMetricSet set = new OperatingSystemMetricSet(mBeanServer);+ Map<String, Metric> metrics = set.getMetrics();+ assertTrue(metrics.size() > 0);+ for (String metric : OperatingSystemMetricSet.METRICS) {+ Gauge<?> gauge = (Gauge<?>)metrics.get(metric);+ if (gauge == null) { // some are optional depending on OS+ continue;+ }+ double value = ((Number)gauge.getValue()).doubleValue();+ assertTrue(value >= 0);+ }+ }++ @Test+ public void testSetupJvmMetrics() throws Exception {+ SolrMetricManager metricManager = jetty.getCoreContainer().getMetricManager();+ Map<String,Metric> metrics = metricManager.registry("solr.jvm").getMetrics();+ assertTrue(metrics.size() > 0);+ assertTrue(metrics.toString(), metrics.entrySet().stream().filter(e -> e.getKey().startsWith("buffers.")).count() > 0);+ assertTrue(metrics.toString(), metrics.entrySet().stream().filter(e -> e.getKey().startsWith("classes.")).count() > 0);+ assertTrue(metrics.toString(), metrics.entrySet().stream().filter(e -> e.getKey().startsWith("os.")).count() > 0);+ assertTrue(metrics.toString(), metrics.entrySet().stream().filter(e -> e.getKey().startsWith("gc.")).count() > 0);+ assertTrue(metrics.toString(), metrics.entrySet().stream().filter(e -> e.getKey().startsWith("memory.")).count() > 0);+ assertTrue(metrics.toString(), metrics.entrySet().stream().filter(e -> e.getKey().startsWith("threads.")).count() > 0);+ }+}