diff --git a/src/main/java/org/elasticsearch/index/fielddata/FieldDataStats.java b/src/main/java/org/elasticsearch/index/fielddata/FieldDataStats.java index 0195758d21a..f9255ab2dd5 100644 --- a/src/main/java/org/elasticsearch/index/fielddata/FieldDataStats.java +++ b/src/main/java/org/elasticsearch/index/fielddata/FieldDataStats.java @@ -34,17 +34,20 @@ import java.io.IOException; public class FieldDataStats implements Streamable, ToXContent { long memorySize; + long evictions; public FieldDataStats() { } - public FieldDataStats(long memorySize) { + public FieldDataStats(long memorySize, long evictions) { this.memorySize = memorySize; + this.evictions = evictions; } public void add(FieldDataStats stats) { this.memorySize += stats.memorySize; + this.evictions += stats.evictions; } public long getMemorySizeInBytes() { @@ -55,6 +58,10 @@ public class FieldDataStats implements Streamable, ToXContent { return new ByteSizeValue(memorySize); } + public long getEvictions() { + return this.evictions; + } + public static FieldDataStats readFieldDataStats(StreamInput in) throws IOException { FieldDataStats stats = new FieldDataStats(); stats.readFrom(in); @@ -64,11 +71,13 @@ public class FieldDataStats implements Streamable, ToXContent { @Override public void readFrom(StreamInput in) throws IOException { memorySize = in.readVLong(); + evictions = in.readVLong(); } @Override public void writeTo(StreamOutput out) throws IOException { out.writeVLong(memorySize); + out.writeVLong(evictions); } @Override @@ -76,6 +85,7 @@ public class FieldDataStats implements Streamable, ToXContent { builder.startObject(Fields.FIELD_DATA); builder.field(Fields.MEMORY_SIZE, memorySize); builder.field(Fields.MEMORY_SIZE_IN_BYTES, getMemorySize().toString()); + builder.field(Fields.EVICTIONS, getEvictions()); builder.endObject(); return builder; } @@ -84,5 +94,6 @@ public class FieldDataStats implements Streamable, ToXContent { static final XContentBuilderString FIELD_DATA = new XContentBuilderString("field_data"); static final XContentBuilderString MEMORY_SIZE = new XContentBuilderString("memory_size"); static final XContentBuilderString MEMORY_SIZE_IN_BYTES = new XContentBuilderString("memory_size_in_bytes"); + static final XContentBuilderString EVICTIONS = new XContentBuilderString("evictions"); } } diff --git a/src/main/java/org/elasticsearch/index/fielddata/IndexFieldDataCache.java b/src/main/java/org/elasticsearch/index/fielddata/IndexFieldDataCache.java index 15c6b1a1505..40c18c6f31f 100644 --- a/src/main/java/org/elasticsearch/index/fielddata/IndexFieldDataCache.java +++ b/src/main/java/org/elasticsearch/index/fielddata/IndexFieldDataCache.java @@ -26,7 +26,6 @@ import com.google.common.cache.RemovalNotification; import org.apache.lucene.index.AtomicReaderContext; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.SegmentReader; -import org.elasticsearch.common.Nullable; import org.elasticsearch.index.Index; import org.elasticsearch.index.mapper.FieldMapper; @@ -49,7 +48,7 @@ public interface IndexFieldDataCache { void onLoad(Index index, FieldMapper.Names fieldNames, FieldDataType fieldDataType, AtomicFieldData fieldData); - void onUnload(Index index, FieldMapper.Names fieldNames, FieldDataType fieldDataType, @Nullable AtomicFieldData fieldData); + void onUnload(Index index, FieldMapper.Names fieldNames, FieldDataType fieldDataType, RemovalNotification notification); } /** @@ -73,7 +72,7 @@ public interface IndexFieldDataCache { @Override public void onRemoval(RemovalNotification notification) { - listener.onUnload(index, fieldNames, fieldDataType, notification.getValue()); + listener.onUnload(index, fieldNames, fieldDataType, notification); } @Override diff --git a/src/main/java/org/elasticsearch/index/fielddata/IndexFieldDataService.java b/src/main/java/org/elasticsearch/index/fielddata/IndexFieldDataService.java index 7fd01eb3a7b..b3b4de39c46 100644 --- a/src/main/java/org/elasticsearch/index/fielddata/IndexFieldDataService.java +++ b/src/main/java/org/elasticsearch/index/fielddata/IndexFieldDataService.java @@ -19,10 +19,10 @@ package org.elasticsearch.index.fielddata; +import com.google.common.cache.RemovalNotification; import com.google.common.collect.ImmutableMap; import org.apache.lucene.index.IndexReader; import org.elasticsearch.ElasticSearchIllegalArgumentException; -import org.elasticsearch.common.Nullable; import org.elasticsearch.common.collect.MapBuilder; import org.elasticsearch.common.collect.Tuple; import org.elasticsearch.common.inject.Inject; @@ -73,6 +73,7 @@ public class IndexFieldDataService extends AbstractIndexComponent implements Ind private final ConcurrentMap loadedFieldData = ConcurrentCollections.newConcurrentMap(); private final CounterMetric memoryUsedInBytes = new CounterMetric(); + private final CounterMetric evictions = new CounterMetric(); public IndexFieldDataService(Index index) { this(index, ImmutableSettings.Builder.EMPTY_SETTINGS); @@ -114,15 +115,18 @@ public class IndexFieldDataService extends AbstractIndexComponent implements Ind } @Override - public void onUnload(Index index, FieldMapper.Names fieldNames, FieldDataType fieldDataType, @Nullable AtomicFieldData fieldData) { + public void onUnload(Index index, FieldMapper.Names fieldNames, FieldDataType fieldDataType, RemovalNotification notification) { assert index.equals(this.index); - if (fieldData != null) { - memoryUsedInBytes.dec(fieldData.getMemorySizeInBytes()); + if (notification.getValue() != null) { + memoryUsedInBytes.dec(notification.getValue().getMemorySizeInBytes()); + } + if (notification.wasEvicted()) { + evictions.inc(); } } public FieldDataStats stats() { - return new FieldDataStats(memoryUsedInBytes.count()); + return new FieldDataStats(memoryUsedInBytes.count(), evictions.count()); } public IFD getForField(FieldMapper mapper) {