Browse Source

Refactor code to avoid JDK bug: JDK-8285835 (#86614)

Refactor code in DateHistogramAggregator$FromDateRange::adapt
to avoid OpenJDK C2 compiler crash with JDK 18.
Nikola Grcevski 3 years ago
parent
commit
1579b64191

+ 5 - 0
docs/changelog/86614.yaml

@@ -0,0 +1,5 @@
+pr: 86614
+summary: "Refactor code to avoid JDK bug: JDK-8285835"
+area: Infra/Core
+type: bug
+issues: []

+ 6 - 2
server/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/DateHistogramAggregator.java

@@ -428,7 +428,10 @@ class DateHistogramAggregator extends BucketsAggregator implements SizedBucketAg
         protected InternalAggregation adapt(InternalAggregation delegateResult) {
             InternalDateRange range = (InternalDateRange) delegateResult;
             List<InternalDateHistogram.Bucket> buckets = new ArrayList<>(range.getBuckets().size());
-            for (InternalDateRange.Bucket rangeBucket : range.getBuckets()) {
+            // This code below was converted from a regular for loop to a stream forEach to avoid
+            // JDK-8285835. It needs to stay in this form until we upgrade our JDK distribution to
+            // pick up a fix for the compiler crash.
+            range.getBuckets().stream().forEach(rangeBucket -> {
                 if (rangeBucket.getDocCount() > 0) {
                     buckets.add(
                         new InternalDateHistogram.Bucket(
@@ -440,7 +443,8 @@ class DateHistogramAggregator extends BucketsAggregator implements SizedBucketAg
                         )
                     );
                 }
-            }
+            });
+
             CollectionUtil.introSort(buckets, BucketOrder.key(true).comparator());
 
             InternalDateHistogram.EmptyBucketInfo emptyBucketInfo = minDocCount == 0