Skip to content

Commit

Permalink
Migrating from joda to java.time. Watcher plugin (#35809)
Browse files Browse the repository at this point in the history
part of the migrating joda time work. Migrating watcher plugin to use JDK's java-time

refers #27330
  • Loading branch information
pgomulka committed Feb 4, 2019
1 parent d975f93 commit 9b64558
Show file tree
Hide file tree
Showing 93 changed files with 676 additions and 632 deletions.
Expand Up @@ -23,10 +23,10 @@
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.xcontent.XContentParser;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

import java.io.IOException;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Locale;
import java.util.Objects;

Expand Down Expand Up @@ -119,15 +119,16 @@ public enum State {
ACKED;
}

private final DateTime timestamp;
private final ZonedDateTime timestamp;
private final State state;

public AckStatus(DateTime timestamp, State state) {
this.timestamp = timestamp.toDateTime(DateTimeZone.UTC);
public AckStatus(ZonedDateTime timestamp, State state) {
assert timestamp.getOffset() == ZoneOffset.UTC;
this.timestamp = timestamp;
this.state = state;
}

public DateTime timestamp() {
public ZonedDateTime timestamp() {
return timestamp;
}

Expand All @@ -151,7 +152,7 @@ public int hashCode() {
}

public static AckStatus parse(String actionId, XContentParser parser) throws IOException {
DateTime timestamp = null;
ZonedDateTime timestamp = null;
State state = null;

String currentFieldName = null;
Expand Down Expand Up @@ -181,25 +182,25 @@ public static AckStatus parse(String actionId, XContentParser parser) throws IOE

public static class Execution {

public static Execution successful(DateTime timestamp) {
public static Execution successful(ZonedDateTime timestamp) {
return new Execution(timestamp, true, null);
}

public static Execution failure(DateTime timestamp, String reason) {
public static Execution failure(ZonedDateTime timestamp, String reason) {
return new Execution(timestamp, false, reason);
}

private final DateTime timestamp;
private final ZonedDateTime timestamp;
private final boolean successful;
private final String reason;

private Execution(DateTime timestamp, boolean successful, String reason) {
this.timestamp = timestamp.toDateTime(DateTimeZone.UTC);
private Execution(ZonedDateTime timestamp, boolean successful, String reason) {
this.timestamp = timestamp.withZoneSameInstant(ZoneOffset.UTC);
this.successful = successful;
this.reason = reason;
}

public DateTime timestamp() {
public ZonedDateTime timestamp() {
return timestamp;
}

Expand Down Expand Up @@ -229,7 +230,7 @@ public int hashCode() {
}

public static Execution parse(String actionId, XContentParser parser) throws IOException {
DateTime timestamp = null;
ZonedDateTime timestamp = null;
Boolean successful = null;
String reason = null;

Expand Down Expand Up @@ -269,15 +270,15 @@ public static Execution parse(String actionId, XContentParser parser) throws IOE

public static class Throttle {

private final DateTime timestamp;
private final ZonedDateTime timestamp;
private final String reason;

public Throttle(DateTime timestamp, String reason) {
this.timestamp = timestamp.toDateTime(DateTimeZone.UTC);
public Throttle(ZonedDateTime timestamp, String reason) {
this.timestamp = timestamp.withZoneSameInstant(ZoneOffset.UTC);
this.reason = reason;
}

public DateTime timestamp() {
public ZonedDateTime timestamp() {
return timestamp;
}

Expand All @@ -300,7 +301,7 @@ public int hashCode() {
}

public static Throttle parse(String actionId, XContentParser parser) throws IOException {
DateTime timestamp = null;
ZonedDateTime timestamp = null;
String reason = null;

String currentFieldName = null;
Expand Down
Expand Up @@ -23,9 +23,10 @@
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.xcontent.XContentParser;
import org.joda.time.DateTime;

import java.io.IOException;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -35,24 +36,23 @@
import static java.util.Collections.unmodifiableMap;
import static org.elasticsearch.client.watcher.WatchStatusDateParser.parseDate;
import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken;
import static org.joda.time.DateTimeZone.UTC;

public class WatchStatus {

private final State state;

private final ExecutionState executionState;
private final DateTime lastChecked;
private final DateTime lastMetCondition;
private final ZonedDateTime lastChecked;
private final ZonedDateTime lastMetCondition;
private final long version;
private final Map<String, ActionStatus> actions;
@Nullable private Map<String, String> headers;

public WatchStatus(long version,
State state,
ExecutionState executionState,
DateTime lastChecked,
DateTime lastMetCondition,
ZonedDateTime lastChecked,
ZonedDateTime lastMetCondition,
Map<String, ActionStatus> actions,
Map<String, String> headers) {
this.version = version;
Expand All @@ -72,11 +72,11 @@ public boolean checked() {
return lastChecked != null;
}

public DateTime lastChecked() {
public ZonedDateTime lastChecked() {
return lastChecked;
}

public DateTime lastMetCondition() {
public ZonedDateTime lastMetCondition() {
return lastMetCondition;
}

Expand Down Expand Up @@ -123,8 +123,8 @@ public int hashCode() {
public static WatchStatus parse(XContentParser parser) throws IOException {
State state = null;
ExecutionState executionState = null;
DateTime lastChecked = null;
DateTime lastMetCondition = null;
ZonedDateTime lastChecked = null;
ZonedDateTime lastMetCondition = null;
Map<String, ActionStatus> actions = null;
Map<String, String> headers = Collections.emptyMap();
long version = -1;
Expand Down Expand Up @@ -203,9 +203,9 @@ public static WatchStatus parse(XContentParser parser) throws IOException {
public static class State {

private final boolean active;
private final DateTime timestamp;
private final ZonedDateTime timestamp;

public State(boolean active, DateTime timestamp) {
public State(boolean active, ZonedDateTime timestamp) {
this.active = active;
this.timestamp = timestamp;
}
Expand All @@ -214,7 +214,7 @@ public boolean isActive() {
return active;
}

public DateTime getTimestamp() {
public ZonedDateTime getTimestamp() {
return timestamp;
}

Expand All @@ -223,7 +223,7 @@ public static State parse(XContentParser parser) throws IOException {
throw new ElasticsearchParseException("expected an object but found [{}] instead", parser.currentToken());
}
boolean active = true;
DateTime timestamp = DateTime.now(UTC);
ZonedDateTime timestamp = ZonedDateTime.now(ZoneOffset.UTC);
String currentFieldName = null;
XContentParser.Token token;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
Expand Down
Expand Up @@ -21,12 +21,14 @@

import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.common.time.DateFormatter;
import org.elasticsearch.common.time.DateFormatters;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.mapper.DateFieldMapper;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

import java.io.IOException;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;

public final class WatchStatusDateParser {

Expand All @@ -36,14 +38,14 @@ private WatchStatusDateParser() {
// Prevent instantiation.
}

public static DateTime parseDate(String fieldName, XContentParser parser) throws IOException {
public static ZonedDateTime parseDate(String fieldName, XContentParser parser) throws IOException {
XContentParser.Token token = parser.currentToken();
if (token == XContentParser.Token.VALUE_NUMBER) {
return new DateTime(parser.longValue(), DateTimeZone.UTC);
return Instant.ofEpochMilli(parser.longValue()).atZone(ZoneOffset.UTC);
}
if (token == XContentParser.Token.VALUE_STRING) {
DateTime dateTime = parseDate(parser.text());
return dateTime.toDateTime(DateTimeZone.UTC);
ZonedDateTime dateTime = parseDate(parser.text());
return dateTime.withZoneSameInstant(ZoneOffset.UTC);
}
if (token == XContentParser.Token.VALUE_NULL) {
return null;
Expand All @@ -52,7 +54,7 @@ public static DateTime parseDate(String fieldName, XContentParser parser) throws
"to be either a number or a string but found [{}] instead", fieldName, token);
}

public static DateTime parseDate(String text) {
return FORMATTER.parseJoda(text);
public static ZonedDateTime parseDate(String text) {
return DateFormatters.from(FORMATTER.parse(text));
}
}
Expand Up @@ -27,10 +27,11 @@
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.XContentTestUtils;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

import java.io.IOException;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.function.Predicate;

public class WatchStatusTests extends ESTestCase {
Expand All @@ -50,32 +51,32 @@ public void testBasicParsing() throws IOException {
assertEquals(expectedVersion, watchStatus.version());
assertEquals(expectedExecutionState, watchStatus.getExecutionState());

assertEquals(new DateTime(1432663467763L, DateTimeZone.UTC), watchStatus.lastChecked());
assertEquals(DateTime.parse("2015-05-26T18:04:27.763Z"), watchStatus.lastMetCondition());
assertEquals(Instant.ofEpochMilli(1432663467763L).atZone(ZoneOffset.UTC), watchStatus.lastChecked());
assertEquals(ZonedDateTime.parse("2015-05-26T18:04:27.763Z"), watchStatus.lastMetCondition());

WatchStatus.State watchState = watchStatus.state();
assertEquals(expectedActive, watchState.isActive());
assertEquals(DateTime.parse("2015-05-26T18:04:27.723Z"), watchState.getTimestamp());
assertEquals(ZonedDateTime.parse("2015-05-26T18:04:27.723Z"), watchState.getTimestamp());

ActionStatus actionStatus = watchStatus.actionStatus("test_index");
assertNotNull(actionStatus);

ActionStatus.AckStatus ackStatus = actionStatus.ackStatus();
assertEquals(DateTime.parse("2015-05-26T18:04:27.763Z"), ackStatus.timestamp());
assertEquals(ZonedDateTime.parse("2015-05-26T18:04:27.763Z"), ackStatus.timestamp());
assertEquals(expectedAckState, ackStatus.state());

ActionStatus.Execution lastExecution = actionStatus.lastExecution();
assertEquals(DateTime.parse("2015-05-25T18:04:27.733Z"), lastExecution.timestamp());
assertEquals(ZonedDateTime.parse("2015-05-25T18:04:27.733Z"), lastExecution.timestamp());
assertFalse(lastExecution.successful());
assertEquals("failed to send email", lastExecution.reason());

ActionStatus.Execution lastSuccessfulExecution = actionStatus.lastSuccessfulExecution();
assertEquals(DateTime.parse("2015-05-25T18:04:27.773Z"), lastSuccessfulExecution.timestamp());
assertEquals(ZonedDateTime.parse("2015-05-25T18:04:27.773Z"), lastSuccessfulExecution.timestamp());
assertTrue(lastSuccessfulExecution.successful());
assertNull(lastSuccessfulExecution.reason());

ActionStatus.Throttle lastThrottle = actionStatus.lastThrottle();
assertEquals(DateTime.parse("2015-04-25T18:05:23.445Z"), lastThrottle.timestamp());
assertEquals(ZonedDateTime.parse("2015-04-25T18:05:23.445Z"), lastThrottle.timestamp());
assertEquals("throttling interval is set to [5 seconds] ...", lastThrottle.reason());
}

Expand Down
Expand Up @@ -204,6 +204,7 @@ public void testRoundupFormatterWithEpochDates() {
assertRoundupFormatter("strict_date_optional_time||epoch_millis", "2018-10-10T12:13:14.123Z", 1539173594123L);
assertRoundupFormatter("strict_date_optional_time||epoch_millis", "1234567890", 1234567890L);
assertRoundupFormatter("strict_date_optional_time||epoch_millis", "2018-10-10", 1539215999999L);
assertRoundupFormatter("strict_date_optional_time||epoch_millis", "2019-01-25T15:37:17.346928Z", 1548430637346L);
assertRoundupFormatter("uuuu-MM-dd'T'HH:mm:ss.SSS||epoch_millis", "2018-10-10T12:13:14.123", 1539173594123L);
assertRoundupFormatter("uuuu-MM-dd'T'HH:mm:ss.SSS||epoch_millis", "1234567890", 1234567890L);

Expand Down

0 comments on commit 9b64558

Please sign in to comment.