diff --git a/plugin/src/main/java/org/elasticsearch/xpack/ml/action/GetModelSnapshotsAction.java b/plugin/src/main/java/org/elasticsearch/xpack/ml/action/GetModelSnapshotsAction.java index 5327022353d..b52eef0d7aa 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/ml/action/GetModelSnapshotsAction.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/ml/action/GetModelSnapshotsAction.java @@ -38,6 +38,7 @@ import org.elasticsearch.xpack.ml.utils.ExceptionsHelper; import java.io.IOException; import java.util.Objects; +import java.util.stream.Collectors; public class GetModelSnapshotsAction extends Action { @@ -333,18 +334,17 @@ extends Action { - clearQuantiles(page); - listener.onResponse(new Response(page)); + listener.onResponse(new Response(clearQuantiles(page))); }, listener::onFailure); } - public static void clearQuantiles(QueryPage page) { - if (page.results() != null) { - for (ModelSnapshot modelSnapshot : page.results()) { - modelSnapshot.setQuantiles(null); - } + public static QueryPage clearQuantiles(QueryPage page) { + if (page.results() == null) { + return page; } + return new QueryPage<>(page.results().stream().map(snapshot -> + new ModelSnapshot.Builder(snapshot).setQuantiles(null).build()) + .collect(Collectors.toList()), page.count(), page.getResultsField()); } } - } diff --git a/plugin/src/main/java/org/elasticsearch/xpack/ml/action/RevertModelSnapshotAction.java b/plugin/src/main/java/org/elasticsearch/xpack/ml/action/RevertModelSnapshotAction.java index 59590cee6a6..298b96cda8a 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/ml/action/RevertModelSnapshotAction.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/ml/action/RevertModelSnapshotAction.java @@ -48,7 +48,6 @@ import org.elasticsearch.xpack.ml.utils.ExceptionsHelper; import java.io.IOException; import java.util.Date; -import java.util.List; import java.util.Objects; import java.util.function.Consumer; @@ -312,8 +311,7 @@ extends Action { logger.warn("More than one model found for [{}: {}, {}: {}] tuple.", Job.ID.getPreferredName(), request.getJobId(), ModelSnapshot.SNAPSHOT_ID.getPreferredName(), request.getSnapshotId()); } - ModelSnapshot modelSnapshot = changeCandidates.get(0); - modelSnapshot.setDescription(request.getDescriptionString()); - jobManager.updateModelSnapshot(modelSnapshot, b -> { - modelSnapshot.setDescription(request.getDescriptionString()); + ModelSnapshot.Builder updatedSnapshotBuilder = new ModelSnapshot.Builder(changeCandidates.get(0)); + updatedSnapshotBuilder.setDescription(request.getDescriptionString()); + ModelSnapshot updatedSnapshot = updatedSnapshotBuilder.build(); + jobManager.updateModelSnapshot(updatedSnapshot, b -> { // The quantiles can be large, and totally dominate the output - // it's clearer to remove them - modelSnapshot.setQuantiles(null); - listener.onResponse(new Response(modelSnapshot)); + listener.onResponse(new Response(new ModelSnapshot.Builder(updatedSnapshot).setQuantiles(null).build())); }, listener::onFailure); }, listener::onFailure); }, listener::onFailure); diff --git a/plugin/src/main/java/org/elasticsearch/xpack/ml/action/util/QueryPage.java b/plugin/src/main/java/org/elasticsearch/xpack/ml/action/util/QueryPage.java index 3a18aea828a..107829ab9c9 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/ml/action/util/QueryPage.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/ml/action/util/QueryPage.java @@ -80,6 +80,10 @@ public final class QueryPage extends ToXConten return count; } + public ParseField getResultsField() { + return resultsField; + } + @Override public int hashCode() { return Objects.hash(results, count); diff --git a/plugin/src/main/java/org/elasticsearch/xpack/ml/job/persistence/JobProvider.java b/plugin/src/main/java/org/elasticsearch/xpack/ml/job/persistence/JobProvider.java index dda228af06b..e7d77df9f07 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/ml/job/persistence/JobProvider.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/ml/job/persistence/JobProvider.java @@ -235,11 +235,11 @@ public class JobProvider { */ public void dataCounts(String jobId, Consumer handler, Consumer errorHandler) { String indexName = AnomalyDetectorsIndex.jobResultsAliasedName(jobId); - get(jobId, indexName, DataCounts.TYPE.getPreferredName(), DataCounts.documentId(jobId), handler, errorHandler, + get(indexName, DataCounts.TYPE.getPreferredName(), DataCounts.documentId(jobId), handler, errorHandler, DataCounts.PARSER, () -> new DataCounts(jobId)); } - private void get(String jobId, String indexName, String type, String id, Consumer handler, Consumer errorHandler, + private void get(String indexName, String type, String id, Consumer handler, Consumer errorHandler, BiFunction objectParser, Supplier notFoundSupplier) { GetRequest getRequest = new GetRequest(indexName, type, id); client.get(getRequest, ActionListener.wrap( @@ -758,7 +758,7 @@ public class JobProvider { String indexName = AnomalyDetectorsIndex.jobStateIndexName(); String quantilesId = Quantiles.documentId(jobId); LOGGER.trace("ES API CALL: get ID {} type {} from index {}", quantilesId, Quantiles.TYPE.getPreferredName(), indexName); - get(jobId, indexName, Quantiles.TYPE.getPreferredName(), quantilesId, handler, errorHandler, Quantiles.PARSER, () -> { + get(indexName, Quantiles.TYPE.getPreferredName(), quantilesId, handler, errorHandler, Quantiles.PARSER, () -> { LOGGER.info("There are currently no quantiles for job " + jobId); return null; }); @@ -773,8 +773,9 @@ public class JobProvider { handler.accept(null); return; } - get(jobId, AnomalyDetectorsIndex.jobResultsAliasedName(jobId), ModelSnapshot.TYPE.getPreferredName(), - ModelSnapshot.documentId(jobId, modelSnapshotId), handler, errorHandler, ModelSnapshot.PARSER, () -> null); + get(AnomalyDetectorsIndex.jobResultsAliasedName(jobId), ModelSnapshot.TYPE.getPreferredName(), + ModelSnapshot.documentId(jobId, modelSnapshotId), handler, errorHandler, + (parser, context) -> ModelSnapshot.PARSER.apply(parser, null).build(), () -> null); } /** @@ -975,7 +976,7 @@ public class JobProvider { ModelSizeStats.RESULT_TYPE_VALUE, ModelSizeStats.RESULT_TYPE_FIELD, jobId); String indexName = AnomalyDetectorsIndex.jobResultsAliasedName(jobId); - get(jobId, indexName, Result.TYPE.getPreferredName(), ModelSizeStats.documentId(jobId), + get(indexName, Result.TYPE.getPreferredName(), ModelSizeStats.documentId(jobId), handler, errorHandler, (parser, context) -> ModelSizeStats.PARSER.apply(parser, null).build(), () -> { LOGGER.trace("No memory usage details for job with id {}", jobId); diff --git a/plugin/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/state/ModelSizeStats.java b/plugin/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/state/ModelSizeStats.java index 1721e490cff..003cb28e014 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/state/ModelSizeStats.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/state/ModelSizeStats.java @@ -146,8 +146,8 @@ public class ModelSizeStats extends ToXContentToBytes implements Writeable { totalPartitionFieldCount = in.readVLong(); bucketAllocationFailuresCount = in.readVLong(); memoryStatus = MemoryStatus.readFromStream(in); - logTime = new Date(in.readLong()); - timestamp = in.readBoolean() ? new Date(in.readLong()) : null; + logTime = new Date(in.readVLong()); + timestamp = in.readBoolean() ? new Date(in.readVLong()) : null; } @Override @@ -159,11 +159,11 @@ public class ModelSizeStats extends ToXContentToBytes implements Writeable { out.writeVLong(totalPartitionFieldCount); out.writeVLong(bucketAllocationFailuresCount); memoryStatus.writeTo(out); - out.writeLong(logTime.getTime()); + out.writeVLong(logTime.getTime()); boolean hasTimestamp = timestamp != null; out.writeBoolean(hasTimestamp); if (hasTimestamp) { - out.writeLong(timestamp.getTime()); + out.writeVLong(timestamp.getTime()); } } diff --git a/plugin/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/state/ModelSnapshot.java b/plugin/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/state/ModelSnapshot.java index e498a421c96..61845f42748 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/state/ModelSnapshot.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/state/ModelSnapshot.java @@ -12,8 +12,8 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.common.xcontent.ConstructingObjectParser; import org.elasticsearch.common.xcontent.NamedXContentRegistry; +import org.elasticsearch.common.xcontent.ObjectParser; import org.elasticsearch.common.xcontent.ObjectParser.ValueType; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; @@ -49,12 +49,11 @@ public class ModelSnapshot extends ToXContentToBytes implements Writeable { */ public static final ParseField TYPE = new ParseField("model_snapshot"); - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>(TYPE.getPreferredName(), a -> new ModelSnapshot((String) a[0])); + public static final ObjectParser PARSER = new ObjectParser<>(TYPE.getPreferredName(), Builder::new); static { - PARSER.declareString(ConstructingObjectParser.constructorArg(), Job.ID); - PARSER.declareField(ModelSnapshot::setTimestamp, p -> { + PARSER.declareString(Builder::setJobId, Job.ID); + PARSER.declareField(Builder::setTimestamp, p -> { if (p.currentToken() == Token.VALUE_NUMBER) { return new Date(p.longValue()); } else if (p.currentToken() == Token.VALUE_STRING) { @@ -62,11 +61,11 @@ public class ModelSnapshot extends ToXContentToBytes implements Writeable { } throw new IllegalArgumentException("unexpected token [" + p.currentToken() + "] for [" + TIMESTAMP.getPreferredName() + "]"); }, TIMESTAMP, ValueType.VALUE); - PARSER.declareString(ModelSnapshot::setDescription, DESCRIPTION); - PARSER.declareString(ModelSnapshot::setSnapshotId, SNAPSHOT_ID); - PARSER.declareInt(ModelSnapshot::setSnapshotDocCount, SNAPSHOT_DOC_COUNT); - PARSER.declareObject(ModelSnapshot::setModelSizeStats, ModelSizeStats.PARSER, ModelSizeStats.RESULT_TYPE_FIELD); - PARSER.declareField(ModelSnapshot::setLatestRecordTimeStamp, p -> { + PARSER.declareString(Builder::setDescription, DESCRIPTION); + PARSER.declareString(Builder::setSnapshotId, SNAPSHOT_ID); + PARSER.declareInt(Builder::setSnapshotDocCount, SNAPSHOT_DOC_COUNT); + PARSER.declareObject(Builder::setModelSizeStats, ModelSizeStats.PARSER, ModelSizeStats.RESULT_TYPE_FIELD); + PARSER.declareField(Builder::setLatestRecordTimeStamp, p -> { if (p.currentToken() == Token.VALUE_NUMBER) { return new Date(p.longValue()); } else if (p.currentToken() == Token.VALUE_STRING) { @@ -75,7 +74,7 @@ public class ModelSnapshot extends ToXContentToBytes implements Writeable { throw new IllegalArgumentException( "unexpected token [" + p.currentToken() + "] for [" + LATEST_RECORD_TIME.getPreferredName() + "]"); }, LATEST_RECORD_TIME, ValueType.VALUE); - PARSER.declareField(ModelSnapshot::setLatestResultTimeStamp, p -> { + PARSER.declareField(Builder::setLatestResultTimeStamp, p -> { if (p.currentToken() == Token.VALUE_NUMBER) { return new Date(p.longValue()); } else if (p.currentToken() == Token.VALUE_STRING) { @@ -84,76 +83,70 @@ public class ModelSnapshot extends ToXContentToBytes implements Writeable { throw new IllegalArgumentException( "unexpected token [" + p.currentToken() + "] for [" + LATEST_RESULT_TIME.getPreferredName() + "]"); }, LATEST_RESULT_TIME, ValueType.VALUE); - PARSER.declareObject(ModelSnapshot::setQuantiles, Quantiles.PARSER, Quantiles.TYPE); + PARSER.declareObject(Builder::setQuantiles, Quantiles.PARSER, Quantiles.TYPE); } private final String jobId; - private Date timestamp; - private String description; - private String snapshotId; - private int snapshotDocCount; - private ModelSizeStats modelSizeStats; - private Date latestRecordTimeStamp; - private Date latestResultTimeStamp; - private Quantiles quantiles; + private final Date timestamp; + private final String description; + private final String snapshotId; + private final int snapshotDocCount; + private final ModelSizeStats modelSizeStats; + private final Date latestRecordTimeStamp; + private final Date latestResultTimeStamp; + private final Quantiles quantiles; - public ModelSnapshot(String jobId) { + private ModelSnapshot(String jobId, Date timestamp, String description, String snapshotId, int snapshotDocCount, + ModelSizeStats modelSizeStats, Date latestRecordTimeStamp, Date latestResultTimeStamp, Quantiles quantiles) { this.jobId = jobId; + this.timestamp = timestamp; + this.description = description; + this.snapshotId = snapshotId; + this.snapshotDocCount = snapshotDocCount; + this.modelSizeStats = modelSizeStats; + this.latestRecordTimeStamp = latestRecordTimeStamp; + this.latestResultTimeStamp = latestResultTimeStamp; + this.quantiles = quantiles; } public ModelSnapshot(StreamInput in) throws IOException { jobId = in.readString(); - if (in.readBoolean()) { - timestamp = new Date(in.readLong()); - } + timestamp = in.readBoolean() ? new Date(in.readVLong()) : null; description = in.readOptionalString(); snapshotId = in.readOptionalString(); snapshotDocCount = in.readInt(); - if (in.readBoolean()) { - modelSizeStats = new ModelSizeStats(in); - } - if (in.readBoolean()) { - latestRecordTimeStamp = new Date(in.readLong()); - } - if (in.readBoolean()) { - latestResultTimeStamp = new Date(in.readLong()); - } - if (in.readBoolean()) { - quantiles = new Quantiles(in); - } + modelSizeStats = in.readOptionalWriteable(ModelSizeStats::new); + latestRecordTimeStamp = in.readBoolean() ? new Date(in.readVLong()) : null; + latestResultTimeStamp = in.readBoolean() ? new Date(in.readVLong()) : null; + quantiles = in.readOptionalWriteable(Quantiles::new); } @Override public void writeTo(StreamOutput out) throws IOException { out.writeString(jobId); - boolean hasTimestamp = timestamp != null; - out.writeBoolean(hasTimestamp); - if (hasTimestamp) { - out.writeLong(timestamp.getTime()); + if (timestamp != null) { + out.writeBoolean(true); + out.writeVLong(timestamp.getTime()); + } else { + out.writeBoolean(false); } out.writeOptionalString(description); out.writeOptionalString(snapshotId); out.writeInt(snapshotDocCount); - boolean hasModelSizeStats = modelSizeStats != null; - out.writeBoolean(hasModelSizeStats); - if (hasModelSizeStats) { - modelSizeStats.writeTo(out); + out.writeOptionalWriteable(modelSizeStats); + if (latestRecordTimeStamp != null) { + out.writeBoolean(true); + out.writeVLong(latestRecordTimeStamp.getTime()); + } else { + out.writeBoolean(false); } - boolean hasLatestRecordTimeStamp = latestRecordTimeStamp != null; - out.writeBoolean(hasLatestRecordTimeStamp); - if (hasLatestRecordTimeStamp) { - out.writeLong(latestRecordTimeStamp.getTime()); - } - boolean hasLatestResultTimeStamp = latestResultTimeStamp != null; - out.writeBoolean(hasLatestResultTimeStamp); - if (hasLatestResultTimeStamp) { - out.writeLong(latestResultTimeStamp.getTime()); - } - boolean hasQuantiles = quantiles != null; - out.writeBoolean(hasQuantiles); - if (hasQuantiles) { - quantiles.writeTo(out); + if (latestResultTimeStamp != null) { + out.writeBoolean(true); + out.writeVLong(latestResultTimeStamp.getTime()); + } else { + out.writeBoolean(false); } + out.writeOptionalWriteable(quantiles); } @Override @@ -196,66 +189,34 @@ public class ModelSnapshot extends ToXContentToBytes implements Writeable { return timestamp; } - public void setTimestamp(Date timestamp) { - this.timestamp = timestamp; - } - public String getDescription() { return description; } - public void setDescription(String description) { - this.description = description; - } - public String getSnapshotId() { return snapshotId; } - public void setSnapshotId(String snapshotId) { - this.snapshotId = snapshotId; - } - public int getSnapshotDocCount() { return snapshotDocCount; } - public void setSnapshotDocCount(int snapshotDocCount) { - this.snapshotDocCount = snapshotDocCount; - } - public ModelSizeStats getModelSizeStats() { return modelSizeStats; } - public void setModelSizeStats(ModelSizeStats.Builder modelSizeStats) { - this.modelSizeStats = modelSizeStats.build(); - } - public Quantiles getQuantiles() { return quantiles; } - public void setQuantiles(Quantiles q) { - quantiles = q; - } - public Date getLatestRecordTimeStamp() { return latestRecordTimeStamp; } - public void setLatestRecordTimeStamp(Date latestRecordTimeStamp) { - this.latestRecordTimeStamp = latestRecordTimeStamp; - } - public Date getLatestResultTimeStamp() { return latestResultTimeStamp; } - public void setLatestResultTimeStamp(Date latestResultTimeStamp) { - this.latestResultTimeStamp = latestResultTimeStamp; - } - @Override public int hashCode() { return Objects.hash(jobId, timestamp, description, snapshotId, quantiles, snapshotDocCount, modelSizeStats, latestRecordTimeStamp, @@ -298,9 +259,96 @@ public class ModelSnapshot extends ToXContentToBytes implements Writeable { public static ModelSnapshot fromJson(BytesReference bytesReference) { try (XContentParser parser = XContentFactory.xContent(bytesReference).createParser(NamedXContentRegistry.EMPTY, bytesReference)) { - return PARSER.apply(parser, null); + return PARSER.apply(parser, null).build(); } catch (IOException e) { throw new ElasticsearchParseException("failed to parse modelSnapshot", e); } } + + public static class Builder { + private String jobId; + private Date timestamp; + private String description; + private String snapshotId; + private int snapshotDocCount; + private ModelSizeStats modelSizeStats; + private Date latestRecordTimeStamp; + private Date latestResultTimeStamp; + private Quantiles quantiles; + + public Builder() { + } + + public Builder(String jobId) { + this(); + this.jobId = jobId; + } + + public Builder(ModelSnapshot modelSnapshot) { + this.jobId = modelSnapshot.jobId; + this.timestamp = modelSnapshot.timestamp; + this.description = modelSnapshot.description; + this.snapshotId = modelSnapshot.snapshotId; + this.snapshotDocCount = modelSnapshot.snapshotDocCount; + this.modelSizeStats = modelSnapshot.modelSizeStats; + this.latestRecordTimeStamp = modelSnapshot.latestRecordTimeStamp; + this.latestResultTimeStamp = modelSnapshot.latestResultTimeStamp; + this.quantiles = modelSnapshot.quantiles; + } + + public Builder setJobId(String jobId) { + this.jobId = jobId; + return this; + } + + public Builder setTimestamp(Date timestamp) { + this.timestamp = timestamp; + return this; + } + + public Builder setDescription(String description) { + this.description = description; + return this; + } + + public Builder setSnapshotId(String snapshotId) { + this.snapshotId = snapshotId; + return this; + } + + public Builder setSnapshotDocCount(int snapshotDocCount) { + this.snapshotDocCount = snapshotDocCount; + return this; + } + + public Builder setModelSizeStats(ModelSizeStats.Builder modelSizeStats) { + this.modelSizeStats = modelSizeStats.build(); + return this; + } + + public Builder setModelSizeStats(ModelSizeStats modelSizeStats) { + this.modelSizeStats = modelSizeStats; + return this; + } + + public Builder setLatestRecordTimeStamp(Date latestRecordTimeStamp) { + this.latestRecordTimeStamp = latestRecordTimeStamp; + return this; + } + + public Builder setLatestResultTimeStamp(Date latestResultTimeStamp) { + this.latestResultTimeStamp = latestResultTimeStamp; + return this; + } + + public Builder setQuantiles(Quantiles quantiles) { + this.quantiles = quantiles; + return this; + } + + public ModelSnapshot build() { + return new ModelSnapshot(jobId, timestamp, description, snapshotId, snapshotDocCount, modelSizeStats, latestRecordTimeStamp, + latestResultTimeStamp, quantiles); + } + } } diff --git a/plugin/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/state/Quantiles.java b/plugin/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/state/Quantiles.java index ae3b9cf2efe..561ec7c94e6 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/state/Quantiles.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/state/Quantiles.java @@ -60,14 +60,14 @@ public class Quantiles extends ToXContentToBytes implements Writeable { public Quantiles(StreamInput in) throws IOException { jobId = in.readString(); - timestamp = new Date(in.readLong()); + timestamp = new Date(in.readVLong()); quantileState = in.readOptionalString(); } @Override public void writeTo(StreamOutput out) throws IOException { out.writeString(jobId); - out.writeLong(timestamp.getTime()); + out.writeVLong(timestamp.getTime()); out.writeOptionalString(quantileState); } diff --git a/plugin/src/main/java/org/elasticsearch/xpack/ml/job/results/AutodetectResult.java b/plugin/src/main/java/org/elasticsearch/xpack/ml/job/results/AutodetectResult.java index 5257b910f28..c93be7f968b 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/ml/job/results/AutodetectResult.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/ml/job/results/AutodetectResult.java @@ -13,9 +13,9 @@ import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.xcontent.ConstructingObjectParser; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.xpack.ml.job.process.autodetect.output.FlushAcknowledgement; import org.elasticsearch.xpack.ml.job.process.autodetect.state.ModelSizeStats; import org.elasticsearch.xpack.ml.job.process.autodetect.state.ModelSnapshot; -import org.elasticsearch.xpack.ml.job.process.autodetect.output.FlushAcknowledgement; import org.elasticsearch.xpack.ml.job.process.autodetect.state.Quantiles; import java.io.IOException; @@ -29,7 +29,8 @@ public class AutodetectResult extends ToXContentToBytes implements Writeable { @SuppressWarnings("unchecked") public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( TYPE.getPreferredName(), a -> new AutodetectResult((Bucket) a[0], (List) a[1], (List) a[2], - (Quantiles) a[3], (ModelSnapshot) a[4], a[5] == null ? null : ((ModelSizeStats.Builder) a[5]).build(), + (Quantiles) a[3], a[4] == null ? null : ((ModelSnapshot.Builder) a[4]).build(), + a[5] == null ? null : ((ModelSizeStats.Builder) a[5]).build(), (ModelPlot) a[6], (CategoryDefinition) a[7], (FlushAcknowledgement) a[8])); static { diff --git a/plugin/src/main/java/org/elasticsearch/xpack/ml/utils/time/TimeUtils.java b/plugin/src/main/java/org/elasticsearch/xpack/ml/utils/time/TimeUtils.java index c7482048a17..b7471d308da 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/ml/utils/time/TimeUtils.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/ml/utils/time/TimeUtils.java @@ -8,10 +8,8 @@ package org.elasticsearch.xpack.ml.utils.time; import org.elasticsearch.common.ParseField; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.index.mapper.DateFieldMapper; -import org.elasticsearch.xpack.ml.job.messages.Messages; import java.util.concurrent.TimeUnit; -import java.util.function.Predicate; public final class TimeUtils { private TimeUtils() { diff --git a/plugin/src/test/java/org/elasticsearch/xpack/ml/action/GetModelSnapshotsActionResponseTests.java b/plugin/src/test/java/org/elasticsearch/xpack/ml/action/GetModelSnapshotsActionResponseTests.java index 9aeec617eeb..e19785bdebf 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/ml/action/GetModelSnapshotsActionResponseTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/ml/action/GetModelSnapshotsActionResponseTests.java @@ -6,8 +6,9 @@ package org.elasticsearch.xpack.ml.action; import org.elasticsearch.xpack.ml.action.GetModelSnapshotsAction.Response; -import org.elasticsearch.xpack.ml.job.process.autodetect.state.ModelSnapshot; import org.elasticsearch.xpack.ml.action.util.QueryPage; +import org.elasticsearch.xpack.ml.job.process.autodetect.state.ModelSnapshot; +import org.elasticsearch.xpack.ml.job.process.autodetect.state.ModelSnapshotTests; import org.elasticsearch.xpack.ml.support.AbstractStreamableTestCase; import java.util.ArrayList; @@ -20,9 +21,7 @@ public class GetModelSnapshotsActionResponseTests extends AbstractStreamableTest int listSize = randomInt(10); List hits = new ArrayList<>(listSize); for (int j = 0; j < listSize; j++) { - ModelSnapshot snapshot = new ModelSnapshot(randomAsciiOfLengthBetween(1, 20)); - snapshot.setDescription(randomAsciiOfLengthBetween(1, 20)); - hits.add(snapshot); + hits.add(ModelSnapshotTests.createRandomized()); } QueryPage snapshots = new QueryPage<>(hits, listSize, ModelSnapshot.RESULTS_FIELD); return new Response(snapshots); @@ -32,5 +31,4 @@ public class GetModelSnapshotsActionResponseTests extends AbstractStreamableTest protected Response createBlankInstance() { return new Response(); } - } diff --git a/plugin/src/test/java/org/elasticsearch/xpack/ml/action/PutModelSnapshotDescriptionActionResponseTests.java b/plugin/src/test/java/org/elasticsearch/xpack/ml/action/PutModelSnapshotDescriptionActionResponseTests.java index 883568bce1f..2d55d00bb8a 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/ml/action/PutModelSnapshotDescriptionActionResponseTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/ml/action/PutModelSnapshotDescriptionActionResponseTests.java @@ -6,21 +6,18 @@ package org.elasticsearch.xpack.ml.action; import org.elasticsearch.xpack.ml.action.UpdateModelSnapshotAction.Response; -import org.elasticsearch.xpack.ml.job.process.autodetect.state.ModelSnapshot; +import org.elasticsearch.xpack.ml.job.process.autodetect.state.ModelSnapshotTests; import org.elasticsearch.xpack.ml.support.AbstractStreamableTestCase; public class PutModelSnapshotDescriptionActionResponseTests extends AbstractStreamableTestCase { @Override protected Response createTestInstance() { - ModelSnapshot snapshot = new ModelSnapshot(randomAsciiOfLengthBetween(1, 20)); - snapshot.setDescription(randomAsciiOfLengthBetween(1, 20)); - return new Response(snapshot); + return new Response(ModelSnapshotTests.createRandomized()); } @Override protected Response createBlankInstance() { return new Response(); } - } diff --git a/plugin/src/test/java/org/elasticsearch/xpack/ml/action/RevertModelSnapshotActionResponseTests.java b/plugin/src/test/java/org/elasticsearch/xpack/ml/action/RevertModelSnapshotActionResponseTests.java index 4da4214de3f..5838979eeb3 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/ml/action/RevertModelSnapshotActionResponseTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/ml/action/RevertModelSnapshotActionResponseTests.java @@ -6,16 +6,14 @@ package org.elasticsearch.xpack.ml.action; import org.elasticsearch.xpack.ml.action.RevertModelSnapshotAction.Response; -import org.elasticsearch.xpack.ml.job.process.autodetect.state.ModelSnapshot; +import org.elasticsearch.xpack.ml.job.process.autodetect.state.ModelSnapshotTests; import org.elasticsearch.xpack.ml.support.AbstractStreamableTestCase; public class RevertModelSnapshotActionResponseTests extends AbstractStreamableTestCase { @Override protected Response createTestInstance() { - ModelSnapshot modelSnapshot = new ModelSnapshot(randomAsciiOfLengthBetween(1, 20)); - modelSnapshot.setDescription(randomAsciiOfLengthBetween(1, 20)); - return new RevertModelSnapshotAction.Response(modelSnapshot); + return new RevertModelSnapshotAction.Response(ModelSnapshotTests.createRandomized()); } @Override diff --git a/plugin/src/test/java/org/elasticsearch/xpack/ml/integration/AutodetectResultProcessorIT.java b/plugin/src/test/java/org/elasticsearch/xpack/ml/integration/AutodetectResultProcessorIT.java index dd6c7e6aa41..60aad2e87af 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/ml/integration/AutodetectResultProcessorIT.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/ml/integration/AutodetectResultProcessorIT.java @@ -53,7 +53,6 @@ import java.util.Optional; import java.util.Set; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; -import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; import static org.mockito.Matchers.any; @@ -305,9 +304,7 @@ public class AutodetectResultProcessorIT extends ESSingleNodeTestCase { } private ModelSnapshot createModelSnapshot() { - ModelSnapshot snapshot = new ModelSnapshot(JOB_ID); - snapshot.setSnapshotId(randomAsciiOfLength(12)); - return snapshot; + return new ModelSnapshot.Builder(JOB_ID).setSnapshotId(randomAsciiOfLength(12)).build(); } private Quantiles createQuantiles() { diff --git a/plugin/src/test/java/org/elasticsearch/xpack/ml/job/persistence/JobDataDeleterTests.java b/plugin/src/test/java/org/elasticsearch/xpack/ml/job/persistence/JobDataDeleterTests.java index eaf24f349d0..ac6d8976715 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/ml/job/persistence/JobDataDeleterTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/ml/job/persistence/JobDataDeleterTests.java @@ -80,9 +80,8 @@ public class JobDataDeleterTests extends ESTestCase { public void testDeleteModelSnapShot() { String jobId = "foo"; - ModelSnapshot snapshot = new ModelSnapshot(jobId); - snapshot.setSnapshotDocCount(5); - snapshot.setSnapshotId("snap-1"); + ModelSnapshot snapshot = new ModelSnapshot.Builder(jobId).setSnapshotDocCount(5) + .setSnapshotId("snap-1").build(); BulkResponse bulkResponse = Mockito.mock(BulkResponse.class); Client client = new MockClientBuilder("myCluster").prepareBulk(bulkResponse).build(); diff --git a/plugin/src/test/java/org/elasticsearch/xpack/ml/job/persistence/JobProviderTests.java b/plugin/src/test/java/org/elasticsearch/xpack/ml/job/persistence/JobProviderTests.java index c9fd145b329..be6a8d57aa2 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/ml/job/persistence/JobProviderTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/ml/job/persistence/JobProviderTests.java @@ -974,9 +974,7 @@ public class JobProviderTests extends ESTestCase { JobProvider provider = createProvider(clientBuilder.build()); - ModelSnapshot modelSnapshot = new ModelSnapshot(JOB_ID); - modelSnapshot.setSnapshotId("123"); - modelSnapshot.setSnapshotDocCount(2); + ModelSnapshot modelSnapshot = new ModelSnapshot.Builder(JOB_ID).setSnapshotId("123").setSnapshotDocCount(2).build(); ByteArrayOutputStream stream = new ByteArrayOutputStream(); diff --git a/plugin/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/AutodetectProcessManagerTests.java b/plugin/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/AutodetectProcessManagerTests.java index 031f4a188c3..69d2358fdbb 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/AutodetectProcessManagerTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/AutodetectProcessManagerTests.java @@ -80,7 +80,7 @@ public class AutodetectProcessManagerTests extends ESTestCase { private NormalizerFactory normalizerFactory; private DataCounts dataCounts = new DataCounts("foo"); - private ModelSnapshot modelSnapshot = new ModelSnapshot("foo"); + private ModelSnapshot modelSnapshot = new ModelSnapshot.Builder("foo").build(); private Quantiles quantiles = new Quantiles("foo", new Date(), "state"); private Set filters = new HashSet<>(); @@ -164,7 +164,7 @@ public class AutodetectProcessManagerTests extends ESTestCase { jobResultsPersister, jobDataCountsPersister, autodetectProcessFactory, normalizerFactory)); DataCounts dataCounts = new DataCounts("foo"); - ModelSnapshot modelSnapshot = new ModelSnapshot("foo"); + ModelSnapshot modelSnapshot = new ModelSnapshot.Builder("foo").build(); Quantiles quantiles = new Quantiles("foo", new Date(), "state"); Set filters = new HashSet<>(); doAnswer(invocationOnMock -> { diff --git a/plugin/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/output/AutoDetectResultProcessorTests.java b/plugin/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/output/AutoDetectResultProcessorTests.java index ccb143168d9..e169ccbf6c8 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/output/AutoDetectResultProcessorTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/output/AutoDetectResultProcessorTests.java @@ -262,8 +262,8 @@ public class AutoDetectResultProcessorTests extends ESTestCase { AutoDetectResultProcessor.Context context = new AutoDetectResultProcessor.Context(JOB_ID, false, bulkBuilder); context.deleteInterimRequired = false; AutodetectResult result = mock(AutodetectResult.class); - ModelSnapshot modelSnapshot = new ModelSnapshot(JOB_ID); - modelSnapshot.setSnapshotId("a_snapshot_id"); + ModelSnapshot modelSnapshot = new ModelSnapshot.Builder(JOB_ID) + .setSnapshotId("a_snapshot_id").build(); when(result.getModelSnapshot()).thenReturn(modelSnapshot); processorUnderTest.processResult(context, result); diff --git a/plugin/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/state/ModelSizeStatsTests.java b/plugin/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/state/ModelSizeStatsTests.java index 319b6352f8a..1e45d00c7bb 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/state/ModelSizeStatsTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/state/ModelSizeStatsTests.java @@ -6,6 +6,7 @@ package org.elasticsearch.xpack.ml.job.process.autodetect.state; import org.elasticsearch.common.io.stream.Writeable.Reader; +import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.xpack.ml.job.process.autodetect.state.ModelSizeStats.MemoryStatus; import org.elasticsearch.xpack.ml.support.AbstractSerializingTestCase; @@ -46,6 +47,10 @@ public class ModelSizeStatsTests extends AbstractSerializingTestCase { @Override protected Quantiles createTestInstance() { - Quantiles quantiles = new Quantiles("foo", new Date(randomLong()), randomAsciiOfLengthBetween(0, 1000)); - return quantiles; + return createRandomized(); + } + + public static Quantiles createRandomized() { + return new Quantiles(randomAsciiOfLengthBetween(1, 20), + new Date(TimeValue.parseTimeValue(randomTimeValue(), "test").millis()), + randomAsciiOfLengthBetween(0, 1000)); } @Override diff --git a/plugin/src/test/java/org/elasticsearch/xpack/ml/job/results/AutodetectResultTests.java b/plugin/src/test/java/org/elasticsearch/xpack/ml/job/results/AutodetectResultTests.java index 3e8b87ee2dd..93347fb3854 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/ml/job/results/AutodetectResultTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/ml/job/results/AutodetectResultTests.java @@ -10,7 +10,9 @@ import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.xpack.ml.job.process.autodetect.state.ModelSizeStats; import org.elasticsearch.xpack.ml.job.process.autodetect.state.ModelSnapshot; import org.elasticsearch.xpack.ml.job.process.autodetect.output.FlushAcknowledgement; +import org.elasticsearch.xpack.ml.job.process.autodetect.state.ModelSnapshotTests; import org.elasticsearch.xpack.ml.job.process.autodetect.state.Quantiles; +import org.elasticsearch.xpack.ml.job.process.autodetect.state.QuantilesTests; import org.elasticsearch.xpack.ml.support.AbstractSerializingTestCase; import java.util.ArrayList; @@ -62,13 +64,12 @@ public class AutodetectResultTests extends AbstractSerializingTestCase modelSnapshots) throws IOException { diff --git a/plugin/src/test/java/org/elasticsearch/xpack/ml/modelsnapshots/GetModelSnapshotsTests.java b/plugin/src/test/java/org/elasticsearch/xpack/ml/modelsnapshots/GetModelSnapshotsTests.java index cb27d89ff13..24af763caa9 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/ml/modelsnapshots/GetModelSnapshotsTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/ml/modelsnapshots/GetModelSnapshotsTests.java @@ -31,12 +31,12 @@ public class GetModelSnapshotsTests extends ESTestCase { } public void testModelSnapshots_clearQuantiles() { - ModelSnapshot m1 = new ModelSnapshot("jobId"); - m1.setQuantiles(new Quantiles("jobId", new Date(), "quantileState")); - ModelSnapshot m2 = new ModelSnapshot("jobId"); + ModelSnapshot m1 = new ModelSnapshot.Builder("jobId").setQuantiles( + new Quantiles("jobId", new Date(), "quantileState")).build(); + ModelSnapshot m2 = new ModelSnapshot.Builder("jobId").build(); QueryPage page = new QueryPage<>(Arrays.asList(m1, m2), 2, new ParseField("field")); - GetModelSnapshotsAction.TransportAction.clearQuantiles(page); + page = GetModelSnapshotsAction.TransportAction.clearQuantiles(page); assertEquals(2, page.results().size()); for (ModelSnapshot modelSnapshot : page.results()) { assertNull(modelSnapshot.getQuantiles());