|
38 | 38 | import org.elasticsearch.cluster.block.ClusterBlock;
|
39 | 39 | import org.elasticsearch.cluster.block.ClusterBlockException;
|
40 | 40 | import org.elasticsearch.cluster.metadata.IndexMetaData;
|
| 41 | +import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; |
41 | 42 | import org.elasticsearch.cluster.metadata.MappingMetaData;
|
42 | 43 | import org.elasticsearch.common.Nullable;
|
43 | 44 | import org.elasticsearch.common.Strings;
|
@@ -252,7 +253,25 @@ public void createJobResultIndex(Job job, ClusterState state, final ActionListen
|
252 | 253 |
|
253 | 254 | String readAliasName = AnomalyDetectorsIndex.jobResultsAliasedName(job.getId());
|
254 | 255 | 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; |
256 | 275 |
|
257 | 276 | final ActionListener<Boolean> createAliasListener = ActionListener.wrap(success -> {
|
258 | 277 | final IndicesAliasesRequest request = client.admin().indices().prepareAliases()
|
|
0 commit comments