Skip to content

Make ValuesSourceRegistry immutable after initilization #55493

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

Conversation

not-napoleon
Copy link
Member

This PR separates out the registration methods from the read methods on ValuesSourceRegistry, putting the former into a new builder class. The various methods called from SearchModule to register aggregators now accept a ValuesSourceRegistryBuilder, and later uses in ValuesSourceConfig &c still use the main ValuesSourceRegistry class. SearchModule, after registering all core & plugin aggregations, constructs an immutable ValuesSourceRegistry from the builder, which it then stores and hands out as needed.

Once again, we jump through some hoops in building the immutable data structure in order to use the Java 9+ immutable collections, which use less memory and have (marginally) better access performance than the older unmodifiable wrappers. (The backport will use the unmodifiable wrappers, because of java version compatibility).

@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-analytics-geo (:Analytics/Aggregations)

Copy link
Member

@nik9000 nik9000 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I left a few small things.

*/
default List<Consumer<ValuesSourceRegistry>> getBareAggregatorRegistrar() {
default List<Consumer<ValuesSourceRegistry.ValuesSourceRegistryBuilder>> getBareAggregatorRegistrar() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ValuesSourceRegistry.Builder?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename method to extendAggregations or something? Also, now that I'm looking at it, why return a list instead of just a method? Or even just:

default void extendAggregations(ValuesSourceRegistry.Builder builder) {}

?

I know it doesn't line up with the other "return lists of stuff" methods on these classes but ValuesSourceRegistry is a different animal anyway.


// after aggs have been registered, see if there are any new VSTypes that need to be linked to core fields
registerFromPlugin(plugins, SearchPlugin::getBareAggregatorRegistrar, this::registerBareAggregatorRegistrar);
registerFromPlugin(plugins, SearchPlugin::getBareAggregatorRegistrar,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it isn't worth having the method named registerBareAggregatorRegistrar now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just use the lambda.

* applied to that type.
* @param aggregatorSupplier An Aggregation-specific specialization of AggregatorSupplier which will construct the mapped aggregator
*/
public synchronized void register(String aggregationName, Predicate<ValuesSourceType> appliesTo,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it needs to be synchronized any more because we do all plugin building in one thread.

@@ -18,6 +18,26 @@
*/
package org.elasticsearch.search.aggregations;

import static org.elasticsearch.test.InternalAggregationTestCase.DEFAULT_MAX_BUCKETS;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think your import order config is a bit confused here. I believe we usually put static last. Not a big deal, but it makes the diff bigger.

/**
* Register a ValuesSource to Aggregator mapping.
*
* Threading behavior notes: This call is both synchronized and expensive. It copies the entire existing mapping structure each
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this still relevant ?

 Conflicts:
	x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/SpatialPlugin.java
@not-napoleon
Copy link
Member Author

@elasticmachine run elasticsearch-ci/1

Copy link
Member

@nik9000 nik9000 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@@ -81,7 +81,8 @@ protected void innerWriteTo(StreamOutput out) {

@Override
protected AvgAggregatorFactory innerBuild(QueryShardContext queryShardContext, ValuesSourceConfig config,
AggregatorFactory parent, Builder subFactoriesBuilder) throws IOException {
AggregatorFactory parent,
AggregatorFactories.Builder subFactoriesBuilder) throws IOException {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Glad you like it. Intellij stuffed in the fully qualified package name there, and I did a manual pass yesterday to trim them down to just AggregatorFactories.Builder.

*/
public void registerAny(String aggregationName, AggregatorSupplier aggregatorSupplier) {
register(aggregationName, (ignored) -> true, aggregatorSupplier);
// Maps Aggregation names to (ValuesSourceType, Supplier) pairs, keyed by ValuesSourceType
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Javadoc instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure

@not-napoleon not-napoleon merged commit 85a160f into elastic:master Apr 23, 2020
@not-napoleon not-napoleon deleted the vs-refactor-immutable-registry branch April 23, 2020 15:54
not-napoleon added a commit to not-napoleon/elasticsearch that referenced this pull request Apr 23, 2020
WIP: this commit just resolves merge conflicts.  Still need to fix
compile errors

 Conflicts:
	server/src/main/java/org/elasticsearch/search/SearchModule.java
	server/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/DateHistogramAggregatorFactory.java
	server/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/HistogramAggregatorFactory.java
	server/src/main/java/org/elasticsearch/search/aggregations/bucket/missing/MissingAggregationBuilder.java
	server/src/main/java/org/elasticsearch/search/aggregations/bucket/missing/MissingAggregatorFactory.java
	server/src/main/java/org/elasticsearch/search/aggregations/bucket/range/AbstractRangeAggregatorFactory.java
	server/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/SignificantTermsAggregatorFactory.java
	server/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/RareTermsAggregatorFactory.java
	server/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/TermsAggregatorFactory.java
	server/src/main/java/org/elasticsearch/search/aggregations/metrics/AvgAggregatorFactory.java
	server/src/main/java/org/elasticsearch/search/aggregations/metrics/ExtendedStatsAggregationBuilder.java
	server/src/main/java/org/elasticsearch/search/aggregations/metrics/ExtendedStatsAggregatorFactory.java
	server/src/main/java/org/elasticsearch/search/aggregations/metrics/MaxAggregatorFactory.java
	server/src/main/java/org/elasticsearch/search/aggregations/metrics/MinAggregatorFactory.java
	server/src/main/java/org/elasticsearch/search/aggregations/metrics/PercentileRanksAggregatorFactory.java
	server/src/main/java/org/elasticsearch/search/aggregations/metrics/PercentilesAggregatorFactory.java
	server/src/main/java/org/elasticsearch/search/aggregations/metrics/StatsAggregatorFactory.java
	server/src/main/java/org/elasticsearch/search/aggregations/metrics/SumAggregatorFactory.java
	server/src/main/java/org/elasticsearch/search/aggregations/support/ValuesSourceRegistry.java
	server/src/main/java/org/elasticsearch/search/aggregations/support/ValuesSourceType.java
	test/framework/src/main/java/org/elasticsearch/search/aggregations/AggregatorTestCase.java
	x-pack/plugin/analytics/src/main/java/org/elasticsearch/xpack/analytics/AnalyticsPlugin.java
	x-pack/plugin/analytics/src/main/java/org/elasticsearch/xpack/analytics/boxplot/BoxplotAggregatorFactory.java
	x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/boxplot/BoxplotAggregatorTests.java
	x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/mapper/HDRPreAggregatedPercentileRanksAggregatorTests.java
	x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/mapper/HDRPreAggregatedPercentilesAggregatorTests.java
	x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/mapper/TDigestPreAggregatedPercentileRanksAggregatorTests.java
	x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/mapper/TDigestPreAggregatedPercentilesAggregatorTests.java
	x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/stringstats/StringStatsAggregatorTests.java
	x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/SpatialPlugin.java
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants