Randomize CacheRecycler instance in TestCluster

This commit is contained in:
Simon Willnauer 2013-12-26 13:39:03 +01:00
parent bc452dff84
commit f52a080eec
2 changed files with 37 additions and 16 deletions

View File

@ -23,12 +23,11 @@ import com.carrotsearch.hppc.*;
import org.elasticsearch.ElasticSearchIllegalArgumentException; import org.elasticsearch.ElasticSearchIllegalArgumentException;
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.recycler.QueueRecycler; import org.elasticsearch.common.recycler.*;
import org.elasticsearch.common.recycler.Recycler;
import org.elasticsearch.common.recycler.SoftThreadLocalRecycler;
import org.elasticsearch.common.recycler.ThreadLocalRecycler;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import java.util.Locale;
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public class CacheRecycler extends AbstractComponent { public class CacheRecycler extends AbstractComponent {
@ -63,7 +62,7 @@ public class CacheRecycler extends AbstractComponent {
@Inject @Inject
public CacheRecycler(Settings settings) { public CacheRecycler(Settings settings) {
super(settings); super(settings);
String type = settings.get("type", "soft_thread_local"); String type = settings.get("type", Type.SOFT_THREAD_LOCAL.name());
int limit = settings.getAsInt("limit", 10); int limit = settings.getAsInt("limit", 10);
int smartSize = settings.getAsInt("smart_size", 1024); int smartSize = settings.getAsInt("smart_size", 1024);
@ -255,19 +254,39 @@ public class CacheRecycler extends AbstractComponent {
private <T> Recycler<T> build(String type, int limit, int smartSize, Recycler.C<T> c) { private <T> Recycler<T> build(String type, int limit, int smartSize, Recycler.C<T> c) {
Recycler<T> recycler; Recycler<T> recycler;
// default to soft_thread_local try {
if (type == null || "soft_thread_local".equals(type)) { // default to soft_thread_local
recycler = new SoftThreadLocalRecycler<T>(c, limit); final Type t = type == null ? Type.SOFT_THREAD_LOCAL : Type.valueOf(type.toUpperCase(Locale.ROOT));
} else if ("thread_local".equals(type)) { switch (t) {
recycler = new ThreadLocalRecycler<T>(c, limit); case SOFT_THREAD_LOCAL:
} else if ("queue".equals(type)) { recycler = new SoftThreadLocalRecycler<T>(c, limit);
recycler = new QueueRecycler<T>(c); break;
} else { case THREAD_LOCAL:
recycler = new ThreadLocalRecycler<T>(c, limit);
break;
case QUEUE:
recycler = new QueueRecycler<T>(c);
break;
case NONE:
recycler = new NoneRecycler<T>(c);
break;
default:
throw new ElasticSearchIllegalArgumentException("no type support [" + type + "] for recycler");
}
if (smartSize > 0) {
recycler = new Recycler.Sizing<T>(recycler, smartSize);
}
} catch (IllegalArgumentException ex) {
throw new ElasticSearchIllegalArgumentException("no type support [" + type + "] for recycler"); throw new ElasticSearchIllegalArgumentException("no type support [" + type + "] for recycler");
} }
if (smartSize > 0) {
recycler = new Recycler.Sizing<T>(recycler, smartSize);
}
return recycler; return recycler;
} }
public static enum Type {
SOFT_THREAD_LOCAL,
THREAD_LOCAL,
QUEUE,
NONE;
}
} }

View File

@ -25,6 +25,7 @@ import com.google.common.collect.Collections2;
import com.google.common.collect.Iterators; import com.google.common.collect.Iterators;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import org.apache.lucene.util.IOUtils; import org.apache.lucene.util.IOUtils;
import org.elasticsearch.cache.recycler.CacheRecycler;
import org.elasticsearch.client.Client; import org.elasticsearch.client.Client;
import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.client.transport.TransportClient;
@ -173,6 +174,7 @@ public final class TestCluster implements Iterable<Client> {
} }
builder.put("path.data", dataPath.toString()); builder.put("path.data", dataPath.toString());
} }
builder.put("type", CacheRecycler.Type.values()[random.nextInt(CacheRecycler.Type.values().length)]);
this.defaultSettings = builder.build(); this.defaultSettings = builder.build();
this.nodeSettingsSource = nodeSettingsSource; this.nodeSettingsSource = nodeSettingsSource;