Skip to content

Commit ab5bc83

Browse files
authored
Deprecation info for joda-java migration on 7.x (#42659)
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
1 parent 790d212 commit ab5bc83

File tree

11 files changed

+725
-196
lines changed

11 files changed

+725
-196
lines changed

server/src/main/java/org/elasticsearch/common/joda/Joda.java

+84-90
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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.joda;
21+
22+
import org.elasticsearch.common.time.DateFormatter;
23+
import org.elasticsearch.common.time.FormatNames;
24+
25+
import java.util.LinkedHashMap;
26+
import java.util.LinkedHashSet;
27+
import java.util.List;
28+
import java.util.Map;
29+
import java.util.Set;
30+
import java.util.stream.Collectors;
31+
32+
public class JodaDeprecationPatterns {
33+
public static final String USE_NEW_FORMAT_SPECIFIERS = "Use new java.time date format specifiiers.";
34+
private static Map<String, String> JODA_PATTERNS_DEPRECATIONS = new LinkedHashMap<>();
35+
36+
static {
37+
JODA_PATTERNS_DEPRECATIONS.put("Y", "'Y' year-of-era should be replaced with 'y'. Use 'Y' for week-based-year.");
38+
JODA_PATTERNS_DEPRECATIONS.put("y", "'y' year should be replaced with 'u'. Use 'y' for year-of-era.");
39+
JODA_PATTERNS_DEPRECATIONS.put("C", "'C' century of era is no longer supported.");
40+
JODA_PATTERNS_DEPRECATIONS.put("x", "'x' weak-year should be replaced with 'Y'. Use 'x' for zone-offset.");
41+
JODA_PATTERNS_DEPRECATIONS.put("Z",
42+
"'Z' time zone offset/id fails when parsing 'Z' for Zulu timezone. Consider using 'X'.");
43+
JODA_PATTERNS_DEPRECATIONS.put("z",
44+
"'z' time zone text. Will print 'Z' for Zulu given UTC timezone.");
45+
}
46+
47+
/**
48+
* Checks if date parsing pattern is deprecated.
49+
* Deprecated here means: when it was not already prefixed with 8 (meaning already upgraded)
50+
* and it is not a predefined pattern from <code>FormatNames</code> like basic_date_time_no_millis
51+
* and it uses pattern characters which changed meaning from joda to java like Y becomes y.
52+
* @param pattern - a format to be checked
53+
* @return true if format is deprecated, otherwise false
54+
*/
55+
public static boolean isDeprecatedPattern(String pattern) {
56+
List<String> patterns = DateFormatter.splitCombinedPatterns(pattern);
57+
58+
for (String subPattern : patterns) {
59+
boolean isDeprecated = subPattern.startsWith("8") == false && FormatNames.exist(subPattern) == false &&
60+
JODA_PATTERNS_DEPRECATIONS.keySet().stream()
61+
.filter(s -> subPattern.contains(s))
62+
.findAny()
63+
.isPresent();
64+
if (isDeprecated) {
65+
return true;
66+
}
67+
}
68+
return false;
69+
}
70+
71+
/**
72+
* Formats deprecation message for suggestion field in a warning header.
73+
* Joins all warnings in a one message.
74+
* @param pattern - a pattern to be formatted
75+
* @return a formatted deprecation message
76+
*/
77+
public static String formatSuggestion(String pattern) {
78+
List<String> patterns = DateFormatter.splitCombinedPatterns(pattern);
79+
80+
Set<String> warnings = new LinkedHashSet<>();
81+
for (String subPattern : patterns) {
82+
if (isDeprecatedPattern(subPattern)) {
83+
String suggestion = JODA_PATTERNS_DEPRECATIONS.entrySet().stream()
84+
.filter(s -> subPattern.contains(s.getKey()))
85+
.map(s -> s.getValue())
86+
.collect(Collectors.joining("; "));
87+
warnings.add(suggestion);
88+
}
89+
}
90+
String combinedWarning = warnings.stream()
91+
.collect(Collectors.joining("; "));
92+
return combinedWarning;
93+
}
94+
}

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

+16-7
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.util.ArrayList;
3232
import java.util.List;
3333
import java.util.Locale;
34+
import java.util.stream.Collectors;
3435

3536
public interface DateFormatter {
3637

@@ -137,18 +138,26 @@ static DateFormatter forPattern(String input) {
137138
input = input.substring(1);
138139
}
139140

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

148146
if (formatters.size() == 1) {
149147
return formatters.get(0);
150148
}
151149

152150
return DateFormatters.merge(input, formatters);
153151
}
152+
153+
static List<String> splitCombinedPatterns(String input) {
154+
List<String> patterns = new ArrayList<>();
155+
for (String pattern : Strings.delimitedListToStringArray(input, "||")) {
156+
if (Strings.hasLength(pattern) == false) {
157+
throw new IllegalArgumentException("Cannot have empty element in multi date format pattern: " + input);
158+
}
159+
patterns.add(pattern);
160+
}
161+
return patterns;
162+
}
154163
}

0 commit comments

Comments
 (0)