You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ab...@apache.org on 2016/12/21 11:38:58 UTC

lucene-solr:master: 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 metr

Repository: lucene-solr
Updated Branches:
  refs/heads/master 19530fa64 -> 80462df86


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.


Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/80462df8
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/80462df8
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/80462df8

Branch: refs/heads/master
Commit: 80462df86efaf093fdebeac5eef1727c5fda968f
Parents: 19530fa
Author: Andrzej Bialecki <ab...@apache.org>
Authored: Wed Dec 21 12:36:18 2016 +0100
Committer: Andrzej Bialecki <ab...@apache.org>
Committed: Wed Dec 21 12:38:47 2016 +0100

----------------------------------------------------------------------
 .../solr/metrics/OperatingSystemMetricSet.java  | 80 ++++++++++++++++++++
 .../apache/solr/servlet/SolrDispatchFilter.java |  8 +-
 .../org/apache/solr/metrics/JvmMetricsTest.java | 67 ++++++++++++++++
 3 files changed, 151 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/80462df8/solr/core/src/java/org/apache/solr/metrics/OperatingSystemMetricSet.java
----------------------------------------------------------------------
diff --git a/solr/core/src/java/org/apache/solr/metrics/OperatingSystemMetricSet.java b/solr/core/src/java/org/apache/solr/metrics/OperatingSystemMetricSet.java
new file mode 100644
index 0000000..b26386a
--- /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;
+  }
+}

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/80462df8/solr/core/src/java/org/apache/solr/servlet/SolrDispatchFilter.java
----------------------------------------------------------------------
diff --git a/solr/core/src/java/org/apache/solr/servlet/SolrDispatchFilter.java b/solr/core/src/java/org/apache/solr/servlet/SolrDispatchFilter.java
index dbc4b35..a411bb3 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?

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/80462df8/solr/core/src/test/org/apache/solr/metrics/JvmMetricsTest.java
----------------------------------------------------------------------
diff --git a/solr/core/src/test/org/apache/solr/metrics/JvmMetricsTest.java b/solr/core/src/test/org/apache/solr/metrics/JvmMetricsTest.java
new file mode 100644
index 0000000..77c4e1a
--- /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);
+  }
+}