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,8 +34,10 @@ public class ClearIndicesCacheRequest extends BroadcastOperationRequest<ClearInd
private boolean filterCache = false;
private boolean fieldDataCache = false;
private boolean idCache = false;
private boolean recycler = false;
private String[] fields = null;
private String[] filterKeys = null;
ClearIndicesCacheRequest() {
}
@ -85,6 +87,15 @@ public class ClearIndicesCacheRequest extends BroadcastOperationRequest<ClearInd
public boolean 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) {
this.idCache = idCache;
@ -96,6 +107,7 @@ public class ClearIndicesCacheRequest extends BroadcastOperationRequest<ClearInd
filterCache = in.readBoolean();
fieldDataCache = in.readBoolean();
idCache = in.readBoolean();
recycler = in.readBoolean();
fields = in.readStringArray();
filterKeys = in.readStringArray();
}
@ -105,7 +117,10 @@ public class ClearIndicesCacheRequest extends BroadcastOperationRequest<ClearInd
out.writeBoolean(filterCache);
out.writeBoolean(fieldDataCache);
out.writeBoolean(idCache);
out.writeBoolean(recycler);
out.writeStringArrayNullable(fields);
out.writeStringArrayNullable(filterKeys);
}
}

View File

@ -33,6 +33,8 @@ class ShardClearIndicesCacheRequest extends BroadcastShardOperationRequest {
private boolean filterCache = false;
private boolean fieldDataCache = false;
private boolean idCache = false;
private boolean recycler;
private String[] fields = null;
private String[] filterKeys = null;
@ -46,6 +48,7 @@ class ShardClearIndicesCacheRequest extends BroadcastShardOperationRequest {
idCache = request.idCache();
fields = request.fields();
filterKeys = request.filterKeys();
recycler = request.recycler();
}
public boolean filterCache() {
@ -59,6 +62,10 @@ class ShardClearIndicesCacheRequest extends BroadcastShardOperationRequest {
public boolean idCache() {
return this.idCache;
}
public boolean recycler() {
return this.recycler;
}
public String[] fields() {
return this.fields;
@ -79,6 +86,7 @@ class ShardClearIndicesCacheRequest extends BroadcastShardOperationRequest {
filterCache = in.readBoolean();
fieldDataCache = in.readBoolean();
idCache = in.readBoolean();
recycler = in.readBoolean();
fields = in.readStringArray();
filterKeys = in.readStringArray();
}
@ -89,6 +97,7 @@ class ShardClearIndicesCacheRequest extends BroadcastShardOperationRequest {
out.writeBoolean(filterCache);
out.writeBoolean(fieldDataCache);
out.writeBoolean(idCache);
out.writeBoolean(recycler);
out.writeStringArrayNullable(fields);
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.routing.GroupShardsIterator;
import org.elasticsearch.cluster.routing.ShardRouting;
import org.elasticsearch.common.CacheRecycler;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
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()) {
clearedAtLeastOne = true;
service.cache().idCache().clear();

View File

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

View File

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

View File

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

View File

@ -81,6 +81,9 @@ public class RestClearIndicesCacheAction extends BaseRestHandler {
if (request.hasParam("id_cache")) {
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.filterKeys(request.paramAsStringArray("filter_keys", clearIndicesCacheRequest.filterKeys()));

View File

@ -140,7 +140,7 @@ public class DocumentActionsTests extends AbstractNodesTests {
assertThat(indicesExistsResponse.isExists(), equalTo(false));
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.getFailedShards(), equalTo(0));