Skip to content

Commit 13ae4b1

Browse files
authored
Core: Switch watcher to use joda bwc time objects (#35966)
This commit converts the watcher execution context to use the joda compat java time objects. It also again removes the joda methods from the painless whitelist.
1 parent 9bf0999 commit 13ae4b1

File tree

18 files changed

+91
-93
lines changed

18 files changed

+91
-93
lines changed

docs/painless/painless-contexts/painless-watcher-condition-context.asciidoc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ to test if a response is necessary.
1212
`ctx['watch_id']` (`String`, read-only)::
1313
The id of the watch.
1414

15-
`ctx['execution_time']` (`DateTime`, read-only)::
15+
`ctx['execution_time']` (`ZonedDateTime`, read-only)::
1616
The start time for the watch.
1717

18-
`ctx['trigger']['scheduled_time']` (`DateTime`, read-only)::
18+
`ctx['trigger']['scheduled_time']` (`ZonedDateTime`, read-only)::
1919
The scheduled trigger time for the watch.
2020

21-
`ctx['trigger']['triggered_time']` (`DateTime`, read-only)::
21+
`ctx['trigger']['triggered_time']` (`ZonedDateTime`, read-only)::
2222
The actual trigger time for the watch.
2323

2424
`ctx['metadata']` (`Map`, read-only)::

docs/painless/painless-contexts/painless-watcher-transform-context.asciidoc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ data into a new payload for use in a response to a condition.
1212
`ctx['watch_id']` (`String`, read-only)::
1313
The id of the watch.
1414

15-
`ctx['execution_time']` (`DateTime`, read-only)::
15+
`ctx['execution_time']` (`ZonedDateTime`, read-only)::
1616
The start time for the watch.
1717

18-
`ctx['trigger']['scheduled_time']` (`DateTime`, read-only)::
18+
`ctx['trigger']['scheduled_time']` (`ZonedDateTime`, read-only)::
1919
The scheduled trigger time for the watch.
2020

21-
`ctx['trigger']['triggered_time']` (`DateTime`, read-only)::
21+
`ctx['trigger']['triggered_time']` (`ZonedDateTime`, read-only)::
2222
The actual trigger time for the watch.
2323

2424
`ctx['metadata']` (`Map`, read-only)::

modules/lang-painless/spi/src/main/java/org/elasticsearch/painless/spi/Whitelist.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ public final class Whitelist {
4848
"java.util.txt",
4949
"java.util.function.txt",
5050
"java.util.regex.txt",
51-
"java.util.stream.txt",
52-
"joda.time.txt"
51+
"java.util.stream.txt"
5352
};
5453

5554
public static final List<Whitelist> BASE_WHITELISTS =

modules/lang-painless/src/main/resources/org/elasticsearch/painless/spi/joda.time.txt

Lines changed: 0 additions & 60 deletions
This file was deleted.

server/src/main/java/org/elasticsearch/script/JodaCompatibleZonedDateTime.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import org.apache.logging.log4j.LogManager;
2323
import org.elasticsearch.common.SuppressForbidden;
2424
import org.elasticsearch.common.logging.DeprecationLogger;
25+
import org.elasticsearch.common.time.DateFormatter;
26+
import org.elasticsearch.common.time.DateFormatters;
2527
import org.elasticsearch.common.time.DateUtils;
2628
import org.joda.time.DateTime;
2729

@@ -50,6 +52,7 @@
5052
public class JodaCompatibleZonedDateTime {
5153
private static final DeprecationLogger DEPRECATION_LOGGER =
5254
new DeprecationLogger(LogManager.getLogger(JodaCompatibleZonedDateTime.class));
55+
private static final DateFormatter DATE_FORMATTER = DateFormatters.forPattern("strict_date_time");
5356

5457
private static void logDeprecated(String key, String message, Object... params) {
5558
// NOTE: we don't check SpecialPermission because this will be called (indirectly) from scripts
@@ -89,7 +92,7 @@ public int hashCode() {
8992

9093
@Override
9194
public String toString() {
92-
return dt.toString();
95+
return DATE_FORMATTER.format(dt);
9396
}
9497

9598
public boolean isAfter(ZonedDateTime o) {

server/src/test/java/org/elasticsearch/script/JodaCompatibleZonedDateTimeTests.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ void assertMethodDeprecation(Runnable assertions, String oldMethod, String newMe
9797
assertDeprecation(assertions, "Use of the joda time method [" + oldMethod + "] is deprecated. Use [" + newMethod + "] instead.");
9898
}
9999

100+
public void testEquals() {
101+
assertThat(javaTime, equalTo(javaTime));
102+
}
103+
100104
public void testToString() {
101105
assertThat(javaTime.toString(), equalTo(jodaTime.toString()));
102106
}

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/support/WatcherDateTimeUtils.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.elasticsearch.common.xcontent.XContentBuilder;
1616
import org.elasticsearch.common.xcontent.XContentParser;
1717
import org.elasticsearch.index.mapper.DateFieldMapper;
18+
import org.elasticsearch.script.JodaCompatibleZonedDateTime;
1819
import org.joda.time.DateTime;
1920
import org.joda.time.DateTimeZone;
2021

@@ -35,6 +36,9 @@ public static DateTime convertToDate(Object value, Clock clock) {
3536
if (value instanceof DateTime) {
3637
return (DateTime) value;
3738
}
39+
if (value instanceof JodaCompatibleZonedDateTime) {
40+
return new DateTime(((JodaCompatibleZonedDateTime) value).toInstant().toEpochMilli(), DateTimeZone.UTC);
41+
}
3842
if (value instanceof String) {
3943
return parseDateMath((String) value, DateTimeZone.UTC, clock);
4044
}

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/trigger/TriggerEvent.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@
88
import org.elasticsearch.common.ParseField;
99
import org.elasticsearch.common.xcontent.ToXContentObject;
1010
import org.elasticsearch.common.xcontent.XContentBuilder;
11+
import org.elasticsearch.script.JodaCompatibleZonedDateTime;
1112
import org.elasticsearch.xpack.core.watcher.support.WatcherDateTimeUtils;
1213
import org.joda.time.DateTime;
1314

1415
import java.io.IOException;
16+
import java.time.Instant;
17+
import java.time.ZoneOffset;
1518
import java.util.HashMap;
1619
import java.util.Map;
1720

@@ -25,7 +28,8 @@ public TriggerEvent(String jobName, DateTime triggeredTime) {
2528
this.jobName = jobName;
2629
this.triggeredTime = triggeredTime;
2730
this.data = new HashMap<>();
28-
data.put(Field.TRIGGERED_TIME.getPreferredName(), triggeredTime);
31+
data.put(Field.TRIGGERED_TIME.getPreferredName(),
32+
new JodaCompatibleZonedDateTime(Instant.ofEpochMilli(triggeredTime.getMillis()), ZoneOffset.UTC));
2933
}
3034

3135
public String jobName() {

x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/support/Variables.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
*/
66
package org.elasticsearch.xpack.watcher.support;
77

8+
import org.elasticsearch.script.JodaCompatibleZonedDateTime;
89
import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext;
910
import org.elasticsearch.xpack.core.watcher.watch.Payload;
1011

12+
import java.time.Instant;
13+
import java.time.ZoneOffset;
1114
import java.util.HashMap;
1215
import java.util.Map;
1316

@@ -34,7 +37,8 @@ public static Map<String, Object> createCtx(WatchExecutionContext ctx, Payload p
3437
Map<String, Object> ctxModel = new HashMap<>();
3538
ctxModel.put(ID, ctx.id().value());
3639
ctxModel.put(WATCH_ID, ctx.id().watchId());
37-
ctxModel.put(EXECUTION_TIME, ctx.executionTime());
40+
ctxModel.put(EXECUTION_TIME,
41+
new JodaCompatibleZonedDateTime(Instant.ofEpochMilli(ctx.executionTime().getMillis()), ZoneOffset.UTC));
3842
ctxModel.put(TRIGGER, ctx.triggerEvent().data());
3943
if (payload != null) {
4044
ctxModel.put(PAYLOAD, payload.data());

x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/ScheduleTriggerEvent.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@
99
import org.elasticsearch.common.ParseField;
1010
import org.elasticsearch.common.xcontent.XContentBuilder;
1111
import org.elasticsearch.common.xcontent.XContentParser;
12+
import org.elasticsearch.script.JodaCompatibleZonedDateTime;
1213
import org.elasticsearch.xpack.core.watcher.support.WatcherDateTimeUtils;
1314
import org.elasticsearch.xpack.core.watcher.trigger.TriggerEvent;
1415
import org.joda.time.DateTime;
1516
import org.joda.time.DateTimeZone;
1617

1718
import java.io.IOException;
1819
import java.time.Clock;
20+
import java.time.Instant;
21+
import java.time.ZoneOffset;
1922

2023
public class ScheduleTriggerEvent extends TriggerEvent {
2124

@@ -28,7 +31,8 @@ public ScheduleTriggerEvent(DateTime triggeredTime, DateTime scheduledTime) {
2831
public ScheduleTriggerEvent(String jobName, DateTime triggeredTime, DateTime scheduledTime) {
2932
super(jobName, triggeredTime);
3033
this.scheduledTime = scheduledTime;
31-
data.put(Field.SCHEDULED_TIME.getPreferredName(), scheduledTime);
34+
data.put(Field.SCHEDULED_TIME.getPreferredName(),
35+
new JodaCompatibleZonedDateTime(Instant.ofEpochMilli(scheduledTime.getMillis()), ZoneOffset.UTC));
3236
}
3337

3438
@Override

0 commit comments

Comments
 (0)