diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt index 1bc9cbbf32f..a0e8c6e4992 100644 --- a/solr/CHANGES.txt +++ b/solr/CHANGES.txt @@ -459,6 +459,8 @@ Bug Fixes * SOLR-11221: SolrJmxReporter broken on core reload. This resulted in some or most metrics not being reported via JMX after core reloads, depending on timing. (ab) +* SOLR-11235: Some SolrCore metrics should check if core is closed before reporting. (ab) + Optimizations ---------------------- diff --git a/solr/core/src/java/org/apache/solr/core/SolrCore.java b/solr/core/src/java/org/apache/solr/core/SolrCore.java index 641d1a1bb62..53708f18965 100644 --- a/solr/core/src/java/org/apache/solr/core/SolrCore.java +++ b/solr/core/src/java/org/apache/solr/core/SolrCore.java @@ -1133,9 +1133,9 @@ public final class SolrCore implements SolrInfoBean, SolrMetricProducer, Closeab manager.registerGauge(this, registry, () -> startTime, true, "startTime", Category.CORE.toString()); manager.registerGauge(this, registry, () -> getOpenCount(), true, "refCount", Category.CORE.toString()); manager.registerGauge(this, registry, () -> resourceLoader.getInstancePath().toString(), true, "instanceDir", Category.CORE.toString()); - manager.registerGauge(this, registry, () -> getIndexDir(), true, "indexDir", Category.CORE.toString()); - manager.registerGauge(this, registry, () -> getIndexSize(), true, "sizeInBytes", Category.INDEX.toString()); - manager.registerGauge(this, registry, () -> NumberUtils.readableSize(getIndexSize()), true, "size", Category.INDEX.toString()); + manager.registerGauge(this, registry, () -> isClosed() ? "(closed)" : getIndexDir(), true, "indexDir", Category.CORE.toString()); + manager.registerGauge(this, registry, () -> isClosed() ? 0 : getIndexSize(), true, "sizeInBytes", Category.INDEX.toString()); + manager.registerGauge(this, registry, () -> isClosed() ? "(closed)" : NumberUtils.readableSize(getIndexSize()), true, "size", Category.INDEX.toString()); if (coreContainer != null) { manager.registerGauge(this, registry, () -> coreContainer.getNamesForCore(this), true, "aliases", Category.CORE.toString()); final CloudDescriptor cd = getCoreDescriptor().getCloudDescriptor(); @@ -1488,6 +1488,16 @@ public final class SolrCore implements SolrInfoBean, SolrMetricProducer, Closeab } log.info(logid+" CLOSING SolrCore " + this); + // stop reporting metrics + try { + coreMetricManager.close(); + } catch (Throwable e) { + SolrException.log(log, e); + if (e instanceof Error) { + throw (Error) e; + } + } + if( closeHooks != null ) { for( CloseHook hook : closeHooks ) { try { @@ -1586,15 +1596,6 @@ public final class SolrCore implements SolrInfoBean, SolrMetricProducer, Closeab } } - try { - coreMetricManager.close(); - } catch (Throwable e) { - SolrException.log(log, e); - if (e instanceof Error) { - throw (Error) e; - } - } - // Close the snapshots meta-data directory. Directory snapshotsDir = snapshotMgr.getSnapshotsDir(); try { diff --git a/solr/core/src/test/org/apache/solr/metrics/reporters/SolrJmxReporterTest.java b/solr/core/src/test/org/apache/solr/metrics/reporters/SolrJmxReporterTest.java index 11cdf3715de..e02db4ba52f 100644 --- a/solr/core/src/test/org/apache/solr/metrics/reporters/SolrJmxReporterTest.java +++ b/solr/core/src/test/org/apache/solr/metrics/reporters/SolrJmxReporterTest.java @@ -16,6 +16,7 @@ */ package org.apache.solr.metrics.reporters; +import javax.management.InstanceNotFoundException; import javax.management.MBeanServer; import javax.management.ObjectInstance; import javax.management.ObjectName; @@ -174,6 +175,41 @@ public class SolrJmxReporterTest extends SolrTestCaseJ4 { rootName.equals(o.getObjectName().getDomain())).count()); } + private static boolean stopped = false; + + @Test + public void testClosedCore() throws Exception { + Set objects = mBeanServer.queryMBeans(new ObjectName("*:category=CORE,name=indexDir,*"), null); + assertEquals("Unexpected number of indexDir beans: " + objects.toString(), 1, objects.size()); + final ObjectInstance inst = objects.iterator().next(); + stopped = false; + try { + Thread t = new Thread() { + public void run() { + while (!stopped) { + try { + Object value = mBeanServer.getAttribute(inst.getObjectName(), "Value"); + assertNotNull(value); + } catch (InstanceNotFoundException x) { + // no longer present + break; + } catch (Exception e) { + fail("Unexpected error retrieving attribute: " + e.toString()); + } + } + } + }; + t.start(); + Thread.sleep(500); + h.getCoreContainer().unload(h.getCore().getName()); + Thread.sleep(2000); + objects = mBeanServer.queryMBeans(new ObjectName("*:category=CORE,name=indexDir,*"), null); + assertEquals("Unexpected number of beans after core closed: " + objects, 0, objects.size()); + } finally { + stopped = true; + } + } + @Test public void testEnabled() throws Exception { String root1 = PREFIX + TestUtil.randomSimpleString(random(), 5, 10);