|
| 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 | + |
| 20 | +package org.elasticsearch.benchmark.search.aggregations; |
| 21 | + |
| 22 | +import org.apache.lucene.analysis.Analyzer; |
| 23 | +import org.apache.lucene.search.IndexSearcher; |
| 24 | +import org.apache.lucene.search.MatchAllDocsQuery; |
| 25 | +import org.apache.lucene.search.Query; |
| 26 | +import org.elasticsearch.common.breaker.CircuitBreaker; |
| 27 | +import org.elasticsearch.common.breaker.PreallocatedCircuitBreakerService; |
| 28 | +import org.elasticsearch.common.lease.Releasable; |
| 29 | +import org.elasticsearch.common.lease.Releasables; |
| 30 | +import org.elasticsearch.common.settings.ClusterSettings; |
| 31 | +import org.elasticsearch.common.settings.Settings; |
| 32 | +import org.elasticsearch.common.util.BigArrays; |
| 33 | +import org.elasticsearch.common.util.PageCacheRecycler; |
| 34 | +import org.elasticsearch.index.Index; |
| 35 | +import org.elasticsearch.index.IndexSettings; |
| 36 | +import org.elasticsearch.index.analysis.NamedAnalyzer; |
| 37 | +import org.elasticsearch.index.cache.bitset.BitsetFilterCache; |
| 38 | +import org.elasticsearch.index.fielddata.IndexFieldData; |
| 39 | +import org.elasticsearch.index.fielddata.IndexFieldDataCache; |
| 40 | +import org.elasticsearch.index.mapper.MappedFieldType; |
| 41 | +import org.elasticsearch.index.mapper.NumberFieldMapper; |
| 42 | +import org.elasticsearch.index.mapper.NumberFieldMapper.NumberType; |
| 43 | +import org.elasticsearch.index.mapper.ObjectMapper; |
| 44 | +import org.elasticsearch.index.query.QueryBuilder; |
| 45 | +import org.elasticsearch.index.query.support.NestedScope; |
| 46 | +import org.elasticsearch.indices.breaker.CircuitBreakerService; |
| 47 | +import org.elasticsearch.indices.breaker.HierarchyCircuitBreakerService; |
| 48 | +import org.elasticsearch.indices.breaker.NoneCircuitBreakerService; |
| 49 | +import org.elasticsearch.indices.fielddata.cache.IndicesFieldDataCache; |
| 50 | +import org.elasticsearch.script.Script; |
| 51 | +import org.elasticsearch.script.ScriptContext; |
| 52 | +import org.elasticsearch.search.SearchModule; |
| 53 | +import org.elasticsearch.search.aggregations.Aggregator; |
| 54 | +import org.elasticsearch.search.aggregations.AggregatorFactories; |
| 55 | +import org.elasticsearch.search.aggregations.MultiBucketConsumerService.MultiBucketConsumer; |
| 56 | +import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder; |
| 57 | +import org.elasticsearch.search.aggregations.metrics.SumAggregationBuilder; |
| 58 | +import org.elasticsearch.search.aggregations.support.AggregationContext; |
| 59 | +import org.elasticsearch.search.aggregations.support.ValuesSourceRegistry; |
| 60 | +import org.elasticsearch.search.internal.SubSearchContext; |
| 61 | +import org.elasticsearch.search.lookup.SearchLookup; |
| 62 | +import org.elasticsearch.search.sort.BucketedSort; |
| 63 | +import org.elasticsearch.search.sort.BucketedSort.ExtraData; |
| 64 | +import org.elasticsearch.search.sort.SortAndFormats; |
| 65 | +import org.elasticsearch.search.sort.SortBuilder; |
| 66 | +import org.openjdk.jmh.annotations.Benchmark; |
| 67 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 68 | +import org.openjdk.jmh.annotations.Fork; |
| 69 | +import org.openjdk.jmh.annotations.Measurement; |
| 70 | +import org.openjdk.jmh.annotations.Mode; |
| 71 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 72 | +import org.openjdk.jmh.annotations.Param; |
| 73 | +import org.openjdk.jmh.annotations.Scope; |
| 74 | +import org.openjdk.jmh.annotations.Setup; |
| 75 | +import org.openjdk.jmh.annotations.State; |
| 76 | +import org.openjdk.jmh.annotations.Threads; |
| 77 | +import org.openjdk.jmh.annotations.Warmup; |
| 78 | + |
| 79 | +import java.io.IOException; |
| 80 | +import java.util.ArrayList; |
| 81 | +import java.util.List; |
| 82 | +import java.util.Optional; |
| 83 | +import java.util.concurrent.TimeUnit; |
| 84 | +import java.util.function.Function; |
| 85 | + |
| 86 | +/** |
| 87 | + * Benchmarks the overhead of constructing {@link Aggregator}s in many |
| 88 | + * parallel threads. Machines with different numbers of cores will see |
| 89 | + * wildly different results running this from running this with more |
| 90 | + * cores seeing more benefits from preallocation. |
| 91 | + */ |
| 92 | +@Fork(2) |
| 93 | +@Warmup(iterations = 10) |
| 94 | +@Measurement(iterations = 5) |
| 95 | +@BenchmarkMode(Mode.AverageTime) |
| 96 | +@OutputTimeUnit(TimeUnit.MICROSECONDS) |
| 97 | +@State(Scope.Benchmark) |
| 98 | +@Threads(Threads.MAX) |
| 99 | +public class AggConstructionContentionBenchmark { |
| 100 | + private final SearchModule searchModule = new SearchModule(Settings.EMPTY, List.of()); |
| 101 | + private final ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS); |
| 102 | + private final PageCacheRecycler recycler = new PageCacheRecycler(Settings.EMPTY); |
| 103 | + private final Index index = new Index("test", "uuid"); |
| 104 | + private final IndicesFieldDataCache indicesFieldDataCache = new IndicesFieldDataCache( |
| 105 | + Settings.EMPTY, |
| 106 | + new IndexFieldDataCache.Listener() { |
| 107 | + } |
| 108 | + ); |
| 109 | + |
| 110 | + private CircuitBreakerService breakerService; |
| 111 | + private BigArrays bigArrays; |
| 112 | + private boolean preallocateBreaker; |
| 113 | + |
| 114 | + @Param({ "noop", "real", "preallocate" }) |
| 115 | + private String breaker; |
| 116 | + |
| 117 | + @Setup |
| 118 | + public void setup() { |
| 119 | + switch (breaker) { |
| 120 | + case "real": |
| 121 | + breakerService = new HierarchyCircuitBreakerService(Settings.EMPTY, List.of(), clusterSettings); |
| 122 | + break; |
| 123 | + case "preallocate": |
| 124 | + preallocateBreaker = true; |
| 125 | + breakerService = new HierarchyCircuitBreakerService(Settings.EMPTY, List.of(), clusterSettings); |
| 126 | + break; |
| 127 | + case "noop": |
| 128 | + breakerService = new NoneCircuitBreakerService(); |
| 129 | + break; |
| 130 | + default: |
| 131 | + throw new UnsupportedOperationException(); |
| 132 | + } |
| 133 | + bigArrays = new BigArrays(recycler, breakerService, "request"); |
| 134 | + } |
| 135 | + |
| 136 | + @Benchmark |
| 137 | + public void sum() throws IOException { |
| 138 | + buildFactories(new AggregatorFactories.Builder().addAggregator(new SumAggregationBuilder("s").field("int_1"))); |
| 139 | + } |
| 140 | + |
| 141 | + @Benchmark |
| 142 | + public void termsSum() throws IOException { |
| 143 | + buildFactories( |
| 144 | + new AggregatorFactories.Builder().addAggregator( |
| 145 | + new TermsAggregationBuilder("t").field("int_1").subAggregation(new SumAggregationBuilder("s").field("int_2")) |
| 146 | + ) |
| 147 | + ); |
| 148 | + } |
| 149 | + |
| 150 | + @Benchmark |
| 151 | + public void termsSixtySums() throws IOException { |
| 152 | + TermsAggregationBuilder b = new TermsAggregationBuilder("t").field("int_1"); |
| 153 | + for (int i = 0; i < 60; i++) { |
| 154 | + b.subAggregation(new SumAggregationBuilder("s" + i).field("int_" + i)); |
| 155 | + } |
| 156 | + buildFactories(new AggregatorFactories.Builder().addAggregator(b)); |
| 157 | + } |
| 158 | + |
| 159 | + private void buildFactories(AggregatorFactories.Builder factories) throws IOException { |
| 160 | + try (DummyAggregationContext context = new DummyAggregationContext(factories.bytesToPreallocate())) { |
| 161 | + factories.build(context, null).createTopLevelAggregators(); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + private class DummyAggregationContext extends AggregationContext { |
| 166 | + private final Query query = new MatchAllDocsQuery(); |
| 167 | + private final List<Releasable> releaseMe = new ArrayList<>(); |
| 168 | + |
| 169 | + private final CircuitBreaker breaker; |
| 170 | + private final PreallocatedCircuitBreakerService preallocated; |
| 171 | + private final MultiBucketConsumer multiBucketConsumer; |
| 172 | + |
| 173 | + DummyAggregationContext(long bytesToPreallocate) { |
| 174 | + CircuitBreakerService breakerService; |
| 175 | + if (preallocateBreaker) { |
| 176 | + breakerService = preallocated = new PreallocatedCircuitBreakerService( |
| 177 | + AggConstructionContentionBenchmark.this.breakerService, |
| 178 | + CircuitBreaker.REQUEST, |
| 179 | + bytesToPreallocate, |
| 180 | + "aggregations" |
| 181 | + ); |
| 182 | + } else { |
| 183 | + breakerService = AggConstructionContentionBenchmark.this.breakerService; |
| 184 | + preallocated = null; |
| 185 | + } |
| 186 | + breaker = breakerService.getBreaker(CircuitBreaker.REQUEST); |
| 187 | + multiBucketConsumer = new MultiBucketConsumer(Integer.MAX_VALUE, breaker); |
| 188 | + } |
| 189 | + |
| 190 | + @Override |
| 191 | + public Query query() { |
| 192 | + return query; |
| 193 | + } |
| 194 | + |
| 195 | + @Override |
| 196 | + public Aggregator profileIfEnabled(Aggregator agg) throws IOException { |
| 197 | + return agg; |
| 198 | + } |
| 199 | + |
| 200 | + @Override |
| 201 | + public boolean profiling() { |
| 202 | + return false; |
| 203 | + } |
| 204 | + |
| 205 | + @Override |
| 206 | + public long nowInMillis() { |
| 207 | + return 0; |
| 208 | + } |
| 209 | + |
| 210 | + @Override |
| 211 | + protected IndexFieldData<?> buildFieldData(MappedFieldType ft) { |
| 212 | + IndexFieldDataCache indexFieldDataCache = indicesFieldDataCache.buildIndexFieldDataCache(new IndexFieldDataCache.Listener() { |
| 213 | + }, index, ft.name()); |
| 214 | + return ft.fielddataBuilder("test", this::lookup).build(indexFieldDataCache, breakerService); |
| 215 | + } |
| 216 | + |
| 217 | + @Override |
| 218 | + public MappedFieldType getFieldType(String path) { |
| 219 | + if (path.startsWith("int")) { |
| 220 | + return new NumberFieldMapper.NumberFieldType(path, NumberType.INTEGER); |
| 221 | + } |
| 222 | + throw new UnsupportedOperationException(); |
| 223 | + } |
| 224 | + |
| 225 | + @Override |
| 226 | + public boolean isFieldMapped(String field) { |
| 227 | + return field.startsWith("int"); |
| 228 | + } |
| 229 | + |
| 230 | + @Override |
| 231 | + public <FactoryType> FactoryType compile(Script script, ScriptContext<FactoryType> context) { |
| 232 | + throw new UnsupportedOperationException(); |
| 233 | + } |
| 234 | + |
| 235 | + @Override |
| 236 | + public SearchLookup lookup() { |
| 237 | + throw new UnsupportedOperationException(); |
| 238 | + } |
| 239 | + |
| 240 | + @Override |
| 241 | + public ValuesSourceRegistry getValuesSourceRegistry() { |
| 242 | + return searchModule.getValuesSourceRegistry(); |
| 243 | + } |
| 244 | + |
| 245 | + @Override |
| 246 | + public BigArrays bigArrays() { |
| 247 | + return bigArrays; |
| 248 | + } |
| 249 | + |
| 250 | + @Override |
| 251 | + public IndexSearcher searcher() { |
| 252 | + return null; |
| 253 | + } |
| 254 | + |
| 255 | + @Override |
| 256 | + public Query buildQuery(QueryBuilder builder) throws IOException { |
| 257 | + throw new UnsupportedOperationException(); |
| 258 | + } |
| 259 | + |
| 260 | + @Override |
| 261 | + public IndexSettings getIndexSettings() { |
| 262 | + throw new UnsupportedOperationException(); |
| 263 | + } |
| 264 | + |
| 265 | + @Override |
| 266 | + public Optional<SortAndFormats> buildSort(List<SortBuilder<?>> sortBuilders) throws IOException { |
| 267 | + throw new UnsupportedOperationException(); |
| 268 | + } |
| 269 | + |
| 270 | + @Override |
| 271 | + public ObjectMapper getObjectMapper(String path) { |
| 272 | + throw new UnsupportedOperationException(); |
| 273 | + } |
| 274 | + |
| 275 | + @Override |
| 276 | + public NestedScope nestedScope() { |
| 277 | + throw new UnsupportedOperationException(); |
| 278 | + } |
| 279 | + |
| 280 | + @Override |
| 281 | + public SubSearchContext subSearchContext() { |
| 282 | + throw new UnsupportedOperationException(); |
| 283 | + } |
| 284 | + |
| 285 | + @Override |
| 286 | + public void addReleasable(Aggregator aggregator) { |
| 287 | + releaseMe.add(aggregator); |
| 288 | + } |
| 289 | + |
| 290 | + @Override |
| 291 | + public MultiBucketConsumer multiBucketConsumer() { |
| 292 | + return multiBucketConsumer; |
| 293 | + } |
| 294 | + |
| 295 | + @Override |
| 296 | + public BitsetFilterCache bitsetFilterCache() { |
| 297 | + throw new UnsupportedOperationException(); |
| 298 | + } |
| 299 | + |
| 300 | + @Override |
| 301 | + public BucketedSort buildBucketedSort(SortBuilder<?> sort, int size, ExtraData values) throws IOException { |
| 302 | + throw new UnsupportedOperationException(); |
| 303 | + } |
| 304 | + |
| 305 | + @Override |
| 306 | + public int shardRandomSeed() { |
| 307 | + return 0; |
| 308 | + } |
| 309 | + |
| 310 | + @Override |
| 311 | + public long getRelativeTimeInMillis() { |
| 312 | + return 0; |
| 313 | + } |
| 314 | + |
| 315 | + @Override |
| 316 | + public boolean isCancelled() { |
| 317 | + return false; |
| 318 | + } |
| 319 | + |
| 320 | + @Override |
| 321 | + public CircuitBreaker breaker() { |
| 322 | + return breaker; |
| 323 | + } |
| 324 | + |
| 325 | + @Override |
| 326 | + public Analyzer getIndexAnalyzer(Function<String, NamedAnalyzer> unindexedFieldAnalyzer) { |
| 327 | + throw new UnsupportedOperationException(); |
| 328 | + } |
| 329 | + |
| 330 | + @Override |
| 331 | + public boolean isCacheable() { |
| 332 | + throw new UnsupportedOperationException(); |
| 333 | + } |
| 334 | + |
| 335 | + @Override |
| 336 | + public void close() { |
| 337 | + List<Releasable> releaseMe = new ArrayList<>(this.releaseMe); |
| 338 | + releaseMe.add(preallocated); |
| 339 | + Releasables.close(releaseMe); |
| 340 | + } |
| 341 | + } |
| 342 | +} |
0 commit comments