diff --git a/docker-java-orchestration-core/pom.xml b/docker-java-orchestration-core/pom.xml index fbec743..c29160b 100644 --- a/docker-java-orchestration-core/pom.xml +++ b/docker-java-orchestration-core/pom.xml @@ -1,10 +1,22 @@ 4.0.0 + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + - com.alexecollins.docker + com.alexecollins.kghimire.docker docker-java-orchestration - 2.11.30-SNAPSHOT + 1.0.0 ../pom.xml @@ -42,12 +54,12 @@ - com.alexecollins.docker + com.alexecollins.kghimire.docker docker-java-orchestration-model ${project.version} - com.alexecollins.docker + com.alexecollins.kghimire.docker docker-java-orchestration-plugin-api ${project.version} diff --git a/docker-java-orchestration-core/src/main/java/com/alexecollins/docker/orchestration/DockerOrchestrator.java b/docker-java-orchestration-core/src/main/java/com/alexecollins/docker/orchestration/DockerOrchestrator.java index c6c2e5e..4f90ea5 100644 --- a/docker-java-orchestration-core/src/main/java/com/alexecollins/docker/orchestration/DockerOrchestrator.java +++ b/docker-java-orchestration-core/src/main/java/com/alexecollins/docker/orchestration/DockerOrchestrator.java @@ -36,7 +36,7 @@ import java.nio.file.Files; import java.util.*; import java.util.Map.Entry; -import java.util.concurrent.TimeUnit; +import java.util.concurrent.*; import java.util.zip.GZIPOutputStream; import static java.util.Arrays.asList; @@ -71,6 +71,7 @@ public boolean accept(File pathname) { private final DefinitionFilter definitionFilter; private final boolean permissionErrorTolerant; + private Set startedContainer = new HashSet<>(); /** * @deprecated Please use builder from now on. */ @@ -434,13 +435,7 @@ private boolean haveBuildFlag(BuildFlag flag) { return buildFlags.contains(flag); } - private void start(final Id id) { - if (id == null) { - throw new IllegalArgumentException("id is null"); - } - - logger.info("Starting " + id); - + private void build(final Id id, CountDownLatch countDownLatch){ try { if (!imageExists(id)) { logger.info("Image does not exist, so building it"); @@ -448,6 +443,28 @@ private void start(final Id id) { } } catch (DockerException e) { throw new OrchestrationException(e); + }finally { + countDownLatch.countDown(); + } + + } + + private void start(final Id id, CountDownLatch countDownLatch) { + if (id == null) { + countDownLatch.countDown(); + startedContainer.add(id.toString()); + throw new IllegalArgumentException("id is null"); + } + + logger.info("Starting " + id); + + while (!isAllContainerStarted(id)) { + logger.info("Sleeping for 1 second for dependent container to start"); + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } } try { @@ -475,15 +492,15 @@ private void start(final Id id) { for (Plugin plugin : plugins) { plugin.started(id, conf); } - sleep(id); - healthCheck(id); } catch (Exception e) { logger.error("Error starting container with id " + id + ": " + e.getMessage()); throw new OrchestrationException(e); } finally { + startedContainer.add(id.toString()); + countDownLatch.countDown(); final Container container = findContainer(id); if (container == null) { logger.error("Could not find container with id {}. No logs can be obtained", id); @@ -872,13 +889,67 @@ public void validate() { throw new OrchestrationException(innerException); } + /* + To start without new thread creation + */ + public void startAsync() { + for (Id id : ids()) { + if (!inclusive(id)) { + continue; + } + build(id, new CountDownLatch(1)); + start(id, new CountDownLatch(1)); + } + } + public void start() { + this.start(1); + } + + public void start(int threadCount) { + ExecutorService executorService = Executors.newFixedThreadPool(threadCount); + CountDownLatch countDownLatch = new CountDownLatch(ids().size()); for (Id id : ids()) { if (!inclusive(id)) { + countDownLatch.countDown(); continue; } - start(id); + Runnable task = ()-> build(id, countDownLatch); + executorService.submit(task); + } + try { + countDownLatch.await(); + } catch (InterruptedException e) { + logger.error("InterruptedException occurred", e); } + + CountDownLatch countDownLatch1 = new CountDownLatch(ids().size()); + + for (Id id : ids()) { + if (!inclusive(id)) { + countDownLatch1.countDown(); + continue; + } + Runnable task = ()-> start(id, countDownLatch1); + executorService.submit(task); + } + + try { + countDownLatch1.await(); + } catch (InterruptedException e) { + logger.error("InterruptedException occurred", e); + } + } + + private boolean isAllContainerStarted(Id id) { + Conf conf = conf(id); + List depends = conf.getDepends(); + for (Depend depend : depends) { + if (!startedContainer.contains(depend.getId().toString())) { + return false; + } + } + return true; } public void copy(String resource, String hostpath) { diff --git a/docker-java-orchestration-core/src/test/java/com/alexecollins/docker/orchestration/DockerOrchestratorTest.java b/docker-java-orchestration-core/src/test/java/com/alexecollins/docker/orchestration/DockerOrchestratorTest.java index 2b1f936..5c3a027 100644 --- a/docker-java-orchestration-core/src/test/java/com/alexecollins/docker/orchestration/DockerOrchestratorTest.java +++ b/docker-java-orchestration-core/src/test/java/com/alexecollins/docker/orchestration/DockerOrchestratorTest.java @@ -590,7 +590,7 @@ public void testWaitForLineFailEndOfInput() { mockLogContainerCmd("Bar"); try { - testObj.start(); + testObj.startAsync(); fail(); } catch (OrchestrationException e) { assertThat(e.getMessage(), endsWith(String.format("%s's log ended before [\"^Foo$\"] appeared in output", idMock))); @@ -621,7 +621,7 @@ public void timeOutWaitingForLogs() { mockLogContainerCmd("Bar"); try { - testObj.start(); + testObj.startAsync(); fail(); } catch (OrchestrationException e) { assertThat(e.getMessage(), endsWith(String.format("timeout after 0 while waiting for \"%s\" in %s's logs", firstLogPattern.getPattern(), idMock))); diff --git a/docker-java-orchestration-model/pom.xml b/docker-java-orchestration-model/pom.xml index e9de937..fe0d0e8 100644 --- a/docker-java-orchestration-model/pom.xml +++ b/docker-java-orchestration-model/pom.xml @@ -2,8 +2,8 @@ docker-java-orchestration - com.alexecollins.docker - 2.11.30-SNAPSHOT + com.alexecollins.kghimire.docker + 1.0.0 4.0.0 diff --git a/docker-java-orchestration-model/src/main/java/com/alexecollins/docker/orchestration/model/Conf.java b/docker-java-orchestration-model/src/main/java/com/alexecollins/docker/orchestration/model/Conf.java index 77a0fef..ad5790b 100644 --- a/docker-java-orchestration-model/src/main/java/com/alexecollins/docker/orchestration/model/Conf.java +++ b/docker-java-orchestration-model/src/main/java/com/alexecollins/docker/orchestration/model/Conf.java @@ -18,6 +18,7 @@ public class Conf { */ private ContainerConf container = new ContainerConf(); private List links = new ArrayList<>(); + private List depends = new ArrayList<>(); private Packaging packaging = new Packaging(); /** * E.g. "8080" or "8080 8080" where the former is the exposed port and the latter the container port. diff --git a/docker-java-orchestration-model/src/main/java/com/alexecollins/docker/orchestration/model/Depend.java b/docker-java-orchestration-model/src/main/java/com/alexecollins/docker/orchestration/model/Depend.java new file mode 100644 index 0000000..d87056f --- /dev/null +++ b/docker-java-orchestration-model/src/main/java/com/alexecollins/docker/orchestration/model/Depend.java @@ -0,0 +1,38 @@ +package com.alexecollins.docker.orchestration.model; + +public class Depend { + private final String value; + + public Depend(String value) { + this.value = value; + } + + public Id getId() { + return new Id(value.replaceFirst(":.*", "")); + } + + public String getAlias() { + return value.contains(":") ? value.replaceFirst(".*:", "") : value; + } + + @Override + public String toString() { + return value; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Depend id = (Depend) o; + + return value.equals(id.value); + + } + + @Override + public int hashCode() { + return value.hashCode(); + } +} diff --git a/docker-java-orchestration-plugin-api/pom.xml b/docker-java-orchestration-plugin-api/pom.xml index 5d52c48..b8cda73 100644 --- a/docker-java-orchestration-plugin-api/pom.xml +++ b/docker-java-orchestration-plugin-api/pom.xml @@ -2,9 +2,9 @@ 4.0.0 - com.alexecollins.docker + com.alexecollins.kghimire.docker docker-java-orchestration - 2.11.30-SNAPSHOT + 1.0.0 ../pom.xml @@ -14,7 +14,7 @@ - com.alexecollins.docker + com.alexecollins.kghimire.docker docker-java-orchestration-model ${project.version} diff --git a/docker-java-orchestration-plugin-boot2docker/pom.xml b/docker-java-orchestration-plugin-boot2docker/pom.xml index bd00abc..37cf270 100644 --- a/docker-java-orchestration-plugin-boot2docker/pom.xml +++ b/docker-java-orchestration-plugin-boot2docker/pom.xml @@ -2,9 +2,9 @@ 4.0.0 - com.alexecollins.docker + com.alexecollins.kghimire.docker docker-java-orchestration - 2.11.30-SNAPSHOT + 1.0.0 ../pom.xml @@ -14,7 +14,7 @@ - com.alexecollins.docker + com.alexecollins.kghimire.docker docker-java-orchestration-plugin-api ${project.version} diff --git a/pom.xml b/pom.xml index 5fb9f62..a0c95ec 100644 --- a/pom.xml +++ b/pom.xml @@ -7,9 +7,9 @@ 7 - com.alexecollins.docker + com.alexecollins.kghimire.docker docker-java-orchestration - 2.11.30-SNAPSHOT + 1.0.0 pom Docker Java Orchestration