Skip to content

Commit

Permalink
Fix sporadic Elasticsearch test failures (#444)
Browse files Browse the repository at this point in the history
  • Loading branch information
trask authored May 27, 2020
1 parent d4a14f6 commit 5c7e321
Show file tree
Hide file tree
Showing 18 changed files with 128 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ class Elasticsearch6RestClientTest extends AgentTestRunner {
RestClient client

def setupSpec() {
withRetryOnBindException({
setupSpecUnderRetry()
})
}

def setupSpecUnderRetry() {
httpPort = PortUtils.randomOpenPort()
tcpPort = PortUtils.randomOpenPort()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ class Elasticsearch5RestClientTest extends AgentTestRunner {
static RestClient client

def setupSpec() {
withRetryOnBindException({
setupSpecUnderRetry()
})
}

def setupSpecUnderRetry() {
httpPort = PortUtils.randomOpenPort()
tcpPort = PortUtils.randomOpenPort()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ class Elasticsearch6RestClientTest extends AgentTestRunner {
RestClient client

def setupSpec() {
withRetryOnBindException({
setupSpecUnderRetry()
})
}

def setupSpecUnderRetry() {
httpPort = PortUtils.randomOpenPort()
tcpPort = PortUtils.randomOpenPort()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ class Elasticsearch6RestClientTest extends AgentTestRunner {
RestClient client

def setupSpec() {
withRetryOnBindException({
setupSpecUnderRetry()
})
}

def setupSpecUnderRetry() {
httpPort = PortUtils.randomOpenPort()
tcpPort = PortUtils.randomOpenPort()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ class Elasticsearch2NodeClientTest extends AgentTestRunner {
def client = testNode.client()

def setupSpec() {
withRetryOnBindException({
setupSpecUnderRetry()
})
}

def setupSpecUnderRetry() {
httpPort = PortUtils.randomOpenPort()
tcpPort = PortUtils.randomOpenPort()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ class Elasticsearch2TransportClientTest extends AgentTestRunner {
TransportClient client

def setupSpec() {
withRetryOnBindException({
setupSpecUnderRetry()
})
}

def setupSpecUnderRetry() {
httpPort = PortUtils.randomOpenPort()
tcpPort = PortUtils.randomOpenPort()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ class Elasticsearch2SpringTemplateTest extends AgentTestRunner {
ElasticsearchTemplate template

def setupSpec() {
withRetryOnBindException({
setupSpecUnderRetry()
})
}

def setupSpecUnderRetry() {
httpPort = PortUtils.randomOpenPort()
tcpPort = PortUtils.randomOpenPort()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ class Elasticsearch2NodeClientTest extends AgentTestRunner {
def client = testNode.client()

def setupSpec() {
withRetryOnBindException({
setupSpecUnderRetry()
})
}

def setupSpecUnderRetry() {
httpPort = PortUtils.randomOpenPort()
tcpPort = PortUtils.randomOpenPort()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ class Elasticsearch2TransportClientTest extends AgentTestRunner {
TransportClient client

def setupSpec() {
withRetryOnBindException({
setupSpecUnderRetry()
})
}

def setupSpecUnderRetry() {
httpPort = PortUtils.randomOpenPort()
tcpPort = PortUtils.randomOpenPort()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ class Elasticsearch2SpringTemplateTest extends AgentTestRunner {
ElasticsearchTemplate template

def setupSpec() {
withRetryOnBindException({
setupSpecUnderRetry()
})
}

def setupSpecUnderRetry() {
httpPort = PortUtils.randomOpenPort()
tcpPort = PortUtils.randomOpenPort()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ class Elasticsearch5NodeClientTest extends AgentTestRunner {
def client = testNode.client()

def setupSpec() {
withRetryOnBindException({
setupSpecUnderRetry()
})
}

def setupSpecUnderRetry() {
httpPort = PortUtils.randomOpenPort()
tcpPort = PortUtils.randomOpenPort()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ class Elasticsearch5TransportClientTest extends AgentTestRunner {
TransportClient client

def setupSpec() {
withRetryOnBindException({
setupSpecUnderRetry()
})
}

def setupSpecUnderRetry() {
httpPort = PortUtils.randomOpenPort()
tcpPort = PortUtils.randomOpenPort()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ class Elasticsearch53NodeClientTest extends AgentTestRunner {
def client = testNode.client()

def setupSpec() {
withRetryOnBindException({
setupSpecUnderRetry()
})
}

def setupSpecUnderRetry() {
httpPort = PortUtils.randomOpenPort()
tcpPort = PortUtils.randomOpenPort()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ class Elasticsearch53TransportClientTest extends AgentTestRunner {
TransportClient client

def setupSpec() {
withRetryOnBindException({
setupSpecUnderRetry()
})
}

def setupSpecUnderRetry() {
httpPort = PortUtils.randomOpenPort()
tcpPort = PortUtils.randomOpenPort()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ class Elasticsearch53SpringTemplateTest extends AgentTestRunner {
ElasticsearchTemplate template

def setupSpec() {
withRetryOnBindException({
setupSpecUnderRetry()
})
}

def setupSpecUnderRetry() {
httpPort = PortUtils.randomOpenPort()
tcpPort = PortUtils.randomOpenPort()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ class Elasticsearch6NodeClientTest extends AgentTestRunner {
def client = testNode.client()

def setupSpec() {
withRetryOnBindException({
setupSpecUnderRetry()
})
}

def setupSpecUnderRetry() {
httpPort = PortUtils.randomOpenPort()
tcpPort = PortUtils.randomOpenPort()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ class Elasticsearch6TransportClientTest extends AgentTestRunner {
TransportClient client

def setupSpec() {
withRetryOnBindException({
setupSpecUnderRetry()
})
}

def setupSpecUnderRetry() {
httpPort = PortUtils.randomOpenPort()
tcpPort = PortUtils.randomOpenPort()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import ch.qos.logback.classic.Logger;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.base.Throwables;
import com.google.common.collect.Sets;
import groovy.lang.Closure;
import groovy.lang.DelegatesTo;
Expand All @@ -35,6 +36,7 @@
import io.opentelemetry.trace.Tracer;
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.Instrumentation;
import java.net.BindException;
import java.util.ArrayList;
import java.util.List;
import java.util.ServiceLoader;
Expand Down Expand Up @@ -177,6 +179,30 @@ public void cleanUpAfterTests() {
TEST_LISTENER.deactivateTest(this);
}

/**
* This is used by setupSpec() methods to auto-retry setup that depends on finding and then using
* an available free port, because that kind of setup can fail sporadically with
* "java.net.BindException: Address already in use" if the available port gets re-used between
* when we find the available port and when we use it.
*
* @param closure the groovy closure to run with retry
*/
public static void withRetryOnBindException(final Closure<?> closure) {
withRetryOnBindException(closure, 3);
}

private static void withRetryOnBindException(final Closure<?> closure, final int numRetries) {
try {
closure.call();
} catch (final Throwable t) {
if (numRetries == 0 || !(Throwables.getRootCause(t) instanceof BindException)) {
throw t;
}
log.debug("retrying due to bind exception: {}", t.getMessage(), t);
withRetryOnBindException(closure, numRetries - 1);
}
}

@AfterClass
public static synchronized void agentCleanup() {
if (null != activeTransformer) {
Expand Down

0 comments on commit 5c7e321

Please sign in to comment.