-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Speed up date_histogram by precomputing ranges #61467
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
Changes from 7 commits
7687e30
e68463d
c13740b
4162ef1
02f0fe3
4fbcfbf
7a7ef49
4f23de0
178a158
f201a25
0e93729
35ef1c4
f1e286e
d2f1d23
2281d35
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ | |
| */ | ||
| package org.elasticsearch.common; | ||
|
|
||
| import org.apache.lucene.util.ArrayUtil; | ||
| import org.elasticsearch.ElasticsearchException; | ||
| import org.elasticsearch.Version; | ||
| import org.elasticsearch.common.LocalTimeOffset.Gap; | ||
|
|
@@ -44,8 +45,10 @@ | |
| import java.time.temporal.TemporalQueries; | ||
| import java.time.zone.ZoneOffsetTransition; | ||
| import java.time.zone.ZoneRules; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.Locale; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
|
|
@@ -467,8 +470,33 @@ private LocalDateTime truncateLocalDateTime(LocalDateTime localDateTime) { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Time zones have a daylight savings time transition after midnight that | ||
| * transitions back before midnight break the array-based rounding so | ||
| * we don't use it for them during those transitions. Luckily they are | ||
| * fairly rare and a while in the past. Note: we can use the array based | ||
| * rounding if the transition is <strong>at</strong> midnight. Just not | ||
| * if it is <strong>after</strong> it. | ||
| */ | ||
| private static final Map<String, Long> FORWARDS_BACKWRADS_ZONES = Map.ofEntries( | ||
| Map.entry("America/Goose_Bay", 1289116800000L), // Stopped transitioning after midnight in 2010 | ||
| Map.entry("America/Moncton", 1162108800000L), // Stopped transitioning after midnight in 2006 | ||
| Map.entry("America/St_Johns", 1289118600000L), // Stopped transitioning after midnight in 2010 | ||
| Map.entry("Canada/Newfoundland", 1289118600000L), // Stopped transitioning after midnight in 2010 | ||
| Map.entry("Pacific/Guam", -29347200000L), // Stopped transitioning after midnight in 1969 | ||
| Map.entry("Pacific/Saipan", -29347200000L) // Stopped transitioning after midnight in 1969 | ||
| ); | ||
|
|
||
| @Override | ||
| public Prepared prepare(long minUtcMillis, long maxUtcMillis) { | ||
| Prepared orig = prepareOffsetRounding(minUtcMillis, maxUtcMillis); | ||
| if (unitRoundsToMidnight && minUtcMillis <= FORWARDS_BACKWRADS_ZONES.getOrDefault(timeZone.getId(), Long.MIN_VALUE)) { | ||
| return orig; | ||
| } | ||
| return maybeUseArray(orig, minUtcMillis, maxUtcMillis, 128); | ||
| } | ||
|
|
||
| private Prepared prepareOffsetRounding(long minUtcMillis, long maxUtcMillis) { | ||
| long minLookup = minUtcMillis - unit.extraLocalOffsetLookup(); | ||
| long maxLookup = maxUtcMillis; | ||
|
|
||
|
|
@@ -487,7 +515,6 @@ public Prepared prepare(long minUtcMillis, long maxUtcMillis) { | |
| // Range too long, just use java.time | ||
| return prepareJavaTime(); | ||
| } | ||
|
|
||
| LocalTimeOffset fixedOffset = lookup.fixedInRange(minLookup, maxLookup); | ||
| if (fixedOffset != null) { | ||
| // The time zone is effectively fixed | ||
|
|
@@ -1111,7 +1138,7 @@ public byte id() { | |
|
|
||
| @Override | ||
| public Prepared prepare(long minUtcMillis, long maxUtcMillis) { | ||
| return wrapPreparedRounding(delegate.prepare(minUtcMillis, maxUtcMillis)); | ||
| return wrapPreparedRounding(delegate.prepare(minUtcMillis - offset, maxUtcMillis - offset)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this change fixing an existing bug?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, but I think the bug is worse with this change.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The new assertions in the |
||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -1186,4 +1213,62 @@ public static Rounding read(StreamInput in) throws IOException { | |
| throw new ElasticsearchException("unknown rounding id [" + id + "]"); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Attempt to build a {@link Prepared} implementation that relies on pre-calcuated | ||
| * "round down" points. If there would be more than {@code max} points then return | ||
| * the original implementation, otherwise return the new, faster implementation. | ||
| */ | ||
| static Prepared maybeUseArray(Prepared orig, long minUtcMillis, long maxUtcMillis, int max) { | ||
| long[] values = new long[1]; | ||
| long rounded = orig.round(minUtcMillis); | ||
| int i = 0; | ||
| values[i++] = rounded; | ||
| while ((rounded = orig.nextRoundingValue(rounded)) <= maxUtcMillis) { | ||
| if (i >= max) { | ||
| return orig; | ||
| } | ||
| assert values[i - 1] == orig.round(rounded - 1); | ||
|
||
| values = ArrayUtil.grow(values, i + 1); | ||
| values[i++]= rounded; | ||
| } | ||
| return new ArrayRounding(values, i, orig); | ||
| } | ||
|
|
||
| /** | ||
| * Implementation of {@link Prepared} using pre-calculated "round down" points. | ||
| */ | ||
| private static class ArrayRounding implements Prepared { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will need to implement new methods added in #61369. |
||
| private final long[] values; | ||
| private final int max; | ||
| private final Prepared delegate; | ||
|
|
||
| private ArrayRounding(long[] values, int max, Prepared delegate) { | ||
| this.values = values; | ||
| this.max = max; | ||
| this.delegate = delegate; | ||
| } | ||
|
|
||
| @Override | ||
| public long round(long utcMillis) { | ||
| assert values[0] <= utcMillis : "utcMillis must be after " + values[0]; | ||
| int idx = Arrays.binarySearch(values, 0, max, utcMillis); | ||
| assert idx != -1 : "The insertion point is before the array! This should have tripped the assertion above."; | ||
| assert -1 - idx <= values.length : "This insertion point is after the end of the array."; | ||
| if (idx < 0) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe assert that idx is neither -1 nor -1 - max?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure! |
||
| idx = -2 - idx; | ||
| } | ||
| return values[idx]; | ||
| } | ||
|
|
||
| @Override | ||
| public long nextRoundingValue(long utcMillis) { | ||
| return delegate.nextRoundingValue(utcMillis); | ||
| } | ||
|
|
||
| @Override | ||
| public double roundingSize(long utcMillis, DateTimeUnit timeUnit) { | ||
| return delegate.roundingSize(utcMillis, timeUnit); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit - It looks like this maps timezone name to milliseconds since epoch for when the zone changed to use a different transition time. Can you just add a comment clarifying that's how the map is structured? It seems unlikely we'd need to add to it, but just in case we do, it'd help to have a reference for how to do so handy.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