Skip to content

Commit a17e0e8

Browse files
authored
Core: Add DateFormatter interface for java time parsing (#33716)
The existing approach used date formatters when a format based string like `date_time||epoch_millis` was used, instead of the custom code. In order to properly solve this, a new interface called `DateFormatter` has been added, which now can be implemented for custom formatters. Currently there are two implementations, one using java time and one doing the epoch_millis formatter, which simply parses a number and then converts it to a date in UTC timezone. The DateFormatter interface now also has a method to retrieve the name of the formatter pattern, which is needed for mapping changes anyway. The existing `CompoundDateTimeFormatter` class has been removed, the name was not really nice anyway. One more minor change is the fact, that the new java time using FormatDateFormatter does not try to parse the date with its printer implementation first (which might be a strict one and fail), but a printer can now be specified in addition. This saves one potential failure/exception when parsing less strict dates. If only a printer is specified, the printer will also be used as a parser. This is the 6.x backport of #33467
1 parent b3962c1 commit a17e0e8

File tree

17 files changed

+552
-293
lines changed

17 files changed

+552
-293
lines changed

server/src/main/java/org/elasticsearch/action/admin/cluster/allocation/ClusterAllocationExplanation.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.elasticsearch.index.shard.ShardId;
3636

3737
import java.io.IOException;
38+
import java.time.Instant;
3839
import java.util.Locale;
3940

4041
import static org.elasticsearch.cluster.routing.allocation.AbstractAllocationDecision.discoveryNodeToXContent;
@@ -189,7 +190,8 @@ private XContentBuilder unassignedInfoToXContent(UnassignedInfo unassignedInfo,
189190

190191
builder.startObject("unassigned_info");
191192
builder.field("reason", unassignedInfo.getReason());
192-
builder.field("at", UnassignedInfo.DATE_TIME_FORMATTER.printer().print(unassignedInfo.getUnassignedTimeInMillis()));
193+
builder.field("at",
194+
UnassignedInfo.DATE_TIME_FORMATTER.format(Instant.ofEpochMilli(unassignedInfo.getUnassignedTimeInMillis())));
193195
if (unassignedInfo.getNumFailedAllocations() > 0) {
194196
builder.field("failed_allocation_attempts", unassignedInfo.getNumFailedAllocations());
195197
}

server/src/main/java/org/elasticsearch/cluster/routing/UnassignedInfo.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,20 @@
2828
import org.elasticsearch.common.io.stream.StreamInput;
2929
import org.elasticsearch.common.io.stream.StreamOutput;
3030
import org.elasticsearch.common.io.stream.Writeable;
31-
import org.elasticsearch.common.joda.FormatDateTimeFormatter;
32-
import org.elasticsearch.common.joda.Joda;
3331
import org.elasticsearch.common.logging.DeprecationLogger;
3432
import org.elasticsearch.common.logging.Loggers;
3533
import org.elasticsearch.common.settings.Setting;
3634
import org.elasticsearch.common.settings.Setting.Property;
3735
import org.elasticsearch.common.settings.Settings;
36+
import org.elasticsearch.common.time.DateFormatter;
37+
import org.elasticsearch.common.time.DateFormatters;
3838
import org.elasticsearch.common.unit.TimeValue;
39-
import org.elasticsearch.common.xcontent.ToXContent.Params;
4039
import org.elasticsearch.common.xcontent.ToXContentFragment;
4140
import org.elasticsearch.common.xcontent.XContentBuilder;
4241

4342
import java.io.IOException;
43+
import java.time.Instant;
44+
import java.time.ZoneOffset;
4445
import java.util.Locale;
4546
import java.util.Objects;
4647

@@ -51,7 +52,7 @@ public final class UnassignedInfo implements ToXContentFragment, Writeable {
5152

5253
private static final DeprecationLogger DEPRECATION_LOGGER = new DeprecationLogger(Loggers.getLogger(UnassignedInfo.class));
5354

54-
public static final FormatDateTimeFormatter DATE_TIME_FORMATTER = Joda.forPattern("dateOptionalTime");
55+
public static final DateFormatter DATE_TIME_FORMATTER = DateFormatters.forPattern("dateOptionalTime").withZone(ZoneOffset.UTC);
5556

5657
public static final Setting<TimeValue> INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING =
5758
new Setting<>("index.unassigned.node_left.delayed_timeout", (s) -> TimeValue.timeValueMinutes(1).getStringRep(), (s) -> {
@@ -421,7 +422,7 @@ public static long findNextDelayedAllocation(long currentNanoTime, ClusterState
421422
public String shortSummary() {
422423
StringBuilder sb = new StringBuilder();
423424
sb.append("[reason=").append(reason).append("]");
424-
sb.append(", at[").append(DATE_TIME_FORMATTER.printer().print(unassignedTimeMillis)).append("]");
425+
sb.append(", at[").append(DATE_TIME_FORMATTER.format(Instant.ofEpochMilli(unassignedTimeMillis))).append("]");
425426
if (failedAllocations > 0) {
426427
sb.append(", failed_attempts[").append(failedAllocations).append("]");
427428
}
@@ -444,7 +445,7 @@ public String toString() {
444445
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
445446
builder.startObject("unassigned_info");
446447
builder.field("reason", reason);
447-
builder.field("at", DATE_TIME_FORMATTER.printer().print(unassignedTimeMillis));
448+
builder.field("at", DATE_TIME_FORMATTER.format(Instant.ofEpochMilli(unassignedTimeMillis)));
448449
if (failedAllocations > 0) {
449450
builder.field("failed_attempts", failedAllocations);
450451
}

server/src/main/java/org/elasticsearch/common/Table.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919

2020
package org.elasticsearch.common;
2121

22-
import org.joda.time.format.DateTimeFormat;
23-
import org.joda.time.format.DateTimeFormatter;
22+
import org.elasticsearch.common.time.DateFormatter;
23+
import org.elasticsearch.common.time.DateFormatters;
2424

25+
import java.time.Instant;
26+
import java.time.ZoneOffset;
2527
import java.util.ArrayList;
2628
import java.util.HashMap;
2729
import java.util.List;
@@ -83,7 +85,7 @@ public Table endHeaders() {
8385
return this;
8486
}
8587

86-
private DateTimeFormatter dateFormat = DateTimeFormat.forPattern("HH:mm:ss");
88+
private static final DateFormatter FORMATTER = DateFormatters.forPattern("HH:mm:ss").withZone(ZoneOffset.UTC);
8789

8890
public Table startRow() {
8991
if (headers.isEmpty()) {
@@ -93,7 +95,7 @@ public Table startRow() {
9395
if (withTime) {
9496
long time = System.currentTimeMillis();
9597
addCell(TimeUnit.SECONDS.convert(time, TimeUnit.MILLISECONDS));
96-
addCell(dateFormat.print(time));
98+
addCell(FORMATTER.format(Instant.ofEpochMilli(time)));
9799
}
98100
return this;
99101
}

server/src/main/java/org/elasticsearch/common/time/CompoundDateTimeFormatter.java

Lines changed: 0 additions & 74 deletions
This file was deleted.
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.common.time;
21+
22+
import java.time.ZoneId;
23+
import java.time.format.DateTimeParseException;
24+
import java.time.temporal.TemporalAccessor;
25+
import java.time.temporal.TemporalField;
26+
import java.util.Arrays;
27+
import java.util.Map;
28+
import java.util.stream.Collectors;
29+
30+
public interface DateFormatter {
31+
32+
/**
33+
* Try to parse input to a java time TemporalAccessor
34+
* @param input An arbitrary string resembling the string representation of a date or time
35+
* @throws DateTimeParseException If parsing fails, this exception will be thrown.
36+
* Note that it can contained suppressed exceptions when several formatters failed parse this value
37+
* @return The java time object containing the parsed input
38+
*/
39+
TemporalAccessor parse(String input);
40+
41+
/**
42+
* Create a copy of this formatter that is configured to parse dates in the specified time zone
43+
*
44+
* @param zoneId The time zone to act on
45+
* @return A copy of the date formatter this has been called on
46+
*/
47+
DateFormatter withZone(ZoneId zoneId);
48+
49+
/**
50+
* Print the supplied java time accessor in a string based representation according to this formatter
51+
*
52+
* @param accessor The temporal accessor used to format
53+
* @return The string result for the formatting
54+
*/
55+
String format(TemporalAccessor accessor);
56+
57+
/**
58+
* A name based format for this formatter. Can be one of the registered formatters like <code>epoch_millis</code> or
59+
* a configured format like <code>HH:mm:ss</code>
60+
*
61+
* @return The name of this formatter
62+
*/
63+
String pattern();
64+
65+
/**
66+
* Configure a formatter using default fields for a TemporalAccessor that should be used in case
67+
* the supplied date is not having all of those fields
68+
*
69+
* @param fields A <code>Map&lt;TemporalField, Long&gt;</code> of fields to be used as fallbacks
70+
* @return A new date formatter instance, that will use those fields during parsing
71+
*/
72+
DateFormatter parseDefaulting(Map<TemporalField, Long> fields);
73+
74+
/**
75+
* Merge several date formatters into a single one. Useful if you need to have several formatters with
76+
* different formats act as one, for example when you specify a
77+
* format like <code>date_hour||epoch_millis</code>
78+
*
79+
* @param formatters The list of date formatters to be merged together
80+
* @return The new date formtter containing the specified date formatters
81+
*/
82+
static DateFormatter merge(DateFormatter ... formatters) {
83+
return new MergedDateFormatter(formatters);
84+
}
85+
86+
class MergedDateFormatter implements DateFormatter {
87+
88+
private final String format;
89+
private final DateFormatter[] formatters;
90+
91+
MergedDateFormatter(DateFormatter ... formatters) {
92+
this.formatters = formatters;
93+
this.format = Arrays.stream(formatters).map(DateFormatter::pattern).collect(Collectors.joining("||"));
94+
}
95+
96+
@Override
97+
public TemporalAccessor parse(String input) {
98+
DateTimeParseException failure = null;
99+
for (DateFormatter formatter : formatters) {
100+
try {
101+
return formatter.parse(input);
102+
} catch (DateTimeParseException e) {
103+
if (failure == null) {
104+
failure = e;
105+
} else {
106+
failure.addSuppressed(e);
107+
}
108+
}
109+
}
110+
throw failure;
111+
}
112+
113+
@Override
114+
public DateFormatter withZone(ZoneId zoneId) {
115+
return new MergedDateFormatter(Arrays.stream(formatters).map(f -> f.withZone(zoneId)).toArray(DateFormatter[]::new));
116+
}
117+
118+
@Override
119+
public String format(TemporalAccessor accessor) {
120+
return formatters[0].format(accessor);
121+
}
122+
123+
@Override
124+
public String pattern() {
125+
return format;
126+
}
127+
128+
@Override
129+
public DateFormatter parseDefaulting(Map<TemporalField, Long> fields) {
130+
return new MergedDateFormatter(Arrays.stream(formatters).map(f -> f.parseDefaulting(fields)).toArray(DateFormatter[]::new));
131+
}
132+
}
133+
}

0 commit comments

Comments
 (0)