Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

LUCENE-7801: SortedSetDocValuesReaderState now implements Accountable

Mike McCandless2f6101b2ab9e
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index aeeebd69d9d..a99a2be29e4 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -108,6 +108,10 @@ Improvements
* LUCENE-7785: Move dictionary for Ukrainian analyzer to external dependency.
(Andriy Rysin via Steve Rowe, Dawid Weiss)
+* LUCENE-7801: SortedSetDocValuesReaderState now implements
+ Accountable so you can see how much RAM it's using (Robert Muir,
+ Mike McCandless)
+
Optimizations
* LUCENE-7787: spatial-extras HeatmapFacetCounter will now short-circuit it's
diff --git a/lucene/facet/src/java/org/apache/lucene/facet/FacetsConfig.java b/lucene/facet/src/java/org/apache/lucene/facet/FacetsConfig.java
index 96db60f4e42..ea67294be52 100644
--- a/lucene/facet/src/java/org/apache/lucene/facet/FacetsConfig.java
+++ b/lucene/facet/src/java/org/apache/lucene/facet/FacetsConfig.java
@@ -105,7 +105,7 @@ public class FacetsConfig {
*
* @return The default configuration to be used for dimensions that
* are not yet set in the {@link FacetsConfig} */
- protected DimConfig getDefaultDimConfig(){
+ protected DimConfig getDefaultDimConfig() {
return DEFAULT_DIM_CONFIG;
}
diff --git a/lucene/facet/src/java/org/apache/lucene/facet/sortedset/DefaultSortedSetDocValuesReaderState.java b/lucene/facet/src/java/org/apache/lucene/facet/sortedset/DefaultSortedSetDocValuesReaderState.java
index 6bcfa465b47..cff1cca06d8 100644
--- a/lucene/facet/src/java/org/apache/lucene/facet/sortedset/DefaultSortedSetDocValuesReaderState.java
+++ b/lucene/facet/src/java/org/apache/lucene/facet/sortedset/DefaultSortedSetDocValuesReaderState.java
@@ -18,6 +18,7 @@ package org.apache.lucene.facet.sortedset;
import java.io.IOException;
import java.util.Arrays;
+import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
@@ -33,6 +34,8 @@ import org.apache.lucene.index.MultiDocValues.MultiSortedSetDocValues;
import org.apache.lucene.index.MultiDocValues.OrdinalMap;
import org.apache.lucene.index.MultiDocValues;
import org.apache.lucene.index.SortedSetDocValues;
+import org.apache.lucene.util.Accountable;
+import org.apache.lucene.util.Accountables;
import org.apache.lucene.util.BytesRef;
/**
@@ -104,11 +107,46 @@ public class DefaultSortedSetDocValuesReaderState extends SortedSetDocValuesRead
}
}
+ /**
+ * Return the memory usage of this object in bytes. Negative values are illegal.
+ */
+ @Override
+ public long ramBytesUsed() {
+ synchronized (cachedOrdMaps) {
+ long bytes = 0;
+ for (OrdinalMap map : cachedOrdMaps.values()) {
+ bytes += map.ramBytesUsed();
+ }
+
+ return bytes;
+ }
+ }
+
+ /**
+ * Returns nested resources of this class.
+ * The result should be a point-in-time snapshot (to avoid race conditions).
+ * @see Accountables
+ */
+ @Override
+ public Collection<Accountable> getChildResources() {
+ synchronized (cachedOrdMaps) {
+ return Accountables.namedAccountables("DefaultSortedSetDocValuesReaderState", cachedOrdMaps);
+ }
+ }
+
+ @Override
+ public String toString() {
+ return "DefaultSortedSetDocValuesReaderState(field=" + field + " origReader=" + origReader + ")";
+ }
+
/** Return top-level doc values. */
@Override
public SortedSetDocValues getDocValues() throws IOException {
// TODO: this is dup'd from slow composite reader wrapper ... can we factor it out to share?
OrdinalMap map = null;
+ // TODO: why are we lazy about this? It's better if ctor pays the cost, not first query? Oh, but we
+ // call this method from ctor, ok. Also, we only ever store one entry in the map (for key=field) so
+ // why are we using a map?
synchronized (cachedOrdMaps) {
map = cachedOrdMaps.get(field);
if (map == null) {
diff --git a/lucene/facet/src/java/org/apache/lucene/facet/sortedset/SortedSetDocValuesReaderState.java b/lucene/facet/src/java/org/apache/lucene/facet/sortedset/SortedSetDocValuesReaderState.java
index 83ed3f04f25..546b3198ed2 100644
--- a/lucene/facet/src/java/org/apache/lucene/facet/sortedset/SortedSetDocValuesReaderState.java
+++ b/lucene/facet/src/java/org/apache/lucene/facet/sortedset/SortedSetDocValuesReaderState.java
@@ -21,6 +21,7 @@ import java.util.Map;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.SortedSetDocValues;
+import org.apache.lucene.util.Accountable;
/** Wraps a {@link IndexReader} and resolves ords
* using existing {@link SortedSetDocValues} APIs without a
@@ -38,7 +39,7 @@ import org.apache.lucene.index.SortedSetDocValues;
* so you should create it once and re-use that one instance
* for a given {@link IndexReader}. */
-public abstract class SortedSetDocValuesReaderState {
+public abstract class SortedSetDocValuesReaderState implements Accountable {
/** Holds start/end range of ords, which maps to one
* dimension (someday we may generalize it to map to
diff --git a/lucene/facet/src/test/org/apache/lucene/facet/sortedset/TestSortedSetDocValuesFacets.java b/lucene/facet/src/test/org/apache/lucene/facet/sortedset/TestSortedSetDocValuesFacets.java
index 5aed22b4e84..3ad6e683d4f 100644
--- a/lucene/facet/src/test/org/apache/lucene/facet/sortedset/TestSortedSetDocValuesFacets.java
+++ b/lucene/facet/src/test/org/apache/lucene/facet/sortedset/TestSortedSetDocValuesFacets.java
@@ -18,6 +18,7 @@ package org.apache.lucene.facet.sortedset;
import java.io.IOException;
import java.util.ArrayList;
+import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -39,6 +40,7 @@ import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
+import org.apache.lucene.util.Accountable;
import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.TestUtil;
@@ -178,6 +180,17 @@ public class TestSortedSetDocValuesFacets extends FacetTestCase {
assertEquals("dim=b path=[] value=2 childCount=2\n bar1 (1)\n bar2 (1)\n", results.get(1).toString());
assertEquals("dim=c path=[] value=1 childCount=1\n baz1 (1)\n", results.get(2).toString());
+ Collection<Accountable> resources = state.getChildResources();
+ assertTrue(state.toString().contains(FacetsConfig.DEFAULT_INDEX_FIELD_NAME));
+ if (searcher.getIndexReader().leaves().size() > 1) {
+ assertTrue(state.ramBytesUsed() > 0);
+ assertFalse(resources.isEmpty());
+ assertTrue(resources.toString().contains(FacetsConfig.DEFAULT_INDEX_FIELD_NAME));
+ } else {
+ assertEquals(0, state.ramBytesUsed());
+ assertTrue(resources.isEmpty());
+ }
+
searcher.getIndexReader().close();
dir.close();
}