Skip to content

Commit

Permalink
Search Preference: Fix for _only_nodes
Browse files Browse the repository at this point in the history
The `_only_nodes` preference was not parsed correctly.
This adds the preference, so it gets parsed as lowercase as
well as tests.

Closes #12460
Closes #12389
  • Loading branch information
Nirmal Chidambaram authored and spinscale committed Jul 28, 2015
1 parent 9d629f7 commit 2fa7404
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 17 deletions.
Expand Up @@ -95,6 +95,8 @@ public static Preference parse(String preference) {
return PREFER_NODE;
case "_only_node":
return ONLY_NODE;
case "_only_nodes":
return ONLY_NODES;
case "_local":
return LOCAL;
case "_primary":
Expand Down
Expand Up @@ -24,20 +24,36 @@
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.Client;
import org.elasticsearch.cluster.routing.operation.plain.Preference;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.test.ElasticsearchIntegrationTest;
import org.junit.Test;

import java.io.IOException;

import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_REPLICAS;
import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_SHARDS;
import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder;
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
import static org.hamcrest.Matchers.*;

public class SearchPreferenceTests extends ElasticsearchIntegrationTest {

@Test
public void testThatAllPreferencesAreParsedToValid(){
//list of all enums and their strings as reference
assertThat(Preference.parse("_shards"),equalTo(Preference.SHARDS));
assertThat(Preference.parse("_prefer_node"),equalTo(Preference.PREFER_NODE));
assertThat(Preference.parse("_local"),equalTo(Preference.LOCAL));
assertThat(Preference.parse("_primary"),equalTo(Preference.PRIMARY));
assertThat(Preference.parse("_primary_first"),equalTo(Preference.PRIMARY_FIRST));
assertThat(Preference.parse("_only_local"),equalTo(Preference.ONLY_LOCAL));
assertThat(Preference.parse("_only_node"),equalTo(Preference.ONLY_NODE));
assertThat(Preference.parse("_only_nodes"), equalTo(Preference.ONLY_NODES));
}

@Test // see #2896
public void testStopOneNodePreferenceWithRedState() throws InterruptedException, IOException {
assertAcked(prepareCreate("test").setSettings(settingsBuilder().put("index.number_of_shards", cluster().numDataNodes()+2).put("index.number_of_replicas", 0)));
Expand All @@ -48,7 +64,7 @@ public void testStopOneNodePreferenceWithRedState() throws InterruptedException,
refresh();
internalCluster().stopRandomDataNode();
client().admin().cluster().prepareHealth().setWaitForStatus(ClusterHealthStatus.RED).execute().actionGet();
String[] preferences = new String[] {"_primary", "_local", "_primary_first", "_prefer_node:somenode", "_prefer_node:server2"};
String[] preferences = new String[] {"_primary","_shards:1","_local", "_primary_first", "_prefer_node:somenode", "_prefer_node:server2","_only_nodes:*"};
for (String pref : preferences) {
SearchResponse searchResponse = client().prepareSearch().setSearchType(SearchType.COUNT).setPreference(pref).execute().actionGet();
assertThat(RestStatus.OK, equalTo(searchResponse.status()));
Expand Down Expand Up @@ -90,25 +106,24 @@ public void noPreferenceRandom() throws Exception {
@Test
public void simplePreferenceTests() throws Exception {
createIndex("test");
ensureGreen();

client().prepareIndex("test", "type1").setSource("field1", "value1").execute().actionGet();
refresh();
ensureGreen("test");

SearchResponse searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setPreference("_local").execute().actionGet();
assertThat(searchResponse.getHits().totalHits(), equalTo(1l));
searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setPreference("_local").execute().actionGet();
assertThat(searchResponse.getHits().totalHits(), equalTo(1l));
client().prepareIndex("test", "type1").setSource("field1", "value1").setRefresh(true).get();

searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setPreference("_primary").execute().actionGet();
assertThat(searchResponse.getHits().totalHits(), equalTo(1l));
searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setPreference("_primary").execute().actionGet();
assertThat(searchResponse.getHits().totalHits(), equalTo(1l));
// built a shard range like 0,1,2,3,4 based on the number of shards
Integer numberOfShards = client().admin().indices().prepareGetIndex().setIndices("test").get().getSettings().get("test").getAsInt(SETTING_NUMBER_OF_SHARDS, 1);
StringBuilder shardsRange = new StringBuilder("_shards:0");
for (int i = 1; i < numberOfShards; i++) {
shardsRange.append(",").append(i);
}

searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setPreference("1234").execute().actionGet();
assertThat(searchResponse.getHits().totalHits(), equalTo(1l));
searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setPreference("1234").execute().actionGet();
assertThat(searchResponse.getHits().totalHits(), equalTo(1l));
String[] preferences = new String[]{"1234", "_primary", "_local", shardsRange.toString(), "_primary_first","_only_nodes:*"};
for (String pref : preferences) {
SearchResponse searchResponse = client().prepareSearch("test").setQuery(matchAllQuery()).setPreference(pref).execute().actionGet();
assertHitCount(searchResponse, 1);
searchResponse = client().prepareSearch("test").setQuery(matchAllQuery()).setPreference(pref).execute().actionGet();
assertHitCount(searchResponse, 1);
}
}

@Test (expected = ElasticsearchIllegalArgumentException.class)
Expand Down

0 comments on commit 2fa7404

Please sign in to comment.