allow CacheRecycler to be cleared via the REST API

This commit is contained in:
Simon Willnauer 2013-04-19 11:45:33 +02:00
parent 79db1bfbf0
commit 05b6c46bec
8 changed files with 104 additions and 35 deletions

View File

@ -34,9 +34,11 @@ public class ClearIndicesCacheRequest extends BroadcastOperationRequest<ClearInd
private boolean filterCache = false; private boolean filterCache = false;
private boolean fieldDataCache = false; private boolean fieldDataCache = false;
private boolean idCache = false; private boolean idCache = false;
private boolean recycler = false;
private String[] fields = null; private String[] fields = null;
private String[] filterKeys = null; private String[] filterKeys = null;
ClearIndicesCacheRequest() { ClearIndicesCacheRequest() {
} }
@ -86,6 +88,15 @@ public class ClearIndicesCacheRequest extends BroadcastOperationRequest<ClearInd
return this.idCache; return this.idCache;
} }
public ClearIndicesCacheRequest recycler(boolean recycler) {
this.recycler = recycler;
return this;
}
public boolean recycler() {
return this.recycler;
}
public ClearIndicesCacheRequest idCache(boolean idCache) { public ClearIndicesCacheRequest idCache(boolean idCache) {
this.idCache = idCache; this.idCache = idCache;
return this; return this;
@ -96,6 +107,7 @@ public class ClearIndicesCacheRequest extends BroadcastOperationRequest<ClearInd
filterCache = in.readBoolean(); filterCache = in.readBoolean();
fieldDataCache = in.readBoolean(); fieldDataCache = in.readBoolean();
idCache = in.readBoolean(); idCache = in.readBoolean();
recycler = in.readBoolean();
fields = in.readStringArray(); fields = in.readStringArray();
filterKeys = in.readStringArray(); filterKeys = in.readStringArray();
} }
@ -105,7 +117,10 @@ public class ClearIndicesCacheRequest extends BroadcastOperationRequest<ClearInd
out.writeBoolean(filterCache); out.writeBoolean(filterCache);
out.writeBoolean(fieldDataCache); out.writeBoolean(fieldDataCache);
out.writeBoolean(idCache); out.writeBoolean(idCache);
out.writeBoolean(recycler);
out.writeStringArrayNullable(fields); out.writeStringArrayNullable(fields);
out.writeStringArrayNullable(filterKeys); out.writeStringArrayNullable(filterKeys);
} }
} }

View File

@ -33,6 +33,8 @@ class ShardClearIndicesCacheRequest extends BroadcastShardOperationRequest {
private boolean filterCache = false; private boolean filterCache = false;
private boolean fieldDataCache = false; private boolean fieldDataCache = false;
private boolean idCache = false; private boolean idCache = false;
private boolean recycler;
private String[] fields = null; private String[] fields = null;
private String[] filterKeys = null; private String[] filterKeys = null;
@ -46,6 +48,7 @@ class ShardClearIndicesCacheRequest extends BroadcastShardOperationRequest {
idCache = request.idCache(); idCache = request.idCache();
fields = request.fields(); fields = request.fields();
filterKeys = request.filterKeys(); filterKeys = request.filterKeys();
recycler = request.recycler();
} }
public boolean filterCache() { public boolean filterCache() {
@ -60,6 +63,10 @@ class ShardClearIndicesCacheRequest extends BroadcastShardOperationRequest {
return this.idCache; return this.idCache;
} }
public boolean recycler() {
return this.recycler;
}
public String[] fields() { public String[] fields() {
return this.fields; return this.fields;
} }
@ -79,6 +86,7 @@ class ShardClearIndicesCacheRequest extends BroadcastShardOperationRequest {
filterCache = in.readBoolean(); filterCache = in.readBoolean();
fieldDataCache = in.readBoolean(); fieldDataCache = in.readBoolean();
idCache = in.readBoolean(); idCache = in.readBoolean();
recycler = in.readBoolean();
fields = in.readStringArray(); fields = in.readStringArray();
filterKeys = in.readStringArray(); filterKeys = in.readStringArray();
} }
@ -89,6 +97,7 @@ class ShardClearIndicesCacheRequest extends BroadcastShardOperationRequest {
out.writeBoolean(filterCache); out.writeBoolean(filterCache);
out.writeBoolean(fieldDataCache); out.writeBoolean(fieldDataCache);
out.writeBoolean(idCache); out.writeBoolean(idCache);
out.writeBoolean(recycler);
out.writeStringArrayNullable(fields); out.writeStringArrayNullable(fields);
out.writeStringArrayNullable(filterKeys); out.writeStringArrayNullable(filterKeys);
} }

