Skip to content

Commit

Permalink
[TEST] only reset clients on nightly tests
Browse files Browse the repository at this point in the history
resetting the clients on each test (in after test) makes the tests running, especially in network mode, much slower, since transport client needs to be created each time when randmized to be used. Also, on OSX, the excessive connections causes bind exceptions eventually which makes running the network tests much harder on it.
closes #7329
  • Loading branch information
kimchy authored and areek committed Sep 8, 2014
1 parent 1491a1c commit 8d30e98
Showing 1 changed file with 42 additions and 72 deletions.
114 changes: 42 additions & 72 deletions src/test/java/org/elasticsearch/test/InternalTestCluster.java
Expand Up @@ -37,7 +37,6 @@
import org.elasticsearch.cache.recycler.PageCacheRecycler;
import org.elasticsearch.cache.recycler.PageCacheRecyclerModule;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.cluster.ClusterName;
import org.elasticsearch.cluster.ClusterService;
Expand Down Expand Up @@ -99,6 +98,7 @@
import java.util.concurrent.atomic.AtomicInteger;

import static com.carrotsearch.randomizedtesting.RandomizedTest.frequently;
import static com.carrotsearch.randomizedtesting.RandomizedTest.isNightly;
import static com.carrotsearch.randomizedtesting.RandomizedTest.systemPropertyAsBoolean;
import static junit.framework.Assert.fail;
import static org.apache.lucene.util.LuceneTestCase.rarely;
Expand Down Expand Up @@ -518,7 +518,7 @@ private NodeAndClient buildNode(int nodeId, long seed, Settings settings, Versio
.put("tests.mock.version", version)
.build();
Node node = nodeBuilder().settings(finalSettings).build();
return new NodeAndClient(name, node, new RandomClientFactory(settingsSource));
return new NodeAndClient(name, node);
}

