Skip to content

Commit

Permalink
Internal: streamline use of IndexClosedException when executing opera…
Browse files Browse the repository at this point in the history
…tion on closed indices

Single index operations to use the newly added IndexClosedException introduced with elastic#6475. This way we can also fail faster when we are trying to execute operations on closed indices and their use is not allowed (depending on indices options). Indices blocks are still checked but we can already throw error while resolving indices (MetaData#concreteIndices).

Effectively this change also affects what we return when using one of the following apis: analyze, bulk, index, update, delete, explain, get, multi_get, mlt, term vector, multi_term vector. We now return `{"error":"IndexClosedException[[test] closed]","status":403}` instead of `{"error":"ClusterBlockException[blocked by: [FORBIDDEN/4/index closed];]","status":403}`.

Closes elastic#6988
  • Loading branch information
javanna committed Jul 24, 2014
1 parent dc9e9cb commit 3e30fa2
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 19 deletions.
18 changes: 12 additions & 6 deletions src/main/java/org/elasticsearch/action/support/IndicesOptions.java
Expand Up @@ -42,6 +42,12 @@ public class IndicesOptions {
private static final byte FORBID_ALIASES_TO_MULTIPLE_INDICES = 16;
private static final byte FORBID_CLOSED_INDICES = 32;

private static final byte STRICT_EXPAND_OPEN = 6;
private static final byte LENIENT_EXPAND_OPEN = 7;
private static final byte STRICT_EXPAND_OPEN_CLOSED = 14;
private static final byte STRICT_EXPAND_OPEN_FORBID_CLOSED = 38;
private static final byte STRICT_SINGLE_INDEX_NO_EXPAND_FORBID_CLOSED = 48;

static {
byte max = 1 << 6;
VALUES = new IndicesOptions[max];
Expand Down Expand Up @@ -177,7 +183,7 @@ public static IndicesOptions fromRequest(RestRequest request, IndicesOptions def
* allows that no indices are resolved from wildcard expressions (not returning an error).
*/
public static IndicesOptions strictExpandOpen() {
return VALUES[6];
return VALUES[STRICT_EXPAND_OPEN];
}

/**
Expand All @@ -186,31 +192,31 @@ public static IndicesOptions strictExpandOpen() {
* use of closed indices by throwing an error.
*/
public static IndicesOptions strictExpandOpenAndForbidClosed() {
return VALUES[38];
return VALUES[STRICT_EXPAND_OPEN_FORBID_CLOSED];
}

/**
* @return indices option that requires every specified index to exist, expands wildcards to both open and closed
* indices and allows that no indices are resolved from wildcard expressions (not returning an error).
*/
public static IndicesOptions strictExpand() {
return VALUES[14];
return VALUES[STRICT_EXPAND_OPEN_CLOSED];
}

/**
* @return indices option that requires each specified index or alias to exist, doesn't expand wildcards and
* throws error if any of the aliases resolves to multiple indices
*/
public static IndicesOptions strictSingleIndexNoExpand() {
return VALUES[FORBID_ALIASES_TO_MULTIPLE_INDICES];
public static IndicesOptions strictSingleIndexNoExpandForbidClosed() {
return VALUES[STRICT_SINGLE_INDEX_NO_EXPAND_FORBID_CLOSED];
}

/**
* @return indices options that ignores unavailable indices, expands wildcards only to open indices and
* allows that no indices are resolved from wildcard expressions (not returning an error).
*/
public static IndicesOptions lenientExpandOpen() {
return VALUES[7];
return VALUES[LENIENT_EXPAND_OPEN];
}

private static byte toByte(boolean ignoreUnavailable, boolean allowNoIndices, boolean wildcardExpandToOpen,
Expand Down
Expand Up @@ -705,7 +705,7 @@ public String[] concreteIndices(IndicesOptions indicesOptions, String... aliases
}

public String concreteSingleIndex(String indexOrAlias) throws IndexMissingException, ElasticsearchIllegalArgumentException {
String[] indices = concreteIndices(IndicesOptions.strictSingleIndexNoExpand(), indexOrAlias);
String[] indices = concreteIndices(IndicesOptions.strictSingleIndexNoExpandForbidClosed(), indexOrAlias);
assert indices.length == 1 : "expected an exception to be thrown otherwise";
return indices[0];
}
Expand Down
Expand Up @@ -24,6 +24,7 @@
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.indices.IndexClosedException;
import org.elasticsearch.indices.IndexMissingException;
import org.elasticsearch.test.ElasticsearchTestCase;
import org.junit.Test;
Expand Down Expand Up @@ -393,34 +394,41 @@ public void testIndexOptions_singleIndexNoExpandWildcards() {
//error on both unavailable and no indices + every alias needs to expand to a single index

try {
md.concreteIndices(IndicesOptions.strictSingleIndexNoExpand(), "baz*");
md.concreteIndices(IndicesOptions.strictSingleIndexNoExpandForbidClosed(), "baz*");
fail();
} catch (IndexMissingException e) {
assertThat(e.index().name(), equalTo("baz*"));
}

try {
md.concreteIndices(IndicesOptions.strictSingleIndexNoExpand(), "foo", "baz*");
md.concreteIndices(IndicesOptions.strictSingleIndexNoExpandForbidClosed(), "foo", "baz*");
fail();
} catch (IndexMissingException e) {
assertThat(e.index().name(), equalTo("baz*"));
}

try {
md.concreteIndices(IndicesOptions.strictSingleIndexNoExpand(), "foofoobar");
md.concreteIndices(IndicesOptions.strictSingleIndexNoExpandForbidClosed(), "foofoobar");
fail();
} catch(ElasticsearchIllegalArgumentException e) {
assertThat(e.getMessage(), containsString("Alias [foofoobar] has more than one indices associated with it"));
}

try {
md.concreteIndices(IndicesOptions.strictSingleIndexNoExpand(), "foo", "foofoobar");
md.concreteIndices(IndicesOptions.strictSingleIndexNoExpandForbidClosed(), "foo", "foofoobar");
fail();
} catch(ElasticsearchIllegalArgumentException e) {
assertThat(e.getMessage(), containsString("Alias [foofoobar] has more than one indices associated with it"));
}

String[] results = md.concreteIndices(IndicesOptions.strictSingleIndexNoExpand(), "foo", "barbaz");
try {
md.concreteIndices(IndicesOptions.strictSingleIndexNoExpandForbidClosed(), "foofoo-closed", "foofoobar");
fail();
} catch(IndexClosedException e) {
assertThat(e.getMessage(), containsString("[foofoo-closed] closed"));
}

String[] results = md.concreteIndices(IndicesOptions.strictSingleIndexNoExpandForbidClosed(), "foo", "barbaz");
assertEquals(2, results.length);
assertThat(results, arrayContainingInAnyOrder("foo", "foofoo"));
}
Expand Down
Expand Up @@ -21,10 +21,8 @@

import org.apache.lucene.util.LuceneTestCase.Slow;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus;
import org.elasticsearch.action.admin.cluster.state.ClusterStateResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.cluster.block.ClusterBlockException;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.cluster.metadata.MappingMetaData;
import org.elasticsearch.cluster.routing.ShardRoutingState;
Expand All @@ -35,11 +33,11 @@
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.gateway.Gateway;
import org.elasticsearch.indices.IndexClosedException;
import org.elasticsearch.test.ElasticsearchIntegrationTest;
import org.elasticsearch.test.ElasticsearchIntegrationTest.ClusterScope;
import org.elasticsearch.test.InternalTestCluster;
import org.elasticsearch.test.InternalTestCluster.RestartCallback;
import org.elasticsearch.test.hamcrest.ElasticsearchAssertions;
import org.junit.Test;

import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder;
Expand Down Expand Up @@ -123,7 +121,7 @@ public void testSimpleOpenClose() throws Exception {
try {
client().prepareIndex("test", "type1", "1").setSource("field1", "value1").setTimeout("1s").execute().actionGet();
fail();
} catch (ClusterBlockException e) {
} catch (IndexClosedException e) {
// all is well
}

Expand Down Expand Up @@ -166,7 +164,7 @@ public void testSimpleOpenClose() throws Exception {
try {
client().prepareIndex("test", "type1", "1").setSource("field1", "value1").setTimeout("1s").execute().actionGet();
fail();
} catch (ClusterBlockException e) {
} catch (IndexClosedException e) {
// all is well
}

Expand Down
Expand Up @@ -25,13 +25,13 @@
import org.elasticsearch.action.admin.indices.close.CloseIndexResponse;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.open.OpenIndexResponse;
import org.elasticsearch.cluster.block.ClusterBlockException;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.cluster.routing.ShardRoutingState;
import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.SettingsException;
import org.elasticsearch.indices.IndexClosedException;
import org.elasticsearch.indices.IndexMissingException;
import org.elasticsearch.indices.IndexPrimaryShardNotAllocatedException;
import org.elasticsearch.test.ElasticsearchIntegrationTest;
Expand Down Expand Up @@ -78,7 +78,7 @@ public void testSimpleOpenClose() {
try {
client().prepareIndex("test", "type1", "1").setSource("field1", "value1").get();
fail();
} catch (ClusterBlockException e) {
} catch (IndexClosedException e) {
// all is well
}

Expand Down

0 comments on commit 3e30fa2

Please sign in to comment.