New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The ignore_unavailable
option should also ignore indices that are closed
#6475
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,7 @@ | |
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 @@ private Map<String, Set<String>> resolveSearchRoutingAllIndices(String routing) | |
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 @@ private Map<String, Set<String>> resolveSearchRoutingAllIndices(String routing) | |
* 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 String[] concreteIndices(IndicesOptions indicesOptions, String... aliases | |
|
||
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<String> actualIndices = new HashSet<>(Arrays.asList(aliasesOrIndices)); | ||
actualIndices.retainAll(new HashSet<Object>(Arrays.asList(allOpenIndices))); | ||
return actualIndices.toArray(new String[actualIndices.size()]); | ||
} else { | ||
return aliasesOrIndices; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code block is an optimization that's useful in case there's no aliases in the input argument and we can return that same array quickly as we received it. I wonder if it still makes sense to have this optimization given that we might need to modify the array. I would love to remove it as it adds complexity... What do you think @martijnvg? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Most of the times requested indices aren't closed indices, so I think in that case the additional checks is worth it. |
||
} | ||
|
||
Set<String> 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 String concreteSingleIndex(String indexOrAlias) throws IndexMissingExcept | |
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 @@ private String[] concreteIndices(String aliasOrIndex, boolean allowNoIndices, bo | |
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; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure if you wanted to do the following instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
indicesOptions
field is already set toIndicesOptions.strictExpandOpenAndForbidClosed()
, so this should be ok?