-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
/** | ||
|
@@ -22,6 +29,7 @@ | |
public class HostPortWaitStrategy extends AbstractWaitStrategy { | ||
|
||
@Override | ||
@SneakyThrows(InterruptedException.class) | ||
protected void waitUntilReady() { | ||
final Set<Integer> externalLivenessCheckPorts = getLivenessCheckPorts(); | ||
if (externalLivenessCheckPorts.isEmpty()) { | ||
|
@@ -31,7 +39,6 @@ protected void waitUntilReady() { | |
return; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
List<Integer> exposedPorts = waitStrategyTarget.getExposedPorts(); | ||
|
||
final Set<Integer> internalPorts = getInternalPorts(externalLivenessCheckPorts, exposedPorts); | ||
|
@@ -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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: " + | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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? :)
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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? 😅
There was a problem hiding this comment.
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 😅
There was a problem hiding this comment.
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
.There was a problem hiding this comment.
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