diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/ProgressEventDispatcher.java b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/ProgressEventDispatcher.java index e7af9a2a20..f1aa4837f9 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/ProgressEventDispatcher.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/ProgressEventDispatcher.java @@ -147,7 +147,7 @@ public void dispatchProgress(long progressUnits) { private long decrementRemainingAllocationUnits(long units) { Preconditions.checkState(!closed); - if (remainingAllocationUnits > units) { + if (remainingAllocationUnits >= units) { remainingAllocationUnits -= units; return units; } diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/StepsRunner.java b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/StepsRunner.java index b4872c5ac1..29ee23650c 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/StepsRunner.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/StepsRunner.java @@ -38,7 +38,6 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Future; -import java.util.function.Supplier; import java.util.stream.Collectors; import javax.annotation.Nullable; @@ -102,10 +101,7 @@ public static StepsRunner begin(BuildConfiguration buildConfiguration) { private final List stepsToRun = new ArrayList<>(); @Nullable private String rootProgressDescription; - private Supplier childProgressDispatcherFactorySupplier = - () -> { - throw new IllegalStateException("root progress dispatcher uninstantiated"); - }; + @Nullable private ProgressEventDispatcher rootProgressDispatcher; private StepsRunner( ListeningExecutorService executorService, BuildConfiguration buildConfiguration) { @@ -114,67 +110,87 @@ private StepsRunner( } private void retrieveTargetRegistryCredentials() { + ProgressEventDispatcher.Factory childProgressDispatcherFactory = + Verify.verifyNotNull(rootProgressDispatcher).newChildProducer(); + results.targetRegistryCredentials = executorService.submit( RetrieveRegistryCredentialsStep.forTargetImage( - buildConfiguration, childProgressDispatcherFactorySupplier.get())); + buildConfiguration, childProgressDispatcherFactory)); } private void authenticatePush() { + ProgressEventDispatcher.Factory childProgressDispatcherFactory = + Verify.verifyNotNull(rootProgressDispatcher).newChildProducer(); + results.pushAuthorization = executorService.submit( () -> new AuthenticatePushStep( buildConfiguration, - childProgressDispatcherFactorySupplier.get(), + childProgressDispatcherFactory, results.targetRegistryCredentials.get()) .call()); } private void pullBaseImage() { + ProgressEventDispatcher.Factory childProgressDispatcherFactory = + Verify.verifyNotNull(rootProgressDispatcher).newChildProducer(); + results.baseImageAndAuth = executorService.submit( - new PullBaseImageStep( - buildConfiguration, childProgressDispatcherFactorySupplier.get())); + new PullBaseImageStep(buildConfiguration, childProgressDispatcherFactory)); } private void pullAndCacheBaseImageLayers() { + ProgressEventDispatcher.Factory childProgressDispatcherFactory = + Verify.verifyNotNull(rootProgressDispatcher).newChildProducer(); + results.baseImageLayers = executorService.submit( () -> scheduleCallables( PullAndCacheBaseImageLayersStep.makeList( buildConfiguration, - childProgressDispatcherFactorySupplier.get(), + childProgressDispatcherFactory, results.baseImageAndAuth.get()))); } private void pushBaseImageLayers() { + ProgressEventDispatcher.Factory childProgressDispatcherFactory = + Verify.verifyNotNull(rootProgressDispatcher).newChildProducer(); + results.baseImageLayerPushResults = executorService.submit( () -> scheduleCallables( PushLayerStep.makeList( buildConfiguration, - childProgressDispatcherFactorySupplier.get(), + childProgressDispatcherFactory, results.pushAuthorization.get(), results.baseImageLayers.get()))); } private void buildAndCacheApplicationLayers() { + ProgressEventDispatcher.Factory childProgressDispatcherFactory = + Verify.verifyNotNull(rootProgressDispatcher).newChildProducer(); + results.applicationLayers = scheduleCallables( BuildAndCacheApplicationLayerStep.makeList( - buildConfiguration, childProgressDispatcherFactorySupplier.get())); + buildConfiguration, childProgressDispatcherFactory)); } private void buildImage() { + ProgressEventDispatcher.Factory childProgressDispatcherFactory = + Verify.verifyNotNull(rootProgressDispatcher).newChildProducer(); + results.builtImage = executorService.submit( () -> new BuildImageStep( buildConfiguration, - childProgressDispatcherFactorySupplier.get(), + childProgressDispatcherFactory, results.baseImageAndAuth.get().getImage(), realizeFutures(results.baseImageLayers.get()), realizeFutures(Verify.verifyNotNull(results.applicationLayers))) @@ -182,30 +198,39 @@ private void buildImage() { } private void pushContainerConfiguration() { + ProgressEventDispatcher.Factory childProgressDispatcherFactory = + Verify.verifyNotNull(rootProgressDispatcher).newChildProducer(); + results.containerConfigurationPushResult = executorService.submit( () -> new PushContainerConfigurationStep( buildConfiguration, - childProgressDispatcherFactorySupplier.get(), + childProgressDispatcherFactory, results.pushAuthorization.get(), results.builtImage.get()) .call()); } private void pushApplicationLayers() { + ProgressEventDispatcher.Factory childProgressDispatcherFactory = + Verify.verifyNotNull(rootProgressDispatcher).newChildProducer(); + results.applicationLayerPushResults = executorService.submit( () -> scheduleCallables( PushLayerStep.makeList( buildConfiguration, - childProgressDispatcherFactorySupplier.get(), + childProgressDispatcherFactory, results.pushAuthorization.get(), Verify.verifyNotNull(results.applicationLayers)))); } private void pushImage() { + ProgressEventDispatcher.Factory childProgressDispatcherFactory = + Verify.verifyNotNull(rootProgressDispatcher).newChildProducer(); + results.buildResult = executorService.submit( () -> { @@ -215,7 +240,7 @@ private void pushImage() { return new PushImageStep( executorService, buildConfiguration, - childProgressDispatcherFactorySupplier.get(), + childProgressDispatcherFactory, results.pushAuthorization.get(), results.containerConfigurationPushResult.get(), results.builtImage.get()) @@ -224,24 +249,30 @@ private void pushImage() { } private void loadDocker(DockerClient dockerClient) { + ProgressEventDispatcher.Factory childProgressDispatcherFactory = + Verify.verifyNotNull(rootProgressDispatcher).newChildProducer(); + results.buildResult = executorService.submit( () -> new LoadDockerStep( buildConfiguration, - childProgressDispatcherFactorySupplier.get(), + childProgressDispatcherFactory, dockerClient, results.builtImage.get()) .call()); } private void writeTarFile(Path outputPath) { + ProgressEventDispatcher.Factory childProgressDispatcherFactory = + Verify.verifyNotNull(rootProgressDispatcher).newChildProducer(); + results.buildResult = executorService.submit( () -> new WriteTarFileStep( buildConfiguration, - childProgressDispatcherFactorySupplier.get(), + childProgressDispatcherFactory, outputPath, results.builtImage.get()) .call()); @@ -253,7 +284,8 @@ public BuildResult run() throws ExecutionException, InterruptedException { try (ProgressEventDispatcher progressEventDispatcher = ProgressEventDispatcher.newRoot( buildConfiguration.getEventHandlers(), rootProgressDescription, stepsToRun.size())) { - childProgressDispatcherFactorySupplier = progressEventDispatcher::newChildProducer; + rootProgressDispatcher = progressEventDispatcher; + stepsToRun.forEach(Runnable::run); return results.buildResult.get();