Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Race condition fix for datetime optimization #10385

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class JavaDateFormatter implements DateFormatter {
private final List<DateTimeFormatter> parsers;
private final JavaDateFormatter roundupParser;
private final Boolean canCacheLastParsedFormatter;
private volatile DateTimeFormatter lastParsedformatter = null;

/**
* A round up formatter
Expand Down Expand Up @@ -150,7 +151,7 @@ JavaDateFormatter getRoundupParser() {
if (parsers.length == 0) {
this.parsers = Collections.singletonList(printer);
} else {
this.parsers = new CopyOnWriteArrayList<>(parsers);
this.parsers = Arrays.asList(parsers);
CaptainDredge marked this conversation as resolved.
Show resolved Hide resolved
}
List<DateTimeFormatter> roundUp = createRoundUpParser(format, roundupParserConsumer);
this.roundupParser = new RoundUpFormatter(format, roundUp);
Expand Down Expand Up @@ -235,7 +236,7 @@ private JavaDateFormatter(
this.printFormat = printFormat;
this.printer = printer;
this.roundupParser = roundUpParsers != null ? new RoundUpFormatter(format, roundUpParsers) : null;
this.parsers = new CopyOnWriteArrayList<>(parsers);
this.parsers = parsers;
this.canCacheLastParsedFormatter = canCacheLastParsedFormatter;
}

Expand Down Expand Up @@ -286,24 +287,22 @@ public TemporalAccessor parse(String input) {
private TemporalAccessor doParse(String input) {
if (parsers.size() > 1) {
Object object = null;
DateTimeFormatter lastParsedformatter = null;
if (canCacheLastParsedFormatter && lastParsedformatter != null) {
reta marked this conversation as resolved.
Show resolved Hide resolved
ParsePosition pos = new ParsePosition(0);
object = lastParsedformatter.toFormat().parseObject(input, pos);
if (parsingSucceeded(object, input, pos)) {
return (TemporalAccessor) object;
}
}
for (DateTimeFormatter formatter : parsers) {
ParsePosition pos = new ParsePosition(0);
object = formatter.toFormat().parseObject(input, pos);
if (parsingSucceeded(object, input, pos)) {
lastParsedformatter = formatter;
break;
return (TemporalAccessor) object;
}
}
if (lastParsedformatter != null) {
if (canCacheLastParsedFormatter && lastParsedformatter != parsers.get(0)) {
synchronized (parsers) {
parsers.remove(lastParsedformatter);
parsers.add(0, lastParsedformatter);
}
}
return (TemporalAccessor) object;
}

throw new DateTimeParseException("Failed to parse with all enclosed parsers", input, 0);
}
return this.parsers.get(0).parse(input);
Expand Down
Loading