Replace joda time in ingest-common module (#38088)

This commit fully replaces any remaining joda time time classes with
java time implementations.

Relates #27330
This commit is contained in:
Alexander Reelsen 2019-02-01 10:15:18 +01:00 committed by GitHub
parent d417997aca
commit 6c5a7387af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 90 additions and 94 deletions

View File

@ -21,10 +21,6 @@ package org.elasticsearch.ingest.common;
import org.elasticsearch.common.time.DateFormatter; import org.elasticsearch.common.time.DateFormatter;
import org.elasticsearch.common.time.DateFormatters; import org.elasticsearch.common.time.DateFormatters;
import org.elasticsearch.common.time.DateUtils;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.ISODateTimeFormat;
import java.time.Instant; import java.time.Instant;
import java.time.LocalDate; import java.time.LocalDate;
@ -48,26 +44,26 @@ import static java.time.temporal.ChronoField.SECOND_OF_DAY;
enum DateFormat { enum DateFormat {
Iso8601 { Iso8601 {
@Override @Override
Function<String, DateTime> getFunction(String format, DateTimeZone timezone, Locale locale) { Function<String, ZonedDateTime> getFunction(String format, ZoneId timezone, Locale locale) {
return ISODateTimeFormat.dateTimeParser().withZone(timezone)::parseDateTime; return (date) -> DateFormatters.from(DateFormatter.forPattern("strict_date_time").parse(date)).withZoneSameInstant(timezone);
} }
}, },
Unix { Unix {
@Override @Override
Function<String, DateTime> getFunction(String format, DateTimeZone timezone, Locale locale) { Function<String, ZonedDateTime> getFunction(String format, ZoneId timezone, Locale locale) {
return (date) -> new DateTime((long)(Double.parseDouble(date) * 1000), timezone); return date -> Instant.ofEpochMilli((long) (Double.parseDouble(date) * 1000.0)).atZone(timezone);
} }
}, },
UnixMs { UnixMs {
@Override @Override
Function<String, DateTime> getFunction(String format, DateTimeZone timezone, Locale locale) { Function<String, ZonedDateTime> getFunction(String format, ZoneId timezone, Locale locale) {
return (date) -> new DateTime(Long.parseLong(date), timezone); return date -> Instant.ofEpochMilli(Long.parseLong(date)).atZone(timezone);
} }
}, },
Tai64n { Tai64n {
@Override @Override
Function<String, DateTime> getFunction(String format, DateTimeZone timezone, Locale locale) { Function<String, ZonedDateTime> getFunction(String format, ZoneId timezone, Locale locale) {
return (date) -> new DateTime(parseMillis(date), timezone); return date -> Instant.ofEpochMilli(parseMillis(date)).atZone(timezone);
} }
private long parseMillis(String date) { private long parseMillis(String date) {
@ -85,13 +81,12 @@ enum DateFormat {
Arrays.asList(NANO_OF_SECOND, SECOND_OF_DAY, MINUTE_OF_DAY, HOUR_OF_DAY, DAY_OF_MONTH, MONTH_OF_YEAR); Arrays.asList(NANO_OF_SECOND, SECOND_OF_DAY, MINUTE_OF_DAY, HOUR_OF_DAY, DAY_OF_MONTH, MONTH_OF_YEAR);
@Override @Override
Function<String, DateTime> getFunction(String format, DateTimeZone timezone, Locale locale) { Function<String, ZonedDateTime> getFunction(String format, ZoneId zoneId, Locale locale) {
// support the 6.x BWC compatible way of parsing java 8 dates // support the 6.x BWC compatible way of parsing java 8 dates
if (format.startsWith("8")) { if (format.startsWith("8")) {
format = format.substring(1); format = format.substring(1);
} }
ZoneId zoneId = DateUtils.dateTimeZoneToZoneId(timezone);
int year = LocalDate.now(ZoneOffset.UTC).getYear(); int year = LocalDate.now(ZoneOffset.UTC).getYear();
DateFormatter formatter = DateFormatter.forPattern(format) DateFormatter formatter = DateFormatter.forPattern(format)
.withLocale(locale) .withLocale(locale)
@ -111,13 +106,12 @@ enum DateFormat {
accessor = newTime.withZoneSameLocal(zoneId); accessor = newTime.withZoneSameLocal(zoneId);
} }
long millis = DateFormatters.from(accessor).toInstant().toEpochMilli(); return DateFormatters.from(accessor);
return new DateTime(millis, timezone);
}; };
} }
}; };
abstract Function<String, DateTime> getFunction(String format, DateTimeZone timezone, Locale locale); abstract Function<String, ZonedDateTime> getFunction(String format, ZoneId timezone, Locale locale);
static DateFormat fromString(String format) { static DateFormat fromString(String format) {
switch (format) { switch (format) {

View File

@ -19,6 +19,18 @@
package org.elasticsearch.ingest.common; package org.elasticsearch.ingest.common;
import org.elasticsearch.ExceptionsHelper;
import org.elasticsearch.common.time.DateFormatter;
import org.elasticsearch.ingest.AbstractProcessor;
import org.elasticsearch.ingest.ConfigurationUtils;
import org.elasticsearch.ingest.IngestDocument;
import org.elasticsearch.ingest.Processor;
import org.elasticsearch.script.ScriptService;
import org.elasticsearch.script.TemplateScript;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.IllformedLocaleException; import java.util.IllformedLocaleException;
@ -27,18 +39,6 @@ import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.function.Function; import java.util.function.Function;
import org.elasticsearch.ExceptionsHelper;
import org.elasticsearch.ingest.AbstractProcessor;
import org.elasticsearch.ingest.ConfigurationUtils;
import org.elasticsearch.ingest.IngestDocument;
import org.elasticsearch.ingest.Processor;
import org.elasticsearch.script.ScriptService;
import org.elasticsearch.script.TemplateScript;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
public final class DateIndexNameProcessor extends AbstractProcessor { public final class DateIndexNameProcessor extends AbstractProcessor {
public static final String TYPE = "date_index_name"; public static final String TYPE = "date_index_name";
@ -47,10 +47,10 @@ public final class DateIndexNameProcessor extends AbstractProcessor {
private final TemplateScript.Factory indexNamePrefixTemplate; private final TemplateScript.Factory indexNamePrefixTemplate;
private final TemplateScript.Factory dateRoundingTemplate; private final TemplateScript.Factory dateRoundingTemplate;
private final TemplateScript.Factory indexNameFormatTemplate; private final TemplateScript.Factory indexNameFormatTemplate;
private final DateTimeZone timezone; private final ZoneId timezone;
private final List<Function<String, DateTime>> dateFormats; private final List<Function<String, ZonedDateTime>> dateFormats;
DateIndexNameProcessor(String tag, String field, List<Function<String, DateTime>> dateFormats, DateTimeZone timezone, DateIndexNameProcessor(String tag, String field, List<Function<String, ZonedDateTime>> dateFormats, ZoneId timezone,
TemplateScript.Factory indexNamePrefixTemplate, TemplateScript.Factory dateRoundingTemplate, TemplateScript.Factory indexNamePrefixTemplate, TemplateScript.Factory dateRoundingTemplate,
TemplateScript.Factory indexNameFormatTemplate) { TemplateScript.Factory indexNameFormatTemplate) {
super(tag); super(tag);
@ -72,9 +72,9 @@ public final class DateIndexNameProcessor extends AbstractProcessor {
date = obj.toString(); date = obj.toString();
} }
DateTime dateTime = null; ZonedDateTime dateTime = null;
Exception lastException = null; Exception lastException = null;
for (Function<String, DateTime> dateParser : dateFormats) { for (Function<String, ZonedDateTime> dateParser : dateFormats) {
try { try {
dateTime = dateParser.apply(date); dateTime = dateParser.apply(date);
} catch (Exception e) { } catch (Exception e) {
@ -90,13 +90,15 @@ public final class DateIndexNameProcessor extends AbstractProcessor {
String indexNameFormat = ingestDocument.renderTemplate(indexNameFormatTemplate); String indexNameFormat = ingestDocument.renderTemplate(indexNameFormatTemplate);
String dateRounding = ingestDocument.renderTemplate(dateRoundingTemplate); String dateRounding = ingestDocument.renderTemplate(dateRoundingTemplate);
DateTimeFormatter formatter = DateTimeFormat.forPattern(indexNameFormat); DateFormatter formatter = DateFormatter.forPattern(indexNameFormat);
// use UTC instead of Z is string representation of UTC, so behaviour is the same between 6.x and 7
String zone = timezone.equals(ZoneOffset.UTC) ? "UTC" : timezone.getId();
StringBuilder builder = new StringBuilder() StringBuilder builder = new StringBuilder()
.append('<') .append('<')
.append(indexNamePrefix) .append(indexNamePrefix)
.append('{') .append('{')
.append(formatter.print(dateTime)).append("||/").append(dateRounding) .append(formatter.format(dateTime)).append("||/").append(dateRounding)
.append('{').append(indexNameFormat).append('|').append(timezone).append('}') .append('{').append(indexNameFormat).append('|').append(zone).append('}')
.append('}') .append('}')
.append('>'); .append('>');
String dynamicIndexName = builder.toString(); String dynamicIndexName = builder.toString();
@ -125,11 +127,11 @@ public final class DateIndexNameProcessor extends AbstractProcessor {
return indexNameFormatTemplate; return indexNameFormatTemplate;
} }
DateTimeZone getTimezone() { ZoneId getTimezone() {
return timezone; return timezone;
} }
List<Function<String, DateTime>> getDateFormats() { List<Function<String, ZonedDateTime>> getDateFormats() {
return dateFormats; return dateFormats;
} }
@ -146,7 +148,7 @@ public final class DateIndexNameProcessor extends AbstractProcessor {
Map<String, Object> config) throws Exception { Map<String, Object> config) throws Exception {
String localeString = ConfigurationUtils.readOptionalStringProperty(TYPE, tag, config, "locale"); String localeString = ConfigurationUtils.readOptionalStringProperty(TYPE, tag, config, "locale");
String timezoneString = ConfigurationUtils.readOptionalStringProperty(TYPE, tag, config, "timezone"); String timezoneString = ConfigurationUtils.readOptionalStringProperty(TYPE, tag, config, "timezone");
DateTimeZone timezone = timezoneString == null ? DateTimeZone.UTC : DateTimeZone.forID(timezoneString); ZoneId timezone = timezoneString == null ? ZoneOffset.UTC : ZoneId.of(timezoneString);
Locale locale = Locale.ENGLISH; Locale locale = Locale.ENGLISH;
if (localeString != null) { if (localeString != null) {
try { try {
@ -159,7 +161,7 @@ public final class DateIndexNameProcessor extends AbstractProcessor {
if (dateFormatStrings == null) { if (dateFormatStrings == null) {
dateFormatStrings = Collections.singletonList("yyyy-MM-dd'T'HH:mm:ss.SSSXX"); dateFormatStrings = Collections.singletonList("yyyy-MM-dd'T'HH:mm:ss.SSSXX");
} }
List<Function<String, DateTime>> dateFormats = new ArrayList<>(dateFormatStrings.size()); List<Function<String, ZonedDateTime>> dateFormats = new ArrayList<>(dateFormatStrings.size());
for (String format : dateFormatStrings) { for (String format : dateFormatStrings) {
DateFormat dateFormat = DateFormat.fromString(format); DateFormat dateFormat = DateFormat.fromString(format);
dateFormats.add(dateFormat.getFunction(format, timezone, locale)); dateFormats.add(dateFormat.getFunction(format, timezone, locale));

View File

@ -21,6 +21,7 @@ package org.elasticsearch.ingest.common;
import org.elasticsearch.ExceptionsHelper; import org.elasticsearch.ExceptionsHelper;
import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.time.DateFormatter;
import org.elasticsearch.common.util.LocaleUtils; import org.elasticsearch.common.util.LocaleUtils;
import org.elasticsearch.ingest.AbstractProcessor; import org.elasticsearch.ingest.AbstractProcessor;
import org.elasticsearch.ingest.ConfigurationUtils; import org.elasticsearch.ingest.ConfigurationUtils;
@ -28,10 +29,10 @@ import org.elasticsearch.ingest.IngestDocument;
import org.elasticsearch.ingest.Processor; import org.elasticsearch.ingest.Processor;
import org.elasticsearch.script.ScriptService; import org.elasticsearch.script.ScriptService;
import org.elasticsearch.script.TemplateScript; import org.elasticsearch.script.TemplateScript;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.ISODateTimeFormat;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
@ -42,13 +43,14 @@ public final class DateProcessor extends AbstractProcessor {
public static final String TYPE = "date"; public static final String TYPE = "date";
static final String DEFAULT_TARGET_FIELD = "@timestamp"; static final String DEFAULT_TARGET_FIELD = "@timestamp";
public static final DateFormatter FORMATTER = DateFormatter.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
private final TemplateScript.Factory timezone; private final TemplateScript.Factory timezone;
private final TemplateScript.Factory locale; private final TemplateScript.Factory locale;
private final String field; private final String field;
private final String targetField; private final String targetField;
private final List<String> formats; private final List<String> formats;
private final List<Function<Map<String, Object>, Function<String, DateTime>>> dateParsers; private final List<Function<Map<String, Object>, Function<String, ZonedDateTime>>> dateParsers;
DateProcessor(String tag, @Nullable TemplateScript.Factory timezone, @Nullable TemplateScript.Factory locale, DateProcessor(String tag, @Nullable TemplateScript.Factory timezone, @Nullable TemplateScript.Factory locale,
String field, List<String> formats, String targetField) { String field, List<String> formats, String targetField) {
@ -65,8 +67,8 @@ public final class DateProcessor extends AbstractProcessor {
} }
} }
private DateTimeZone newDateTimeZone(Map<String, Object> params) { private ZoneId newDateTimeZone(Map<String, Object> params) {
return timezone == null ? DateTimeZone.UTC : DateTimeZone.forID(timezone.newInstance(params).execute()); return timezone == null ? ZoneOffset.UTC : ZoneId.of(timezone.newInstance(params).execute());
} }
private Locale newLocale(Map<String, Object> params) { private Locale newLocale(Map<String, Object> params) {
@ -82,9 +84,9 @@ public final class DateProcessor extends AbstractProcessor {
value = obj.toString(); value = obj.toString();
} }
DateTime dateTime = null; ZonedDateTime dateTime = null;
Exception lastException = null; Exception lastException = null;
for (Function<Map<String, Object>, Function<String, DateTime>> dateParser : dateParsers) { for (Function<Map<String, Object>, Function<String, ZonedDateTime>> dateParser : dateParsers) {
try { try {
dateTime = dateParser.apply(ingestDocument.getSourceAndMetadata()).apply(value); dateTime = dateParser.apply(ingestDocument.getSourceAndMetadata()).apply(value);
} catch (Exception e) { } catch (Exception e) {
@ -97,7 +99,7 @@ public final class DateProcessor extends AbstractProcessor {
throw new IllegalArgumentException("unable to parse date [" + value + "]", lastException); throw new IllegalArgumentException("unable to parse date [" + value + "]", lastException);
} }
ingestDocument.setFieldValue(targetField, ISODateTimeFormat.dateTime().print(dateTime)); ingestDocument.setFieldValue(targetField, FORMATTER.format(dateTime));
return ingestDocument; return ingestDocument;
} }

View File

@ -21,10 +21,7 @@ package org.elasticsearch.ingest.common;
import org.elasticsearch.common.time.DateUtils; import org.elasticsearch.common.time.DateUtils;
import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.ESTestCase;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import java.time.Instant;
import java.time.ZoneId; import java.time.ZoneId;
import java.time.ZoneOffset; import java.time.ZoneOffset;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
@ -38,9 +35,9 @@ import static org.hamcrest.core.IsEqual.equalTo;
public class DateFormatTests extends ESTestCase { public class DateFormatTests extends ESTestCase {
public void testParseJava() { public void testParseJava() {
Function<String, DateTime> javaFunction = DateFormat.Java.getFunction("MMM dd HH:mm:ss Z", Function<String, ZonedDateTime> javaFunction = DateFormat.Java.getFunction("MMM dd HH:mm:ss Z",
DateTimeZone.forOffsetHours(-8), Locale.ENGLISH); ZoneOffset.ofHours(-8), Locale.ENGLISH);
assertThat(Instant.ofEpochMilli(javaFunction.apply("Nov 24 01:29:01 -0800").getMillis()) assertThat(javaFunction.apply("Nov 24 01:29:01 -0800").toInstant()
.atZone(ZoneId.of("GMT-8")) .atZone(ZoneId.of("GMT-8"))
.format(DateTimeFormatter.ofPattern("MM dd HH:mm:ss", Locale.ENGLISH)), .format(DateTimeFormatter.ofPattern("MM dd HH:mm:ss", Locale.ENGLISH)),
equalTo("11 24 01:29:01")); equalTo("11 24 01:29:01"));
@ -48,33 +45,35 @@ public class DateFormatTests extends ESTestCase {
public void testParseJavaDefaultYear() { public void testParseJavaDefaultYear() {
String format = randomFrom("8dd/MM", "dd/MM"); String format = randomFrom("8dd/MM", "dd/MM");
DateTimeZone timezone = DateUtils.zoneIdToDateTimeZone(ZoneId.of("Europe/Amsterdam")); ZoneId timezone = DateUtils.of("Europe/Amsterdam");
Function<String, DateTime> javaFunction = DateFormat.Java.getFunction(format, timezone, Locale.ENGLISH); Function<String, ZonedDateTime> javaFunction = DateFormat.Java.getFunction(format, timezone, Locale.ENGLISH);
int year = ZonedDateTime.now(ZoneOffset.UTC).getYear(); int year = ZonedDateTime.now(ZoneOffset.UTC).getYear();
DateTime dateTime = javaFunction.apply("12/06"); ZonedDateTime dateTime = javaFunction.apply("12/06");
assertThat(dateTime.getYear(), is(year)); assertThat(dateTime.getYear(), is(year));
assertThat(dateTime.toString(), is(year + "-06-12T00:00:00.000+02:00"));
} }
public void testParseUnixMs() { public void testParseUnixMs() {
assertThat(DateFormat.UnixMs.getFunction(null, DateTimeZone.UTC, null).apply("1000500").getMillis(), equalTo(1000500L)); assertThat(DateFormat.UnixMs.getFunction(null, ZoneOffset.UTC, null).apply("1000500").toInstant().toEpochMilli(),
equalTo(1000500L));
} }
public void testParseUnix() { public void testParseUnix() {
assertThat(DateFormat.Unix.getFunction(null, DateTimeZone.UTC, null).apply("1000.5").getMillis(), equalTo(1000500L)); assertThat(DateFormat.Unix.getFunction(null, ZoneOffset.UTC, null).apply("1000.5").toInstant().toEpochMilli(),
equalTo(1000500L));
} }
public void testParseUnixWithMsPrecision() { public void testParseUnixWithMsPrecision() {
assertThat(DateFormat.Unix.getFunction(null, DateTimeZone.UTC, null).apply("1495718015").getMillis(), equalTo(1495718015000L)); assertThat(DateFormat.Unix.getFunction(null, ZoneOffset.UTC, null).apply("1495718015").toInstant().toEpochMilli(),
equalTo(1495718015000L));
} }
public void testParseISO8601() { public void testParseISO8601() {
assertThat(DateFormat.Iso8601.getFunction(null, DateTimeZone.UTC, null).apply("2001-01-01T00:00:00-0800").getMillis(), assertThat(DateFormat.Iso8601.getFunction(null, ZoneOffset.UTC, null).apply("2001-01-01T00:00:00-0800").toInstant().toEpochMilli(),
equalTo(978336000000L)); equalTo(978336000000L));
} }
public void testParseISO8601Failure() { public void testParseISO8601Failure() {
Function<String, DateTime> function = DateFormat.Iso8601.getFunction(null, DateTimeZone.UTC, null); Function<String, ZonedDateTime> function = DateFormat.Iso8601.getFunction(null, ZoneOffset.UTC, null);
try { try {
function.apply("2001-01-0:00-0800"); function.apply("2001-01-0:00-0800");
fail("parse should have failed"); fail("parse should have failed");
@ -86,7 +85,7 @@ public class DateFormatTests extends ESTestCase {
public void testTAI64NParse() { public void testTAI64NParse() {
String input = "4000000050d506482dbdf024"; String input = "4000000050d506482dbdf024";
String expected = "2012-12-22T03:00:46.767+02:00"; String expected = "2012-12-22T03:00:46.767+02:00";
assertThat(DateFormat.Tai64n.getFunction(null, DateTimeZone.forOffsetHours(2), null) assertThat(DateFormat.Tai64n.getFunction(null, ZoneOffset.ofHours(2), null)
.apply((randomBoolean() ? "@" : "") + input).toString(), equalTo(expected)); .apply((randomBoolean() ? "@" : "") + input).toString(), equalTo(expected));
} }

View File

@ -23,8 +23,8 @@ import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.ingest.TestTemplateService; import org.elasticsearch.ingest.TestTemplateService;
import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.ESTestCase;
import org.hamcrest.Matchers; import org.hamcrest.Matchers;
import org.joda.time.DateTimeZone;
import java.time.ZoneOffset;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
@ -44,7 +44,7 @@ public class DateIndexNameFactoryTests extends ESTestCase {
assertThat(processor.getIndexNamePrefixTemplate().newInstance(Collections.emptyMap()).execute(), Matchers.equalTo("")); assertThat(processor.getIndexNamePrefixTemplate().newInstance(Collections.emptyMap()).execute(), Matchers.equalTo(""));
assertThat(processor.getDateRoundingTemplate().newInstance(Collections.emptyMap()).execute(), Matchers.equalTo("y")); assertThat(processor.getDateRoundingTemplate().newInstance(Collections.emptyMap()).execute(), Matchers.equalTo("y"));
assertThat(processor.getIndexNameFormatTemplate().newInstance(Collections.emptyMap()).execute(), Matchers.equalTo("yyyy-MM-dd")); assertThat(processor.getIndexNameFormatTemplate().newInstance(Collections.emptyMap()).execute(), Matchers.equalTo("yyyy-MM-dd"));
assertThat(processor.getTimezone(), Matchers.equalTo(DateTimeZone.UTC)); assertThat(processor.getTimezone(), Matchers.equalTo(ZoneOffset.UTC));
} }
public void testSpecifyOptionalSettings() throws Exception { public void testSpecifyOptionalSettings() throws Exception {
@ -74,7 +74,7 @@ public class DateIndexNameFactoryTests extends ESTestCase {
config.put("timezone", "+02:00"); config.put("timezone", "+02:00");
processor = factory.create(null, null, config); processor = factory.create(null, null, config);
assertThat(processor.getTimezone(), Matchers.equalTo(DateTimeZone.forOffsetHours(2))); assertThat(processor.getTimezone(), Matchers.equalTo(ZoneOffset.ofHours(2)));
config = new HashMap<>(); config = new HashMap<>();
config.put("field", "_field"); config.put("field", "_field");

View File

@ -18,13 +18,14 @@
*/ */
package org.elasticsearch.ingest.common; package org.elasticsearch.ingest.common;
import org.elasticsearch.common.time.DateFormatter;
import org.elasticsearch.ingest.IngestDocument; import org.elasticsearch.ingest.IngestDocument;
import org.elasticsearch.ingest.TestTemplateService; import org.elasticsearch.ingest.TestTemplateService;
import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.ESTestCase;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
@ -35,9 +36,9 @@ import static org.hamcrest.CoreMatchers.equalTo;
public class DateIndexNameProcessorTests extends ESTestCase { public class DateIndexNameProcessorTests extends ESTestCase {
public void testJavaPattern() throws Exception { public void testJavaPattern() throws Exception {
Function<String, DateTime> function = DateFormat.Java.getFunction("yyyy-MM-dd'T'HH:mm:ss.SSSXX", DateTimeZone.UTC, Locale.ROOT); Function<String, ZonedDateTime> function = DateFormat.Java.getFunction("yyyy-MM-dd'T'HH:mm:ss.SSSXX", ZoneOffset.UTC, Locale.ROOT);
DateIndexNameProcessor processor = createProcessor("_field", Collections.singletonList(function), DateIndexNameProcessor processor = createProcessor("_field", Collections.singletonList(function),
DateTimeZone.UTC, "events-", "y", "yyyyMMdd"); ZoneOffset.UTC, "events-", "y", "yyyyMMdd");
IngestDocument document = new IngestDocument("_index", "_type", "_id", null, null, null, IngestDocument document = new IngestDocument("_index", "_type", "_id", null, null, null,
Collections.singletonMap("_field", "2016-04-25T12:24:20.101Z")); Collections.singletonMap("_field", "2016-04-25T12:24:20.101Z"));
processor.execute(document); processor.execute(document);
@ -45,9 +46,9 @@ public class DateIndexNameProcessorTests extends ESTestCase {
} }
public void testTAI64N()throws Exception { public void testTAI64N()throws Exception {
Function<String, DateTime> function = DateFormat.Tai64n.getFunction(null, DateTimeZone.UTC, null); Function<String, ZonedDateTime> function = DateFormat.Tai64n.getFunction(null, ZoneOffset.UTC, null);
DateIndexNameProcessor dateProcessor = createProcessor("_field", Collections.singletonList(function), DateIndexNameProcessor dateProcessor = createProcessor("_field", Collections.singletonList(function),
DateTimeZone.UTC, "events-", "m", "yyyyMMdd"); ZoneOffset.UTC, "events-", "m", "yyyyMMdd");
IngestDocument document = new IngestDocument("_index", "_type", "_id", null, null, null, IngestDocument document = new IngestDocument("_index", "_type", "_id", null, null, null,
Collections.singletonMap("_field", (randomBoolean() ? "@" : "") + "4000000050d506482dbdf024")); Collections.singletonMap("_field", (randomBoolean() ? "@" : "") + "4000000050d506482dbdf024"));
dateProcessor.execute(document); dateProcessor.execute(document);
@ -55,9 +56,9 @@ public class DateIndexNameProcessorTests extends ESTestCase {
} }
public void testUnixMs()throws Exception { public void testUnixMs()throws Exception {
Function<String, DateTime> function = DateFormat.UnixMs.getFunction(null, DateTimeZone.UTC, null); Function<String, ZonedDateTime> function = DateFormat.UnixMs.getFunction(null, ZoneOffset.UTC, null);
DateIndexNameProcessor dateProcessor = createProcessor("_field", Collections.singletonList(function), DateIndexNameProcessor dateProcessor = createProcessor("_field", Collections.singletonList(function),
DateTimeZone.UTC, "events-", "m", "yyyyMMdd"); ZoneOffset.UTC, "events-", "m", "yyyyMMdd");
IngestDocument document = new IngestDocument("_index", "_type", "_id", null, null, null, IngestDocument document = new IngestDocument("_index", "_type", "_id", null, null, null,
Collections.singletonMap("_field", "1000500")); Collections.singletonMap("_field", "1000500"));
dateProcessor.execute(document); dateProcessor.execute(document);
@ -70,9 +71,9 @@ public class DateIndexNameProcessorTests extends ESTestCase {
} }
public void testUnix()throws Exception { public void testUnix()throws Exception {
Function<String, DateTime> function = DateFormat.Unix.getFunction(null, DateTimeZone.UTC, null); Function<String, ZonedDateTime> function = DateFormat.Unix.getFunction(null, ZoneOffset.UTC, null);
DateIndexNameProcessor dateProcessor = createProcessor("_field", Collections.singletonList(function), DateIndexNameProcessor dateProcessor = createProcessor("_field", Collections.singletonList(function),
DateTimeZone.UTC, "events-", "m", "yyyyMMdd"); ZoneOffset.UTC, "events-", "m", "yyyyMMdd");
IngestDocument document = new IngestDocument("_index", "_type", "_id", null, null, null, IngestDocument document = new IngestDocument("_index", "_type", "_id", null, null, null,
Collections.singletonMap("_field", "1000.5")); Collections.singletonMap("_field", "1000.5"));
dateProcessor.execute(document); dateProcessor.execute(document);
@ -84,10 +85,10 @@ public class DateIndexNameProcessorTests extends ESTestCase {
String dateRounding = randomFrom("y", "M", "w", "d", "h", "m", "s"); String dateRounding = randomFrom("y", "M", "w", "d", "h", "m", "s");
String indexNameFormat = randomFrom("yyyy-MM-dd'T'HH:mm:ss.SSSZZ", "yyyyMMdd", "MM/dd/yyyy"); String indexNameFormat = randomFrom("yyyy-MM-dd'T'HH:mm:ss.SSSZZ", "yyyyMMdd", "MM/dd/yyyy");
String date = Integer.toString(randomInt()); String date = Integer.toString(randomInt());
Function<String, DateTime> dateTimeFunction = DateFormat.Unix.getFunction(null, DateTimeZone.UTC, null); Function<String, ZonedDateTime> dateTimeFunction = DateFormat.Unix.getFunction(null, ZoneOffset.UTC, null);
DateIndexNameProcessor dateProcessor = createProcessor("_field", DateIndexNameProcessor dateProcessor = createProcessor("_field",
Collections.singletonList(dateTimeFunction), DateTimeZone.UTC, indexNamePrefix, Collections.singletonList(dateTimeFunction), ZoneOffset.UTC, indexNamePrefix,
dateRounding, indexNameFormat); dateRounding, indexNameFormat);
IngestDocument document = new IngestDocument("_index", "_type", "_id", null, null, null, IngestDocument document = new IngestDocument("_index", "_type", "_id", null, null, null,
@ -95,12 +96,12 @@ public class DateIndexNameProcessorTests extends ESTestCase {
dateProcessor.execute(document); dateProcessor.execute(document);
assertThat(document.getSourceAndMetadata().get("_index"), assertThat(document.getSourceAndMetadata().get("_index"),
equalTo("<"+indexNamePrefix+"{"+DateTimeFormat.forPattern(indexNameFormat) equalTo("<"+indexNamePrefix+"{" + DateFormatter.forPattern(indexNameFormat)
.print(dateTimeFunction.apply(date))+"||/"+dateRounding+"{"+indexNameFormat+"|UTC}}>")); .format(dateTimeFunction.apply(date))+"||/"+dateRounding+"{"+indexNameFormat+"|UTC}}>"));
} }
private DateIndexNameProcessor createProcessor(String field, List<Function<String, DateTime>> dateFormats, private DateIndexNameProcessor createProcessor(String field, List<Function<String, ZonedDateTime>> dateFormats,
DateTimeZone timezone, String indexNamePrefix, String dateRounding, ZoneId timezone, String indexNamePrefix, String dateRounding,
String indexNameFormat) { String indexNameFormat) {
return new DateIndexNameProcessor(randomAlphaOfLength(10), field, dateFormats, timezone, return new DateIndexNameProcessor(randomAlphaOfLength(10), field, dateFormats, timezone,
new TestTemplateService.MockTemplateScript.Factory(indexNamePrefix), new TestTemplateService.MockTemplateScript.Factory(indexNamePrefix),

View File

@ -22,9 +22,9 @@ package org.elasticsearch.ingest.common;
import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.ingest.TestTemplateService; import org.elasticsearch.ingest.TestTemplateService;
import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.ESTestCase;
import org.joda.time.DateTimeZone;
import org.junit.Before; import org.junit.Before;
import java.time.ZoneId;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
@ -105,10 +105,10 @@ public class DateProcessorFactoryTests extends ESTestCase {
config.put("field", sourceField); config.put("field", sourceField);
config.put("formats", Collections.singletonList("dd/MM/yyyyy")); config.put("formats", Collections.singletonList("dd/MM/yyyyy"));
DateTimeZone timezone = randomDateTimeZone(); ZoneId timezone = randomZone();
config.put("timezone", timezone.getID()); config.put("timezone", timezone.getId());
DateProcessor processor = factory.create(null, null, config); DateProcessor processor = factory.create(null, null, config);
assertThat(processor.getTimezone().newInstance(Collections.emptyMap()).execute(), equalTo(timezone.getID())); assertThat(processor.getTimezone().newInstance(Collections.emptyMap()).execute(), equalTo(timezone.getId()));
} }
public void testParseMatchFormats() throws Exception { public void testParseMatchFormats() throws Exception {

View File

@ -45,9 +45,7 @@ public class DateProcessorTests extends ESTestCase {
} }
private TemplateScript.Factory templatize(ZoneId timezone) { private TemplateScript.Factory templatize(ZoneId timezone) {
// prevent writing "UTC" as string, as joda time does not parse it return new TestTemplateService.MockTemplateScript.Factory(timezone.getId());
String id = timezone.equals(ZoneOffset.UTC) ? "UTC" : timezone.getId();
return new TestTemplateService.MockTemplateScript.Factory(id);
} }
public void testJavaPattern() { public void testJavaPattern() {
@ -186,7 +184,7 @@ public class DateProcessorTests extends ESTestCase {
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, IllegalArgumentException e = expectThrows(IllegalArgumentException.class,
() -> processor.execute(RandomDocumentPicks.randomIngestDocument(random(), document))); () -> processor.execute(RandomDocumentPicks.randomIngestDocument(random(), document)));
assertThat(e.getMessage(), equalTo("unable to parse date [2010]")); assertThat(e.getMessage(), equalTo("unable to parse date [2010]"));
assertThat(e.getCause().getMessage(), equalTo("The datetime zone id 'invalid_timezone' is not recognised")); assertThat(e.getCause().getMessage(), equalTo("Unknown time-zone ID: invalid_timezone"));
} }
public void testInvalidLocale() { public void testInvalidLocale() {