From c74b93b09bb2c01300f578db8a3a4619261888a3 Mon Sep 17 00:00:00 2001 From: Benjamin Trent Date: Thu, 17 Dec 2020 14:35:43 -0500 Subject: [PATCH] [ML] fixes xcontent serialization for ML scaling reason (#66548) Fixes MlScalingReason toxcontent serialization and adds test to catch regression. --- .../xpack/ml/autoscaling/MlScalingReason.java | 7 ++++++- .../ml/autoscaling/MlScalingReasonTests.java | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/autoscaling/MlScalingReason.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/autoscaling/MlScalingReason.java index fdbf469f43b75..070bbace52a6d 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/autoscaling/MlScalingReason.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/autoscaling/MlScalingReason.java @@ -119,7 +119,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws builder.startObject(); builder.field(WAITING_ANALYTICS_JOBS, waitingAnalyticsJobs); builder.field(WAITING_ANOMALY_JOBS, waitingAnalyticsJobs); - builder.field(CONFIGURATION, passedConfiguration); + builder.startObject(CONFIGURATION).value(passedConfiguration).endObject(); if (largestWaitingAnalyticsJob != null) { builder.field(LARGEST_WAITING_ANALYTICS_JOB, largestWaitingAnalyticsJob); } @@ -132,6 +132,11 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws return builder; } + @Override + public boolean isFragment() { + return false; + } + static class Builder { private List waitingAnalyticsJobs = Collections.emptyList(); private List waitingAnomalyJobs = Collections.emptyList(); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/autoscaling/MlScalingReasonTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/autoscaling/MlScalingReasonTests.java index 40e2ba0b1e26e..030fe67bae934 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/autoscaling/MlScalingReasonTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/autoscaling/MlScalingReasonTests.java @@ -6,6 +6,7 @@ package org.elasticsearch.xpack.ml.autoscaling; +import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.settings.Settings; @@ -16,6 +17,13 @@ import java.util.stream.Collectors; import java.util.stream.Stream; +import static org.elasticsearch.xpack.ml.autoscaling.MlScalingReason.CONFIGURATION; +import static org.elasticsearch.xpack.ml.autoscaling.MlScalingReason.CURRENT_CAPACITY; +import static org.elasticsearch.xpack.ml.autoscaling.MlScalingReason.REASON; +import static org.elasticsearch.xpack.ml.autoscaling.MlScalingReason.WAITING_ANALYTICS_JOBS; +import static org.elasticsearch.xpack.ml.autoscaling.MlScalingReason.WAITING_ANOMALY_JOBS; +import static org.hamcrest.Matchers.containsString; + public class MlScalingReasonTests extends AbstractWireSerializingTestCase { @Override @@ -55,4 +63,13 @@ private static Settings randomConfiguration() { return builder.build(); } + public void testToXContent() throws Exception { + MlScalingReason reason = createTestInstance(); + String xcontentString = Strings.toString(reason); + assertThat(xcontentString, containsString(WAITING_ANALYTICS_JOBS)); + assertThat(xcontentString, containsString(WAITING_ANOMALY_JOBS)); + assertThat(xcontentString, containsString(CONFIGURATION)); + assertThat(xcontentString, containsString(CURRENT_CAPACITY)); + assertThat(xcontentString, containsString(REASON)); + } }