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 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() {
} }
@ -85,6 +87,15 @@ public class ClearIndicesCacheRequest extends BroadcastOperationRequest<ClearInd
public boolean idCache() { public boolean idCache() {
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;
@ -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() {
@ -59,6 +62,10 @@ class ShardClearIndicesCacheRequest extends BroadcastShardOperationRequest {
public boolean idCache() { public boolean idCache() {
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,8 +113,10 @@ public class DefaultCacheRecycler implements Recycler {
ref = ConcurrentCollections.newQueue(); ref = ConcurrentCollections.newQueue();
hashMap.set(ref); hashMap.set(ref);
} }
map.clear(); if (ref.size() < QUEUE_MAX_SIZE) {
ref.add(map); map.clear();
ref.add(map);
}
} }
// ----- THashSet ----- // ----- THashSet -----
@ -148,8 +150,10 @@ public class DefaultCacheRecycler implements Recycler {
ref = ConcurrentCollections.newQueue(); ref = ConcurrentCollections.newQueue();
hashSet.set(ref); hashSet.set(ref);
} }
map.clear(); if (ref.size() < QUEUE_MAX_SIZE) {
ref.add(map); map.clear();
ref.add(map);
}
} }
// ------ ExtTDoubleObjectHashMap ----- // ------ ExtTDoubleObjectHashMap -----
@ -183,8 +187,10 @@ public class DefaultCacheRecycler implements Recycler {
ref = ConcurrentCollections.newQueue(); ref = ConcurrentCollections.newQueue();
doubleObjectHashMap.set(ref); doubleObjectHashMap.set(ref);
} }
map.clear(); if (ref.size() < QUEUE_MAX_SIZE) {
ref.add(map); map.clear();
ref.add(map);
}
} }
// ----- ExtTLongObjectHashMap ---- // ----- ExtTLongObjectHashMap ----
@ -218,8 +224,10 @@ public class DefaultCacheRecycler implements Recycler {
ref = ConcurrentCollections.newQueue(); ref = ConcurrentCollections.newQueue();
longObjectHashMap.set(ref); longObjectHashMap.set(ref);
} }
map.clear(); if (ref.size() < QUEUE_MAX_SIZE) {
ref.add(map); map.clear();
ref.add(map);
}
} }
// ----- TLongLongHashMap ---- // ----- TLongLongHashMap ----
@ -252,8 +260,10 @@ public class DefaultCacheRecycler implements Recycler {
ref = ConcurrentCollections.newQueue(); ref = ConcurrentCollections.newQueue();
longLongHashMap.set(ref); longLongHashMap.set(ref);
} }
map.clear(); if (ref.size() < QUEUE_MAX_SIZE) {
ref.add(map); map.clear();
ref.add(map);
}
} }
// ----- TIntIntHashMap ---- // ----- TIntIntHashMap ----
@ -287,8 +297,10 @@ public class DefaultCacheRecycler implements Recycler {
ref = ConcurrentCollections.newQueue(); ref = ConcurrentCollections.newQueue();
intIntHashMap.set(ref); intIntHashMap.set(ref);
} }
map.clear(); if (ref.size() < QUEUE_MAX_SIZE) {
ref.add(map); map.clear();
ref.add(map);
}
} }
@ -323,8 +335,10 @@ public class DefaultCacheRecycler implements Recycler {
ref = ConcurrentCollections.newQueue(); ref = ConcurrentCollections.newQueue();
floatIntHashMap.set(ref); floatIntHashMap.set(ref);
} }
map.clear(); if (ref.size() < QUEUE_MAX_SIZE) {
ref.add(map); map.clear();
ref.add(map);
}
} }
@ -359,8 +373,10 @@ public class DefaultCacheRecycler implements Recycler {
ref = ConcurrentCollections.newQueue(); ref = ConcurrentCollections.newQueue();
doubleIntHashMap.set(ref); doubleIntHashMap.set(ref);
} }
map.clear(); if (ref.size() < QUEUE_MAX_SIZE) {
ref.add(map); map.clear();
ref.add(map);
}
} }
@ -395,8 +411,10 @@ public class DefaultCacheRecycler implements Recycler {
ref = ConcurrentCollections.newQueue(); ref = ConcurrentCollections.newQueue();
byteIntHashMap.set(ref); byteIntHashMap.set(ref);
} }
map.clear(); if (ref.size() < QUEUE_MAX_SIZE) {
ref.add(map); map.clear();
ref.add(map);
}
} }
// ----- TShortIntHashMap --- // ----- TShortIntHashMap ---
@ -430,8 +448,10 @@ public class DefaultCacheRecycler implements Recycler {
ref = ConcurrentCollections.newQueue(); ref = ConcurrentCollections.newQueue();
shortIntHashMap.set(ref); shortIntHashMap.set(ref);
} }
map.clear(); if (ref.size() < QUEUE_MAX_SIZE) {
ref.add(map); map.clear();
ref.add(map);
}
} }
@ -466,8 +486,10 @@ public class DefaultCacheRecycler implements Recycler {
ref = ConcurrentCollections.newQueue(); ref = ConcurrentCollections.newQueue();
longIntHashMap.set(ref); longIntHashMap.set(ref);
} }
map.clear(); if (ref.size() < QUEUE_MAX_SIZE) {
ref.add(map); map.clear();
ref.add(map);
}
} }
// ------ TObjectIntHashMap ----- // ------ TObjectIntHashMap -----
@ -502,8 +524,10 @@ public class DefaultCacheRecycler implements Recycler {
ref = ConcurrentCollections.newQueue(); ref = ConcurrentCollections.newQueue();
objectIntHashMap.set(ref); objectIntHashMap.set(ref);
} }
map.clear(); if (ref.size() < QUEUE_MAX_SIZE) {
ref.add(map); map.clear();
ref.add(map);
}
} }
// ------ TIntObjectHashMap ----- // ------ TIntObjectHashMap -----
@ -538,8 +562,10 @@ public class DefaultCacheRecycler implements Recycler {
ref = ConcurrentCollections.newQueue(); ref = ConcurrentCollections.newQueue();
intObjectHashMap.set(ref); intObjectHashMap.set(ref);
} }
map.clear(); if (ref.size() < QUEUE_MAX_SIZE) {
ref.add(map); map.clear();
ref.add(map);
}
} }
// ------ TObjectFloatHashMap ----- // ------ TObjectFloatHashMap -----
@ -573,8 +599,10 @@ public class DefaultCacheRecycler implements Recycler {
ref = ConcurrentCollections.newQueue(); ref = ConcurrentCollections.newQueue();
objectFloatHashMap.set(ref); objectFloatHashMap.set(ref);
} }
map.clear(); if (ref.size() < QUEUE_MAX_SIZE) {
ref.add(map); map.clear();
ref.add(map);
}
} }
// ----- int[] ----- // ----- int[] -----
@ -611,8 +639,10 @@ public class DefaultCacheRecycler implements Recycler {
ref = ConcurrentCollections.newQueue(); ref = ConcurrentCollections.newQueue();
objectArray.set(ref); objectArray.set(ref);
} }
Arrays.fill(objects, null); if (ref.size() < QUEUE_MAX_SIZE) {
ref.add(objects); Arrays.fill(objects, null);
ref.add(objects);
}
} }
@ -676,7 +706,9 @@ public class DefaultCacheRecycler implements Recycler {
ref = ConcurrentCollections.newQueue(); ref = ConcurrentCollections.newQueue();
intArray.set(ref); intArray.set(ref);
} }
Arrays.fill(ints, sentinal); if (ref.size() < QUEUE_MAX_SIZE) {
ref.add(ints); Arrays.fill(ints, sentinal);
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));