SOLR-13870: SolrJmxReporterTest no longer depends on (buggy) getNextAvailablePort()

(cherry picked from commit 367fe2f341)
This commit is contained in:
Chris Hostetter 2019-11-12 09:42:05 -07:00
parent d4f2a84df5
commit 26b7799af6
1 changed files with 42 additions and 24 deletions

View File

@ -18,10 +18,11 @@ package org.apache.solr.metrics.reporters;
import javax.management.InstanceNotFoundException; import javax.management.InstanceNotFoundException;
import javax.management.MBeanServer; import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.ObjectInstance; import javax.management.ObjectInstance;
import javax.management.ObjectName; import javax.management.ObjectName;
import java.rmi.registry.LocateRegistry; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Random; import java.util.Random;
@ -41,6 +42,7 @@ import org.apache.solr.metrics.SolrMetricReporter;
import org.apache.solr.metrics.SolrMetricTestUtils; import org.apache.solr.metrics.SolrMetricTestUtils;
import org.apache.solr.schema.FieldType; import org.apache.solr.schema.FieldType;
import org.junit.After; import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before; import org.junit.Before;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
@ -49,25 +51,28 @@ public class SolrJmxReporterTest extends SolrTestCaseJ4 {
private static final int MAX_ITERATIONS = 20; private static final int MAX_ITERATIONS = 20;
private static int jmxPort; /** The MbeanServer started by the test, that we expect the SolrJmxReporter to find by agentId */
private static MBeanServer TEST_MBEAN_SERVER;
private static String PREFIX; private static String PREFIX;
private String domain; private String domain;
private SolrCoreMetricManager coreMetricManager; private SolrCoreMetricManager coreMetricManager;
private SolrMetricManager metricManager; private SolrMetricManager metricManager;
private SolrJmxReporter reporter;
private MBeanServer mBeanServer;
private String reporterName;
private String rootName; private String rootName;
@BeforeClass @BeforeClass
public static void init() throws Exception { public static void init() throws Exception {
jmxPort = getNextAvailablePort(); TEST_MBEAN_SERVER = MBeanServerFactory.createMBeanServer();
assertFalse(jmxPort == -1);
LocateRegistry.createRegistry(jmxPort);
PREFIX = getSimpleClassName() + "-"; PREFIX = getSimpleClassName() + "-";
} }
@AfterClass
public static void shutdown() throws Exception {
if (null != TEST_MBEAN_SERVER) {
MBeanServerFactory.releaseMBeanServer(TEST_MBEAN_SERVER);
TEST_MBEAN_SERVER = null;
}
}
@Before @Before
public void beforeTest() throws Exception { public void beforeTest() throws Exception {
@ -85,14 +90,16 @@ public class SolrJmxReporterTest extends SolrTestCaseJ4 {
Map<String, SolrMetricReporter> reporters = metricManager.getReporters(coreMetricManager.getRegistryName()); Map<String, SolrMetricReporter> reporters = metricManager.getReporters(coreMetricManager.getRegistryName());
assertTrue("reporters.size should be > 0, but was + " + reporters.size(), reporters.size() > 0); assertTrue("reporters.size should be > 0, but was + " + reporters.size(), reporters.size() > 0);
reporterName = pluginInfo.name; String reporterName = pluginInfo.name;
String taggedName = reporterName + "@" + coreMetricManager.getTag(); String taggedName = reporterName + "@" + coreMetricManager.getTag();
assertNotNull("reporter " + taggedName + " not present among " + reporters, reporters.get(taggedName)); assertNotNull("reporter " + taggedName + " not present among " + reporters, reporters.get(taggedName));
assertTrue("wrong reporter class: " + reporters.get(taggedName), reporters.get(taggedName) instanceof SolrJmxReporter); assertTrue("wrong reporter class: " + reporters.get(taggedName), reporters.get(taggedName) instanceof SolrJmxReporter);
reporter = (SolrJmxReporter) reporters.get(taggedName); SolrJmxReporter reporter = (SolrJmxReporter) reporters.get(taggedName);
mBeanServer = reporter.getMBeanServer(); assertNotNull("MBean server not found on reporter", reporter.getMBeanServer());
assertNotNull("MBean server not found.", mBeanServer); assertEquals("Wrong MBeanServer found on reporter",
TEST_MBEAN_SERVER,
reporter.getMBeanServer());
} }
private PluginInfo createReporterPluginInfo(String rootName, boolean enabled) { private PluginInfo createReporterPluginInfo(String rootName, boolean enabled) {
@ -100,13 +107,21 @@ public class SolrJmxReporterTest extends SolrTestCaseJ4 {
String className = SolrJmxReporter.class.getName(); String className = SolrJmxReporter.class.getName();
String reporterName = PREFIX + TestUtil.randomSimpleString(random, 5, 10); String reporterName = PREFIX + TestUtil.randomSimpleString(random, 5, 10);
Map<String, Object> attrs = new HashMap<>(); Map<String, Object> attrs = new HashMap<>();
attrs.put(FieldType.CLASS_NAME, className); attrs.put(FieldType.CLASS_NAME, className);
attrs.put(CoreAdminParams.NAME, reporterName); attrs.put(CoreAdminParams.NAME, reporterName);
attrs.put("rootName", rootName); attrs.put("rootName", rootName);
attrs.put("enabled", enabled); attrs.put("enabled", enabled);
attrs.put("serviceUrl", "service:jmx:rmi:///jndi/rmi://localhost:" + jmxPort + "/solrjmx");
try {
String agentId = (String) TEST_MBEAN_SERVER.getAttribute
(new ObjectName("JMImplementation:type=MBeanServerDelegate"),
"MBeanServerId");
attrs.put("agentId", agentId);
} catch (Exception e) {
throw new RuntimeException("Unable to determine agentId of MBeanServer: " + e.getMessage(), e);
}
boolean shouldOverrideDomain = random.nextBoolean(); boolean shouldOverrideDomain = random.nextBoolean();
if (shouldOverrideDomain) { if (shouldOverrideDomain) {
domain = PREFIX + TestUtil.randomSimpleString(random); domain = PREFIX + TestUtil.randomSimpleString(random);
@ -121,13 +136,16 @@ public class SolrJmxReporterTest extends SolrTestCaseJ4 {
if (null == metricManager) { if (null == metricManager) {
return; // test failed to init, nothing to cleanup return; // test failed to init, nothing to cleanup
} }
metricManager.closeReporters(coreMetricManager.getRegistryName()); metricManager.closeReporters(coreMetricManager.getRegistryName());
Set<ObjectInstance> objects =
mBeanServer.queryMBeans(ObjectName.getInstance(domain + ":*"), null); // sanity check there are no longer any registered beans in our domain....
assertTrue(objects.isEmpty()); assertEquals(Collections.emptySet(),
TEST_MBEAN_SERVER.queryMBeans(ObjectName.getInstance(domain + ":*"), null));
coreMetricManager.close(); coreMetricManager.close();
deleteCore(); deleteCore();
} }
@Test @Test
@ -145,7 +163,7 @@ public class SolrJmxReporterTest extends SolrTestCaseJ4 {
coreMetricManager.registerMetricProducer(scope, producer); coreMetricManager.registerMetricProducer(scope, producer);
registered.putAll(metrics); registered.putAll(metrics);
//waitForListener(); //waitForListener();
Set<ObjectInstance> objects = mBeanServer.queryMBeans(null, null); Set<ObjectInstance> objects = TEST_MBEAN_SERVER.queryMBeans(null, null);
assertEquals(registered.size(), objects.stream(). assertEquals(registered.size(), objects.stream().
filter(o -> scope.equals(o.getObjectName().getKeyProperty("scope")) && filter(o -> scope.equals(o.getObjectName().getKeyProperty("scope")) &&
rootName.equals(o.getObjectName().getDomain())).count()); rootName.equals(o.getObjectName().getDomain())).count());
@ -161,7 +179,7 @@ public class SolrJmxReporterTest extends SolrTestCaseJ4 {
Map<String, Counter> metrics = SolrMetricTestUtils.getRandomMetrics(random, true); Map<String, Counter> metrics = SolrMetricTestUtils.getRandomMetrics(random, true);
SolrMetricProducer producer = SolrMetricTestUtils.getProducerOf(metricManager, category, scope, metrics); SolrMetricProducer producer = SolrMetricTestUtils.getProducerOf(metricManager, category, scope, metrics);
coreMetricManager.registerMetricProducer(scope, producer); coreMetricManager.registerMetricProducer(scope, producer);
Set<ObjectInstance> objects = mBeanServer.queryMBeans(null, null); Set<ObjectInstance> objects = TEST_MBEAN_SERVER.queryMBeans(null, null);
assertEquals(metrics.size(), objects.stream(). assertEquals(metrics.size(), objects.stream().
filter(o -> scope.equals(o.getObjectName().getKeyProperty("scope")) && filter(o -> scope.equals(o.getObjectName().getKeyProperty("scope")) &&
o.getObjectName().getDomain().equals(rootName)).count()); o.getObjectName().getDomain().equals(rootName)).count());
@ -172,7 +190,7 @@ public class SolrJmxReporterTest extends SolrTestCaseJ4 {
pluginInfo, String.valueOf(coreMetricManager.getCore().hashCode())); pluginInfo, String.valueOf(coreMetricManager.getCore().hashCode()));
coreMetricManager.registerMetricProducer(scope, producer); coreMetricManager.registerMetricProducer(scope, producer);
objects = mBeanServer.queryMBeans(null, null); objects = TEST_MBEAN_SERVER.queryMBeans(null, null);
assertEquals(metrics.size(), objects.stream(). assertEquals(metrics.size(), objects.stream().
filter(o -> scope.equals(o.getObjectName().getKeyProperty("scope")) && filter(o -> scope.equals(o.getObjectName().getKeyProperty("scope")) &&
rootName.equals(o.getObjectName().getDomain())).count()); rootName.equals(o.getObjectName().getDomain())).count());
@ -182,7 +200,7 @@ public class SolrJmxReporterTest extends SolrTestCaseJ4 {
@Test @Test
public void testClosedCore() throws Exception { public void testClosedCore() throws Exception {
Set<ObjectInstance> objects = mBeanServer.queryMBeans(new ObjectName("*:category=CORE,name=indexDir,*"), null); Set<ObjectInstance> objects = TEST_MBEAN_SERVER.queryMBeans(new ObjectName("*:category=CORE,name=indexDir,*"), null);
assertEquals("Unexpected number of indexDir beans: " + objects.toString(), 1, objects.size()); assertEquals("Unexpected number of indexDir beans: " + objects.toString(), 1, objects.size());
final ObjectInstance inst = objects.iterator().next(); final ObjectInstance inst = objects.iterator().next();
stopped = false; stopped = false;
@ -191,7 +209,7 @@ public class SolrJmxReporterTest extends SolrTestCaseJ4 {
public void run() { public void run() {
while (!stopped) { while (!stopped) {
try { try {
Object value = mBeanServer.getAttribute(inst.getObjectName(), "Value"); Object value = TEST_MBEAN_SERVER.getAttribute(inst.getObjectName(), "Value");
assertNotNull(value); assertNotNull(value);
} catch (InstanceNotFoundException x) { } catch (InstanceNotFoundException x) {
// no longer present // no longer present
@ -206,7 +224,7 @@ public class SolrJmxReporterTest extends SolrTestCaseJ4 {
Thread.sleep(500); Thread.sleep(500);
h.getCoreContainer().unload(h.getCore().getName()); h.getCoreContainer().unload(h.getCore().getName());
Thread.sleep(2000); Thread.sleep(2000);
objects = mBeanServer.queryMBeans(new ObjectName("*:category=CORE,name=indexDir,*"), null); objects = TEST_MBEAN_SERVER.queryMBeans(new ObjectName("*:category=CORE,name=indexDir,*"), null);
assertEquals("Unexpected number of beans after core closed: " + objects, 0, objects.size()); assertEquals("Unexpected number of beans after core closed: " + objects, 0, objects.size());
} finally { } finally {
stopped = true; stopped = true;
@ -235,7 +253,7 @@ public class SolrJmxReporterTest extends SolrTestCaseJ4 {
Map<String, Counter> metrics = SolrMetricTestUtils.getRandomMetrics(random(), true); Map<String, Counter> metrics = SolrMetricTestUtils.getRandomMetrics(random(), true);
SolrMetricProducer producer = SolrMetricTestUtils.getProducerOf(metricManager, category, scope, metrics); SolrMetricProducer producer = SolrMetricTestUtils.getProducerOf(metricManager, category, scope, metrics);
coreMetricManager.registerMetricProducer(scope, producer); coreMetricManager.registerMetricProducer(scope, producer);
Set<ObjectInstance> objects = mBeanServer.queryMBeans(null, null); Set<ObjectInstance> objects = TEST_MBEAN_SERVER.queryMBeans(null, null);
assertEquals(metrics.size(), objects.stream(). assertEquals(metrics.size(), objects.stream().
filter(o -> scope.equals(o.getObjectName().getKeyProperty("scope")) && filter(o -> scope.equals(o.getObjectName().getKeyProperty("scope")) &&
root1.equals(o.getObjectName().getDomain())).count()); root1.equals(o.getObjectName().getDomain())).count());