Apache Lucene open-source search software
SOLR-8892: Allow SolrInfoMBeans to return different statistics for /jmx vs web ui calls
Gregory Chananfd0d69974776
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 9e6da65d9ce..d90a628e83a 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -133,6 +133,9 @@ Other Changes* SOLR-8869: Optionally disable printing field cache entries in SolrFieldCacheMBean (Gregory Chanan)+* SOLR-8892: Allow SolrInfoMBeans to return different statistics for /jmx vs web ui calls.+ (Gregory Chana, Mark Miller)+================== 6.0.0 ==================Consult the LUCENE_CHANGES.txt file for additional, low level, changes in this releasediff --git a/solr/core/src/java/org/apache/solr/core/JmxMonitoredMap.java b/solr/core/src/java/org/apache/solr/core/JmxMonitoredMap.javaindex 0260f516d2a..57bde93decd 100644--- a/solr/core/src/java/org/apache/solr/core/JmxMonitoredMap.java+++ b/solr/core/src/java/org/apache/solr/core/JmxMonitoredMap.java@@ -258,7 +258,15 @@ public class JmxMonitoredMap<K, V> extendspublic SolrDynamicMBean(String coreHashCode, SolrInfoMBean managedResource, boolean useCachedStatsBetweenGetMBeanInfoCalls) {this.useCachedStatsBetweenGetMBeanInfoCalls = useCachedStatsBetweenGetMBeanInfoCalls;- this.infoBean = managedResource;+ if (managedResource instanceof JmxAugmentedSolrInfoMBean) {+ final JmxAugmentedSolrInfoMBean jmxSpecific = (JmxAugmentedSolrInfoMBean)managedResource;+ this.infoBean = new SolrInfoMBeanWrapper(jmxSpecific) {+ @Override+ public NamedList getStatistics() { return jmxSpecific.getStatisticsForJmx(); }+ };+ } else {+ this.infoBean = managedResource;+ }staticStats = new HashSet<>();// For which getters are already available in SolrInfoMBean@@ -412,4 +420,17 @@ public class JmxMonitoredMap<K, V> extendsthrow new UnsupportedOperationException("Operation not Supported");}}++ /**+ * SolrInfoMBean that provides JMX-specific statistics. Used, for example,+ * if generating full statistics is expensive; the expensive statistics can+ * be generated normally for use with the web ui, while an abbreviated version+ * are generated for period jmx use.+ */+ public interface JmxAugmentedSolrInfoMBean extends SolrInfoMBean {+ /**+ * JMX-specific statistics+ */+ public NamedList getStatisticsForJmx();+ }}diff --git a/solr/core/src/java/org/apache/solr/core/SolrInfoMBeanWrapper.java b/solr/core/src/java/org/apache/solr/core/SolrInfoMBeanWrapper.javanew file mode 100644index 00000000000..534b8844931--- /dev/null+++ b/solr/core/src/java/org/apache/solr/core/SolrInfoMBeanWrapper.java@@ -0,0 +1,62 @@+/*+ * 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.core;++import java.net.URL;++import org.apache.solr.common.util.NamedList;++/**+ * Wraps a {@link SolrInfoMBean}.+ */+public class SolrInfoMBeanWrapper implements SolrInfoMBean {+ private final SolrInfoMBean mbean;++ public SolrInfoMBeanWrapper(SolrInfoMBean mbean) {+ this.mbean = mbean;+ }++ /** {@inheritDoc} */+ @Override+ public String getName() { return mbean.getName(); }++ /** {@inheritDoc} */+ @Override+ public String getVersion() { return mbean.getVersion(); }++ /** {@inheritDoc} */+ @Override+ public String getDescription() { return mbean.getDescription(); }++ /** {@inheritDoc} */+ @Override+ public Category getCategory() { return mbean.getCategory(); }++ /** {@inheritDoc} */+ @Override+ public String getSource() { return mbean.getSource(); }++ /** {@inheritDoc} */+ @Override+ public URL[] getDocs() { return mbean.getDocs(); }++ /** {@inheritDoc} */+ @Override+ public NamedList getStatistics() { return mbean.getStatistics(); }++}diff --git a/solr/core/src/java/org/apache/solr/search/SolrFieldCacheMBean.java b/solr/core/src/java/org/apache/solr/search/SolrFieldCacheMBean.javaindex cfc68c4d687..da997081c2e 100644--- a/solr/core/src/java/org/apache/solr/search/SolrFieldCacheMBean.java+++ b/solr/core/src/java/org/apache/solr/search/SolrFieldCacheMBean.java@@ -24,14 +24,16 @@ import org.apache.solr.common.util.SimpleOrderedMap;import org.apache.solr.core.SolrCore;import org.apache.solr.core.SolrInfoMBean;+import org.apache.solr.core.JmxMonitoredMap.JmxAugmentedSolrInfoMBean;/*** A SolrInfoMBean that provides introspection of the Solr FieldCache**/-public class SolrFieldCacheMBean implements SolrInfoMBean {+public class SolrFieldCacheMBean implements JmxAugmentedSolrInfoMBean {private boolean disableEntryList = Boolean.getBoolean("disableSolrFieldCacheMBeanEntryList");+ private boolean disableJmxEntryList = Boolean.getBoolean("disableSolrFieldCacheMBeanEntryListJmx");@Overridepublic String getName() { return this.getClass().getName(); }@@ -51,10 +53,19 @@ public class SolrFieldCacheMBean implements SolrInfoMBean {}@Overridepublic NamedList getStatistics() {+ return getStats(!disableEntryList);+ }++ @Override+ public NamedList getStatisticsForJmx() {+ return getStats(!disableEntryList && !disableJmxEntryList);+ }++ private NamedList getStats(boolean listEntries) {NamedList stats = new SimpleOrderedMap();String[] entries = UninvertingReader.getUninvertedStats();stats.add("entries_count", entries.length);- if (!disableEntryList) {+ if (listEntries) {for (int i = 0; i < entries.length; i++) {stats.add("entry#" + i, entries[i]);}diff --git a/solr/core/src/test/org/apache/solr/core/TestJmxMonitoredMap.java b/solr/core/src/test/org/apache/solr/core/TestJmxMonitoredMap.javaindex e98b7902900..9f1e2ab515b 100644--- a/solr/core/src/test/org/apache/solr/core/TestJmxMonitoredMap.java+++ b/solr/core/src/test/org/apache/solr/core/TestJmxMonitoredMap.java@@ -180,4 +180,41 @@ public class TestJmxMonitoredMap extends LuceneTestCase {}+ @Test+ public void testJmxAugmentedSolrInfoMBean() throws Exception {+ final MockInfoMBean mock = new MockInfoMBean();+ final String jmxKey = "jmx";+ final String jmxValue = "jmxValue";++ MockJmxAugmentedSolrInfoMBean mbean = new MockJmxAugmentedSolrInfoMBean(mock) {+ @Override+ public NamedList getStatisticsForJmx() {+ NamedList stats = getStatistics();+ stats.add(jmxKey, jmxValue);+ return stats;+ }+ };+ monitoredMap.put("mock", mbean);++ // assert getStatistics called when used as a map. Note can't use equals here to compare+ // because getStatistics returns a new Object each time.+ assertNull(monitoredMap.get("mock").getStatistics().get(jmxKey));++ // assert getStatisticsForJmx called when used as jmx server+ Set<ObjectInstance> objects = mbeanServer.queryMBeans(null, Query.match(+ Query.attr("name"), Query.value("mock")));+ ObjectName name = objects.iterator().next().getObjectName();+ assertMBeanTypeAndValue(name, jmxKey, jmxValue.getClass(), jmxValue);+ }++ private static abstract class MockJmxAugmentedSolrInfoMBean+ extends SolrInfoMBeanWrapper implements JmxMonitoredMap.JmxAugmentedSolrInfoMBean {++ public MockJmxAugmentedSolrInfoMBean(SolrInfoMBean mbean) {+ super(mbean);+ }++ @Override+ public abstract NamedList getStatisticsForJmx();+ }}diff --git a/solr/core/src/test/org/apache/solr/search/TestSolrFieldCacheMBean.java b/solr/core/src/test/org/apache/solr/search/TestSolrFieldCacheMBean.javaindex b90430e9ef6..5343f7345fb 100644--- a/solr/core/src/test/org/apache/solr/search/TestSolrFieldCacheMBean.java+++ b/solr/core/src/test/org/apache/solr/search/TestSolrFieldCacheMBean.java@@ -42,20 +42,40 @@ public class TestSolrFieldCacheMBean extends SolrTestCaseJ4 {assertU(commit());assertQ(req("q", "*:*", "sort", "id asc"), "//*[@numFound='1']");- SolrFieldCacheMBean mbean = new SolrFieldCacheMBean();- NamedList stats = mbean.getStatistics();- assert(new Integer(stats.get("entries_count").toString()) > 0);- assertNotNull(stats.get("entry#0"));+ // Test with entry list enabled+ assertEntryListIncluded(false);// Test again with entry list disabledSystem.setProperty("disableSolrFieldCacheMBeanEntryList", "true");try {- mbean = new SolrFieldCacheMBean();- stats = mbean.getStatistics();- assert(new Integer(stats.get("entries_count").toString()) > 0);- assertNull(stats.get("entry#0"));+ assertEntryListNotIncluded(false);} finally {System.clearProperty("disableSolrFieldCacheMBeanEntryList");}++ // Test with entry list enabled for jmx+ assertEntryListIncluded(true);++ // Test with entry list disabled for jmx+ System.setProperty("disableSolrFieldCacheMBeanEntryListJmx", "true");+ try {+ assertEntryListNotIncluded(true);+ } finally {+ System.clearProperty("disableSolrFieldCacheMBeanEntryListJmx");+ }+ }++ private void assertEntryListIncluded(boolean checkJmx) {+ SolrFieldCacheMBean mbean = new SolrFieldCacheMBean();+ NamedList stats = checkJmx ? mbean.getStatisticsForJmx() : mbean.getStatistics();+ assert(new Integer(stats.get("entries_count").toString()) > 0);+ assertNotNull(stats.get("entry#0"));+ }++ private void assertEntryListNotIncluded(boolean checkJmx) {+ SolrFieldCacheMBean mbean = new SolrFieldCacheMBean();+ NamedList stats = checkJmx ? mbean.getStatisticsForJmx() : mbean.getStatistics();+ assert(new Integer(stats.get("entries_count").toString()) > 0);+ assertNull(stats.get("entry#0"));}}