diff --git a/server/src/main/java/org/elasticsearch/cluster/metadata/IndexNameExpressionResolver.java b/server/src/main/java/org/elasticsearch/cluster/metadata/IndexNameExpressionResolver.java index d0824ea7d8f35..9be5acc0561d9 100644 --- a/server/src/main/java/org/elasticsearch/cluster/metadata/IndexNameExpressionResolver.java +++ b/server/src/main/java/org/elasticsearch/cluster/metadata/IndexNameExpressionResolver.java @@ -27,27 +27,27 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.collect.Tuple; import org.elasticsearch.common.component.AbstractComponent; -import org.elasticsearch.common.joda.FormatDateTimeFormatter; import org.elasticsearch.common.regex.Regex; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.time.DateFormatter; +import org.elasticsearch.common.time.DateFormatters; import org.elasticsearch.common.time.DateMathParser; -import org.elasticsearch.common.time.DateUtils; +import org.elasticsearch.common.time.JavaDateMathParser; import org.elasticsearch.common.util.set.Sets; import org.elasticsearch.index.Index; import org.elasticsearch.index.IndexNotFoundException; import org.elasticsearch.indices.IndexClosedException; import org.elasticsearch.indices.InvalidIndexNameException; -import org.joda.time.DateTimeZone; -import org.joda.time.format.DateTimeFormat; -import org.joda.time.format.DateTimeFormatter; +import java.time.Instant; +import java.time.ZoneId; +import java.time.ZoneOffset; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; -import java.util.Locale; import java.util.Map; import java.util.Set; import java.util.SortedMap; @@ -62,7 +62,7 @@ public class IndexNameExpressionResolver extends AbstractComponent { public IndexNameExpressionResolver(Settings settings) { super(settings); expressionResolvers = Arrays.asList( - dateMathExpressionResolver = new DateMathExpressionResolver(settings), + dateMathExpressionResolver = new DateMathExpressionResolver(), new WildcardExpressionResolver() ); } @@ -815,6 +815,7 @@ private static List resolveEmptyOrTrivialWildcard(IndicesOptions options static final class DateMathExpressionResolver implements ExpressionResolver { + private static final DateFormatter DEFAULT_DATE_FORMATTER = DateFormatters.forPattern("uuuu.MM.dd"); private static final String EXPRESSION_LEFT_BOUND = "<"; private static final String EXPRESSION_RIGHT_BOUND = ">"; private static final char LEFT_BOUND = '{'; @@ -822,17 +823,6 @@ static final class DateMathExpressionResolver implements ExpressionResolver { private static final char ESCAPE_CHAR = '\\'; private static final char TIME_ZONE_BOUND = '|'; - private final DateTimeZone defaultTimeZone; - private final String defaultDateFormatterPattern; - private final DateTimeFormatter defaultDateFormatter; - - DateMathExpressionResolver(Settings settings) { - String defaultTimeZoneId = settings.get("date_math_expression_resolver.default_time_zone", "UTC"); - this.defaultTimeZone = DateTimeZone.forID(defaultTimeZoneId); - defaultDateFormatterPattern = settings.get("date_math_expression_resolver.default_date_format", "YYYY.MM.dd"); - this.defaultDateFormatter = DateTimeFormat.forPattern(defaultDateFormatterPattern); - } - @Override public List resolve(final Context context, List expressions) { List result = new ArrayList<>(expressions.size()); @@ -896,13 +886,12 @@ String resolveExpression(String expression, final Context context) { int dateTimeFormatLeftBoundIndex = inPlaceHolderString.indexOf(LEFT_BOUND); String mathExpression; String dateFormatterPattern; - DateTimeFormatter dateFormatter; - final DateTimeZone timeZone; + DateFormatter dateFormatter; + final ZoneId timeZone; if (dateTimeFormatLeftBoundIndex < 0) { mathExpression = inPlaceHolderString; - dateFormatterPattern = defaultDateFormatterPattern; - dateFormatter = defaultDateFormatter; - timeZone = defaultTimeZone; + dateFormatter = DEFAULT_DATE_FORMATTER; + timeZone = ZoneOffset.UTC; } else { if (inPlaceHolderString.lastIndexOf(RIGHT_BOUND) != inPlaceHolderString.length() - 1) { 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) { int formatPatternTimeZoneSeparatorIndex = dateFormatterPatternAndTimeZoneId.indexOf(TIME_ZONE_BOUND); if (formatPatternTimeZoneSeparatorIndex != -1) { dateFormatterPattern = dateFormatterPatternAndTimeZoneId.substring(0, formatPatternTimeZoneSeparatorIndex); - timeZone = DateTimeZone.forID(dateFormatterPatternAndTimeZoneId.substring(formatPatternTimeZoneSeparatorIndex + 1)); + timeZone = ZoneId.of(dateFormatterPatternAndTimeZoneId.substring(formatPatternTimeZoneSeparatorIndex + 1)); } else { dateFormatterPattern = dateFormatterPatternAndTimeZoneId; - timeZone = defaultTimeZone; + timeZone = ZoneOffset.UTC; } - dateFormatter = DateTimeFormat.forPattern(dateFormatterPattern); + dateFormatter = DateFormatters.forPattern(dateFormatterPattern); } - DateTimeFormatter parser = dateFormatter.withZone(timeZone); - FormatDateTimeFormatter formatter = new FormatDateTimeFormatter(dateFormatterPattern, parser, Locale.ROOT); - DateMathParser dateMathParser = formatter.toDateMathParser(); - long millis = dateMathParser.parse(mathExpression, context::getStartTime, false, - DateUtils.dateTimeZoneToZoneId(timeZone)); + DateFormatter formatter = dateFormatter.withZone(timeZone); + DateMathParser dateMathParser = new JavaDateMathParser(formatter); + long millis = dateMathParser.parse(mathExpression, context::getStartTime, false, timeZone); - String time = formatter.printer().print(millis); + String time = formatter.format(Instant.ofEpochMilli(millis)); beforePlaceHolderSb.append(time); inPlaceHolderSb = new StringBuilder(); inPlaceHolder = false; @@ -968,18 +955,4 @@ String resolveExpression(String expression, final Context context) { return beforePlaceHolderSb.toString(); } } - - /** - * Returns true iff the given expression resolves to the given index name otherwise false - */ - public final boolean matchesIndex(String indexName, String expression, ClusterState state) { - final String[] concreteIndices = concreteIndexNames(state, IndicesOptions.lenientExpandOpen(), expression); - for (String index : concreteIndices) { - if (Regex.simpleMatch(index, indexName)) { - return true; - } - } - return indexName.equals(expression); - } - } diff --git a/server/src/test/java/org/elasticsearch/cluster/metadata/DateMathExpressionResolverTests.java b/server/src/test/java/org/elasticsearch/cluster/metadata/DateMathExpressionResolverTests.java index 4f235e52636b3..b06609296487b 100644 --- a/server/src/test/java/org/elasticsearch/cluster/metadata/DateMathExpressionResolverTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/metadata/DateMathExpressionResolverTests.java @@ -25,7 +25,6 @@ import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver.Context; import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver.DateMathExpressionResolver; -import org.elasticsearch.common.settings.Settings; import org.elasticsearch.test.ESTestCase; import org.joda.time.DateTime; import org.joda.time.DateTimeZone; @@ -42,7 +41,7 @@ public class DateMathExpressionResolverTests extends ESTestCase { - private final DateMathExpressionResolver expressionResolver = new DateMathExpressionResolver(Settings.EMPTY); + private final DateMathExpressionResolver expressionResolver = new DateMathExpressionResolver(); private final Context context = new Context( ClusterState.builder(new ClusterName("_name")).build(), IndicesOptions.strictExpand() ); @@ -118,37 +117,6 @@ public void testExpression_MixedArray() throws Exception { assertThat(result.get(3), equalTo(".logstash-" + DateTimeFormat.forPattern("YYYY.MM").print(new DateTime(context.getStartTime(), UTC).withDayOfMonth(1)))); } - public void testExpression_CustomTimeZoneInSetting() throws Exception { - DateTimeZone timeZone; - int hoursOffset; - int minutesOffset = 0; - if (randomBoolean()) { - hoursOffset = randomIntBetween(-12, 14); - timeZone = DateTimeZone.forOffsetHours(hoursOffset); - } else { - hoursOffset = randomIntBetween(-11, 13); - minutesOffset = randomIntBetween(0, 59); - timeZone = DateTimeZone.forOffsetHoursMinutes(hoursOffset, minutesOffset); - } - DateTime now; - if (hoursOffset >= 0) { - // rounding to next day 00:00 - now = DateTime.now(UTC).plusHours(hoursOffset).plusMinutes(minutesOffset).withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0); - } else { - // rounding to today 00:00 - now = DateTime.now(UTC).withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0); - } - Settings settings = Settings.builder() - .put("date_math_expression_resolver.default_time_zone", timeZone.getID()) - .build(); - DateMathExpressionResolver expressionResolver = new DateMathExpressionResolver(settings); - Context context = new Context(this.context.getState(), this.context.getOptions(), now.getMillis()); - List results = expressionResolver.resolve(context, Arrays.asList("<.marvel-{now/d{YYYY.MM.dd}}>")); - assertThat(results.size(), equalTo(1)); - logger.info("timezone: [{}], now [{}], name: [{}]", timeZone, now, results.get(0)); - assertThat(results.get(0), equalTo(".marvel-" + DateTimeFormat.forPattern("YYYY.MM.dd").print(now.withZone(timeZone)))); - } - public void testExpression_CustomTimeZoneInIndexName() throws Exception { DateTimeZone timeZone; int hoursOffset;