Skip to content

Commit a0da390

Browse files
authored
Scripting: 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 99f89cd commit a0da390

File tree

18 files changed

+97
-95
lines changed

18 files changed

+97
-95
lines changed

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

+3-3
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

+3-3
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

+1-2
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

-60
This file was deleted.

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

+9-2
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

@@ -42,11 +44,13 @@
4244
import java.time.temporal.TemporalUnit;
4345
import java.time.temporal.WeekFields;
4446
import java.util.Locale;
47+
import java.util.Objects;
4548

4649
/**
4750
* A wrapper around ZonedDateTime that exposes joda methods for backcompat.
4851
*/
4952
public class JodaCompatibleZonedDateTime {
53+
private static final DateFormatter DATE_FORMATTER = DateFormatters.forPattern("strict_date_time");
5054
private static final DeprecationLogger deprecationLogger =
5155
new DeprecationLogger(LogManager.getLogger(JodaCompatibleZonedDateTime.class));
5256

@@ -75,7 +79,10 @@ public ZonedDateTime getZonedDateTime() {
7579

7680
@Override
7781
public boolean equals(Object o) {
78-
return dt.equals(o);
82+
if (this == o) return true;
83+
if (o == null || getClass() != o.getClass()) return false;
84+
JodaCompatibleZonedDateTime that = (JodaCompatibleZonedDateTime) o;
85+
return Objects.equals(dt, that.dt);
7986
}
8087

8188
@Override
@@ -85,7 +92,7 @@ public int hashCode() {
8592

8693
@Override
8794
public String toString() {
88-
return dt.toString();
95+
return DATE_FORMATTER.format(dt);
8996
}
9097

9198
public boolean isAfter(ZonedDateTime o) {

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

+4
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

+4
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

+5-1
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

+5-1
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

+5-1
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

x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/email/EmailActionTests.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.elasticsearch.common.xcontent.XContentBuilder;
1717
import org.elasticsearch.common.xcontent.XContentParser;
1818
import org.elasticsearch.common.xcontent.json.JsonXContent;
19+
import org.elasticsearch.script.JodaCompatibleZonedDateTime;
1920
import org.elasticsearch.test.ESTestCase;
2021
import org.elasticsearch.xpack.core.watcher.actions.Action;
2122
import org.elasticsearch.xpack.core.watcher.common.secret.Secret;
@@ -52,6 +53,8 @@
5253
import java.io.InputStream;
5354
import java.io.InputStreamReader;
5455
import java.nio.charset.StandardCharsets;
56+
import java.time.Instant;
57+
import java.time.ZoneOffset;
5558
import java.util.ArrayList;
5659
import java.util.HashMap;
5760
import java.util.List;
@@ -129,6 +132,7 @@ public void testExecute() throws Exception {
129132
Map<String, Object> metadata = MapBuilder.<String, Object>newMapBuilder().put("_key", "_val").map();
130133

131134
DateTime now = DateTime.now(DateTimeZone.UTC);
135+
JodaCompatibleZonedDateTime jodaJavaNow = new JodaCompatibleZonedDateTime(Instant.ofEpochMilli(now.getMillis()), ZoneOffset.UTC);
132136

133137
Wid wid = new Wid("watch1", now);
134138
WatchExecutionContext ctx = mockExecutionContextBuilder("watch1")
@@ -139,14 +143,14 @@ public void testExecute() throws Exception {
139143
.buildMock();
140144

141145
Map<String, Object> triggerModel = new HashMap<>();
142-
triggerModel.put("triggered_time", now);
143-
triggerModel.put("scheduled_time", now);
146+
triggerModel.put("triggered_time", jodaJavaNow);
147+
triggerModel.put("scheduled_time", jodaJavaNow);
144148
Map<String, Object> ctxModel = new HashMap<>();
145149
ctxModel.put("id", ctx.id().value());
146150
ctxModel.put("watch_id", "watch1");
147151
ctxModel.put("payload", data);
148152
ctxModel.put("metadata", metadata);
149-
ctxModel.put("execution_time", now);
153+
ctxModel.put("execution_time", jodaJavaNow);
150154
ctxModel.put("trigger", triggerModel);
151155
ctxModel.put("vars", emptyMap());
152156
Map<String, Object> expectedModel = singletonMap("ctx", ctxModel);

x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/hipchat/HipChatActionTests.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.elasticsearch.common.xcontent.XContentParser;
1717
import org.elasticsearch.common.xcontent.XContentType;
1818
import org.elasticsearch.common.xcontent.json.JsonXContent;
19+
import org.elasticsearch.script.JodaCompatibleZonedDateTime;
1920
import org.elasticsearch.test.ESTestCase;
2021
import org.elasticsearch.xpack.core.watcher.actions.Action;
2122
import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext;
@@ -35,6 +36,8 @@
3536
import org.junit.Before;
3637

3738
import java.io.IOException;
39+
import java.time.Instant;
40+
import java.time.ZoneOffset;
3841
import java.util.Arrays;
3942
import java.util.Collections;
4043
import java.util.HashMap;
@@ -78,6 +81,7 @@ public void testExecute() throws Exception {
7881
Map<String, Object> metadata = MapBuilder.<String, Object>newMapBuilder().put("_key", "_val").map();
7982

8083
DateTime now = DateTime.now(DateTimeZone.UTC);
84+
JodaCompatibleZonedDateTime jodaJavaNow = new JodaCompatibleZonedDateTime(Instant.ofEpochMilli(now.getMillis()), ZoneOffset.UTC);
8185

8286
Wid wid = new Wid(randomAlphaOfLength(5), now);
8387
WatchExecutionContext ctx = mockExecutionContextBuilder(wid.watchId())
@@ -88,14 +92,14 @@ public void testExecute() throws Exception {
8892
.buildMock();
8993

9094
Map<String, Object> triggerModel = new HashMap<>();
91-
triggerModel.put("triggered_time", now);
92-
triggerModel.put("scheduled_time", now);
95+
triggerModel.put("triggered_time", jodaJavaNow);
96+
triggerModel.put("scheduled_time", jodaJavaNow);
9397
Map<String, Object> ctxModel = new HashMap<>();
9498
ctxModel.put("id", ctx.id().value());
9599
ctxModel.put("watch_id", wid.watchId());
96100
ctxModel.put("payload", data);
97101
ctxModel.put("metadata", metadata);
98-
ctxModel.put("execution_time", now);
102+
ctxModel.put("execution_time", jodaJavaNow);
99103
ctxModel.put("trigger", triggerModel);
100104
ctxModel.put("vars", Collections.emptyMap());
101105
Map<String, Object> expectedModel = singletonMap("ctx", ctxModel);

x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/logging/LoggingActionTests.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.elasticsearch.common.SuppressLoggerChecks;
1111
import org.elasticsearch.common.xcontent.XContentBuilder;
1212
import org.elasticsearch.common.xcontent.XContentParser;
13+
import org.elasticsearch.script.JodaCompatibleZonedDateTime;
1314
import org.elasticsearch.test.ESTestCase;
1415
import org.elasticsearch.xpack.core.watcher.actions.Action;
1516
import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext;
@@ -22,6 +23,8 @@
2223
import org.junit.Before;
2324

2425
import java.io.IOException;
26+
import java.time.Instant;
27+
import java.time.ZoneOffset;
2528
import java.util.HashMap;
2629
import java.util.Map;
2730

@@ -55,18 +58,19 @@ public void init() throws IOException {
5558

5659
public void testExecute() throws Exception {
5760
final DateTime now = DateTime.now(UTC);
61+
JodaCompatibleZonedDateTime jodaJavaNow = new JodaCompatibleZonedDateTime(Instant.ofEpochMilli(now.getMillis()), ZoneOffset.UTC);
5862

5963
WatchExecutionContext ctx = WatcherTestUtils.mockExecutionContextBuilder("_watch_id")
6064
.time("_watch_id", now)
6165
.buildMock();
6266

6367
Map<String, Object> triggerModel = new HashMap<>();
64-
triggerModel.put("scheduled_time", now);
65-
triggerModel.put("triggered_time", now);
68+
triggerModel.put("scheduled_time", jodaJavaNow);
69+
triggerModel.put("triggered_time", jodaJavaNow);
6670
Map<String, Object> ctxModel = new HashMap<>();
6771
ctxModel.put("id", ctx.id().value());
6872
ctxModel.put("watch_id", "_watch_id");
69-
ctxModel.put("execution_time", now);
73+
ctxModel.put("execution_time", jodaJavaNow);
7074
ctxModel.put("payload", emptyMap());
7175
ctxModel.put("metadata", emptyMap());
7276
ctxModel.put("vars", emptyMap());

0 commit comments

Comments
 (0)