Skip to content

Commit 9b64558

Browse files
authored
Migrating from joda to java.time. Watcher plugin (#35809)
part of the migrating joda time work. Migrating watcher plugin to use JDK's java-time refers #27330
1 parent d975f93 commit 9b64558

File tree

93 files changed

+676
-632
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+676
-632
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/ActionStatus.java

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323
import org.elasticsearch.common.Nullable;
2424
import org.elasticsearch.common.ParseField;
2525
import org.elasticsearch.common.xcontent.XContentParser;
26-
import org.joda.time.DateTime;
27-
import org.joda.time.DateTimeZone;
2826

2927
import java.io.IOException;
28+
import java.time.ZoneOffset;
29+
import java.time.ZonedDateTime;
3030
import java.util.Locale;
3131
import java.util.Objects;
3232

@@ -119,15 +119,16 @@ public enum State {
119119
ACKED;
120120
}
121121

122-
private final DateTime timestamp;
122+
private final ZonedDateTime timestamp;
123123
private final State state;
124124

125-
public AckStatus(DateTime timestamp, State state) {
126-
this.timestamp = timestamp.toDateTime(DateTimeZone.UTC);
125+
public AckStatus(ZonedDateTime timestamp, State state) {
126+
assert timestamp.getOffset() == ZoneOffset.UTC;
127+
this.timestamp = timestamp;
127128
this.state = state;
128129
}
129130

130-
public DateTime timestamp() {
131+
public ZonedDateTime timestamp() {
131132
return timestamp;
132133
}
133134

@@ -151,7 +152,7 @@ public int hashCode() {
151152
}
152153

153154
public static AckStatus parse(String actionId, XContentParser parser) throws IOException {
154-
DateTime timestamp = null;
155+
ZonedDateTime timestamp = null;
155156
State state = null;
156157

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

182183
public static class Execution {
183184

184-
public static Execution successful(DateTime timestamp) {
185+
public static Execution successful(ZonedDateTime timestamp) {
185186
return new Execution(timestamp, true, null);
186187
}
187188

188-
public static Execution failure(DateTime timestamp, String reason) {
189+
public static Execution failure(ZonedDateTime timestamp, String reason) {
189190
return new Execution(timestamp, false, reason);
190191
}
191192

192-
private final DateTime timestamp;
193+
private final ZonedDateTime timestamp;
193194
private final boolean successful;
194195
private final String reason;
195196

196-
private Execution(DateTime timestamp, boolean successful, String reason) {
197-
this.timestamp = timestamp.toDateTime(DateTimeZone.UTC);
197+
private Execution(ZonedDateTime timestamp, boolean successful, String reason) {
198+
this.timestamp = timestamp.withZoneSameInstant(ZoneOffset.UTC);
198199
this.successful = successful;
199200
this.reason = reason;
200201
}
201202

202-
public DateTime timestamp() {
203+
public ZonedDateTime timestamp() {
203204
return timestamp;
204205
}
205206

@@ -229,7 +230,7 @@ public int hashCode() {
229230
}
230231

231232
public static Execution parse(String actionId, XContentParser parser) throws IOException {
232-
DateTime timestamp = null;
233+
ZonedDateTime timestamp = null;
233234
Boolean successful = null;
234235
String reason = null;
235236

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

270271
public static class Throttle {
271272

272-
private final DateTime timestamp;
273+
private final ZonedDateTime timestamp;
273274
private final String reason;
274275

275-
public Throttle(DateTime timestamp, String reason) {
276-
this.timestamp = timestamp.toDateTime(DateTimeZone.UTC);
276+
public Throttle(ZonedDateTime timestamp, String reason) {
277+
this.timestamp = timestamp.withZoneSameInstant(ZoneOffset.UTC);
277278
this.reason = reason;
278279
}
279280

280-
public DateTime timestamp() {
281+
public ZonedDateTime timestamp() {
281282
return timestamp;
282283
}
283284

@@ -300,7 +301,7 @@ public int hashCode() {
300301
}
301302

302303
public static Throttle parse(String actionId, XContentParser parser) throws IOException {
303-
DateTime timestamp = null;
304+
ZonedDateTime timestamp = null;
304305
String reason = null;
305306

306307
String currentFieldName = null;

client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/WatchStatus.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@
2323
import org.elasticsearch.common.Nullable;
2424
import org.elasticsearch.common.ParseField;
2525
import org.elasticsearch.common.xcontent.XContentParser;
26-
import org.joda.time.DateTime;
2726

2827
import java.io.IOException;
28+
import java.time.ZoneOffset;
29+
import java.time.ZonedDateTime;
2930
import java.util.Collections;
3031
import java.util.HashMap;
3132
import java.util.Map;
@@ -35,24 +36,23 @@
3536
import static java.util.Collections.unmodifiableMap;
3637
import static org.elasticsearch.client.watcher.WatchStatusDateParser.parseDate;
3738
import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken;
38-
import static org.joda.time.DateTimeZone.UTC;
3939

4040
public class WatchStatus {
4141

4242
private final State state;
4343

4444
private final ExecutionState executionState;
45-
private final DateTime lastChecked;
46-
private final DateTime lastMetCondition;
45+
private final ZonedDateTime lastChecked;
46+
private final ZonedDateTime lastMetCondition;
4747
private final long version;
4848
private final Map<String, ActionStatus> actions;
4949
@Nullable private Map<String, String> headers;
5050

5151
public WatchStatus(long version,
5252
State state,
5353
ExecutionState executionState,
54-
DateTime lastChecked,
55-
DateTime lastMetCondition,
54+
ZonedDateTime lastChecked,
55+
ZonedDateTime lastMetCondition,
5656
Map<String, ActionStatus> actions,
5757
Map<String, String> headers) {
5858
this.version = version;
@@ -72,11 +72,11 @@ public boolean checked() {
7272
return lastChecked != null;
7373
}
7474

75-
public DateTime lastChecked() {
75+
public ZonedDateTime lastChecked() {
7676
return lastChecked;
7777
}
7878

79-
public DateTime lastMetCondition() {
79+
public ZonedDateTime lastMetCondition() {
8080
return lastMetCondition;
8181
}
8282

@@ -123,8 +123,8 @@ public int hashCode() {
123123
public static WatchStatus parse(XContentParser parser) throws IOException {
124124
State state = null;
125125
ExecutionState executionState = null;
126-
DateTime lastChecked = null;
127-
DateTime lastMetCondition = null;
126+
ZonedDateTime lastChecked = null;
127+
ZonedDateTime lastMetCondition = null;
128128
Map<String, ActionStatus> actions = null;
129129
Map<String, String> headers = Collections.emptyMap();
130130
long version = -1;
@@ -203,9 +203,9 @@ public static WatchStatus parse(XContentParser parser) throws IOException {
203203
public static class State {
204204

205205
private final boolean active;
206-
private final DateTime timestamp;
206+
private final ZonedDateTime timestamp;
207207

208-
public State(boolean active, DateTime timestamp) {
208+
public State(boolean active, ZonedDateTime timestamp) {
209209
this.active = active;
210210
this.timestamp = timestamp;
211211
}
@@ -214,7 +214,7 @@ public boolean isActive() {
214214
return active;
215215
}
216216

217-
public DateTime getTimestamp() {
217+
public ZonedDateTime getTimestamp() {
218218
return timestamp;
219219
}
220220

@@ -223,7 +223,7 @@ public static State parse(XContentParser parser) throws IOException {
223223
throw new ElasticsearchParseException("expected an object but found [{}] instead", parser.currentToken());
224224
}
225225
boolean active = true;
226-
DateTime timestamp = DateTime.now(UTC);
226+
ZonedDateTime timestamp = ZonedDateTime.now(ZoneOffset.UTC);
227227
String currentFieldName = null;
228228
XContentParser.Token token;
229229
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {

client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/WatchStatusDateParser.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@
2121

2222
import org.elasticsearch.ElasticsearchParseException;
2323
import org.elasticsearch.common.time.DateFormatter;
24+
import org.elasticsearch.common.time.DateFormatters;
2425
import org.elasticsearch.common.xcontent.XContentParser;
2526
import org.elasticsearch.index.mapper.DateFieldMapper;
26-
import org.joda.time.DateTime;
27-
import org.joda.time.DateTimeZone;
2827

2928
import java.io.IOException;
29+
import java.time.Instant;
30+
import java.time.ZoneOffset;
31+
import java.time.ZonedDateTime;
3032

3133
public final class WatchStatusDateParser {
3234

@@ -36,14 +38,14 @@ private WatchStatusDateParser() {
3638
// Prevent instantiation.
3739
}
3840

39-
public static DateTime parseDate(String fieldName, XContentParser parser) throws IOException {
41+
public static ZonedDateTime parseDate(String fieldName, XContentParser parser) throws IOException {
4042
XContentParser.Token token = parser.currentToken();
4143
if (token == XContentParser.Token.VALUE_NUMBER) {
42-
return new DateTime(parser.longValue(), DateTimeZone.UTC);
44+
return Instant.ofEpochMilli(parser.longValue()).atZone(ZoneOffset.UTC);
4345
}
4446
if (token == XContentParser.Token.VALUE_STRING) {
45-
DateTime dateTime = parseDate(parser.text());
46-
return dateTime.toDateTime(DateTimeZone.UTC);
47+
ZonedDateTime dateTime = parseDate(parser.text());
48+
return dateTime.withZoneSameInstant(ZoneOffset.UTC);
4749
}
4850
if (token == XContentParser.Token.VALUE_NULL) {
4951
return null;
@@ -52,7 +54,7 @@ public static DateTime parseDate(String fieldName, XContentParser parser) throws
5254
"to be either a number or a string but found [{}] instead", fieldName, token);
5355
}
5456

55-
public static DateTime parseDate(String text) {
56-
return FORMATTER.parseJoda(text);
57+
public static ZonedDateTime parseDate(String text) {
58+
return DateFormatters.from(FORMATTER.parse(text));
5759
}
5860
}

client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/WatchStatusTests.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@
2727
import org.elasticsearch.common.xcontent.XContentType;
2828
import org.elasticsearch.test.ESTestCase;
2929
import org.elasticsearch.test.XContentTestUtils;
30-
import org.joda.time.DateTime;
31-
import org.joda.time.DateTimeZone;
3230

3331
import java.io.IOException;
32+
import java.time.Instant;
33+
import java.time.ZoneOffset;
34+
import java.time.ZonedDateTime;
3435
import java.util.function.Predicate;
3536

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

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

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

6061
ActionStatus actionStatus = watchStatus.actionStatus("test_index");
6162
assertNotNull(actionStatus);
6263

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

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

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

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

server/src/test/java/org/elasticsearch/common/time/DateFormattersTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ public void testRoundupFormatterWithEpochDates() {
204204
assertRoundupFormatter("strict_date_optional_time||epoch_millis", "2018-10-10T12:13:14.123Z", 1539173594123L);
205205
assertRoundupFormatter("strict_date_optional_time||epoch_millis", "1234567890", 1234567890L);
206206
assertRoundupFormatter("strict_date_optional_time||epoch_millis", "2018-10-10", 1539215999999L);
207+
assertRoundupFormatter("strict_date_optional_time||epoch_millis", "2019-01-25T15:37:17.346928Z", 1548430637346L);
207208
assertRoundupFormatter("uuuu-MM-dd'T'HH:mm:ss.SSS||epoch_millis", "2018-10-10T12:13:14.123", 1539173594123L);
208209
assertRoundupFormatter("uuuu-MM-dd'T'HH:mm:ss.SSS||epoch_millis", "1234567890", 1234567890L);
209210

0 commit comments

Comments
 (0)