From 67ddbf1fac0c8d6824099da9fe309388de5fd5d8 Mon Sep 17 00:00:00 2001 From: David Roberts Date: Fri, 2 Jun 2017 16:38:18 +0100 Subject: [PATCH] [ML] Don't serialise the detector_index field to cluster state (elastic/x-pack-elasticsearch#1628) Because: 1. It's pointless, as new detector_index values are assigned when an analysis_config is parsed 2. It creates a backwards compatibility issue when upgrading from v5.4 Original commit: elastic/x-pack-elasticsearch@2f61aa457ea2cd181c00b80704adebf6c5e36f36 --- .../elasticsearch/xpack/ml/MlMetadata.java | 7 +++++-- .../xpack/ml/job/config/AnalysisConfig.java | 6 +++++- .../xpack/ml/job/config/Detector.java | 5 ++++- .../xpack/ml/utils/ToXContentParams.java | 21 +++++++++++++++++++ 4 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 plugin/src/main/java/org/elasticsearch/xpack/ml/utils/ToXContentParams.java diff --git a/plugin/src/main/java/org/elasticsearch/xpack/ml/MlMetadata.java b/plugin/src/main/java/org/elasticsearch/xpack/ml/MlMetadata.java index 37e4c35575e..f6bdcd0d2be 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/ml/MlMetadata.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/ml/MlMetadata.java @@ -31,6 +31,7 @@ import org.elasticsearch.xpack.ml.job.config.JobState; import org.elasticsearch.xpack.ml.job.config.JobTaskStatus; import org.elasticsearch.xpack.ml.job.messages.Messages; import org.elasticsearch.xpack.ml.utils.ExceptionsHelper; +import org.elasticsearch.xpack.ml.utils.ToXContentParams; import org.elasticsearch.xpack.persistent.PersistentTasksCustomMetaData; import org.elasticsearch.xpack.persistent.PersistentTasksCustomMetaData.PersistentTask; @@ -139,8 +140,10 @@ public class MlMetadata implements MetaData.Custom { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - mapValuesToXContent(JOBS_FIELD, jobs, builder, params); - mapValuesToXContent(DATAFEEDS_FIELD, datafeeds, builder, params); + DelegatingMapParams extendedParams = + new DelegatingMapParams(Collections.singletonMap(ToXContentParams.FOR_CLUSTER_STATE, "true"), params); + mapValuesToXContent(JOBS_FIELD, jobs, builder, extendedParams); + mapValuesToXContent(DATAFEEDS_FIELD, datafeeds, builder, extendedParams); return builder; } diff --git a/plugin/src/main/java/org/elasticsearch/xpack/ml/job/config/AnalysisConfig.java b/plugin/src/main/java/org/elasticsearch/xpack/ml/job/config/AnalysisConfig.java index 4d48c1dae36..1b70a9d9aed 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/ml/job/config/AnalysisConfig.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/ml/job/config/AnalysisConfig.java @@ -353,7 +353,11 @@ public class AnalysisConfig extends ToXContentToBytes implements Writeable { if (summaryCountFieldName != null) { builder.field(SUMMARY_COUNT_FIELD_NAME.getPreferredName(), summaryCountFieldName); } - builder.field(DETECTORS.getPreferredName(), detectors); + builder.startArray(DETECTORS.getPreferredName()); + for (Detector detector: detectors) { + detector.toXContent(builder, params); + } + builder.endArray(); builder.field(INFLUENCERS.getPreferredName(), influencers); if (overlappingBuckets != null) { builder.field(OVERLAPPING_BUCKETS.getPreferredName(), overlappingBuckets); diff --git a/plugin/src/main/java/org/elasticsearch/xpack/ml/job/config/Detector.java b/plugin/src/main/java/org/elasticsearch/xpack/ml/job/config/Detector.java index 708437cdddb..7b10f26f464 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/ml/job/config/Detector.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/ml/job/config/Detector.java @@ -18,6 +18,7 @@ import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.xpack.ml.job.messages.Messages; import org.elasticsearch.xpack.ml.utils.ExceptionsHelper; +import org.elasticsearch.xpack.ml.utils.ToXContentParams; import java.io.IOException; import java.util.ArrayList; @@ -382,7 +383,9 @@ public class Detector extends ToXContentToBytes implements Writeable { } builder.field(DETECTOR_RULES_FIELD.getPreferredName(), detectorRules); // negative means "unknown", which should only happen for a 5.4 job - if (detectorIndex >= 0) { + if (detectorIndex >= 0 + // no point writing this to cluster state, as the indexes will get reassigned on reload anyway + && params.paramAsBoolean(ToXContentParams.FOR_CLUSTER_STATE, false) == false) { builder.field(DETECTOR_INDEX.getPreferredName(), detectorIndex); } builder.endObject(); diff --git a/plugin/src/main/java/org/elasticsearch/xpack/ml/utils/ToXContentParams.java b/plugin/src/main/java/org/elasticsearch/xpack/ml/utils/ToXContentParams.java new file mode 100644 index 00000000000..ad31351a9f2 --- /dev/null +++ b/plugin/src/main/java/org/elasticsearch/xpack/ml/utils/ToXContentParams.java @@ -0,0 +1,21 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ +package org.elasticsearch.xpack.ml.utils; + + +/** + * Parameters used by machine learning for controlling X Content serialisation. + */ +public final class ToXContentParams { + + /** + * Parameter to indicate whether we are serialising to X Content for cluster state output. + */ + public static final String FOR_CLUSTER_STATE = "for_cluster_state"; + + private ToXContentParams() { + } +}