forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introducing TestFeatureService to ESRestTestCase (elastic#102243)
* Introducing TestFeatureService to ESRestTestCase - Added RestTestLegacyFeatures to encompass legacy (historical) features that have been removed from production code but are still needed by REST tests - Encapsulated Mark's getHistoricalFeatures method inside a FeatureProvider (ESRestTestCaseHistoricalFeatures) - ESRestTestCaseHistoricalFeatures is not yet used, as we need to figure out how to deal with old cluster tests
- Loading branch information
Showing
8 changed files
with
251 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
server/src/main/java/org/elasticsearch/features/FeatureData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.features; | ||
|
||
import org.elasticsearch.Version; | ||
import org.elasticsearch.common.Strings; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.NavigableMap; | ||
import java.util.Set; | ||
import java.util.TreeMap; | ||
|
||
import static org.elasticsearch.features.FeatureService.CLUSTER_FEATURES_ADDED_VERSION; | ||
|
||
/** | ||
* Reads and consolidate features exposed by a list {@link FeatureSpecification}, grouping them into historical features and node | ||
* features for the consumption of {@link FeatureService} | ||
*/ | ||
public class FeatureData { | ||
private final NavigableMap<Version, Set<String>> historicalFeatures; | ||
private final Map<String, NodeFeature> nodeFeatures; | ||
|
||
private FeatureData(NavigableMap<Version, Set<String>> historicalFeatures, Map<String, NodeFeature> nodeFeatures) { | ||
this.historicalFeatures = historicalFeatures; | ||
this.nodeFeatures = nodeFeatures; | ||
} | ||
|
||
public static FeatureData createFromSpecifications(List<? extends FeatureSpecification> specs) { | ||
Map<String, FeatureSpecification> allFeatures = new HashMap<>(); | ||
|
||
NavigableMap<Version, Set<String>> historicalFeatures = new TreeMap<>(); | ||
Map<String, NodeFeature> nodeFeatures = new HashMap<>(); | ||
for (FeatureSpecification spec : specs) { | ||
for (var hfe : spec.getHistoricalFeatures().entrySet()) { | ||
FeatureSpecification existing = allFeatures.putIfAbsent(hfe.getKey().id(), spec); | ||
// the same SPI class can be loaded multiple times if it's in the base classloader | ||
if (existing != null && existing.getClass() != spec.getClass()) { | ||
throw new IllegalArgumentException( | ||
Strings.format("Duplicate feature - [%s] is declared by both [%s] and [%s]", hfe.getKey().id(), existing, spec) | ||
); | ||
} | ||
|
||
if (hfe.getValue().after(CLUSTER_FEATURES_ADDED_VERSION)) { | ||
throw new IllegalArgumentException( | ||
Strings.format( | ||
"Historical feature [%s] declared by [%s] for version [%s] is not a historical version", | ||
hfe.getKey().id(), | ||
spec, | ||
hfe.getValue() | ||
) | ||
); | ||
} | ||
|
||
historicalFeatures.computeIfAbsent(hfe.getValue(), k -> new HashSet<>()).add(hfe.getKey().id()); | ||
} | ||
|
||
for (NodeFeature f : spec.getFeatures()) { | ||
FeatureSpecification existing = allFeatures.putIfAbsent(f.id(), spec); | ||
if (existing != null && existing.getClass() != spec.getClass()) { | ||
throw new IllegalArgumentException( | ||
Strings.format("Duplicate feature - [%s] is declared by both [%s] and [%s]", f.id(), existing, spec) | ||
); | ||
} | ||
|
||
nodeFeatures.put(f.id(), f); | ||
} | ||
} | ||
|
||
return new FeatureData(consolidateHistoricalFeatures(historicalFeatures), Map.copyOf(nodeFeatures)); | ||
} | ||
|
||
private static NavigableMap<Version, Set<String>> consolidateHistoricalFeatures( | ||
NavigableMap<Version, Set<String>> declaredHistoricalFeatures | ||
) { | ||
// update each version by adding in all features from previous versions | ||
Set<String> featureAggregator = new HashSet<>(); | ||
for (Map.Entry<Version, Set<String>> versions : declaredHistoricalFeatures.entrySet()) { | ||
featureAggregator.addAll(versions.getValue()); | ||
versions.setValue(Set.copyOf(featureAggregator)); | ||
} | ||
|
||
return Collections.unmodifiableNavigableMap(declaredHistoricalFeatures); | ||
} | ||
|
||
public NavigableMap<Version, Set<String>> getHistoricalFeatures() { | ||
return historicalFeatures; | ||
} | ||
|
||
public Map<String, NodeFeature> getNodeFeatures() { | ||
return nodeFeatures; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.