Cleanup IndicesFieldDataCache and IndexFieldDataCache

This commit adds several asserts and removes possible `null` values
from the `FieldDataCache` implementation.

Closes #5664
This commit is contained in:
Simon Willnauer 2014-04-02 16:28:51 +02:00
parent f389c666c1
commit 7bc5ab45bc
2 changed files with 34 additions and 45 deletions

View File

@ -68,14 +68,14 @@ public interface IndexFieldDataCache {
* The resident field data cache is a *per field* cache that keeps all the values in memory. * The resident field data cache is a *per field* cache that keeps all the values in memory.
*/ */
static abstract class FieldBased implements IndexFieldDataCache, SegmentReader.CoreClosedListener, RemovalListener<FieldBased.Key, AtomicFieldData> { static abstract class FieldBased implements IndexFieldDataCache, SegmentReader.CoreClosedListener, RemovalListener<FieldBased.Key, AtomicFieldData> {
@Nullable
private final IndexService indexService; private final IndexService indexService;
private final FieldMapper.Names fieldNames; private final FieldMapper.Names fieldNames;
private final FieldDataType fieldDataType; private final FieldDataType fieldDataType;
private final Cache<Key, AtomicFieldData> cache; private final Cache<Key, AtomicFieldData> cache;
private final IndicesFieldDataCacheListener indicesFieldDataCacheListener; private final IndicesFieldDataCacheListener indicesFieldDataCacheListener;
protected FieldBased(@Nullable IndexService indexService, FieldMapper.Names fieldNames, FieldDataType fieldDataType, CacheBuilder cache, IndicesFieldDataCacheListener indicesFieldDataCacheListener) { protected FieldBased(IndexService indexService, FieldMapper.Names fieldNames, FieldDataType fieldDataType, CacheBuilder cache, IndicesFieldDataCacheListener indicesFieldDataCacheListener) {
assert indexService != null;
this.indexService = indexService; this.indexService = indexService;
this.fieldNames = fieldNames; this.fieldNames = fieldNames;
this.fieldDataType = fieldDataType; this.fieldDataType = fieldDataType;
@ -87,11 +87,12 @@ public interface IndexFieldDataCache {
@Override @Override
public void onRemoval(RemovalNotification<Key, AtomicFieldData> notification) { public void onRemoval(RemovalNotification<Key, AtomicFieldData> notification) {
Key key = notification.getKey(); final Key key = notification.getKey();
assert key != null && key.listeners != null; assert key != null && key.listeners != null;
AtomicFieldData value = notification.getValue(); final AtomicFieldData value = notification.getValue();
long sizeInBytes = key.sizeInBytes; long sizeInBytes = key.sizeInBytes;
assert sizeInBytes >= 0 || value != null : "Expected size [" + sizeInBytes + "] to be positive or value [" + value + "] to be non-null";
if (sizeInBytes == -1 && value != null) { if (sizeInBytes == -1 && value != null) {
sizeInBytes = value.getMemorySizeInBytes(); sizeInBytes = value.getMemorySizeInBytes();
} }
@ -111,16 +112,13 @@ public interface IndexFieldDataCache {
AtomicFieldData fieldData = indexFieldData.loadDirect(context); AtomicFieldData fieldData = indexFieldData.loadDirect(context);
key.sizeInBytes = fieldData.getMemorySizeInBytes(); key.sizeInBytes = fieldData.getMemorySizeInBytes();
key.listeners.add(indicesFieldDataCacheListener); key.listeners.add(indicesFieldDataCacheListener);
final ShardId shardId = ShardUtils.extractShardId(context.reader());
if (indexService != null) {
ShardId shardId = ShardUtils.extractShardId(context.reader());
if (shardId != null) { if (shardId != null) {
IndexShard shard = indexService.shard(shardId.id()); final IndexShard shard = indexService.shard(shardId.id());
if (shard != null) { if (shard != null) {
key.listeners.add(shard.fieldData()); key.listeners.add(shard.fieldData());
} }
} }
}
for (Listener listener : key.listeners) { for (Listener listener : key.listeners) {
listener.onLoad(fieldNames, fieldDataType, fieldData); listener.onLoad(fieldNames, fieldDataType, fieldData);
} }
@ -175,14 +173,14 @@ public interface IndexFieldDataCache {
static class Resident extends FieldBased { static class Resident extends FieldBased {
public Resident(@Nullable IndexService indexService, FieldMapper.Names fieldNames, FieldDataType fieldDataType, IndicesFieldDataCacheListener indicesFieldDataCacheListener) { public Resident(IndexService indexService, FieldMapper.Names fieldNames, FieldDataType fieldDataType, IndicesFieldDataCacheListener indicesFieldDataCacheListener) {
super(indexService, fieldNames, fieldDataType, CacheBuilder.newBuilder(), indicesFieldDataCacheListener); super(indexService, fieldNames, fieldDataType, CacheBuilder.newBuilder(), indicesFieldDataCacheListener);
} }
} }
static class Soft extends FieldBased { static class Soft extends FieldBased {
public Soft(@Nullable IndexService indexService, FieldMapper.Names fieldNames, FieldDataType fieldDataType, IndicesFieldDataCacheListener indicesFieldDataCacheListener) { public Soft(IndexService indexService, FieldMapper.Names fieldNames, FieldDataType fieldDataType, IndicesFieldDataCacheListener indicesFieldDataCacheListener) {
super(indexService, fieldNames, fieldDataType, CacheBuilder.newBuilder().softValues(), indicesFieldDataCacheListener); super(indexService, fieldNames, fieldDataType, CacheBuilder.newBuilder().softValues(), indicesFieldDataCacheListener);
} }
} }

View File

@ -22,7 +22,6 @@ package org.elasticsearch.indices.fielddata.cache;
import com.google.common.cache.*; import com.google.common.cache.*;
import org.apache.lucene.index.AtomicReaderContext; import org.apache.lucene.index.AtomicReaderContext;
import org.apache.lucene.index.SegmentReader; import org.apache.lucene.index.SegmentReader;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.component.AbstractComponent; import org.elasticsearch.common.component.AbstractComponent;
import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.lucene.SegmentReaderUtils; import org.elasticsearch.common.lucene.SegmentReaderUtils;
@ -50,25 +49,15 @@ import java.util.concurrent.TimeUnit;
public class IndicesFieldDataCache extends AbstractComponent implements RemovalListener<IndicesFieldDataCache.Key, AtomicFieldData> { public class IndicesFieldDataCache extends AbstractComponent implements RemovalListener<IndicesFieldDataCache.Key, AtomicFieldData> {
private final IndicesFieldDataCacheListener indicesFieldDataCacheListener; private final IndicesFieldDataCacheListener indicesFieldDataCacheListener;
private final Cache<Key, AtomicFieldData> cache;
Cache<Key, AtomicFieldData> cache;
private volatile String size;
private volatile long sizeInBytes;
private volatile TimeValue expire;
@Inject @Inject
public IndicesFieldDataCache(Settings settings, IndicesFieldDataCacheListener indicesFieldDataCacheListener) { public IndicesFieldDataCache(Settings settings, IndicesFieldDataCacheListener indicesFieldDataCacheListener) {
super(settings); super(settings);
this.indicesFieldDataCacheListener = indicesFieldDataCacheListener; this.indicesFieldDataCacheListener = indicesFieldDataCacheListener;
this.size = componentSettings.get("size", "-1"); final String size = componentSettings.get("size", "-1");
this.sizeInBytes = componentSettings.getAsMemory("size", "-1").bytes(); final long sizeInBytes = componentSettings.getAsMemory("size", "-1").bytes();
this.expire = componentSettings.getAsTime("expire", null); final TimeValue expire = componentSettings.getAsTime("expire", null);
buildCache();
}
private void buildCache() {
CacheBuilder<Key, AtomicFieldData> cacheBuilder = CacheBuilder.newBuilder() CacheBuilder<Key, AtomicFieldData> cacheBuilder = CacheBuilder.newBuilder()
.removalListener(this); .removalListener(this);
if (sizeInBytes > 0) { if (sizeInBytes > 0) {
@ -87,18 +76,18 @@ public class IndicesFieldDataCache extends AbstractComponent implements RemovalL
cache.invalidateAll(); cache.invalidateAll();
} }
public IndexFieldDataCache buildIndexFieldDataCache(@Nullable IndexService indexService, Index index, FieldMapper.Names fieldNames, FieldDataType fieldDataType) { public IndexFieldDataCache buildIndexFieldDataCache(IndexService indexService, Index index, FieldMapper.Names fieldNames, FieldDataType fieldDataType) {
return new IndexFieldCache(indexService, index, fieldNames, fieldDataType); return new IndexFieldCache(cache, indicesFieldDataCacheListener, indexService, index, fieldNames, fieldDataType);
} }
@Override @Override
public void onRemoval(RemovalNotification<Key, AtomicFieldData> notification) { public void onRemoval(RemovalNotification<Key, AtomicFieldData> notification) {
Key key = notification.getKey(); final Key key = notification.getKey();
assert key != null && key.listeners != null; assert key != null && key.listeners != null;
IndexFieldCache indexCache = key.indexCache; IndexFieldCache indexCache = key.indexCache;
long sizeInBytes = key.sizeInBytes; long sizeInBytes = key.sizeInBytes;
AtomicFieldData value = notification.getValue(); final AtomicFieldData value = notification.getValue();
assert sizeInBytes >= 0 || value != null : "Expected size [" + sizeInBytes + "] to be positive or value [" + value + "] to be non-null";
if (sizeInBytes == -1 && value != null) { if (sizeInBytes == -1 && value != null) {
sizeInBytes = value.getMemorySizeInBytes(); sizeInBytes = value.getMemorySizeInBytes();
} }
@ -119,19 +108,23 @@ public class IndicesFieldDataCache extends AbstractComponent implements RemovalL
/** /**
* A specific cache instance for the relevant parameters of it (index, fieldNames, fieldType). * A specific cache instance for the relevant parameters of it (index, fieldNames, fieldType).
*/ */
class IndexFieldCache implements IndexFieldDataCache, SegmentReader.CoreClosedListener { static class IndexFieldCache implements IndexFieldDataCache, SegmentReader.CoreClosedListener {
@Nullable
private final IndexService indexService; private final IndexService indexService;
final Index index; final Index index;
final FieldMapper.Names fieldNames; final FieldMapper.Names fieldNames;
final FieldDataType fieldDataType; final FieldDataType fieldDataType;
private final Cache<Key, AtomicFieldData> cache;
private final IndicesFieldDataCacheListener indicesFieldDataCacheListener;
IndexFieldCache(@Nullable IndexService indexService, Index index, FieldMapper.Names fieldNames, FieldDataType fieldDataType) { IndexFieldCache(final Cache<Key, AtomicFieldData> cache, IndicesFieldDataCacheListener indicesFieldDataCacheListener, IndexService indexService, Index index, FieldMapper.Names fieldNames, FieldDataType fieldDataType) {
this.indexService = indexService; this.indexService = indexService;
this.index = index; this.index = index;
this.fieldNames = fieldNames; this.fieldNames = fieldNames;
this.fieldDataType = fieldDataType; this.fieldDataType = fieldDataType;
this.cache = cache;
this.indicesFieldDataCacheListener = indicesFieldDataCacheListener;
assert indexService != null;
} }
@Override @Override
@ -143,17 +136,15 @@ public class IndicesFieldDataCache extends AbstractComponent implements RemovalL
public AtomicFieldData call() throws Exception { public AtomicFieldData call() throws Exception {
SegmentReaderUtils.registerCoreListener(context.reader(), IndexFieldCache.this); SegmentReaderUtils.registerCoreListener(context.reader(), IndexFieldCache.this);
AtomicFieldData fieldData = indexFieldData.loadDirect(context); AtomicFieldData fieldData = indexFieldData.loadDirect(context);
key.sizeInBytes = fieldData.getMemorySizeInBytes();
key.listeners.add(indicesFieldDataCacheListener); key.listeners.add(indicesFieldDataCacheListener);
final ShardId shardId = ShardUtils.extractShardId(context.reader());
if (indexService != null) {
ShardId shardId = ShardUtils.extractShardId(context.reader());
if (shardId != null) { if (shardId != null) {
IndexShard shard = indexService.shard(shardId.id()); final IndexShard shard = indexService.shard(shardId.id());
if (shard != null) { if (shard != null) {
key.listeners.add(shard.fieldData()); key.listeners.add(shard.fieldData());
} }
} }
}
for (Listener listener : key.listeners) { for (Listener listener : key.listeners) {
listener.onLoad(fieldNames, fieldDataType, fieldData); listener.onLoad(fieldNames, fieldDataType, fieldData);
} }