If no specified index or alias exists and `ignore_indices` is set to `missing` an index missing error is returned instead of resolving to all open indices (e.g. when searching). This breaks backwards comp. with 0.20.x and before.

Closes #2837
This commit is contained in:
Martijn van Groningen 2013-04-02 19:06:17 +02:00
parent bdb2241892
commit cf00acf5b0
6 changed files with 108 additions and 9 deletions

View File

@ -222,7 +222,13 @@ public class TransportClusterHealthAction extends TransportMasterNodeOperationAc
response.numberOfNodes = clusterState.nodes().size();
response.numberOfDataNodes = clusterState.nodes().dataNodes().size();
for (String index : clusterState.metaData().concreteIndicesIgnoreMissing(request.indices())) {
String[] concreteIndices;
try {
concreteIndices = clusterState.metaData().concreteIndicesIgnoreMissing(request.indices());
} catch (IndexMissingException e) {
return response;
}
for (String index : concreteIndices) {
IndexRoutingTable indexRoutingTable = clusterState.routingTable().index(index);
IndexMetaData indexMetaData = clusterState.metaData().index(index);
if (indexRoutingTable == null) {

View File

@ -504,9 +504,6 @@ public class MetaData implements Iterable<IndexMetaData> {
}
String[] actualLst = aliasAndIndexToIndexMap.get(aliasOrIndex);
if (actualLst == null) {
if (ignoreIndices == IgnoreIndices.MISSING) {
return Strings.EMPTY_ARRAY;
}
throw new IndexMissingException(new Index(aliasOrIndex));
} else {
return actualLst;
@ -539,6 +536,10 @@ public class MetaData implements Iterable<IndexMetaData> {
}
}
}
if (actualIndices.isEmpty()) {
throw new IndexMissingException(new Index(Arrays.toString(aliasesOrIndices)));
}
return actualIndices.toArray(new String[actualIndices.size()]);
}

View File

@ -21,6 +21,7 @@ package org.elasticsearch.test.integration.cluster;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.node.Node;
import org.elasticsearch.test.integration.AbstractNodesTests;
import org.testng.annotations.AfterMethod;
@ -40,8 +41,33 @@ public class ClusterHealthTests extends AbstractNodesTests {
public void testHealth() {
Node node1 = startNode("node1");
logger.info("--> running cluster health on an index that does not exists");
ClusterHealthResponse healthResponse = node1.client().admin().cluster().prepareHealth("test").setWaitForYellowStatus().setTimeout("1s").execute().actionGet();
ClusterHealthResponse healthResponse = node1.client().admin().cluster().prepareHealth("test1").setWaitForYellowStatus().setTimeout("1s").execute().actionGet();
assertThat(healthResponse.isTimedOut(), equalTo(true));
assertThat(healthResponse.getStatus(), equalTo(ClusterHealthStatus.RED));
assertThat(healthResponse.getIndices().isEmpty(), equalTo(true));
logger.info("--> running cluster wide health");
healthResponse = node1.client().admin().cluster().prepareHealth().setWaitForYellowStatus().setTimeout("1s").execute().actionGet();
assertThat(healthResponse.isTimedOut(), equalTo(false));
assertThat(healthResponse.getStatus(), equalTo(ClusterHealthStatus.GREEN));
assertThat(healthResponse.getIndices().isEmpty(), equalTo(true));
logger.info("--> Creating index test1 with zero replicas");
node1.client().admin().indices().prepareCreate("test1")
.setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_replicas", 0))
.execute().actionGet();
logger.info("--> running cluster health on an index that does exists");
healthResponse = node1.client().admin().cluster().prepareHealth("test1").setWaitForYellowStatus().setTimeout("1s").execute().actionGet();
assertThat(healthResponse.isTimedOut(), equalTo(false));
assertThat(healthResponse.getStatus(), equalTo(ClusterHealthStatus.GREEN));
assertThat(healthResponse.getIndices().get("test1").getStatus(), equalTo(ClusterHealthStatus.GREEN));
logger.info("--> running cluster health on an index that does exists and an index that doesn't exists");
healthResponse = node1.client().admin().cluster().prepareHealth("test1", "test2").setWaitForYellowStatus().setTimeout("1s").execute().actionGet();
assertThat(healthResponse.isTimedOut(), equalTo(true));
assertThat(healthResponse.getStatus(), equalTo(ClusterHealthStatus.RED));
assertThat(healthResponse.getIndices().get("test1").getStatus(), equalTo(ClusterHealthStatus.GREEN));
assertThat(healthResponse.getIndices().size(), equalTo(1));
}
}

View File

@ -170,6 +170,28 @@ public class IgnoreIndicesTests extends AbstractNodesTests {
client.admin().indices().prepareValidateQuery("test1", "test2").execute().actionGet();
}
@Test
public void testAllMissing() throws Exception {
client.admin().indices().prepareDelete().execute().actionGet();
client.admin().indices().prepareCreate("test1").execute().actionGet();
ClusterHealthResponse clusterHealthResponse = client.admin().cluster().prepareHealth().setWaitForYellowStatus().execute().actionGet();
assertThat(clusterHealthResponse.isTimedOut(), equalTo(false));
try {
client.prepareSearch("test2").setQuery(QueryBuilders.matchAllQuery()).setIgnoreIndices(IgnoreIndices.MISSING).execute().actionGet();
fail("Exception should have been thrown.");
} catch (IndexMissingException e) {
}
try {
client.prepareSearch("test2","test3").setQuery(QueryBuilders.matchAllQuery()).setIgnoreIndices(IgnoreIndices.MISSING).execute().actionGet();
fail("Exception should have been thrown.");
} catch (IndexMissingException e) {
}
//you should still be able to run empty searches without things blowing up
client.prepareSearch().setQuery(QueryBuilders.matchAllQuery()).setIgnoreIndices(IgnoreIndices.MISSING).execute().actionGet();
}
@Test
// For now don't handle closed indices
public void testClosed() throws Exception {

View File

@ -23,12 +23,14 @@ import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
import org.elasticsearch.action.admin.indices.exists.types.TypesExistsResponse;
import org.elasticsearch.action.support.IgnoreIndices;
import org.elasticsearch.client.Client;
import org.elasticsearch.indices.IndexMissingException;
import org.elasticsearch.test.integration.AbstractNodesTests;
import org.testng.annotations.Test;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.testng.Assert.fail;
public class TypesExistsTests extends AbstractNodesTests {
@ -54,10 +56,14 @@ public class TypesExistsTests extends AbstractNodesTests {
assertThat(response.isExists(), equalTo(true));
response = client.admin().indices().prepareTypesExists("test1").setTypes("type3").execute().actionGet();
assertThat(response.isExists(), equalTo(false));
response = client.admin().indices().prepareTypesExists("notExist").setTypes("type1").setIgnoreIndices(IgnoreIndices.MISSING).execute().actionGet();
assertThat(response.isExists(), equalTo(false));
response = client.admin().indices().prepareTypesExists("notExist").setTypes("type0").setIgnoreIndices(IgnoreIndices.MISSING).execute().actionGet();
assertThat(response.isExists(), equalTo(false));
try {
client.admin().indices().prepareTypesExists("notExist").setTypes("type1").setIgnoreIndices(IgnoreIndices.MISSING).execute().actionGet();
fail("Exception should have been thrown");
} catch (IndexMissingException e) {}
try {
client.admin().indices().prepareTypesExists("notExist").setTypes("type0").setIgnoreIndices(IgnoreIndices.MISSING).execute().actionGet();
fail("Exception should have been thrown");
} catch (IndexMissingException e) {}
response = client.admin().indices().prepareTypesExists("alias1").setTypes("type1").execute().actionGet();
assertThat(response.isExists(), equalTo(true));
response = client.admin().indices().prepareTypesExists("*").setTypes("type1").execute().actionGet();

View File

@ -19,11 +19,13 @@
package org.elasticsearch.test.unit.cluster.metadata;
import com.google.common.collect.Sets;
import org.elasticsearch.action.support.IgnoreIndices;
import org.elasticsearch.cluster.metadata.AliasMetaData;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.indices.IndexMissingException;
import org.testng.annotations.Test;
import static com.google.common.collect.Sets.newHashSet;
@ -66,4 +68,40 @@ public class MetaDataTests {
private IndexMetaData.Builder indexBuilder(String index) {
return IndexMetaData.builder(index).settings(ImmutableSettings.settingsBuilder().put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0));
}
@Test(expectedExceptions = IndexMissingException.class)
public void concreteIndicesIgnoreIndicesOneMissingIndex() {
MetaData.Builder mdBuilder = MetaData.builder()
.put(indexBuilder("testXXX"))
.put(indexBuilder("kuku"));
MetaData md = mdBuilder.build();
md.concreteIndices(new String[]{"testZZZ"}, IgnoreIndices.MISSING, true);
}
@Test
public void concreteIndicesIgnoreIndicesOneMissingIndexOtherFound() {
MetaData.Builder mdBuilder = MetaData.builder()
.put(indexBuilder("testXXX"))
.put(indexBuilder("kuku"));
MetaData md = mdBuilder.build();
assertThat(newHashSet(md.concreteIndices(new String[]{"testXXX","testZZZ"}, IgnoreIndices.MISSING, true)), equalTo(newHashSet("testXXX")));
}
@Test(expectedExceptions = IndexMissingException.class)
public void concreteIndicesIgnoreIndicesAllMissing() {
MetaData.Builder mdBuilder = MetaData.builder()
.put(indexBuilder("testXXX"))
.put(indexBuilder("kuku"));
MetaData md = mdBuilder.build();
assertThat(newHashSet(md.concreteIndices(new String[]{"testMo","testMahdy"}, IgnoreIndices.MISSING, true)), equalTo(newHashSet("testXXX")));
}
@Test
public void concreteIndicesIgnoreIndicesEmptyRequest() {
MetaData.Builder mdBuilder = MetaData.builder()
.put(indexBuilder("testXXX"))
.put(indexBuilder("kuku"));
MetaData md = mdBuilder.build();
assertThat(newHashSet(md.concreteIndices(new String[]{}, IgnoreIndices.MISSING, true)), equalTo(Sets.<String>newHashSet("kuku","testXXX")));
}
}