PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/solr/core/src/test/org/apache/solr/core/TestJmxMonitoredMap.java

http://github.com/apache/lucene-solr
Java | 217 lines | 157 code | 32 blank | 28 comment | 4 complexity | 3f5b2eb493fd84c5d20e7b3a33f2618c MD5 | raw file
Possible License(s): LGPL-2.1, CPL-1.0, MPL-2.0-no-copyleft-exception, JSON, Apache-2.0, AGPL-1.0, GPL-2.0, GPL-3.0, MIT, BSD-3-Clause
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.solr.core;
  18. import org.apache.lucene.util.LuceneTestCase;
  19. import org.apache.solr.common.util.NamedList;
  20. import org.apache.solr.core.SolrConfig.JmxConfiguration;
  21. import org.junit.After;
  22. import org.junit.Before;
  23. import org.junit.Test;
  24. import org.slf4j.Logger;
  25. import org.slf4j.LoggerFactory;
  26. import javax.management.MBeanServerConnection;
  27. import javax.management.ObjectInstance;
  28. import javax.management.ObjectName;
  29. import javax.management.Query;
  30. import javax.management.remote.JMXConnector;
  31. import javax.management.remote.JMXConnectorFactory;
  32. import javax.management.remote.JMXServiceURL;
  33. import java.io.IOException;
  34. import java.lang.invoke.MethodHandles;
  35. import java.net.ServerSocket;
  36. import java.rmi.registry.LocateRegistry;
  37. import java.rmi.server.RMIServerSocketFactory;
  38. import java.util.Set;
  39. import static org.hamcrest.CoreMatchers.allOf;
  40. import static org.hamcrest.CoreMatchers.equalTo;
  41. import static org.hamcrest.CoreMatchers.instanceOf;
  42. /**
  43. * Test for JmxMonitoredMap
  44. *
  45. *
  46. * @since solr 1.3
  47. */
  48. public class TestJmxMonitoredMap extends LuceneTestCase {
  49. private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
  50. private int port = 0;
  51. private JMXConnector connector;
  52. private MBeanServerConnection mbeanServer;
  53. private JmxMonitoredMap<String, SolrInfoMBean> monitoredMap;
  54. @Override
  55. @Before
  56. public void setUp() throws Exception {
  57. super.setUp();
  58. String oldHost = System.getProperty("java.rmi.server.hostname");
  59. try {
  60. // this stupid sysprop thing is needed, because remote stubs use an
  61. // arbitrary local ip to connect
  62. // See: http://weblogs.java.net/blog/emcmanus/archive/2006/12/multihomed_comp.html
  63. System.setProperty("java.rmi.server.hostname", "127.0.0.1");
  64. class LocalhostRMIServerSocketFactory implements RMIServerSocketFactory {
  65. ServerSocket socket;
  66. @Override
  67. public ServerSocket createServerSocket(int port) throws IOException {
  68. return socket = new ServerSocket(port);
  69. }
  70. };
  71. LocalhostRMIServerSocketFactory factory = new LocalhostRMIServerSocketFactory();
  72. LocateRegistry.createRegistry(0, null, factory);
  73. port = factory.socket.getLocalPort();
  74. log.info("Using port: " + port);
  75. String url = "service:jmx:rmi:///jndi/rmi://127.0.0.1:"+port+"/solrjmx";
  76. JmxConfiguration config = new JmxConfiguration(true, null, url, null);
  77. monitoredMap = new JmxMonitoredMap<>("", "", config);
  78. JMXServiceURL u = new JMXServiceURL(url);
  79. connector = JMXConnectorFactory.connect(u);
  80. mbeanServer = connector.getMBeanServerConnection();
  81. } finally {
  82. if (oldHost == null) {
  83. System.clearProperty("java.rmi.server.hostname");
  84. } else {
  85. System.setProperty("java.rmi.server.hostname", oldHost);
  86. }
  87. }
  88. }
  89. @Override
  90. @After
  91. public void tearDown() throws Exception {
  92. try {
  93. connector.close();
  94. } catch (Exception e) {
  95. }
  96. super.tearDown();
  97. }
  98. @Test
  99. public void testTypeName() throws Exception{
  100. MockInfoMBean mock = new MockInfoMBean();
  101. monitoredMap.put("mock", mock);
  102. NamedList dynamicStats = mock.getStatistics();
  103. assertTrue(dynamicStats.size() != 0);
  104. assertTrue(dynamicStats.get("Integer") instanceof Integer);
  105. assertTrue(dynamicStats.get("Double") instanceof Double);
  106. assertTrue(dynamicStats.get("Long") instanceof Long);
  107. assertTrue(dynamicStats.get("Short") instanceof Short);
  108. assertTrue(dynamicStats.get("Byte") instanceof Byte);
  109. assertTrue(dynamicStats.get("Float") instanceof Float);
  110. assertTrue(dynamicStats.get("String") instanceof String);
  111. Set<ObjectInstance> objects = mbeanServer.queryMBeans(null, Query.match(
  112. Query.attr("name"), Query.value("mock")));
  113. ObjectName name = objects.iterator().next().getObjectName();
  114. assertMBeanTypeAndValue(name, "Integer", Integer.class, 123);
  115. assertMBeanTypeAndValue(name, "Double", Double.class, 567.534);
  116. assertMBeanTypeAndValue(name, "Long", Long.class, 32352463l);
  117. assertMBeanTypeAndValue(name, "Short", Short.class, (short) 32768);
  118. assertMBeanTypeAndValue(name, "Byte", Byte.class, (byte) 254);
  119. assertMBeanTypeAndValue(name, "Float", Float.class, 3.456f);
  120. assertMBeanTypeAndValue(name, "String",String.class, "testing");
  121. }
  122. @SuppressWarnings("unchecked")
  123. public void assertMBeanTypeAndValue(ObjectName name, String attr, Class type, Object value) throws Exception {
  124. assertThat(mbeanServer.getAttribute(name, attr),
  125. allOf(instanceOf(type), equalTo(value))
  126. );
  127. }
  128. @Test
  129. public void testPutRemoveClear() throws Exception {
  130. MockInfoMBean mock = new MockInfoMBean();
  131. monitoredMap.put("mock", mock);
  132. Set<ObjectInstance> objects = mbeanServer.queryMBeans(null, Query.match(
  133. Query.attr("name"), Query.value("mock")));
  134. assertFalse("No MBean for mock object found in MBeanServer", objects
  135. .isEmpty());
  136. monitoredMap.remove("mock");
  137. objects = mbeanServer.queryMBeans(null, Query.match(Query.attr("name"),
  138. Query.value("mock")));
  139. assertTrue("MBean for mock object found in MBeanServer even after removal",
  140. objects.isEmpty());
  141. monitoredMap.put("mock", mock);
  142. monitoredMap.put("mock2", mock);
  143. objects = mbeanServer.queryMBeans(null, Query.match(Query.attr("name"),
  144. Query.value("mock")));
  145. assertFalse("No MBean for mock object found in MBeanServer", objects
  146. .isEmpty());
  147. monitoredMap.clear();
  148. objects = mbeanServer.queryMBeans(null, Query.match(Query.attr("name"),
  149. Query.value("mock")));
  150. assertTrue(
  151. "MBean for mock object found in MBeanServer even after clear has been called",
  152. objects.isEmpty());
  153. }
  154. @Test
  155. public void testJmxAugmentedSolrInfoMBean() throws Exception {
  156. final MockInfoMBean mock = new MockInfoMBean();
  157. final String jmxKey = "jmx";
  158. final String jmxValue = "jmxValue";
  159. MockJmxAugmentedSolrInfoMBean mbean = new MockJmxAugmentedSolrInfoMBean(mock) {
  160. @Override
  161. public NamedList getStatisticsForJmx() {
  162. NamedList stats = getStatistics();
  163. stats.add(jmxKey, jmxValue);
  164. return stats;
  165. }
  166. };
  167. monitoredMap.put("mock", mbean);
  168. // assert getStatistics called when used as a map. Note can't use equals here to compare
  169. // because getStatistics returns a new Object each time.
  170. assertNull(monitoredMap.get("mock").getStatistics().get(jmxKey));
  171. // assert getStatisticsForJmx called when used as jmx server
  172. Set<ObjectInstance> objects = mbeanServer.queryMBeans(null, Query.match(
  173. Query.attr("name"), Query.value("mock")));
  174. ObjectName name = objects.iterator().next().getObjectName();
  175. assertMBeanTypeAndValue(name, jmxKey, jmxValue.getClass(), jmxValue);
  176. }
  177. private static abstract class MockJmxAugmentedSolrInfoMBean
  178. extends SolrInfoMBeanWrapper implements JmxMonitoredMap.JmxAugmentedSolrInfoMBean {
  179. public MockJmxAugmentedSolrInfoMBean(SolrInfoMBean mbean) {
  180. super(mbean);
  181. }
  182. @Override
  183. public abstract NamedList getStatisticsForJmx();
  184. }
  185. }