Skip to content

Commit 63e7565

Browse files
committed
Serialize top-level pipeline aggs as part of InternalAggregations (elastic#40177)
We currently convert pipeline aggregators to their corresponding InternalAggregation instance as part of the final reduction phase. They arrive to the coordinating node as part of QuerySearchResult objects fom the shards and, despite we may incrementally reduce aggs (hence we may have some non-final reduce and the final one later) all the reduction phases happen on the same node. With CCS minimizing roundtrips though, each cluster performs its own non-final reduction, and then serializes the results back to the CCS coordinating node which will perform the final coordination. This breaks the assumptions made up until now around reductions happening all on the same node. With elastic#40101 we have made sure that top-level pipeline aggs are not reduced as part of the non-final reduction. The next step is to make sure that they don't get lost, meaning that each coordinating node needs to send them back to the CCS coordinating node as part of the top-level `InternalAggregations` object. Closes elastic#40059
1 parent af55cb3 commit 63e7565

File tree

4 files changed

+235
-19
lines changed

4 files changed

+235
-19
lines changed

server/src/main/java/org/elasticsearch/search/aggregations/InternalAggregations.java

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,22 @@
1818
*/
1919
package org.elasticsearch.search.aggregations;
2020

21+
import org.elasticsearch.Version;
2122
import org.elasticsearch.common.io.stream.StreamInput;
2223
import org.elasticsearch.common.io.stream.StreamOutput;
2324
import org.elasticsearch.common.io.stream.Streamable;
2425
import org.elasticsearch.search.aggregations.InternalAggregation.ReduceContext;
26+
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
2527
import org.elasticsearch.search.aggregations.pipeline.SiblingPipelineAggregator;
2628

2729
import java.io.IOException;
2830
import java.util.ArrayList;
31+
import java.util.Collections;
2932
import java.util.Comparator;
3033
import java.util.HashMap;
3134
import java.util.List;
3235
import java.util.Map;
36+
import java.util.Objects;
3337

3438
import static java.util.Collections.emptyMap;
3539

@@ -49,6 +53,8 @@ public final class InternalAggregations extends Aggregations implements Streamab
4953
}
5054
};
5155

56+
private List<SiblingPipelineAggregator> topLevelPipelineAggregators = Collections.emptyList();
57+
5258
private InternalAggregations() {
5359
}
5460

@@ -60,18 +66,42 @@ public InternalAggregations(List<InternalAggregation> aggregations) {
6066
}
6167

