-
Notifications
You must be signed in to change notification settings - Fork 24.9k
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
Mitigate date histogram slowdowns with non-fixed timezones. #30534
Changes from 1 commit
919f39c
da06498
9646c0b
fbd1ad6
44b8820
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 |
---|---|---|
|
@@ -17,14 +17,27 @@ | |
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.search.aggregations.bucket; | ||
package org.elasticsearch.search.aggregations.bucket.histogram; | ||
|
||
import org.apache.lucene.document.Document; | ||
import org.apache.lucene.document.LongPoint; | ||
import org.apache.lucene.document.SortedNumericDocValuesField; | ||
import org.apache.lucene.index.DirectoryReader; | ||
import org.apache.lucene.index.IndexReader; | ||
import org.apache.lucene.index.IndexWriter; | ||
import org.apache.lucene.store.Directory; | ||
import org.elasticsearch.common.joda.FormatDateTimeFormatter; | ||
import org.elasticsearch.common.joda.Joda; | ||
import org.elasticsearch.index.query.QueryShardContext; | ||
import org.elasticsearch.search.aggregations.BaseAggregationTestCase; | ||
import org.elasticsearch.search.aggregations.BucketOrder; | ||
import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramAggregationBuilder; | ||
import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval; | ||
import org.elasticsearch.search.aggregations.bucket.histogram.ExtendedBoundsTests; | ||
import org.elasticsearch.search.aggregations.BucketOrder; | ||
import org.joda.time.DateTimeZone; | ||
import org.junit.Assume; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
|
@@ -120,4 +133,52 @@ private List<BucketOrder> randomOrder() { | |
return orders; | ||
} | ||
|
||
private static Document documentForDate(String field, long millis) { | ||
Document doc = new Document(); | ||
doc.add(new LongPoint(field, millis)); | ||
doc.add(new SortedNumericDocValuesField(field, millis)); | ||
return doc; | ||
} | ||
|
||
public void testRewriteTimeZone() throws IOException { | ||
Assume.assumeTrue(getCurrentTypes().length > 0); // we need mappings | ||
FormatDateTimeFormatter format = Joda.forPattern("strict_date_optional_time"); | ||
|
||
try (Directory dir = newDirectory(); | ||
IndexWriter w = new IndexWriter(dir, newIndexWriterConfig())) { | ||
|
||
w.addDocument(documentForDate(DATE_FIELD_NAME, format.parser().parseDateTime("2018-03-11T11:55:00").getMillis())); | ||
w.addDocument(documentForDate(DATE_FIELD_NAME, format.parser().parseDateTime("2018-03-20T18:13:00").getMillis())); | ||
|
||
try (IndexReader readerThatDoesntCross = DirectoryReader.open(w)) { | ||
|
||
w.addDocument(documentForDate(DATE_FIELD_NAME, format.parser().parseDateTime("2018-03-25T02:44:00").getMillis())); | ||
|
||
try (IndexReader readerThatCrosses = DirectoryReader.open(w)) { | ||
|
||
QueryShardContext shardContextThatDoesntCross = createShardContext(readerThatDoesntCross); | ||
QueryShardContext shardContextThatCrosses = createShardContext(readerThatCrosses); | ||
|
||
DateHistogramAggregationBuilder builder = new DateHistogramAggregationBuilder("my_date_histo"); | ||
builder.field(DATE_FIELD_NAME); | ||
// no timeZone => no rewrite | ||
assertNull(builder.rewriteTimeZone(shardContextThatDoesntCross)); | ||
assertNull(builder.rewriteTimeZone(shardContextThatCrosses)); | ||
|
||
// fixed timeZone => no rewrite | ||
DateTimeZone tz = DateTimeZone.forOffsetHours(1); | ||
builder.timeZone(tz); | ||
assertSame(tz, builder.rewriteTimeZone(shardContextThatDoesntCross)); | ||
assertSame(tz, builder.rewriteTimeZone(shardContextThatCrosses)); | ||
|
||
// daylight-saving-times => rewrite if doesn't cross | ||
tz = DateTimeZone.forID("Europe/Paris"); | ||
builder.timeZone(tz); | ||
assertEquals(DateTimeZone.forOffsetHours(1), builder.rewriteTimeZone(shardContextThatDoesntCross)); | ||
assertSame(tz, builder.rewriteTimeZone(shardContextThatCrosses)); | ||
} | ||
} | ||
} | ||
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. Should we also have a test that makes sure fixed time zones are not changed? 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 above assertions check it? 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. sorry I had missed that, you are right |
||
} | ||
|
||
} |
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.
Should this not be guaranteed to be an
IndexNumericFieldData
? if so maybe this should either be an assert or we should throw an exception if this condition is false?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.
good point. I had it implemented in rewrite() initially where it felt wrong since validation happens afterwards, but here the values source is already resolved so an exception would have been thrown already if the histogram ran against a non-numeric field