Skip to content

Commit 0810eb4

Browse files
authored
ML allow aliased .ml-anomalies* index on PUT Job (#38821)
1 parent e754020 commit 0810eb4

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/JobResultsProvider.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.elasticsearch.cluster.block.ClusterBlock;
3939
import org.elasticsearch.cluster.block.ClusterBlockException;
4040
import org.elasticsearch.cluster.metadata.IndexMetaData;
41+
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
4142
import org.elasticsearch.cluster.metadata.MappingMetaData;
4243
import org.elasticsearch.common.Nullable;
4344
import org.elasticsearch.common.Strings;
@@ -252,7 +253,25 @@ public void createJobResultIndex(Job job, ClusterState state, final ActionListen
252253

253254
String readAliasName = AnomalyDetectorsIndex.jobResultsAliasedName(job.getId());
254255
String writeAliasName = AnomalyDetectorsIndex.resultsWriteAlias(job.getId());
255-
String indexName = job.getInitialResultsIndexName();
256+
String tempIndexName = job.getInitialResultsIndexName();
257+
258+
// Our read/write aliases should point to the concrete index
259+
// If the initial index is NOT an alias, either it is already a concrete index, or it does not exist yet
260+
if (state.getMetaData().hasAlias(tempIndexName)) {
261+
IndexNameExpressionResolver resolver = new IndexNameExpressionResolver();
262+
String[] concreteIndices = resolver.concreteIndexNames(state, IndicesOptions.lenientExpandOpen(), tempIndexName);
263+
264+
// SHOULD NOT be closed as in typical call flow checkForLeftOverDocuments already verified this
265+
// if it is closed, we bailout and return an error
266+
if (concreteIndices.length == 0) {
267+
finalListener.onFailure(
268+
ExceptionsHelper.badRequestException("Cannot create job [{}] as it requires closed index {}", job.getId(),
269+
tempIndexName));
270+
return;
271+
}
272+
tempIndexName = concreteIndices[0];
273+
}
274+
final String indexName = tempIndexName;
256275

257276
final ActionListener<Boolean> createAliasListener = ActionListener.wrap(success -> {
258277
final IndicesAliasesRequest request = client.admin().indices().prepareAliases()

0 commit comments

Comments
 (0)