From 003c7ff588c90b74b7204222e907e0b80435ddca Mon Sep 17 00:00:00 2001 From: Nik Everett Date: Tue, 9 Jun 2020 13:15:21 -0400 Subject: [PATCH] Deprecte Rounding#round (#57845) This deprecates `Rounding#round` and `Rounding#nextRoundingValue` in favor of calling ``` Rounding.Prepared prepared = rounding.prepare(min, max); ... prepared.round(val) ``` because it is always going to be faster to prepare once. There are going to be some cases where we won't know what to prepare *for* and in those cases you can call `prepareForUnknown` and stil be faster than calling the deprecated method over and over and over again. Ultimately, this is important because it doesn't look like there is an easy way to cache `Rounding.Prepared` or any of its precursors like `LocalTimeOffset.Lookup`. Instead, we can just build it at most once per request. Relates to #56124 --- .../org/elasticsearch/common/Rounding.java | 8 ++--- .../common/rounding/RoundingDuelTests.java | 6 ++-- .../InternalAutoDateHistogramTests.java | 21 ++++++------- .../histogram/InternalDateHistogramTests.java | 11 +++++-- .../rollup/job/DateHistogramGroupConfig.java | 6 ++-- .../pivot/DateHistogramGroupSource.java | 30 +++++++++++-------- .../job/RollupIndexerIndexingTests.java | 4 +-- 7 files changed, 48 insertions(+), 38 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/common/Rounding.java b/server/src/main/java/org/elasticsearch/common/Rounding.java index 4a49a5ff3e708..e1a034fb7924c 100644 --- a/server/src/main/java/org/elasticsearch/common/Rounding.java +++ b/server/src/main/java/org/elasticsearch/common/Rounding.java @@ -240,9 +240,9 @@ public interface Prepared { /** * Rounds the given value. - *

- * Prefer {@link #prepare(long, long)} if rounding many values. + * @deprecated Prefer {@link #prepare} and then {@link Prepared#round(long)} */ + @Deprecated public final long round(long utcMillis) { return prepare(utcMillis, utcMillis).round(utcMillis); } @@ -252,9 +252,9 @@ public final long round(long utcMillis) { * {@link #round(long)}, returns the next rounding value. For * example, with interval based rounding, if the interval is * 3, {@code nextRoundValue(6) = 9}. - *

- * Prefer {@link #prepare(long, long)} if rounding many values. + * @deprecated Prefer {@link #prepare} and then {@link Prepared#nextRoundingValue(long)} */ + @Deprecated public final long nextRoundingValue(long utcMillis) { return prepare(utcMillis, utcMillis).nextRoundingValue(utcMillis); } diff --git a/server/src/test/java/org/elasticsearch/common/rounding/RoundingDuelTests.java b/server/src/test/java/org/elasticsearch/common/rounding/RoundingDuelTests.java index 463189743e3cb..ef7b1ac7ed536 100644 --- a/server/src/test/java/org/elasticsearch/common/rounding/RoundingDuelTests.java +++ b/server/src/test/java/org/elasticsearch/common/rounding/RoundingDuelTests.java @@ -35,16 +35,16 @@ public class RoundingDuelTests extends ESTestCase { public void testDuellingImplementations() { org.elasticsearch.common.Rounding.DateTimeUnit randomDateTimeUnit = randomFrom(org.elasticsearch.common.Rounding.DateTimeUnit.values()); - org.elasticsearch.common.Rounding rounding; + org.elasticsearch.common.Rounding.Prepared rounding; Rounding roundingJoda; if (randomBoolean()) { - rounding = org.elasticsearch.common.Rounding.builder(randomDateTimeUnit).timeZone(ZoneOffset.UTC).build(); + rounding = org.elasticsearch.common.Rounding.builder(randomDateTimeUnit).timeZone(ZoneOffset.UTC).build().prepareForUnknown(); DateTimeUnit dateTimeUnit = DateTimeUnit.resolve(randomDateTimeUnit.getId()); roundingJoda = Rounding.builder(dateTimeUnit).timeZone(DateTimeZone.UTC).build(); } else { TimeValue interval = timeValue(); - rounding = org.elasticsearch.common.Rounding.builder(interval).timeZone(ZoneOffset.UTC).build(); + rounding = org.elasticsearch.common.Rounding.builder(interval).timeZone(ZoneOffset.UTC).build().prepareForUnknown(); roundingJoda = Rounding.builder(interval).timeZone(DateTimeZone.UTC).build(); } diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/InternalAutoDateHistogramTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/InternalAutoDateHistogramTests.java index a2cd4d4fb1c8b..2d2cab2d7333f 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/InternalAutoDateHistogramTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/InternalAutoDateHistogramTests.java @@ -157,6 +157,7 @@ protected void assertReduced(InternalAutoDateHistogram reduced, List expectedCounts = new TreeMap<>(); if (totalBucketConut > 0) { - long keyForBucket = roundingInfo.rounding.round(lowest); - while (keyForBucket <= roundingInfo.rounding.round(highest)) { + long keyForBucket = prepared.round(lowest); + while (keyForBucket <= prepared.round(highest)) { long nextKey = keyForBucket; for (int i = 0; i < innerIntervalToUse; i++) { - nextKey = roundingInfo.rounding.nextRoundingValue(nextKey); + nextKey = prepared.nextRoundingValue(nextKey); } Instant key = Instant.ofEpochMilli(keyForBucket); expectedCounts.put(key, 0L); @@ -214,7 +215,7 @@ protected void assertReduced(InternalAutoDateHistogram reduced, List= keyForBucket && roundedBucketKey < nextKey) { expectedCounts.compute(key, @@ -227,8 +228,8 @@ protected void assertReduced(InternalAutoDateHistogram reduced, List createParser(boolean lenient) { @@ -296,7 +300,7 @@ public ZoneId getTimeZone() { return timeZone; } - public Rounding getRounding() { + Rounding.Prepared getRounding() { return rounding; } diff --git a/x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/job/RollupIndexerIndexingTests.java b/x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/job/RollupIndexerIndexingTests.java index a183217b6693e..181c35ce1e495 100644 --- a/x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/job/RollupIndexerIndexingTests.java +++ b/x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/job/RollupIndexerIndexingTests.java @@ -274,7 +274,7 @@ public void testSimpleDateHistoWithDelay() throws Exception { asMap("the_histo", now) ) ); - final Rounding rounding = dateHistoConfig.createRounding(); + final Rounding.Prepared rounding = dateHistoConfig.createRounding(); executeTestCase(dataset, job, now, (resp) -> { assertThat(resp.size(), equalTo(3)); IndexRequest request = resp.get(0); @@ -338,7 +338,7 @@ public void testSimpleDateHistoWithOverlappingDelay() throws Exception { asMap("the_histo", now) ) ); - final Rounding rounding = dateHistoConfig.createRounding(); + final Rounding.Prepared rounding = dateHistoConfig.createRounding(); executeTestCase(dataset, job, now, (resp) -> { assertThat(resp.size(), equalTo(2)); IndexRequest request = resp.get(0);