From 85bea22bc8f089e83d21b78a9ddc3ea3a78204d0 Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Thu, 12 Jun 2014 11:05:33 +0200 Subject: [PATCH] Core: The ignore_unavailable=true setting also ignores indices that are closed. Closes #6471 Closes #6475 --- .../percolate/MultiPercolateRequest.java | 17 ++++--- .../action/search/MultiSearchRequest.java | 15 +++--- .../search/MultiSearchRequestBuilder.java | 4 +- .../action/search/SearchRequest.java | 2 +- .../action/support/IndicesOptions.java | 51 +++++++++++++++---- .../broadcast/BroadcastOperationRequest.java | 2 +- .../cluster/metadata/MetaData.java | 45 +++++++++++++--- .../indices/IndexClosedException.java | 39 ++++++++++++++ .../MultiPercolatorRequestTests.java | 12 ++--- .../search/MultiSearchRequestTests.java | 4 +- .../action/support/IndicesOptionsTests.java | 17 +++++-- .../IndicesOptionsIntegrationTests.java | 21 +++----- 12 files changed, 168 insertions(+), 61 deletions(-) create mode 100644 src/main/java/org/elasticsearch/indices/IndexClosedException.java diff --git a/src/main/java/org/elasticsearch/action/percolate/MultiPercolateRequest.java b/src/main/java/org/elasticsearch/action/percolate/MultiPercolateRequest.java index 1bb25a6bead..8b4371b1572 100644 --- a/src/main/java/org/elasticsearch/action/percolate/MultiPercolateRequest.java +++ b/src/main/java/org/elasticsearch/action/percolate/MultiPercolateRequest.java @@ -48,7 +48,7 @@ public class MultiPercolateRequest extends ActionRequest private String[] indices; private String documentType; - private IndicesOptions indicesOptions = IndicesOptions.strictExpandOpen(); + private IndicesOptions indicesOptions = IndicesOptions.strictExpandOpenAndForbidClosed(); private List requests = Lists.newArrayList(); public MultiPercolateRequest add(PercolateRequestBuilder requestBuilder) { @@ -62,7 +62,7 @@ public class MultiPercolateRequest extends ActionRequest if (request.documentType() == null && documentType != null) { request.documentType(documentType); } - if (request.indicesOptions() == IndicesOptions.strictExpandOpen() && indicesOptions != IndicesOptions.strictExpandOpen()) { + if (request.indicesOptions() == IndicesOptions.strictExpandOpenAndForbidClosed() && indicesOptions != IndicesOptions.strictExpandOpenAndForbidClosed()) { request.indicesOptions(indicesOptions); } requests.add(request); @@ -96,7 +96,7 @@ public class MultiPercolateRequest extends ActionRequest if (documentType != null) { percolateRequest.documentType(documentType); } - if (indicesOptions != IndicesOptions.strictExpandOpen()) { + if (indicesOptions != IndicesOptions.strictExpandOpenAndForbidClosed()) { percolateRequest.indicesOptions(indicesOptions); } @@ -165,10 +165,11 @@ public class MultiPercolateRequest extends ActionRequest } } - boolean ignoreUnavailable = IndicesOptions.strictExpandOpen().ignoreUnavailable(); - boolean allowNoIndices = IndicesOptions.strictExpandOpen().allowNoIndices(); - boolean expandWildcardsOpen = IndicesOptions.strictExpandOpen().expandWildcardsOpen(); - boolean expandWildcardsClosed = IndicesOptions.strictExpandOpen().expandWildcardsClosed(); + IndicesOptions defaultOptions = indicesOptions; + boolean ignoreUnavailable = defaultOptions.ignoreUnavailable(); + boolean allowNoIndices = defaultOptions.allowNoIndices(); + boolean expandWildcardsOpen = defaultOptions.expandWildcardsOpen(); + boolean expandWildcardsClosed = defaultOptions.expandWildcardsClosed(); if (header.containsKey("id")) { GetRequest getRequest = new GetRequest(globalIndex); @@ -280,7 +281,7 @@ public class MultiPercolateRequest extends ActionRequest } } } - percolateRequest.indicesOptions(IndicesOptions.fromOptions(ignoreUnavailable, allowNoIndices, expandWildcardsOpen, expandWildcardsClosed)); + percolateRequest.indicesOptions(IndicesOptions.fromOptions(ignoreUnavailable, allowNoIndices, expandWildcardsOpen, expandWildcardsClosed, defaultOptions)); } private String[] parseArray(XContentParser parser) throws IOException { diff --git a/src/main/java/org/elasticsearch/action/search/MultiSearchRequest.java b/src/main/java/org/elasticsearch/action/search/MultiSearchRequest.java index c283454e412..0574a0b0b8b 100644 --- a/src/main/java/org/elasticsearch/action/search/MultiSearchRequest.java +++ b/src/main/java/org/elasticsearch/action/search/MultiSearchRequest.java @@ -48,7 +48,7 @@ public class MultiSearchRequest extends ActionRequest { private List requests = Lists.newArrayList(); - private IndicesOptions indicesOptions = IndicesOptions.strictExpandOpen(); + private IndicesOptions indicesOptions = IndicesOptions.strictExpandOpenAndForbidClosed(); /** * Add a search request to execute. Note, the order is important, the search response will be returned in the @@ -70,7 +70,7 @@ public class MultiSearchRequest extends ActionRequest { public MultiSearchRequest add(byte[] data, int from, int length, boolean contentUnsafe, @Nullable String[] indices, @Nullable String[] types, @Nullable String searchType) throws Exception { - return add(new BytesArray(data, from, length), contentUnsafe, indices, types, searchType, null, IndicesOptions.strictExpandOpen(), true); + return add(new BytesArray(data, from, length), contentUnsafe, indices, types, searchType, null, IndicesOptions.strictExpandOpenAndForbidClosed(), true); } public MultiSearchRequest add(BytesReference data, boolean contentUnsafe, @Nullable String[] indices, @Nullable String[] types, @Nullable String searchType, IndicesOptions indicesOptions) throws Exception { @@ -108,10 +108,11 @@ public class MultiSearchRequest extends ActionRequest { } searchRequest.searchType(searchType); - boolean ignoreUnavailable = IndicesOptions.strictExpandOpen().ignoreUnavailable(); - boolean allowNoIndices = IndicesOptions.strictExpandOpen().allowNoIndices(); - boolean expandWildcardsOpen = IndicesOptions.strictExpandOpen().expandWildcardsOpen(); - boolean expandWildcardsClosed = IndicesOptions.strictExpandOpen().expandWildcardsClosed(); + IndicesOptions defaultOptions = IndicesOptions.strictExpandOpenAndForbidClosed(); + boolean ignoreUnavailable = defaultOptions.ignoreUnavailable(); + boolean allowNoIndices = defaultOptions.allowNoIndices(); + boolean expandWildcardsOpen = defaultOptions.expandWildcardsOpen(); + boolean expandWildcardsClosed = defaultOptions.expandWildcardsClosed(); // now parse the action if (nextMarker - from > 0) { @@ -181,7 +182,7 @@ public class MultiSearchRequest extends ActionRequest { } } } - searchRequest.indicesOptions(IndicesOptions.fromOptions(ignoreUnavailable, allowNoIndices, expandWildcardsOpen, expandWildcardsClosed)); + searchRequest.indicesOptions(IndicesOptions.fromOptions(ignoreUnavailable, allowNoIndices, expandWildcardsOpen, expandWildcardsClosed, defaultOptions)); // move pointers from = nextMarker + 1; diff --git a/src/main/java/org/elasticsearch/action/search/MultiSearchRequestBuilder.java b/src/main/java/org/elasticsearch/action/search/MultiSearchRequestBuilder.java index a1d6f3c7fb1..5446ef943ad 100644 --- a/src/main/java/org/elasticsearch/action/search/MultiSearchRequestBuilder.java +++ b/src/main/java/org/elasticsearch/action/search/MultiSearchRequestBuilder.java @@ -41,7 +41,7 @@ public class MultiSearchRequestBuilder extends ActionRequestBuilder { private String[] types = Strings.EMPTY_ARRAY; - private IndicesOptions indicesOptions = IndicesOptions.strictExpandOpen(); + private IndicesOptions indicesOptions = IndicesOptions.strictExpandOpenAndForbidClosed(); public SearchRequest() { } diff --git a/src/main/java/org/elasticsearch/action/support/IndicesOptions.java b/src/main/java/org/elasticsearch/action/support/IndicesOptions.java index 112614602cb..e7a0c8b842f 100644 --- a/src/main/java/org/elasticsearch/action/support/IndicesOptions.java +++ b/src/main/java/org/elasticsearch/action/support/IndicesOptions.java @@ -40,9 +40,10 @@ public class IndicesOptions { private static final byte EXPAND_WILDCARDS_OPEN = 4; private static final byte EXPAND_WILDCARDS_CLOSED = 8; private static final byte FORBID_ALIASES_TO_MULTIPLE_INDICES = 16; + private static final byte FORBID_CLOSED_INDICES = 32; static { - byte max = 1 << 5; + byte max = 1 << 6; VALUES = new IndicesOptions[max]; for (byte id = 0; id < max; id++) { VALUES[id] = new IndicesOptions(id); @@ -84,6 +85,13 @@ public class IndicesOptions { return (id & EXPAND_WILDCARDS_CLOSED) != 0; } + /** + * @return Whether execution on closed indices is allowed. + */ + public boolean forbidClosedIndices() { + return (id & FORBID_CLOSED_INDICES) != 0; + } + /** * @return whether aliases pointing to multiple indices are allowed */ @@ -94,12 +102,16 @@ public class IndicesOptions { } public void writeIndicesOptions(StreamOutput out) throws IOException { - if (allowAliasesToMultipleIndices() || out.getVersion().onOrAfter(Version.V_1_2_0)) { + if (out.getVersion().onOrAfter(Version.V_1_2_2)) { out.write(id); - } else { - //if we are talking to a node that doesn't support the newly added flag (allowAliasesToMultipleIndices) - //flip to 0 all the bits starting from the 5th + } else if (out.getVersion().before(Version.V_1_2_0)) { + // Target node doesn't know about the FORBID_CLOSED_INDICES and FORBID_ALIASES_TO_MULTIPLE_INDICES flags, + // so unset the bits starting from the 5th position. out.write(id & 0xf); + } else { + // Target node doesn't know about the FORBID_CLOSED_INDICES flag, + // so unset the bits starting from the 6th position. + out.write(id & 0x1f); } } @@ -114,11 +126,15 @@ public class IndicesOptions { } public static IndicesOptions fromOptions(boolean ignoreUnavailable, boolean allowNoIndices, boolean expandToOpenIndices, boolean expandToClosedIndices) { - return fromOptions(ignoreUnavailable, allowNoIndices, expandToOpenIndices, expandToClosedIndices, true); + return fromOptions(ignoreUnavailable, allowNoIndices, expandToOpenIndices, expandToClosedIndices, true, false); } - static IndicesOptions fromOptions(boolean ignoreUnavailable, boolean allowNoIndices, boolean expandToOpenIndices, boolean expandToClosedIndices, boolean allowAliasesToMultipleIndices) { - byte id = toByte(ignoreUnavailable, allowNoIndices, expandToOpenIndices, expandToClosedIndices, allowAliasesToMultipleIndices); + public static IndicesOptions fromOptions(boolean ignoreUnavailable, boolean allowNoIndices, boolean expandToOpenIndices, boolean expandToClosedIndices, IndicesOptions defaultOptions) { + return fromOptions(ignoreUnavailable, allowNoIndices, expandToOpenIndices, expandToClosedIndices, defaultOptions.allowAliasesToMultipleIndices(), defaultOptions.forbidClosedIndices()); + } + + static IndicesOptions fromOptions(boolean ignoreUnavailable, boolean allowNoIndices, boolean expandToOpenIndices, boolean expandToClosedIndices, boolean allowAliasesToMultipleIndices, boolean forbidClosedIndices) { + byte id = toByte(ignoreUnavailable, allowNoIndices, expandToOpenIndices, expandToClosedIndices, allowAliasesToMultipleIndices, forbidClosedIndices); return VALUES[id]; } @@ -150,7 +166,9 @@ public class IndicesOptions { toBool(sIgnoreUnavailable, defaultSettings.ignoreUnavailable()), toBool(sAllowNoIndices, defaultSettings.allowNoIndices()), expandWildcardsOpen, - expandWildcardsClosed + expandWildcardsClosed, + defaultSettings.allowAliasesToMultipleIndices(), + defaultSettings.forbidClosedIndices() ); } @@ -162,6 +180,15 @@ public class IndicesOptions { return VALUES[6]; } + /** + * @return indices options that requires every specified index to exist, expands wildcards only to open indices, + * allows that no indices are resolved from wildcard expressions (not returning an error) and forbids the + * use of closed indices by throwing an error. + */ + public static IndicesOptions strictExpandOpenAndForbidClosed() { + return VALUES[38]; + } + /** * @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). @@ -186,7 +213,8 @@ public class IndicesOptions { return VALUES[7]; } - private static byte toByte(boolean ignoreUnavailable, boolean allowNoIndices, boolean wildcardExpandToOpen, boolean wildcardExpandToClosed, boolean allowAliasesToMultipleIndices) { + private static byte toByte(boolean ignoreUnavailable, boolean allowNoIndices, boolean wildcardExpandToOpen, + boolean wildcardExpandToClosed, boolean allowAliasesToMultipleIndices, boolean forbidClosedIndices) { byte id = 0; if (ignoreUnavailable) { id |= IGNORE_UNAVAILABLE; @@ -205,6 +233,9 @@ public class IndicesOptions { if (!allowAliasesToMultipleIndices) { id |= FORBID_ALIASES_TO_MULTIPLE_INDICES; } + if (forbidClosedIndices) { + id |= FORBID_CLOSED_INDICES; + } return id; } diff --git a/src/main/java/org/elasticsearch/action/support/broadcast/BroadcastOperationRequest.java b/src/main/java/org/elasticsearch/action/support/broadcast/BroadcastOperationRequest.java index 64fdcd0405c..36aa05f9531 100644 --- a/src/main/java/org/elasticsearch/action/support/broadcast/BroadcastOperationRequest.java +++ b/src/main/java/org/elasticsearch/action/support/broadcast/BroadcastOperationRequest.java @@ -34,7 +34,7 @@ import java.io.IOException; public abstract class BroadcastOperationRequest extends ActionRequest { protected String[] indices; - private IndicesOptions indicesOptions = IndicesOptions.strictExpandOpen(); + private IndicesOptions indicesOptions = IndicesOptions.strictExpandOpenAndForbidClosed(); protected BroadcastOperationRequest() { diff --git a/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java b/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java index 5d214f53743..cda247ed1f2 100644 --- a/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java +++ b/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java @@ -41,6 +41,7 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.loader.SettingsLoader; import org.elasticsearch.common.xcontent.*; import org.elasticsearch.index.Index; +import org.elasticsearch.indices.IndexClosedException; import org.elasticsearch.indices.IndexMissingException; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.search.warmer.IndexWarmersMetaData; @@ -623,8 +624,10 @@ public class MetaData implements Iterable { return null; } + /** * Translates the provided indices or aliases, eventually containing wildcard expressions, into actual indices. + * * @param indicesOptions how the aliases or indices need to be resolved to concrete indices * @param aliasesOrIndices the aliases or indices to be resolved to concrete indices * @return the obtained concrete indices @@ -635,7 +638,6 @@ public class MetaData implements Iterable { * indices options don't allow such a case. */ public String[] concreteIndices(IndicesOptions indicesOptions, String... aliasesOrIndices) throws IndexMissingException, ElasticsearchIllegalArgumentException { - if (indicesOptions.expandWildcardsOpen() || indicesOptions.expandWildcardsClosed()) { if (isAllIndices(aliasesOrIndices)) { String[] concreteIndices; @@ -655,27 +657,44 @@ public class MetaData implements Iterable { aliasesOrIndices = convertFromWildcards(aliasesOrIndices, indicesOptions); } + boolean failClosed = indicesOptions.forbidClosedIndices() && !indicesOptions.ignoreUnavailable(); // optimize for single element index (common case) if (aliasesOrIndices.length == 1) { - return concreteIndices(aliasesOrIndices[0], indicesOptions.allowNoIndices(), indicesOptions.allowAliasesToMultipleIndices()); + return concreteIndices(aliasesOrIndices[0], indicesOptions.allowNoIndices(), failClosed, indicesOptions.allowAliasesToMultipleIndices()); } // check if its a possible aliased index, if not, just return the passed array boolean possiblyAliased = false; + boolean closedIndices = false; for (String index : aliasesOrIndices) { - if (!this.indices.containsKey(index)) { + IndexMetaData indexMetaData = indices.get(index); + if (indexMetaData == null) { possiblyAliased = true; break; + } else { + if (indicesOptions.forbidClosedIndices() && indexMetaData.getState() == IndexMetaData.State.CLOSE) { + if (failClosed) { + throw new IndexClosedException(new Index(index)); + } else { + closedIndices = true; + } + } } } if (!possiblyAliased) { - return aliasesOrIndices; + if (closedIndices) { + Set actualIndices = new HashSet<>(Arrays.asList(aliasesOrIndices)); + actualIndices.retainAll(new HashSet(Arrays.asList(allOpenIndices))); + return actualIndices.toArray(new String[actualIndices.size()]); + } else { + return aliasesOrIndices; + } } Set actualIndices = new HashSet<>(); for (String aliasOrIndex : aliasesOrIndices) { - String[] indices = concreteIndices(aliasOrIndex, indicesOptions.ignoreUnavailable(), indicesOptions.allowAliasesToMultipleIndices()); + String[] indices = concreteIndices(aliasOrIndex, indicesOptions.ignoreUnavailable(), failClosed, indicesOptions.allowAliasesToMultipleIndices()); Collections.addAll(actualIndices, indices); } @@ -691,10 +710,15 @@ public class MetaData implements Iterable { return indices[0]; } - private String[] concreteIndices(String aliasOrIndex, boolean allowNoIndices, boolean allowMultipleIndices) throws IndexMissingException, ElasticsearchIllegalArgumentException { + private String[] concreteIndices(String aliasOrIndex, boolean allowNoIndices, boolean failClosed, boolean allowMultipleIndices) throws IndexMissingException, ElasticsearchIllegalArgumentException { // a quick check, if this is an actual index, if so, return it - if (indices.containsKey(aliasOrIndex)) { - return new String[]{aliasOrIndex}; + IndexMetaData indexMetaData = indices.get(aliasOrIndex); + if (indexMetaData != null) { + if (indexMetaData.getState() == IndexMetaData.State.CLOSE && failClosed) { + throw new IndexClosedException(new Index(aliasOrIndex)); + } else { + return new String[]{aliasOrIndex}; + } } // not an actual index, fetch from an alias String[] indices = aliasAndIndexToIndexMap.getOrDefault(aliasOrIndex, Strings.EMPTY_ARRAY); @@ -704,6 +728,11 @@ public class MetaData implements Iterable { if (indices.length > 1 && !allowMultipleIndices) { throw new ElasticsearchIllegalArgumentException("Alias [" + aliasOrIndex + "] has more than one indices associated with it [" + Arrays.toString(indices) + "], can't execute a single index op"); } + + indexMetaData = this.indices.get(aliasOrIndex); + if (indexMetaData != null && indexMetaData.getState() == IndexMetaData.State.CLOSE && failClosed) { + throw new IndexClosedException(new Index(aliasOrIndex)); + } return indices; } diff --git a/src/main/java/org/elasticsearch/indices/IndexClosedException.java b/src/main/java/org/elasticsearch/indices/IndexClosedException.java new file mode 100644 index 00000000000..e7080012921 --- /dev/null +++ b/src/main/java/org/elasticsearch/indices/IndexClosedException.java @@ -0,0 +1,39 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.indices; + +import org.elasticsearch.index.Index; +import org.elasticsearch.index.IndexException; +import org.elasticsearch.rest.RestStatus; + +/** + * Exception indicating that one or more requested indices are closed. + */ +public class IndexClosedException extends IndexException { + + public IndexClosedException(Index index) { + super(index, "closed"); + } + + @Override + public RestStatus status() { + return RestStatus.FORBIDDEN; + } +} \ No newline at end of file diff --git a/src/test/java/org/elasticsearch/action/percolate/MultiPercolatorRequestTests.java b/src/test/java/org/elasticsearch/action/percolate/MultiPercolatorRequestTests.java index aa49912813c..975b4b91a67 100644 --- a/src/test/java/org/elasticsearch/action/percolate/MultiPercolatorRequestTests.java +++ b/src/test/java/org/elasticsearch/action/percolate/MultiPercolatorRequestTests.java @@ -44,7 +44,7 @@ public class MultiPercolatorRequestTests extends ElasticsearchTestCase { assertThat(percolateRequest.documentType(), equalTo("my-type1")); assertThat(percolateRequest.routing(), equalTo("my-routing-1")); assertThat(percolateRequest.preference(), equalTo("_local")); - assertThat(percolateRequest.indicesOptions(), equalTo(IndicesOptions.strictExpandOpen())); + assertThat(percolateRequest.indicesOptions(), equalTo(IndicesOptions.strictExpandOpenAndForbidClosed())); assertThat(percolateRequest.onlyCount(), equalTo(false)); assertThat(percolateRequest.getRequest(), nullValue()); assertThat(percolateRequest.source(), notNullValue()); @@ -57,7 +57,7 @@ public class MultiPercolatorRequestTests extends ElasticsearchTestCase { assertThat(percolateRequest.documentType(), equalTo("my-type1")); assertThat(percolateRequest.routing(), equalTo("my-routing-1")); assertThat(percolateRequest.preference(), equalTo("_local")); - assertThat(percolateRequest.indicesOptions(), equalTo(IndicesOptions.lenientExpandOpen())); + assertThat(percolateRequest.indicesOptions(), equalTo(IndicesOptions.fromOptions(true, true, true, false, IndicesOptions.strictExpandOpenAndForbidClosed()))); assertThat(percolateRequest.onlyCount(), equalTo(false)); assertThat(percolateRequest.getRequest(), nullValue()); assertThat(percolateRequest.source(), notNullValue()); @@ -70,7 +70,7 @@ public class MultiPercolatorRequestTests extends ElasticsearchTestCase { assertThat(percolateRequest.documentType(), equalTo("my-type1")); assertThat(percolateRequest.routing(), equalTo("my-routing-1")); assertThat(percolateRequest.preference(), equalTo("_local")); - assertThat(percolateRequest.indicesOptions(), equalTo(IndicesOptions.fromOptions(false, true, true, true))); + assertThat(percolateRequest.indicesOptions(), equalTo(IndicesOptions.fromOptions(false, true, true, true, IndicesOptions.strictExpandOpenAndForbidClosed()))); assertThat(percolateRequest.onlyCount(), equalTo(true)); assertThat(percolateRequest.getRequest(), nullValue()); assertThat(percolateRequest.source(), notNullValue()); @@ -82,7 +82,7 @@ public class MultiPercolatorRequestTests extends ElasticsearchTestCase { assertThat(percolateRequest.documentType(), equalTo("my-type1")); assertThat(percolateRequest.routing(), equalTo("my-routing-1")); assertThat(percolateRequest.preference(), equalTo("_local")); - assertThat(percolateRequest.indicesOptions(), equalTo(IndicesOptions.fromOptions(false, true, true, true))); + assertThat(percolateRequest.indicesOptions(), equalTo(IndicesOptions.fromOptions(false, true, true, true, IndicesOptions.strictExpandOpenAndForbidClosed()))); assertThat(percolateRequest.onlyCount(), equalTo(false)); assertThat(percolateRequest.getRequest(), notNullValue()); assertThat(percolateRequest.getRequest().id(), equalTo("1")); @@ -96,7 +96,7 @@ public class MultiPercolatorRequestTests extends ElasticsearchTestCase { assertThat(percolateRequest.documentType(), equalTo("my-type1")); assertThat(percolateRequest.routing(), equalTo("my-routing-1")); assertThat(percolateRequest.preference(), equalTo("_local")); - assertThat(percolateRequest.indicesOptions(), equalTo(IndicesOptions.strictExpandOpen())); + assertThat(percolateRequest.indicesOptions(), equalTo(IndicesOptions.strictExpandOpenAndForbidClosed())); assertThat(percolateRequest.onlyCount(), equalTo(true)); assertThat(percolateRequest.getRequest(), notNullValue()); assertThat(percolateRequest.getRequest().id(), equalTo("2")); @@ -110,7 +110,7 @@ public class MultiPercolatorRequestTests extends ElasticsearchTestCase { assertThat(percolateRequest.documentType(), equalTo("my-type1")); assertThat(percolateRequest.routing(), equalTo("my-routing-1")); assertThat(percolateRequest.preference(), equalTo("primary")); - assertThat(percolateRequest.indicesOptions(), equalTo(IndicesOptions.strictExpandOpen())); + assertThat(percolateRequest.indicesOptions(), equalTo(IndicesOptions.strictExpandOpenAndForbidClosed())); assertThat(percolateRequest.onlyCount(), equalTo(false)); assertThat(percolateRequest.getRequest(), nullValue()); assertThat(percolateRequest.source(), notNullValue()); diff --git a/src/test/java/org/elasticsearch/action/search/MultiSearchRequestTests.java b/src/test/java/org/elasticsearch/action/search/MultiSearchRequestTests.java index 103ab7dbb98..96eec02770a 100644 --- a/src/test/java/org/elasticsearch/action/search/MultiSearchRequestTests.java +++ b/src/test/java/org/elasticsearch/action/search/MultiSearchRequestTests.java @@ -37,10 +37,10 @@ public class MultiSearchRequestTests extends ElasticsearchTestCase { MultiSearchRequest request = new MultiSearchRequest().add(data, 0, data.length, false, null, null, null); assertThat(request.requests().size(), equalTo(5)); assertThat(request.requests().get(0).indices()[0], equalTo("test")); - assertThat(request.requests().get(0).indicesOptions(), equalTo(IndicesOptions.fromOptions(true, true, true, true))); + assertThat(request.requests().get(0).indicesOptions(), equalTo(IndicesOptions.fromOptions(true, true, true, true, IndicesOptions.strictExpandOpenAndForbidClosed()))); assertThat(request.requests().get(0).types().length, equalTo(0)); assertThat(request.requests().get(1).indices()[0], equalTo("test")); - assertThat(request.requests().get(1).indicesOptions(), equalTo(IndicesOptions.fromOptions(false, true, true, true))); + assertThat(request.requests().get(1).indicesOptions(), equalTo(IndicesOptions.fromOptions(false, true, true, true, IndicesOptions.strictExpandOpenAndForbidClosed()))); assertThat(request.requests().get(1).types()[0], equalTo("type1")); assertThat(request.requests().get(2).indices(), nullValue()); assertThat(request.requests().get(2).types().length, equalTo(0)); diff --git a/src/test/java/org/elasticsearch/action/support/IndicesOptionsTests.java b/src/test/java/org/elasticsearch/action/support/IndicesOptionsTests.java index 804fede957b..f1e585c929f 100644 --- a/src/test/java/org/elasticsearch/action/support/IndicesOptionsTests.java +++ b/src/test/java/org/elasticsearch/action/support/IndicesOptionsTests.java @@ -33,7 +33,7 @@ public class IndicesOptionsTests extends ElasticsearchTestCase { public void testSerialization() throws Exception { int iterations = randomIntBetween(5, 20); for (int i = 0; i < iterations; i++) { - IndicesOptions indicesOptions = IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean()); + IndicesOptions indicesOptions = IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean()); BytesStreamOutput output = new BytesStreamOutput(); Version outputVersion = randomVersion(); @@ -49,11 +49,16 @@ public class IndicesOptionsTests extends ElasticsearchTestCase { assertThat(indicesOptions2.expandWildcardsOpen(), equalTo(indicesOptions.expandWildcardsOpen())); assertThat(indicesOptions2.expandWildcardsClosed(), equalTo(indicesOptions.expandWildcardsClosed())); - if (outputVersion.onOrAfter(Version.V_1_2_0)) { + if (outputVersion.onOrAfter(Version.V_1_2_2)) { + assertThat(indicesOptions2.forbidClosedIndices(), equalTo(indicesOptions.forbidClosedIndices())); assertThat(indicesOptions2.allowAliasesToMultipleIndices(), equalTo(indicesOptions.allowAliasesToMultipleIndices())); + } else if (outputVersion.onOrAfter(Version.V_1_2_0)) { + assertThat(indicesOptions2.allowAliasesToMultipleIndices(), equalTo(indicesOptions.allowAliasesToMultipleIndices())); + assertThat(indicesOptions2.forbidClosedIndices(), equalTo(false)); } else { //default value (true) if the node version doesn't support the allowAliasesToMultipleIndices flag assertThat(indicesOptions2.allowAliasesToMultipleIndices(), equalTo(true)); + assertThat(indicesOptions2.forbidClosedIndices(), equalTo(false)); } } } @@ -67,13 +72,19 @@ public class IndicesOptionsTests extends ElasticsearchTestCase { boolean expandToOpenIndices = randomBoolean(); boolean expandToClosedIndices = randomBoolean(); boolean allowAliasesToMultipleIndices = randomBoolean(); - IndicesOptions indicesOptions = IndicesOptions.fromOptions(ignoreUnavailable, allowNoIndices, expandToOpenIndices, expandToClosedIndices, allowAliasesToMultipleIndices); + boolean forbidClosedIndices = randomBoolean(); + IndicesOptions indicesOptions = IndicesOptions.fromOptions( + ignoreUnavailable, allowNoIndices,expandToOpenIndices, expandToClosedIndices, + allowAliasesToMultipleIndices, forbidClosedIndices + ); assertThat(indicesOptions.ignoreUnavailable(), equalTo(ignoreUnavailable)); assertThat(indicesOptions.allowNoIndices(), equalTo(allowNoIndices)); assertThat(indicesOptions.expandWildcardsOpen(), equalTo(expandToOpenIndices)); assertThat(indicesOptions.expandWildcardsClosed(), equalTo(expandToClosedIndices)); assertThat(indicesOptions.allowAliasesToMultipleIndices(), equalTo(allowAliasesToMultipleIndices)); + assertThat(indicesOptions.allowAliasesToMultipleIndices(), equalTo(allowAliasesToMultipleIndices)); + assertThat(indicesOptions.forbidClosedIndices(), equalTo(forbidClosedIndices)); } } } diff --git a/src/test/java/org/elasticsearch/indices/IndicesOptionsIntegrationTests.java b/src/test/java/org/elasticsearch/indices/IndicesOptionsIntegrationTests.java index 1d7a87c0f44..7747ff03ef4 100644 --- a/src/test/java/org/elasticsearch/indices/IndicesOptionsIntegrationTests.java +++ b/src/test/java/org/elasticsearch/indices/IndicesOptionsIntegrationTests.java @@ -49,7 +49,6 @@ import org.elasticsearch.action.search.SearchRequestBuilder; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.suggest.SuggestRequestBuilder; import org.elasticsearch.action.support.IndicesOptions; -import org.elasticsearch.cluster.block.ClusterBlockException; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.settings.ImmutableSettings; @@ -409,16 +408,12 @@ public class IndicesOptionsIntegrationTests extends ElasticsearchIntegrationTest verify(count("test1", "test2"), false); assertAcked(client().admin().indices().prepareClose("test2").get()); - try { - search("test1", "test2").get(); - fail("Exception should have been thrown"); - } catch (ClusterBlockException e) { - } - try { - count("test1", "test2").get(); - fail("Exception should have been thrown"); - } catch (ClusterBlockException e) { - } + verify(search("test1", "test2"), true); + verify(count("test1", "test2"), true); + + IndicesOptions options = IndicesOptions.fromOptions(true, true, true, false, IndicesOptions.strictExpandOpenAndForbidClosed()); + verify(search("test1", "test2").setIndicesOptions(options), false); + verify(count("test1", "test2").setIndicesOptions(options), false); verify(search(), false); verify(count(), false); @@ -846,8 +841,8 @@ public class IndicesOptionsIntegrationTests extends ElasticsearchIntegrationTest } else { try { requestBuilder.get(); - fail("IndexMissingException was expected"); - } catch (IndexMissingException e) {} + fail("IndexMissingException or IndexClosedException was expected"); + } catch (IndexMissingException | IndexClosedException e) {} } } else { if (requestBuilder instanceof SearchRequestBuilder) {