Skip to content

Commit

Permalink
Deprecate camel case date format backport(elastic#59555) (elastic#59948)
Browse files Browse the repository at this point in the history
Camel case date formats are deprecated and snake case should be used
instead.
backports elastic#59555
  • Loading branch information
pgomulka committed Jul 21, 2020
1 parent b633a97 commit c68dfa8
Show file tree
Hide file tree
Showing 13 changed files with 213 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -386,12 +386,12 @@ public void testParentFilterResolvedCorrectly() throws Exception {
.startObject("dates")
.field("type", "object")
.startObject("properties")
.startObject("day").field("type", "date").field("format", "dateOptionalTime").endObject()
.startObject("day").field("type", "date").field("format", "date_optional_time").endObject()
.startObject("month")
.field("type", "object")
.startObject("properties")
.startObject("end").field("type", "date").field("format", "dateOptionalTime").endObject()
.startObject("start").field("type", "date").field("format", "dateOptionalTime").endObject()
.startObject("end").field("type", "date").field("format", "date_optional_time").endObject()
.startObject("start").field("type", "date").field("format", "date_optional_time").endObject()
.startObject("label").field("type", "keyword").endObject()
.endObject()
.endObject()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ public void testStoredFieldsWithoutSource() throws Exception {
.field("long_field", 4L)
.field("float_field", 5.0f)
.field("double_field", 6.0d)
.field("date_field", DateFormatter.forPattern("dateOptionalTime").format(date))
.field("date_field", DateFormatter.forPattern("date_optional_time").format(date))
.field("boolean_field", true)
.field("binary_field", Base64.getEncoder().encodeToString("testing text".getBytes("UTF-8")))
.endObject()).get();
Expand Down Expand Up @@ -640,7 +640,7 @@ public void testStoredFieldsWithoutSource() throws Exception {
assertThat(searchHit.getFields().get("long_field").getValue(), equalTo((Object) 4L));
assertThat(searchHit.getFields().get("float_field").getValue(), equalTo((Object) 5.0f));
assertThat(searchHit.getFields().get("double_field").getValue(), equalTo((Object) 6.0d));
String dateTime = DateFormatter.forPattern("dateOptionalTime").format(date);
String dateTime = DateFormatter.forPattern("date_optional_time").format(date);
assertThat(searchHit.getFields().get("date_field").getValue(), equalTo((Object) dateTime));
assertThat(searchHit.getFields().get("boolean_field").getValue(), equalTo((Object) Boolean.TRUE));
assertThat(searchHit.getFields().get("binary_field").getValue(), equalTo(new BytesArray("testing text" .getBytes("UTF8"))));
Expand Down Expand Up @@ -818,7 +818,7 @@ public void testDocValueFields() throws Exception {
.field("long_field", 4L)
.field("float_field", 5.0f)
.field("double_field", 6.0d)
.field("date_field", DateFormatter.forPattern("dateOptionalTime").format(date))
.field("date_field", DateFormatter.forPattern("date_optional_time").format(date))
.field("boolean_field", true)
.field("binary_field", new byte[] {42, 100})
.field("ip_field", "::1")
Expand Down Expand Up @@ -855,7 +855,7 @@ public void testDocValueFields() throws Exception {
assertThat(searchResponse.getHits().getAt(0).getFields().get("float_field").getValue(), equalTo((Object) 5.0));
assertThat(searchResponse.getHits().getAt(0).getFields().get("double_field").getValue(), equalTo((Object) 6.0d));
assertThat(searchResponse.getHits().getAt(0).getFields().get("date_field").getValue(),
equalTo(DateFormatter.forPattern("dateOptionalTime").format(date)));
equalTo(DateFormatter.forPattern("date_optional_time").format(date)));
assertThat(searchResponse.getHits().getAt(0).getFields().get("boolean_field").getValue(), equalTo((Object) true));
assertThat(searchResponse.getHits().getAt(0).getFields().get("text_field").getValue(), equalTo("foo"));
assertThat(searchResponse.getHits().getAt(0).getFields().get("keyword_field").getValue(), equalTo("foo"));
Expand All @@ -880,7 +880,7 @@ public void testDocValueFields() throws Exception {
assertThat(searchResponse.getHits().getAt(0).getFields().get("float_field").getValue(), equalTo((Object) 5.0));
assertThat(searchResponse.getHits().getAt(0).getFields().get("double_field").getValue(), equalTo((Object) 6.0d));
assertThat(searchResponse.getHits().getAt(0).getFields().get("date_field").getValue(),
equalTo(DateFormatter.forPattern("dateOptionalTime").format(date)));
equalTo(DateFormatter.forPattern("date_optional_time").format(date)));
assertThat(searchResponse.getHits().getAt(0).getFields().get("boolean_field").getValue(), equalTo((Object) true));
assertThat(searchResponse.getHits().getAt(0).getFields().get("text_field").getValue(), equalTo("foo"));
assertThat(searchResponse.getHits().getAt(0).getFields().get("keyword_field").getValue(), equalTo("foo"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
*/
public final class UnassignedInfo implements ToXContentFragment, Writeable {

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

public static final Setting<TimeValue> INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING =
Setting.positiveTimeSetting("index.unassigned.node_left.delayed_timeout", TimeValue.timeValueMinutes(1), Property.Dynamic,
Expand Down
30 changes: 24 additions & 6 deletions server/src/main/java/org/elasticsearch/common/joda/Joda.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.common.time.DateFormatter;
import org.elasticsearch.common.time.FormatNames;
import org.elasticsearch.common.util.LazyInitializable;
import org.elasticsearch.common.time.FormatNames;
import org.joda.time.Chronology;
import org.joda.time.DateTime;
import org.joda.time.DateTimeField;
Expand All @@ -51,10 +53,14 @@
import java.util.Locale;
import java.util.regex.Pattern;

@Deprecated
public class Joda {

private static DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger(Joda.class));

// Joda.forPattern could be used even before the logging is initialized.
// If LogManager.getLogger is called before logging config is loaded
// it results in errors sent to status logger and startup to fail.
// Hence a lazy initialization.
private static final LazyInitializable<DeprecationLogger, RuntimeException> deprecationLogger
= new LazyInitializable(() -> new DeprecationLogger(LogManager.getLogger(FormatNames.class)));
/**
* Parses a joda based pattern, including some named ones (similar to the built in Joda ISO ones).
*/
Expand All @@ -66,6 +72,14 @@ public static JodaDateFormatter forPattern(String input) {
throw new IllegalArgumentException("No date pattern provided");
}

FormatNames formatName = FormatNames.forName(input);
if (formatName != null && formatName.isCamelCase(input)) {
String msg = "Camel case format name {} is deprecated and will be removed in a future version. " +
"Use snake case name {} instead.";
getDeprecationLogger()
.deprecatedAndMaybeLog("camelCaseDateFormat", msg, formatName.getCamelCaseName(), formatName.getSnakeCaseName());
}

DateTimeFormatter formatter;
if (FormatNames.BASIC_DATE.matches(input)) {
formatter = ISODateTimeFormat.basicDate();
Expand Down Expand Up @@ -272,7 +286,7 @@ public static JodaDateFormatter forPattern(String input) {
private static void maybeLogJodaDeprecation(String format) {
if (JodaDeprecationPatterns.isDeprecatedPattern(format)) {
String suggestion = JodaDeprecationPatterns.formatSuggestion(format);
deprecationLogger.deprecatedAndMaybeLog("joda-pattern-deprecation",
getDeprecationLogger().deprecatedAndMaybeLog("joda-pattern-deprecation",
suggestion + " " + JodaDeprecationPatterns.USE_NEW_FORMAT_SPECIFIERS);
}
}
Expand Down Expand Up @@ -382,11 +396,11 @@ public int parseInto(DateTimeParserBucket bucket, String text, int position) {
long millis = new BigDecimal(text).longValue() * factor;
// check for deprecations, but after it has parsed correctly so invalid values aren't counted as deprecated
if (millis < 0) {
deprecationLogger.deprecatedAndMaybeLog("epoch-negative", "Use of negative values" +
getDeprecationLogger().deprecatedAndMaybeLog("epoch-negative", "Use of negative values" +
" in epoch time formats is deprecated and will not be supported in the next major version of Elasticsearch.");
}
if (scientificNotation.matcher(text).find()) {
deprecationLogger.deprecatedAndMaybeLog("epoch-scientific-notation", "Use of scientific notation" +
getDeprecationLogger().deprecatedAndMaybeLog("epoch-scientific-notation", "Use of scientific notation" +
" in epoch time formats is deprecated and will not be supported in the next major version of Elasticsearch.");
}
DateTime dt = new DateTime(millis, DateTimeZone.UTC);
Expand All @@ -405,6 +419,10 @@ public int parseInto(DateTimeParserBucket bucket, String text, int position) {
}
}

private static DeprecationLogger getDeprecationLogger() {
return deprecationLogger.getOrCompute();
}

public static class EpochTimePrinter implements DateTimePrinter {

private boolean hasMilliSecondPrecision;
Expand Down
Loading

0 comments on commit c68dfa8

Please sign in to comment.