View File

@ -30,6 +30,7 @@ import org.elasticsearch.cluster.block.ClusterBlockException;
import org.elasticsearch.cluster.block.ClusterBlockLevel; import org.elasticsearch.cluster.block.ClusterBlockLevel;
import org.elasticsearch.cluster.routing.GroupShardsIterator; import org.elasticsearch.cluster.routing.GroupShardsIterator;
import org.elasticsearch.cluster.routing.ShardRouting; import org.elasticsearch.cluster.routing.ShardRouting;
import org.elasticsearch.common.CacheRecycler;
import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.service.IndexService; import org.elasticsearch.index.service.IndexService;
@ -143,6 +144,11 @@ public class TransportClearIndicesCacheAction extends TransportBroadcastOperatio
} }
} }
} }
if (request.recycler()) {
logger.info("Clear CacheRecycler on index [{}]", service.index());
clearedAtLeastOne = true;
CacheRecycler.clear();
}
if (request.idCache()) { if (request.idCache()) {
clearedAtLeastOne = true; clearedAtLeastOne = true;
service.cache().idCache().clear(); service.cache().idCache().clear();

View File

@ -21,13 +21,13 @@ package org.elasticsearch.common;
import gnu.trove.map.hash.*; import gnu.trove.map.hash.*;
import gnu.trove.set.hash.THashSet; import gnu.trove.set.hash.THashSet;
import org.elasticsearch.common.trove.ExtTDoubleObjectHashMap; import org.elasticsearch.common.trove.ExtTDoubleObjectHashMap;
import org.elasticsearch.common.trove.ExtTHashMap; import org.elasticsearch.common.trove.ExtTHashMap;
import org.elasticsearch.common.trove.ExtTLongObjectHashMap; import org.elasticsearch.common.trove.ExtTLongObjectHashMap;
public final class CacheRecycler { public final class CacheRecycler {
private static final Recycler INSTANCE; private static final Recycler INSTANCE;
static { static {
String property = System.getProperty("ES.RECYCLE"); String property = System.getProperty("ES.RECYCLE");

View File

@ -42,7 +42,7 @@ import org.elasticsearch.common.util.concurrent.ConcurrentCollections;
public class DefaultCacheRecycler implements Recycler { public class DefaultCacheRecycler implements Recycler {
private static final int QUEUE_MAX_SIZE = 256;
@Override @Override
public void clear() { public void clear() {
@ -113,9 +113,11 @@ public class DefaultCacheRecycler implements Recycler {
ref = ConcurrentCollections.newQueue(); ref = ConcurrentCollections.newQueue();
hashMap.set(ref); hashMap.set(ref);
} }
if (ref.size() < QUEUE_MAX_SIZE) {
map.clear(); map.clear();
ref.add(map); ref.add(map);
} }
}
// ----- THashSet ----- // ----- THashSet -----
@ -148,9 +150,11 @@ public class DefaultCacheRecycler implements Recycler {
ref = ConcurrentCollections.newQueue(); ref = ConcurrentCollections.newQueue();
hashSet.set(ref); hashSet.set(ref);
} }
if (ref.size() < QUEUE_MAX_SIZE) {
map.clear(); map.clear();
ref.add(map); ref.add(map);
} }
}
// ------ ExtTDoubleObjectHashMap ----- // ------ ExtTDoubleObjectHashMap -----
@ -183,9 +187,11 @@ public class DefaultCacheRecycler implements Recycler {
ref = ConcurrentCollections.newQueue(); ref = ConcurrentCollections.newQueue();
doubleObjectHashMap.set(ref); doubleObjectHashMap.set(ref);
} }
if (ref.size() < QUEUE_MAX_SIZE) {
map.clear(); map.clear();
ref.add(map); ref.add(map);
} }
}
// ----- ExtTLongObjectHashMap ---- // ----- ExtTLongObjectHashMap ----
@ -218,9 +224,11 @@ public class DefaultCacheRecycler implements Recycler {
ref = ConcurrentCollections.newQueue(); ref = ConcurrentCollections.newQueue();
longObjectHashMap.set(ref); longObjectHashMap.set(ref);
} }
if (ref.size() < QUEUE_MAX_SIZE) {
map.clear(); map.clear();
ref.add(map); ref.add(map);
} }
}
// ----- TLongLongHashMap ---- // ----- TLongLongHashMap ----
@ -252,9 +260,11 @@ public class DefaultCacheRecycler implements Recycler {
ref = ConcurrentCollections.newQueue(); ref = ConcurrentCollections.newQueue();
longLongHashMap.set(ref); longLongHashMap.set(ref);
} }
if (ref.size() < QUEUE_MAX_SIZE) {
map.clear(); map.clear();
ref.add(map); ref.add(map);
} }
}
// ----- TIntIntHashMap ---- // ----- TIntIntHashMap ----
@ -287,9 +297,11 @@ public class DefaultCacheRecycler implements Recycler {
ref = ConcurrentCollections.newQueue(); ref = ConcurrentCollections.newQueue();
intIntHashMap.set(ref); intIntHashMap.set(ref);
} }
if (ref.size() < QUEUE_MAX_SIZE) {
map.clear(); map.clear();
ref.add(map); ref.add(map);
} }
}
// ----- TFloatIntHashMap --- // ----- TFloatIntHashMap ---
@ -323,9 +335,11 @@ public class DefaultCacheRecycler implements Recycler {
ref = ConcurrentCollections.newQueue(); ref = ConcurrentCollections.newQueue();
floatIntHashMap.set(ref); floatIntHashMap.set(ref);
} }
if (ref.size() < QUEUE_MAX_SIZE) {
map.clear(); map.clear();
ref.add(map); ref.add(map);
} }
}
// ----- TDoubleIntHashMap --- // ----- TDoubleIntHashMap ---
@ -359,9 +373,11 @@ public class DefaultCacheRecycler implements Recycler {
ref = ConcurrentCollections.newQueue(); ref = ConcurrentCollections.newQueue();
doubleIntHashMap.set(ref); doubleIntHashMap.set(ref);
} }
if (ref.size() < QUEUE_MAX_SIZE) {
map.clear(); map.clear();
ref.add(map); ref.add(map);
} }
}
// ----- TByteIntHashMap --- // ----- TByteIntHashMap ---
@ -395,9 +411,11 @@ public class DefaultCacheRecycler implements Recycler {
ref = ConcurrentCollections.newQueue(); ref = ConcurrentCollections.newQueue();
byteIntHashMap.set(ref); byteIntHashMap.set(ref);
} }
if (ref.size() < QUEUE_MAX_SIZE) {
map.clear(); map.clear();
ref.add(map); ref.add(map);
} }
}
// ----- TShortIntHashMap --- // ----- TShortIntHashMap ---
@ -430,9 +448,11 @@ public class DefaultCacheRecycler implements Recycler {
ref = ConcurrentCollections.newQueue(); ref = ConcurrentCollections.newQueue();
shortIntHashMap.set(ref); shortIntHashMap.set(ref);
} }
if (ref.size() < QUEUE_MAX_SIZE) {
map.clear(); map.clear();
ref.add(map); ref.add(map);
} }
}
// ----- TLongIntHashMap ---- // ----- TLongIntHashMap ----
@ -466,9 +486,11 @@ public class DefaultCacheRecycler implements Recycler {
ref = ConcurrentCollections.newQueue(); ref = ConcurrentCollections.newQueue();
longIntHashMap.set(ref); longIntHashMap.set(ref);
} }
if (ref.size() < QUEUE_MAX_SIZE) {
map.clear(); map.clear();
ref.add(map); ref.add(map);
} }
}
// ------ TObjectIntHashMap ----- // ------ TObjectIntHashMap -----
@ -502,9 +524,11 @@ public class DefaultCacheRecycler implements Recycler {
ref = ConcurrentCollections.newQueue(); ref = ConcurrentCollections.newQueue();
objectIntHashMap.set(ref); objectIntHashMap.set(ref);
} }
if (ref.size() < QUEUE_MAX_SIZE) {
map.clear(); map.clear();
ref.add(map); ref.add(map);
} }
}
// ------ TIntObjectHashMap ----- // ------ TIntObjectHashMap -----
@ -538,9 +562,11 @@ public class DefaultCacheRecycler implements Recycler {
ref = ConcurrentCollections.newQueue(); ref = ConcurrentCollections.newQueue();
intObjectHashMap.set(ref); intObjectHashMap.set(ref);
} }
if (ref.size() < QUEUE_MAX_SIZE) {
map.clear(); map.clear();
ref.add(map); ref.add(map);
} }
}
// ------ TObjectFloatHashMap ----- // ------ TObjectFloatHashMap -----
@ -573,9 +599,11 @@ public class DefaultCacheRecycler implements Recycler {
ref = ConcurrentCollections.newQueue(); ref = ConcurrentCollections.newQueue();
objectFloatHashMap.set(ref); objectFloatHashMap.set(ref);
} }
if (ref.size() < QUEUE_MAX_SIZE) {
map.clear(); map.clear();
ref.add(map); ref.add(map);
} }
}
// ----- int[] ----- // ----- int[] -----
@ -611,9 +639,11 @@ public class DefaultCacheRecycler implements Recycler {
ref = ConcurrentCollections.newQueue(); ref = ConcurrentCollections.newQueue();
objectArray.set(ref); objectArray.set(ref);
} }
if (ref.size() < QUEUE_MAX_SIZE) {
Arrays.fill(objects, null); Arrays.fill(objects, null);
ref.add(objects); ref.add(objects);
} }
}
private final SoftWrapper<Queue<int[]>> intArray = new SoftWrapper<Queue<int[]>>(); private final SoftWrapper<Queue<int[]>> intArray = new SoftWrapper<Queue<int[]>>();
@ -676,7 +706,9 @@ public class DefaultCacheRecycler implements Recycler {
ref = ConcurrentCollections.newQueue(); ref = ConcurrentCollections.newQueue();
intArray.set(ref); intArray.set(ref);
} }
if (ref.size() < QUEUE_MAX_SIZE) {
Arrays.fill(ints, sentinal); Arrays.fill(ints, sentinal);
ref.add(ints); ref.add(ints);
} }
} }
}

