From e58a90f18d9823404f3f4ff59bffedf31d093a0d Mon Sep 17 00:00:00 2001 From: Andrzej Bialecki Date: Mon, 4 Nov 2019 15:30:21 +0100 Subject: [PATCH] SOLR-13858: Clean up SolrInfoBean / SolrMetricProducer API. --- solr/CHANGES.txt | 3 + .../handler/dataimport/DataImportHandler.java | 2 +- .../org/apache/solr/core/CoreContainer.java | 26 ++--- .../solr/core/HdfsDirectoryFactory.java | 5 + .../java/org/apache/solr/core/SolrCore.java | 48 ++++------ .../org/apache/solr/core/SolrInfoBean.java | 55 +---------- .../solr/handler/ReplicationHandler.java | 20 ++-- .../solr/handler/RequestHandlerBase.java | 26 ++--- .../solr/handler/admin/PluginInfoHandler.java | 4 +- .../handler/admin/SolrInfoMBeanHandler.java | 4 +- .../handler/component/SearchComponent.java | 18 ++-- .../handler/component/SuggestComponent.java | 4 +- .../handler/component/TermsComponent.java | 2 +- .../highlight/HighlightingPluginBase.java | 10 +- .../solr/metrics/SolrCoreMetricManager.java | 27 +++--- .../solr/metrics/SolrMetricManager.java | 36 +++---- .../solr/metrics/SolrMetricProducer.java | 10 +- .../solr/metrics/SolrMetricsContext.java | 96 ++++++++++++++----- .../org/apache/solr/search/CaffeineCache.java | 7 +- .../org/apache/solr/search/FastLRUCache.java | 8 +- .../java/org/apache/solr/search/LFUCache.java | 7 +- .../java/org/apache/solr/search/LRUCache.java | 7 +- .../org/apache/solr/search/QParserPlugin.java | 11 ++- .../org/apache/solr/search/SolrCache.java | 5 +- .../apache/solr/search/SolrCacheHolder.java | 17 +--- .../solr/search/SolrFieldCacheBean.java | 13 +-- .../apache/solr/search/SolrIndexSearcher.java | 36 +++---- .../solr/security/AuditLoggerPlugin.java | 28 +++--- .../solr/security/AuthenticationPlugin.java | 24 ++--- .../apache/solr/store/blockcache/Metrics.java | 10 +- .../solr/store/hdfs/HdfsLocalityReporter.java | 16 +--- .../solr/update/DirectUpdateHandler2.java | 47 +++++---- .../java/org/apache/solr/update/PeerSync.java | 13 ++- .../solr/update/PeerSyncWithLeader.java | 13 ++- .../apache/solr/update/SolrIndexWriter.java | 24 ++--- .../org/apache/solr/update/UpdateHandler.java | 11 +-- .../org/apache/solr/update/UpdateLog.java | 19 ++-- .../solr/update/UpdateShardHandler.java | 10 +- .../InstrumentedHttpListenerFactory.java | 7 +- ...tedPoolingHttpClientConnectionManager.java | 18 +++- .../apache/solr/util/stats/MetricUtils.java | 10 +- .../apache/solr/cloud/TestTlogReplica.java | 6 +- .../org/apache/solr/core/MockInfoBean.java | 7 +- .../MockQuerySenderListenerReqHandler.java | 2 +- .../apache/solr/core/RequestHandlersTest.java | 5 +- .../solr/handler/admin/MBeansHandlerTest.java | 20 ++-- .../handler/admin/MetricsHandlerTest.java | 2 +- .../solr/metrics/SolrMetricTestUtils.java | 9 +- .../solr/search/join/TestNestedDocsSort.java | 4 +- 49 files changed, 370 insertions(+), 442 deletions(-) diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt index adeb9ff4684..7606233b608 100644 --- a/solr/CHANGES.txt +++ b/solr/CHANGES.txt @@ -72,6 +72,9 @@ Upgrade Notes followed by a space (e.g. {shape=square, color=yellow} rather than {shape=square,color=yellow}) for consistency with other java.util.Map implementations based on AbstractMap (Chris Hennick). +* SOLR-13858: SolrMetricProducer / SolrInfoBean API has changed, third-party components need to be + updated. (ab) + Improvements ---------------------- diff --git a/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/DataImportHandler.java b/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/DataImportHandler.java index 8b64b6ffb30..c48c332616f 100644 --- a/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/DataImportHandler.java +++ b/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/DataImportHandler.java @@ -299,7 +299,7 @@ public class DataImportHandler extends RequestHandlerBase implements map.put(DataImporter.MSG.TOTAL_DOCS_SKIPPED, cumulative.skipDocCount); } }); - solrMetricsContext.gauge(this, metrics, true, "importer", getCategory().toString(), scope); + solrMetricsContext.gauge(metrics, true, "importer", getCategory().toString(), scope); } // //////////////////////SolrInfoMBeans methods ////////////////////// diff --git a/solr/core/src/java/org/apache/solr/core/CoreContainer.java b/solr/core/src/java/org/apache/solr/core/CoreContainer.java index 551e62588e3..62a0ce96737 100644 --- a/solr/core/src/java/org/apache/solr/core/CoreContainer.java +++ b/solr/core/src/java/org/apache/solr/core/CoreContainer.java @@ -694,20 +694,20 @@ public class CoreContainer { // initialize gauges for reporting the number of cores and disk total/free - solrMetricsContext.gauge(null, () -> solrCores.getCores().size(), + solrMetricsContext.gauge(() -> solrCores.getCores().size(), true, "loaded", SolrInfoBean.Category.CONTAINER.toString(), "cores"); - solrMetricsContext.gauge(null, () -> solrCores.getLoadedCoreNames().size() - solrCores.getCores().size(), + solrMetricsContext.gauge(() -> solrCores.getLoadedCoreNames().size() - solrCores.getCores().size(), true, "lazy", SolrInfoBean.Category.CONTAINER.toString(), "cores"); - solrMetricsContext.gauge(null, () -> solrCores.getAllCoreNames().size() - solrCores.getLoadedCoreNames().size(), + solrMetricsContext.gauge(() -> solrCores.getAllCoreNames().size() - solrCores.getLoadedCoreNames().size(), true, "unloaded", SolrInfoBean.Category.CONTAINER.toString(), "cores"); Path dataHome = cfg.getSolrDataHome() != null ? cfg.getSolrDataHome() : cfg.getCoreRootDirectory(); - solrMetricsContext.gauge(null, () -> dataHome.toFile().getTotalSpace(), + solrMetricsContext.gauge(() -> dataHome.toFile().getTotalSpace(), true, "totalSpace", SolrInfoBean.Category.CONTAINER.toString(), "fs"); - solrMetricsContext.gauge(null, () -> dataHome.toFile().getUsableSpace(), + solrMetricsContext.gauge(() -> dataHome.toFile().getUsableSpace(), true, "usableSpace", SolrInfoBean.Category.CONTAINER.toString(), "fs"); - solrMetricsContext.gauge(null, () -> dataHome.toAbsolutePath().toString(), + solrMetricsContext.gauge(() -> dataHome.toAbsolutePath().toString(), true, "path", SolrInfoBean.Category.CONTAINER.toString(), "fs"); - solrMetricsContext.gauge(null, () -> { + solrMetricsContext.gauge(() -> { try { return org.apache.lucene.util.IOUtils.spins(dataHome.toAbsolutePath()); } catch (IOException e) { @@ -716,13 +716,13 @@ public class CoreContainer { } }, true, "spins", SolrInfoBean.Category.CONTAINER.toString(), "fs"); - solrMetricsContext.gauge(null, () -> cfg.getCoreRootDirectory().toFile().getTotalSpace(), + solrMetricsContext.gauge(() -> cfg.getCoreRootDirectory().toFile().getTotalSpace(), true, "totalSpace", SolrInfoBean.Category.CONTAINER.toString(), "fs", "coreRoot"); - solrMetricsContext.gauge(null, () -> cfg.getCoreRootDirectory().toFile().getUsableSpace(), + solrMetricsContext.gauge(() -> cfg.getCoreRootDirectory().toFile().getUsableSpace(), true, "usableSpace", SolrInfoBean.Category.CONTAINER.toString(), "fs", "coreRoot"); - solrMetricsContext.gauge(null, () -> cfg.getCoreRootDirectory().toAbsolutePath().toString(), + solrMetricsContext.gauge(() -> cfg.getCoreRootDirectory().toAbsolutePath().toString(), true, "path", SolrInfoBean.Category.CONTAINER.toString(), "fs", "coreRoot"); - solrMetricsContext.gauge(null, () -> { + solrMetricsContext.gauge(() -> { try { return org.apache.lucene.util.IOUtils.spins(cfg.getCoreRootDirectory().toAbsolutePath()); } catch (IOException e) { @@ -732,9 +732,9 @@ public class CoreContainer { }, true, "spins", SolrInfoBean.Category.CONTAINER.toString(), "fs", "coreRoot"); // add version information - solrMetricsContext.gauge(null, () -> this.getClass().getPackage().getSpecificationVersion(), + solrMetricsContext.gauge(() -> this.getClass().getPackage().getSpecificationVersion(), true, "specification", SolrInfoBean.Category.CONTAINER.toString(), "version"); - solrMetricsContext.gauge(null, () -> this.getClass().getPackage().getImplementationVersion(), + solrMetricsContext.gauge(() -> this.getClass().getPackage().getImplementationVersion(), true, "implementation", SolrInfoBean.Category.CONTAINER.toString(), "version"); SolrFieldCacheBean fieldCacheBean = new SolrFieldCacheBean(); diff --git a/solr/core/src/java/org/apache/solr/core/HdfsDirectoryFactory.java b/solr/core/src/java/org/apache/solr/core/HdfsDirectoryFactory.java index 942f429eaff..1ce0872126b 100644 --- a/solr/core/src/java/org/apache/solr/core/HdfsDirectoryFactory.java +++ b/solr/core/src/java/org/apache/solr/core/HdfsDirectoryFactory.java @@ -509,6 +509,11 @@ public class HdfsDirectoryFactory extends CachingDirectoryFactory implements Sol LocalityHolder.reporter.initializeMetrics(parentContext, scope); } + @Override + public SolrMetricsContext getSolrMetricsContext() { + return null; + } + @Override public void inform(SolrCore core) { setHost(core.getCoreContainer().getHostName()); 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 09067b18978..2c6b1750fcd 100644 --- a/solr/core/src/java/org/apache/solr/core/SolrCore.java +++ b/solr/core/src/java/org/apache/solr/core/SolrCore.java @@ -58,7 +58,6 @@ import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.locks.ReentrantLock; import com.codahale.metrics.Counter; -import com.codahale.metrics.MetricRegistry; import com.codahale.metrics.Timer; import com.google.common.collect.Iterators; import com.google.common.collect.MapMaker; @@ -182,7 +181,7 @@ import static org.apache.solr.common.params.CommonParams.PATH; * When multi-core support was added to Solr way back in version 1.3, this class was required so that the core * functionality could be re-used multiple times. */ -public final class SolrCore implements SolrInfoBean, SolrMetricProducer, Closeable { +public final class SolrCore implements SolrInfoBean, Closeable { public static final String version = "1.0"; @@ -962,7 +961,7 @@ public final class SolrCore implements SolrInfoBean, SolrMetricProducer, Closeab checkVersionFieldExistsInSchema(schema, coreDescriptor); - // initialize searcher-related metrics + // initialize core metrics initializeMetrics(solrMetricsContext, null); SolrFieldCacheBean solrFieldCacheBean = new SolrFieldCacheBean(); @@ -1183,24 +1182,24 @@ public final class SolrCore implements SolrInfoBean, SolrMetricProducer, Closeab @Override public void initializeMetrics(SolrMetricsContext parentContext, String scope) { - newSearcherCounter = parentContext.counter(this, "new", Category.SEARCHER.toString()); - newSearcherTimer = parentContext.timer(this, "time", Category.SEARCHER.toString(), "new"); - newSearcherWarmupTimer = parentContext.timer(this, "warmup", Category.SEARCHER.toString(), "new"); - newSearcherMaxReachedCounter = parentContext.counter(this, "maxReached", Category.SEARCHER.toString(), "new"); - newSearcherOtherErrorsCounter = parentContext.counter(this, "errors", Category.SEARCHER.toString(), "new"); + newSearcherCounter = parentContext.counter("new", Category.SEARCHER.toString()); + newSearcherTimer = parentContext.timer("time", Category.SEARCHER.toString(), "new"); + newSearcherWarmupTimer = parentContext.timer("warmup", Category.SEARCHER.toString(), "new"); + newSearcherMaxReachedCounter = parentContext.counter("maxReached", Category.SEARCHER.toString(), "new"); + newSearcherOtherErrorsCounter = parentContext.counter("errors", Category.SEARCHER.toString(), "new"); - parentContext.gauge(this, () -> name == null ? "(null)" : name, true, "coreName", Category.CORE.toString()); - parentContext.gauge(this, () -> startTime, true, "startTime", Category.CORE.toString()); - parentContext.gauge(this, () -> getOpenCount(), true, "refCount", Category.CORE.toString()); - parentContext.gauge(this, () -> resourceLoader.getInstancePath().toString(), true, "instanceDir", Category.CORE.toString()); - parentContext.gauge(this, () -> isClosed() ? "(closed)" : getIndexDir(), true, "indexDir", Category.CORE.toString()); - parentContext.gauge(this, () -> isClosed() ? 0 : getIndexSize(), true, "sizeInBytes", Category.INDEX.toString()); - parentContext.gauge(this, () -> isClosed() ? "(closed)" : NumberUtils.readableSize(getIndexSize()), true, "size", Category.INDEX.toString()); + parentContext.gauge(() -> name == null ? "(null)" : name, true, "coreName", Category.CORE.toString()); + parentContext.gauge(() -> startTime, true, "startTime", Category.CORE.toString()); + parentContext.gauge(() -> getOpenCount(), true, "refCount", Category.CORE.toString()); + parentContext.gauge(() -> resourceLoader.getInstancePath().toString(), true, "instanceDir", Category.CORE.toString()); + parentContext.gauge(() -> isClosed() ? "(closed)" : getIndexDir(), true, "indexDir", Category.CORE.toString()); + parentContext.gauge(() -> isClosed() ? 0 : getIndexSize(), true, "sizeInBytes", Category.INDEX.toString()); + parentContext.gauge(() -> isClosed() ? "(closed)" : NumberUtils.readableSize(getIndexSize()), true, "size", Category.INDEX.toString()); if (coreContainer != null) { - parentContext.gauge(this, () -> coreContainer.getNamesForCore(this), true, "aliases", Category.CORE.toString()); + parentContext.gauge(() -> coreContainer.getNamesForCore(this), true, "aliases", Category.CORE.toString()); final CloudDescriptor cd = getCoreDescriptor().getCloudDescriptor(); if (cd != null) { - parentContext.gauge(this, () -> { + parentContext.gauge(() -> { if (cd.getCollectionName() != null) { return cd.getCollectionName(); } else { @@ -1208,7 +1207,7 @@ public final class SolrCore implements SolrInfoBean, SolrMetricProducer, Closeab } }, true, "collection", Category.CORE.toString()); - parentContext.gauge(this, () -> { + parentContext.gauge(() -> { if (cd.getShardId() != null) { return cd.getShardId(); } else { @@ -1220,10 +1219,10 @@ public final class SolrCore implements SolrInfoBean, SolrMetricProducer, Closeab // initialize disk total / free metrics Path dataDirPath = Paths.get(dataDir); File dataDirFile = dataDirPath.toFile(); - parentContext.gauge(this, () -> dataDirFile.getTotalSpace(), true, "totalSpace", Category.CORE.toString(), "fs"); - parentContext.gauge(this, () -> dataDirFile.getUsableSpace(), true, "usableSpace", Category.CORE.toString(), "fs"); - parentContext.gauge(this, () -> dataDirPath.toAbsolutePath().toString(), true, "path", Category.CORE.toString(), "fs"); - parentContext.gauge(this, () -> { + parentContext.gauge(() -> dataDirFile.getTotalSpace(), true, "totalSpace", Category.CORE.toString(), "fs"); + parentContext.gauge(() -> dataDirFile.getUsableSpace(), true, "usableSpace", Category.CORE.toString(), "fs"); + parentContext.gauge(() -> dataDirPath.toAbsolutePath().toString(), true, "path", Category.CORE.toString(), "fs"); + parentContext.gauge(() -> { try { return org.apache.lucene.util.IOUtils.spins(dataDirPath.toAbsolutePath()); } catch (IOException e) { @@ -2948,11 +2947,6 @@ public final class SolrCore implements SolrInfoBean, SolrMetricProducer, Closeab return Category.CORE; } - @Override - public MetricRegistry getMetricRegistry() { - return coreMetricManager.getRegistry(); - } - public Codec getCodec() { return codec; } diff --git a/solr/core/src/java/org/apache/solr/core/SolrInfoBean.java b/solr/core/src/java/org/apache/solr/core/SolrInfoBean.java index d7ddd8676c9..0486c9549e7 100644 --- a/solr/core/src/java/org/apache/solr/core/SolrInfoBean.java +++ b/solr/core/src/java/org/apache/solr/core/SolrInfoBean.java @@ -16,20 +16,14 @@ */ package org.apache.solr.core; -import java.util.Map; -import java.util.Set; - -import com.codahale.metrics.MetricRegistry; import org.apache.solr.metrics.SolrMetricProducer; -import org.apache.solr.metrics.SolrMetricsContext; -import org.apache.solr.util.stats.MetricUtils; /** * Interface for getting various ui friendly strings * for use by objects which are 'pluggable' to make server administration * easier. */ -public interface SolrInfoBean { +public interface SolrInfoBean extends SolrMetricProducer { /** * Category of Solr component. @@ -51,51 +45,4 @@ public interface SolrInfoBean { String getDescription(); /** Category of this component */ Category getCategory(); - - /** Optionally return a snapshot of metrics that this component reports, or null. - * Default implementation requires that both {@link #getMetricNames()} and - * {@link #getMetricRegistry()} return non-null values. - */ - default Map getMetricsSnapshot() { - if (getMetricRegistry() == null || getMetricNames() == null) { - return null; - } - return MetricUtils.convertMetrics(getMetricRegistry(), getMetricNames()); - } - - /** - * Modifiable set of metric names that this component reports (default is null, - * which means none). If not null then this set is used by {@link #registerMetricName(String)} - * to capture what metrics names are reported from this component. - *

