From commits-return-112102-archive-asf-public=cust-asf.ponee.io@lucene.apache.org Wed Dec 18 16:38:56 2019 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [207.244.88.153]) by mx-eu-01.ponee.io (Postfix) with SMTP id 96C3E180660 for ; Wed, 18 Dec 2019 17:38:55 +0100 (CET) Received: (qmail 14159 invoked by uid 500); 18 Dec 2019 16:38:43 -0000 Mailing-List: contact commits-help@lucene.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@lucene.apache.org Delivered-To: mailing list commits@lucene.apache.org Received: (qmail 13844 invoked by uid 99); 18 Dec 2019 16:38:42 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 18 Dec 2019 16:38:42 +0000 Received: by gitbox.apache.org (ASF Mail Server at gitbox.apache.org, from userid 33) id BC3BF8D80F; Wed, 18 Dec 2019 16:38:42 +0000 (UTC) Date: Wed, 18 Dec 2019 16:38:55 +0000 To: "commits@lucene.apache.org" Subject: [lucene-solr] 17/36: More refactoring. MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit From: ab@apache.org In-Reply-To: <157668711865.12807.1829950386408648793@gitbox.apache.org> References: <157668711865.12807.1829950386408648793@gitbox.apache.org> X-Git-Host: gitbox.apache.org X-Git-Repo: lucene-solr X-Git-Refname: refs/heads/jira/solr-13579 X-Git-Reftype: branch X-Git-Rev: 32e2cad6df07a4b8ea73d48a0bed05216abb8950 X-Git-NotificationType: diff X-Git-Multimail-Version: 1.5.dev Auto-Submitted: auto-generated Message-Id: <20191218163842.BC3BF8D80F@gitbox.apache.org> This is an automated email from the ASF dual-hosted git repository. ab pushed a commit to branch jira/solr-13579 in repository https://gitbox.apache.org/repos/asf/lucene-solr.git commit 32e2cad6df07a4b8ea73d48a0bed05216abb8950 Author: Andrzej Bialecki AuthorDate: Wed Jul 24 22:10:08 2019 +0200 More refactoring. --- .../solr/managed/DefaultResourceManager.java | 2 +- .../solr/managed/DefaultResourceManagerPool.java | 19 ++++++- .../apache/solr/managed/ManagedMetricProducer.java | 4 +- .../org/apache/solr/managed/ManagedResource.java | 6 +- .../apache/solr/managed/NoOpResourceManager.java | 12 +++- .../apache/solr/managed/ResourceManagerPool.java | 22 +++++++- .../solr/managed/plugins/CacheManagerPlugin.java | 44 +++++++-------- .../java/org/apache/solr/search/FastLRUCache.java | 64 +++++++++++----------- .../src/java/org/apache/solr/search/LFUCache.java | 63 ++++++++++----------- .../src/java/org/apache/solr/search/LRUCache.java | 37 +++++++------ .../src/java/org/apache/solr/search/SolrCache.java | 11 ++++ 11 files changed, 169 insertions(+), 115 deletions(-) diff --git a/solr/core/src/java/org/apache/solr/managed/DefaultResourceManager.java b/solr/core/src/java/org/apache/solr/managed/DefaultResourceManager.java index df211b3..d0d8233 100644 --- a/solr/core/src/java/org/apache/solr/managed/DefaultResourceManager.java +++ b/solr/core/src/java/org/apache/solr/managed/DefaultResourceManager.java @@ -52,7 +52,7 @@ public class DefaultResourceManager extends ResourceManager { public static final String MAX_NUM_POOLS_PARAM = "maxNumPools"; public static final int DEFAULT_MAX_POOLS = 100; - public static final int DEFAULT_SCHEDULE_DELAY_SECONDS = 30; + public static final int DEFAULT_SCHEDULE_DELAY_SECONDS = 10; public static final String NODE_SEARCHER_CACHE_POOL = "nodeSearcherCachePool"; diff --git a/solr/core/src/java/org/apache/solr/managed/DefaultResourceManagerPool.java b/solr/core/src/java/org/apache/solr/managed/DefaultResourceManagerPool.java index ace9da4..d46666f 100644 --- a/solr/core/src/java/org/apache/solr/managed/DefaultResourceManagerPool.java +++ b/solr/core/src/java/org/apache/solr/managed/DefaultResourceManagerPool.java @@ -38,11 +38,13 @@ public class DefaultResourceManagerPool implements ResourceManagerPool { private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); private final Map resources = new ConcurrentHashMap<>(); + private final Map resourceContexts = new ConcurrentHashMap<>(); private Map poolLimits; private final String type; private final String name; private final ResourceManagerPlugin resourceManagerPlugin; private final Map args; + private final Context poolContext = new Context(); private Map totalValues = null; private final ReentrantLock updateLock = new ReentrantLock(); int scheduleDelaySeconds; @@ -91,10 +93,12 @@ public class DefaultResourceManagerPool implements ResourceManagerPool { if (existing != null) { throw new IllegalArgumentException("Resource '" + managedResource.getResourceId() + "' already exists in pool '" + name + "' !"); } + resourceContexts.putIfAbsent(managedResource.getResourceId().toString(), new Context()); } @Override public boolean removeResource(String name) { + resourceContexts.remove(name); return resources.remove(name) != null; } @@ -143,7 +147,7 @@ public class DefaultResourceManagerPool implements ResourceManagerPool { } @Override - public Map getTotalValues() throws InterruptedException { + public Map getTotalValues() throws InterruptedException { updateLock.lockInterruptibly(); try { return Collections.unmodifiableMap(totalValues); @@ -163,6 +167,16 @@ public class DefaultResourceManagerPool implements ResourceManagerPool { } @Override + public Context getPoolContext() { + return poolContext; + } + + @Override + public Context getResourceContext(String name) { + return resourceContexts.get(name); + } + + @Override public void run() { try { resourceManagerPlugin.manage(this); @@ -177,5 +191,8 @@ public class DefaultResourceManagerPool implements ResourceManagerPool { scheduledFuture.cancel(true); scheduledFuture = null; } + resources.clear(); + resourceContexts.clear(); + poolContext.clear(); } } diff --git a/solr/core/src/java/org/apache/solr/managed/ManagedMetricProducer.java b/solr/core/src/java/org/apache/solr/managed/ManagedMetricProducer.java index 891c1d8..82d431e 100644 --- a/solr/core/src/java/org/apache/solr/managed/ManagedMetricProducer.java +++ b/solr/core/src/java/org/apache/solr/managed/ManagedMetricProducer.java @@ -29,13 +29,13 @@ import org.apache.solr.core.SolrInfoBean; public interface ManagedMetricProducer extends SolrInfoBean, ManagedResource { @Override - default Map getMonitoredValues(Collection tags) { + default Map getMonitoredValues(Collection params) { Map metrics = getMetricsSnapshot(); if (metrics == null) { return Collections.emptyMap(); } Map result = new HashMap<>(); - tags.forEach(tag -> { + params.forEach(tag -> { Object value = metrics.get(tag); if (value == null) { return; diff --git a/solr/core/src/java/org/apache/solr/managed/ManagedResource.java b/solr/core/src/java/org/apache/solr/managed/ManagedResource.java index d943384..d058c97 100644 --- a/solr/core/src/java/org/apache/solr/managed/ManagedResource.java +++ b/solr/core/src/java/org/apache/solr/managed/ManagedResource.java @@ -74,8 +74,8 @@ public interface ManagedResource { /** * Returns monitored values that are used for calculating optimal settings of managed resource limits. - * @param tags selected monitored tags, empty collection to return all monitored values - * @return map of tags to current values. + * @param params selected monitored parameters, empty collection to return all monitored values + * @return map of parameter names to current values. */ - Map getMonitoredValues(Collection tags) throws Exception; + Map getMonitoredValues(Collection params) throws Exception; } diff --git a/solr/core/src/java/org/apache/solr/managed/NoOpResourceManager.java b/solr/core/src/java/org/apache/solr/managed/NoOpResourceManager.java index 08546aa..1eccff2 100644 --- a/solr/core/src/java/org/apache/solr/managed/NoOpResourceManager.java +++ b/solr/core/src/java/org/apache/solr/managed/NoOpResourceManager.java @@ -64,7 +64,7 @@ public class NoOpResourceManager extends ResourceManager { } @Override - public Map getTotalValues() throws InterruptedException { + public Map getTotalValues() throws InterruptedException { return Collections.emptyMap(); } @@ -84,6 +84,16 @@ public class NoOpResourceManager extends ResourceManager { } @Override + public Context getPoolContext() { + return null; + } + + @Override + public Context getResourceContext(String name) { + return null; + } + + @Override public void close() throws IOException { } diff --git a/solr/core/src/java/org/apache/solr/managed/ResourceManagerPool.java b/solr/core/src/java/org/apache/solr/managed/ResourceManagerPool.java index cf32d40..a8a7666 100644 --- a/solr/core/src/java/org/apache/solr/managed/ResourceManagerPool.java +++ b/solr/core/src/java/org/apache/solr/managed/ResourceManagerPool.java @@ -1,14 +1,18 @@ package org.apache.solr.managed; import java.io.Closeable; -import java.io.IOException; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; /** * */ public interface ResourceManagerPool extends Runnable, Closeable { + public class Context extends ConcurrentHashMap { + + } + /** Unique pool name. */ String getName(); @@ -26,7 +30,7 @@ public interface ResourceManagerPool extends Runnable, Closeable { /** * Get the current monitored values from all resources. Result is a map with resource names as keys, - * and tag/value maps as values. + * and param/value maps as values. */ Map> getCurrentValues() throws InterruptedException; @@ -34,7 +38,7 @@ public interface ResourceManagerPool extends Runnable, Closeable { * This returns cumulative monitored values of all resources. *

