diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/calendars/ScheduledEvent.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/calendars/ScheduledEvent.java index 042775c8024e4..afd10e0c17b01 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/calendars/ScheduledEvent.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/calendars/ScheduledEvent.java @@ -27,8 +27,6 @@ import java.io.IOException; import java.time.Instant; -import java.time.ZoneOffset; -import java.time.ZonedDateTime; import java.util.ArrayList; import java.util.List; import java.util.Objects; @@ -55,18 +53,18 @@ private static ObjectParser createParser(boolean i parser.declareString(ScheduledEvent.Builder::description, DESCRIPTION); parser.declareField(ScheduledEvent.Builder::startTime, p -> { if (p.currentToken() == XContentParser.Token.VALUE_NUMBER) { - return ZonedDateTime.ofInstant(Instant.ofEpochMilli(p.longValue()), ZoneOffset.UTC); + return Instant.ofEpochMilli(p.longValue()); } else if (p.currentToken() == XContentParser.Token.VALUE_STRING) { - return ZonedDateTime.ofInstant(Instant.ofEpochMilli(TimeUtils.dateStringToEpoch(p.text())), ZoneOffset.UTC); + return Instant.ofEpochMilli(TimeUtils.dateStringToEpoch(p.text())); } throw new IllegalArgumentException( "unexpected token [" + p.currentToken() + "] for [" + START_TIME.getPreferredName() + "]"); }, START_TIME, ObjectParser.ValueType.VALUE); parser.declareField(ScheduledEvent.Builder::endTime, p -> { if (p.currentToken() == XContentParser.Token.VALUE_NUMBER) { - return ZonedDateTime.ofInstant(Instant.ofEpochMilli(p.longValue()), ZoneOffset.UTC); + return Instant.ofEpochMilli(p.longValue()); } else if (p.currentToken() == XContentParser.Token.VALUE_STRING) { - return ZonedDateTime.ofInstant(Instant.ofEpochMilli(TimeUtils.dateStringToEpoch(p.text())), ZoneOffset.UTC); + return Instant.ofEpochMilli(TimeUtils.dateStringToEpoch(p.text())); } throw new IllegalArgumentException( "unexpected token [" + p.currentToken() + "] for [" + END_TIME.getPreferredName() + "]"); @@ -83,12 +81,12 @@ public static String documentId(String eventId) { } private final String description; - private final ZonedDateTime startTime; - private final ZonedDateTime endTime; + private final Instant startTime; + private final Instant endTime; private final String calendarId; private final String eventId; - ScheduledEvent(String description, ZonedDateTime startTime, ZonedDateTime endTime, String calendarId, @Nullable String eventId) { + ScheduledEvent(String description, Instant startTime, Instant endTime, String calendarId, @Nullable String eventId) { this.description = Objects.requireNonNull(description); this.startTime = Objects.requireNonNull(startTime); this.endTime = Objects.requireNonNull(endTime); @@ -98,8 +96,8 @@ public static String documentId(String eventId) { public ScheduledEvent(StreamInput in) throws IOException { description = in.readString(); - startTime = ZonedDateTime.ofInstant(Instant.ofEpochMilli(in.readVLong()), ZoneOffset.UTC); - endTime = ZonedDateTime.ofInstant(Instant.ofEpochMilli(in.readVLong()), ZoneOffset.UTC); + startTime = Instant.ofEpochMilli(in.readVLong()); + endTime = Instant.ofEpochMilli(in.readVLong()); calendarId = in.readString(); eventId = in.readOptionalString(); } @@ -108,11 +106,11 @@ public String getDescription() { return description; } - public ZonedDateTime getStartTime() { + public Instant getStartTime() { return startTime; } - public ZonedDateTime getEndTime() { + public Instant getEndTime() { return endTime; } @@ -141,9 +139,9 @@ public DetectionRule toDetectionRule(TimeValue bucketSpan) { long bucketSpanSecs = bucketSpan.getSeconds(); - long bucketStartTime = Intervals.alignToFloor(getStartTime().toEpochSecond(), bucketSpanSecs); + long bucketStartTime = Intervals.alignToFloor(getStartTime().getEpochSecond(), bucketSpanSecs); conditions.add(RuleCondition.createTime(Operator.GTE, bucketStartTime)); - long bucketEndTime = Intervals.alignToCeil(getEndTime().toEpochSecond(), bucketSpanSecs); + long bucketEndTime = Intervals.alignToCeil(getEndTime().getEpochSecond(), bucketSpanSecs); conditions.add(RuleCondition.createTime(Operator.LT, bucketEndTime)); DetectionRule.Builder builder = new DetectionRule.Builder(conditions); @@ -154,8 +152,8 @@ public DetectionRule toDetectionRule(TimeValue bucketSpan) { @Override public void writeTo(StreamOutput out) throws IOException { out.writeString(description); - out.writeVLong(startTime.toInstant().toEpochMilli()); - out.writeVLong(endTime.toInstant().toEpochMilli()); + out.writeVLong(startTime.toEpochMilli()); + out.writeVLong(endTime.toEpochMilli()); out.writeString(calendarId); out.writeOptionalString(eventId); } @@ -164,8 +162,8 @@ public void writeTo(StreamOutput out) throws IOException { public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(); builder.field(DESCRIPTION.getPreferredName(), description); - builder.timeField(START_TIME.getPreferredName(), START_TIME.getPreferredName() + "_string", startTime.toInstant().toEpochMilli()); - builder.timeField(END_TIME.getPreferredName(), END_TIME.getPreferredName() + "_string", endTime.toInstant().toEpochMilli()); + builder.timeField(START_TIME.getPreferredName(), START_TIME.getPreferredName() + "_string", startTime.toEpochMilli()); + builder.timeField(END_TIME.getPreferredName(), END_TIME.getPreferredName() + "_string", endTime.toEpochMilli()); builder.field(Calendar.ID.getPreferredName(), calendarId); if (eventId != null) { builder.field(EVENT_ID.getPreferredName(), eventId); @@ -197,8 +195,8 @@ public boolean equals(Object obj) { // Note ZonedDataTime.equals() fails because the time zone and date-time must be the same // which isn't the case in tests where the time zone is randomised. return description.equals(other.description) - && Objects.equals(startTime.toInstant().getEpochSecond(), other.startTime.toInstant().getEpochSecond()) - && Objects.equals(endTime.toInstant().getEpochSecond(), other.endTime.toInstant().getEpochSecond()) + && Objects.equals(startTime.getEpochSecond(), other.startTime.getEpochSecond()) + && Objects.equals(endTime.getEpochSecond(), other.endTime.getEpochSecond()) && calendarId.equals(other.calendarId); } @@ -209,8 +207,8 @@ public int hashCode() { public static class Builder { private String description; - private ZonedDateTime startTime; - private ZonedDateTime endTime; + private Instant startTime; + private Instant endTime; private String calendarId; private String eventId; @@ -219,12 +217,12 @@ public Builder description(String description) { return this; } - public Builder startTime(ZonedDateTime startTime) { + public Builder startTime(Instant startTime) { this.startTime = startTime; return this; } - public Builder endTime(ZonedDateTime endTime) { + public Builder endTime(Instant endTime) { this.endTime = endTime; return this; } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/calendars/ScheduledEventTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/calendars/ScheduledEventTests.java index 9555d364a7bb1..cd14d4b35bd86 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/calendars/ScheduledEventTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/calendars/ScheduledEventTests.java @@ -7,7 +7,6 @@ import org.elasticsearch.ElasticsearchStatusException; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.common.time.DateUtils; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.json.JsonXContent; @@ -18,7 +17,7 @@ import org.elasticsearch.xpack.core.ml.job.config.RuleCondition; import java.io.IOException; -import java.time.ZonedDateTime; +import java.time.Instant; import java.util.EnumSet; import java.util.List; @@ -27,7 +26,7 @@ public class ScheduledEventTests extends AbstractSerializingTestCase { public static ScheduledEvent createScheduledEvent(String calendarId) { - ZonedDateTime start = DateUtils.nowWithMillisResolution(); + Instant start = Instant.ofEpochMilli(Instant.now().toEpochMilli()); return new ScheduledEvent(randomAlphaOfLength(10), start, start.plusSeconds(randomIntBetween(1, 10000)), calendarId, null); } @@ -70,7 +69,7 @@ public void testToDetectionRule() { long conditionEndTime = (long) conditions.get(1).getValue(); assertEquals(0, conditionEndTime % bucketSpanSecs); - long eventTime = event.getEndTime().toEpochSecond() - conditionStartTime; + long eventTime = event.getEndTime().getEpochSecond() - conditionStartTime; long numbBucketsInEvent = (eventTime + bucketSpanSecs -1) / bucketSpanSecs; assertEquals(bucketSpanSecs * (bucketCount + numbBucketsInEvent), conditionEndTime); } @@ -83,11 +82,11 @@ public void testBuild() { builder.description("foo"); e = expectThrows(ElasticsearchStatusException.class, builder::build); assertEquals("Field [start_time] cannot be null", e.getMessage()); - ZonedDateTime now = ZonedDateTime.now(); + Instant now = Instant.now(); builder.startTime(now); e = expectThrows(ElasticsearchStatusException.class, builder::build); assertEquals("Field [end_time] cannot be null", e.getMessage()); - builder.endTime(now.plusHours(1)); + builder.endTime(now.plusSeconds(1*60*60)); e = expectThrows(ElasticsearchStatusException.class, builder::build); assertEquals("Field [calendar_id] cannot be null", e.getMessage()); builder.calendarId("foo"); @@ -96,7 +95,7 @@ public void testBuild() { builder = new ScheduledEvent.Builder().description("f").calendarId("c"); builder.startTime(now); - builder.endTime(now.minusHours(2)); + builder.endTime(now.minusSeconds(2*60*60)); e = expectThrows(ElasticsearchStatusException.class, builder::build); assertThat(e.getMessage(), containsString("must come before end time")); diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/test/java/org/elasticsearch/xpack/ml/integration/ScheduledEventsIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/test/java/org/elasticsearch/xpack/ml/integration/ScheduledEventsIT.java index fb261908e2c10..2086adb869f6d 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/test/java/org/elasticsearch/xpack/ml/integration/ScheduledEventsIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/test/java/org/elasticsearch/xpack/ml/integration/ScheduledEventsIT.java @@ -25,8 +25,6 @@ import java.io.IOException; import java.time.Instant; -import java.time.ZoneOffset; -import java.time.ZonedDateTime; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -56,21 +54,21 @@ public void testScheduledEvents() throws IOException { long firstEventStartTime = 1514937600000L; long firstEventEndTime = firstEventStartTime + 2 * 60 * 60 * 1000; events.add(new ScheduledEvent.Builder().description("1st event (2hr)") - .startTime(ZonedDateTime.ofInstant(Instant.ofEpochMilli(firstEventStartTime), ZoneOffset.UTC)) - .endTime(ZonedDateTime.ofInstant(Instant.ofEpochMilli(firstEventEndTime), ZoneOffset.UTC)) + .startTime(Instant.ofEpochMilli(firstEventStartTime)) + .endTime(Instant.ofEpochMilli(firstEventEndTime)) .calendarId(calendarId).build()); // add 10 min event smaller than the bucket long secondEventStartTime = 1515067200000L; long secondEventEndTime = secondEventStartTime + 10 * 60 * 1000; events.add(new ScheduledEvent.Builder().description("2nd event with period smaller than bucketspan") - .startTime(ZonedDateTime.ofInstant(Instant.ofEpochMilli(secondEventStartTime), ZoneOffset.UTC)) - .endTime(ZonedDateTime.ofInstant(Instant.ofEpochMilli(secondEventEndTime), ZoneOffset.UTC)) + .startTime(Instant.ofEpochMilli(secondEventStartTime)) + .endTime(Instant.ofEpochMilli(secondEventEndTime)) .calendarId(calendarId).build()); long thirdEventStartTime = 1515088800000L; long thirdEventEndTime = thirdEventStartTime + 3 * 60 * 60 * 1000; events.add(new ScheduledEvent.Builder().description("3rd event 3hr") - .startTime(ZonedDateTime.ofInstant(Instant.ofEpochMilli(thirdEventStartTime), ZoneOffset.UTC)) - .endTime(ZonedDateTime.ofInstant(Instant.ofEpochMilli(thirdEventEndTime), ZoneOffset.UTC)) + .startTime(Instant.ofEpochMilli(thirdEventStartTime)) + .endTime(Instant.ofEpochMilli(thirdEventEndTime)) .calendarId(calendarId).build()); postScheduledEvents(calendarId, events); @@ -168,8 +166,8 @@ public void testScheduledEventWithInterimResults() throws IOException { long firstEventStartTime = startTime + bucketSpan.millis() * bucketCount; long firstEventEndTime = firstEventStartTime + bucketSpan.millis() * 2; events.add(new ScheduledEvent.Builder().description("1st event 2hr") - .startTime(ZonedDateTime.ofInstant(Instant.ofEpochMilli(firstEventStartTime), ZoneOffset.UTC)) - .endTime(ZonedDateTime.ofInstant(Instant.ofEpochMilli(firstEventEndTime), ZoneOffset.UTC)) + .startTime(Instant.ofEpochMilli(firstEventStartTime)) + .endTime((Instant.ofEpochMilli(firstEventEndTime))) .calendarId(calendarId).build()); postScheduledEvents(calendarId, events); @@ -217,8 +215,8 @@ public void testAddEventsToOpenJob() throws Exception { long eventStartTime = startTime + (bucketCount + 1) * bucketSpan.millis(); long eventEndTime = eventStartTime + (long)(1.5 * bucketSpan.millis()); events.add(new ScheduledEvent.Builder().description("Some Event") - .startTime(ZonedDateTime.ofInstant(Instant.ofEpochMilli(eventStartTime), ZoneOffset.UTC)) - .endTime(ZonedDateTime.ofInstant(Instant.ofEpochMilli(eventEndTime), ZoneOffset.UTC)) + .startTime((Instant.ofEpochMilli(eventStartTime))) + .endTime((Instant.ofEpochMilli(eventEndTime))) .calendarId(calendarId).build()); postScheduledEvents(calendarId, events); @@ -287,8 +285,8 @@ public void testAddOpenedJobToGroupWithCalendar() throws Exception { long eventStartTime = startTime + (bucketCount + 1) * bucketSpan.millis(); long eventEndTime = eventStartTime + (long)(1.5 * bucketSpan.millis()); events.add(new ScheduledEvent.Builder().description("Some Event") - .startTime(ZonedDateTime.ofInstant(Instant.ofEpochMilli(eventStartTime), ZoneOffset.UTC)) - .endTime(ZonedDateTime.ofInstant(Instant.ofEpochMilli(eventEndTime), ZoneOffset.UTC)) + .startTime((Instant.ofEpochMilli(eventStartTime))) + .endTime((Instant.ofEpochMilli(eventEndTime))) .calendarId(calendarId).build()); postScheduledEvents(calendarId, events); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/integration/JobResultsProviderIT.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/integration/JobResultsProviderIT.java index 02cc738477cfb..091fc33a2b9bd 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/integration/JobResultsProviderIT.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/integration/JobResultsProviderIT.java @@ -41,8 +41,8 @@ import org.elasticsearch.xpack.ml.MlSingleNodeTestCase; import org.elasticsearch.xpack.ml.job.persistence.CalendarQueryBuilder; import org.elasticsearch.xpack.ml.job.persistence.JobDataCountsPersister; -import org.elasticsearch.xpack.ml.job.persistence.JobResultsProvider; import org.elasticsearch.xpack.ml.job.persistence.JobResultsPersister; +import org.elasticsearch.xpack.ml.job.persistence.JobResultsProvider; import org.elasticsearch.xpack.ml.job.persistence.ScheduledEventsQueryBuilder; import org.elasticsearch.xpack.ml.job.process.autodetect.params.AutodetectParams; import org.junit.Before; @@ -335,7 +335,12 @@ public void testScheduledEventsForJob_withGroup() throws Exception { } private ScheduledEvent buildScheduledEvent(String description, ZonedDateTime start, ZonedDateTime end, String calendarId) { - return new ScheduledEvent.Builder().description(description).startTime(start).endTime(end).calendarId(calendarId).build(); + return new ScheduledEvent.Builder() + .description(description) + .startTime(start.toInstant()) + .endTime(end.toInstant()) + .calendarId(calendarId) + .build(); } public void testGetAutodetectParams() throws Exception { diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/AutodetectControlMsgWriterTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/AutodetectControlMsgWriterTests.java index 27de6633712dc..0f9e1e859b5b8 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/AutodetectControlMsgWriterTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/AutodetectControlMsgWriterTests.java @@ -226,14 +226,14 @@ public void testWriteUpdateScheduledEventsMessage() throws IOException { ScheduledEvent.Builder event1 = new ScheduledEvent.Builder(); event1.calendarId("moon"); event1.description("new year"); - event1.startTime(ZonedDateTime.parse("2018-01-01T00:00:00Z")); - event1.endTime(ZonedDateTime.parse("2018-01-02T00:00:00Z")); + event1.startTime(ZonedDateTime.parse("2018-01-01T00:00:00Z").toInstant()); + event1.endTime(ZonedDateTime.parse("2018-01-02T00:00:00Z").toInstant()); ScheduledEvent.Builder event2 = new ScheduledEvent.Builder(); event2.calendarId("moon"); event2.description("Jan maintenance day"); - event2.startTime(ZonedDateTime.parse("2018-01-06T00:00:00Z")); - event2.endTime(ZonedDateTime.parse("2018-01-07T00:00:00Z")); + event2.startTime(ZonedDateTime.parse("2018-01-06T00:00:00Z").toInstant()); + event2.endTime(ZonedDateTime.parse("2018-01-07T00:00:00Z").toInstant()); writer.writeUpdateScheduledEventsMessage(Arrays.asList(event1.build(), event2.build()), TimeValue.timeValueHours(1)); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/FieldConfigWriterTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/FieldConfigWriterTests.java index d26dbb203c84e..0934d62f5c77c 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/FieldConfigWriterTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/FieldConfigWriterTests.java @@ -30,8 +30,6 @@ import java.io.StringReader; import java.nio.charset.StandardCharsets; import java.time.Instant; -import java.time.ZoneOffset; -import java.time.ZonedDateTime; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -239,12 +237,12 @@ public void testWrite_GivenScheduledEvents() throws IOException { analysisConfig = builder.build(); scheduledEvents.add(new ScheduledEvent.Builder().description("The Ashes") - .startTime(ZonedDateTime.ofInstant(Instant.ofEpochMilli(1511395200000L), ZoneOffset.UTC)) - .endTime(ZonedDateTime.ofInstant(Instant.ofEpochMilli(1515369600000L), ZoneOffset.UTC)) + .startTime(Instant.ofEpochMilli(1511395200000L)) + .endTime(Instant.ofEpochMilli(1515369600000L)) .calendarId("calendar_id").build()); scheduledEvents.add(new ScheduledEvent.Builder().description("elasticon") - .startTime(ZonedDateTime.ofInstant(Instant.ofEpochMilli(1519603200000L), ZoneOffset.UTC)) - .endTime(ZonedDateTime.ofInstant(Instant.ofEpochMilli(1519862400000L), ZoneOffset.UTC)) + .startTime(Instant.ofEpochMilli(1519603200000L)) + .endTime(Instant.ofEpochMilli(1519862400000L)) .calendarId("calendar_id").build()); writer = mock(OutputStreamWriter.class); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/ScheduledEventsWriterTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/ScheduledEventsWriterTests.java index 2806aef128575..ad9ab67468083 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/ScheduledEventsWriterTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/ScheduledEventsWriterTests.java @@ -11,8 +11,6 @@ import java.io.IOException; import java.time.Instant; -import java.time.ZoneOffset; -import java.time.ZonedDateTime; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -32,12 +30,12 @@ public void testWrite_GivenEmpty() throws IOException { public void testWrite() throws IOException { List events = new ArrayList<>(); events.add(new ScheduledEvent.Builder().description("Black Friday") - .startTime(ZonedDateTime.ofInstant(Instant.ofEpochMilli(1511395200000L), ZoneOffset.UTC)) - .endTime(ZonedDateTime.ofInstant(Instant.ofEpochMilli(1515369600000L), ZoneOffset.UTC)) + .startTime(Instant.ofEpochMilli(1511395200000L)) + .endTime(Instant.ofEpochMilli(1515369600000L)) .calendarId("calendar_id").build()); events.add(new ScheduledEvent.Builder().description("Blue Monday") - .startTime(ZonedDateTime.ofInstant(Instant.ofEpochMilli(1519603200000L), ZoneOffset.UTC)) - .endTime(ZonedDateTime.ofInstant(Instant.ofEpochMilli(1519862400000L), ZoneOffset.UTC)) + .startTime(Instant.ofEpochMilli(1519603200000L)) + .endTime(Instant.ofEpochMilli(1519862400000L)) .calendarId("calendar_id").build()); StringBuilder buffer = new StringBuilder(); @@ -54,4 +52,4 @@ public void testWrite() throws IOException { "\n"; assertThat(buffer.toString(), equalTo(expectedString)); } -} \ No newline at end of file +}