Skip to content

Commit 0f0a7f7

Browse files
spinscalekcm
authored andcommitted
Core: Move IndexNameExpressionResolver to java time (#34507)
This switches from joda time to java time when resolving index names using date math. This commit also removes two non registered settings from the code, which could not be used anyway. An unused method was removed as well. Relates #27330
1 parent 4564085 commit 0f0a7f7

File tree

2 files changed

+20
-79
lines changed

2 files changed

+20
-79
lines changed

server/src/main/java/org/elasticsearch/cluster/metadata/IndexNameExpressionResolver.java

Lines changed: 19 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,27 @@
2727
import org.elasticsearch.common.Strings;
2828
import org.elasticsearch.common.collect.Tuple;
2929
import org.elasticsearch.common.component.AbstractComponent;
30-
import org.elasticsearch.common.joda.FormatDateTimeFormatter;
3130
import org.elasticsearch.common.regex.Regex;
3231
import org.elasticsearch.common.settings.Settings;
32+
import org.elasticsearch.common.time.DateFormatter;
33+
import org.elasticsearch.common.time.DateFormatters;
3334
import org.elasticsearch.common.time.DateMathParser;
34-
import org.elasticsearch.common.time.DateUtils;
35+
import org.elasticsearch.common.time.JavaDateMathParser;
3536
import org.elasticsearch.common.util.set.Sets;
3637
import org.elasticsearch.index.Index;
3738
import org.elasticsearch.index.IndexNotFoundException;
3839
import org.elasticsearch.indices.IndexClosedException;
3940
import org.elasticsearch.indices.InvalidIndexNameException;
40-
import org.joda.time.DateTimeZone;
41-
import org.joda.time.format.DateTimeFormat;
42-
import org.joda.time.format.DateTimeFormatter;
4341

42+
import java.time.Instant;
43+
import java.time.ZoneId;
44+
import java.time.ZoneOffset;
4445
import java.util.ArrayList;
4546
import java.util.Arrays;
4647
import java.util.Collections;
4748
import java.util.HashMap;
4849
import java.util.HashSet;
4950
import java.util.List;
50-
import java.util.Locale;
5151
import java.util.Map;
5252
import java.util.Set;
5353
import java.util.SortedMap;
@@ -62,7 +62,7 @@ public class IndexNameExpressionResolver extends AbstractComponent {
6262
public IndexNameExpressionResolver(Settings settings) {
6363
super(settings);
6464
expressionResolvers = Arrays.asList(
65-
dateMathExpressionResolver = new DateMathExpressionResolver(settings),
65+
dateMathExpressionResolver = new DateMathExpressionResolver(),
6666
new WildcardExpressionResolver()
6767
);
6868
}
@@ -815,24 +815,14 @@ private static List<String> resolveEmptyOrTrivialWildcard(IndicesOptions options
815815

816816
static final class DateMathExpressionResolver implements ExpressionResolver {
817817

818+
private static final DateFormatter DEFAULT_DATE_FORMATTER = DateFormatters.forPattern("uuuu.MM.dd");
818819
private static final String EXPRESSION_LEFT_BOUND = "<";
819820
private static final String EXPRESSION_RIGHT_BOUND = ">";
820821
private static final char LEFT_BOUND = '{';
821822
private static final char RIGHT_BOUND = '}';
822823
private static final char ESCAPE_CHAR = '\\';
823824
private static final char TIME_ZONE_BOUND = '|';
824825

825-
private final DateTimeZone defaultTimeZone;
826-
private final String defaultDateFormatterPattern;
827-
private final DateTimeFormatter defaultDateFormatter;
828-
829-
DateMathExpressionResolver(Settings settings) {
830-
String defaultTimeZoneId = settings.get("date_math_expression_resolver.default_time_zone", "UTC");
831-
this.defaultTimeZone = DateTimeZone.forID(defaultTimeZoneId);
832-
defaultDateFormatterPattern = settings.get("date_math_expression_resolver.default_date_format", "YYYY.MM.dd");
833-
this.defaultDateFormatter = DateTimeFormat.forPattern(defaultDateFormatterPattern);
834-
}
835-
836826
@Override
837827
public List<String> resolve(final Context context, List<String> expressions) {
838828
List<String> result = new ArrayList<>(expressions.size());
@@ -896,13 +886,12 @@ String resolveExpression(String expression, final Context context) {
896886
int dateTimeFormatLeftBoundIndex = inPlaceHolderString.indexOf(LEFT_BOUND);
897887
String mathExpression;
898888
String dateFormatterPattern;
899-
DateTimeFormatter dateFormatter;
900-
final DateTimeZone timeZone;
889+
DateFormatter dateFormatter;
890+
final ZoneId timeZone;
901891
if (dateTimeFormatLeftBoundIndex < 0) {
902892
mathExpression = inPlaceHolderString;
903-
dateFormatterPattern = defaultDateFormatterPattern;
904-
dateFormatter = defaultDateFormatter;
905-
timeZone = defaultTimeZone;
893+
dateFormatter = DEFAULT_DATE_FORMATTER;
894+
timeZone = ZoneOffset.UTC;
906895
} else {
907896
if (inPlaceHolderString.lastIndexOf(RIGHT_BOUND) != inPlaceHolderString.length() - 1) {
908897
throw new ElasticsearchParseException("invalid dynamic name expression [{}]. missing closing `}` for date math format", inPlaceHolderString);
@@ -915,20 +904,18 @@ String resolveExpression(String expression, final Context context) {
915904
int formatPatternTimeZoneSeparatorIndex = dateFormatterPatternAndTimeZoneId.indexOf(TIME_ZONE_BOUND);
916905
if (formatPatternTimeZoneSeparatorIndex != -1) {
917906
dateFormatterPattern = dateFormatterPatternAndTimeZoneId.substring(0, formatPatternTimeZoneSeparatorIndex);
918-
timeZone = DateTimeZone.forID(dateFormatterPatternAndTimeZoneId.substring(formatPatternTimeZoneSeparatorIndex + 1));
907+
timeZone = ZoneId.of(dateFormatterPatternAndTimeZoneId.substring(formatPatternTimeZoneSeparatorIndex + 1));
919908
} else {
920909
dateFormatterPattern = dateFormatterPatternAndTimeZoneId;
921-
timeZone = defaultTimeZone;
910+
timeZone = ZoneOffset.UTC;
922911
}
923-
dateFormatter = DateTimeFormat.forPattern(dateFormatterPattern);
912+
dateFormatter = DateFormatters.forPattern(dateFormatterPattern);
924913
}
925-
DateTimeFormatter parser = dateFormatter.withZone(timeZone);
926-
FormatDateTimeFormatter formatter = new FormatDateTimeFormatter(dateFormatterPattern, parser, Locale.ROOT);
927-
DateMathParser dateMathParser = formatter.toDateMathParser();
928-
long millis = dateMathParser.parse(mathExpression, context::getStartTime, false,
929-
DateUtils.dateTimeZoneToZoneId(timeZone));
914+
DateFormatter formatter = dateFormatter.withZone(timeZone);
915+
DateMathParser dateMathParser = new JavaDateMathParser(formatter);
916+
long millis = dateMathParser.parse(mathExpression, context::getStartTime, false, timeZone);
930917

931-
String time = formatter.printer().print(millis);
918+
String time = formatter.format(Instant.ofEpochMilli(millis));
932919
beforePlaceHolderSb.append(time);
933920
inPlaceHolderSb = new StringBuilder();
934921
inPlaceHolder = false;
@@ -968,18 +955,4 @@ String resolveExpression(String expression, final Context context) {
968955
return beforePlaceHolderSb.toString();
969956
}
970957
}
971-
972-
/**
973-
* Returns <code>true</code> iff the given expression resolves to the given index name otherwise <code>false</code>
974-
*/
975-
public final boolean matchesIndex(String indexName, String expression, ClusterState state) {
976-
final String[] concreteIndices = concreteIndexNames(state, IndicesOptions.lenientExpandOpen(), expression);
977-
for (String index : concreteIndices) {
978-
if (Regex.simpleMatch(index, indexName)) {
979-
return true;
980-
}
981-
}
982-
return indexName.equals(expression);
983-
}
984-
985958
}

server/src/test/java/org/elasticsearch/cluster/metadata/DateMathExpressionResolverTests.java

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import org.elasticsearch.cluster.ClusterState;
2626
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver.Context;
2727
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver.DateMathExpressionResolver;
28-
import org.elasticsearch.common.settings.Settings;
2928
import org.elasticsearch.test.ESTestCase;
3029
import org.joda.time.DateTime;
3130
import org.joda.time.DateTimeZone;
@@ -42,7 +41,7 @@
4241

4342
public class DateMathExpressionResolverTests extends ESTestCase {
4443

45-
private final DateMathExpressionResolver expressionResolver = new DateMathExpressionResolver(Settings.EMPTY);
44+
private final DateMathExpressionResolver expressionResolver = new DateMathExpressionResolver();
4645
private final Context context = new Context(
4746
ClusterState.builder(new ClusterName("_name")).build(), IndicesOptions.strictExpand()
4847
);
@@ -118,37 +117,6 @@ public void testExpression_MixedArray() throws Exception {
118117
assertThat(result.get(3), equalTo(".logstash-" + DateTimeFormat.forPattern("YYYY.MM").print(new DateTime(context.getStartTime(), UTC).withDayOfMonth(1))));
119118
}
120119

121-
public void testExpression_CustomTimeZoneInSetting() throws Exception {
122-
DateTimeZone timeZone;
123-
int hoursOffset;
124-
int minutesOffset = 0;
125-
if (randomBoolean()) {
126-
hoursOffset = randomIntBetween(-12, 14);
127-
timeZone = DateTimeZone.forOffsetHours(hoursOffset);
128-
} else {
129-
hoursOffset = randomIntBetween(-11, 13);
130-
minutesOffset = randomIntBetween(0, 59);
131-
timeZone = DateTimeZone.forOffsetHoursMinutes(hoursOffset, minutesOffset);
132-
}
133-
DateTime now;
134-
if (hoursOffset >= 0) {
135-
// rounding to next day 00:00
136-
now = DateTime.now(UTC).plusHours(hoursOffset).plusMinutes(minutesOffset).withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0);
137-
} else {
138-
// rounding to today 00:00
139-
now = DateTime.now(UTC).withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0);
140-
}
141-
Settings settings = Settings.builder()
142-
.put("date_math_expression_resolver.default_time_zone", timeZone.getID())
143-
.build();
144-
DateMathExpressionResolver expressionResolver = new DateMathExpressionResolver(settings);
145-
Context context = new Context(this.context.getState(), this.context.getOptions(), now.getMillis());
146-
List<String> results = expressionResolver.resolve(context, Arrays.asList("<.marvel-{now/d{YYYY.MM.dd}}>"));
147-
assertThat(results.size(), equalTo(1));
148-
logger.info("timezone: [{}], now [{}], name: [{}]", timeZone, now, results.get(0));
149-
assertThat(results.get(0), equalTo(".marvel-" + DateTimeFormat.forPattern("YYYY.MM.dd").print(now.withZone(timeZone))));
150-
}
151-
152120
public void testExpression_CustomTimeZoneInIndexName() throws Exception {
153121
DateTimeZone timeZone;
154122
int hoursOffset;

0 commit comments

Comments
 (0)