6268
/**
63-
* Reduces the given list of aggregations
69+
* Constructs a new aggregation providing its {@link InternalAggregation}s and {@link SiblingPipelineAggregator}s
70+
*/
71+
public InternalAggregations(List<InternalAggregation> aggregations, List<SiblingPipelineAggregator> topLevelPipelineAggregators) {
72+
super(aggregations);
73+
this.topLevelPipelineAggregators = Objects.requireNonNull(topLevelPipelineAggregators);
74+
}
75+
76+
/**
77+
* Returns the top-level pipeline aggregators.
78+
* Note that top-level pipeline aggregators become normal aggregation once the final reduction has been performed, after which they
79+
* become part of the list of {@link InternalAggregation}s.
80+
*/
81+
List<SiblingPipelineAggregator> getTopLevelPipelineAggregators() {
82+
return topLevelPipelineAggregators;
83+
}
84+
85+
/**
86+
* Reduces the given list of aggregations as well as the top-level pipeline aggregators extracted from the first
87+
* {@link InternalAggregations} object found in the list.
88+
* Note that top-level pipeline aggregators are reduced only as part of the final reduction phase, otherwise they are left untouched.
6489
*/
65-
public static InternalAggregations reduce(List<InternalAggregations> aggregationsList, ReduceContext context) {
66-
return reduce(aggregationsList, null, context);
90+
public static InternalAggregations reduce(List<InternalAggregations> aggregationsList,
91+
ReduceContext context) {
92+
if (aggregationsList.isEmpty()) {
93+
return null;
94+
}
95+
InternalAggregations first = aggregationsList.get(0);
96+
return reduce(aggregationsList, first.topLevelPipelineAggregators, context);
6797
}
6898

6999
/**
70-
* Reduces the given list of aggregations as well as the provided sibling pipeline aggregators.
71-
* Note that sibling pipeline aggregators are ignored when non final reduction is performed.
100+
* Reduces the given list of aggregations as well as the provided top-level pipeline aggregators.
101+
* Note that top-level pipeline aggregators are reduced only as part of the final reduction phase, otherwise they are left untouched.
72102
*/
73103
public static InternalAggregations reduce(List<InternalAggregations> aggregationsList,
74-
List<SiblingPipelineAggregator> siblingPipelineAggregators,
104+
List<SiblingPipelineAggregator> topLevelPipelineAggregators,
75105
ReduceContext context) {
76106
if (aggregationsList.isEmpty()) {
77107
return null;
@@ -98,15 +128,14 @@ public static InternalAggregations reduce(List<InternalAggregations> aggregation
98128
reducedAggregations.add(first.reduce(aggregations, context));
99129
}
100130

101-
if (siblingPipelineAggregators != null) {
102-
if (context.isFinalReduce()) {
103-
for (SiblingPipelineAggregator pipelineAggregator : siblingPipelineAggregators) {
104-
InternalAggregation newAgg = pipelineAggregator.doReduce(new InternalAggregations(reducedAggregations), context);
105-
reducedAggregations.add(newAgg);
106-
}
131+
if (context.isFinalReduce()) {
132+
for (SiblingPipelineAggregator pipelineAggregator : topLevelPipelineAggregators) {
133+
InternalAggregation newAgg = pipelineAggregator.doReduce(new InternalAggregations(reducedAggregations), context);
134+
reducedAggregations.add(newAgg);
107135
}
136+
return new InternalAggregations(reducedAggregations);
108137
}
109-
return new InternalAggregations(reducedAggregations);
138+
return new InternalAggregations(reducedAggregations, topLevelPipelineAggregators);
110139
}
111140

112141
public static InternalAggregations readAggregations(StreamInput in) throws IOException {
@@ -121,11 +150,20 @@ public void readFrom(StreamInput in) throws IOException {
121150
if (aggregations.isEmpty()) {
122151
aggregationsAsMap = emptyMap();
123152
}
153+
if (in.getVersion().onOrAfter(Version.V_6_7_0)) {
154+
this.topLevelPipelineAggregators = in.readList(
155+
stream -> (SiblingPipelineAggregator)in.readNamedWriteable(PipelineAggregator.class));
156+
} else {
157+
this.topLevelPipelineAggregators = Collections.emptyList();
158+
}
124159
}
125160

126161
@Override
127162
@SuppressWarnings("unchecked")
128163
public void writeTo(StreamOutput out) throws IOException {
129164
out.writeNamedWriteableList((List<InternalAggregation>)aggregations);
165+
if (out.getVersion().onOrAfter(Version.V_6_7_0)) {
166+
out.writeNamedWriteableList(topLevelPipelineAggregators);
167+
}
130168
}
131169
}

server/src/main/java/org/elasticsearch/search/query/QuerySearchResult.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,11 @@
3535
import org.elasticsearch.search.suggest.Suggest;
3636

3737
import java.io.IOException;
38+
import java.util.Collections;
3839
import java.util.List;
40+
import java.util.Objects;
3941
import java.util.stream.Collectors;
4042

41-
import static java.util.Collections.emptyList;
4243
import static org.elasticsearch.common.lucene.Lucene.readTopDocs;
4344
import static org.elasticsearch.common.lucene.Lucene.writeTopDocs;
4445

@@ -50,7 +51,7 @@ public final class QuerySearchResult extends SearchPhaseResult {
5051
private DocValueFormat[] sortValueFormats;
5152
private InternalAggregations aggregations;
5253
private boolean hasAggs;
53-
private List<SiblingPipelineAggregator> pipelineAggregators;
54+
private List<SiblingPipelineAggregator> pipelineAggregators = Collections.emptyList();
5455
private Suggest suggest;
5556
private boolean searchTimedOut;
5657
private Boolean terminatedEarly = null;
@@ -79,7 +80,6 @@ public QuerySearchResult queryResult() {
7980
return this;
8081
}
8182

82-
8383
public void searchTimedOut(boolean searchTimedOut) {
8484
this.searchTimedOut = searchTimedOut;
8585
}
@@ -203,7 +203,7 @@ public List<SiblingPipelineAggregator> pipelineAggregators() {
203203
}
204204

205205
public void pipelineAggregators(List<SiblingPipelineAggregator> pipelineAggregators) {
206-
this.pipelineAggregators = pipelineAggregators;
206+
this.pipelineAggregators = Objects.requireNonNull(pipelineAggregators);
207207
}
208208

209209
public Suggest suggest() {
@@ -337,7 +337,7 @@ public void writeToNoId(StreamOutput out) throws IOException {
337337
out.writeBoolean(true);
338338
aggregations.writeTo(out);
339339
}
340-
out.writeNamedWriteableList(pipelineAggregators == null ? emptyList() : pipelineAggregators);
340+
out.writeNamedWriteableList(pipelineAggregators);
341341
if (suggest == null) {
342342
out.writeBoolean(false);
343343
} else {

server/src/test/java/org/elasticsearch/action/search/TransportSearchActionTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ public void testBuildClusters() {
331331
assertEquals(successful, clusters.getSuccessful());
332332
assertEquals(skipped, clusters.getSkipped());
333333
}
334-
334+
335335
private static OriginalIndices randomOriginalIndices() {
336336
int numLocalIndices = randomIntBetween(0, 5);
337337
String[] localIndices = new String[numLocalIndices];
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.elasticsearch.search.aggregations;
20+
21+
import org.elasticsearch.Version;
22+
import org.elasticsearch.common.io.stream.BytesStreamOutput;
23+
import org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput;
24+
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
25+
import org.elasticsearch.common.io.stream.StreamInput;
26+
import org.elasticsearch.common.settings.Settings;
27+
import org.elasticsearch.search.DocValueFormat;
28+
import org.elasticsearch.search.SearchModule;
29+
import org.elasticsearch.search.aggregations.bucket.histogram.InternalDateHistogram;
30+
import org.elasticsearch.search.aggregations.bucket.histogram.InternalDateHistogramTests;
31+
import org.elasticsearch.search.aggregations.bucket.terms.StringTerms;
32+
import org.elasticsearch.search.aggregations.bucket.terms.StringTermsTests;
33+
import org.elasticsearch.search.aggregations.pipeline.InternalSimpleValue;
34+
import org.elasticsearch.search.aggregations.pipeline.InternalSimpleValueTests;
35+
import org.elasticsearch.search.aggregations.pipeline.SiblingPipelineAggregator;
36+
import org.elasticsearch.search.aggregations.pipeline.bucketmetrics.avg.AvgBucketPipelineAggregationBuilder;
37+
import org.elasticsearch.search.aggregations.pipeline.bucketmetrics.max.MaxBucketPipelineAggregationBuilder;
38+
import org.elasticsearch.search.aggregations.pipeline.bucketmetrics.sum.SumBucketPipelineAggregationBuilder;
39+
import org.elasticsearch.test.ESTestCase;
40+
import org.elasticsearch.test.VersionUtils;
41+
import org.hamcrest.Matchers;
42+
43+
import java.io.IOException;
44+
import java.util.ArrayList;
45+
import java.util.Base64;
46+
import java.util.Collections;
47+
import java.util.List;
48+
49+
public class InternalAggregationsTests extends ESTestCase {
50+
51+
private final NamedWriteableRegistry registry = new NamedWriteableRegistry(
52+
new SearchModule(Settings.EMPTY, false, Collections.emptyList()).getNamedWriteables());
53+
54+
public void testReduceEmptyAggs() {
55+
List<InternalAggregations> aggs = Collections.emptyList();
56+
InternalAggregation.ReduceContext reduceContext = new InternalAggregation.ReduceContext(null, null, randomBoolean());
57+
assertNull(InternalAggregations.reduce(aggs, Collections.emptyList(), reduceContext));
58+
}
59+
60+
public void testNonFinalReduceTopLevelPipelineAggs() throws IOException {
61+
InternalAggregation terms = new StringTerms("name", BucketOrder.key(true),
62+
10, 1, Collections.emptyList(), Collections.emptyMap(), DocValueFormat.RAW, 25, false, 10, Collections.emptyList(), 0);
63+
List<InternalAggregations> aggs = Collections.singletonList(new InternalAggregations(Collections.singletonList(terms)));
64+
List<SiblingPipelineAggregator> topLevelPipelineAggs = new ArrayList<>();
65+
MaxBucketPipelineAggregationBuilder maxBucketPipelineAggregationBuilder = new MaxBucketPipelineAggregationBuilder("test", "test");
66+
topLevelPipelineAggs.add((SiblingPipelineAggregator)maxBucketPipelineAggregationBuilder.create());
67+
InternalAggregation.ReduceContext reduceContext = new InternalAggregation.ReduceContext(null, null, false);
68+
InternalAggregations reducedAggs = InternalAggregations.reduce(aggs, topLevelPipelineAggs, reduceContext);
69+
assertEquals(1, reducedAggs.getTopLevelPipelineAggregators().size());
70+
assertEquals(1, reducedAggs.aggregations.size());
71+
}
72+
73+
public void testFinalReduceTopLevelPipelineAggs() throws IOException {
74+
InternalAggregation terms = new StringTerms("name", BucketOrder.key(true),
75+
10, 1, Collections.emptyList(), Collections.emptyMap(), DocValueFormat.RAW, 25, false, 10, Collections.emptyList(), 0);
76+
77+
MaxBucketPipelineAggregationBuilder maxBucketPipelineAggregationBuilder = new MaxBucketPipelineAggregationBuilder("test", "test");
78+
SiblingPipelineAggregator siblingPipelineAggregator = (SiblingPipelineAggregator) maxBucketPipelineAggregationBuilder.create();
79+
InternalAggregation.ReduceContext reduceContext = new InternalAggregation.ReduceContext(null, null, true);
80+
final InternalAggregations reducedAggs;
81+
if (randomBoolean()) {
82+
InternalAggregations aggs = new InternalAggregations(Collections.singletonList(terms),
83+
Collections.singletonList(siblingPipelineAggregator));
84+
reducedAggs = InternalAggregations.reduce(Collections.singletonList(aggs), reduceContext);
85+
} else {
86+
InternalAggregations aggs = new InternalAggregations(Collections.singletonList(terms));
87+
List<SiblingPipelineAggregator> topLevelPipelineAggs = Collections.singletonList(siblingPipelineAggregator);
88+
reducedAggs = InternalAggregations.reduce(Collections.singletonList(aggs), topLevelPipelineAggs, reduceContext);
89+
}
90+
assertEquals(0, reducedAggs.getTopLevelPipelineAggregators().size());
91+
assertEquals(2, reducedAggs.aggregations.size());
92+
}
93+
94+
public void testSerialization() throws Exception {
95+
List<InternalAggregation> aggsList = new ArrayList<>();
96+
if (randomBoolean()) {
97+
StringTermsTests stringTermsTests = new StringTermsTests();
98+
stringTermsTests.init();
99+
stringTermsTests.setUp();
100+
aggsList.add(stringTermsTests.createTestInstance());
101+
}
102+
if (randomBoolean()) {
103+
InternalDateHistogramTests dateHistogramTests = new InternalDateHistogramTests();
104+
dateHistogramTests.setUp();
105+
aggsList.add(dateHistogramTests.createTestInstance());
106+
}
107+
if (randomBoolean()) {
108+
InternalSimpleValueTests simpleValueTests = new InternalSimpleValueTests();
109+
aggsList.add(simpleValueTests.createTestInstance());
110+
}
111+
List<SiblingPipelineAggregator> topLevelPipelineAggs = new ArrayList<>();
112+
if (randomBoolean()) {
113+
if (randomBoolean()) {
114+
topLevelPipelineAggs.add((SiblingPipelineAggregator)new MaxBucketPipelineAggregationBuilder("name1", "bucket1").create());
115+
}
116+
if (randomBoolean()) {
117+
topLevelPipelineAggs.add((SiblingPipelineAggregator)new AvgBucketPipelineAggregationBuilder("name2", "bucket2").create());
118+
}
119+
if (randomBoolean()) {
120+
topLevelPipelineAggs.add((SiblingPipelineAggregator)new SumBucketPipelineAggregationBuilder("name3", "bucket3").create());
121+
}
122+
}
123+
InternalAggregations aggregations = new InternalAggregations(aggsList, topLevelPipelineAggs);
124+
writeToAndReadFrom(aggregations, 0);
125+
}
126+
127+
private void writeToAndReadFrom(InternalAggregations aggregations, int iteration) throws IOException {
128+
Version version = VersionUtils.randomVersion(random());
129+
try (BytesStreamOutput out = new BytesStreamOutput()) {
130+
out.setVersion(version);
131+
aggregations.writeTo(out);
132+
try (StreamInput in = new NamedWriteableAwareStreamInput(StreamInput.wrap(out.bytes().toBytesRef().bytes), registry)) {
133+
in.setVersion(version);
134+
InternalAggregations deserialized = InternalAggregations.readAggregations(in);
135+
assertEquals(aggregations.aggregations, deserialized.aggregations);
136+
if (aggregations.getTopLevelPipelineAggregators() == null) {
137+
assertEquals(0, deserialized.getTopLevelPipelineAggregators().size());
138+
} else {
139+
if (version.before(Version.V_6_7_0)) {
140+
assertEquals(0, deserialized.getTopLevelPipelineAggregators().size());
141+
} else {
142+
assertEquals(aggregations.getTopLevelPipelineAggregators().size(),
143+
deserialized.getTopLevelPipelineAggregators().size());
144+
for (int i = 0; i < aggregations.getTopLevelPipelineAggregators().size(); i++) {
145+
SiblingPipelineAggregator siblingPipelineAggregator1 = aggregations.getTopLevelPipelineAggregators().get(i);
146+
SiblingPipelineAggregator siblingPipelineAggregator2 = deserialized.getTopLevelPipelineAggregators().get(i);
147+
assertArrayEquals(siblingPipelineAggregator1.bucketsPaths(), siblingPipelineAggregator2.bucketsPaths());
148+
assertEquals(siblingPipelineAggregator1.name(), siblingPipelineAggregator2.name());
149+
}
150+
}
151+
}
152+
if (iteration < 2) {
153+
//serialize this enough times to make sure that we are able to write again what we read
154+
writeToAndReadFrom(deserialized, iteration + 1);
155+
}
156+
}
157+
}
158+
}
159+
160+
public void testSerializationFromPre_6_7_0() throws IOException {
161+
String aggsString = "AwZzdGVybXMFb0F0Q0EKCQVsZG5ncgAFeG56RWcFeUFxVmcABXBhQVVpBUtYc2VIAAVaclRESwVqUkxySAAFelp5d1AFRUREcEYABW1" +
162+
"sckF0BU5wWWVFAAVJYVJmZgVURlJVbgAFT0RiU04FUWNwSVoABU1sb09HBUNzZHFlAAVWWmJHaQABAwGIDgNyYXcFAQAADmRhdGVfaGlzdG9ncmFt" +
163+
"BVhHbVl4/wADAAKAurcDA1VUQwABAQAAAWmOhukAAQAAAWmR9dEAAAAAAAAAAAAAAANyYXcACAAAAWmQrDoAUQAAAAFpkRoXAEMAAAABaZGH9AAtA" +
164+
"AAAAWmR9dEAJwAAAAFpkmOuAFwAAAABaZLRiwAYAAAAAWmTP2gAKgAAAAFpk61FABsADHNpbXBsZV92YWx1ZQVsWVNLVv8AB2RlY2ltYWwGIyMjLi" +
165+
"MjQLZWZVy5zBYAAAAAAAAAAAAAAAAAAAAAAAAA";
166+
167+
byte[] aggsBytes = Base64.getDecoder().decode(aggsString);
168+
try (NamedWriteableAwareStreamInput in = new NamedWriteableAwareStreamInput(StreamInput.wrap(aggsBytes), registry)) {
169+
in.setVersion(VersionUtils.randomVersionBetween(random(), Version.CURRENT.minimumCompatibilityVersion(),
170+
Version.max(Version.CURRENT.minimumCompatibilityVersion(), VersionUtils.getPreviousVersion(Version.CURRENT))));
171+
InternalAggregations deserialized = InternalAggregations.readAggregations(in);
172+
assertEquals(3, deserialized.aggregations.size());
173+
assertThat(deserialized.aggregations.get(0), Matchers.instanceOf(StringTerms.class));
174+
assertThat(deserialized.aggregations.get(1), Matchers.instanceOf(InternalDateHistogram.class));
175+
assertThat(deserialized.aggregations.get(2), Matchers.instanceOf(InternalSimpleValue.class));
176+
}
177+
}
178+
}

0 commit comments

Comments
 (0)