Treat empty prefrence as a `not set` in Plain Operation Routing

An empty preference was causing a AIOOB exception in
PlainOperationRouting. We now check for `null` or `empty` instead of
just `null`

Closes #3591
This commit is contained in:
Simon Willnauer 2013-08-28 20:57:39 +02:00
parent 86cb76a0ce
commit 82397c0554
2 changed files with 22 additions and 4 deletions

View File

@ -161,7 +161,7 @@ public class PlainOperationRouting extends AbstractComponent implements Operatio
}
private ShardIterator preferenceActiveShardIterator(IndexShardRoutingTable indexShard, String localNodeId, DiscoveryNodes nodes, @Nullable String preference) {
if (preference == null) {
if (preference == null || preference.isEmpty()) {
String[] awarenessAttributes = awarenessAllocationDecider.awarenessAttributes();
if (awarenessAttributes.length == 0) {
return indexShard.activeInitializingShardsRandomIt();

View File

@ -27,6 +27,8 @@ import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.test.integration.AbstractSharedClusterTest;
import org.junit.Test;
import java.util.concurrent.ExecutionException;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
import static org.elasticsearch.index.query.QueryBuilders.rangeQuery;
@ -51,11 +53,28 @@ public class SimpleSearchTests extends AbstractSharedClusterTest {
}
}
@Test
public void testSearchRandomPreference() throws InterruptedException, ExecutionException {
client().admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", between(1,3))).get();
indexRandom("test", true,
client().prepareIndex("test", "type", "1").setSource("field", "value"),
client().prepareIndex("test", "type", "2").setSource("field", "value"),
client().prepareIndex("test", "type", "3").setSource("field", "value"),
client().prepareIndex("test", "type", "4").setSource("field", "value"),
client().prepareIndex("test", "type", "5").setSource("field", "value"),
client().prepareIndex("test", "type", "6").setSource("field", "value"));
int iters = atLeast(10);
for (int i = 0; i < iters; i++) {
// id is not indexed, but lets see that we automatically convert to
SearchResponse searchResponse = client().prepareSearch().setQuery(QueryBuilders.matchAllQuery()).setPreference(randomUnicodeOfLengthBetween(0, 4)).get();
assertThat(searchResponse.getHits().totalHits(), equalTo(6l));
}
}
@Test
public void simpleIpTests() throws Exception {
client().admin().indices().prepareDelete().execute().actionGet();
client().admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 1)).execute().actionGet();
client().admin().indices().preparePutMapping("test").setType("type1")
@ -76,7 +95,6 @@ public class SimpleSearchTests extends AbstractSharedClusterTest {
@Test
public void simpleIdTests() {
client().admin().indices().prepareDelete().execute().actionGet();
client().admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 1)).execute().actionGet();
client().prepareIndex("test", "type", "XXX1").setSource("field", "value").setRefresh(true).execute().actionGet();