Skip to content

Commit

Permalink
fix(podman): fix blocked thread on container inspection request
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewazores committed Jun 7, 2023
1 parent a6c8d90 commit 5cfd8ea
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.net.SocketAddress;
import io.vertx.ext.web.client.HttpResponse;
import io.vertx.ext.web.client.WebClient;
import io.vertx.ext.web.codec.BodyCodec;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -168,53 +167,38 @@ private void queryContainers() {

containers.removeAll(removed);
removed.stream()
.map(this::convert)
.filter(Objects::nonNull)
.forEach(
spec ->
notifyAsyncTargetDiscovery(
EventKind.LOST, convert(spec)));
.forEach(spec -> notifyAsyncTargetDiscovery(EventKind.LOST, spec));

containers.addAll(added);
added.stream()
.map(this::convert)
.filter(Objects::nonNull)
.forEach(
spec ->
notifyAsyncTargetDiscovery(
EventKind.FOUND, convert(spec)));
.forEach(spec -> notifyAsyncTargetDiscovery(EventKind.FOUND, spec));
});
}

private void doPodmanListRequest(Consumer<List<ContainerSpec>> successHandler) {
URI requestPath = URI.create("http://d/v3.0.0/libpod/containers/json");
executor.submit(
() ->
webClient
.get()
.request(
HttpMethod.GET,
podmanSocket,
80,
"localhost",
requestPath.toString())
.addQueryParam(
"filters",
gson.toJson(Map.of("label", List.of(DISCOVERY_LABEL))))
.timeout(2_000L)
.as(BodyCodec.string())
.send(
ar -> {
if (ar.failed()) {
Throwable t = ar.cause();
logger.error("Podman API request failed", t);
return;
}
HttpResponse<String> response = ar.result();
successHandler.accept(
gson.fromJson(
response.body(),
new TypeToken<
List<ContainerSpec>>() {}));
}));
webClient
.get()
.request(HttpMethod.GET, podmanSocket, 80, "localhost", requestPath.toString())
.addQueryParam("filters", gson.toJson(Map.of("label", List.of(DISCOVERY_LABEL))))
.timeout(2_000L)
.as(BodyCodec.string())
.send(
ar -> {
if (ar.failed()) {
Throwable t = ar.cause();
logger.error("Podman API request failed", t);
return;
}
successHandler.accept(
gson.fromJson(
ar.result().body(),
new TypeToken<List<ContainerSpec>>() {}));
});
}

private CompletableFuture<ContainerDetails> doPodmanInspectRequest(ContainerSpec container) {
Expand All @@ -232,9 +216,6 @@ private CompletableFuture<ContainerDetails> doPodmanInspectRequest(ContainerSpec
80,
"localhost",
requestPath.toString())
.addQueryParam(
"filters",
gson.toJson(Map.of("label", List.of(JMX_PORT_LABEL))))
.timeout(2_000L)
.as(BodyCodec.string())
.send(
Expand All @@ -245,10 +226,10 @@ private CompletableFuture<ContainerDetails> doPodmanInspectRequest(ContainerSpec
result.completeExceptionally(t);
return;
}
HttpResponse<String> response = ar.result();
result.complete(
gson.fromJson(
response.body(), ContainerDetails.class));
ar.result().body(),
ContainerDetails.class));
});
});
return result;
Expand Down Expand Up @@ -280,6 +261,7 @@ private ServiceRef convert(ContainerSpec desc) {
.Config
.Hostname;
} catch (InterruptedException | TimeoutException | ExecutionException e) {
containers.remove(desc);
logger.warn(e);
return null;
}
Expand All @@ -306,6 +288,7 @@ private ServiceRef convert(ContainerSpec desc) {

return serviceRef;
} catch (NumberFormatException | URISyntaxException | MalformedURLException e) {
containers.remove(desc);
logger.warn(e);
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
import java.net.URI;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

Expand Down Expand Up @@ -111,7 +111,7 @@ public boolean isAvailable() {
private boolean testPodmanApi() {
CompletableFuture<Boolean> result = new CompletableFuture<>();
URI requestPath = URI.create("http://d/info");
Executors.newSingleThreadExecutor()
ForkJoinPool.commonPool()
.submit(
() -> {
webClient
Expand Down Expand Up @@ -147,7 +147,7 @@ private boolean testPodmanApi() {
public PodmanPlatformClient getPlatformClient() {
logger.info("Selected {} Strategy", getClass().getSimpleName());
return new PodmanPlatformClient(
Executors.newSingleThreadExecutor(),
ForkJoinPool.commonPool(),
webClient,
vertx,
getSocket(),
Expand Down

0 comments on commit 5cfd8ea

Please sign in to comment.