private String buildNodeName(int id) {
Expand Down Expand Up @@ -667,17 +667,14 @@ public void close() {

private final class NodeAndClient implements Closeable {
private InternalNode node;
private Client client;
private Client nodeClient;
private Client transportClient;
private final AtomicBoolean closed = new AtomicBoolean(false);
private final ClientFactory clientFactory;
private final String name;

NodeAndClient(String name, Node node, ClientFactory factory) {
NodeAndClient(String name, Node node) {
this.node = (InternalNode) node;
this.name = name;
this.clientFactory = factory;
}

Node node() {
Expand All @@ -691,54 +688,60 @@ Client client(Random random) {
if (closed.get()) {
throw new RuntimeException("already closed");
}
if (client != null) {
return client;
double nextDouble = random.nextDouble();
if (nextDouble < transportClientRatio) {
if (logger.isDebugEnabled()) {
logger.debug("Using transport client for node [{}] sniff: [{}]", node.settings().get("name"), false);
}
return getOrBuildTransportClient();
} else {
return getOrBuildNodeClient();
}
return client = clientFactory.client(node, clusterName, random);
}

Client nodeClient() {
if (closed.get()) {
throw new RuntimeException("already closed");
}
if (nodeClient == null) {
Client maybeNodeClient = client(random);
if (client instanceof NodeClient) {
nodeClient = maybeNodeClient;
} else {
nodeClient = node.client();
}
}
return nodeClient;
return getOrBuildNodeClient();
}

Client transportClient() {
if (closed.get()) {
throw new RuntimeException("already closed");
}
if (transportClient == null) {
Client maybeTransportClient = client(random);
if (maybeTransportClient instanceof TransportClient) {
transportClient = maybeTransportClient;
} else {
transportClient = TransportClientFactory.noSniff(settingsSource.transportClient()).client(node, clusterName, random);
}
return getOrBuildTransportClient();
}

private Client getOrBuildNodeClient() {
if (nodeClient != null) {
return nodeClient;
}
return transportClient;
return nodeClient = node.client();
}

private Client getOrBuildTransportClient() {
if (transportClient != null) {
return transportClient;
}
/* no sniff client for now - doesn't work will all tests since it might throw NoNodeAvailableException if nodes are shut down.
* we first need support of transportClientRatio as annotations or so
*/
return transportClient = TransportClientFactory.noSniff(settingsSource.transportClient()).client(node, clusterName, random);
}

void resetClient() throws IOException {
if (closed.get()) {
throw new RuntimeException("already closed");
}
Releasables.close(client, nodeClient, transportClient);
client = null;
Releasables.close(nodeClient, transportClient);
nodeClient = null;
transportClient = null;
}

void restart(RestartCallback callback) throws Exception {
assert callback != null;
resetClient();
if (!node.isClosed()) {
node.close();
}
Expand All @@ -753,29 +756,19 @@ void restart(RestartCallback callback) throws Exception {
}
}
node = (InternalNode) nodeBuilder().settings(node.settings()).settings(newSettings).node();
resetClient();
}


@Override
public void close() throws IOException {
resetClient();
closed.set(true);
Releasables.close(client, nodeClient);
client = null;
nodeClient = null;
node.close();
}
}

static class ClientFactory {

public Client client(Node node, String clusterName, Random random) {
return node.client();
}
}

public static final String TRANSPORT_CLIENT_PREFIX = "transport_client_";
static class TransportClientFactory extends ClientFactory {
static class TransportClientFactory {

private static TransportClientFactory NO_SNIFF_CLIENT_FACTORY = new TransportClientFactory(false, ImmutableSettings.EMPTY);
private static TransportClientFactory SNIFF_CLIENT_FACTORY = new TransportClientFactory(true, ImmutableSettings.EMPTY);
Expand All @@ -802,7 +795,6 @@ public static TransportClientFactory sniff(Settings settings) {
this.settings = settings != null ? settings : ImmutableSettings.EMPTY;
}

@Override
public Client client(Node node, String clusterName, Random random) {
TransportAddress addr = ((InternalNode) node).injector().getInstance(TransportService.class).boundAddress().publishAddress();
Settings nodeSettings = node.settings();
Expand All @@ -824,39 +816,14 @@ public Client client(Node node, String clusterName, Random random) {
}
}

class RandomClientFactory extends ClientFactory {

private SettingsSource settingsSource;

RandomClientFactory(SettingsSource settingsSource) {
this.settingsSource = settingsSource;
}

@Override
public Client client(Node node, String clusterName, Random random) {
double nextDouble = random.nextDouble();
if (nextDouble < transportClientRatio) {
if (logger.isDebugEnabled()) {
logger.debug("Using transport client for node [{}] sniff: [{}]", node.settings().get("name"), false);
}
/* no sniff client for now - doesn't work will all tests since it might throw NoNodeAvailableException if nodes are shut down.
* we first need support of transportClientRatio as annotations or so
*/
return TransportClientFactory.noSniff(settingsSource.transportClient()).client(node, clusterName, random);
} else {
return node.client();
}
}
}

@Override
public synchronized void beforeTest(Random random, double transportClientRatio) throws IOException {
super.beforeTest(random, transportClientRatio);
reset(true);
}

private synchronized void reset(boolean wipeData) throws IOException {
resetClients(); /* reset all clients - each test gets its own client based on the Random instance created above. */
randomlyResetClients();
if (wipeData) {
wipeDataDirectories();
}
Expand Down Expand Up @@ -936,13 +903,16 @@ private synchronized void reset(boolean wipeData) throws IOException {
@Override
public synchronized void afterTest() throws IOException {
wipeDataDirectories();
resetClients(); /* reset all clients - each test gets its own client based on the Random instance created above. */
randomlyResetClients(); /* reset all clients - each test gets its own client based on the Random instance created above. */
}

private void resetClients() throws IOException {
final Collection<NodeAndClient> nodesAndClients = nodes.values();
for (NodeAndClient nodeAndClient : nodesAndClients) {
nodeAndClient.resetClient();
private void randomlyResetClients() throws IOException {
// only reset the clients on nightly tests, it causes heavy load...
if (isNightly() && rarely(random)) {
final Collection<NodeAndClient> nodesAndClients = nodes.values();
for (NodeAndClient nodeAndClient : nodesAndClients) {
nodeAndClient.resetClient();
}
}
}

Expand Down

0 comments on commit 8d30e98

Please sign in to comment.