Skip to content

[7.x] Make ValuesSourceRegistry immutable after initilization #55493 #55697

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,9 @@ default List<AggregationSpec> getAggregations() {
/**
* Allows plugins to register new aggregations using aggregation names that are already defined
* in Core, as long as the new aggregations target different ValuesSourceTypes
* @return A list of the new registrar functions
*/
default List<Consumer<ValuesSourceRegistry>> getBareAggregatorRegistrar() {
default List<Consumer<ValuesSourceRegistry.Builder>> getAggregationExtentions() {
return emptyList();
}
/**
Expand Down Expand Up @@ -268,7 +269,7 @@ public QuerySpec(String name, Writeable.Reader<T> reader, QueryParser<T> parser)
*/
class AggregationSpec extends SearchExtensionSpec<AggregationBuilder, ContextParser<String, ? extends AggregationBuilder>> {
private final Map<String, Writeable.Reader<? extends InternalAggregation>> resultReaders = new TreeMap<>();
private Consumer<ValuesSourceRegistry> aggregatorRegistrar;
private Consumer<ValuesSourceRegistry.Builder> aggregatorRegistrar;

/**
* Specification for an {@link Aggregation}.
Expand Down Expand Up @@ -356,15 +357,15 @@ public Map<String, Writeable.Reader<? extends InternalAggregation>> getResultRea
* Get the function to register the {@link org.elasticsearch.search.aggregations.support.ValuesSource} to aggregator mappings for
* this aggregation
*/
public Consumer<ValuesSourceRegistry> getAggregatorRegistrar() {
public Consumer<ValuesSourceRegistry.Builder> getAggregatorRegistrar() {
return aggregatorRegistrar;
}

/**
* Set the function to register the {@link org.elasticsearch.search.aggregations.support.ValuesSource} to aggregator mappings for
* this aggregation
*/
public AggregationSpec setAggregatorRegistrar(Consumer<ValuesSourceRegistry> aggregatorRegistrar) {
public AggregationSpec setAggregatorRegistrar(Consumer<ValuesSourceRegistry.Builder> aggregatorRegistrar) {
this.aggregatorRegistrar = aggregatorRegistrar;
return this;
}
Expand Down
108 changes: 55 additions & 53 deletions server/src/main/java/org/elasticsearch/search/SearchModule.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public GeoHashGridAggregationBuilder(StreamInput in) throws IOException {
super(in);
}

public static void registerAggregators(ValuesSourceRegistry valuesSourceRegistry) {
GeoHashGridAggregatorFactory.registerAggregators(valuesSourceRegistry);
public static void registerAggregators(ValuesSourceRegistry.Builder builder) {
GeoHashGridAggregatorFactory.registerAggregators(builder);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ protected Aggregator doCreateInternal(final ValuesSource valuesSource,
requiredSize, shardSize, searchContext, parent, metadata);
}

static void registerAggregators(ValuesSourceRegistry valuesSourceRegistry) {
valuesSourceRegistry.register(GeoHashGridAggregationBuilder.NAME, CoreValuesSourceType.GEOPOINT,
static void registerAggregators(ValuesSourceRegistry.Builder builder) {
builder.register(GeoHashGridAggregationBuilder.NAME, CoreValuesSourceType.GEOPOINT,
(GeoGridAggregatorSupplier) (name, factories, valuesSource, precision, geoBoundingBox, requiredSize, shardSize,
aggregationContext, parent, metadata) -> {
CellIdSource cellIdSource = new CellIdSource((ValuesSource.GeoPoint) valuesSource, precision, geoBoundingBox,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public GeoTileGridAggregationBuilder(StreamInput in) throws IOException {
super(in);
}

public static void registerAggregators(ValuesSourceRegistry valuesSourceRegistry) {
GeoTileGridAggregatorFactory.registerAggregators(valuesSourceRegistry);
public static void registerAggregators(ValuesSourceRegistry.Builder builder) {
GeoTileGridAggregatorFactory.registerAggregators(builder);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ protected Aggregator doCreateInternal(final ValuesSource valuesSource,
requiredSize, shardSize, searchContext, parent, metadata);
}

static void registerAggregators(ValuesSourceRegistry valuesSourceRegistry) {
valuesSourceRegistry.register(GeoTileGridAggregationBuilder.NAME, CoreValuesSourceType.GEOPOINT,
static void registerAggregators(ValuesSourceRegistry.Builder builder) {
builder.register(GeoTileGridAggregationBuilder.NAME, CoreValuesSourceType.GEOPOINT,
(GeoGridAggregatorSupplier) (name, factories, valuesSource, precision, geoBoundingBox, requiredSize, shardSize,
aggregationContext, parent, metadata) -> {
CellIdSource cellIdSource = new CellIdSource((ValuesSource.GeoPoint) valuesSource, precision, geoBoundingBox,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import org.elasticsearch.index.mapper.MappedFieldType.Relation;
import org.elasticsearch.index.query.QueryShardContext;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregatorFactories.Builder;
import org.elasticsearch.search.aggregations.AggregatorFactories;
import org.elasticsearch.search.aggregations.AggregatorFactory;
import org.elasticsearch.search.aggregations.BucketOrder;
import org.elasticsearch.search.aggregations.InternalOrder;
Expand Down Expand Up @@ -117,8 +117,8 @@ public class DateHistogramAggregationBuilder extends ValuesSourceAggregationBuil
Histogram.ORDER_FIELD);
}

public static void registerAggregators(ValuesSourceRegistry valuesSourceRegistry) {
DateHistogramAggregatorFactory.registerAggregators(valuesSourceRegistry);
public static void registerAggregators(ValuesSourceRegistry.Builder builder) {
DateHistogramAggregatorFactory.registerAggregators(builder);
}

private DateIntervalWrapper dateHistogramInterval = new DateIntervalWrapper();
Expand All @@ -134,7 +134,7 @@ public DateHistogramAggregationBuilder(String name) {
}

protected DateHistogramAggregationBuilder(DateHistogramAggregationBuilder clone,
Builder factoriesBuilder, Map<String, Object> metadata) {
AggregatorFactories.Builder factoriesBuilder, Map<String, Object> metadata) {
super(clone, factoriesBuilder, metadata);
this.dateHistogramInterval = clone.dateHistogramInterval;
this.offset = clone.offset;
Expand All @@ -145,7 +145,7 @@ protected DateHistogramAggregationBuilder(DateHistogramAggregationBuilder clone,
}

@Override
protected AggregationBuilder shallowCopy(Builder factoriesBuilder, Map<String, Object> metadata) {
protected AggregationBuilder shallowCopy(AggregatorFactories.Builder factoriesBuilder, Map<String, Object> metadata) {
return new DateHistogramAggregationBuilder(this, factoriesBuilder, metadata);
}

Expand Down Expand Up @@ -526,7 +526,7 @@ ZoneId rewriteTimeZone(QueryShardContext context) throws IOException {
protected ValuesSourceAggregatorFactory innerBuild(QueryShardContext queryShardContext,
ValuesSourceConfig config,
AggregatorFactory parent,
Builder subFactoriesBuilder) throws IOException {
AggregatorFactories.Builder subFactoriesBuilder) throws IOException {
final ZoneId tz = timeZone();
final Rounding rounding = dateHistogramInterval.createRounding(tz, offset);
final ZoneId rewrittenTimeZone = rewriteTimeZone(queryShardContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@

public final class DateHistogramAggregatorFactory extends ValuesSourceAggregatorFactory {

public static void registerAggregators(ValuesSourceRegistry valuesSourceRegistry) {
valuesSourceRegistry.register(DateHistogramAggregationBuilder.NAME,
public static void registerAggregators(ValuesSourceRegistry.Builder builder) {
builder.register(DateHistogramAggregationBuilder.NAME,
Arrays.asList(CoreValuesSourceType.DATE, CoreValuesSourceType.NUMERIC, CoreValuesSourceType.BOOLEAN),
(DateHistogramAggregationSupplier) (String name,
AggregatorFactories factories,
Expand All @@ -62,7 +62,7 @@ public static void registerAggregators(ValuesSourceRegistry valuesSourceRegistry
factories, rounding, shardRounding, order, keyed, minDocCount, extendedBounds, (ValuesSource.Numeric) valuesSource,
formatter, aggregationContext, parent, metadata));

valuesSourceRegistry.register(DateHistogramAggregationBuilder.NAME,
builder.register(DateHistogramAggregationBuilder.NAME,
CoreValuesSourceType.RANGE,
(DateHistogramAggregationSupplier) (String name,
AggregatorFactories factories,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.query.QueryShardContext;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregatorFactories.Builder;
import org.elasticsearch.search.aggregations.AggregatorFactories;
import org.elasticsearch.search.aggregations.AggregatorFactory;
import org.elasticsearch.search.aggregations.BucketOrder;
import org.elasticsearch.search.aggregations.InternalOrder;
Expand Down Expand Up @@ -79,8 +79,8 @@ public class HistogramAggregationBuilder extends ValuesSourceAggregationBuilder<
Histogram.ORDER_FIELD);
}

public static void registerAggregators(ValuesSourceRegistry valuesSourceRegistry) {
HistogramAggregatorFactory.registerAggregators(valuesSourceRegistry);
public static void registerAggregators(ValuesSourceRegistry.Builder builder) {
HistogramAggregatorFactory.registerAggregators(builder);
}

private double interval;
Expand All @@ -101,7 +101,9 @@ public HistogramAggregationBuilder(String name) {
super(name);
}

protected HistogramAggregationBuilder(HistogramAggregationBuilder clone, Builder factoriesBuilder, Map<String, Object> metadata) {
protected HistogramAggregationBuilder(HistogramAggregationBuilder clone,
AggregatorFactories.Builder factoriesBuilder,
Map<String, Object> metadata) {
super(clone, factoriesBuilder, metadata);
this.interval = clone.interval;
this.offset = clone.offset;
Expand All @@ -113,7 +115,7 @@ protected HistogramAggregationBuilder(HistogramAggregationBuilder clone, Builder
}

@Override
protected AggregationBuilder shallowCopy(Builder factoriesBuilder, Map<String, Object> metadata) {
protected AggregationBuilder shallowCopy(AggregatorFactories.Builder factoriesBuilder, Map<String, Object> metadata) {
return new HistogramAggregationBuilder(this, factoriesBuilder, metadata);
}

Expand Down Expand Up @@ -304,7 +306,7 @@ public String getType() {
protected ValuesSourceAggregatorFactory innerBuild(QueryShardContext queryShardContext,
ValuesSourceConfig config,
AggregatorFactory parent,
Builder subFactoriesBuilder) throws IOException {
AggregatorFactories.Builder subFactoriesBuilder) throws IOException {
return new HistogramAggregatorFactory(name, config, interval, offset, order, keyed, minDocCount, minBound, maxBound,
queryShardContext, parent, subFactoriesBuilder, metadata);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ public final class HistogramAggregatorFactory extends ValuesSourceAggregatorFact
private final long minDocCount;
private final double minBound, maxBound;

// TODO: Registration should happen on the actual aggregator classes, but I don't want to set up the whole dynamic loading thing yet
static void registerAggregators(ValuesSourceRegistry valuesSourceRegistry) {
valuesSourceRegistry.register(HistogramAggregationBuilder.NAME, CoreValuesSourceType.RANGE,
static void registerAggregators(ValuesSourceRegistry.Builder builder) {
builder.register(HistogramAggregationBuilder.NAME, CoreValuesSourceType.RANGE,
new HistogramAggregatorSupplier() {
@Override
public Aggregator build(String name, AggregatorFactories factories, double interval, double offset,
Expand All @@ -73,7 +72,7 @@ public Aggregator build(String name, AggregatorFactories factories, double inter
}
);

valuesSourceRegistry.register(HistogramAggregationBuilder.NAME,
builder.register(HistogramAggregationBuilder.NAME,
Collections.unmodifiableList(Arrays.asList(CoreValuesSourceType.NUMERIC,
CoreValuesSourceType.DATE,
CoreValuesSourceType.BOOLEAN)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.query.QueryShardContext;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregatorFactories.Builder;
import org.elasticsearch.search.aggregations.AggregatorFactories;
import org.elasticsearch.search.aggregations.AggregatorFactory;
import org.elasticsearch.search.aggregations.support.CoreValuesSourceType;
import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder;
import org.elasticsearch.search.aggregations.support.ValuesSourceAggregatorFactory;
import org.elasticsearch.search.aggregations.support.ValuesSourceConfig;
import org.elasticsearch.search.aggregations.support.ValuesSourceRegistry;
import org.elasticsearch.search.aggregations.support.ValuesSourceType;

import java.io.IOException;
Expand All @@ -46,11 +47,17 @@ public class MissingAggregationBuilder extends ValuesSourceAggregationBuilder<Mi
ValuesSourceAggregationBuilder.declareFields(PARSER, true, true, false);
}

public static void registerAggregators(ValuesSourceRegistry.Builder builder) {
MissingAggregatorFactory.registerAggregators(builder);
}

public MissingAggregationBuilder(String name) {
super(name);
}

protected MissingAggregationBuilder(MissingAggregationBuilder clone, Builder factoriesBuilder, Map<String, Object> metadata) {
protected MissingAggregationBuilder(MissingAggregationBuilder clone,
AggregatorFactories.Builder factoriesBuilder,
Map<String, Object> metadata) {
super(clone, factoriesBuilder, metadata);
}

Expand All @@ -60,7 +67,7 @@ protected ValuesSourceType defaultValueSourceType() {
}

@Override
protected AggregationBuilder shallowCopy(Builder factoriesBuilder, Map<String, Object> metadata) {
protected AggregationBuilder shallowCopy(AggregatorFactories.Builder factoriesBuilder, Map<String, Object> metadata) {
return new MissingAggregationBuilder(this, factoriesBuilder, metadata);
}

Expand Down Expand Up @@ -90,7 +97,7 @@ public BucketCardinality bucketCardinality() {
protected ValuesSourceAggregatorFactory innerBuild(QueryShardContext queryShardContext,
ValuesSourceConfig config,
AggregatorFactory parent,
Builder subFactoriesBuilder) throws IOException {
AggregatorFactories.Builder subFactoriesBuilder) throws IOException {
return new MissingAggregatorFactory(name, config, queryShardContext, parent, subFactoriesBuilder, metadata);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,35 @@
import org.elasticsearch.search.aggregations.Aggregator;
import org.elasticsearch.search.aggregations.AggregatorFactories;
import org.elasticsearch.search.aggregations.AggregatorFactory;
import org.elasticsearch.search.aggregations.support.CoreValuesSourceType;
import org.elasticsearch.search.aggregations.support.ValuesSource;
import org.elasticsearch.search.aggregations.support.ValuesSourceAggregatorFactory;
import org.elasticsearch.search.aggregations.support.ValuesSourceConfig;
import org.elasticsearch.search.aggregations.support.ValuesSourceRegistry;
import org.elasticsearch.search.internal.SearchContext;

import java.io.IOException;
import java.util.Arrays;
import java.util.Map;

public class MissingAggregatorFactory extends ValuesSourceAggregatorFactory {

public static void registerAggregators(ValuesSourceRegistry.Builder builder) {
builder.register(
MissingAggregationBuilder.NAME,
Arrays.asList(
CoreValuesSourceType.NUMERIC,
CoreValuesSourceType.BYTES,
CoreValuesSourceType.GEOPOINT,
CoreValuesSourceType.RANGE,
CoreValuesSourceType.IP,
CoreValuesSourceType.BOOLEAN,
CoreValuesSourceType.DATE
),
(MissingAggregatorSupplier) MissingAggregator::new
);
}

public MissingAggregatorFactory(String name, ValuesSourceConfig config, QueryShardContext queryShardContext,
AggregatorFactory parent, AggregatorFactories.Builder subFactoriesBuilder,
Map<String, Object> metadata) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.elasticsearch.search.aggregations.bucket.missing;

import org.elasticsearch.common.Nullable;
import org.elasticsearch.search.aggregations.Aggregator;
import org.elasticsearch.search.aggregations.AggregatorFactories;
import org.elasticsearch.search.aggregations.support.AggregatorSupplier;
import org.elasticsearch.search.aggregations.support.ValuesSource;
import org.elasticsearch.search.internal.SearchContext;

import java.io.IOException;
import java.util.Map;

@FunctionalInterface
public interface MissingAggregatorSupplier extends AggregatorSupplier {

Aggregator build(String name,
AggregatorFactories factories,
@Nullable ValuesSource valuesSource,
SearchContext aggregationContext,
Aggregator parent,
Map<String, Object> metadata) throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ public class AbstractRangeAggregatorFactory<R extends Range> extends ValuesSourc
private final boolean keyed;
private final String aggregationTypeName;

public static void registerAggregators(ValuesSourceRegistry valuesSourceRegistry, String aggregationName) {
valuesSourceRegistry.register(aggregationName,
public static void registerAggregators(ValuesSourceRegistry.Builder builder,
String aggregationName) {
builder.register(aggregationName,
Arrays.asList(CoreValuesSourceType.NUMERIC, CoreValuesSourceType.DATE, CoreValuesSourceType.BOOLEAN),
new RangeAggregatorSupplier() {
@Override
Expand Down
Loading