Skip to content

Commit

Permalink
Deprecation info for joda-java migration on 7.x (#42659)
Browse files Browse the repository at this point in the history
 Some clusters might have been already migrated to version 7 without being warned about the joda-java migration changes.
Deprecation api on that version will give them guidance on what patterns need to be changed.
relates. This change is using the same logic like in 6.8 that is: verifying the pattern is from the incompatible set ('y'-Y', 'C', 'Z' etc), not from predifined set, not prefixed with 8. AND was also created in 6.x. Mappings created in 7.x are considered migrated and should not generate warnings

There is no pipeline check (present on 6.8) as it is impossible to verify when the pipeline was created, and therefore to make sure the format is depracated or not
#42010
  • Loading branch information
pgomulka authored Jun 5, 2019
1 parent 790d212 commit ab5bc83
Show file tree
Hide file tree
Showing 11 changed files with 725 additions and 196 deletions.
174 changes: 84 additions & 90 deletions server/src/main/java/org/elasticsearch/common/joda/Joda.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* 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.joda;

import org.elasticsearch.common.time.DateFormatter;
import org.elasticsearch.common.time.FormatNames;

import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class JodaDeprecationPatterns {
public static final String USE_NEW_FORMAT_SPECIFIERS = "Use new java.time date format specifiiers.";
private static Map<String, String> JODA_PATTERNS_DEPRECATIONS = new LinkedHashMap<>();

static {
JODA_PATTERNS_DEPRECATIONS.put("Y", "'Y' year-of-era should be replaced with 'y'. Use 'Y' for week-based-year.");
JODA_PATTERNS_DEPRECATIONS.put("y", "'y' year should be replaced with 'u'. Use 'y' for year-of-era.");
JODA_PATTERNS_DEPRECATIONS.put("C", "'C' century of era is no longer supported.");
JODA_PATTERNS_DEPRECATIONS.put("x", "'x' weak-year should be replaced with 'Y'. Use 'x' for zone-offset.");
JODA_PATTERNS_DEPRECATIONS.put("Z",
"'Z' time zone offset/id fails when parsing 'Z' for Zulu timezone. Consider using 'X'.");
JODA_PATTERNS_DEPRECATIONS.put("z",
"'z' time zone text. Will print 'Z' for Zulu given UTC timezone.");
}

/**
* Checks if date parsing pattern is deprecated.
* Deprecated here means: when it was not already prefixed with 8 (meaning already upgraded)
* and it is not a predefined pattern from <code>FormatNames</code> like basic_date_time_no_millis
* and it uses pattern characters which changed meaning from joda to java like Y becomes y.
* @param pattern - a format to be checked
* @return true if format is deprecated, otherwise false
*/
public static boolean isDeprecatedPattern(String pattern) {
List<String> patterns = DateFormatter.splitCombinedPatterns(pattern);

for (String subPattern : patterns) {
boolean isDeprecated = subPattern.startsWith("8") == false && FormatNames.exist(subPattern) == false &&
JODA_PATTERNS_DEPRECATIONS.keySet().stream()
.filter(s -> subPattern.contains(s))
.findAny()
.isPresent();
if (isDeprecated) {
return true;
}
}
return false;
}

/**
* Formats deprecation message for suggestion field in a warning header.
* Joins all warnings in a one message.
* @param pattern - a pattern to be formatted
* @return a formatted deprecation message
*/
public static String formatSuggestion(String pattern) {
List<String> patterns = DateFormatter.splitCombinedPatterns(pattern);

Set<String> warnings = new LinkedHashSet<>();
for (String subPattern : patterns) {
if (isDeprecatedPattern(subPattern)) {
String suggestion = JODA_PATTERNS_DEPRECATIONS.entrySet().stream()
.filter(s -> subPattern.contains(s.getKey()))
.map(s -> s.getValue())
.collect(Collectors.joining("; "));
warnings.add(suggestion);
}
}
String combinedWarning = warnings.stream()
.collect(Collectors.joining("; "));
return combinedWarning;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;

public interface DateFormatter {

Expand Down Expand Up @@ -137,18 +138,26 @@ static DateFormatter forPattern(String input) {
input = input.substring(1);
}

List<DateFormatter> formatters = new ArrayList<>();
for (String pattern : Strings.delimitedListToStringArray(input, "||")) {
if (Strings.hasLength(pattern) == false) {
throw new IllegalArgumentException("Cannot have empty element in multi date format pattern: " + input);
}
formatters.add(DateFormatters.forPattern(pattern));
}
List<String> patterns = splitCombinedPatterns(input);
List<DateFormatter> formatters = patterns.stream()
.map(DateFormatters::forPattern)
.collect(Collectors.toList());

if (formatters.size() == 1) {
return formatters.get(0);
}

return DateFormatters.merge(input, formatters);
}

static List<String> splitCombinedPatterns(String input) {
List<String> patterns = new ArrayList<>();
for (String pattern : Strings.delimitedListToStringArray(input, "||")) {
if (Strings.hasLength(pattern) == false) {
throw new IllegalArgumentException("Cannot have empty element in multi date format pattern: " + input);
}
patterns.add(pattern);
}
return patterns;
}
}
Loading

0 comments on commit ab5bc83

Please sign in to comment.