Skip to content

Commit

Permalink
Core: Add DateFormatter interface for java time parsing (#33716)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
spinscale authored Sep 17, 2018
1 parent b3962c1 commit a17e0e8
Show file tree
Hide file tree
Showing 17 changed files with 552 additions and 293 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.elasticsearch.index.shard.ShardId;

import java.io.IOException;
import java.time.Instant;
import java.util.Locale;

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

builder.startObject("unassigned_info");
builder.field("reason", unassignedInfo.getReason());
builder.field("at", UnassignedInfo.DATE_TIME_FORMATTER.printer().print(unassignedInfo.getUnassignedTimeInMillis()));
builder.field("at",
UnassignedInfo.DATE_TIME_FORMATTER.format(Instant.ofEpochMilli(unassignedInfo.getUnassignedTimeInMillis())));
if (unassignedInfo.getNumFailedAllocations() > 0) {
builder.field("failed_allocation_attempts", unassignedInfo.getNumFailedAllocations());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,20 @@
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.joda.FormatDateTimeFormatter;
import org.elasticsearch.common.joda.Joda;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Setting.Property;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.time.DateFormatter;
import org.elasticsearch.common.time.DateFormatters;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.ToXContent.Params;
import org.elasticsearch.common.xcontent.ToXContentFragment;
import org.elasticsearch.common.xcontent.XContentBuilder;

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

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

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

public static final FormatDateTimeFormatter DATE_TIME_FORMATTER = Joda.forPattern("dateOptionalTime");
public static final DateFormatter DATE_TIME_FORMATTER = DateFormatters.forPattern("dateOptionalTime").withZone(ZoneOffset.UTC);

public static final Setting<TimeValue> INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING =
new Setting<>("index.unassigned.node_left.delayed_timeout", (s) -> TimeValue.timeValueMinutes(1).getStringRep(), (s) -> {
Expand Down Expand Up @@ -421,7 +422,7 @@ public static long findNextDelayedAllocation(long currentNanoTime, ClusterState
public String shortSummary() {
StringBuilder sb = new StringBuilder();
sb.append("[reason=").append(reason).append("]");
sb.append(", at[").append(DATE_TIME_FORMATTER.printer().print(unassignedTimeMillis)).append("]");
sb.append(", at[").append(DATE_TIME_FORMATTER.format(Instant.ofEpochMilli(unassignedTimeMillis))).append("]");
if (failedAllocations > 0) {
sb.append(", failed_attempts[").append(failedAllocations).append("]");
}
Expand All @@ -444,7 +445,7 @@ public String toString() {
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject("unassigned_info");
builder.field("reason", reason);
builder.field("at", DATE_TIME_FORMATTER.printer().print(unassignedTimeMillis));
builder.field("at", DATE_TIME_FORMATTER.format(Instant.ofEpochMilli(unassignedTimeMillis)));
if (failedAllocations > 0) {
builder.field("failed_attempts", failedAllocations);
}
Expand Down
10 changes: 6 additions & 4 deletions server/src/main/java/org/elasticsearch/common/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@

package org.elasticsearch.common;

import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.elasticsearch.common.time.DateFormatter;
import org.elasticsearch.common.time.DateFormatters;

import java.time.Instant;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -83,7 +85,7 @@ public Table endHeaders() {
return this;
}

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

public Table startRow() {
if (headers.isEmpty()) {
Expand All @@ -93,7 +95,7 @@ public Table startRow() {
if (withTime) {
long time = System.currentTimeMillis();
addCell(TimeUnit.SECONDS.convert(time, TimeUnit.MILLISECONDS));
addCell(dateFormat.print(time));
addCell(FORMATTER.format(Instant.ofEpochMilli(time)));
}
return this;
}
Expand Down

This file was deleted.

133 changes: 133 additions & 0 deletions server/src/main/java/org/elasticsearch/common/time/DateFormatter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.elasticsearch.common.time;

import java.time.ZoneId;
import java.time.format.DateTimeParseException;
import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalField;
import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

public interface DateFormatter {

/**
* Try to parse input to a java time TemporalAccessor
* @param input An arbitrary string resembling the string representation of a date or time
* @throws DateTimeParseException If parsing fails, this exception will be thrown.
* Note that it can contained suppressed exceptions when several formatters failed parse this value
* @return The java time object containing the parsed input
*/
TemporalAccessor parse(String input);

/**
* Create a copy of this formatter that is configured to parse dates in the specified time zone
*
* @param zoneId The time zone to act on
* @return A copy of the date formatter this has been called on
*/
DateFormatter withZone(ZoneId zoneId);

/**
* Print the supplied java time accessor in a string based representation according to this formatter
*
* @param accessor The temporal accessor used to format
* @return The string result for the formatting
*/
String format(TemporalAccessor accessor);

/**
* A name based format for this formatter. Can be one of the registered formatters like <code>epoch_millis</code> or
* a configured format like <code>HH:mm:ss</code>
*
* @return The name of this formatter
*/
String pattern();

/**
* Configure a formatter using default fields for a TemporalAccessor that should be used in case
* the supplied date is not having all of those fields
*
* @param fields A <code>Map&lt;TemporalField, Long&gt;</code> of fields to be used as fallbacks
* @return A new date formatter instance, that will use those fields during parsing
*/
DateFormatter parseDefaulting(Map<TemporalField, Long> fields);

/**
* Merge several date formatters into a single one. Useful if you need to have several formatters with
* different formats act as one, for example when you specify a
* format like <code>date_hour||epoch_millis</code>
*
* @param formatters The list of date formatters to be merged together
* @return The new date formtter containing the specified date formatters
*/
static DateFormatter merge(DateFormatter ... formatters) {
return new MergedDateFormatter(formatters);
}

class MergedDateFormatter implements DateFormatter {

private final String format;
private final DateFormatter[] formatters;

MergedDateFormatter(DateFormatter ... formatters) {
this.formatters = formatters;
this.format = Arrays.stream(formatters).map(DateFormatter::pattern).collect(Collectors.joining("||"));
}

@Override
public TemporalAccessor parse(String input) {
DateTimeParseException failure = null;
for (DateFormatter formatter : formatters) {
try {
return formatter.parse(input);
} catch (DateTimeParseException e) {
if (failure == null) {
failure = e;
} else {
failure.addSuppressed(e);
}
}
}
throw failure;
}

@Override
public DateFormatter withZone(ZoneId zoneId) {
return new MergedDateFormatter(Arrays.stream(formatters).map(f -> f.withZone(zoneId)).toArray(DateFormatter[]::new));
}

@Override
public String format(TemporalAccessor accessor) {
return formatters[0].format(accessor);
}

@Override
public String pattern() {
return format;
}

@Override
public DateFormatter parseDefaulting(Map<TemporalField, Long> fields) {
return new MergedDateFormatter(Arrays.stream(formatters).map(f -> f.parseDefaulting(fields)).toArray(DateFormatter[]::new));
}
}
}
Loading

0 comments on commit a17e0e8

Please sign in to comment.