Skip to content

Commit 7731316

Browse files
authored
[main] [ML] Use latest results index for new Anomaly Detection jobs (#122597) (#122673)
* [ML] Use latest results index for new Anomaly Detection jobs (#122597) After upgrading from v7 new anomaly detection jobs should use the latest results index if one has been created. * Delete 8.x test
1 parent e54037b commit 7731316

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125
import org.elasticsearch.xpack.core.ml.stats.ForecastStats;
126126
import org.elasticsearch.xpack.core.ml.stats.StatsAccumulator;
127127
import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper;
128+
import org.elasticsearch.xpack.core.ml.utils.MlIndexAndAlias;
128129
import org.elasticsearch.xpack.core.security.support.Exceptions;
129130
import org.elasticsearch.xpack.ml.MachineLearning;
130131
import org.elasticsearch.xpack.ml.job.categorization.GrokPatternCreator;
@@ -306,11 +307,15 @@ public void createJobResultIndex(Job job, ClusterState state, final ActionListen
306307
String readAliasName = AnomalyDetectorsIndex.jobResultsAliasedName(job.getId());
307308
String writeAliasName = AnomalyDetectorsIndex.resultsWriteAlias(job.getId());
308309
String tempIndexName = job.getInitialResultsIndexName();
310+
// Find all indices starting with this name and pick the latest one
311+
String[] concreteIndices = resolver.concreteIndexNames(state, IndicesOptions.lenientExpandOpen(), tempIndexName + "*");
312+
if (concreteIndices.length > 0) {
313+
tempIndexName = MlIndexAndAlias.latestIndex(concreteIndices);
314+
}
309315

310316
// Our read/write aliases should point to the concrete index
311317
// If the initial index is NOT an alias, either it is already a concrete index, or it does not exist yet
312318
if (state.getMetadata().getProject().hasAlias(tempIndexName)) {
313-
String[] concreteIndices = resolver.concreteIndexNames(state, IndicesOptions.lenientExpandOpen(), tempIndexName);
314319

315320
// SHOULD NOT be closed as in typical call flow checkForLeftOverDocuments already verified this
316321
// if it is closed, we bailout and return an error
@@ -324,8 +329,8 @@ public void createJobResultIndex(Job job, ClusterState state, final ActionListen
324329
);
325330
return;
326331
}
327-
tempIndexName = concreteIndices[0];
328332
}
333+
329334
final String indexName = tempIndexName;
330335

331336
ActionListener<Boolean> indexAndMappingsListener = ActionListener.wrap(success -> {

x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/MlJobSnapshotUpgradeIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ private Response buildAndPutJob(String jobId, TimeValue bucketSpan) throws Excep
279279
return client().performRequest(request);
280280
}
281281

282-
private static List<String> generateData(
282+
static List<String> generateData(
283283
long timestamp,
284284
TimeValue bucketSpan,
285285
int bucketCount,

x-pack/qa/src/main/java/org/elasticsearch/xpack/test/rest/XPackRestTestConstants.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,15 @@ public final class XPackRestTestConstants {
1717
public static final String[] TEMPLATE_NAMES_NO_ILM = new String[] { HISTORY_TEMPLATE_NAME_NO_ILM };
1818

1919
// ML constants:
20-
public static final String ML_META_INDEX_NAME = ".ml-meta";
21-
public static final String CONFIG_INDEX = ".ml-config";
2220
public static final String RESULTS_INDEX_PREFIX = ".ml-anomalies-";
2321
public static final String STATE_INDEX_PREFIX = ".ml-state";
24-
public static final String RESULTS_INDEX_DEFAULT = "shared";
2522

2623
public static final List<String> ML_POST_V7120_TEMPLATES = List.of(STATE_INDEX_PREFIX, RESULTS_INDEX_PREFIX);
2724

2825
// Transform constants:
2926
public static final String TRANSFORM_TASK_NAME = "data_frame/transforms";
3027
public static final String TRANSFORM_INTERNAL_INDEX_PREFIX = ".transform-internal-";
31-
public static final String TRANSFORM_NOTIFICATIONS_INDEX_PREFIX = ".transform-notifications-";
3228
public static final String TRANSFORM_INTERNAL_INDEX_PREFIX_DEPRECATED = ".data-frame-internal-";
33-
public static final String TRANSFORM_NOTIFICATIONS_INDEX_PREFIX_DEPRECATED = ".data-frame-notifications-";
3429

3530
private XPackRestTestConstants() {}
3631
}

0 commit comments

Comments
 (0)