use of org.opensearch.index.IndexNotFoundException in project OpenSearch by opensearch-project.
the class WildcardExpressionResolverTests method testResolveAliases.
public void testResolveAliases() {
Metadata.Builder mdBuilder = Metadata.builder().put(indexBuilder("foo_foo").state(State.OPEN)).put(indexBuilder("bar_bar").state(State.OPEN)).put(indexBuilder("foo_index").state(State.OPEN).putAlias(AliasMetadata.builder("foo_alias"))).put(indexBuilder("bar_index").state(State.OPEN).putAlias(AliasMetadata.builder("foo_alias")));
ClusterState state = ClusterState.builder(new ClusterName("_name")).metadata(mdBuilder).build();
IndexNameExpressionResolver.WildcardExpressionResolver resolver = new IndexNameExpressionResolver.WildcardExpressionResolver();
// when ignoreAliases option is not set, WildcardExpressionResolver resolves the provided
// expressions against the defined indices and aliases
IndicesOptions indicesAndAliasesOptions = IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), true, false, true, false, false, false);
IndexNameExpressionResolver.Context indicesAndAliasesContext = new IndexNameExpressionResolver.Context(state, indicesAndAliasesOptions, false);
// ignoreAliases option is set, WildcardExpressionResolver throws error when
IndicesOptions skipAliasesIndicesOptions = IndicesOptions.fromOptions(true, true, true, false, true, false, true, false);
IndexNameExpressionResolver.Context skipAliasesLenientContext = new IndexNameExpressionResolver.Context(state, skipAliasesIndicesOptions, false);
// ignoreAliases option is set, WildcardExpressionResolver resolves the provided expressions only against the defined indices
IndicesOptions errorOnAliasIndicesOptions = IndicesOptions.fromOptions(false, false, true, false, true, false, true, false);
IndexNameExpressionResolver.Context skipAliasesStrictContext = new IndexNameExpressionResolver.Context(state, errorOnAliasIndicesOptions, false);
{
List<String> indices = resolver.resolve(indicesAndAliasesContext, Collections.singletonList("foo_a*"));
assertThat(indices, containsInAnyOrder("foo_index", "bar_index"));
}
{
List<String> indices = resolver.resolve(skipAliasesLenientContext, Collections.singletonList("foo_a*"));
assertEquals(0, indices.size());
}
{
IndexNotFoundException infe = expectThrows(IndexNotFoundException.class, () -> resolver.resolve(skipAliasesStrictContext, Collections.singletonList("foo_a*")));
assertEquals("foo_a*", infe.getIndex().getName());
}
{
List<String> indices = resolver.resolve(indicesAndAliasesContext, Collections.singletonList("foo*"));
assertThat(indices, containsInAnyOrder("foo_foo", "foo_index", "bar_index"));
}
{
List<String> indices = resolver.resolve(skipAliasesLenientContext, Collections.singletonList("foo*"));
assertThat(indices, containsInAnyOrder("foo_foo", "foo_index"));
}
{
List<String> indices = resolver.resolve(skipAliasesStrictContext, Collections.singletonList("foo*"));
assertThat(indices, containsInAnyOrder("foo_foo", "foo_index"));
}
{
List<String> indices = resolver.resolve(indicesAndAliasesContext, Collections.singletonList("foo_alias"));
assertThat(indices, containsInAnyOrder("foo_alias"));
}
{
List<String> indices = resolver.resolve(skipAliasesLenientContext, Collections.singletonList("foo_alias"));
assertThat(indices, containsInAnyOrder("foo_alias"));
}
{
IllegalArgumentException iae = expectThrows(IllegalArgumentException.class, () -> resolver.resolve(skipAliasesStrictContext, Collections.singletonList("foo_alias")));
assertEquals("The provided expression [foo_alias] matches an alias, " + "specify the corresponding concrete indices instead.", iae.getMessage());
}
}
use of org.opensearch.index.IndexNotFoundException in project OpenSearch by opensearch-project.
the class IndexNameExpressionResolverTests method testIndexOptionsEmptyCluster.
public void testIndexOptionsEmptyCluster() {
ClusterState state = ClusterState.builder(new ClusterName("_name")).metadata(Metadata.builder().build()).build();
IndicesOptions options = IndicesOptions.strictExpandOpen();
final IndexNameExpressionResolver.Context context = new IndexNameExpressionResolver.Context(state, options, false);
String[] results = indexNameExpressionResolver.concreteIndexNames(context, Strings.EMPTY_ARRAY);
assertThat(results, emptyArray());
{
IndexNotFoundException infe = expectThrows(IndexNotFoundException.class, () -> indexNameExpressionResolver.concreteIndexNames(context, "foo"));
assertThat(infe.getIndex().getName(), equalTo("foo"));
}
results = indexNameExpressionResolver.concreteIndexNames(context, "foo*");
assertThat(results, emptyArray());
{
IndexNotFoundException infe = expectThrows(IndexNotFoundException.class, () -> indexNameExpressionResolver.concreteIndexNames(context, "foo*", "bar"));
assertThat(infe.getIndex().getName(), equalTo("bar"));
}
final IndexNameExpressionResolver.Context context2 = new IndexNameExpressionResolver.Context(state, IndicesOptions.lenientExpandOpen(), false);
results = indexNameExpressionResolver.concreteIndexNames(context2, Strings.EMPTY_ARRAY);
assertThat(results, emptyArray());
results = indexNameExpressionResolver.concreteIndexNames(context2, "foo");
assertThat(results, emptyArray());
results = indexNameExpressionResolver.concreteIndexNames(context2, "foo*");
assertThat(results, emptyArray());
results = indexNameExpressionResolver.concreteIndexNames(context2, "foo*", "bar");
assertThat(results, emptyArray());
final IndexNameExpressionResolver.Context context3 = new IndexNameExpressionResolver.Context(state, IndicesOptions.fromOptions(true, false, true, false), false);
IndexNotFoundException infe = expectThrows(IndexNotFoundException.class, () -> indexNameExpressionResolver.concreteIndexNames(context3, Strings.EMPTY_ARRAY));
assertThat(infe.getResourceId().toString(), equalTo("[_all]"));
}
use of org.opensearch.index.IndexNotFoundException in project OpenSearch by opensearch-project.
the class MetadataDeleteIndexServiceTests method testDeleteMissing.
public void testDeleteMissing() {
Index index = new Index("missing", "doesn't matter");
ClusterState state = ClusterState.builder(ClusterName.DEFAULT).build();
IndexNotFoundException e = expectThrows(IndexNotFoundException.class, () -> service.deleteIndices(state, singleton(index)));
assertEquals(index, e.getIndex());
}
use of org.opensearch.index.IndexNotFoundException in project anomaly-detection by opensearch-project.
the class AnomalyDetectionIndices method deleteIndexIteration.
private void deleteIndexIteration(String[] toDelete) {
for (String index : toDelete) {
DeleteIndexRequest singleDeleteRequest = new DeleteIndexRequest(index);
adminClient.indices().delete(singleDeleteRequest, ActionListener.wrap(singleDeleteResponse -> {
if (!singleDeleteResponse.isAcknowledged()) {
logger.error("Retrying deleting {} does not succeed.", index);
}
}, exception -> {
if (exception instanceof IndexNotFoundException) {
logger.info("{} was already deleted.", index);
} else {
logger.error(new ParameterizedMessage("Retrying deleting {} does not succeed.", index), exception);
}
}));
}
}
use of org.opensearch.index.IndexNotFoundException in project anomaly-detection by opensearch-project.
the class AnomalyDetectionIndices method updateJobIndexSettingIfNecessary.
private void updateJobIndexSettingIfNecessary(IndexState jobIndexState, ActionListener<Void> listener) {
GetSettingsRequest getSettingsRequest = new GetSettingsRequest().indices(ADIndex.JOB.getIndexName()).names(new String[] { IndexMetadata.SETTING_NUMBER_OF_SHARDS, IndexMetadata.SETTING_NUMBER_OF_REPLICAS, IndexMetadata.SETTING_AUTO_EXPAND_REPLICAS });
client.execute(GetSettingsAction.INSTANCE, getSettingsRequest, ActionListener.wrap(settingResponse -> {
// auto expand setting is a range string like "1-all"
String autoExpandReplica = getStringSetting(settingResponse, IndexMetadata.SETTING_AUTO_EXPAND_REPLICAS);
// if the auto expand setting is already there, return immediately
if (autoExpandReplica != null) {
jobIndexState.settingUpToDate = true;
logger.info(new ParameterizedMessage("Mark [{}]'s mapping up-to-date", ADIndex.JOB.getIndexName()));
listener.onResponse(null);
return;
}
Integer primaryShardsNumber = getIntegerSetting(settingResponse, IndexMetadata.SETTING_NUMBER_OF_SHARDS);
Integer replicaNumber = getIntegerSetting(settingResponse, IndexMetadata.SETTING_NUMBER_OF_REPLICAS);
if (primaryShardsNumber == null || replicaNumber == null) {
logger.error(new ParameterizedMessage("Fail to find AD job index's primary or replica shard number: primary [{}], replica [{}]", primaryShardsNumber, replicaNumber));
// don't throw exception as we don't know how to handle it and retry next time
listener.onResponse(null);
return;
}
// at least minJobIndexReplicas
// at most maxJobIndexReplicas / primaryShardsNumber replicas.
// For example, if we have 2 primary shards, since the max number of shards are maxJobIndexReplicas (20),
// we will use 20 / 2 = 10 replicas as the upper bound of replica.
int maxExpectedReplicas = Math.max(maxJobIndexReplicas / primaryShardsNumber, minJobIndexReplicas);
Settings updatedSettings = Settings.builder().put(IndexMetadata.SETTING_AUTO_EXPAND_REPLICAS, minJobIndexReplicas + "-" + maxExpectedReplicas).build();
final UpdateSettingsRequest updateSettingsRequest = new UpdateSettingsRequest(ADIndex.JOB.getIndexName()).settings(updatedSettings);
client.admin().indices().updateSettings(updateSettingsRequest, ActionListener.wrap(response -> {
jobIndexState.settingUpToDate = true;
logger.info(new ParameterizedMessage("Mark [{}]'s mapping up-to-date", ADIndex.JOB.getIndexName()));
listener.onResponse(null);
}, listener::onFailure));
}, e -> {
if (e instanceof IndexNotFoundException) {
// new index will be created with auto expand replica setting
jobIndexState.settingUpToDate = true;
logger.info(new ParameterizedMessage("Mark [{}]'s mapping up-to-date", ADIndex.JOB.getIndexName()));
listener.onResponse(null);
} else {
listener.onFailure(e);
}
}));
}
Aggregations