Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

SOLR-6747: Add an optional caching option as a workaround for SOLR-6586.

Mark Robert Miller570058d937ba
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index 95416064dc9..f492cacf640 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -399,6 +399,9 @@ Other Changes
* SOLR-6570: Run SolrZkClient session watch asynchronously.
(Ramkumar Aiyengar via Mark Miller)
+
+* SOLR-6747: Add an optional caching option as a workaround for SOLR-6586.
+ (Mark Miller, Gregory Chanan)
================== 4.10.3 ==================
diff --git a/solr/core/src/java/org/apache/solr/core/JmxMonitoredMap.java b/solr/core/src/java/org/apache/solr/core/JmxMonitoredMap.java
index 0bf34349537..6bba774e7c0 100644
--- a/solr/core/src/java/org/apache/solr/core/JmxMonitoredMap.java
+++ b/solr/core/src/java/org/apache/solr/core/JmxMonitoredMap.java
@@ -53,6 +53,10 @@ public class JmxMonitoredMap<K, V> extends
private static final Logger LOG = LoggerFactory.getLogger(JmxMonitoredMap.class
.getName());
+ // set to true to use cached statistics NamedLists between getMBeanInfo calls to work
+ // around over calling getStatistics on MBeanInfos when iterating over all attributes (SOLR-6586)
+ private boolean useCachedStatsBetweenGetMBeanInfoCalls = Boolean.getBoolean("useCachedStatsBetweenGetMBeanInfoCalls");
+
private MBeanServer server = null;
private String jmxRootName;
@@ -144,7 +148,7 @@ public class JmxMonitoredMap<K, V> extends
ObjectName name = getObjectName(key, infoBean);
if (server.isRegistered(name))
server.unregisterMBean(name);
- SolrDynamicMBean mbean = new SolrDynamicMBean(coreHashCode, infoBean);
+ SolrDynamicMBean mbean = new SolrDynamicMBean(coreHashCode, infoBean, useCachedStatsBetweenGetMBeanInfoCalls);
server.registerMBean(mbean, name);
} catch (Exception e) {
LOG.warn( "Failed to register info bean: " + key, e);
@@ -213,8 +217,17 @@ public class JmxMonitoredMap<K, V> extends
private HashSet<String> staticStats;
private String coreHashCode;
-
+
+ private volatile NamedList cachedDynamicStats;
+
+ private boolean useCachedStatsBetweenGetMBeanInfoCalls;
+
public SolrDynamicMBean(String coreHashCode, SolrInfoMBean managedResource) {
+ this(coreHashCode, managedResource, false);
+ }
+
+ public SolrDynamicMBean(String coreHashCode, SolrInfoMBean managedResource, boolean useCachedStatsBetweenGetMBeanInfoCalls) {
+ this.useCachedStatsBetweenGetMBeanInfoCalls = useCachedStatsBetweenGetMBeanInfoCalls;
this.infoBean = managedResource;
staticStats = new HashSet<>();
@@ -242,6 +255,11 @@ public class JmxMonitoredMap<K, V> extends
try {
NamedList dynamicStats = infoBean.getStatistics();
+
+ if (useCachedStatsBetweenGetMBeanInfoCalls) {
+ cachedDynamicStats = dynamicStats;
+ }
+
if (dynamicStats != null) {
for (int i = 0; i < dynamicStats.size(); i++) {
String name = dynamicStats.getName(i);
@@ -303,8 +321,17 @@ public class JmxMonitoredMap<K, V> extends
throw new AttributeNotFoundException(attribute);
}
} else {
- NamedList list = infoBean.getStatistics();
- val = list.get(attribute);
+ NamedList stats = null;
+ if (useCachedStatsBetweenGetMBeanInfoCalls) {
+ NamedList cachedStats = this.cachedDynamicStats;
+ if (cachedStats != null) {
+ stats = cachedStats;
+ }
+ }
+ if (stats == null) {
+ stats = infoBean.getStatistics();
+ }
+ val = stats.get(attribute);
}
if (val != null) {
diff --git a/solr/core/src/test/org/apache/solr/core/MockInfoMBean.java b/solr/core/src/test/org/apache/solr/core/MockInfoMBean.java
new file mode 100644
index 00000000000..0b436247f0f
--- /dev/null
+++ b/solr/core/src/test/org/apache/solr/core/MockInfoMBean.java
@@ -0,0 +1,71 @@
+package org.apache.solr.core;
+
+import java.net.URL;
+
+import org.apache.solr.common.util.NamedList;
+import org.apache.solr.core.SolrInfoMBean.Category;
+
+/*
+ * 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.
+ */
+
+class MockInfoMBean implements SolrInfoMBean {
+ @Override
+ public String getName() {
+ return "mock";
+ }
+
+ @Override
+ public Category getCategory() {
+ return Category.OTHER;
+ }
+
+ @Override
+ public String getDescription() {
+ return "mock";
+ }
+
+ @Override
+ public URL[] getDocs() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public String getVersion() {
+ return "mock";
+ }
+
+ @Override
+ public String getSource() {
+ return "mock";
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public NamedList getStatistics() {
+ NamedList myList = new NamedList<Integer>();
+ myList.add("Integer", 123);
+ myList.add("Double",567.534);
+ myList.add("Long", 32352463l);
+ myList.add("Short", (short) 32768);
+ myList.add("Byte", (byte) 254);
+ myList.add("Float", 3.456f);
+ myList.add("String","testing");
+ myList.add("Object", new Object());
+ return myList;
+ }
+}
\ No newline at end of file
diff --git a/solr/core/src/test/org/apache/solr/core/TestJmxMonitoredMap.java b/solr/core/src/test/org/apache/solr/core/TestJmxMonitoredMap.java
index 11c5a045a00..6624d648b14 100644
--- a/solr/core/src/test/org/apache/solr/core/TestJmxMonitoredMap.java
+++ b/solr/core/src/test/org/apache/solr/core/TestJmxMonitoredMap.java
@@ -32,9 +32,9 @@ import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import javax.management.remote.rmi.RMIConnectorServer;
+
import java.io.IOException;
import java.net.ServerSocket;
-import java.net.URL;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.RMIServerSocketFactory;
import java.util.Set;
@@ -175,51 +175,4 @@ public class TestJmxMonitoredMap extends LuceneTestCase {
}
- private class MockInfoMBean implements SolrInfoMBean {
- @Override
- public String getName() {
- return "mock";
- }
-
- @Override
- public Category getCategory() {
- return Category.OTHER;
- }
-
- @Override
- public String getDescription() {
- return "mock";
- }
-
- @Override
- public URL[] getDocs() {
- // TODO Auto-generated method stub
- return null;
- }
-
- @Override
- public String getVersion() {
- return "mock";
- }
-
- @Override
- public String getSource() {
- return "mock";
- }
-
- @Override
- @SuppressWarnings("unchecked")
- public NamedList getStatistics() {
- NamedList myList = new NamedList<Integer>();
- myList.add("Integer", 123);
- myList.add("Double",567.534);
- myList.add("Long", 32352463l);
- myList.add("Short", (short) 32768);
- myList.add("Byte", (byte) 254);
- myList.add("Float", 3.456f);
- myList.add("String","testing");
- return myList;
- }
- }
-
}
diff --git a/solr/core/src/test/org/apache/solr/core/TestSolrDynamicMBean.java b/solr/core/src/test/org/apache/solr/core/TestSolrDynamicMBean.java
new file mode 100644
index 00000000000..eae4e799e89
--- /dev/null
+++ b/solr/core/src/test/org/apache/solr/core/TestSolrDynamicMBean.java
@@ -0,0 +1,87 @@
+/*
+ * 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 org.apache.lucene.util.LuceneTestCase;
+import org.apache.solr.core.JmxMonitoredMap.SolrDynamicMBean;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Test for JmxMonitoredMap
+ *
+ *
+ * @since solr 1.3
+ */
+public class TestSolrDynamicMBean extends LuceneTestCase {
+
+
+ @Override
+ @Before
+ public void setUp() throws Exception {
+ super.setUp();
+ }
+
+
+ @Test
+ public void testCachedStatsOption() throws Exception{
+ // SOLR-6747 Add an optional caching option as a workaround for SOLR-6586.
+
+ SolrInfoMBean solrmbeaninfo = new MockInfoMBean();
+ SolrDynamicMBean sdmbean = new SolrDynamicMBean("", solrmbeaninfo);
+
+ sdmbean.getMBeanInfo();
+
+ Object object1 = sdmbean.getAttribute("Object");
+ Object object2 = sdmbean.getAttribute("Object");
+
+ assertNotSame(object1, object2);
+
+ sdmbean.getMBeanInfo();
+
+ Object object12 = sdmbean.getAttribute("Object");
+ Object object22 = sdmbean.getAttribute("Object");
+
+ assertNotSame(object1, object12);
+ assertNotSame(object2, object22);
+
+
+ // test cached stats
+
+ solrmbeaninfo = new MockInfoMBean();
+ sdmbean = new SolrDynamicMBean("", solrmbeaninfo, true);
+
+ sdmbean.getMBeanInfo();
+
+ object1 = sdmbean.getAttribute("Object");
+ object2 = sdmbean.getAttribute("Object");
+
+ assertEquals(object1, object2);
+
+ sdmbean.getMBeanInfo();
+
+ object12 = sdmbean.getAttribute("Object");
+ object22 = sdmbean.getAttribute("Object");
+
+ assertNotSame(object1, object12);
+ assertNotSame(object2, object22);
+
+ assertEquals(object12, object22);
+
+ }
+
+}