View File

@ -51,6 +51,7 @@ public class NoCacheCacheRecycler implements Recycler {
@Override @Override
public void pushHashMap(ExtTHashMap map) { public void pushHashMap(ExtTHashMap map) {
map.clear();
} }
@Override @Override
@ -61,6 +62,7 @@ public class NoCacheCacheRecycler implements Recycler {
@Override @Override
public void pushHashSet(THashSet map) { public void pushHashSet(THashSet map) {
map.clear();
} }
@Override @Override
@ -71,6 +73,7 @@ public class NoCacheCacheRecycler implements Recycler {
@Override @Override
public void pushDoubleObjectMap(ExtTDoubleObjectHashMap map) { public void pushDoubleObjectMap(ExtTDoubleObjectHashMap map) {
map.clear();
} }
@Override @Override
@ -81,6 +84,7 @@ public class NoCacheCacheRecycler implements Recycler {
@Override @Override
public void pushLongObjectMap(ExtTLongObjectHashMap map) { public void pushLongObjectMap(ExtTLongObjectHashMap map) {
map.clear();
} }
@Override @Override

View File

@ -81,6 +81,9 @@ public class RestClearIndicesCacheAction extends BaseRestHandler {
if (request.hasParam("id_cache")) { if (request.hasParam("id_cache")) {
clearIndicesCacheRequest.idCache(request.paramAsBoolean("id_cache", clearIndicesCacheRequest.idCache())); clearIndicesCacheRequest.idCache(request.paramAsBoolean("id_cache", clearIndicesCacheRequest.idCache()));
} }
if (request.hasParam("recycler")) {
clearIndicesCacheRequest.recycler(request.paramAsBoolean("recycler", clearIndicesCacheRequest.recycler()));
}
clearIndicesCacheRequest.fields(request.paramAsStringArray("fields", clearIndicesCacheRequest.fields())); clearIndicesCacheRequest.fields(request.paramAsStringArray("fields", clearIndicesCacheRequest.fields()));
clearIndicesCacheRequest.filterKeys(request.paramAsStringArray("filter_keys", clearIndicesCacheRequest.filterKeys())); clearIndicesCacheRequest.filterKeys(request.paramAsStringArray("filter_keys", clearIndicesCacheRequest.filterKeys()));

View File

@ -140,7 +140,7 @@ public class DocumentActionsTests extends AbstractNodesTests {
assertThat(indicesExistsResponse.isExists(), equalTo(false)); assertThat(indicesExistsResponse.isExists(), equalTo(false));
logger.info("Clearing cache"); logger.info("Clearing cache");
ClearIndicesCacheResponse clearIndicesCacheResponse = client1.admin().indices().clearCache(clearIndicesCacheRequest("test")).actionGet(); ClearIndicesCacheResponse clearIndicesCacheResponse = client1.admin().indices().clearCache(clearIndicesCacheRequest("test").recycler(true).fieldDataCache(true).filterCache(true).idCache(true)).actionGet();
assertThat(clearIndicesCacheResponse.getSuccessfulShards(), equalTo(10)); assertThat(clearIndicesCacheResponse.getSuccessfulShards(), equalTo(10));
assertThat(clearIndicesCacheResponse.getFailedShards(), equalTo(0)); assertThat(clearIndicesCacheResponse.getFailedShards(), equalTo(0));