From bfcb33ce7d3284d8a9e08bebcea1d4db992de7ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roland=20Hu=C3=9F?= Date: Fri, 14 Oct 2016 15:15:54 +0200 Subject: [PATCH] Filter out non aliases in network links for dependencies #586 External container ids should not be wait for. --- .../io/fabric8/maven/docker/StartMojo.java | 56 ++++++++++++++----- .../docker/config/ImageConfiguration.java | 6 +- .../io/fabric8/maven/docker/util/EnvUtil.java | 2 +- 3 files changed, 46 insertions(+), 18 deletions(-) diff --git a/src/main/java/io/fabric8/maven/docker/StartMojo.java b/src/main/java/io/fabric8/maven/docker/StartMojo.java index bee4e161f..789a8d84d 100644 --- a/src/main/java/io/fabric8/maven/docker/StartMojo.java +++ b/src/main/java/io/fabric8/maven/docker/StartMojo.java @@ -116,8 +116,10 @@ public synchronized void executeInternal(final ServiceHub hub) throws DockerAcce final PomLabel pomLabel = getPomLabel(); try { - + // Queu of images to start final Queue imagesWaitingToStart = new ArrayDeque<>(); + // All aliases which are provided in the image configuration: + final Set imageAliases = new HashSet<>(); for (StartOrderResolver.Resolvable resolvable : runService.getImagesConfigsInOrder(queryService, getResolvedImages())) { final ImageConfiguration imageConfig = (ImageConfiguration) resolvable; @@ -127,7 +129,7 @@ public synchronized void executeInternal(final ServiceHub hub) throws DockerAcce String imageName = imageConfig.getName(); checkImageWithAutoPull(hub, imageName, - getConfiguredRegistry(imageConfig, pullRegistry), imageConfig.getBuildConfiguration() == null); + getConfiguredRegistry(imageConfig, pullRegistry), imageConfig.getBuildConfiguration() == null); RunImageConfiguration runConfig = imageConfig.getRunConfiguration(); NetworkConfig config = runConfig.getNetworkingConfig(); @@ -135,23 +137,24 @@ public synchronized void executeInternal(final ServiceHub hub) throws DockerAcce runService.createCustomNetworkIfNotExistant(config.getCustomNetwork()); } imagesWaitingToStart.add(imageConfig); + if (imageConfig.getAlias() != null) { + imageAliases.add(imageConfig.getAlias()); + } } final Set startedContainers = new HashSet<>(); - final ExecutorService executorService; - if (startParallel) { - executorService = Executors.newCachedThreadPool(); - } else { - executorService = MoreExecutors.newDirectExecutorService(); - } + final ExecutorService executorService = getExecutorService(); final ExecutorCompletionService startingContainers = new ExecutorCompletionService<>(executorService); while (!imagesWaitingToStart.isEmpty()) { final List startableImages = new ArrayList<>(); + // Check for all images which can be already started for (ImageConfiguration imageWaitingToStart : imagesWaitingToStart) { - if (startedContainers.containsAll(imageWaitingToStart.getDependencies())) { + List allDependencies = imageWaitingToStart.getDependencies(); + List aliasDependencies = filterOutNonAliases(imageAliases, allDependencies); + if (startedContainers.containsAll(aliasDependencies)) { startableImages.add(imageWaitingToStart); } } @@ -188,18 +191,23 @@ public StartedContainerImage call() throws Exception { final Future imageStartResult = startingContainers.take(); try { final StartedContainerImage startedContainerImage = imageStartResult.get(); + final String containerId = startedContainerImage.containerId; final ImageConfiguration imageConfig = startedContainerImage.imageConfig; - final RunImageConfiguration runConfig = imageConfig.getRunConfiguration(); - final PortMapping portMapping = runService.getPortMapping(runConfig, projProperties); + // Add the alias to the list of started containers when it is set. When it's + // not set it cant be used in the dependency resolution anyway, so we are ignoring + // it here. + if (imageConfig.getAlias() != null) { + startedContainers.add(imageConfig.getAlias()); + } - startedContainers.add(startedContainerImage.imageConfig.getAlias()); + final RunImageConfiguration runConfig = imageConfig.getRunConfiguration(); + final PortMapping portMapping = runService.getPortMapping(runConfig, projProperties); + // Write out properties and expose them as project properties, too portMappingPropertyWriteHelper.add(portMapping, runConfig.getPortPropertyFile()); - // Expose container info as properties exposeContainerProps(hub.getQueryService(), containerId, imageConfig); - } catch (ExecutionException e) { try { if (e.getCause() instanceof RuntimeException) { @@ -253,6 +261,26 @@ public StartedContainerImage call() throws Exception { } } + private List filterOutNonAliases(Set imageAliases, List dependencies) { + List ret = new ArrayList<>(); + for (String alias : dependencies) { + if (imageAliases.contains(alias)) { + ret.add(alias); + } + } + return ret; + } + + private ExecutorService getExecutorService() { + final ExecutorService executorService; + if (startParallel) { + executorService = Executors.newCachedThreadPool(); + } else { + executorService = MoreExecutors.newDirectExecutorService(); + } + return executorService; + } + // ======================================================================================================== private void waitIfRequested(ServiceHub hub, ImageConfiguration imageConfig, diff --git a/src/main/java/io/fabric8/maven/docker/config/ImageConfiguration.java b/src/main/java/io/fabric8/maven/docker/config/ImageConfiguration.java index 5ecc887c4..a782e653d 100644 --- a/src/main/java/io/fabric8/maven/docker/config/ImageConfiguration.java +++ b/src/main/java/io/fabric8/maven/docker/config/ImageConfiguration.java @@ -124,10 +124,10 @@ private void addContainerNetwork(RunImageConfiguration runConfig, List r } private void addDependsOn(RunImageConfiguration runConfig, List ret) { - // Only used in required networks. + // Only used in custom networks. if (runConfig.getDependsOn() != null && runConfig.getNetworkingConfig().isCustomNetwork()) { - for (String[] link : EnvUtil.splitOnLastColon(runConfig.getDependsOn())) { - ret.add(link[0]); + for (String link : runConfig.getDependsOn()) { + ret.add(link); } } } diff --git a/src/main/java/io/fabric8/maven/docker/util/EnvUtil.java b/src/main/java/io/fabric8/maven/docker/util/EnvUtil.java index 040868e43..532039bbb 100644 --- a/src/main/java/io/fabric8/maven/docker/util/EnvUtil.java +++ b/src/main/java/io/fabric8/maven/docker/util/EnvUtil.java @@ -19,7 +19,7 @@ import static java.util.concurrent.TimeUnit.*; /** - * Utility class for various (loosely) environment related tasks. + * Utility class for various (loosely related) environment related tasks. * * @author roland * @since 04.04.14