Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Perform the port checks in parallel #4463

Merged
merged 3 commits into from
Oct 13, 2021
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -6,10 +6,26 @@

import java.time.Duration;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

public abstract class AbstractWaitStrategy implements WaitStrategy {

static final ExecutorService EXECUTOR = Executors.newCachedThreadPool(new ThreadFactory() {
Copy link
Member

Choose a reason for hiding this comment

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

Could we use Guava's ThreadFactoryBuilder here?

Copy link
Member Author

Choose a reason for hiding this comment

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

I would rather reduce Guava usage, not increase it 😅 I could change it, but this is some "static" code, and it is not huge, so... WDYT? :)

Copy link
Member

Choose a reason for hiding this comment

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

I'd say not use Guava and use a real class WaitStrategyDaemonThreadFactory or something.

Copy link
Member Author

Choose a reason for hiding this comment

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

what's wrong with a non-reusable 10 lines anonymous class close to the usage? 😅

Copy link
Member

Choose a reason for hiding this comment

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

That I am confronted with lower-level implementation detail at the head of the file instead of a class name acting as an abstraction. But these are differences in our style, which we might never overcome 😅

Copy link
Member

Choose a reason for hiding this comment

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

I don't mind; I'd rather see the removal of Guava as a dependency too, but it's there.

Perhaps we could extract this to our own class, a bit like @kiview suggests? We have the same code over in Startables.

Copy link
Member

Choose a reason for hiding this comment

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

This has dragged on a bit, so: let’s leave it. A bit of repetition doesn’t do any harm. If we ever add a thread factory again, though, let’s make all occurrences use a shared class


private final AtomicLong COUNTER = new AtomicLong(0);

@Override
public Thread newThread(Runnable r) {
Thread thread = new Thread(r, "testcontainers-wait-" + COUNTER.getAndIncrement());
thread.setDaemon(true);
return thread;
}
});

private static final RateLimiter DOCKER_CLIENT_RATE_LIMITER = RateLimiterBuilder
.newBuilder()
.withRate(1, TimeUnit.SECONDS)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
package org.testcontainers.containers.wait.strategy;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.rnorth.ducttape.TimeoutException;
import org.rnorth.ducttape.unreliables.Unreliables;
import org.awaitility.Awaitility;
import org.testcontainers.containers.ContainerLaunchException;
import org.testcontainers.containers.wait.internal.ExternalPortListeningCheck;
import org.testcontainers.containers.wait.internal.InternalCommandPortListeningCheck;

import java.time.Duration;
import java.time.Instant;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.stream.Collectors;

/**
Expand All @@ -22,6 +29,7 @@
public class HostPortWaitStrategy extends AbstractWaitStrategy {

@Override
@SneakyThrows(InterruptedException.class)
protected void waitUntilReady() {
final Set<Integer> externalLivenessCheckPorts = getLivenessCheckPorts();
if (externalLivenessCheckPorts.isEmpty()) {
Expand All @@ -31,7 +39,6 @@ protected void waitUntilReady() {
return;
}

@SuppressWarnings("unchecked")
List<Integer> exposedPorts = waitStrategyTarget.getExposedPorts();

final Set<Integer> internalPorts = getInternalPorts(externalLivenessCheckPorts, exposedPorts);
Expand All @@ -41,10 +48,43 @@ protected void waitUntilReady() {
Callable<Boolean> externalCheck = new ExternalPortListeningCheck(waitStrategyTarget, externalLivenessCheckPorts);

try {
Unreliables.retryUntilTrue((int) startupTimeout.getSeconds(), TimeUnit.SECONDS,
() -> getRateLimiter().getWhenReady(() -> internalCheck.call() && externalCheck.call()));
List<Future<Boolean>> futures = EXECUTOR.invokeAll(Arrays.asList(
Copy link
Member

Choose a reason for hiding this comment

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

👍

// Blocking
() -> {
Instant now = Instant.now();
Boolean result = internalCheck.call();
log.debug(
"Internal port check {} for {} in {}",
Boolean.TRUE.equals(result) ? "passed" : "failed",
internalPorts,
Duration.between(now, Instant.now())
);
return result;
},
// Polling
() -> {
Instant now = Instant.now();
Awaitility.await()
.pollInSameThread()
.pollInterval(Duration.ofMillis(100))
.pollDelay(Duration.ZERO)
.forever()
.until(externalCheck);

} catch (TimeoutException e) {
log.debug(
"External port check passed for {} mapped as {} in {}",
internalPorts,
externalLivenessCheckPorts,
Duration.between(now, Instant.now())
);
return true;
}
), startupTimeout.getSeconds(), TimeUnit.SECONDS);

for (Future<Boolean> future : futures) {
future.get(0, TimeUnit.SECONDS);
}
} catch (CancellationException | ExecutionException | TimeoutException e) {
throw new ContainerLaunchException("Timed out waiting for container port to open (" +
waitStrategyTarget.getHost() +
" ports: " +
Expand Down