use of com.google.cloud.tools.jib.event.EventHandlers in project jib by GoogleContainerTools.
the class ProgressEventTest method testSmoke.
@Test
public void testSmoke() {
Consumer<ProgressEvent> progressEventConsumer = progressEvent -> {
Allocation allocation = progressEvent.getAllocation();
long units = progressEvent.getUnits();
updateCompletionMap(allocation, units);
};
EventHandlers eventHandlers = makeEventHandlers(progressEventConsumer);
eventHandlers.dispatch(new ProgressEvent(AllocationTree.child1Child, 50));
Assert.assertEquals(1, allocationCompletionMap.size());
Assert.assertEquals(50, allocationCompletionMap.get(AllocationTree.child1Child).longValue());
eventHandlers.dispatch(new ProgressEvent(AllocationTree.child1Child, 50));
Assert.assertEquals(3, allocationCompletionMap.size());
Assert.assertEquals(100, allocationCompletionMap.get(AllocationTree.child1Child).longValue());
Assert.assertEquals(1, allocationCompletionMap.get(AllocationTree.child1).longValue());
Assert.assertEquals(1, allocationCompletionMap.get(AllocationTree.root).longValue());
eventHandlers.dispatch(new ProgressEvent(AllocationTree.child2, 200));
Assert.assertEquals(4, allocationCompletionMap.size());
Assert.assertEquals(100, allocationCompletionMap.get(AllocationTree.child1Child).longValue());
Assert.assertEquals(1, allocationCompletionMap.get(AllocationTree.child1).longValue());
Assert.assertEquals(200, allocationCompletionMap.get(AllocationTree.child2).longValue());
Assert.assertEquals(2, allocationCompletionMap.get(AllocationTree.root).longValue());
}
use of com.google.cloud.tools.jib.event.EventHandlers in project jib by GoogleContainerTools.
the class ProgressEventHandlerTest method testAccept.
@Test
public void testAccept() throws ExecutionException, InterruptedException, IOException {
try (MultithreadedExecutor multithreadedExecutor = new MultithreadedExecutor()) {
DoubleAccumulator maxProgress = new DoubleAccumulator(Double::max, 0);
ProgressEventHandler progressEventHandler = new ProgressEventHandler(update -> maxProgress.accumulate(update.getProgress()));
EventHandlers eventHandlers = EventHandlers.builder().add(ProgressEvent.class, progressEventHandler).build();
// Adds root, child1, and child1Child.
multithreadedExecutor.invoke(() -> {
eventHandlers.dispatch(new ProgressEvent(AllocationTree.root, 0L));
return null;
});
multithreadedExecutor.invoke(() -> {
eventHandlers.dispatch(new ProgressEvent(AllocationTree.child1, 0L));
return null;
});
multithreadedExecutor.invoke(() -> {
eventHandlers.dispatch(new ProgressEvent(AllocationTree.child1Child, 0L));
return null;
});
Assert.assertEquals(0.0, maxProgress.get(), DOUBLE_ERROR_MARGIN);
// Adds 50 to child1Child and 100 to child2.
List<Callable<Void>> callables = new ArrayList<>(150);
callables.addAll(Collections.nCopies(50, () -> {
eventHandlers.dispatch(new ProgressEvent(AllocationTree.child1Child, 1L));
return null;
}));
callables.addAll(Collections.nCopies(100, () -> {
eventHandlers.dispatch(new ProgressEvent(AllocationTree.child2, 1L));
return null;
}));
multithreadedExecutor.invokeAll(callables);
Assert.assertEquals(1.0 / 2 / 100 * 50 + 1.0 / 2 / 200 * 100, maxProgress.get(), DOUBLE_ERROR_MARGIN);
// 0 progress doesn't do anything.
multithreadedExecutor.invokeAll(Collections.nCopies(100, () -> {
eventHandlers.dispatch(new ProgressEvent(AllocationTree.child1, 0L));
return null;
}));
Assert.assertEquals(1.0 / 2 / 100 * 50 + 1.0 / 2 / 200 * 100, maxProgress.get(), DOUBLE_ERROR_MARGIN);
// Adds 50 to child1Child and 100 to child2 to finish it up.
multithreadedExecutor.invokeAll(callables);
Assert.assertEquals(1.0, maxProgress.get(), DOUBLE_ERROR_MARGIN);
}
}
use of com.google.cloud.tools.jib.event.EventHandlers in project jib by GoogleContainerTools.
the class CheckManifestStep method call.
@Override
public Optional<ManifestAndDigest<ManifestTemplate>> call() throws IOException, RegistryException {
DescriptorDigest manifestDigest = Digests.computeJsonDigest(manifestTemplate);
EventHandlers eventHandlers = buildContext.getEventHandlers();
try (TimerEventDispatcher ignored = new TimerEventDispatcher(eventHandlers, DESCRIPTION);
ProgressEventDispatcher ignored2 = progressEventDispatcherFactory.create("checking existence of manifest for " + manifestDigest, 1)) {
eventHandlers.dispatch(LogEvent.info("Checking existence of manifest for " + manifestDigest + "..."));
if (!JibSystemProperties.skipExistingImages()) {
eventHandlers.dispatch(LogEvent.info("Skipping manifest existence check; system property set to false"));
return Optional.empty();
}
return registryClient.checkManifest(manifestDigest.toString());
}
}
use of com.google.cloud.tools.jib.event.EventHandlers in project jib by GoogleContainerTools.
the class LoadDockerStep method call.
@Override
public BuildResult call() throws InterruptedException, IOException {
EventHandlers eventHandlers = buildContext.getEventHandlers();
try (TimerEventDispatcher ignored = new TimerEventDispatcher(eventHandlers, "Loading to Docker daemon")) {
eventHandlers.dispatch(LogEvent.progress("Loading to Docker daemon..."));
ImageTarball imageTarball = new ImageTarball(builtImage, buildContext.getTargetImageConfiguration().getImage(), buildContext.getAllTargetImageTags());
// See https://github.com/GoogleContainerTools/jib/pull/1960#discussion_r321898390
try (ProgressEventDispatcher progressEventDispatcher = progressEventDispatcherFactory.create("loading to Docker daemon", imageTarball.getTotalLayerSize());
ThrottledAccumulatingConsumer throttledProgressReporter = new ThrottledAccumulatingConsumer(progressEventDispatcher::dispatchProgress)) {
// Load the image to docker daemon.
eventHandlers.dispatch(LogEvent.debug(dockerClient.load(imageTarball, throttledProgressReporter)));
return BuildResult.fromImage(builtImage, buildContext.getTargetFormat());
}
}
}
use of com.google.cloud.tools.jib.event.EventHandlers in project jib by GoogleContainerTools.
the class ObtainBaseImageLayerStep method call.
@Override
public PreparedLayer call() throws IOException, CacheCorruptedException, RegistryException {
EventHandlers eventHandlers = buildContext.getEventHandlers();
DescriptorDigest layerDigest = layer.getBlobDescriptor().getDigest();
try (ProgressEventDispatcher progressEventDispatcher = progressEventDispatcherFactory.create("checking base image layer " + layerDigest, 1);
TimerEventDispatcher ignored = new TimerEventDispatcher(eventHandlers, String.format(DESCRIPTION, layerDigest))) {
StateInTarget stateInTarget = blobExistenceChecker.check(layerDigest);
if (stateInTarget == StateInTarget.EXISTING) {
eventHandlers.dispatch(LogEvent.info("Skipping pull; BLOB already exists on target registry : " + layer.getBlobDescriptor()));
return new PreparedLayer.Builder(layer).setStateInTarget(stateInTarget).build();
}
Cache cache = buildContext.getBaseImageLayersCache();
// Checks if the layer already exists in the cache.
Optional<CachedLayer> optionalCachedLayer = cache.retrieve(layerDigest);
if (optionalCachedLayer.isPresent()) {
CachedLayer cachedLayer = optionalCachedLayer.get();
return new PreparedLayer.Builder(cachedLayer).setStateInTarget(stateInTarget).build();
} else if (buildContext.isOffline()) {
throw new IOException("Cannot run Jib in offline mode; local Jib cache for base image is missing image layer " + layerDigest + ". Rerun Jib in online mode with \"-Djib.alwaysCacheBaseImage=true\" to " + "re-download the base image layers.");
}
try (ThrottledProgressEventDispatcherWrapper progressEventDispatcherWrapper = new ThrottledProgressEventDispatcherWrapper(progressEventDispatcher.newChildProducer(), "pulling base image layer " + layerDigest)) {
CachedLayer cachedLayer = cache.writeCompressedLayer(Verify.verifyNotNull(registryClient).pullBlob(layerDigest, progressEventDispatcherWrapper::setProgressTarget, progressEventDispatcherWrapper::dispatchProgress));
return new PreparedLayer.Builder(cachedLayer).setStateInTarget(stateInTarget).build();
}
}
}
Aggregations