NOTE: you must call {@link #getCurrentValues()} first!

*/ - Map getTotalValues() throws InterruptedException; + Map getTotalValues() throws InterruptedException; /** Get current pool limits. */ Map getPoolLimits(); @@ -46,4 +50,16 @@ public interface ResourceManagerPool extends Runnable, Closeable { * Pool limits are defined using controlled tags. */ void setPoolLimits(Map poolLimits); + + /** + * Pool context used for managing pool state. + */ + Context getPoolContext(); + + /** + * Resource context used for managing resource state. This context is always present for + * a resource registered in this pool, and it is unique to this pool. + * @param name resource name + */ + Context getResourceContext(String name); } diff --git a/solr/core/src/java/org/apache/solr/managed/plugins/CacheManagerPlugin.java b/solr/core/src/java/org/apache/solr/managed/plugins/CacheManagerPlugin.java index 1524d89..f522ad3 100644 --- a/solr/core/src/java/org/apache/solr/managed/plugins/CacheManagerPlugin.java +++ b/solr/core/src/java/org/apache/solr/managed/plugins/CacheManagerPlugin.java @@ -8,6 +8,7 @@ import java.util.Map; import org.apache.solr.managed.AbstractResourceManagerPlugin; import org.apache.solr.managed.ResourceManagerPool; +import org.apache.solr.search.SolrCache; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -16,51 +17,46 @@ import org.slf4j.LoggerFactory; * the management of {@link org.apache.solr.search.SolrCache} instances. *

This plugin calculates the total size and maxRamMB of all registered cache instances * and adjusts each cache's limits so that the aggregated values again fit within the pool limits.

- *

In order to avoid thrashing the plugin uses a dead zone (by default {@link #DEFAULT_DEAD_BAND}), + *

In order to avoid thrashing the plugin uses a dead band (by default {@link #DEFAULT_DEAD_BAND}), * which can be adjusted using configuration parameter {@link #DEAD_BAND}. If monitored values don't - * exceed the limits +/- the dead zone no action is taken.

+ * exceed the limits +/- the dead band then no action is taken.

*/ public class CacheManagerPlugin extends AbstractResourceManagerPlugin { private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); public static String TYPE = "cache"; - public static final String SIZE_TAG = "size"; - public static final String HIT_RATIO_TAG = "hitratio"; - public static final String RAM_BYTES_USED_TAG = "ramBytesUsed"; - public static final String MAX_RAM_MB_TAG = "maxRamMB"; - public static final String DEAD_BAND = "deadBand"; public static final float DEFAULT_DEAD_BAND = 0.1f; private static final Map controlledToMonitored = new HashMap<>(); static { - controlledToMonitored.put(MAX_RAM_MB_TAG, RAM_BYTES_USED_TAG); - controlledToMonitored.put(SIZE_TAG, SIZE_TAG); + controlledToMonitored.put(SolrCache.MAX_RAM_MB_PARAM, SolrCache.RAM_BYTES_USED_PARAM); + controlledToMonitored.put(SolrCache.MAX_SIZE_PARAM, SolrCache.SIZE_PARAM); } - private static final Collection MONITORED_TAGS = Arrays.asList( - SIZE_TAG, - HIT_RATIO_TAG, - RAM_BYTES_USED_TAG + private static final Collection MONITORED_PARAMS = Arrays.asList( + SolrCache.SIZE_PARAM, + SolrCache.HIT_RATIO_PARAM, + SolrCache.RAM_BYTES_USED_PARAM ); - private static final Collection CONTROLLED_TAGS = Arrays.asList( - MAX_RAM_MB_TAG, - SIZE_TAG + private static final Collection CONTROLLED_PARAMS = Arrays.asList( + SolrCache.MAX_RAM_MB_PARAM, + SolrCache.MAX_SIZE_PARAM ); private float deadBand = DEFAULT_DEAD_BAND; @Override public Collection getMonitoredParams() { - return MONITORED_TAGS; + return MONITORED_PARAMS; } @Override public Collection getControlledParams() { - return CONTROLLED_TAGS; + return CONTROLLED_PARAMS; } @Override @@ -81,7 +77,7 @@ public class CacheManagerPlugin extends AbstractResourceManagerPlugin { @Override public void manage(ResourceManagerPool pool) throws Exception { Map> currentValues = pool.getCurrentValues(); - Map totalValues = pool.getTotalValues(); + Map totalValues = pool.getTotalValues(); // pool limits are defined using controlled tags pool.getPoolLimits().forEach((poolLimitName, value) -> { // only numeric limits are supported @@ -96,22 +92,24 @@ public class CacheManagerPlugin extends AbstractResourceManagerPlugin { if (monitoredTag == null) { return; } - Float totalValue = totalValues.get(monitoredTag); - if (totalValue == null || totalValue <= 0.0f) { + Number totalValue = totalValues.get(monitoredTag); + if (totalValue == null || totalValue.floatValue() <= 0.0f) { return; } - float totalDelta = poolLimitValue - totalValue; + float totalDelta = poolLimitValue - totalValue.floatValue(); // dead band to avoid thrashing if (Math.abs(totalDelta / poolLimitValue) < deadBand) { return; } - float changeRatio = poolLimitValue / totalValue; + float changeRatio = poolLimitValue / totalValue.floatValue(); // modify current limits by the changeRatio pool.getResources().forEach((name, resource) -> { Map resourceLimits = resource.getResourceLimits(); Object limit = resourceLimits.get(poolLimitName); + // XXX we could attempt here to control eg. ramBytesUsed by adjusting maxSize limit + // XXX and vice versa if the current limit is undefined or unsupported if (limit == null || !(limit instanceof Number)) { return; } diff --git a/solr/core/src/java/org/apache/solr/search/FastLRUCache.java b/solr/core/src/java/org/apache/solr/search/FastLRUCache.java index 539cd05..0ad4a9b 100644 --- a/solr/core/src/java/org/apache/solr/search/FastLRUCache.java +++ b/solr/core/src/java/org/apache/solr/search/FastLRUCache.java @@ -57,7 +57,6 @@ public class FastLRUCache extends SolrCacheBase implements SolrCache, private static final long BASE_RAM_BYTES_USED = RamUsageEstimator.shallowSizeOfInstance(FastLRUCache.class); - public static final String MIN_SIZE_PARAM = "minSize"; public static final String ACCEPTABLE_SIZE_PARAM = "acceptableSize"; public static final String INITIAL_SIZE_PARAM = "initialSize"; public static final String CLEANUP_THREAD_PARAM = "cleanupThread"; @@ -73,8 +72,8 @@ public class FastLRUCache extends SolrCacheBase implements SolrCache, private int showItems = 0; private long maxRamBytes; - private int sizeLimit; - private int minSizeLimit; + private int maxSize; + private int minSize; private int initialSize; private int acceptableSize; private boolean cleanupThread; @@ -89,26 +88,26 @@ public class FastLRUCache extends SolrCacheBase implements SolrCache, public Object init(Map args, Object persistence, CacheRegenerator regenerator) { super.init(args, regenerator); String str = (String) args.get(SIZE_PARAM); - sizeLimit = str == null ? 1024 : Integer.parseInt(str); + maxSize = str == null ? 1024 : Integer.parseInt(str); str = (String) args.get(MIN_SIZE_PARAM); if (str == null) { - minSizeLimit = (int) (sizeLimit * 0.9); + minSize = (int) (maxSize * 0.9); } else { - minSizeLimit = Integer.parseInt(str); + minSize = Integer.parseInt(str); } checkAndAdjustLimits(); str = (String) args.get(ACCEPTABLE_SIZE_PARAM); if (str == null) { - acceptableSize = (int) (sizeLimit * 0.95); + acceptableSize = (int) (maxSize * 0.95); } else { acceptableSize = Integer.parseInt(str); } // acceptable limit should be somewhere between minLimit and limit - acceptableSize = Math.max(minSizeLimit, acceptableSize); + acceptableSize = Math.max(minSize, acceptableSize); str = (String) args.get(INITIAL_SIZE_PARAM); - initialSize = str == null ? sizeLimit : Integer.parseInt(str); + initialSize = str == null ? maxSize : Integer.parseInt(str); str = (String) args.get(CLEANUP_THREAD_PARAM); cleanupThread = str == null ? false : Boolean.parseBoolean(str); @@ -124,8 +123,8 @@ public class FastLRUCache extends SolrCacheBase implements SolrCache, cache = new ConcurrentLRUCache<>(ramLowerWatermark, maxRamBytes, cleanupThread, null); } else { ramLowerWatermark = -1L; - description = generateDescription(sizeLimit, initialSize, minSizeLimit, acceptableSize, cleanupThread); - cache = new ConcurrentLRUCache<>(sizeLimit, minSizeLimit, acceptableSize, initialSize, cleanupThread, false, null); + description = generateDescription(maxSize, initialSize, minSize, acceptableSize, cleanupThread); + cache = new ConcurrentLRUCache<>(maxSize, minSize, acceptableSize, initialSize, cleanupThread, false, null); } cache.setAlive(false); @@ -148,7 +147,7 @@ public class FastLRUCache extends SolrCacheBase implements SolrCache, if (maxRamBytes != Long.MAX_VALUE) { return generateDescription(maxRamBytes, ramLowerWatermark, cleanupThread); } else { - return generateDescription(sizeLimit, initialSize, minSizeLimit, acceptableSize, cleanupThread); + return generateDescription(maxSize, initialSize, minSize, acceptableSize, cleanupThread); } } @@ -280,13 +279,14 @@ public class FastLRUCache extends SolrCacheBase implements SolrCache, map.put("lookups", lookups); map.put("hits", hits); - map.put("hitratio", calcHitRatio(lookups, hits)); + map.put(HIT_RATIO_PARAM, calcHitRatio(lookups, hits)); map.put("inserts", inserts); map.put("evictions", evictions); - map.put("size", size); + map.put(SIZE_PARAM, size); map.put("cleanupThread", cleanupThread); - map.put("ramBytesUsed", ramBytesUsed()); - map.put("maxRamMB", maxRamBytes != Long.MAX_VALUE ? maxRamBytes / 1024L / 1024L : -1L); + map.put(RAM_BYTES_USED_PARAM, ramBytesUsed()); + map.put(MAX_SIZE_PARAM, maxSize); + map.put(MAX_RAM_MB_PARAM, maxRamBytes != Long.MAX_VALUE ? maxRamBytes / 1024L / 1024L : -1L); map.put("warmupTime", warmupTime); map.put("cumulative_lookups", clookups); @@ -339,8 +339,8 @@ public class FastLRUCache extends SolrCacheBase implements SolrCache, @Override public Map getResourceLimits() { Map limits = new HashMap<>(); - limits.put(SIZE_PARAM, cache.getStats().getCurrentSize()); - limits.put(MIN_SIZE_PARAM, minSizeLimit); + limits.put(MAX_SIZE_PARAM, maxSize); + limits.put(MIN_SIZE_PARAM, minSize); limits.put(ACCEPTABLE_SIZE_PARAM, acceptableSize); limits.put(CLEANUP_THREAD_PARAM, cleanupThread); limits.put(SHOW_ITEMS_PARAM, showItems); @@ -349,7 +349,7 @@ public class FastLRUCache extends SolrCacheBase implements SolrCache, } @Override - public Map getMonitoredValues(Collection tags) throws Exception { + public Map getMonitoredValues(Collection params) throws Exception { return cacheMap.getValue(); } @@ -390,21 +390,21 @@ public class FastLRUCache extends SolrCacheBase implements SolrCache, throw new IllegalArgumentException("Invalid new value for numeric limit '" + limitName +"': " + value); } switch (limitName) { - case SIZE_PARAM: - sizeLimit = value.intValue(); + case MAX_SIZE_PARAM: + maxSize = value.intValue(); checkAndAdjustLimits(); - cache.setUpperWaterMark(sizeLimit); - cache.setLowerWaterMark(minSizeLimit); + cache.setUpperWaterMark(maxSize); + cache.setLowerWaterMark(minSize); break; case MIN_SIZE_PARAM: - minSizeLimit = value.intValue(); + minSize = value.intValue(); checkAndAdjustLimits(); - cache.setUpperWaterMark(sizeLimit); - cache.setLowerWaterMark(minSizeLimit); + cache.setUpperWaterMark(maxSize); + cache.setLowerWaterMark(minSize); break; case ACCEPTABLE_SIZE_PARAM: acceptableSize = value.intValue(); - acceptableSize = Math.max(minSizeLimit, acceptableSize); + acceptableSize = Math.max(minSize, acceptableSize); cache.setAcceptableWaterMark(acceptableSize); break; case MAX_RAM_MB_PARAM: @@ -428,12 +428,12 @@ public class FastLRUCache extends SolrCacheBase implements SolrCache, } private void checkAndAdjustLimits() { - if (minSizeLimit <= 0) minSizeLimit = 1; - if (sizeLimit <= minSizeLimit) { - if (sizeLimit > 1) { - minSizeLimit = sizeLimit - 1; + if (minSize <= 0) minSize = 1; + if (maxSize <= minSize) { + if (maxSize > 1) { + minSize = maxSize - 1; } else { - sizeLimit = minSizeLimit + 1; + maxSize = minSize + 1; } } } 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 5750247..5d82a2d 100644 --- a/solr/core/src/java/org/apache/solr/search/LFUCache.java +++ b/solr/core/src/java/org/apache/solr/search/LFUCache.java @@ -63,7 +63,6 @@ public class LFUCache implements SolrCache, Accountable { public static final String TIME_DECAY_PARAM = "timeDecay"; public static final String CLEANUP_THREAD_PARAM = "cleanupThread"; public static final String INITIAL_SIZE_PARAM = "initialSize"; - public static final String MIN_SIZE_PARAM = "minSize"; public static final String ACCEPTABLE_SIZE_PARAM = "acceptableSize"; public static final String AUTOWARM_COUNT_PARAM = "autowarmCount"; public static final String SHOW_ITEMS_PARAM = "showItems"; @@ -86,8 +85,8 @@ public class LFUCache implements SolrCache, Accountable { private MetricRegistry registry; private ResourceId resourceId; - private int sizeLimit; - private int minSizeLimit; + private int maxSize; + private int minSize; private int initialSize; private int acceptableSize; private boolean cleanupThread; @@ -98,26 +97,26 @@ public class LFUCache implements SolrCache, Accountable { this.regenerator = regenerator; name = (String) args.get(NAME); String str = (String) args.get(SIZE_PARAM); - sizeLimit = str == null ? 1024 : Integer.parseInt(str); + maxSize = str == null ? 1024 : Integer.parseInt(str); str = (String) args.get(MIN_SIZE_PARAM); if (str == null) { - minSizeLimit = (int) (sizeLimit * 0.9); + minSize = (int) (maxSize * 0.9); } else { - minSizeLimit = Integer.parseInt(str); + minSize = Integer.parseInt(str); } checkAndAdjustLimits(); str = (String) args.get(ACCEPTABLE_SIZE_PARAM); if (str == null) { - acceptableSize = (int) (sizeLimit * 0.95); + acceptableSize = (int) (maxSize * 0.95); } else { acceptableSize = Integer.parseInt(str); } // acceptable limit should be somewhere between minLimit and limit - acceptableSize = Math.max(minSizeLimit, acceptableSize); + acceptableSize = Math.max(minSize, acceptableSize); str = (String) args.get(INITIAL_SIZE_PARAM); - initialSize = str == null ? sizeLimit : Integer.parseInt(str); + initialSize = str == null ? maxSize : Integer.parseInt(str); str = (String) args.get(AUTOWARM_COUNT_PARAM); autowarmCount = str == null ? 0 : Integer.parseInt(str); str = (String) args.get(CLEANUP_THREAD_PARAM); @@ -132,7 +131,7 @@ public class LFUCache implements SolrCache, Accountable { description = generateDescription(); - cache = new ConcurrentLFUCache<>(sizeLimit, minSizeLimit, acceptableSize, initialSize, cleanupThread, false, null, timeDecay); + cache = new ConcurrentLFUCache<>(maxSize, minSize, acceptableSize, initialSize, cleanupThread, false, null, timeDecay); cache.setAlive(false); statsList = (List) persistence; @@ -150,8 +149,8 @@ public class LFUCache implements SolrCache, Accountable { } private String generateDescription() { - String descr = "Concurrent LFU Cache(maxSize=" + sizeLimit + ", initialSize=" + initialSize + - ", minSize=" + minSizeLimit + ", acceptableSize=" + acceptableSize + ", cleanupThread=" + cleanupThread + + String descr = "Concurrent LFU Cache(maxSize=" + maxSize + ", initialSize=" + initialSize + + ", minSize=" + minSize + ", acceptableSize=" + acceptableSize + ", cleanupThread=" + cleanupThread + ", timeDecay=" + Boolean.toString(timeDecay); if (autowarmCount > 0) { descr += ", autowarmCount=" + autowarmCount + ", regenerator=" + regenerator; @@ -273,10 +272,13 @@ public class LFUCache implements SolrCache, Accountable { map.put("lookups", lookups); map.put("hits", hits); - map.put("hitratio", calcHitRatio(lookups, hits)); + map.put(HIT_RATIO_PARAM, calcHitRatio(lookups, hits)); map.put("inserts", inserts); map.put("evictions", evictions); - map.put("size", size); + map.put(SIZE_PARAM, size); + map.put(MAX_SIZE_PARAM, maxSize); + map.put(MIN_SIZE_PARAM, minSize); + map.put(RAM_BYTES_USED_PARAM, ramBytesUsed()); map.put("warmupTime", warmupTime); map.put("timeDecay", timeDecay); @@ -299,7 +301,6 @@ public class LFUCache implements SolrCache, Accountable { map.put("cumulative_hitratio", calcHitRatio(clookups, chits)); map.put("cumulative_inserts", cinserts); map.put("cumulative_evictions", cevictions); - map.put("ramBytesUsed", ramBytesUsed()); if (detailed && showItems != 0) { Map items = cache.getMostUsedItems(showItems == -1 ? Integer.MAX_VALUE : showItems); @@ -359,8 +360,8 @@ public class LFUCache implements SolrCache, Accountable { @Override public Map getResourceLimits() { Map limits = new HashMap<>(); - limits.put(SIZE_PARAM, cache.getStats().getCurrentSize()); - limits.put(MIN_SIZE_PARAM, minSizeLimit); + limits.put(MAX_SIZE_PARAM, maxSize); + limits.put(MIN_SIZE_PARAM, minSize); limits.put(ACCEPTABLE_SIZE_PARAM, acceptableSize); limits.put(AUTOWARM_COUNT_PARAM, autowarmCount); limits.put(CLEANUP_THREAD_PARAM, cleanupThread); @@ -370,7 +371,7 @@ public class LFUCache implements SolrCache, Accountable { } @Override - public Map getMonitoredValues(Collection tags) throws Exception { + public Map getMonitoredValues(Collection params) throws Exception { return cacheMap.getValue(); } @@ -409,21 +410,21 @@ public class LFUCache implements SolrCache, Accountable { throw new IllegalArgumentException("Out of range new value for numeric limit '" + limitName +"': " + value); } switch (limitName) { - case SIZE_PARAM: - sizeLimit = value.intValue(); + case MAX_SIZE_PARAM: + maxSize = value.intValue(); checkAndAdjustLimits(); - cache.setUpperWaterMark(sizeLimit); - cache.setLowerWaterMark(minSizeLimit); + cache.setUpperWaterMark(maxSize); + cache.setLowerWaterMark(minSize); break; case MIN_SIZE_PARAM: - minSizeLimit = value.intValue(); + minSize = value.intValue(); checkAndAdjustLimits(); - cache.setUpperWaterMark(sizeLimit); - cache.setLowerWaterMark(minSizeLimit); + cache.setUpperWaterMark(maxSize); + cache.setLowerWaterMark(minSize); break; case ACCEPTABLE_SIZE_PARAM: acceptableSize = value.intValue(); - acceptableSize = Math.max(minSizeLimit, acceptableSize); + acceptableSize = Math.max(minSize, acceptableSize); cache.setAcceptableWaterMark(acceptableSize); break; case AUTOWARM_COUNT_PARAM: @@ -440,12 +441,12 @@ public class LFUCache implements SolrCache, Accountable { } private void checkAndAdjustLimits() { - if (minSizeLimit <= 0) minSizeLimit = 1; - if (sizeLimit <= minSizeLimit) { - if (sizeLimit > 1) { - minSizeLimit = sizeLimit - 1; + if (minSize <= 0) minSize = 1; + if (maxSize <= minSize) { + if (maxSize > 1) { + minSize = maxSize - 1; } else { - sizeLimit = minSizeLimit + 1; + maxSize = minSize + 1; } } } 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 535c2f0..3ab0d60 100644 --- a/solr/core/src/java/org/apache/solr/search/LRUCache.java +++ b/solr/core/src/java/org/apache/solr/search/LRUCache.java @@ -79,7 +79,7 @@ public class LRUCache extends SolrCacheBase implements SolrCache, Acco private MetricsMap cacheMap; private Set metricNames = ConcurrentHashMap.newKeySet(); private MetricRegistry registry; - private int sizeLimit; + private int maxSize; private int initialSize; private ResourceId resourceId; @@ -92,9 +92,9 @@ public class LRUCache extends SolrCacheBase implements SolrCache, Acco public Object init(Map args, Object persistence, CacheRegenerator regenerator) { super.init(args, regenerator); String str = (String)args.get(SIZE_PARAM); - this.sizeLimit = str==null ? 1024 : Integer.parseInt(str); + this.maxSize = str==null ? 1024 : Integer.parseInt(str); str = (String)args.get("initialSize"); - initialSize = Math.min(str==null ? 1024 : Integer.parseInt(str), sizeLimit); + initialSize = Math.min(str==null ? 1024 : Integer.parseInt(str), maxSize); str = (String) args.get(MAX_RAM_MB_PARAM); this.maxRamBytes = str == null ? Long.MAX_VALUE : (long) (Double.parseDouble(str) * 1024L * 1024L); description = generateDescription(); @@ -119,7 +119,7 @@ public class LRUCache extends SolrCacheBase implements SolrCache, Acco // must return false according to javadocs of removeEldestEntry if we're modifying // the map ourselves return false; - } else if (size() > getSizeLimit()) { + } else if (size() > getMaxSize()) { Iterator> iterator = entrySet().iterator(); do { Map.Entry entry = iterator.next(); @@ -133,7 +133,7 @@ public class LRUCache extends SolrCacheBase implements SolrCache, Acco iterator.remove(); evictions++; stats.evictions.increment(); - } while (iterator.hasNext() && size() > getSizeLimit()); + } while (iterator.hasNext() && size() > getMaxSize()); // must return false according to javadocs of removeEldestEntry if we're modifying // the map ourselves return false; @@ -153,8 +153,8 @@ public class LRUCache extends SolrCacheBase implements SolrCache, Acco return persistence; } - public int getSizeLimit() { - return sizeLimit; + public int getMaxSize() { + return maxSize; } public long getMaxRamBytes() { @@ -166,7 +166,7 @@ public class LRUCache extends SolrCacheBase implements SolrCache, Acco * @return Returns the description of this cache. */ private String generateDescription() { - String description = "LRU Cache(maxSize=" + getSizeLimit() + ", initialSize=" + initialSize; + String description = "LRU Cache(maxSize=" + getMaxSize() + ", initialSize=" + initialSize; if (isAutowarmingOn()) { description += ", " + getAutowarmDescription(); } @@ -186,7 +186,7 @@ public class LRUCache extends SolrCacheBase implements SolrCache, Acco @Override public V put(K key, V value) { - if (sizeLimit == Integer.MAX_VALUE && maxRamBytes == Long.MAX_VALUE) { + if (maxSize == Integer.MAX_VALUE && maxRamBytes == Long.MAX_VALUE) { throw new IllegalStateException("Cache: " + getName() + " has neither size nor RAM limit!"); } synchronized (map) { @@ -319,12 +319,13 @@ public class LRUCache extends SolrCacheBase implements SolrCache, Acco synchronized (map) { res.put("lookups", lookups); res.put("hits", hits); - res.put("hitratio", calcHitRatio(lookups,hits)); + res.put(HIT_RATIO_PARAM, calcHitRatio(lookups,hits)); res.put("inserts", inserts); res.put("evictions", evictions); - res.put("size", map.size()); - res.put("ramBytesUsed", ramBytesUsed()); - res.put("maxRamMB", maxRamBytes != Long.MAX_VALUE ? maxRamBytes / 1024L / 1024L : -1L); + res.put(SIZE_PARAM, map.size()); + res.put(MAX_SIZE_PARAM, maxSize); + res.put(RAM_BYTES_USED_PARAM, ramBytesUsed()); + res.put(MAX_RAM_MB_PARAM, maxRamBytes != Long.MAX_VALUE ? maxRamBytes / 1024L / 1024L : -1L); res.put("evictionsRamUsage", evictionsRamUsage); } res.put("warmupTime", warmupTime); @@ -374,13 +375,13 @@ public class LRUCache extends SolrCacheBase implements SolrCache, Acco @Override public Map getResourceLimits() { Map limits = new HashMap<>(); - limits.put(SIZE_PARAM, sizeLimit); + limits.put(MAX_SIZE_PARAM, maxSize); limits.put(MAX_RAM_MB_PARAM, maxRamBytes != Long.MAX_VALUE ? maxRamBytes / 1024L / 1024L : -1L); return limits; } @Override - public Map getMonitoredValues(Collection tags) throws Exception { + public Map getMonitoredValues(Collection params) throws Exception { return cacheMap.getValue(); } @@ -408,11 +409,11 @@ public class LRUCache extends SolrCacheBase implements SolrCache, Acco throw new IllegalArgumentException("Invalid new value for limit '" + limitName +"': " + value); } switch (limitName) { - case SIZE_PARAM: + case MAX_SIZE_PARAM: if (value.intValue() > 0) { - sizeLimit = value.intValue(); + maxSize = value.intValue(); } else { - sizeLimit = Integer.MAX_VALUE; + maxSize = Integer.MAX_VALUE; } break; case MAX_RAM_MB_PARAM: 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 14fbb82..e2c6440 100644 --- a/solr/core/src/java/org/apache/solr/search/SolrCache.java +++ b/solr/core/src/java/org/apache/solr/search/SolrCache.java @@ -28,8 +28,19 @@ import java.util.Map; */ public interface SolrCache extends SolrInfoBean, SolrMetricProducer, ManagedResource { + /** Current size of the cache. */ String SIZE_PARAM = "size"; + /** Maximum size of the cache. */ + String MAX_SIZE_PARAM = "maxSize"; + /** Minimum size of the cache. */ + String MIN_SIZE_PARAM = "minSize"; + /** Maximum RAM use in MB. */ String MAX_RAM_MB_PARAM = "maxRamMB"; + /** Ram usage estimate. */ + String RAM_BYTES_USED_PARAM = "ramBytesUsed"; + // not camelCase for back-compat + /** Cache hit ratio. */ + String HIT_RATIO_PARAM = "hitratio"; /** * The initialization routine. Instance specific arguments are passed in