add evictions stats to field data

This commit is contained in:
Shay Banon 2013-03-03 18:41:17 +01:00
parent e9ba98913b
commit e01879a698
3 changed files with 23 additions and 9 deletions

View File

@ -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");
}
}

View File

@ -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<Object, AtomicFieldData> notification);
}
/**
@ -73,7 +72,7 @@ public interface IndexFieldDataCache {
@Override
public void onRemoval(RemovalNotification<Object, AtomicFieldData> notification) {
listener.onUnload(index, fieldNames, fieldDataType, notification.getValue());
listener.onUnload(index, fieldNames, fieldDataType, notification);
}
@Override

View File

@ -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<String, IndexFieldData> 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<Object, AtomicFieldData> 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 extends IndexFieldData> IFD getForField(FieldMapper mapper) {