diff --git a/plugin/src/main/java/org/elasticsearch/xpack/ml/action/PutJobAction.java b/plugin/src/main/java/org/elasticsearch/xpack/ml/action/PutJobAction.java index 959515343ef..d1e29dea7ce 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/ml/action/PutJobAction.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/ml/action/PutJobAction.java @@ -38,6 +38,7 @@ import org.elasticsearch.xpack.ml.job.JobManager; import org.elasticsearch.xpack.ml.job.config.Job; import java.io.IOException; +import java.util.Date; import java.util.Objects; public class PutJobAction extends Action { @@ -62,8 +63,14 @@ public class PutJobAction extends Action implements ToXContent { public static Request parseRequest(String jobId, XContentParser parser) { - Job job = Job.PARSER.apply(parser, null).build(true, jobId); - return new Request(job); + Job.Builder job = Job.PARSER.apply(parser, null); + if (job.getId() == null) { + job.setId(jobId); + } + if (job.getCreateTime() == null) { + job.setCreateTime(new Date()); + } + return new Request(job.build()); } private Job job; diff --git a/plugin/src/main/java/org/elasticsearch/xpack/ml/action/ValidateJobConfigAction.java b/plugin/src/main/java/org/elasticsearch/xpack/ml/action/ValidateJobConfigAction.java index 916afb35b54..a95b27ac25a 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/ml/action/ValidateJobConfigAction.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/ml/action/ValidateJobConfigAction.java @@ -28,6 +28,7 @@ import org.elasticsearch.transport.TransportService; import org.elasticsearch.xpack.ml.job.config.Job; import java.io.IOException; +import java.util.Date; import java.util.Objects; public class ValidateJobConfigAction @@ -66,7 +67,8 @@ extends Action implements Writeable, ToXContent return id; } + public Date getCreateTime() { + return createTime; + } + public void setCustomSettings(Map customSettings) { this.customSettings = customSettings; } @@ -617,31 +621,16 @@ public class Job extends AbstractDiffable implements Writeable, ToXContent } public Job build() { - return build(false, null); - } - - public Job build(boolean fromApi, String urlJobId) { Date createTime; Date finishedTime; Date lastDataTime; String modelSnapshotId; - if (fromApi) { - if (id == null) { - id = urlJobId; - } else if (!id.equals(urlJobId)) { - throw new IllegalArgumentException(Messages.getMessage(Messages.INCONSISTENT_ID, ID.getPreferredName(), id, urlJobId)); - } - createTime = this.createTime == null ? new Date() : this.createTime; - finishedTime = null; - lastDataTime = null; - modelSnapshotId = null; - } else { - createTime = this.createTime; - finishedTime = this.finishedTime; - lastDataTime = this.lastDataTime; - modelSnapshotId = this.modelSnapshotId; - } + + createTime = ExceptionsHelper.requireNonNull(this.createTime, CREATE_TIME.getPreferredName()); + finishedTime = this.finishedTime; + lastDataTime = this.lastDataTime; + modelSnapshotId = this.modelSnapshotId; if (analysisConfig == null) { throw new IllegalArgumentException(Messages.getMessage(Messages.JOB_CONFIG_MISSING_ANALYSISCONFIG)); diff --git a/plugin/src/test/java/org/elasticsearch/license/MachineLearningLicensingTests.java b/plugin/src/test/java/org/elasticsearch/license/MachineLearningLicensingTests.java index 5fe56b1b1c2..8b05578ef9e 100644 --- a/plugin/src/test/java/org/elasticsearch/license/MachineLearningLicensingTests.java +++ b/plugin/src/test/java/org/elasticsearch/license/MachineLearningLicensingTests.java @@ -53,7 +53,7 @@ public class MachineLearningLicensingTests extends BaseMlIntegTestCase { try (TransportClient client = new TestXPackTransportClient(internalCluster().transportClient().settings())) { client.addTransportAddress(internalCluster().getDataNodeInstance(Transport.class).boundAddress().publishAddress()); PlainListenableActionFuture listener = new PlainListenableActionFuture<>(client.threadPool()); - new MachineLearningClient(client).putJob(new PutJobAction.Request(createJob("foo").build(true, "foo")), listener); + new MachineLearningClient(client).putJob(new PutJobAction.Request(createJob("foo").build()), listener); listener.actionGet(); fail("put job action should not be enabled!"); } catch (ElasticsearchSecurityException e) { @@ -70,7 +70,7 @@ public class MachineLearningLicensingTests extends BaseMlIntegTestCase { try (TransportClient client = new TestXPackTransportClient(internalCluster().transportClient().settings())) { client.addTransportAddress(internalCluster().getDataNodeInstance(Transport.class).boundAddress().publishAddress()); PlainListenableActionFuture listener = new PlainListenableActionFuture<>(client.threadPool()); - new MachineLearningClient(client).putJob(new PutJobAction.Request(createJob("foo").build(true, "foo")), listener); + new MachineLearningClient(client).putJob(new PutJobAction.Request(createJob("foo").build()), listener); PutJobAction.Response response = listener.actionGet(); assertNotNull(response); } @@ -83,7 +83,7 @@ public class MachineLearningLicensingTests extends BaseMlIntegTestCase { try (TransportClient client = new TestXPackTransportClient(internalCluster().transportClient().settings())) { client.addTransportAddress(internalCluster().getDataNodeInstance(Transport.class).boundAddress().publishAddress()); PlainListenableActionFuture putJobListener = new PlainListenableActionFuture<>(client.threadPool()); - new MachineLearningClient(client).putJob(new PutJobAction.Request(createJob("foo").build(true, "foo")), putJobListener); + new MachineLearningClient(client).putJob(new PutJobAction.Request(createJob("foo").build()), putJobListener); PutJobAction.Response response = putJobListener.actionGet(); assertNotNull(response); } @@ -126,7 +126,7 @@ public class MachineLearningLicensingTests extends BaseMlIntegTestCase { try (TransportClient client = new TestXPackTransportClient(internalCluster().transportClient().settings())) { client.addTransportAddress(internalCluster().getDataNodeInstance(Transport.class).boundAddress().publishAddress()); PlainListenableActionFuture putJobListener = new PlainListenableActionFuture<>(client.threadPool()); - new MachineLearningClient(client).putJob(new PutJobAction.Request(createJob("foo").build(true, "foo")), putJobListener); + new MachineLearningClient(client).putJob(new PutJobAction.Request(createJob("foo").build()), putJobListener); PutJobAction.Response putJobResponse = putJobListener.actionGet(); assertNotNull(putJobResponse); } @@ -171,7 +171,7 @@ public class MachineLearningLicensingTests extends BaseMlIntegTestCase { try (TransportClient client = new TestXPackTransportClient(internalCluster().transportClient().settings())) { client.addTransportAddress(internalCluster().getDataNodeInstance(Transport.class).boundAddress().publishAddress()); PlainListenableActionFuture putJobListener = new PlainListenableActionFuture<>(client.threadPool()); - new MachineLearningClient(client).putJob(new PutJobAction.Request(createJob("foo").build(true, "foo")), putJobListener); + new MachineLearningClient(client).putJob(new PutJobAction.Request(createJob("foo").build()), putJobListener); PutJobAction.Response putJobResponse = putJobListener.actionGet(); assertNotNull(putJobResponse); PlainListenableActionFuture putDatafeedListener = new PlainListenableActionFuture<>( @@ -224,7 +224,7 @@ public class MachineLearningLicensingTests extends BaseMlIntegTestCase { try (TransportClient client = new TestXPackTransportClient(internalCluster().transportClient().settings())) { client.addTransportAddress(internalCluster().getDataNodeInstance(Transport.class).boundAddress().publishAddress()); PlainListenableActionFuture putJobListener = new PlainListenableActionFuture<>(client.threadPool()); - new MachineLearningClient(client).putJob(new PutJobAction.Request(createJob("foo").build(true, "foo")), putJobListener); + new MachineLearningClient(client).putJob(new PutJobAction.Request(createJob("foo").build()), putJobListener); PutJobAction.Response putJobResponse = putJobListener.actionGet(); assertNotNull(putJobResponse); PlainListenableActionFuture putDatafeedListener = new PlainListenableActionFuture<>( @@ -264,7 +264,7 @@ public class MachineLearningLicensingTests extends BaseMlIntegTestCase { try (TransportClient client = new TestXPackTransportClient(internalCluster().transportClient().settings())) { client.addTransportAddress(internalCluster().getDataNodeInstance(Transport.class).boundAddress().publishAddress()); PlainListenableActionFuture putJobListener = new PlainListenableActionFuture<>(client.threadPool()); - new MachineLearningClient(client).putJob(new PutJobAction.Request(createJob("foo").build(true, "foo")), putJobListener); + new MachineLearningClient(client).putJob(new PutJobAction.Request(createJob("foo").build()), putJobListener); PutJobAction.Response putJobResponse = putJobListener.actionGet(); assertNotNull(putJobResponse); PlainListenableActionFuture openJobListener = new PlainListenableActionFuture<>(client.threadPool()); @@ -294,7 +294,7 @@ public class MachineLearningLicensingTests extends BaseMlIntegTestCase { try (TransportClient client = new TestXPackTransportClient(internalCluster().transportClient().settings())) { client.addTransportAddress(internalCluster().getDataNodeInstance(Transport.class).boundAddress().publishAddress()); PlainListenableActionFuture putJobListener = new PlainListenableActionFuture<>(client.threadPool()); - new MachineLearningClient(client).putJob(new PutJobAction.Request(createJob("foo").build(true, "foo")), putJobListener); + new MachineLearningClient(client).putJob(new PutJobAction.Request(createJob("foo").build()), putJobListener); PutJobAction.Response putJobResponse = putJobListener.actionGet(); assertNotNull(putJobResponse); } @@ -318,7 +318,7 @@ public class MachineLearningLicensingTests extends BaseMlIntegTestCase { try (TransportClient client = new TestXPackTransportClient(internalCluster().transportClient().settings())) { client.addTransportAddress(internalCluster().getDataNodeInstance(Transport.class).boundAddress().publishAddress()); PlainListenableActionFuture putJobListener = new PlainListenableActionFuture<>(client.threadPool()); - new MachineLearningClient(client).putJob(new PutJobAction.Request(createJob("foo").build(true, "foo")), putJobListener); + new MachineLearningClient(client).putJob(new PutJobAction.Request(createJob("foo").build()), putJobListener); PutJobAction.Response putJobResponse = putJobListener.actionGet(); assertNotNull(putJobResponse); PlainListenableActionFuture putDatafeedListener = new PlainListenableActionFuture<>( diff --git a/plugin/src/test/java/org/elasticsearch/xpack/ml/action/DatafeedJobsIT.java b/plugin/src/test/java/org/elasticsearch/xpack/ml/action/DatafeedJobsIT.java index 1c42ef82f9e..fb73f102410 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/ml/action/DatafeedJobsIT.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/ml/action/DatafeedJobsIT.java @@ -52,7 +52,7 @@ public class DatafeedJobsIT extends BaseMlIntegTestCase { indexDocs("data-2", numDocs2, oneWeekAgo, now); Job.Builder job = createScheduledJob("lookback-job"); - PutJobAction.Request putJobRequest = new PutJobAction.Request(job.build(true, job.getId())); + PutJobAction.Request putJobRequest = new PutJobAction.Request(job.build()); PutJobAction.Response putJobResponse = client().execute(PutJobAction.INSTANCE, putJobRequest).get(); assertTrue(putJobResponse.isAcknowledged()); client().execute(OpenJobAction.INSTANCE, new OpenJobAction.Request(job.getId())); @@ -91,7 +91,7 @@ public class DatafeedJobsIT extends BaseMlIntegTestCase { indexDocs("data", numDocs1, lastWeek, now); Job.Builder job = createScheduledJob("realtime-job"); - PutJobAction.Request putJobRequest = new PutJobAction.Request(job.build(true, job.getId())); + PutJobAction.Request putJobRequest = new PutJobAction.Request(job.build()); PutJobAction.Response putJobResponse = client().execute(PutJobAction.INSTANCE, putJobRequest).get(); assertTrue(putJobResponse.isAcknowledged()); client().execute(OpenJobAction.INSTANCE, new OpenJobAction.Request(job.getId())); diff --git a/plugin/src/test/java/org/elasticsearch/xpack/ml/action/PutJobActionRequestTests.java b/plugin/src/test/java/org/elasticsearch/xpack/ml/action/PutJobActionRequestTests.java index 051017b2983..6f2b816832c 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/ml/action/PutJobActionRequestTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/ml/action/PutJobActionRequestTests.java @@ -10,17 +10,20 @@ import org.elasticsearch.xpack.ml.action.PutJobAction.Request; import org.elasticsearch.xpack.ml.job.config.Job; import org.elasticsearch.xpack.ml.support.AbstractStreamableXContentTestCase; +import java.util.Date; + import static org.elasticsearch.xpack.ml.job.config.JobTests.buildJobBuilder; import static org.elasticsearch.xpack.ml.job.config.JobTests.randomValidJobId; public class PutJobActionRequestTests extends AbstractStreamableXContentTestCase { private final String jobId = randomValidJobId(); + private final Date date = new Date(); @Override protected Request createTestInstance() { - Job.Builder jobConfiguration = buildJobBuilder(jobId); - return new Request(jobConfiguration.build(true, jobConfiguration.getId())); + Job.Builder jobConfiguration = buildJobBuilder(jobId, date); + return new Request(jobConfiguration.build()); } @Override diff --git a/plugin/src/test/java/org/elasticsearch/xpack/ml/action/ValidateJobConfigActionRequestTests.java b/plugin/src/test/java/org/elasticsearch/xpack/ml/action/ValidateJobConfigActionRequestTests.java index 1cba0f92fcc..79f9fe78360 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/ml/action/ValidateJobConfigActionRequestTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/ml/action/ValidateJobConfigActionRequestTests.java @@ -15,7 +15,9 @@ import org.elasticsearch.xpack.ml.job.config.Job; import org.elasticsearch.xpack.ml.support.AbstractStreamableXContentTestCase; import java.util.ArrayList; +import java.util.Date; import java.util.List; +import java.util.Locale; public class ValidateJobConfigActionRequestTests extends AbstractStreamableXContentTestCase { @@ -72,7 +74,9 @@ public class ValidateJobConfigActionRequestTests extends AbstractStreamableXCont if (randomBoolean()) { job.setAnalysisLimits(new AnalysisLimits(randomNonNegativeLong(), randomNonNegativeLong())); } - return new Request(job.build(true, "ok")); + job.setCreateTime(new Date()); + job.setId(randomAsciiOfLength(5).replace(".", "-").replace("_", "-").toLowerCase(Locale.ENGLISH)); + return new Request(job.build()); } @Override diff --git a/plugin/src/test/java/org/elasticsearch/xpack/ml/datafeed/DatafeedJobRunnerTests.java b/plugin/src/test/java/org/elasticsearch/xpack/ml/datafeed/DatafeedJobRunnerTests.java index 0a56d1ced6a..4f9f89342ed 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/ml/datafeed/DatafeedJobRunnerTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/ml/datafeed/DatafeedJobRunnerTests.java @@ -310,6 +310,7 @@ public class DatafeedJobRunnerTests extends ESTestCase { Job.Builder builder = new Job.Builder("foo"); builder.setAnalysisConfig(acBuilder); + builder.setCreateTime(new Date()); return builder; } 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 a9f063a7564..0a7c569d8a4 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 @@ -257,6 +257,7 @@ public class AutodetectResultProcessorIT extends ESSingleNodeTestCase { AnalysisConfig.Builder analysisConfBuilder = new AnalysisConfig.Builder(Collections.singletonList(detectorBuilder.build())); analysisConfBuilder.setInfluencers(Collections.singletonList("influence_field")); jobBuilder.setAnalysisConfig(analysisConfBuilder); + jobBuilder.setCreateTime(new Date()); ClusterState cs = ClusterState.builder(new ClusterName("_name")) .metaData(MetaData.builder().putCustom(MlMetadata.TYPE, MlMetadata.EMPTY_METADATA).indices(ImmutableOpenMap.of())).build(); diff --git a/plugin/src/test/java/org/elasticsearch/xpack/ml/integration/BasicDistributedJobsIT.java b/plugin/src/test/java/org/elasticsearch/xpack/ml/integration/BasicDistributedJobsIT.java index 2528603f28b..70039551540 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/ml/integration/BasicDistributedJobsIT.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/ml/integration/BasicDistributedJobsIT.java @@ -27,6 +27,7 @@ import org.elasticsearch.xpack.persistent.PersistentTasksInProgress.PersistentTa import java.io.IOException; import java.util.Collection; import java.util.Collections; +import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -42,7 +43,7 @@ public class BasicDistributedJobsIT extends BaseMlIntegTestCase { ensureStableCluster(4); Job.Builder job = createJob("job_id"); - PutJobAction.Request putJobRequest = new PutJobAction.Request(job.build(true, job.getId())); + PutJobAction.Request putJobRequest = new PutJobAction.Request(job.build()); PutJobAction.Response putJobResponse = client().execute(PutJobAction.INSTANCE, putJobRequest).get(); assertTrue(putJobResponse.isAcknowledged()); ensureGreen(); @@ -79,7 +80,7 @@ public class BasicDistributedJobsIT extends BaseMlIntegTestCase { ensureStableCluster(4); Job.Builder job = createScheduledJob("job_id"); - PutJobAction.Request putJobRequest = new PutJobAction.Request(job.build(true, job.getId())); + PutJobAction.Request putJobRequest = new PutJobAction.Request(job.build()); PutJobAction.Response putJobResponse = client().execute(PutJobAction.INSTANCE, putJobRequest).get(); assertTrue(putJobResponse.isAcknowledged()); DatafeedConfig config = createDatafeed("data_feed_id", job.getId(), Collections.singletonList("*")); @@ -152,7 +153,7 @@ public class BasicDistributedJobsIT extends BaseMlIntegTestCase { ensureStableCluster(3); Job.Builder job = createJob("job_id"); - PutJobAction.Request putJobRequest = new PutJobAction.Request(job.build(true, job.getId())); + PutJobAction.Request putJobRequest = new PutJobAction.Request(job.build()); PutJobAction.Response putJobResponse = client().execute(PutJobAction.INSTANCE, putJobRequest).get(); assertTrue(putJobResponse.isAcknowledged()); @@ -247,7 +248,7 @@ public class BasicDistributedJobsIT extends BaseMlIntegTestCase { int numJobs = numMlNodes * 10; for (int i = 0; i < numJobs; i++) { Job.Builder job = createJob(Integer.toString(i)); - PutJobAction.Request putJobRequest = new PutJobAction.Request(job.build(true, job.getId())); + PutJobAction.Request putJobRequest = new PutJobAction.Request(job.build()); PutJobAction.Response putJobResponse = client().execute(PutJobAction.INSTANCE, putJobRequest).get(); assertTrue(putJobResponse.isAcknowledged()); diff --git a/plugin/src/test/java/org/elasticsearch/xpack/ml/integration/TooManyJobsIT.java b/plugin/src/test/java/org/elasticsearch/xpack/ml/integration/TooManyJobsIT.java index b3685a9ecfc..9183ba2c524 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/ml/integration/TooManyJobsIT.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/ml/integration/TooManyJobsIT.java @@ -28,7 +28,7 @@ public class TooManyJobsIT extends BaseMlIntegTestCase { // create and open first job, which succeeds: Job.Builder job = createJob("1"); - PutJobAction.Request putJobRequest = new PutJobAction.Request(job.build(true, job.getId())); + PutJobAction.Request putJobRequest = new PutJobAction.Request(job.build()); PutJobAction.Response putJobResponse = client().execute(PutJobAction.INSTANCE, putJobRequest).get(); assertTrue(putJobResponse.isAcknowledged()); client().execute(OpenJobAction.INSTANCE, new OpenJobAction.Request(job.getId())).get(); @@ -40,7 +40,7 @@ public class TooManyJobsIT extends BaseMlIntegTestCase { // create and try to open second job, which fails: job = createJob("2"); - putJobRequest = new PutJobAction.Request(job.build(true, job.getId())); + putJobRequest = new PutJobAction.Request(job.build()); putJobResponse = client().execute(PutJobAction.INSTANCE, putJobRequest).get(); assertTrue(putJobResponse.isAcknowledged()); expectThrows(ElasticsearchStatusException.class, @@ -74,7 +74,7 @@ public class TooManyJobsIT extends BaseMlIntegTestCase { int clusterWideMaxNumberOfJobs = numNodes * maxNumberOfJobsPerNode; for (int i = 1; i <= (clusterWideMaxNumberOfJobs + 1); i++) { Job.Builder job = createJob(Integer.toString(i)); - PutJobAction.Request putJobRequest = new PutJobAction.Request(job.build(true, job.getId())); + PutJobAction.Request putJobRequest = new PutJobAction.Request(job.build()); PutJobAction.Response putJobResponse = client().execute(PutJobAction.INSTANCE, putJobRequest).get(); assertTrue(putJobResponse.isAcknowledged()); diff --git a/plugin/src/test/java/org/elasticsearch/xpack/ml/job/config/JobTests.java b/plugin/src/test/java/org/elasticsearch/xpack/ml/job/config/JobTests.java index c1bc07b0619..7579fb4ad17 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/ml/job/config/JobTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/ml/job/config/JobTests.java @@ -37,7 +37,7 @@ public class JobTests extends AbstractSerializingTestCase { } public void testConstructor_GivenEmptyJobConfiguration() { - Job job = buildJobBuilder("foo").build(true, "foo"); + Job job = buildJobBuilder("foo").build(); assertEquals("foo", job.getId()); assertNotNull(job.getCreateTime()); @@ -58,12 +58,7 @@ public class JobTests extends AbstractSerializingTestCase { } public void testNoId() { - expectThrows(IllegalArgumentException.class, () -> buildJobBuilder("").build(true, "")); - } - - public void testInconsistentId() { - Exception e = expectThrows(IllegalArgumentException.class, () -> buildJobBuilder("foo").build(true, "bar")); - assertEquals(Messages.getMessage(Messages.INCONSISTENT_ID, Job.ID.getPreferredName(), "foo", "bar"), e.getMessage()); + expectThrows(IllegalArgumentException.class, () -> buildJobBuilder("").build()); } public void testEquals_GivenDifferentClass() { @@ -82,42 +77,54 @@ public class JobTests extends AbstractSerializingTestCase { } public void testEquals_GivenDifferentRenormalizationWindowDays() { + Date date = new Date(); Job.Builder jobDetails1 = new Job.Builder("foo"); jobDetails1.setAnalysisConfig(createAnalysisConfig()); jobDetails1.setRenormalizationWindowDays(3L); + jobDetails1.setCreateTime(date); Job.Builder jobDetails2 = new Job.Builder("foo"); jobDetails2.setRenormalizationWindowDays(4L); jobDetails2.setAnalysisConfig(createAnalysisConfig()); + jobDetails2.setCreateTime(date); assertFalse(jobDetails1.build().equals(jobDetails2.build())); } public void testEquals_GivenDifferentBackgroundPersistInterval() { + Date date = new Date(); Job.Builder jobDetails1 = new Job.Builder("foo"); jobDetails1.setAnalysisConfig(createAnalysisConfig()); jobDetails1.setBackgroundPersistInterval(10000L); + jobDetails1.setCreateTime(date); Job.Builder jobDetails2 = new Job.Builder("foo"); jobDetails2.setBackgroundPersistInterval(8000L); jobDetails2.setAnalysisConfig(createAnalysisConfig()); + jobDetails2.setCreateTime(date); assertFalse(jobDetails1.build().equals(jobDetails2.build())); } public void testEquals_GivenDifferentModelSnapshotRetentionDays() { + Date date = new Date(); Job.Builder jobDetails1 = new Job.Builder("foo"); jobDetails1.setAnalysisConfig(createAnalysisConfig()); jobDetails1.setModelSnapshotRetentionDays(10L); + jobDetails1.setCreateTime(date); Job.Builder jobDetails2 = new Job.Builder("foo"); jobDetails2.setModelSnapshotRetentionDays(8L); jobDetails2.setAnalysisConfig(createAnalysisConfig()); + jobDetails2.setCreateTime(date); assertFalse(jobDetails1.build().equals(jobDetails2.build())); } public void testEquals_GivenDifferentResultsRetentionDays() { + Date date = new Date(); Job.Builder jobDetails1 = new Job.Builder("foo"); jobDetails1.setAnalysisConfig(createAnalysisConfig()); + jobDetails1.setCreateTime(date); jobDetails1.setResultsRetentionDays(30L); Job.Builder jobDetails2 = new Job.Builder("foo"); jobDetails2.setResultsRetentionDays(4L); jobDetails2.setAnalysisConfig(createAnalysisConfig()); + jobDetails2.setCreateTime(date); assertFalse(jobDetails1.build().equals(jobDetails2.build())); } @@ -333,9 +340,9 @@ public class JobTests extends AbstractSerializingTestCase { assertEquals(Messages.getMessage(Messages.INVALID_ID, Job.RESULTS_INDEX_NAME.getPreferredName()), e.getMessage()); } - public static Job.Builder buildJobBuilder(String id) { + public static Job.Builder buildJobBuilder(String id, Date date) { Job.Builder builder = new Job.Builder(id); - builder.setCreateTime(new Date()); + builder.setCreateTime(date); AnalysisConfig.Builder ac = createAnalysisConfig(); DataDescription.Builder dc = new DataDescription.Builder(); builder.setAnalysisConfig(ac); @@ -343,6 +350,10 @@ public class JobTests extends AbstractSerializingTestCase { return builder; } + public static Job.Builder buildJobBuilder(String id) { + return buildJobBuilder(id, new Date()); + } + public static String randomValidJobId() { CodepointSetGenerator generator = new CodepointSetGenerator("abcdefghijklmnopqrstuvwxyz".toCharArray()); return generator.ofCodePointsLength(random(), 10, 10); diff --git a/plugin/src/test/java/org/elasticsearch/xpack/ml/job/config/JobUpdateTests.java b/plugin/src/test/java/org/elasticsearch/xpack/ml/job/config/JobUpdateTests.java index 63a58994f54..d110c3af5cd 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/ml/job/config/JobUpdateTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/ml/job/config/JobUpdateTests.java @@ -13,6 +13,7 @@ import org.elasticsearch.xpack.ml.support.AbstractSerializingTestCase; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; +import java.util.Date; import java.util.List; import java.util.Map; @@ -125,6 +126,7 @@ public class JobUpdateTests extends AbstractSerializingTestCase { AnalysisConfig.Builder ac = new AnalysisConfig.Builder(Arrays.asList(d1.build(), d2.build())); ac.setCategorizationFieldName("cat_field"); jobBuilder.setAnalysisConfig(ac); + jobBuilder.setCreateTime(new Date()); Job updatedJob = update.mergeWithJob(jobBuilder.build()); diff --git a/plugin/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/AutodetectCommunicatorTests.java b/plugin/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/AutodetectCommunicatorTests.java index 3f389eba20d..95f57e46139 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/AutodetectCommunicatorTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/AutodetectCommunicatorTests.java @@ -26,6 +26,7 @@ import java.io.IOException; import java.io.InputStream; import java.time.Duration; import java.util.Collections; +import java.util.Date; import java.util.List; import java.util.Optional; import java.util.concurrent.CountDownLatch; @@ -129,6 +130,7 @@ public class AutodetectCommunicatorTests extends ESTestCase { builder.setDataDescription(dd); builder.setAnalysisConfig(ac); + builder.setCreateTime(new Date()); return builder.build(); } 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 e03731b215a..a42572d20f9 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 @@ -366,6 +366,7 @@ public class AutodetectProcessManagerTests extends ESTestCase { Job.Builder builder = new Job.Builder(jobId); builder.setDataDescription(dd); builder.setAnalysisConfig(ac); + builder.setCreateTime(new Date()); return builder.build(); } diff --git a/plugin/src/test/java/org/elasticsearch/xpack/ml/job/process/normalizer/ScoresUpdaterTests.java b/plugin/src/test/java/org/elasticsearch/xpack/ml/job/process/normalizer/ScoresUpdaterTests.java index 56a58c1efc0..623846aff33 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/ml/job/process/normalizer/ScoresUpdaterTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/ml/job/process/normalizer/ScoresUpdaterTests.java @@ -75,6 +75,7 @@ public class ScoresUpdaterTests extends ESTestCase { AnalysisConfig.Builder configBuilder = new AnalysisConfig.Builder(detectors); configBuilder.setBucketSpan(DEFAULT_BUCKET_SPAN); jobBuilder.setAnalysisConfig(configBuilder); + jobBuilder.setCreateTime(new Date()); job = jobBuilder.build(); diff --git a/plugin/src/test/java/org/elasticsearch/xpack/ml/support/BaseMlIntegTestCase.java b/plugin/src/test/java/org/elasticsearch/xpack/ml/support/BaseMlIntegTestCase.java index 1ceb8da7354..e1862e07ae5 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/ml/support/BaseMlIntegTestCase.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/ml/support/BaseMlIntegTestCase.java @@ -37,6 +37,7 @@ import org.elasticsearch.xpack.ml.job.process.autodetect.state.DataCounts; import org.elasticsearch.xpack.persistent.RemovePersistentTaskAction; import java.util.Collections; +import java.util.Date; import java.util.List; import java.util.Map; import java.util.concurrent.ExecutionException; @@ -84,6 +85,7 @@ public abstract class BaseMlIntegTestCase extends SecurityIntegTestCase { Job.Builder builder = new Job.Builder(); builder.setId(id); + builder.setCreateTime(new Date()); builder.setAnalysisConfig(analysisConfig); builder.setDataDescription(dataDescription); @@ -103,6 +105,7 @@ public abstract class BaseMlIntegTestCase extends SecurityIntegTestCase { builder.setAnalysisConfig(analysisConfig); builder.setDataDescription(dataDescription); + builder.setCreateTime(new Date()); return builder; }