Skip to content

Commit

Permalink
[ML] fixes xcontent serialization for ML scaling reason (elastic#66548)
Browse files Browse the repository at this point in the history
Fixes MlScalingReason toxcontent serialization and adds test to catch regression.
  • Loading branch information
benwtrent committed Dec 17, 2020
1 parent d79ae0e commit c74b93b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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<String> waitingAnalyticsJobs = Collections.emptyList();
private List<String> waitingAnomalyJobs = Collections.emptyList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<MlScalingReason> {

@Override
Expand Down Expand Up @@ -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));
}
}

0 comments on commit c74b93b

Please sign in to comment.