|
| 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