Skip to content

Commit b9fe169

Browse files
committed
Core: The ignore_unavailable=true setting also ignores indices that are closed.
Closes #6471 Closes #6475
1 parent 6eba38a commit b9fe169

File tree

12 files changed

+167
-61
lines changed

12 files changed

+167
-61
lines changed

src/main/java/org/elasticsearch/action/percolate/MultiPercolateRequest.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public class MultiPercolateRequest extends ActionRequest<MultiPercolateRequest>
4848

4949
private String[] indices;
5050
private String documentType;
51-
private IndicesOptions indicesOptions = IndicesOptions.strictExpandOpen();
51+
private IndicesOptions indicesOptions = IndicesOptions.strictExpandOpenAndForbidClosed();
5252
private List<PercolateRequest> requests = Lists.newArrayList();
5353

5454
public MultiPercolateRequest add(PercolateRequestBuilder requestBuilder) {
@@ -62,7 +62,7 @@ public MultiPercolateRequest add(PercolateRequest request) {
6262
if (request.documentType() == null && documentType != null) {
6363
request.documentType(documentType);
6464
}
65-
if (request.indicesOptions() == IndicesOptions.strictExpandOpen() && indicesOptions != IndicesOptions.strictExpandOpen()) {
65+
if (request.indicesOptions() == IndicesOptions.strictExpandOpenAndForbidClosed() && indicesOptions != IndicesOptions.strictExpandOpenAndForbidClosed()) {
6666
request.indicesOptions(indicesOptions);
6767
}
6868
requests.add(request);
@@ -96,7 +96,7 @@ public MultiPercolateRequest add(BytesReference data, boolean contentUnsafe, boo
9696
if (documentType != null) {
9797
percolateRequest.documentType(documentType);
9898
}
99-
if (indicesOptions != IndicesOptions.strictExpandOpen()) {
99+
if (indicesOptions != IndicesOptions.strictExpandOpenAndForbidClosed()) {
100100
percolateRequest.indicesOptions(indicesOptions);
101101
}
102102

@@ -165,10 +165,11 @@ private void parsePercolateAction(XContentParser parser, PercolateRequest percol
165165
}
166166
}
167167

168-
boolean ignoreUnavailable = IndicesOptions.strictExpandOpen().ignoreUnavailable();
169-
boolean allowNoIndices = IndicesOptions.strictExpandOpen().allowNoIndices();
170-
boolean expandWildcardsOpen = IndicesOptions.strictExpandOpen().expandWildcardsOpen();
171-
boolean expandWildcardsClosed = IndicesOptions.strictExpandOpen().expandWildcardsClosed();
168+
IndicesOptions defaultOptions = indicesOptions;
169+
boolean ignoreUnavailable = defaultOptions.ignoreUnavailable();
170+
boolean allowNoIndices = defaultOptions.allowNoIndices();
171+
boolean expandWildcardsOpen = defaultOptions.expandWildcardsOpen();
172+
boolean expandWildcardsClosed = defaultOptions.expandWildcardsClosed();
172173

173174
if (header.containsKey("id")) {
174175
GetRequest getRequest = new GetRequest(globalIndex);
@@ -280,7 +281,7 @@ private void parsePercolateAction(XContentParser parser, PercolateRequest percol
280281
}
281282
}
282283
}
283-
percolateRequest.indicesOptions(IndicesOptions.fromOptions(ignoreUnavailable, allowNoIndices, expandWildcardsOpen, expandWildcardsClosed));
284+
percolateRequest.indicesOptions(IndicesOptions.fromOptions(ignoreUnavailable, allowNoIndices, expandWildcardsOpen, expandWildcardsClosed, defaultOptions));
284285
}
285286

286287
private String[] parseArray(XContentParser parser) throws IOException {

src/main/java/org/elasticsearch/action/search/MultiSearchRequest.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public class MultiSearchRequest extends ActionRequest<MultiSearchRequest> {
4848

4949
private List<SearchRequest> requests = Lists.newArrayList();
5050

51-
private IndicesOptions indicesOptions = IndicesOptions.strictExpandOpen();
51+
private IndicesOptions indicesOptions = IndicesOptions.strictExpandOpenAndForbidClosed();
5252

5353
/**
5454
* Add a search request to execute. Note, the order is important, the search response will be returned in the
@@ -70,7 +70,7 @@ public MultiSearchRequest add(SearchRequest request) {
7070

7171
public MultiSearchRequest add(byte[] data, int from, int length, boolean contentUnsafe,
7272
@Nullable String[] indices, @Nullable String[] types, @Nullable String searchType) throws Exception {
73-
return add(new BytesArray(data, from, length), contentUnsafe, indices, types, searchType, null, IndicesOptions.strictExpandOpen(), true);
73+
return add(new BytesArray(data, from, length), contentUnsafe, indices, types, searchType, null, IndicesOptions.strictExpandOpenAndForbidClosed(), true);
7474
}
7575

7676
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 MultiSearchRequest add(BytesReference data, boolean contentUnsafe, @Nulla
108108
}
109109
searchRequest.searchType(searchType);
110110

111-
boolean ignoreUnavailable = IndicesOptions.strictExpandOpen().ignoreUnavailable();
112-
boolean allowNoIndices = IndicesOptions.strictExpandOpen().allowNoIndices();
113-
boolean expandWildcardsOpen = IndicesOptions.strictExpandOpen().expandWildcardsOpen();
114-
boolean expandWildcardsClosed = IndicesOptions.strictExpandOpen().expandWildcardsClosed();
111+
IndicesOptions defaultOptions = IndicesOptions.strictExpandOpenAndForbidClosed();
112+
boolean ignoreUnavailable = defaultOptions.ignoreUnavailable();
113+
boolean allowNoIndices = defaultOptions.allowNoIndices();
114+
boolean expandWildcardsOpen = defaultOptions.expandWildcardsOpen();
115+
boolean expandWildcardsClosed = defaultOptions.expandWildcardsClosed();
115116

116117
// now parse the action
117118
if (nextMarker - from > 0) {
@@ -181,7 +182,7 @@ public MultiSearchRequest add(BytesReference data, boolean contentUnsafe, @Nulla
181182
}
182183
}
183184
}
184-
searchRequest.indicesOptions(IndicesOptions.fromOptions(ignoreUnavailable, allowNoIndices, expandWildcardsOpen, expandWildcardsClosed));
185+
searchRequest.indicesOptions(IndicesOptions.fromOptions(ignoreUnavailable, allowNoIndices, expandWildcardsOpen, expandWildcardsClosed, defaultOptions));
185186

186187
// move pointers
187188
from = nextMarker + 1;

src/main/java/org/elasticsearch/action/search/MultiSearchRequestBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public MultiSearchRequestBuilder(Client client) {
4242
* will not be used (if set).
4343
*/
4444
public MultiSearchRequestBuilder add(SearchRequest request) {
45-
if (request.indicesOptions() == IndicesOptions.strictExpandOpen() && request().indicesOptions() != IndicesOptions.strictExpandOpen()) {
45+
if (request.indicesOptions() == IndicesOptions.strictExpandOpenAndForbidClosed() && request().indicesOptions() != IndicesOptions.strictExpandOpenAndForbidClosed()) {
4646
request.indicesOptions(request().indicesOptions());
4747
}
4848

@@ -55,7 +55,7 @@ public MultiSearchRequestBuilder add(SearchRequest request) {
5555
* same order as the search requests.
5656
*/
5757
public MultiSearchRequestBuilder add(SearchRequestBuilder request) {
58-
if (request.request().indicesOptions() == IndicesOptions.strictExpandOpen() && request().indicesOptions() != IndicesOptions.strictExpandOpen()) {
58+
if (request.request().indicesOptions() == IndicesOptions.strictExpandOpenAndForbidClosed() && request().indicesOptions() != IndicesOptions.strictExpandOpenAndForbidClosed()) {
5959
request.request().indicesOptions(request().indicesOptions());
6060
}
6161

src/main/java/org/elasticsearch/action/search/SearchRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public class SearchRequest extends ActionRequest<SearchRequest> {
8383

8484
private String[] types = Strings.EMPTY_ARRAY;
8585

86-
private IndicesOptions indicesOptions = IndicesOptions.strictExpandOpen();
86+
private IndicesOptions indicesOptions = IndicesOptions.strictExpandOpenAndForbidClosed();
8787

8888
public SearchRequest() {
8989
}

src/main/java/org/elasticsearch/action/support/IndicesOptions.java

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,10 @@ public class IndicesOptions {
4040
private static final byte EXPAND_WILDCARDS_OPEN = 4;
4141
private static final byte EXPAND_WILDCARDS_CLOSED = 8;
4242
private static final byte FORBID_ALIASES_TO_MULTIPLE_INDICES = 16;
43+
private static final byte FORBID_CLOSED_INDICES = 32;
4344

4445
static {
45-
byte max = 1 << 5;
46+
byte max = 1 << 6;
4647
VALUES = new IndicesOptions[max];
4748
for (byte id = 0; id < max; id++) {
4849
VALUES[id] = new IndicesOptions(id);
@@ -84,6 +85,13 @@ public boolean expandWildcardsClosed() {
8485
return (id & EXPAND_WILDCARDS_CLOSED) != 0;
8586
}
8687

88+
/**
89+
* @return Whether execution on closed indices is allowed.
90+
*/
91+
public boolean forbidClosedIndices() {
92+
return (id & FORBID_CLOSED_INDICES) != 0;
93+
}
94+
8795
/**
8896
* @return whether aliases pointing to multiple indices are allowed
8997
*/
@@ -94,12 +102,16 @@ public boolean allowAliasesToMultipleIndices() {
94102
}
95103

96104
public void writeIndicesOptions(StreamOutput out) throws IOException {
97-
if (allowAliasesToMultipleIndices() || out.getVersion().onOrAfter(Version.V_1_2_0)) {
105+
if (out.getVersion().onOrAfter(Version.V_1_2_2)) {
98106
out.write(id);
99-
} else {
100-
//if we are talking to a node that doesn't support the newly added flag (allowAliasesToMultipleIndices)
101-
//flip to 0 all the bits starting from the 5th
107+
} else if (out.getVersion().before(Version.V_1_2_0)) {
108+
// Target node doesn't know about the FORBID_CLOSED_INDICES and FORBID_ALIASES_TO_MULTIPLE_INDICES flags,
109+
// so unset the bits starting from the 5th position.
102110
out.write(id & 0xf);
111+
} else {
112+
// Target node doesn't know about the FORBID_CLOSED_INDICES flag,
113+
// so unset the bits starting from the 6th position.
114+
out.write(id & 0x1f);
103115
}
104116
}
105117

@@ -114,11 +126,15 @@ public static IndicesOptions readIndicesOptions(StreamInput in) throws IOExcepti
114126
}
115127

116128
public static IndicesOptions fromOptions(boolean ignoreUnavailable, boolean allowNoIndices, boolean expandToOpenIndices, boolean expandToClosedIndices) {
117-
return fromOptions(ignoreUnavailable, allowNoIndices, expandToOpenIndices, expandToClosedIndices, true);
129+
return fromOptions(ignoreUnavailable, allowNoIndices, expandToOpenIndices, expandToClosedIndices, true, false);
118130
}
119131

120-
static IndicesOptions fromOptions(boolean ignoreUnavailable, boolean allowNoIndices, boolean expandToOpenIndices, boolean expandToClosedIndices, boolean allowAliasesToMultipleIndices) {
121-
byte id = toByte(ignoreUnavailable, allowNoIndices, expandToOpenIndices, expandToClosedIndices, allowAliasesToMultipleIndices);
132+
public static IndicesOptions fromOptions(boolean ignoreUnavailable, boolean allowNoIndices, boolean expandToOpenIndices, boolean expandToClosedIndices, IndicesOptions defaultOptions) {
133+
return fromOptions(ignoreUnavailable, allowNoIndices, expandToOpenIndices, expandToClosedIndices, defaultOptions.allowAliasesToMultipleIndices(), defaultOptions.forbidClosedIndices());
134+
}
135+
136+
static IndicesOptions fromOptions(boolean ignoreUnavailable, boolean allowNoIndices, boolean expandToOpenIndices, boolean expandToClosedIndices, boolean allowAliasesToMultipleIndices, boolean forbidClosedIndices) {
137+
byte id = toByte(ignoreUnavailable, allowNoIndices, expandToOpenIndices, expandToClosedIndices, allowAliasesToMultipleIndices, forbidClosedIndices);
122138
return VALUES[id];
123139
}
124140

@@ -150,7 +166,9 @@ public static IndicesOptions fromRequest(RestRequest request, IndicesOptions def
150166
toBool(sIgnoreUnavailable, defaultSettings.ignoreUnavailable()),
151167
toBool(sAllowNoIndices, defaultSettings.allowNoIndices()),
152168
expandWildcardsOpen,
153-
expandWildcardsClosed
169+
expandWildcardsClosed,
170+
defaultSettings.allowAliasesToMultipleIndices(),
171+
defaultSettings.forbidClosedIndices()
154172
);
155173
}
156174

@@ -172,6 +190,15 @@ public static IndicesOptions strictExpandOpen() {
172190
return VALUES[6];
173191
}
174192

193+
/**
194+
* @return indices options that requires every specified index to exist, expands wildcards only to open indices,
195+
* allows that no indices are resolved from wildcard expressions (not returning an error) and forbids the
196+
* use of closed indices by throwing an error.
197+
*/
198+
public static IndicesOptions strictExpandOpenAndForbidClosed() {
199+
return VALUES[38];
200+
}
201+
175202
/**
176203
* @return indices option that requires every specified index to exist, expands wildcards to both open and closed
177204
* indices and allows that no indices are resolved from wildcard expressions (not returning an error).
@@ -206,7 +233,8 @@ public static IndicesOptions lenientExpandOpen() {
206233
return VALUES[7];
207234
}
208235

209-
private static byte toByte(boolean ignoreUnavailable, boolean allowNoIndices, boolean wildcardExpandToOpen, boolean wildcardExpandToClosed, boolean allowAliasesToMultipleIndices) {
236+
private static byte toByte(boolean ignoreUnavailable, boolean allowNoIndices, boolean wildcardExpandToOpen,
237+
boolean wildcardExpandToClosed, boolean allowAliasesToMultipleIndices, boolean forbidClosedIndices) {
210238
byte id = 0;
211239
if (ignoreUnavailable) {
212240
id |= IGNORE_UNAVAILABLE;
@@ -225,6 +253,9 @@ private static byte toByte(boolean ignoreUnavailable, boolean allowNoIndices, bo
225253
if (!allowAliasesToMultipleIndices) {
226254
id |= FORBID_ALIASES_TO_MULTIPLE_INDICES;
227255
}
256+
if (forbidClosedIndices) {
257+
id |= FORBID_CLOSED_INDICES;
258+
}
228259
return id;
229260
}
230261

src/main/java/org/elasticsearch/action/support/broadcast/BroadcastOperationRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
public abstract class BroadcastOperationRequest<T extends BroadcastOperationRequest> extends ActionRequest<T> {
3535

3636
protected String[] indices;
37-
private IndicesOptions indicesOptions = IndicesOptions.strictExpandOpen();
37+
private IndicesOptions indicesOptions = IndicesOptions.strictExpandOpenAndForbidClosed();
3838

3939
protected BroadcastOperationRequest() {
4040

src/main/java/org/elasticsearch/cluster/metadata/MetaData.java

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.elasticsearch.common.settings.loader.SettingsLoader;
4242
import org.elasticsearch.common.xcontent.*;
4343
import org.elasticsearch.index.Index;
44+
import org.elasticsearch.indices.IndexClosedException;
4445
import org.elasticsearch.indices.IndexMissingException;
4546
import org.elasticsearch.rest.RestStatus;
4647
import org.elasticsearch.search.warmer.IndexWarmersMetaData;
@@ -635,6 +636,7 @@ public String[] concreteIndices(String[] indices) throws IndexMissingException {
635636

636637
/**
637638
* Translates the provided indices or aliases, eventually containing wildcard expressions, into actual indices.
639+
*
638640
* @param indicesOptions how the aliases or indices need to be resolved to concrete indices
639641
* @param aliasesOrIndices the aliases or indices to be resolved to concrete indices
640642
* @return the obtained concrete indices
@@ -645,7 +647,6 @@ public String[] concreteIndices(String[] indices) throws IndexMissingException {
645647
* indices options don't allow such a case.
646648
*/
647649
public String[] concreteIndices(IndicesOptions indicesOptions, String... aliasesOrIndices) throws IndexMissingException, ElasticsearchIllegalArgumentException {
648-
649650
if (indicesOptions.expandWildcardsOpen() || indicesOptions.expandWildcardsClosed()) {
650651
if (isAllIndices(aliasesOrIndices)) {
651652
String[] concreteIndices;
@@ -665,27 +666,44 @@ public String[] concreteIndices(IndicesOptions indicesOptions, String... aliases
665666

666667
aliasesOrIndices = convertFromWildcards(aliasesOrIndices, indicesOptions);
667668
}
669+
boolean failClosed = indicesOptions.forbidClosedIndices() && !indicesOptions.ignoreUnavailable();
668670

669671
// optimize for single element index (common case)
670672
if (aliasesOrIndices.length == 1) {
671-
return concreteIndices(aliasesOrIndices[0], indicesOptions.allowNoIndices(), indicesOptions.allowAliasesToMultipleIndices());
673+
return concreteIndices(aliasesOrIndices[0], indicesOptions.allowNoIndices(), failClosed, indicesOptions.allowAliasesToMultipleIndices());
672674
}
673675

674676
// check if its a possible aliased index, if not, just return the passed array
675677
boolean possiblyAliased = false;
678+
boolean closedIndices = false;
676679
for (String index : aliasesOrIndices) {
677-
if (!this.indices.containsKey(index)) {
680+
IndexMetaData indexMetaData = indices.get(index);
681+
if (indexMetaData == null) {
678682
possiblyAliased = true;
679683
break;
684+
} else {
685+
if (indicesOptions.forbidClosedIndices() && indexMetaData.getState() == IndexMetaData.State.CLOSE) {
686+
if (failClosed) {
687+
throw new IndexClosedException(new Index(index));
688+
} else {
689+
closedIndices = true;
690+
}
691+
}
680692
}
681693
}
682694
if (!possiblyAliased) {
683-
return aliasesOrIndices;
695+
if (closedIndices) {
696+
Set<String> actualIndices = new HashSet<>(Arrays.asList(aliasesOrIndices));
697+
actualIndices.retainAll(new HashSet<Object>(Arrays.asList(allOpenIndices)));
698+
return actualIndices.toArray(new String[actualIndices.size()]);
699+
} else {
700+
return aliasesOrIndices;
701+
}
684702
}
685703

686704
Set<String> actualIndices = new HashSet<>();
687705
for (String aliasOrIndex : aliasesOrIndices) {
688-
String[] indices = concreteIndices(aliasOrIndex, indicesOptions.ignoreUnavailable(), indicesOptions.allowAliasesToMultipleIndices());
706+
String[] indices = concreteIndices(aliasOrIndex, indicesOptions.ignoreUnavailable(), failClosed, indicesOptions.allowAliasesToMultipleIndices());
689707
Collections.addAll(actualIndices, indices);
690708
}
691709

@@ -701,10 +719,15 @@ public String concreteSingleIndex(String indexOrAlias) throws IndexMissingExcept
701719
return indices[0];
702720
}
703721

704-
private String[] concreteIndices(String aliasOrIndex, boolean allowNoIndices, boolean allowMultipleIndices) throws IndexMissingException, ElasticsearchIllegalArgumentException {
722+
private String[] concreteIndices(String aliasOrIndex, boolean allowNoIndices, boolean failClosed, boolean allowMultipleIndices) throws IndexMissingException, ElasticsearchIllegalArgumentException {
705723
// a quick check, if this is an actual index, if so, return it
706-
if (indices.containsKey(aliasOrIndex)) {
707-
return new String[]{aliasOrIndex};
724+
IndexMetaData indexMetaData = indices.get(aliasOrIndex);
725+
if (indexMetaData != null) {
726+
if (indexMetaData.getState() == IndexMetaData.State.CLOSE && failClosed) {
727+
throw new IndexClosedException(new Index(aliasOrIndex));
728+
} else {
729+
return new String[]{aliasOrIndex};
730+
}
708731
}
709732
// not an actual index, fetch from an alias
710733
String[] indices = aliasAndIndexToIndexMap.getOrDefault(aliasOrIndex, Strings.EMPTY_ARRAY);
@@ -714,6 +737,11 @@ private String[] concreteIndices(String aliasOrIndex, boolean allowNoIndices, bo
714737
if (indices.length > 1 && !allowMultipleIndices) {
715738
throw new ElasticsearchIllegalArgumentException("Alias [" + aliasOrIndex + "] has more than one indices associated with it [" + Arrays.toString(indices) + "], can't execute a single index op");
716739
}
740+
741+
indexMetaData = this.indices.get(aliasOrIndex);
742+
if (indexMetaData != null && indexMetaData.getState() == IndexMetaData.State.CLOSE && failClosed) {
743+
throw new IndexClosedException(new Index(aliasOrIndex));
744+
}
717745
return indices;
718746
}
719747

0 commit comments

Comments
 (0)