NOTE: this set has to allow iteration under modifications.

- */ - default Set getMetricNames() { - return null; - } - - /** - * An instance of {@link MetricRegistry} that this component uses for metrics reporting - * (default is null, which means no registry). - */ - default MetricRegistry getMetricRegistry() { - if (this instanceof SolrMetricProducer) { - SolrMetricsContext context = ((SolrMetricProducer)this).getSolrMetricsContext(); - return context != null ? context.getMetricRegistry() : null; - } - return null; - } - - /** Register a metric name that this component reports. This method is called by various - * metric registration methods in {@link org.apache.solr.metrics.SolrMetricManager} in order - * to capture what metric names are reported from this component (which in turn is called - * from {@link org.apache.solr.metrics.SolrMetricProducer#initializeMetrics(SolrMetricsContext, String)}). - *

Default implementation registers all metrics added by a component. Implementations may - * override this to avoid reporting some or all metrics returned by {@link #getMetricsSnapshot()}

- */ - default void registerMetricName(String name) { - Set names = getMetricNames(); - if (names != null) { - names.add(name); - } - } } diff --git a/solr/core/src/java/org/apache/solr/handler/ReplicationHandler.java b/solr/core/src/java/org/apache/solr/handler/ReplicationHandler.java index 6f905c702bd..fae1351afdb 100644 --- a/solr/core/src/java/org/apache/solr/handler/ReplicationHandler.java +++ b/solr/core/src/java/org/apache/solr/handler/ReplicationHandler.java @@ -865,17 +865,17 @@ public class ReplicationHandler extends RequestHandlerBase implements SolrCoreAw @Override public void initializeMetrics(SolrMetricsContext parentContext, String scope) { super.initializeMetrics(parentContext, scope); - solrMetricsContext.gauge(this, () -> (core != null && !core.isClosed() ? NumberUtils.readableSize(core.getIndexSize()) : ""), + solrMetricsContext.gauge(() -> (core != null && !core.isClosed() ? NumberUtils.readableSize(core.getIndexSize()) : ""), true, "indexSize", getCategory().toString(), scope); - solrMetricsContext.gauge(this, () -> (core != null && !core.isClosed() ? getIndexVersion().toString() : ""), + solrMetricsContext.gauge(() -> (core != null && !core.isClosed() ? getIndexVersion().toString() : ""), true, "indexVersion", getCategory().toString(), scope); - solrMetricsContext.gauge(this, () -> (core != null && !core.isClosed() ? getIndexVersion().generation : 0), + solrMetricsContext.gauge(() -> (core != null && !core.isClosed() ? getIndexVersion().generation : 0), true, GENERATION, getCategory().toString(), scope); - solrMetricsContext.gauge(this, () -> (core != null && !core.isClosed() ? core.getIndexDir() : ""), + solrMetricsContext.gauge(() -> (core != null && !core.isClosed() ? core.getIndexDir() : ""), true, "indexPath", getCategory().toString(), scope); - solrMetricsContext.gauge(this, () -> isMaster, + solrMetricsContext.gauge(() -> isMaster, true, "isMaster", getCategory().toString(), scope); - solrMetricsContext.gauge(this, () -> isSlave, + solrMetricsContext.gauge(() -> isSlave, true, "isSlave", getCategory().toString(), scope); final MetricsMap fetcherMap = new MetricsMap((detailed, map) -> { IndexFetcher fetcher = currentIndexFetcher; @@ -905,12 +905,12 @@ public class ReplicationHandler extends RequestHandlerBase implements SolrCoreAw addVal(map, IndexFetcher.CONF_FILES_REPLICATED, props, String.class); } }); - solrMetricsContext.gauge(this , fetcherMap, true, "fetcher", getCategory().toString(), scope); - solrMetricsContext.gauge(this, () -> isMaster && includeConfFiles != null ? includeConfFiles : "", + solrMetricsContext.gauge(fetcherMap, true, "fetcher", getCategory().toString(), scope); + solrMetricsContext.gauge(() -> isMaster && includeConfFiles != null ? includeConfFiles : "", true, "confFilesToReplicate", getCategory().toString(), scope); - solrMetricsContext.gauge(this, () -> isMaster ? getReplicateAfterStrings() : Collections.emptyList(), + solrMetricsContext.gauge(() -> isMaster ? getReplicateAfterStrings() : Collections.emptyList(), true, REPLICATE_AFTER, getCategory().toString(), scope); - solrMetricsContext.gauge(this, () -> isMaster && replicationEnabled.get(), + solrMetricsContext.gauge( () -> isMaster && replicationEnabled.get(), true, "replicationEnabled", getCategory().toString(), scope); } diff --git a/solr/core/src/java/org/apache/solr/handler/RequestHandlerBase.java b/solr/core/src/java/org/apache/solr/handler/RequestHandlerBase.java index 4d9e96b8b74..71498218b79 100644 --- a/solr/core/src/java/org/apache/solr/handler/RequestHandlerBase.java +++ b/solr/core/src/java/org/apache/solr/handler/RequestHandlerBase.java @@ -38,7 +38,6 @@ import org.apache.solr.core.PluginBag; import org.apache.solr.core.PluginInfo; import org.apache.solr.core.SolrInfoBean; import org.apache.solr.metrics.MetricsMap; -import org.apache.solr.metrics.SolrMetricProducer; import org.apache.solr.metrics.SolrMetricsContext; import org.apache.solr.request.SolrQueryRequest; import org.apache.solr.request.SolrRequestHandler; @@ -53,7 +52,7 @@ import static org.apache.solr.core.RequestParams.USEPARAM; /** * */ -public abstract class RequestHandlerBase implements SolrRequestHandler, SolrInfoBean, SolrMetricProducer, NestedRequestHandler, ApiSupport { +public abstract class RequestHandlerBase implements SolrRequestHandler, SolrInfoBean, NestedRequestHandler, ApiSupport { protected NamedList initArgs = null; protected SolrParams defaults; @@ -148,17 +147,17 @@ public abstract class RequestHandlerBase implements SolrRequestHandler, SolrInfo @Override public void initializeMetrics(SolrMetricsContext parentContext, String scope) { this.solrMetricsContext = parentContext.getChildContext(this); - numErrors = solrMetricsContext.meter(this, "errors", getCategory().toString(), scope); - numServerErrors = solrMetricsContext.meter(this, "serverErrors", getCategory().toString(), scope); - numClientErrors = solrMetricsContext.meter(this, "clientErrors", getCategory().toString(), scope); - numTimeouts = solrMetricsContext.meter(this, "timeouts", getCategory().toString(), scope); - requests = solrMetricsContext.counter(this, "requests", getCategory().toString(), scope); + numErrors = solrMetricsContext.meter("errors", getCategory().toString(), scope); + numServerErrors = solrMetricsContext.meter("serverErrors", getCategory().toString(), scope); + numClientErrors = solrMetricsContext.meter("clientErrors", getCategory().toString(), scope); + numTimeouts = solrMetricsContext.meter("timeouts", getCategory().toString(), scope); + requests = solrMetricsContext.counter("requests", getCategory().toString(), scope); MetricsMap metricsMap = new MetricsMap((detail, map) -> shardPurposes.forEach((k, v) -> map.put(k, v.getCount()))); - solrMetricsContext.gauge(this, metricsMap, true, "shardRequests", getCategory().toString(), scope); - requestTimes = solrMetricsContext.timer(this,"requestTimes", getCategory().toString(), scope); - totalTime = solrMetricsContext.counter(this, "totalTime", getCategory().toString(), scope); - solrMetricsContext.gauge(this, () -> handlerStart, true, "handlerStart", getCategory().toString(), scope); + solrMetricsContext.gauge(metricsMap, true, "shardRequests", getCategory().toString(), scope); + requestTimes = solrMetricsContext.timer("requestTimes", getCategory().toString(), scope); + totalTime = solrMetricsContext.counter("totalTime", getCategory().toString(), scope); + solrMetricsContext.gauge(() -> handlerStart, true, "handlerStart", getCategory().toString(), scope); } public static SolrParams getSolrParamsFromNamedList(NamedList args, String key) { @@ -267,11 +266,6 @@ public abstract class RequestHandlerBase implements SolrRequestHandler, SolrInfo return Category.QUERY; } - @Override - public Set getMetricNames() { - return metricNames; - } - @Override public SolrRequestHandler getSubHandler(String subPath) { return null; diff --git a/solr/core/src/java/org/apache/solr/handler/admin/PluginInfoHandler.java b/solr/core/src/java/org/apache/solr/handler/admin/PluginInfoHandler.java index 8bdc478788a..7d9c3d7f97d 100644 --- a/solr/core/src/java/org/apache/solr/handler/admin/PluginInfoHandler.java +++ b/solr/core/src/java/org/apache/solr/handler/admin/PluginInfoHandler.java @@ -62,8 +62,8 @@ public class PluginInfoHandler extends RequestHandlerBase info.add( NAME, (m.getName() !=null ? m.getName() : na) ); info.add( "description", (m.getDescription()!=null ? m.getDescription() : na) ); - if (stats) { - info.add( "stats", m.getMetricsSnapshot()); + if (stats && m.getSolrMetricsContext() != null) { + info.add( "stats", m.getSolrMetricsContext().getMetricsSnapshot()); } } } diff --git a/solr/core/src/java/org/apache/solr/handler/admin/SolrInfoMBeanHandler.java b/solr/core/src/java/org/apache/solr/handler/admin/SolrInfoMBeanHandler.java index f9a3c053cdc..7654fa73800 100644 --- a/solr/core/src/java/org/apache/solr/handler/admin/SolrInfoMBeanHandler.java +++ b/solr/core/src/java/org/apache/solr/handler/admin/SolrInfoMBeanHandler.java @@ -144,8 +144,8 @@ public class SolrInfoMBeanHandler extends RequestHandlerBase { mBeanInfo.add("class", m.getName()); mBeanInfo.add("description", m.getDescription()); - if (req.getParams().getFieldBool(key, "stats", false)) - mBeanInfo.add("stats", m.getMetricsSnapshot()); + if (req.getParams().getFieldBool(key, "stats", false) && m.getSolrMetricsContext() != null) + mBeanInfo.add("stats", m.getSolrMetricsContext().getMetricsSnapshot()); catInfo.add(key, mBeanInfo); } diff --git a/solr/core/src/java/org/apache/solr/handler/component/SearchComponent.java b/solr/core/src/java/org/apache/solr/handler/component/SearchComponent.java index d92330679a9..5a524a9c14a 100644 --- a/solr/core/src/java/org/apache/solr/handler/component/SearchComponent.java +++ b/solr/core/src/java/org/apache/solr/handler/component/SearchComponent.java @@ -20,12 +20,10 @@ import java.io.IOException; import java.util.Collections; import java.util.HashMap; import java.util.Map; -import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; -import com.codahale.metrics.MetricRegistry; import org.apache.solr.common.util.NamedList; import org.apache.solr.core.SolrInfoBean; +import org.apache.solr.metrics.SolrMetricsContext; import org.apache.solr.search.facet.FacetModule; import org.apache.solr.util.plugin.NamedListInitializedPlugin; @@ -42,8 +40,7 @@ public abstract class SearchComponent implements SolrInfoBean, NamedListInitiali */ private String name = this.getClass().getName(); - protected Set metricNames = ConcurrentHashMap.newKeySet(); - protected MetricRegistry registry; + protected SolrMetricsContext solrMetricsContext; /** * Prepare the response. Guaranteed to be called before any SearchComponent {@link #process(org.apache.solr.handler.component.ResponseBuilder)} method. @@ -100,7 +97,7 @@ public abstract class SearchComponent implements SolrInfoBean, NamedListInitiali // By default do nothing } - //////////////////////// SolrInfoMBeans methods ////////////////////// + //////////////////////// SolrInfoBean methods ////////////////////// @Override public String getName() { @@ -116,13 +113,14 @@ public abstract class SearchComponent implements SolrInfoBean, NamedListInitiali } @Override - public Set getMetricNames() { - return metricNames; + public SolrMetricsContext getSolrMetricsContext() { + return solrMetricsContext; } @Override - public MetricRegistry getMetricRegistry() { - return registry; + public void initializeMetrics(SolrMetricsContext parentContext, String scope) { + // By default don't register any metrics - but prepare a child context + this.solrMetricsContext = parentContext.getChildContext(this); } public static final Map> standard_components; diff --git a/solr/core/src/java/org/apache/solr/handler/component/SuggestComponent.java b/solr/core/src/java/org/apache/solr/handler/component/SuggestComponent.java index d70cf9928e4..fe24f5e6acc 100644 --- a/solr/core/src/java/org/apache/solr/handler/component/SuggestComponent.java +++ b/solr/core/src/java/org/apache/solr/handler/component/SuggestComponent.java @@ -358,14 +358,14 @@ public class SuggestComponent extends SearchComponent implements SolrCoreAware, public void initializeMetrics(SolrMetricsContext parentContext, String scope) { this.metricsContext = parentContext.getChildContext(this); - this.metricsContext.gauge(this, () -> ramBytesUsed(), true, "totalSizeInBytes", getCategory().toString()); + this.metricsContext.gauge(() -> ramBytesUsed(), true, "totalSizeInBytes", getCategory().toString()); MetricsMap suggestersMap = new MetricsMap((detailed, map) -> { for (Map.Entry entry : suggesters.entrySet()) { SolrSuggester suggester = entry.getValue(); map.put(entry.getKey(), suggester.toString()); } }); - this.metricsContext.gauge(this, suggestersMap, true, "suggesters", getCategory().toString(), scope); + this.metricsContext.gauge(suggestersMap, true, "suggesters", getCategory().toString(), scope); } @Override diff --git a/solr/core/src/java/org/apache/solr/handler/component/TermsComponent.java b/solr/core/src/java/org/apache/solr/handler/component/TermsComponent.java index 8801b486147..71d1390ecea 100644 --- a/solr/core/src/java/org/apache/solr/handler/component/TermsComponent.java +++ b/solr/core/src/java/org/apache/solr/handler/component/TermsComponent.java @@ -96,7 +96,7 @@ public class TermsComponent extends SearchComponent { (String) args.get(HttpShardHandlerFactory.INIT_SHARDS_WHITELIST), !HttpShardHandlerFactory.doGetDisableShardsWhitelist()); } - + @Override public void prepare(ResponseBuilder rb) throws IOException { SolrParams params = rb.req.getParams(); diff --git a/solr/core/src/java/org/apache/solr/highlight/HighlightingPluginBase.java b/solr/core/src/java/org/apache/solr/highlight/HighlightingPluginBase.java index 8186b12ce5f..5c41b13f8d3 100644 --- a/solr/core/src/java/org/apache/solr/highlight/HighlightingPluginBase.java +++ b/solr/core/src/java/org/apache/solr/highlight/HighlightingPluginBase.java @@ -23,14 +23,13 @@ import com.codahale.metrics.Counter; import org.apache.solr.common.params.SolrParams; import org.apache.solr.common.util.NamedList; import org.apache.solr.core.SolrInfoBean; -import org.apache.solr.metrics.SolrMetricProducer; import org.apache.solr.metrics.SolrMetricsContext; /** * * @since solr 1.3 */ -public abstract class HighlightingPluginBase implements SolrInfoBean, SolrMetricProducer +public abstract class HighlightingPluginBase implements SolrInfoBean { protected Counter numRequests; protected SolrParams defaults; @@ -62,11 +61,6 @@ public abstract class HighlightingPluginBase implements SolrInfoBean, SolrMetric return Category.HIGHLIGHTER; } - @Override - public Set getMetricNames() { - return metricNames; - } - @Override public SolrMetricsContext getSolrMetricsContext() { return solrMetricsContext; @@ -75,7 +69,7 @@ public abstract class HighlightingPluginBase implements SolrInfoBean, SolrMetric @Override public void initializeMetrics(SolrMetricsContext parentContext, String scope) { solrMetricsContext = parentContext.getChildContext(this); - numRequests = solrMetricsContext.counter(this, "requests", getCategory().toString(), scope); + numRequests = solrMetricsContext.counter("requests", getCategory().toString(), scope); } } diff --git a/solr/core/src/java/org/apache/solr/metrics/SolrCoreMetricManager.java b/solr/core/src/java/org/apache/solr/metrics/SolrCoreMetricManager.java index be52b5f5ed3..572c01cf06b 100644 --- a/solr/core/src/java/org/apache/solr/metrics/SolrCoreMetricManager.java +++ b/solr/core/src/java/org/apache/solr/metrics/SolrCoreMetricManager.java @@ -18,7 +18,6 @@ package org.apache.solr.metrics; import java.io.Closeable; import java.io.IOException; -import java.lang.invoke.MethodHandles; import com.codahale.metrics.MetricRegistry; import org.apache.solr.cloud.CloudDescriptor; @@ -28,8 +27,6 @@ import org.apache.solr.core.NodeConfig; import org.apache.solr.core.PluginInfo; import org.apache.solr.core.SolrCore; import org.apache.solr.core.SolrInfoBean; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; /** * Helper class for managing registration of {@link SolrMetricProducer}'s @@ -37,8 +34,6 @@ import org.slf4j.LoggerFactory; */ public class SolrCoreMetricManager implements Closeable { - private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); - private final SolrCore core; private SolrMetricsContext solrMetricsContext; private SolrMetricManager metricManager; @@ -85,8 +80,8 @@ public class SolrCoreMetricManager implements Closeable { CoreContainer coreContainer = core.getCoreContainer(); NodeConfig nodeConfig = coreContainer.getConfig(); PluginInfo[] pluginInfos = nodeConfig.getMetricsConfig().getMetricReporters(); - metricManager.loadReporters(pluginInfos, core.getResourceLoader(), coreContainer, core, solrMetricsContext.tag, - SolrInfoBean.Group.core, solrMetricsContext.registry); + metricManager.loadReporters(pluginInfos, core.getResourceLoader(), coreContainer, core, solrMetricsContext.getTag(), + SolrInfoBean.Group.core, solrMetricsContext.getRegistryName()); if (cloudMode) { metricManager.loadShardReporters(pluginInfos, core); } @@ -98,7 +93,7 @@ public class SolrCoreMetricManager implements Closeable { * This method also reloads reporters so that they use the new core name. */ public void afterCoreSetName() { - String oldRegistryName = solrMetricsContext.registry; + String oldRegistryName = solrMetricsContext.getRegistryName(); String oldLeaderRegistryName = leaderRegistryName; initCloudMode(); String newRegistryName = createRegistryName(cloudMode, collectionName, shardName, replicaName, core.getName()); @@ -107,11 +102,11 @@ public class SolrCoreMetricManager implements Closeable { return; } // close old reporters - metricManager.closeReporters(oldRegistryName, solrMetricsContext.tag); + metricManager.closeReporters(oldRegistryName, solrMetricsContext.getTag()); if (oldLeaderRegistryName != null) { - metricManager.closeReporters(oldLeaderRegistryName, solrMetricsContext.tag); + metricManager.closeReporters(oldLeaderRegistryName, solrMetricsContext.getTag()); } - solrMetricsContext = new SolrMetricsContext(metricManager, newRegistryName, solrMetricsContext.tag); + solrMetricsContext = new SolrMetricsContext(metricManager, newRegistryName, solrMetricsContext.getTag()); // load reporters again, using the new core name loadReporters(); } @@ -147,11 +142,11 @@ public class SolrCoreMetricManager implements Closeable { */ @Override public void close() throws IOException { - metricManager.closeReporters(solrMetricsContext.registry, solrMetricsContext.tag); + metricManager.closeReporters(solrMetricsContext.getRegistryName(), solrMetricsContext.getTag()); if (getLeaderRegistryName() != null) { - metricManager.closeReporters(getLeaderRegistryName(), solrMetricsContext.tag); + metricManager.closeReporters(getLeaderRegistryName(), solrMetricsContext.getTag()); } - metricManager.unregisterGauges(solrMetricsContext.registry, solrMetricsContext.tag); + metricManager.unregisterGauges(solrMetricsContext.getRegistryName(), solrMetricsContext.getTag()); } public SolrMetricsContext getSolrMetricsContext() { @@ -180,7 +175,7 @@ public class SolrCoreMetricManager implements Closeable { * @return the metric registry name of the manager. */ public String getRegistryName() { - return solrMetricsContext != null ? solrMetricsContext.registry : null; + return solrMetricsContext != null ? solrMetricsContext.getRegistryName() : null; } /** @@ -195,7 +190,7 @@ public class SolrCoreMetricManager implements Closeable { * Return a tag specific to this instance. */ public String getTag() { - return solrMetricsContext.tag; + return solrMetricsContext.getTag(); } public static String createRegistryName(boolean cloud, String collectionName, String shardName, String replicaName, String coreName) { diff --git a/solr/core/src/java/org/apache/solr/metrics/SolrMetricManager.java b/solr/core/src/java/org/apache/solr/metrics/SolrMetricManager.java index 977b0ca66be..7ab1d0969e2 100644 --- a/solr/core/src/java/org/apache/solr/metrics/SolrMetricManager.java +++ b/solr/core/src/java/org/apache/solr/metrics/SolrMetricManager.java @@ -71,7 +71,7 @@ import org.slf4j.MDC; * {@link MetricRegistry} instances are automatically created when first referenced by name. Similarly, * instances of {@link Metric} implementations, such as {@link Meter}, {@link Counter}, {@link Timer} and * {@link Histogram} are automatically created and registered under hierarchical names, in a specified - * registry, when {@link #meter(SolrInfoBean, String, String, String...)} and other similar methods are called. + * registry, when {@link #meter(SolrMetricsContext, String, String, String...)} and other similar methods are called. *

This class enforces a common prefix ({@link #REGISTRY_NAME_PREFIX}) in all registry * names.

*

Solr uses several different registries for collecting metrics belonging to different groups, using @@ -605,10 +605,10 @@ public class SolrMetricManager { * @param metricPath (optional) additional top-most metric name path elements * @return existing or a newly created {@link Meter} */ - public Meter meter(SolrInfoBean info, String registry, String metricName, String... metricPath) { + public Meter meter(SolrMetricsContext context, String registry, String metricName, String... metricPath) { final String name = mkName(metricName, metricPath); - if (info != null) { - info.registerMetricName(name); + if (context != null) { + context.registerMetricName(name); } return registry(registry).meter(name, meterSupplier); } @@ -622,10 +622,10 @@ public class SolrMetricManager { * @param metricPath (optional) additional top-most metric name path elements * @return existing or a newly created {@link Timer} */ - public Timer timer(SolrInfoBean info, String registry, String metricName, String... metricPath) { + public Timer timer(SolrMetricsContext context, String registry, String metricName, String... metricPath) { final String name = mkName(metricName, metricPath); - if (info != null) { - info.registerMetricName(name); + if (context != null) { + context.registerMetricName(name); } return registry(registry).timer(name, timerSupplier); } @@ -639,10 +639,10 @@ public class SolrMetricManager { * @param metricPath (optional) additional top-most metric name path elements * @return existing or a newly created {@link Counter} */ - public Counter counter(SolrInfoBean info, String registry, String metricName, String... metricPath) { + public Counter counter(SolrMetricsContext context, String registry, String metricName, String... metricPath) { final String name = mkName(metricName, metricPath); - if (info != null) { - info.registerMetricName(name); + if (context != null) { + context.registerMetricName(name); } return registry(registry).counter(name, counterSupplier); } @@ -656,10 +656,10 @@ public class SolrMetricManager { * @param metricPath (optional) additional top-most metric name path elements * @return existing or a newly created {@link Histogram} */ - public Histogram histogram(SolrInfoBean info, String registry, String metricName, String... metricPath) { + public Histogram histogram(SolrMetricsContext context, String registry, String metricName, String... metricPath) { final String name = mkName(metricName, metricPath); - if (info != null) { - info.registerMetricName(name); + if (context != null) { + context.registerMetricName(name); } return registry(registry).histogram(name, histogramSupplier); } @@ -676,11 +676,11 @@ public class SolrMetricManager { * using dotted notation * @param metricPath (optional) additional top-most metric name path elements */ - public void registerMetric(SolrInfoBean info, String registry, Metric metric, boolean force, String metricName, String... metricPath) { + public void registerMetric(SolrMetricsContext context, String registry, Metric metric, boolean force, String metricName, String... metricPath) { MetricRegistry metricRegistry = registry(registry); String fullName = mkName(metricName, metricPath); - if (info != null) { - info.registerMetricName(fullName); + if (context != null) { + context.registerMetricName(fullName); } synchronized (metricRegistry) { if (force && metricRegistry.getMetrics().containsKey(fullName)) { @@ -720,8 +720,8 @@ public class SolrMetricManager { } } - public void registerGauge(SolrInfoBean info, String registry, Gauge gauge, String tag, boolean force, String metricName, String... metricPath) { - registerMetric(info, registry, new GaugeWrapper(gauge, tag), force, metricName, metricPath); + public void registerGauge(SolrMetricsContext context, String registry, Gauge gauge, String tag, boolean force, String metricName, String... metricPath) { + registerMetric(context, registry, new GaugeWrapper(gauge, tag), force, metricName, metricPath); } public int unregisterGauges(String registryName, String tagSegment) { diff --git a/solr/core/src/java/org/apache/solr/metrics/SolrMetricProducer.java b/solr/core/src/java/org/apache/solr/metrics/SolrMetricProducer.java index 6479b99ea9f..c321a11012a 100644 --- a/solr/core/src/java/org/apache/solr/metrics/SolrMetricProducer.java +++ b/solr/core/src/java/org/apache/solr/metrics/SolrMetricProducer.java @@ -51,14 +51,12 @@ public interface SolrMetricProducer extends AutoCloseable { void initializeMetrics(SolrMetricsContext parentContext, String scope); /** - * Implementations should override this method to return the context used in + * Implementations should return the context used in * {@link #initializeMetrics(SolrMetricsContext, String)} to ensure proper cleanup of metrics - * at the end of the life-cycle of this component. This should be the child context if one was created. - *

The default implementation returns null, which disables unregistration of metrics on {@link #close()}.

+ * at the end of the life-cycle of this component. This should be the child context if one was created, + * or null if the parent context was used. */ - default SolrMetricsContext getSolrMetricsContext() { - return null; - } + SolrMetricsContext getSolrMetricsContext(); /** * Implementations should always call SolrMetricProducer.super.close() to ensure that diff --git a/solr/core/src/java/org/apache/solr/metrics/SolrMetricsContext.java b/solr/core/src/java/org/apache/solr/metrics/SolrMetricsContext.java index dd37e1fabe8..6861457a983 100644 --- a/solr/core/src/java/org/apache/solr/metrics/SolrMetricsContext.java +++ b/solr/core/src/java/org/apache/solr/metrics/SolrMetricsContext.java @@ -17,25 +17,32 @@ package org.apache.solr.metrics; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + import com.codahale.metrics.Counter; import com.codahale.metrics.Gauge; import com.codahale.metrics.Histogram; import com.codahale.metrics.Meter; import com.codahale.metrics.MetricRegistry; import com.codahale.metrics.Timer; -import org.apache.solr.core.SolrInfoBean; +import org.apache.solr.util.stats.MetricUtils; /** * This class represents a metrics context that ties together components with the same life-cycle * and provides convenient access to the metric registry. + *

Additionally it's used for registering and reporting metrics specific to the components that + * use the same instance of context.

*/ public class SolrMetricsContext { - public final String registry; - public final SolrMetricManager metricManager; - public final String tag; + private final String registryName; + private final SolrMetricManager metricManager; + private final String tag; + private final Set metricNames = ConcurrentHashMap.newKeySet(); - public SolrMetricsContext(SolrMetricManager metricManager, String registry, String tag) { - this.registry = registry; + public SolrMetricsContext(SolrMetricManager metricManager, String registryName, String tag) { + this.registryName = registryName; this.metricManager = metricManager; this.tag = tag; } @@ -47,6 +54,27 @@ public class SolrMetricsContext { return tag; } + /** + * Return metric registry name used in this context. + */ + public String getRegistryName() { + return registryName; + } + + /** + * Return the instance of {@link SolrMetricManager} used in this context. + */ + public SolrMetricManager getMetricManager() { + return metricManager; + } + + /** + * Return a modifiable set of metric names that this component registers. + */ + public Set getMetricNames() { + return metricNames; + } + /** * Unregister all {@link Gauge} metrics that use this context's tag. * @@ -55,7 +83,7 @@ public class SolrMetricsContext { * do so may result in hard-to-debug memory leaks.

*/ public void unregister() { - metricManager.unregisterGauges(registry, tag); + metricManager.unregisterGauges(registryName, tag); } /** @@ -65,50 +93,66 @@ public class SolrMetricsContext { * @param child child object that produces metrics with a different life-cycle than the parent. */ public SolrMetricsContext getChildContext(Object child) { - SolrMetricsContext childContext = new SolrMetricsContext(metricManager, registry, SolrMetricProducer.getUniqueMetricTag(child, tag)); + SolrMetricsContext childContext = new SolrMetricsContext(metricManager, registryName, SolrMetricProducer.getUniqueMetricTag(child, tag)); return childContext; } - /** - * Convenience method for {@link SolrMetricManager#meter(SolrInfoBean, String, String, String...)}. + /** Register a metric name that this component reports. This method is called by various + * metric registration methods in {@link org.apache.solr.metrics.SolrMetricManager} in order + * to capture what metric names are reported from this component (which in turn is called + * from {@link org.apache.solr.metrics.SolrMetricProducer#initializeMetrics(SolrMetricsContext, String)}). */ - public Meter meter(SolrInfoBean info, String metricName, String... metricPath) { - return metricManager.meter(info, registry, metricName, metricPath); + public void registerMetricName(String name) { + metricNames.add(name); } /** - * Convenience method for {@link SolrMetricManager#counter(SolrInfoBean, String, String, String...)}. + * Return a snapshot of metric values that this component reports. */ - public Counter counter(SolrInfoBean info, String metricName, String... metricPath) { - return metricManager.counter(info, registry, metricName, metricPath); + public Map getMetricsSnapshot() { + return MetricUtils.convertMetrics(getMetricRegistry(), metricNames); + } + + /** + * Convenience method for {@link SolrMetricManager#meter(SolrMetricsContext, String, String, String...)}. + */ + public Meter meter(String metricName, String... metricPath) { + return metricManager.meter(this, registryName, metricName, metricPath); + } + + /** + * Convenience method for {@link SolrMetricManager#counter(SolrMetricsContext, String, String, String...)}. + */ + public Counter counter(String metricName, String... metricPath) { + return metricManager.counter(this, registryName, metricName, metricPath); } /** - * Convenience method for {@link SolrMetricManager#registerGauge(SolrInfoBean, String, Gauge, String, boolean, String, String...)}. + * Convenience method for {@link SolrMetricManager#registerGauge(SolrMetricsContext, String, Gauge, String, boolean, String, String...)}. */ - public void gauge(SolrInfoBean info, Gauge gauge, boolean force, String metricName, String... metricPath) { - metricManager.registerGauge(info, registry, gauge, tag, force, metricName, metricPath); + public void gauge(Gauge gauge, boolean force, String metricName, String... metricPath) { + metricManager.registerGauge(this, registryName, gauge, tag, force, metricName, metricPath); } /** - * Convenience method for {@link SolrMetricManager#meter(SolrInfoBean, String, String, String...)}. + * Convenience method for {@link SolrMetricManager#meter(SolrMetricsContext, String, String, String...)}. */ - public Timer timer(SolrInfoBean info, String metricName, String... metricPath) { - return metricManager.timer(info, registry, metricName, metricPath); + public Timer timer(String metricName, String... metricPath) { + return metricManager.timer(this, registryName, metricName, metricPath); } /** - * Convenience method for {@link SolrMetricManager#histogram(SolrInfoBean, String, String, String...)}. + * Convenience method for {@link SolrMetricManager#histogram(SolrMetricsContext, String, String, String...)}. */ - public Histogram histogram(SolrInfoBean info, String metricName, String... metricPath) { - return metricManager.histogram(info, registry, metricName, metricPath); + public Histogram histogram(String metricName, String... metricPath) { + return metricManager.histogram(this, registryName, metricName, metricPath); } /** - * Get the MetricRegistry instance that is used for registering metrics in this context. + * Get the {@link MetricRegistry} instance that is used for registering metrics in this context. */ public MetricRegistry getMetricRegistry() { - return metricManager.registry(registry); + return metricManager.registry(registryName); } } diff --git a/solr/core/src/java/org/apache/solr/search/CaffeineCache.java b/solr/core/src/java/org/apache/solr/search/CaffeineCache.java index f3c4c66f422..77503423eeb 100644 --- a/solr/core/src/java/org/apache/solr/search/CaffeineCache.java +++ b/solr/core/src/java/org/apache/solr/search/CaffeineCache.java @@ -331,11 +331,6 @@ public class CaffeineCache extends SolrCacheBase implements SolrCache getMetricNames() { - return metricNames; - } - @Override public void initializeMetrics(SolrMetricsContext parentContext, String scope) { solrMetricsContext = parentContext.getChildContext(this); @@ -362,6 +357,6 @@ public class CaffeineCache extends SolrCacheBase implements SolrCache extends SolrCacheBase implements SolrCache return description; } - @Override - public Set getMetricNames() { - return metricNames; - } - - @Override public SolrMetricsContext getSolrMetricsContext() { return solrMetricsContext; @@ -317,7 +311,7 @@ public class FastLRUCache extends SolrCacheBase implements SolrCache @Override public void initializeMetrics(SolrMetricsContext parentContext, String scope) { this.solrMetricsContext = parentContext.getChildContext(this); - this.solrMetricsContext.gauge(this, cacheMap, true, scope, getCategory().toString()); + this.solrMetricsContext.gauge(cacheMap, true, scope, getCategory().toString()); } // for unit tests only diff --git a/solr/core/src/java/org/apache/solr/search/LFUCache.java b/solr/core/src/java/org/apache/solr/search/LFUCache.java index 125f08a3bf8..6eee4bf87a5 100644 --- a/solr/core/src/java/org/apache/solr/search/LFUCache.java +++ b/solr/core/src/java/org/apache/solr/search/LFUCache.java @@ -336,7 +336,7 @@ public class LFUCache implements SolrCache, Accountable { } }); - solrMetricsContext.gauge(this, cacheMap, true, scope, getCategory().toString()); + solrMetricsContext.gauge(cacheMap, true, scope, getCategory().toString()); } // for unit tests only @@ -344,11 +344,6 @@ public class LFUCache implements SolrCache, Accountable { return cacheMap; } - @Override - public Set getMetricNames() { - return metricNames; - } - @Override public String toString() { return name + (cacheMap != null ? cacheMap.getValue().toString() : ""); diff --git a/solr/core/src/java/org/apache/solr/search/LRUCache.java b/solr/core/src/java/org/apache/solr/search/LRUCache.java index 7a1b37cfb96..b009f4c7324 100644 --- a/solr/core/src/java/org/apache/solr/search/LRUCache.java +++ b/solr/core/src/java/org/apache/solr/search/LRUCache.java @@ -390,11 +390,6 @@ public class LRUCache extends SolrCacheBase implements SolrCache, Acco return description; } - @Override - public Set getMetricNames() { - return metricNames; - } - @Override public SolrMetricsContext getSolrMetricsContext() { return solrMetricsContext; @@ -431,7 +426,7 @@ public class LRUCache extends SolrCacheBase implements SolrCache, Acco res.put("cumulative_evictionsRamUsage", stats.evictionsRamUsage.longValue()); res.put("cumulative_evictionsIdleTime", stats.evictionsIdleTime.longValue()); }); - solrMetricsContext.gauge(this, cacheMap, true, scope, getCategory().toString()); + solrMetricsContext.gauge(cacheMap, true, scope, getCategory().toString()); } // for unit tests only diff --git a/solr/core/src/java/org/apache/solr/search/QParserPlugin.java b/solr/core/src/java/org/apache/solr/search/QParserPlugin.java index d602c7aab9f..91a18bc5fe5 100644 --- a/solr/core/src/java/org/apache/solr/search/QParserPlugin.java +++ b/solr/core/src/java/org/apache/solr/search/QParserPlugin.java @@ -19,11 +19,11 @@ package org.apache.solr.search; import java.util.Collections; import java.util.HashMap; import java.util.Map; -import java.util.Set; import org.apache.solr.common.params.SolrParams; import org.apache.solr.common.util.NamedList; import org.apache.solr.core.SolrInfoBean; +import org.apache.solr.metrics.SolrMetricsContext; import org.apache.solr.request.SolrQueryRequest; import org.apache.solr.search.join.BlockJoinChildQParserPlugin; import org.apache.solr.search.join.BlockJoinParentQParserPlugin; @@ -114,10 +114,15 @@ public abstract class QParserPlugin implements NamedListInitializedPlugin, SolrI } @Override - public Set getMetricNames() { - return null; + public void initializeMetrics(SolrMetricsContext parentContext, String scope) { + // by default do nothing } + // by default no metrics + @Override + public SolrMetricsContext getSolrMetricsContext() { + return null; + } } diff --git a/solr/core/src/java/org/apache/solr/search/SolrCache.java b/solr/core/src/java/org/apache/solr/search/SolrCache.java index 55f57ec72f4..acaa5e0dd16 100644 --- a/solr/core/src/java/org/apache/solr/search/SolrCache.java +++ b/solr/core/src/java/org/apache/solr/search/SolrCache.java @@ -17,7 +17,6 @@ package org.apache.solr.search; import org.apache.solr.core.SolrInfoBean; -import org.apache.solr.metrics.SolrMetricProducer; import java.util.Map; @@ -25,7 +24,7 @@ import java.util.Map; /** * Primary API for dealing with Solr's internal caches. */ -public interface SolrCache extends SolrInfoBean, SolrMetricProducer { +public interface SolrCache extends SolrInfoBean { String HIT_RATIO_PARAM = "hitratio"; String HITS_PARAM = "hits"; @@ -138,7 +137,7 @@ public interface SolrCache extends SolrInfoBean, SolrMetricProducer { /** Frees any non-memory resources */ default void close() throws Exception { - SolrMetricProducer.super.close(); + SolrInfoBean.super.close(); } /** Returns maximum size limit (number of items) if set and supported, -1 otherwise. */ diff --git a/solr/core/src/java/org/apache/solr/search/SolrCacheHolder.java b/solr/core/src/java/org/apache/solr/search/SolrCacheHolder.java index 7afe96dc732..52dacd79b7c 100644 --- a/solr/core/src/java/org/apache/solr/search/SolrCacheHolder.java +++ b/solr/core/src/java/org/apache/solr/search/SolrCacheHolder.java @@ -19,9 +19,7 @@ package org.apache.solr.search; import java.lang.invoke.MethodHandles; import java.util.Map; -import java.util.Set; -import com.codahale.metrics.MetricRegistry; import org.apache.solr.metrics.SolrMetricsContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -120,16 +118,6 @@ public class SolrCacheHolder implements SolrCache { return delegate.getName(); } - @Override - public MetricRegistry getMetricRegistry() { - return delegate.getMetricRegistry(); - } - - @Override - public Set getMetricNames() { - return delegate.getMetricNames(); - } - @Override public String getDescription() { return delegate.getDescription(); @@ -145,4 +133,9 @@ public class SolrCacheHolder implements SolrCache { delegate.initializeMetrics(parentContext, scope); } + @Override + public SolrMetricsContext getSolrMetricsContext() { + return delegate.getSolrMetricsContext(); + } + } diff --git a/solr/core/src/java/org/apache/solr/search/SolrFieldCacheBean.java b/solr/core/src/java/org/apache/solr/search/SolrFieldCacheBean.java index b6deb7c506b..50056272ee8 100644 --- a/solr/core/src/java/org/apache/solr/search/SolrFieldCacheBean.java +++ b/solr/core/src/java/org/apache/solr/search/SolrFieldCacheBean.java @@ -16,12 +16,8 @@ */ package org.apache.solr.search; -import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; - import org.apache.solr.core.SolrInfoBean; import org.apache.solr.metrics.MetricsMap; -import org.apache.solr.metrics.SolrMetricProducer; import org.apache.solr.metrics.SolrMetricsContext; import org.apache.solr.uninverting.UninvertingReader; @@ -29,13 +25,12 @@ import org.apache.solr.uninverting.UninvertingReader; * A SolrInfoBean that provides introspection of the Solr FieldCache * */ -public class SolrFieldCacheBean implements SolrInfoBean, SolrMetricProducer { +public class SolrFieldCacheBean implements SolrInfoBean { private boolean disableEntryList = Boolean.getBoolean("disableSolrFieldCacheMBeanEntryList"); private boolean disableJmxEntryList = Boolean.getBoolean("disableSolrFieldCacheMBeanEntryListJmx"); private SolrMetricsContext solrMetricsContext; - private Set metricNames = ConcurrentHashMap.newKeySet(); @Override public String getName() { return this.getClass().getName(); } @@ -45,10 +40,6 @@ public class SolrFieldCacheBean implements SolrInfoBean, SolrMetricProducer { } @Override public Category getCategory() { return Category.CACHE; } - @Override - public Set getMetricNames() { - return metricNames; - } @Override public SolrMetricsContext getSolrMetricsContext() { @@ -72,6 +63,6 @@ public class SolrFieldCacheBean implements SolrInfoBean, SolrMetricProducer { map.put("entries_count", UninvertingReader.getUninvertedStatsSize()); } }); - solrMetricsContext.gauge(this, metricsMap, true, "fieldCache", Category.CACHE.toString(), scope); + solrMetricsContext.gauge(metricsMap, true, "fieldCache", Category.CACHE.toString(), scope); } } diff --git a/solr/core/src/java/org/apache/solr/search/SolrIndexSearcher.java b/solr/core/src/java/org/apache/solr/search/SolrIndexSearcher.java index 4ad59045650..3582af8e04f 100644 --- a/solr/core/src/java/org/apache/solr/search/SolrIndexSearcher.java +++ b/solr/core/src/java/org/apache/solr/search/SolrIndexSearcher.java @@ -67,7 +67,6 @@ import org.apache.solr.core.SolrInfoBean; import org.apache.solr.index.SlowCompositeReaderWrapper; import org.apache.solr.metrics.MetricsMap; import org.apache.solr.metrics.SolrMetricManager; -import org.apache.solr.metrics.SolrMetricProducer; import org.apache.solr.metrics.SolrMetricsContext; import org.apache.solr.request.LocalSolrQueryRequest; import org.apache.solr.request.SolrQueryRequest; @@ -89,7 +88,7 @@ import org.slf4j.LoggerFactory; * * @since solr 0.9 */ -public class SolrIndexSearcher extends IndexSearcher implements Closeable, SolrInfoBean, SolrMetricProducer { +public class SolrIndexSearcher extends IndexSearcher implements Closeable, SolrInfoBean { private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); @@ -489,7 +488,7 @@ public class SolrIndexSearcher extends IndexSearcher implements Closeable, SolrI } try { - SolrMetricProducer.super.close(); + SolrInfoBean.super.close(); } catch (Exception e) { log.warn("Exception closing", e); } @@ -2277,11 +2276,6 @@ public class SolrIndexSearcher extends IndexSearcher implements Closeable, SolrI return Category.CORE; } - @Override - public Set getMetricNames() { - return metricNames; - } - @Override public SolrMetricsContext getSolrMetricsContext() { return solrMetricsContext; @@ -2289,20 +2283,20 @@ public class SolrIndexSearcher extends IndexSearcher implements Closeable, SolrI @Override public void initializeMetrics(SolrMetricsContext parentContext, String scope) { - parentContext.gauge(this, () -> name, true, "searcherName", Category.SEARCHER.toString(), scope); - parentContext.gauge(this, () -> cachingEnabled, true, "caching", Category.SEARCHER.toString(), scope); - parentContext.gauge(this, () -> openTime, true, "openedAt", Category.SEARCHER.toString(), scope); - parentContext.gauge(this, () -> warmupTime, true, "warmupTime", Category.SEARCHER.toString(), scope); - parentContext.gauge(this, () -> registerTime, true, "registeredAt", Category.SEARCHER.toString(), scope); + parentContext.gauge(() -> name, true, "searcherName", Category.SEARCHER.toString(), scope); + parentContext.gauge(() -> cachingEnabled, true, "caching", Category.SEARCHER.toString(), scope); + parentContext.gauge(() -> openTime, true, "openedAt", Category.SEARCHER.toString(), scope); + parentContext.gauge(() -> warmupTime, true, "warmupTime", Category.SEARCHER.toString(), scope); + parentContext.gauge(() -> registerTime, true, "registeredAt", Category.SEARCHER.toString(), scope); // reader stats - parentContext.gauge(this, () -> reader.numDocs(), true, "numDocs", Category.SEARCHER.toString(), scope); - parentContext.gauge(this, () -> reader.maxDoc(), true, "maxDoc", Category.SEARCHER.toString(), scope); - parentContext.gauge(this, () -> reader.maxDoc() - reader.numDocs(), true, "deletedDocs", Category.SEARCHER.toString(), scope); - parentContext.gauge(this, () -> reader.toString(), true, "reader", Category.SEARCHER.toString(), scope); - parentContext.gauge(this, () -> reader.directory().toString(), true, "readerDir", Category.SEARCHER.toString(), scope); - parentContext.gauge(this, () -> reader.getVersion(), true, "indexVersion", Category.SEARCHER.toString(), scope); + parentContext.gauge(() -> reader.numDocs(), true, "numDocs", Category.SEARCHER.toString(), scope); + parentContext.gauge(() -> reader.maxDoc(), true, "maxDoc", Category.SEARCHER.toString(), scope); + parentContext.gauge(() -> reader.maxDoc() - reader.numDocs(), true, "deletedDocs", Category.SEARCHER.toString(), scope); + parentContext.gauge(() -> reader.toString(), true, "reader", Category.SEARCHER.toString(), scope); + parentContext.gauge(() -> reader.directory().toString(), true, "readerDir", Category.SEARCHER.toString(), scope); + parentContext.gauge(() -> reader.getVersion(), true, "indexVersion", Category.SEARCHER.toString(), scope); // size of the currently opened commit - parentContext.gauge(this, () -> { + parentContext.gauge(() -> { try { Collection files = reader.getIndexCommit().getFileNames(); long total = 0; @@ -2315,7 +2309,7 @@ public class SolrIndexSearcher extends IndexSearcher implements Closeable, SolrI } }, true, "indexCommitSize", Category.SEARCHER.toString(), scope); // statsCache metrics - parentContext.gauge(this, + parentContext.gauge( new MetricsMap((detailed, map) -> { statsCache.getCacheMetrics().getSnapshot(map::put); map.put("statsCacheImpl", statsCache.getClass().getSimpleName()); diff --git a/solr/core/src/java/org/apache/solr/security/AuditLoggerPlugin.java b/solr/core/src/java/org/apache/solr/security/AuditLoggerPlugin.java index d5ff666d380..c3ff2546b21 100644 --- a/solr/core/src/java/org/apache/solr/security/AuditLoggerPlugin.java +++ b/solr/core/src/java/org/apache/solr/security/AuditLoggerPlugin.java @@ -44,7 +44,6 @@ import org.apache.solr.common.SolrException; import org.apache.solr.common.util.ExecutorUtil; import org.apache.solr.common.util.SolrjNamedThreadFactory; import org.apache.solr.core.SolrInfoBean; -import org.apache.solr.metrics.SolrMetricProducer; import org.apache.solr.metrics.SolrMetricsContext; import org.apache.solr.security.AuditEvent.EventType; import org.slf4j.Logger; @@ -56,7 +55,7 @@ import org.slf4j.LoggerFactory; * @since 8.1.0 * @lucene.experimental */ -public abstract class AuditLoggerPlugin implements Closeable, Runnable, SolrInfoBean, SolrMetricProducer { +public abstract class AuditLoggerPlugin implements Closeable, Runnable, SolrInfoBean { private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); private static final String PARAM_EVENT_TYPES = "eventTypes"; static final String PARAM_ASYNC = "async"; @@ -240,17 +239,17 @@ public abstract class AuditLoggerPlugin implements Closeable, Runnable, SolrInfo solrMetricsContext = parentContext.getChildContext(this); String className = this.getClass().getSimpleName(); log.debug("Initializing metrics for {}", className); - numErrors = solrMetricsContext.meter(this, "errors", getCategory().toString(), scope, className); - numLost = solrMetricsContext.meter(this, "lost", getCategory().toString(), scope, className); - numLogged = solrMetricsContext.meter(this, "count", getCategory().toString(), scope, className); - requestTimes = solrMetricsContext.timer(this, "requestTimes", getCategory().toString(), scope, className); - totalTime = solrMetricsContext.counter(this, "totalTime", getCategory().toString(), scope, className); + numErrors = solrMetricsContext.meter("errors", getCategory().toString(), scope, className); + numLost = solrMetricsContext.meter("lost", getCategory().toString(), scope, className); + numLogged = solrMetricsContext.meter("count", getCategory().toString(), scope, className); + requestTimes = solrMetricsContext.timer("requestTimes", getCategory().toString(), scope, className); + totalTime = solrMetricsContext.counter("totalTime", getCategory().toString(), scope, className); if (async) { - solrMetricsContext.gauge(this, () -> blockingQueueSize, true, "queueCapacity", getCategory().toString(), scope, className); - solrMetricsContext.gauge(this, () -> blockingQueueSize - queue.remainingCapacity(), true, "queueSize", getCategory().toString(), scope, className); - queuedTime = solrMetricsContext.timer(this, "queuedTime", getCategory().toString(), scope, className); + solrMetricsContext.gauge(() -> blockingQueueSize, true, "queueCapacity", getCategory().toString(), scope, className); + solrMetricsContext.gauge(() -> blockingQueueSize - queue.remainingCapacity(), true, "queueSize", getCategory().toString(), scope, className); + queuedTime = solrMetricsContext.timer("queuedTime", getCategory().toString(), scope, className); } - solrMetricsContext.gauge(this, () -> async, true, "async", getCategory().toString(), scope, className); + solrMetricsContext.gauge(() -> async, true, "async", getCategory().toString(), scope, className); } @Override @@ -268,11 +267,6 @@ public abstract class AuditLoggerPlugin implements Closeable, Runnable, SolrInfo return Category.SECURITY; } - @Override - public Set getMetricNames() { - return metricNames; - } - @Override public SolrMetricsContext getSolrMetricsContext() { return solrMetricsContext; @@ -320,7 +314,7 @@ public abstract class AuditLoggerPlugin implements Closeable, Runnable, SolrInfo log.info("Shutting down async Auditlogger background thread(s)"); executorService.shutdownNow(); try { - SolrMetricProducer.super.close(); + SolrInfoBean.super.close(); } catch (Exception e) { throw new IOException("Exception closing", e); } diff --git a/solr/core/src/java/org/apache/solr/security/AuthenticationPlugin.java b/solr/core/src/java/org/apache/solr/security/AuthenticationPlugin.java index 320f661c5fc..f08651ab3d0 100644 --- a/solr/core/src/java/org/apache/solr/security/AuthenticationPlugin.java +++ b/solr/core/src/java/org/apache/solr/security/AuthenticationPlugin.java @@ -29,7 +29,6 @@ import com.codahale.metrics.Timer; import org.apache.http.HttpRequest; import org.apache.http.protocol.HttpContext; import org.apache.solr.core.SolrInfoBean; -import org.apache.solr.metrics.SolrMetricProducer; import org.apache.solr.metrics.SolrMetricsContext; import org.eclipse.jetty.client.api.Request; @@ -37,7 +36,7 @@ import org.eclipse.jetty.client.api.Request; * * @lucene.experimental */ -public abstract class AuthenticationPlugin implements SolrInfoBean, SolrMetricProducer { +public abstract class AuthenticationPlugin implements SolrInfoBean { final public static String AUTHENTICATION_PLUGIN_PROP = "authenticationPlugin"; final public static String HTTP_HEADER_X_SOLR_AUTHDATA = "X-Solr-AuthData"; @@ -147,14 +146,14 @@ public abstract class AuthenticationPlugin implements SolrInfoBean, SolrMetricPr public void initializeMetrics(SolrMetricsContext parentContext, String scope) { this.solrMetricsContext = parentContext.getChildContext(this); // Metrics - numErrors = this.solrMetricsContext.meter(this, "errors", getCategory().toString(), scope); - requests = this.solrMetricsContext.counter(this, "requests", getCategory().toString(), scope); - numAuthenticated = this.solrMetricsContext.counter(this, "authenticated",getCategory().toString(), scope); - numPassThrough = this.solrMetricsContext.counter(this, "passThrough", getCategory().toString(), scope); - numWrongCredentials = this.solrMetricsContext.counter(this, "failWrongCredentials",getCategory().toString(), scope); - numMissingCredentials = this.solrMetricsContext.counter(this, "failMissingCredentials",getCategory().toString(), scope); - requestTimes = this.solrMetricsContext.timer(this,"requestTimes", getCategory().toString(), scope); - totalTime = this.solrMetricsContext.counter(this,"totalTime", getCategory().toString(), scope); + numErrors = this.solrMetricsContext.meter("errors", getCategory().toString(), scope); + requests = this.solrMetricsContext.counter("requests", getCategory().toString(), scope); + numAuthenticated = this.solrMetricsContext.counter("authenticated",getCategory().toString(), scope); + numPassThrough = this.solrMetricsContext.counter("passThrough", getCategory().toString(), scope); + numWrongCredentials = this.solrMetricsContext.counter("failWrongCredentials",getCategory().toString(), scope); + numMissingCredentials = this.solrMetricsContext.counter("failMissingCredentials",getCategory().toString(), scope); + requestTimes = this.solrMetricsContext.timer("requestTimes", getCategory().toString(), scope); + totalTime = this.solrMetricsContext.counter("totalTime", getCategory().toString(), scope); } @Override @@ -171,9 +170,4 @@ public abstract class AuthenticationPlugin implements SolrInfoBean, SolrMetricPr public Category getCategory() { return Category.SECURITY; } - - @Override - public Set getMetricNames() { - return metricNames; - } } diff --git a/solr/core/src/java/org/apache/solr/store/blockcache/Metrics.java b/solr/core/src/java/org/apache/solr/store/blockcache/Metrics.java index d7654b60c56..37d735e1ec5 100644 --- a/solr/core/src/java/org/apache/solr/store/blockcache/Metrics.java +++ b/solr/core/src/java/org/apache/solr/store/blockcache/Metrics.java @@ -22,7 +22,6 @@ import java.util.concurrent.atomic.AtomicLong; import org.apache.solr.core.SolrInfoBean; import org.apache.solr.metrics.MetricsMap; -import org.apache.solr.metrics.SolrMetricProducer; import org.apache.solr.metrics.SolrMetricsContext; import org.apache.solr.search.SolrCacheBase; @@ -31,7 +30,7 @@ import org.apache.solr.search.SolrCacheBase; * * @lucene.experimental */ -public class Metrics extends SolrCacheBase implements SolrInfoBean, SolrMetricProducer { +public class Metrics extends SolrCacheBase implements SolrInfoBean { public AtomicLong blockCacheSize = new AtomicLong(0); @@ -103,7 +102,7 @@ public class Metrics extends SolrCacheBase implements SolrInfoBean, SolrMetricPr previous = now; }); - solrMetricsContext.gauge(this, metricsMap, true, getName(), getCategory().toString(), scope); + solrMetricsContext.gauge(metricsMap, true, getName(), getCategory().toString(), scope); } private float getPerSecond(long value, double seconds) { @@ -122,11 +121,6 @@ public class Metrics extends SolrCacheBase implements SolrInfoBean, SolrMetricPr return "Provides metrics for the HdfsDirectoryFactory BlockCache."; } - @Override - public Set getMetricNames() { - return metricNames; - } - @Override public SolrMetricsContext getSolrMetricsContext() { return solrMetricsContext; diff --git a/solr/core/src/java/org/apache/solr/store/hdfs/HdfsLocalityReporter.java b/solr/core/src/java/org/apache/solr/store/hdfs/HdfsLocalityReporter.java index 0739edeb68a..4cf90905566 100644 --- a/solr/core/src/java/org/apache/solr/store/hdfs/HdfsLocalityReporter.java +++ b/solr/core/src/java/org/apache/solr/store/hdfs/HdfsLocalityReporter.java @@ -26,18 +26,16 @@ import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; -import com.codahale.metrics.MetricRegistry; import org.apache.hadoop.fs.BlockLocation; import org.apache.hadoop.fs.FileStatus; import org.apache.hadoop.fs.FileSystem; import org.apache.solr.core.SolrInfoBean; import org.apache.solr.metrics.MetricsMap; -import org.apache.solr.metrics.SolrMetricProducer; import org.apache.solr.metrics.SolrMetricsContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class HdfsLocalityReporter implements SolrInfoBean, SolrMetricProducer { +public class HdfsLocalityReporter implements SolrInfoBean { public static final String LOCALITY_BYTES_TOTAL = "locality.bytes.total"; public static final String LOCALITY_BYTES_LOCAL = "locality.bytes.local"; public static final String LOCALITY_BYTES_RATIO = "locality.bytes.ratio"; @@ -80,16 +78,6 @@ public class HdfsLocalityReporter implements SolrInfoBean, SolrMetricProducer { return Category.OTHER; } - @Override - public Set getMetricNames() { - return metricNames; - } - - @Override - public MetricRegistry getMetricRegistry() { - return solrMetricsContext != null ? solrMetricsContext.getMetricRegistry() : null; - } - @Override public SolrMetricsContext getSolrMetricsContext() { return solrMetricsContext; @@ -150,7 +138,7 @@ public class HdfsLocalityReporter implements SolrInfoBean, SolrMetricProducer { map.put(LOCALITY_BLOCKS_RATIO, localCount / (double) totalCount); } }); - solrMetricsContext.gauge(this, metricsMap, true, "hdfsLocality", getCategory().toString(), scope); + solrMetricsContext.gauge(metricsMap, true, "hdfsLocality", getCategory().toString(), scope); } /** diff --git a/solr/core/src/java/org/apache/solr/update/DirectUpdateHandler2.java b/solr/core/src/java/org/apache/solr/update/DirectUpdateHandler2.java index d9703a256f1..b6232ae6c2a 100644 --- a/solr/core/src/java/org/apache/solr/update/DirectUpdateHandler2.java +++ b/solr/core/src/java/org/apache/solr/update/DirectUpdateHandler2.java @@ -96,7 +96,6 @@ public class DirectUpdateHandler2 extends UpdateHandler implements SolrCoreState LongAdder numDocsPending = new LongAdder(); LongAdder numErrors = new LongAdder(); Meter numErrorsCumulative; - SolrMetricsContext solrMetricsContext; // tracks when auto-commit should occur protected final CommitTracker commitTracker; @@ -171,44 +170,44 @@ public class DirectUpdateHandler2 extends UpdateHandler implements SolrCoreState @Override public void initializeMetrics(SolrMetricsContext parentContext, String scope) { solrMetricsContext = parentContext.getChildContext(this); - commitCommands = solrMetricsContext.meter(this, "commits", getCategory().toString(), scope); - solrMetricsContext.gauge(this, () -> commitTracker.getCommitCount(), true, "autoCommits", getCategory().toString(), scope); - solrMetricsContext.gauge(this, () -> softCommitTracker.getCommitCount(), true, "softAutoCommits", getCategory().toString(), scope); + commitCommands = solrMetricsContext.meter("commits", getCategory().toString(), scope); + solrMetricsContext.gauge(() -> commitTracker.getCommitCount(), true, "autoCommits", getCategory().toString(), scope); + solrMetricsContext.gauge(() -> softCommitTracker.getCommitCount(), true, "softAutoCommits", getCategory().toString(), scope); if (commitTracker.getDocsUpperBound() > 0) { - solrMetricsContext.gauge(this, () -> commitTracker.getDocsUpperBound(), true, "autoCommitMaxDocs", + solrMetricsContext.gauge(() -> commitTracker.getDocsUpperBound(), true, "autoCommitMaxDocs", getCategory().toString(), scope); } if (commitTracker.getTimeUpperBound() > 0) { - solrMetricsContext.gauge(this, () -> "" + commitTracker.getTimeUpperBound() + "ms", true, "autoCommitMaxTime", + solrMetricsContext.gauge(() -> "" + commitTracker.getTimeUpperBound() + "ms", true, "autoCommitMaxTime", getCategory().toString(), scope); } if (commitTracker.getTLogFileSizeUpperBound() > 0) { - solrMetricsContext.gauge(this, () -> commitTracker.getTLogFileSizeUpperBound(), true, "autoCommitMaxSize", + solrMetricsContext.gauge(() -> commitTracker.getTLogFileSizeUpperBound(), true, "autoCommitMaxSize", getCategory().toString(), scope); } if (softCommitTracker.getDocsUpperBound() > 0) { - solrMetricsContext.gauge(this, () -> softCommitTracker.getDocsUpperBound(), true, "softAutoCommitMaxDocs", + solrMetricsContext.gauge(() -> softCommitTracker.getDocsUpperBound(), true, "softAutoCommitMaxDocs", getCategory().toString(), scope); } if (softCommitTracker.getTimeUpperBound() > 0) { - solrMetricsContext.gauge(this, () -> "" + softCommitTracker.getTimeUpperBound() + "ms", true, "softAutoCommitMaxTime", + solrMetricsContext.gauge(() -> "" + softCommitTracker.getTimeUpperBound() + "ms", true, "softAutoCommitMaxTime", getCategory().toString(), scope); } - optimizeCommands = solrMetricsContext.meter(this, "optimizes", getCategory().toString(), scope); - rollbackCommands = solrMetricsContext.meter(this, "rollbacks", getCategory().toString(), scope); - splitCommands = solrMetricsContext.meter(this, "splits", getCategory().toString(), scope); - mergeIndexesCommands = solrMetricsContext.meter(this, "merges", getCategory().toString(), scope); - expungeDeleteCommands = solrMetricsContext.meter(this, "expungeDeletes", getCategory().toString(), scope); - solrMetricsContext.gauge(this, () -> numDocsPending.longValue(), true, "docsPending", getCategory().toString(), scope); - solrMetricsContext.gauge(this, () -> addCommands.longValue(), true, "adds", getCategory().toString(), scope); - solrMetricsContext.gauge(this, () -> deleteByIdCommands.longValue(), true, "deletesById", getCategory().toString(), scope); - solrMetricsContext.gauge(this, () -> deleteByQueryCommands.longValue(), true, "deletesByQuery", getCategory().toString(), scope); - solrMetricsContext.gauge(this, () -> numErrors.longValue(), true, "errors", getCategory().toString(), scope); + optimizeCommands = solrMetricsContext.meter("optimizes", getCategory().toString(), scope); + rollbackCommands = solrMetricsContext.meter("rollbacks", getCategory().toString(), scope); + splitCommands = solrMetricsContext.meter("splits", getCategory().toString(), scope); + mergeIndexesCommands = solrMetricsContext.meter("merges", getCategory().toString(), scope); + expungeDeleteCommands = solrMetricsContext.meter("expungeDeletes", getCategory().toString(), scope); + solrMetricsContext.gauge(() -> numDocsPending.longValue(), true, "docsPending", getCategory().toString(), scope); + solrMetricsContext.gauge(() -> addCommands.longValue(), true, "adds", getCategory().toString(), scope); + solrMetricsContext.gauge(() -> deleteByIdCommands.longValue(), true, "deletesById", getCategory().toString(), scope); + solrMetricsContext.gauge(() -> deleteByQueryCommands.longValue(), true, "deletesByQuery", getCategory().toString(), scope); + solrMetricsContext.gauge(() -> numErrors.longValue(), true, "errors", getCategory().toString(), scope); - addCommandsCumulative = solrMetricsContext.meter(this, "cumulativeAdds", getCategory().toString(), scope); - deleteByIdCommandsCumulative = solrMetricsContext.meter(this, "cumulativeDeletesById", getCategory().toString(), scope); - deleteByQueryCommandsCumulative = solrMetricsContext.meter(this, "cumulativeDeletesByQuery", getCategory().toString(), scope); - numErrorsCumulative = solrMetricsContext.meter(this, "cumulativeErrors", getCategory().toString(), scope); + addCommandsCumulative = solrMetricsContext.meter("cumulativeAdds", getCategory().toString(), scope); + deleteByIdCommandsCumulative = solrMetricsContext.meter("cumulativeDeletesById", getCategory().toString(), scope); + deleteByQueryCommandsCumulative = solrMetricsContext.meter("cumulativeDeletesByQuery", getCategory().toString(), scope); + numErrorsCumulative = solrMetricsContext.meter("cumulativeErrors", getCategory().toString(), scope); } private void deleteAll() throws IOException { @@ -803,7 +802,7 @@ public class DirectUpdateHandler2 extends UpdateHandler implements SolrCoreState numDocsPending.reset(); try { - SolrMetricProducer.super.close(); + super.close(); } catch (Exception e) { throw new IOException("Error closing", e); } diff --git a/solr/core/src/java/org/apache/solr/update/PeerSync.java b/solr/core/src/java/org/apache/solr/update/PeerSync.java index 7b7409bb691..801e48f8d42 100644 --- a/solr/core/src/java/org/apache/solr/update/PeerSync.java +++ b/solr/core/src/java/org/apache/solr/update/PeerSync.java @@ -96,6 +96,7 @@ public class PeerSync implements SolrMetricProducer { private Timer syncTime; private Counter syncErrors; private Counter syncSkipped; + private SolrMetricsContext solrMetricsContext; // comparator that sorts by absolute value, putting highest first public static Comparator absComparator = (l1, l2) -> Long.compare(Math.abs(l2), Math.abs(l1)); @@ -132,11 +133,17 @@ public class PeerSync implements SolrMetricProducer { public static final String METRIC_SCOPE = "peerSync"; + @Override + public SolrMetricsContext getSolrMetricsContext() { + return solrMetricsContext; + } + @Override public void initializeMetrics(SolrMetricsContext parentContext, String scope) { - syncTime = parentContext.timer(null, "time", scope, METRIC_SCOPE); - syncErrors = parentContext.counter(null, "errors", scope, METRIC_SCOPE); - syncSkipped = parentContext.counter(null, "skipped", scope, METRIC_SCOPE); + this.solrMetricsContext = parentContext.getChildContext(this); + syncTime = solrMetricsContext.timer("time", scope, METRIC_SCOPE); + syncErrors = solrMetricsContext.counter("errors", scope, METRIC_SCOPE); + syncSkipped = solrMetricsContext.counter("skipped", scope, METRIC_SCOPE); } public static long percentile(List arr, float frac) { diff --git a/solr/core/src/java/org/apache/solr/update/PeerSyncWithLeader.java b/solr/core/src/java/org/apache/solr/update/PeerSyncWithLeader.java index ebe41d66d27..fca105e3ba3 100644 --- a/solr/core/src/java/org/apache/solr/update/PeerSyncWithLeader.java +++ b/solr/core/src/java/org/apache/solr/update/PeerSyncWithLeader.java @@ -70,6 +70,7 @@ public class PeerSyncWithLeader implements SolrMetricProducer { private Timer syncTime; private Counter syncErrors; private Counter syncSkipped; + private SolrMetricsContext solrMetricsContext; public PeerSyncWithLeader(SolrCore core, String leaderUrl, int nUpdates) { this.core = core; @@ -89,11 +90,17 @@ public class PeerSyncWithLeader implements SolrMetricProducer { public static final String METRIC_SCOPE = "peerSync"; + @Override + public SolrMetricsContext getSolrMetricsContext() { + return solrMetricsContext; + } + @Override public void initializeMetrics(SolrMetricsContext parentContext, String scope) { - syncTime = parentContext.timer(null, "time", scope, METRIC_SCOPE); - syncErrors = parentContext.counter(null, "errors", scope, METRIC_SCOPE); - syncSkipped = parentContext.counter(null, "skipped", scope, METRIC_SCOPE); + this.solrMetricsContext = parentContext.getChildContext(this); + syncTime = solrMetricsContext.timer("time", scope, METRIC_SCOPE); + syncErrors = solrMetricsContext.counter("errors", scope, METRIC_SCOPE); + syncSkipped = solrMetricsContext.counter("skipped", scope, METRIC_SCOPE); } // start of peersync related debug messages. includes the core name for correlation. diff --git a/solr/core/src/java/org/apache/solr/update/SolrIndexWriter.java b/solr/core/src/java/org/apache/solr/update/SolrIndexWriter.java index 55597b1fa45..4070ea91643 100644 --- a/solr/core/src/java/org/apache/solr/update/SolrIndexWriter.java +++ b/solr/core/src/java/org/apache/solr/update/SolrIndexWriter.java @@ -157,21 +157,21 @@ public class SolrIndexWriter extends IndexWriter { } if (mergeDetails) { mergeTotals = true; // override - majorMergedDocs = solrMetricsContext.meter(null, "docs", SolrInfoBean.Category.INDEX.toString(), "merge", "major"); - majorDeletedDocs = solrMetricsContext.meter(null, "deletedDocs", SolrInfoBean.Category.INDEX.toString(), "merge", "major"); + majorMergedDocs = solrMetricsContext.meter("docs", SolrInfoBean.Category.INDEX.toString(), "merge", "major"); + majorDeletedDocs = solrMetricsContext.meter("deletedDocs", SolrInfoBean.Category.INDEX.toString(), "merge", "major"); } if (mergeTotals) { - minorMerge = solrMetricsContext.timer(null, "minor", SolrInfoBean.Category.INDEX.toString(), "merge"); - majorMerge = solrMetricsContext.timer(null, "major", SolrInfoBean.Category.INDEX.toString(), "merge"); - mergeErrors = solrMetricsContext.counter(null, "errors", SolrInfoBean.Category.INDEX.toString(), "merge"); + minorMerge = solrMetricsContext.timer("minor", SolrInfoBean.Category.INDEX.toString(), "merge"); + majorMerge = solrMetricsContext.timer("major", SolrInfoBean.Category.INDEX.toString(), "merge"); + mergeErrors = solrMetricsContext.counter("errors", SolrInfoBean.Category.INDEX.toString(), "merge"); String tag = core.getMetricTag(); - solrMetricsContext.gauge(null, () -> runningMajorMerges.get(), true, "running", SolrInfoBean.Category.INDEX.toString(), "merge", "major"); - solrMetricsContext.gauge(null, () -> runningMinorMerges.get(), true, "running", SolrInfoBean.Category.INDEX.toString(), "merge", "minor"); - solrMetricsContext.gauge(null, () -> runningMajorMergesDocs.get(), true, "running.docs", SolrInfoBean.Category.INDEX.toString(), "merge", "major"); - solrMetricsContext.gauge(null, () -> runningMinorMergesDocs.get(), true, "running.docs", SolrInfoBean.Category.INDEX.toString(), "merge", "minor"); - solrMetricsContext.gauge(null, () -> runningMajorMergesSegments.get(), true, "running.segments", SolrInfoBean.Category.INDEX.toString(), "merge", "major"); - solrMetricsContext.gauge(null, () -> runningMinorMergesSegments.get(), true, "running.segments", SolrInfoBean.Category.INDEX.toString(), "merge", "minor"); - flushMeter = solrMetricsContext.meter(null, "flush", SolrInfoBean.Category.INDEX.toString()); + solrMetricsContext.gauge(() -> runningMajorMerges.get(), true, "running", SolrInfoBean.Category.INDEX.toString(), "merge", "major"); + solrMetricsContext.gauge(() -> runningMinorMerges.get(), true, "running", SolrInfoBean.Category.INDEX.toString(), "merge", "minor"); + solrMetricsContext.gauge(() -> runningMajorMergesDocs.get(), true, "running.docs", SolrInfoBean.Category.INDEX.toString(), "merge", "major"); + solrMetricsContext.gauge(() -> runningMinorMergesDocs.get(), true, "running.docs", SolrInfoBean.Category.INDEX.toString(), "merge", "minor"); + solrMetricsContext.gauge(() -> runningMajorMergesSegments.get(), true, "running.segments", SolrInfoBean.Category.INDEX.toString(), "merge", "major"); + solrMetricsContext.gauge(() -> runningMinorMergesSegments.get(), true, "running.segments", SolrInfoBean.Category.INDEX.toString(), "merge", "minor"); + flushMeter = solrMetricsContext.meter("flush", SolrInfoBean.Category.INDEX.toString()); } } } diff --git a/solr/core/src/java/org/apache/solr/update/UpdateHandler.java b/solr/core/src/java/org/apache/solr/update/UpdateHandler.java index 59dae8a369a..0b122a4c470 100644 --- a/solr/core/src/java/org/apache/solr/update/UpdateHandler.java +++ b/solr/core/src/java/org/apache/solr/update/UpdateHandler.java @@ -18,9 +18,7 @@ package org.apache.solr.update; import java.io.IOException; import java.lang.invoke.MethodHandles; -import java.util.Set; import java.util.Vector; -import java.util.concurrent.ConcurrentHashMap; import org.apache.solr.core.DirectoryFactory; import org.apache.solr.core.HdfsDirectoryFactory; @@ -28,6 +26,7 @@ import org.apache.solr.core.PluginInfo; import org.apache.solr.core.SolrCore; import org.apache.solr.core.SolrEventListener; import org.apache.solr.core.SolrInfoBean; +import org.apache.solr.metrics.SolrMetricsContext; import org.apache.solr.schema.FieldType; import org.apache.solr.schema.SchemaField; import org.apache.solr.util.plugin.SolrCoreAware; @@ -55,7 +54,7 @@ public abstract class UpdateHandler implements SolrInfoBean { protected final UpdateLog ulog; - protected Set metricNames = ConcurrentHashMap.newKeySet(); + protected SolrMetricsContext solrMetricsContext; private void parseEventListeners() { final Class clazz = SolrEventListener.class; @@ -160,7 +159,6 @@ public abstract class UpdateHandler implements SolrInfoBean { public abstract int mergeIndexes(MergeIndexesCommand cmd) throws IOException; public abstract void commit(CommitUpdateCommand cmd) throws IOException; public abstract void rollback(RollbackUpdateCommand cmd) throws IOException; - public abstract void close() throws IOException; public abstract UpdateLog getUpdateLog(); /** @@ -205,8 +203,9 @@ public abstract class UpdateHandler implements SolrInfoBean { public Category getCategory() { return Category.UPDATE; } + @Override - public Set getMetricNames() { - return metricNames; + public SolrMetricsContext getSolrMetricsContext() { + return solrMetricsContext; } } diff --git a/solr/core/src/java/org/apache/solr/update/UpdateLog.java b/solr/core/src/java/org/apache/solr/update/UpdateLog.java index dbef06eed7f..9fd185dd6fd 100644 --- a/solr/core/src/java/org/apache/solr/update/UpdateLog.java +++ b/solr/core/src/java/org/apache/solr/update/UpdateLog.java @@ -449,13 +449,18 @@ public class UpdateLog implements PluginInfoInitialized, SolrMetricProducer { } }; - solrMetricsContext.gauge(null, bufferedOpsGauge, true, "ops", scope, "buffered"); - solrMetricsContext.gauge(null, () -> logs.size(), true, "logs", scope, "replay", "remaining"); - solrMetricsContext.gauge(null, () -> getTotalLogsSize(), true, "bytes", scope, "replay", "remaining"); - applyingBufferedOpsMeter = solrMetricsContext.meter(null, "ops", scope, "applyingBuffered"); - replayOpsMeter = solrMetricsContext.meter(null, "ops", scope, "replay"); - copyOverOldUpdatesMeter = solrMetricsContext.meter(null, "ops", scope, "copyOverOldUpdates"); - solrMetricsContext.gauge(null, () -> state.getValue(), true, "state", scope); + solrMetricsContext.gauge(bufferedOpsGauge, true, "ops", scope, "buffered"); + solrMetricsContext.gauge(() -> logs.size(), true, "logs", scope, "replay", "remaining"); + solrMetricsContext.gauge(() -> getTotalLogsSize(), true, "bytes", scope, "replay", "remaining"); + applyingBufferedOpsMeter = solrMetricsContext.meter("ops", scope, "applyingBuffered"); + replayOpsMeter = solrMetricsContext.meter("ops", scope, "replay"); + copyOverOldUpdatesMeter = solrMetricsContext.meter("ops", scope, "copyOverOldUpdates"); + solrMetricsContext.gauge(() -> state.getValue(), true, "state", scope); + } + + @Override + public SolrMetricsContext getSolrMetricsContext() { + return solrMetricsContext; } /** diff --git a/solr/core/src/java/org/apache/solr/update/UpdateShardHandler.java b/solr/core/src/java/org/apache/solr/update/UpdateShardHandler.java index fe966cbc929..20609784b0e 100644 --- a/solr/core/src/java/org/apache/solr/update/UpdateShardHandler.java +++ b/solr/core/src/java/org/apache/solr/update/UpdateShardHandler.java @@ -38,7 +38,6 @@ import org.apache.solr.common.util.IOUtils; import org.apache.solr.common.util.SolrjNamedThreadFactory; import org.apache.solr.core.SolrInfoBean; import org.apache.solr.metrics.SolrMetricManager; -import org.apache.solr.metrics.SolrMetricProducer; import org.apache.solr.metrics.SolrMetricsContext; import org.apache.solr.security.HttpClientBuilderPlugin; import org.apache.solr.update.processor.DistributedUpdateProcessor; @@ -53,7 +52,7 @@ import org.slf4j.LoggerFactory; import static org.apache.solr.util.stats.InstrumentedHttpRequestExecutor.KNOWN_METRIC_NAME_STRATEGIES; -public class UpdateShardHandler implements SolrMetricProducer, SolrInfoBean { +public class UpdateShardHandler implements SolrInfoBean { private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); @@ -200,11 +199,6 @@ public class UpdateShardHandler implements SolrMetricProducer, SolrInfoBean { return Category.UPDATE; } - @Override - public Set getMetricNames() { - return metricNames; - } - @Override public SolrMetricsContext getSolrMetricsContext() { return solrMetricsContext; @@ -260,7 +254,7 @@ public class UpdateShardHandler implements SolrMetricProducer, SolrInfoBean { throw new RuntimeException(e); } finally { try { - SolrMetricProducer.super.close(); + SolrInfoBean.super.close(); } catch (Exception e) { // do nothing } diff --git a/solr/core/src/java/org/apache/solr/util/stats/InstrumentedHttpListenerFactory.java b/solr/core/src/java/org/apache/solr/util/stats/InstrumentedHttpListenerFactory.java index c3bc3e569c4..821c9c05e65 100644 --- a/solr/core/src/java/org/apache/solr/util/stats/InstrumentedHttpListenerFactory.java +++ b/solr/core/src/java/org/apache/solr/util/stats/InstrumentedHttpListenerFactory.java @@ -97,7 +97,7 @@ public class InstrumentedHttpListenerFactory implements SolrMetricProducer, Http } private Timer timer(Request request) { - return solrMetricsContext.timer(null, nameStrategy.getNameFor(scope, request)); + return solrMetricsContext.timer(nameStrategy.getNameFor(scope, request)); } @Override @@ -105,5 +105,10 @@ public class InstrumentedHttpListenerFactory implements SolrMetricProducer, Http this.solrMetricsContext = parentContext; this.scope = scope; } + + @Override + public SolrMetricsContext getSolrMetricsContext() { + return solrMetricsContext; + } } diff --git a/solr/core/src/java/org/apache/solr/util/stats/InstrumentedPoolingHttpClientConnectionManager.java b/solr/core/src/java/org/apache/solr/util/stats/InstrumentedPoolingHttpClientConnectionManager.java index c7397ba3f8c..e9144df139a 100644 --- a/solr/core/src/java/org/apache/solr/util/stats/InstrumentedPoolingHttpClientConnectionManager.java +++ b/solr/core/src/java/org/apache/solr/util/stats/InstrumentedPoolingHttpClientConnectionManager.java @@ -44,14 +44,24 @@ public class InstrumentedPoolingHttpClientConnectionManager extends PoolingHttpC @Override public void initializeMetrics(SolrMetricsContext parentContext, String scope) { this.solrMetricsContext = parentContext.getChildContext(this); - parentContext.gauge(null, () -> getTotalStats().getAvailable(), + solrMetricsContext.gauge(() -> getTotalStats().getAvailable(), true, SolrMetricManager.mkName("availableConnections", scope)); // this acquires a lock on the connection pool; remove if contention sucks - parentContext.gauge(null, () -> getTotalStats().getLeased(), + solrMetricsContext.gauge(() -> getTotalStats().getLeased(), true, SolrMetricManager.mkName("leasedConnections", scope)); - parentContext.gauge(null, () -> getTotalStats().getMax(), + solrMetricsContext.gauge(() -> getTotalStats().getMax(), true, SolrMetricManager.mkName("maxConnections", scope)); - parentContext.gauge(null, () -> getTotalStats().getPending(), + solrMetricsContext.gauge(() -> getTotalStats().getPending(), true, SolrMetricManager.mkName("pendingConnections", scope)); } + + @Override + public void close() { + super.close(); + try { + SolrMetricProducer.super.close(); + } catch (Exception e) { + throw new RuntimeException("Exception closing.", e); + } + } } diff --git a/solr/core/src/java/org/apache/solr/util/stats/MetricUtils.java b/solr/core/src/java/org/apache/solr/util/stats/MetricUtils.java index 687c5055ef5..138318ada13 100644 --- a/solr/core/src/java/org/apache/solr/util/stats/MetricUtils.java +++ b/solr/core/src/java/org/apache/solr/util/stats/MetricUtils.java @@ -554,11 +554,11 @@ public class MetricUtils { * Returns an instrumented wrapper over the given executor service. */ public static ExecutorService instrumentedExecutorService(ExecutorService delegate, SolrInfoBean info, MetricRegistry metricRegistry, String scope) { - if (info != null && info.getMetricNames() != null) { - info.getMetricNames().add(MetricRegistry.name(scope, "submitted")); - info.getMetricNames().add(MetricRegistry.name(scope, "running")); - info.getMetricNames().add(MetricRegistry.name(scope, "completed")); - info.getMetricNames().add(MetricRegistry.name(scope, "duration")); + if (info != null && info.getSolrMetricsContext() != null) { + info.getSolrMetricsContext().registerMetricName(MetricRegistry.name(scope, "submitted")); + info.getSolrMetricsContext().registerMetricName(MetricRegistry.name(scope, "running")); + info.getSolrMetricsContext().registerMetricName(MetricRegistry.name(scope, "completed")); + info.getSolrMetricsContext().registerMetricName(MetricRegistry.name(scope, "duration")); } return new InstrumentedExecutorService(delegate, metricRegistry, scope); } diff --git a/solr/core/src/test/org/apache/solr/cloud/TestTlogReplica.java b/solr/core/src/test/org/apache/solr/cloud/TestTlogReplica.java index 8f5b94f31d5..26d0d8f0d98 100644 --- a/solr/core/src/test/org/apache/solr/cloud/TestTlogReplica.java +++ b/solr/core/src/test/org/apache/solr/cloud/TestTlogReplica.java @@ -476,12 +476,12 @@ public class TestTlogReplica extends SolrCloudTestCase { .process(cloudClient, collectionName); { - long docsPending = (long) getSolrCore(true).get(0).getMetricRegistry().getGauges().get("UPDATE.updateHandler.docsPending").getValue(); + long docsPending = (long) getSolrCore(true).get(0).getSolrMetricsContext().getMetricRegistry().getGauges().get("UPDATE.updateHandler.docsPending").getValue(); assertEquals("Expected 4 docs are pending in core " + getSolrCore(true).get(0).getCoreDescriptor(),4, docsPending); } for (SolrCore solrCore : getSolrCore(false)) { - long docsPending = (long) solrCore.getMetricRegistry().getGauges().get("UPDATE.updateHandler.docsPending").getValue(); + long docsPending = (long) solrCore.getSolrMetricsContext().getMetricRegistry().getGauges().get("UPDATE.updateHandler.docsPending").getValue(); assertEquals("Expected non docs are pending in core " + solrCore.getCoreDescriptor(),0, docsPending); } @@ -967,6 +967,6 @@ public class TestTlogReplica extends SolrCloudTestCase { } private long getTimesCopyOverOldUpdates(SolrCore core) { - return ((Meter)core.getMetricRegistry().getMetrics().get("TLOG.copyOverOldUpdates.ops")).getCount(); + return ((Meter)core.getSolrMetricsContext().getMetricRegistry().getMetrics().get("TLOG.copyOverOldUpdates.ops")).getCount(); } } diff --git a/solr/core/src/test/org/apache/solr/core/MockInfoBean.java b/solr/core/src/test/org/apache/solr/core/MockInfoBean.java index 3ea3ace7565..ce73a02c2c7 100644 --- a/solr/core/src/test/org/apache/solr/core/MockInfoBean.java +++ b/solr/core/src/test/org/apache/solr/core/MockInfoBean.java @@ -42,11 +42,6 @@ class MockInfoBean implements SolrInfoBean, SolrMetricProducer { return "mock"; } - @Override - public Set getMetricNames() { - return metricNames; - } - @Override public SolrMetricsContext getSolrMetricsContext() { return solrMetricsContext; @@ -65,6 +60,6 @@ class MockInfoBean implements SolrInfoBean, SolrMetricProducer { map.put("String","testing"); map.put("Object", new Object()); }); - solrMetricsContext.gauge(this, metricsMap, true, getClass().getSimpleName(), getCategory().toString(), scope); + solrMetricsContext.gauge(metricsMap, true, getClass().getSimpleName(), getCategory().toString(), scope); } } diff --git a/solr/core/src/test/org/apache/solr/core/MockQuerySenderListenerReqHandler.java b/solr/core/src/test/org/apache/solr/core/MockQuerySenderListenerReqHandler.java index ad62c40bc61..23bccdf2cdd 100644 --- a/solr/core/src/test/org/apache/solr/core/MockQuerySenderListenerReqHandler.java +++ b/solr/core/src/test/org/apache/solr/core/MockQuerySenderListenerReqHandler.java @@ -44,7 +44,7 @@ public class MockQuerySenderListenerReqHandler extends RequestHandlerBase { @Override public void initializeMetrics(SolrMetricsContext parentContext, String scope) { super.initializeMetrics(parentContext, scope); - solrMetricsContext.gauge(this, () -> initCounter.intValue(), true, "initCount", getCategory().toString(), scope); + solrMetricsContext.gauge(() -> initCounter.intValue(), true, "initCount", getCategory().toString(), scope); } @Override diff --git a/solr/core/src/test/org/apache/solr/core/RequestHandlersTest.java b/solr/core/src/test/org/apache/solr/core/RequestHandlersTest.java index 637aa24aa8e..c40929d417a 100644 --- a/solr/core/src/test/org/apache/solr/core/RequestHandlersTest.java +++ b/solr/core/src/test/org/apache/solr/core/RequestHandlersTest.java @@ -22,7 +22,6 @@ import com.codahale.metrics.Gauge; import org.apache.solr.SolrTestCaseJ4; import org.apache.solr.metrics.SolrMetricManager; import org.apache.solr.request.SolrRequestHandler; -import org.apache.solr.util.stats.MetricUtils; import org.junit.BeforeClass; import org.junit.Test; @@ -110,8 +109,8 @@ public class RequestHandlersTest extends SolrTestCaseJ4 { "text", "line up and fly directly at the enemy death cannons, clogging them with wreckage!")); assertU(commit()); - Map updateStats = MetricUtils.convertMetrics(updateHandler.getMetricRegistry(), updateHandler.getMetricNames()); - Map termStats = MetricUtils.convertMetrics(termHandler.getMetricRegistry(), termHandler.getMetricNames()); + Map updateStats = updateHandler.getSolrMetricsContext().getMetricsSnapshot(); + Map termStats = termHandler.getSolrMetricsContext().getMetricsSnapshot(); Long updateTime = (Long) updateStats.get("UPDATE./update.totalTime"); Long termTime = (Long) termStats.get("QUERY./terms.totalTime"); diff --git a/solr/core/src/test/org/apache/solr/handler/admin/MBeansHandlerTest.java b/solr/core/src/test/org/apache/solr/handler/admin/MBeansHandlerTest.java index 76168857fde..5ec4b615ef9 100644 --- a/solr/core/src/test/org/apache/solr/handler/admin/MBeansHandlerTest.java +++ b/solr/core/src/test/org/apache/solr/handler/admin/MBeansHandlerTest.java @@ -19,20 +19,18 @@ package org.apache.solr.handler.admin; import java.util.ArrayList; import java.util.Collections; import java.util.List; -import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.regex.Matcher; import java.util.regex.Pattern; -import com.codahale.metrics.MetricRegistry; import org.apache.solr.SolrTestCaseJ4; import org.apache.solr.common.params.CommonParams; import org.apache.solr.common.util.ContentStream; import org.apache.solr.common.util.ContentStreamBase; import org.apache.solr.common.util.NamedList; import org.apache.solr.core.SolrInfoBean; +import org.apache.solr.metrics.SolrMetricsContext; import org.apache.solr.request.LocalSolrQueryRequest; import org.junit.BeforeClass; import org.junit.Test; @@ -132,9 +130,8 @@ public class MBeansHandlerTest extends SolrTestCaseJ4 { @Test public void testMetricsSnapshot() throws Exception { final CountDownLatch counter = new CountDownLatch(500); - MetricRegistry registry = new MetricRegistry(); - Set names = ConcurrentHashMap.newKeySet(); SolrInfoBean bean = new SolrInfoBean() { + SolrMetricsContext solrMetricsContext; @Override public String getName() { return "foo"; @@ -151,20 +148,21 @@ public class MBeansHandlerTest extends SolrTestCaseJ4 { } @Override - public Set getMetricNames() { - return names; + public void initializeMetrics(SolrMetricsContext parentContext, String scope) { + this.solrMetricsContext = parentContext.getChildContext(this); } @Override - public MetricRegistry getMetricRegistry() { - return registry; + public SolrMetricsContext getSolrMetricsContext() { + return solrMetricsContext; } }; + bean.initializeMetrics(new SolrMetricsContext(h.getCoreContainer().getMetricManager(), "testMetricsSnapshot", "foobar"), "foo"); runSnapshots = true; Thread modifier = new Thread(() -> { int i = 0; while (runSnapshots) { - bean.registerMetricName("name-" + i++); + bean.getSolrMetricsContext().registerMetricName("name-" + i++); try { Thread.sleep(31); } catch (InterruptedException e) { @@ -176,7 +174,7 @@ public class MBeansHandlerTest extends SolrTestCaseJ4 { Thread reader = new Thread(() -> { while (runSnapshots) { try { - bean.getMetricsSnapshot(); + bean.getSolrMetricsContext().getMetricsSnapshot(); } catch (Exception e) { runSnapshots = false; e.printStackTrace(); diff --git a/solr/core/src/test/org/apache/solr/handler/admin/MetricsHandlerTest.java b/solr/core/src/test/org/apache/solr/handler/admin/MetricsHandlerTest.java index 534b7896a5c..18b4efe257d 100644 --- a/solr/core/src/test/org/apache/solr/handler/admin/MetricsHandlerTest.java +++ b/solr/core/src/test/org/apache/solr/handler/admin/MetricsHandlerTest.java @@ -443,7 +443,7 @@ public class MetricsHandlerTest extends SolrTestCaseJ4 { public void initializeMetrics(SolrMetricsContext parentContext, String scope) { super.initializeMetrics(parentContext, scope); MetricsMap metrics = new MetricsMap((detailed, map) -> map.putAll(gaugevals)); - solrMetricsContext.gauge(this, + solrMetricsContext.gauge( metrics, true, "dumphandlergauge", getCategory().toString(), scope); } diff --git a/solr/core/src/test/org/apache/solr/metrics/SolrMetricTestUtils.java b/solr/core/src/test/org/apache/solr/metrics/SolrMetricTestUtils.java index e11c06ea508..1f5f614fe8e 100644 --- a/solr/core/src/test/org/apache/solr/metrics/SolrMetricTestUtils.java +++ b/solr/core/src/test/org/apache/solr/metrics/SolrMetricTestUtils.java @@ -77,8 +77,10 @@ public final class SolrMetricTestUtils { public static SolrMetricProducer getProducerOf(SolrMetricManager metricManager, SolrInfoBean.Category category, String scope, Map metrics) { return new SolrMetricProducer() { + SolrMetricsContext solrMetricsContext; @Override public void initializeMetrics(SolrMetricsContext parentContext, String scope) { + this.solrMetricsContext = parentContext.getChildContext(this); if (category == null) { throw new IllegalArgumentException("null category"); } @@ -86,10 +88,15 @@ public final class SolrMetricTestUtils { return; } for (Map.Entry entry : metrics.entrySet()) { - parentContext.counter(null, entry.getKey(), category.toString(), scope); + solrMetricsContext.counter(entry.getKey(), category.toString(), scope); } } + @Override + public SolrMetricsContext getSolrMetricsContext() { + return solrMetricsContext; + } + @Override public String toString() { return "SolrMetricProducer.of{" + diff --git a/solr/core/src/test/org/apache/solr/search/join/TestNestedDocsSort.java b/solr/core/src/test/org/apache/solr/search/join/TestNestedDocsSort.java index 8c7ee2a4219..b8c853d6036 100644 --- a/solr/core/src/test/org/apache/solr/search/join/TestNestedDocsSort.java +++ b/solr/core/src/test/org/apache/solr/search/join/TestNestedDocsSort.java @@ -125,7 +125,7 @@ public class TestNestedDocsSort extends SolrTestCaseJ4 { try { final SolrCache cache = req.getSearcher().getCache("perSegFilter"); assertNotNull(cache); - final Map state = cache.getMetricsSnapshot(); + final Map state = cache.getSolrMetricsContext().getMetricsSnapshot(); String lookupsKey = null; for(String key : state.keySet()){ if(key.endsWith(".lookups")) { @@ -135,7 +135,7 @@ public class TestNestedDocsSort extends SolrTestCaseJ4 { } Number before = (Number) state.get(lookupsKey); parse("childfield(name_s1,$q) asc"); - Number after = (Number) cache.getMetricsSnapshot().get(lookupsKey); + Number after = (Number) cache.getSolrMetricsContext().getMetricsSnapshot().get(lookupsKey); assertEquals("parsing bjq lookups parent filter," + "parsing sort spec lookups parent and child filters, " + "hopefully for the purpose",3, after.intValue()-before.intValue());