From f52a080eecad4a4063abc4c4656ef5184a642918 Mon Sep 17 00:00:00 2001 From: Simon Willnauer Date: Thu, 26 Dec 2013 13:39:03 +0100 Subject: [PATCH] Randomize CacheRecycler instance in TestCluster --- .../cache/recycler/CacheRecycler.java | 51 +++++++++++++------ .../org/elasticsearch/test/TestCluster.java | 2 + 2 files changed, 37 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/elasticsearch/cache/recycler/CacheRecycler.java b/src/main/java/org/elasticsearch/cache/recycler/CacheRecycler.java index 074d9ec5799..61518b6924a 100644 --- a/src/main/java/org/elasticsearch/cache/recycler/CacheRecycler.java +++ b/src/main/java/org/elasticsearch/cache/recycler/CacheRecycler.java @@ -23,12 +23,11 @@ import com.carrotsearch.hppc.*; import org.elasticsearch.ElasticSearchIllegalArgumentException; import org.elasticsearch.common.component.AbstractComponent; import org.elasticsearch.common.inject.Inject; -import org.elasticsearch.common.recycler.QueueRecycler; -import org.elasticsearch.common.recycler.Recycler; -import org.elasticsearch.common.recycler.SoftThreadLocalRecycler; -import org.elasticsearch.common.recycler.ThreadLocalRecycler; +import org.elasticsearch.common.recycler.*; import org.elasticsearch.common.settings.Settings; +import java.util.Locale; + @SuppressWarnings("unchecked") public class CacheRecycler extends AbstractComponent { @@ -63,7 +62,7 @@ public class CacheRecycler extends AbstractComponent { @Inject public CacheRecycler(Settings 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 smartSize = settings.getAsInt("smart_size", 1024); @@ -255,19 +254,39 @@ public class CacheRecycler extends AbstractComponent { private Recycler build(String type, int limit, int smartSize, Recycler.C c) { Recycler recycler; - // default to soft_thread_local - if (type == null || "soft_thread_local".equals(type)) { - recycler = new SoftThreadLocalRecycler(c, limit); - } else if ("thread_local".equals(type)) { - recycler = new ThreadLocalRecycler(c, limit); - } else if ("queue".equals(type)) { - recycler = new QueueRecycler(c); - } else { + try { + // default to soft_thread_local + final Type t = type == null ? Type.SOFT_THREAD_LOCAL : Type.valueOf(type.toUpperCase(Locale.ROOT)); + switch (t) { + case SOFT_THREAD_LOCAL: + recycler = new SoftThreadLocalRecycler(c, limit); + break; + case THREAD_LOCAL: + recycler = new ThreadLocalRecycler(c, limit); + break; + case QUEUE: + recycler = new QueueRecycler(c); + break; + case NONE: + recycler = new NoneRecycler(c); + break; + default: + throw new ElasticSearchIllegalArgumentException("no type support [" + type + "] for recycler"); + } + if (smartSize > 0) { + recycler = new Recycler.Sizing(recycler, smartSize); + } + } catch (IllegalArgumentException ex) { throw new ElasticSearchIllegalArgumentException("no type support [" + type + "] for recycler"); } - if (smartSize > 0) { - recycler = new Recycler.Sizing(recycler, smartSize); - } + return recycler; } + + public static enum Type { + SOFT_THREAD_LOCAL, + THREAD_LOCAL, + QUEUE, + NONE; + } } \ No newline at end of file diff --git a/src/test/java/org/elasticsearch/test/TestCluster.java b/src/test/java/org/elasticsearch/test/TestCluster.java index 48ec7f2838d..6509ba74fcb 100644 --- a/src/test/java/org/elasticsearch/test/TestCluster.java +++ b/src/test/java/org/elasticsearch/test/TestCluster.java @@ -25,6 +25,7 @@ import com.google.common.collect.Collections2; import com.google.common.collect.Iterators; import com.google.common.collect.Sets; import org.apache.lucene.util.IOUtils; +import org.elasticsearch.cache.recycler.CacheRecycler; import org.elasticsearch.client.Client; import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.client.transport.TransportClient; @@ -173,6 +174,7 @@ public final class TestCluster implements Iterable { } builder.put("path.data", dataPath.toString()); } + builder.put("type", CacheRecycler.Type.values()[random.nextInt(CacheRecycler.Type.values().length)]); this.defaultSettings = builder.build(); this.nodeSettingsSource = nodeSettingsSource;