Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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 @@ -28,7 +28,7 @@
import org.elasticsearch.test.cluster.ElasticsearchCluster;
import org.elasticsearch.test.cluster.FeatureFlag;
import org.elasticsearch.test.cluster.local.LocalClusterConfigProvider;
import org.elasticsearch.test.rest.ESRestTestCase;
import org.elasticsearch.test.rest.ESRestTestFeatureService;
import org.elasticsearch.test.rest.ObjectPath;
import org.elasticsearch.test.rest.TestFeatureService;
import org.elasticsearch.test.rest.yaml.restspec.ClientYamlSuiteRestApi;
Expand All @@ -50,7 +50,6 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
Expand Down Expand Up @@ -315,13 +314,10 @@ protected ClientYamlTestExecutionContext createRestTestExecutionContext(
// Reconcile and provide unified features, os, version(s), based on both clientYamlTestClient and searchYamlTestClient
var searchOs = readOsFromNodesInfo(adminSearchClient);
var searchNodeVersions = readVersionsFromNodesInfo(adminSearchClient);
var semanticNodeVersions = searchNodeVersions.stream()
.map(ESRestTestCase::parseLegacyVersion)
.flatMap(Optional::stream)
.collect(Collectors.toSet());

final TestFeatureService searchTestFeatureService = createTestFeatureService(
getClusterStateFeatures(adminSearchClient),
semanticNodeVersions
ESRestTestFeatureService.fromSemanticVersions(searchNodeVersions)
);
final TestFeatureService combinedTestFeatureService = (featureId, any) -> {
boolean adminFeature = testFeatureService.clusterHasFeature(featureId, any);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import org.elasticsearch.test.cluster.FeatureFlag;
import org.elasticsearch.test.cluster.local.LocalClusterConfigProvider;
import org.elasticsearch.test.cluster.util.resource.Resource;
import org.elasticsearch.test.rest.ESRestTestCase;
import org.elasticsearch.test.rest.ESRestTestFeatureService;
import org.elasticsearch.test.rest.ObjectPath;
import org.elasticsearch.test.rest.TestFeatureService;
import org.elasticsearch.test.rest.yaml.CcsCommonYamlTestSuiteIT.TestCandidateAwareClient;
Expand All @@ -45,7 +45,6 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
Expand Down Expand Up @@ -298,14 +297,9 @@ protected ClientYamlTestExecutionContext createRestTestExecutionContext(
var searchOs = readOsFromNodesInfo(adminSearchClient);
var searchNodeVersions = readVersionsFromNodesInfo(adminSearchClient);

var semanticNodeVersions = searchNodeVersions.stream()
.map(ESRestTestCase::parseLegacyVersion)
.flatMap(Optional::stream)
.collect(Collectors.toSet());

final TestFeatureService searchTestFeatureService = createTestFeatureService(
getClusterStateFeatures(adminSearchClient),
semanticNodeVersions
ESRestTestFeatureService.fromSemanticVersions(searchNodeVersions)
);

final TestFeatureService combinedTestFeatureService = (featureId, any) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -446,13 +446,10 @@ public void initClient() throws IOException {
}
}
nodesVersions = Collections.unmodifiableSet(versions);

var semanticNodeVersions = nodesVersions.stream()
.map(ESRestTestCase::parseLegacyVersion)
.flatMap(Optional::stream)
.collect(Collectors.toSet());
assert semanticNodeVersions.isEmpty() == false || serverless;
testFeatureService = createTestFeatureService(getClusterStateFeatures(adminClient), semanticNodeVersions);
testFeatureService = createTestFeatureService(
getClusterStateFeatures(adminClient),
ESRestTestFeatureService.fromSemanticVersions(nodesVersions)
);

configureProjects();
}
Expand All @@ -467,9 +464,9 @@ public void initClient() throws IOException {

protected final TestFeatureService createTestFeatureService(
Map<String, Set<String>> clusterStateFeatures,
Set<Version> semanticNodeVersions
ESRestTestFeatureService.VersionFeaturesPredicate versionFeaturesPredicate
) {
return new ESRestTestFeatureService(semanticNodeVersions, clusterStateFeatures.values());
return new ESRestTestFeatureService(versionFeaturesPredicate, clusterStateFeatures.values());
}

protected static boolean has(ProductFeature feature) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,47 @@
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Predicate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import static java.util.Collections.emptySet;

class ESRestTestFeatureService implements TestFeatureService {
public class ESRestTestFeatureService implements TestFeatureService {

/**
* In order to migrate from version checks to cluster feature checks,
* we provide synthetic features derived from versions, e.g. "gte_v8.0.0".
*/
private static final Pattern VERSION_FEATURE_PATTERN = Pattern.compile("gte_v(\\d+\\.\\d+\\.\\d+)");

private final Collection<Version> nodeVersions;
public interface VersionFeaturesPredicate {
boolean test(Version featureVersion, boolean any);
}

public static VersionFeaturesPredicate fromSemanticVersions(Set<String> nodesVersions) {
Set<Version> semanticNodeVersions = nodesVersions.stream()
.map(ESRestTestCase::parseLegacyVersion)
.flatMap(Optional::stream)
.collect(Collectors.toSet());
if (semanticNodeVersions.isEmpty()) {
// Nodes do not have a semantic version (e.g. serverless).
// We assume the cluster is on the "latest version", and all is supported.
return ((featureVersion, any) -> true);
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

}

return (featureVersion, any) -> checkCollection(semanticNodeVersions, nodeVersion -> nodeVersion.onOrAfter(featureVersion), any);
}

private final VersionFeaturesPredicate versionFeaturesPredicate;
private final Collection<Set<String>> nodeFeatures;

ESRestTestFeatureService(Set<Version> nodeVersions, Collection<Set<String>> nodeFeatures) {
this.nodeVersions = nodeVersions;
ESRestTestFeatureService(VersionFeaturesPredicate versionFeaturesPredicate, Collection<Set<String>> nodeFeatures) {
this.versionFeaturesPredicate = versionFeaturesPredicate;
this.nodeFeatures = nodeFeatures;
}

Expand Down Expand Up @@ -88,7 +108,7 @@ public boolean clusterHasFeature(String featureId, boolean any) {
);
}

return checkCollection(nodeVersions, v -> v.onOrAfter(extractedVersion), any);
return versionFeaturesPredicate.test(extractedVersion, any);
Copy link
Contributor

Choose a reason for hiding this comment

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

optional nit... it took me a bit to recall what any was, maybe we should rename to anyMatch, matchAny, isAnySufficient ...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same :)
At least is consistent all the way, but I'll try and come up with a better name for it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Renamed.
I'm not entirely happy yet, I think I might do a followup that either splits the functions or uses an enum for better clarity (here but mainly in TestFeatureService#clusterHasFeature)

}

if (hasFeatureMetadata()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.elasticsearch.core.IOUtils;
import org.elasticsearch.test.ClasspathUtils;
import org.elasticsearch.test.rest.ESRestTestCase;
import org.elasticsearch.test.rest.ESRestTestFeatureService;
import org.elasticsearch.test.rest.TestFeatureService;
import org.elasticsearch.test.rest.yaml.restspec.ClientYamlSuiteRestApi;
import org.elasticsearch.test.rest.yaml.restspec.ClientYamlSuiteRestSpec;
Expand Down Expand Up @@ -54,7 +55,6 @@
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.stream.Collectors;

/**
* Runs a suite of yaml tests shared with all the official Elasticsearch
Expand Down Expand Up @@ -127,13 +127,9 @@ public void initAndResetContext() throws Exception {

logger.info("initializing client, node versions [{}], hosts {}, os [{}]", nodesVersions, hosts, os);

var semanticNodeVersions = nodesVersions.stream()
.map(ESRestTestCase::parseLegacyVersion)
.flatMap(Optional::stream)
.collect(Collectors.toSet());
final TestFeatureService testFeatureService = createTestFeatureService(
getClusterStateFeatures(adminClient()),
semanticNodeVersions
ESRestTestFeatureService.fromSemanticVersions(nodesVersions)
);

logger.info("initializing client, node versions [{}], hosts {}, os [{}]", nodesVersions, hosts, os);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import org.elasticsearch.core.IOUtils;
import org.elasticsearch.test.TestClustersThreadFilter;
import org.elasticsearch.test.cluster.ElasticsearchCluster;
import org.elasticsearch.test.rest.ESRestTestCase;
import org.elasticsearch.test.rest.ESRestTestFeatureService;
import org.elasticsearch.test.rest.TestFeatureService;
import org.elasticsearch.xpack.esql.CsvSpecReader;
import org.elasticsearch.xpack.esql.CsvSpecReader.CsvTestCase;
Expand All @@ -38,7 +38,6 @@
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -179,11 +178,10 @@ protected void shouldSkipTest(String testName) throws IOException {
private TestFeatureService remoteFeaturesService() throws IOException {
if (remoteFeaturesService == null) {
var remoteNodeVersions = readVersionsFromNodesInfo(remoteClusterClient());
var semanticNodeVersions = remoteNodeVersions.stream()
.map(ESRestTestCase::parseLegacyVersion)
.flatMap(Optional::stream)
.collect(Collectors.toSet());
remoteFeaturesService = createTestFeatureService(getClusterStateFeatures(remoteClusterClient()), semanticNodeVersions);
remoteFeaturesService = createTestFeatureService(
getClusterStateFeatures(remoteClusterClient()),
ESRestTestFeatureService.fromSemanticVersions(remoteNodeVersions)
);
}
return remoteFeaturesService;
}
Expand Down