Skip to content

Commit

Permalink
Refactoring: extract methods from ESRestTestCase#initClient (elasti…
Browse files Browse the repository at this point in the history
…c#103579)

Initialization of information about the cluster under test is factored out into 2 methods, which can be used with different admin clients (e.g. in case of multi-cluster, like CCS).
  • Loading branch information
ldematte authored Dec 20, 2023
1 parent 7773364 commit 0406924
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.function.Predicate;
Expand Down Expand Up @@ -216,9 +215,27 @@ public enum ProductFeature {
}

private static EnumSet<ProductFeature> availableFeatures;
private static Set<String> nodeVersions;
private static Set<String> nodesVersions;
private static TestFeatureService testFeatureService;

protected static Set<String> getCachedNodesVersions() {
assert nodesVersions != null;
return nodesVersions;
}

protected static Set<String> readVersionsFromNodesInfo(RestClient adminClient) throws IOException {
return getNodesInfo(adminClient).values().stream().map(nodeInfo -> nodeInfo.get("version").toString()).collect(Collectors.toSet());
}

protected static Map<String, Map<?, ?>> getNodesInfo(RestClient adminClient) throws IOException {
Map<?, ?> response = entityAsMap(adminClient.performRequest(new Request("GET", "_nodes/plugins")));
Map<?, ?> nodes = (Map<?, ?>) response.get("nodes");

return nodes.entrySet()
.stream()
.collect(Collectors.toUnmodifiableMap(entry -> entry.getKey().toString(), entry -> (Map<?, ?>) entry.getValue()));
}

protected static boolean clusterHasFeature(String featureId) {
return testFeatureService.clusterHasFeature(featureId);
}
Expand All @@ -233,24 +250,20 @@ public void initClient() throws IOException {
assert adminClient == null;
assert clusterHosts == null;
assert availableFeatures == null;
assert nodeVersions == null;
assert nodesVersions == null;
assert testFeatureService == null;
clusterHosts = parseClusterHosts(getTestRestCluster());
logger.info("initializing REST clients against {}", clusterHosts);
client = buildClient(restClientSettings(), clusterHosts.toArray(new HttpHost[clusterHosts.size()]));
adminClient = buildClient(restAdminSettings(), clusterHosts.toArray(new HttpHost[clusterHosts.size()]));

availableFeatures = EnumSet.of(ProductFeature.LEGACY_TEMPLATES);
nodeVersions = new TreeSet<>();
var semanticNodeVersions = new HashSet<Version>();
Set<String> versions = new HashSet<>();
boolean serverless = false;
Map<?, ?> response = entityAsMap(adminClient.performRequest(new Request("GET", "_nodes/plugins")));
Map<?, ?> nodes = (Map<?, ?>) response.get("nodes");
for (Map.Entry<?, ?> node : nodes.entrySet()) {
Map<?, ?> nodeInfo = (Map<?, ?>) node.getValue();

for (Map<?, ?> nodeInfo : getNodesInfo(adminClient).values()) {
var nodeVersion = nodeInfo.get("version").toString();
nodeVersions.add(nodeVersion);
parseLegacyVersion(nodeVersion).map(semanticNodeVersions::add);
versions.add(nodeVersion);
for (Object module : (List<?>) nodeInfo.get("modules")) {
Map<?, ?> moduleInfo = (Map<?, ?>) module;
final String moduleName = moduleInfo.get("name").toString();
Expand Down Expand Up @@ -289,29 +302,39 @@ 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;

// Historical features information is unavailable when using legacy test plugins
boolean hasHistoricalFeaturesInformation = System.getProperty("tests.features.metadata.path") != null;
var providers = hasHistoricalFeaturesInformation
? List.of(new RestTestLegacyFeatures(), new ESRestTestCaseHistoricalFeatures())
: List.of(new RestTestLegacyFeatures());

testFeatureService = new TestFeatureService(
hasHistoricalFeaturesInformation,
providers,
semanticNodeVersions,
ClusterFeatures.calculateAllNodeFeatures(getClusterStateFeatures().values())
);
testFeatureService = createTestFeatureService(adminClient, semanticNodeVersions);
}

assert testFeatureService != null;
assert client != null;
assert adminClient != null;
assert clusterHosts != null;
assert availableFeatures != null;
assert nodeVersions != null;
assert nodesVersions != null;
}

protected static TestFeatureService createTestFeatureService(RestClient adminClient, Set<Version> semanticNodeVersions)
throws IOException {
// Historical features information is unavailable when using legacy test plugins
boolean hasHistoricalFeaturesInformation = System.getProperty("tests.features.metadata.path") != null;
var providers = hasHistoricalFeaturesInformation
? List.of(new RestTestLegacyFeatures(), new ESRestTestCaseHistoricalFeatures())
: List.of(new RestTestLegacyFeatures());

return new TestFeatureService(
hasHistoricalFeaturesInformation,
providers,
semanticNodeVersions,
ClusterFeatures.calculateAllNodeFeatures(getClusterStateFeatures(adminClient).values())
);
}

protected static boolean has(ProductFeature feature) {
Expand Down Expand Up @@ -415,7 +438,7 @@ private boolean isExclusivelyTargetingCurrentVersionCluster() {

public static RequestOptions expectVersionSpecificWarnings(Consumer<VersionSensitiveWarningsHandler> expectationsSetter) {
Builder builder = RequestOptions.DEFAULT.toBuilder();
VersionSensitiveWarningsHandler warningsHandler = new VersionSensitiveWarningsHandler(new HashSet<>(nodeVersions));
VersionSensitiveWarningsHandler warningsHandler = new VersionSensitiveWarningsHandler(getCachedNodesVersions());
expectationsSetter.accept(warningsHandler);
builder.setWarningsHandler(warningsHandler);
return builder.build();
Expand Down Expand Up @@ -484,7 +507,7 @@ public static void closeClients() throws IOException {
client = null;
adminClient = null;
availableFeatures = null;
nodeVersions = null;
nodesVersions = null;
testFeatureService = null;
}
}
Expand Down Expand Up @@ -2071,11 +2094,11 @@ public void ensurePeerRecoveryRetentionLeasesRenewedAndSynced(String index) thro
}, 60, TimeUnit.SECONDS);
}

private static Map<String, Set<String>> getClusterStateFeatures() throws IOException {
private static Map<String, Set<String>> getClusterStateFeatures(RestClient adminClient) throws IOException {
final Request request = new Request("GET", "_cluster/state");
request.addParameter("filter_path", "nodes_features");

final Response response = adminClient().performRequest(request);
final Response response = adminClient.performRequest(request);

var responseData = responseAsMap(response);
if (responseData.get("nodes_features") instanceof List<?> nodesFeatures) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import java.util.Set;
import java.util.function.Predicate;

class TestFeatureService {
public class TestFeatureService {
private final Predicate<String> historicalFeaturesPredicate;
private final Set<String> clusterStateFeatures;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ Tuple<Version, Version> readVersionsFromCatNodes(RestClient restClient) throws I
return new Tuple<>(version, masterVersion);
}

String readOsFromNodesInfo(RestClient restClient) throws IOException {
static String readOsFromNodesInfo(RestClient restClient) throws IOException {
final Request request = new Request("GET", "/_nodes/os");
Response response = restClient.performRequest(request);
ClientYamlTestResponse restTestResponse = new ClientYamlTestResponse(response);
Expand Down

0 comments on commit 0406924

Please sign in to comment.