Skip to content

Commit

Permalink
only return primary if it is active in PlainOperationRounting
Browse files Browse the repository at this point in the history
  • Loading branch information
s1monw committed Apr 16, 2013
1 parent ef5b741 commit d6d4cbd
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
Expand Up @@ -284,6 +284,14 @@ public ShardIterator assignedShardsIt(int index) {
public ShardIterator primaryShardIt() {
return new PlainShardIterator(shardId, primaryAsList);
}

public ShardIterator primaryActiveShardIt() {
if (!primaryAsList.isEmpty() && !primaryAsList.get(0).active()) {
List<ShardRouting> primaryList = ImmutableList.of();
return new PlainShardIterator(shardId, primaryList);
}
return primaryShardIt();
}

public ShardIterator primaryFirstActiveShardsIt() {
ArrayList<ShardRouting> ordered = new ArrayList<ShardRouting>(activeShards.size());
Expand Down
Expand Up @@ -233,7 +233,7 @@ private ShardIterator preferenceActiveShardIterator(IndexShardRoutingTable index
return indexShard.preferNodeActiveShardsIt(localNodeId);
}
if ("_primary".equals(preference)) {
return indexShard.primaryShardIt();
return indexShard.primaryActiveShardIt();
}
if ("_primary_first".equals(preference) || "_primaryFirst".equals(preference)) {
return indexShard.primaryFirstActiveShardsIt();
Expand Down
Expand Up @@ -19,10 +19,14 @@

package org.elasticsearch.test.integration.search.preference;

import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.Priority;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.test.integration.AbstractNodesTests;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
Expand All @@ -32,6 +36,7 @@
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.not;

@Test
Expand All @@ -56,6 +61,30 @@ public void closeNodes() {
protected Client getClient() {
return client("server1");
}

@Test // see #2896
public void testStopOneNodePreferenceWithRedState() throws InterruptedException {
startNode("server3");
client.admin().indices().prepareDelete().execute().actionGet();
client.admin().indices().prepareCreate("test").setSettings(settingsBuilder().put("index.number_of_shards", 10).put("index.number_of_replicas", 0)).execute().actionGet();
client.admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().execute().actionGet();
for (int i = 0; i < 10; i++) {
client.prepareIndex("test", "type1", ""+i).setSource("field1", "value1").execute().actionGet();
}
client.admin().indices().prepareRefresh().execute().actionGet();
closeNode("server3");
client.admin().cluster().prepareHealth().setWaitForStatus(ClusterHealthStatus.RED).execute().actionGet();
String[] preferences = new String[] {"_primary", "_local", "_primary_first", "_only_local", "_prefer_node:somenode", "_prefer_node:server2"};
for (String pref : preferences) {
SearchResponse searchResponse = client.prepareSearch().setSearchType(SearchType.COUNT).setPreference(pref).execute().actionGet();
assertThat(RestStatus.OK, equalTo(searchResponse.status()));
assertThat(pref, searchResponse.getFailedShards(), greaterThanOrEqualTo(0));
searchResponse = client.prepareSearch().setPreference(pref).execute().actionGet();
assertThat(RestStatus.OK, equalTo(searchResponse.status()));
assertThat(pref, searchResponse.getFailedShards(), greaterThanOrEqualTo(0));
}
}


@Test
public void noPreferenceRandom() throws Exception {
Expand Down

0 comments on commit d6d4cbd

Please sign in to comment.