Skip to content

Commit 482585f

Browse files
committed
Several internal improvements to internal test cluster infra (#26214)
This chance adds several random test infrastructure improvements that caused issues in on-going developments but are generally useful. For instance is it impossible to restart a node with a secure setting source since we close it after the node is started. This change makes it cloneable such that we can reuse it for a restart.
1 parent 25552dd commit 482585f

File tree

7 files changed

+55
-16
lines changed

7 files changed

+55
-16
lines changed

core/src/main/java/org/elasticsearch/client/transport/TransportClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ ThreadPool getThreadPool() {
235235
public static final String CLIENT_TYPE = "transport";
236236

237237
final Injector injector;
238-
final NamedWriteableRegistry namedWriteableRegistry;
238+
protected final NamedWriteableRegistry namedWriteableRegistry;
239239

240240
private final List<LifecycleComponent> pluginLifecycleComponents;
241241
private final TransportClientNodesService nodesService;

test/framework/src/main/java/org/elasticsearch/common/settings/MockSecureSettings.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ public class MockSecureSettings implements SecureSettings {
3838
private Set<String> settingNames = new HashSet<>();
3939
private final AtomicBoolean closed = new AtomicBoolean(false);
4040

41+
public MockSecureSettings() {
42+
}
43+
44+
private MockSecureSettings(MockSecureSettings source) {
45+
secureStrings.putAll(source.secureStrings);
46+
files.putAll(source.files);
47+
settingNames.addAll(source.settingNames);
48+
}
49+
4150
@Override
4251
public boolean isLoaded() {
4352
return true;
@@ -94,4 +103,9 @@ private void ensureOpen() {
94103
throw new IllegalStateException("secure settings are already closed");
95104
}
96105
}
106+
107+
public SecureSettings clone() {
108+
ensureOpen();
109+
return new MockSecureSettings(this);
110+
}
97111
}

test/framework/src/main/java/org/elasticsearch/test/ESIntegTestCase.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,14 +1086,7 @@ protected void ensureClusterSizeConsistency() {
10861086
*/
10871087
protected void ensureClusterStateConsistency() throws IOException {
10881088
if (cluster() != null && cluster().size() > 0) {
1089-
final NamedWriteableRegistry namedWriteableRegistry;
1090-
if (isInternalCluster()) {
1091-
// If it's internal cluster - using existing registry in case plugin registered custom data
1092-
namedWriteableRegistry = internalCluster().getInstance(NamedWriteableRegistry.class);
1093-
} else {
1094-
// If it's external cluster - fall back to the standard set
1095-
namedWriteableRegistry = new NamedWriteableRegistry(ClusterModule.getNamedWriteables());
1096-
}
1089+
final NamedWriteableRegistry namedWriteableRegistry = cluster().getNamedWriteableRegistry();
10971090
ClusterState masterClusterState = client().admin().cluster().prepareState().all().get().getState();
10981091
byte[] masterClusterStateBytes = ClusterState.Builder.toBytes(masterClusterState);
10991092
// remove local node reference
@@ -2194,9 +2187,13 @@ protected static RestClient createRestClient(RestClientBuilder.HttpClientConfigC
21942187
}
21952188

21962189
protected static RestClient createRestClient(RestClientBuilder.HttpClientConfigCallback httpClientConfigCallback, String protocol) {
2197-
final NodesInfoResponse nodeInfos = client().admin().cluster().prepareNodesInfo().get();
2198-
final List<NodeInfo> nodes = nodeInfos.getNodes();
2199-
assertFalse(nodeInfos.hasFailures());
2190+
NodesInfoResponse nodesInfoResponse = client().admin().cluster().prepareNodesInfo().get();
2191+
assertFalse(nodesInfoResponse.hasFailures());
2192+
return createRestClient(nodesInfoResponse.getNodes(), httpClientConfigCallback, protocol);
2193+
}
2194+
2195+
protected static RestClient createRestClient(final List<NodeInfo> nodes,
2196+
RestClientBuilder.HttpClientConfigCallback httpClientConfigCallback, String protocol) {
22002197
List<HttpHost> hosts = new ArrayList<>();
22012198
for (NodeInfo node : nodes) {
22022199
if (node.getHttp() != null) {

test/framework/src/main/java/org/elasticsearch/test/ExternalTestCluster.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.elasticsearch.client.transport.TransportClient;
2929
import org.elasticsearch.cluster.node.DiscoveryNode;
3030
import org.elasticsearch.common.breaker.CircuitBreaker;
31+
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
3132
import org.elasticsearch.common.logging.Loggers;
3233
import org.elasticsearch.common.network.NetworkModule;
3334
import org.elasticsearch.common.settings.Settings;
@@ -60,7 +61,7 @@ public final class ExternalTestCluster extends TestCluster {
6061
private static final AtomicInteger counter = new AtomicInteger();
6162
public static final String EXTERNAL_CLUSTER_PREFIX = "external_";
6263

63-
private final Client client;
64+
private final MockTransportClient client;
6465

6566
private final InetSocketAddress[] httpAddresses;
6667

@@ -87,8 +88,7 @@ public ExternalTestCluster(Path tempDir, Settings additionalSettings, Collection
8788
}
8889
}
8990
Settings clientSettings = clientSettingsBuilder.build();
90-
TransportClient client = new MockTransportClient(clientSettings, pluginClasses);
91-
91+
MockTransportClient client = new MockTransportClient(clientSettings, pluginClasses);
9292
try {
9393
client.addTransportAddresses(transportAddresses);
9494
NodesInfoResponse nodeInfos = client.admin().cluster().prepareNodesInfo().clear().setSettings(true).setHttp(true).get();
@@ -109,6 +109,7 @@ public ExternalTestCluster(Path tempDir, Settings additionalSettings, Collection
109109
this.numDataNodes = dataNodes;
110110
this.numMasterAndDataNodes = masterAndDataNodes;
111111
this.client = client;
112+
112113
logger.info("Setup ExternalTestCluster [{}] made of [{}] nodes", nodeInfos.getClusterName().value(), size());
113114
} catch (Exception e) {
114115
client.close();
@@ -178,6 +179,11 @@ public Iterable<Client> getClients() {
178179
return Collections.singleton(client);
179180
}
180181

182+
@Override
183+
public NamedWriteableRegistry getNamedWriteableRegistry() {
184+
return client.getNamedWriteableRegistry();
185+
}
186+
181187
@Override
182188
public String getClusterName() {
183189
return clusterName;

test/framework/src/main/java/org/elasticsearch/test/InternalTestCluster.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,11 @@
5151
import org.elasticsearch.common.Strings;
5252
import org.elasticsearch.common.breaker.CircuitBreaker;
5353
import org.elasticsearch.common.io.FileSystemUtils;
54+
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
5455
import org.elasticsearch.common.lease.Releasables;
5556
import org.elasticsearch.common.logging.Loggers;
5657
import org.elasticsearch.common.network.NetworkModule;
58+
import org.elasticsearch.common.settings.MockSecureSettings;
5759
import org.elasticsearch.common.settings.SecureSettings;
5860
import org.elasticsearch.common.settings.Settings;
5961
import org.elasticsearch.common.settings.Settings.Builder;
@@ -609,6 +611,10 @@ private NodeAndClient buildNode(int nodeId, long seed, Settings settings,
609611
throw new IllegalArgumentException(DISCOVERY_ZEN_MINIMUM_MASTER_NODES_SETTING.getKey() + " must be configured");
610612
}
611613
SecureSettings secureSettings = finalSettings.getSecureSettings();
614+
if (secureSettings instanceof MockSecureSettings) {
615+
// we clone this here since in the case of a node restart we might need it again
616+
secureSettings = ((MockSecureSettings) secureSettings).clone();
617+
}
612618
MockNode node = new MockNode(finalSettings.build(), plugins, nodeConfigurationSource.nodeConfigPath(nodeId));
613619
try {
614620
IOUtils.close(secureSettings);
@@ -1961,6 +1967,11 @@ public void remove() {
19611967
};
19621968
}
19631969

1970+
@Override
1971+
public NamedWriteableRegistry getNamedWriteableRegistry() {
1972+
return getInstance(NamedWriteableRegistry.class);
1973+
}
1974+
19641975
/**
19651976
* Returns a predicate that only accepts settings of nodes with one of the given names.
19661977
*/

test/framework/src/main/java/org/elasticsearch/test/TestCluster.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.elasticsearch.client.Client;
2727
import org.elasticsearch.cluster.metadata.IndexMetaData;
2828
import org.elasticsearch.cluster.metadata.IndexTemplateMetaData;
29+
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
2930
import org.elasticsearch.common.logging.Loggers;
3031
import org.elasticsearch.index.IndexNotFoundException;
3132
import org.elasticsearch.indices.IndexTemplateMissingException;
@@ -34,6 +35,7 @@
3435
import java.io.Closeable;
3536
import java.io.IOException;
3637
import java.net.InetSocketAddress;
38+
import java.util.List;
3739
import java.util.Random;
3840
import java.util.Set;
3941

@@ -233,5 +235,9 @@ public void wipeRepositories(String... repositories) {
233235
*/
234236
public abstract Iterable<Client> getClients();
235237

236-
238+
/**
239+
* Returns this clusters {@link NamedWriteableRegistry} this is needed to
240+
* deserialize binary content from this cluster that might include custom named writeables
241+
*/
242+
public abstract NamedWriteableRegistry getNamedWriteableRegistry();
237243
}

test/framework/src/main/java/org/elasticsearch/transport/MockTransportClient.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.elasticsearch.transport;
2020

2121
import org.elasticsearch.client.transport.TransportClient;
22+
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
2223
import org.elasticsearch.common.settings.Settings;
2324
import org.elasticsearch.plugins.Plugin;
2425

@@ -52,4 +53,8 @@ private static Collection<Class<? extends Plugin>> addMockTransportIfMissing(Col
5253
plugins.add(MockTcpTransportPlugin.class);
5354
return plugins;
5455
}
56+
57+
public NamedWriteableRegistry getNamedWriteableRegistry() {
58+
return namedWriteableRegistry;
59+
}
5560
}

0 commit comments

Comments
 (0)