Search in sources :

Example 1 with ThrottledAccumulatingConsumer

use of com.google.cloud.tools.jib.event.progress.ThrottledAccumulatingConsumer in project jib by GoogleContainerTools.

the class PushBlobStep method call.

@Override
public BlobDescriptor call() throws IOException, RegistryException {
    EventHandlers eventHandlers = buildContext.getEventHandlers();
    DescriptorDigest blobDigest = blobDescriptor.getDigest();
    try (ProgressEventDispatcher progressEventDispatcher = progressEventDispatcherFactory.create("pushing blob " + blobDigest, blobDescriptor.getSize());
        TimerEventDispatcher ignored = new TimerEventDispatcher(eventHandlers, DESCRIPTION + blobDescriptor);
        ThrottledAccumulatingConsumer throttledProgressReporter = new ThrottledAccumulatingConsumer(progressEventDispatcher::dispatchProgress)) {
        // check if the BLOB is available
        if (!forcePush && registryClient.checkBlob(blobDigest).isPresent()) {
            eventHandlers.dispatch(LogEvent.info("Skipping push; BLOB already exists on target registry : " + blobDescriptor));
            return blobDescriptor;
        }
        // If base and target images are in the same registry, then use mount/from to try mounting the
        // BLOB from the base image repository to the target image repository and possibly avoid
        // having to push the BLOB. See
        // https://docs.docker.com/registry/spec/api/#cross-repository-blob-mount for details.
        String baseRegistry = buildContext.getBaseImageConfiguration().getImageRegistry();
        String baseRepository = buildContext.getBaseImageConfiguration().getImageRepository();
        String targetRegistry = buildContext.getTargetImageConfiguration().getImageRegistry();
        String sourceRepository = targetRegistry.equals(baseRegistry) ? baseRepository : null;
        registryClient.pushBlob(blobDigest, blob, sourceRepository, throttledProgressReporter);
        return blobDescriptor;
    }
}
Also used : ProgressEventDispatcher(com.google.cloud.tools.jib.builder.ProgressEventDispatcher) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) TimerEventDispatcher(com.google.cloud.tools.jib.builder.TimerEventDispatcher) ThrottledAccumulatingConsumer(com.google.cloud.tools.jib.event.progress.ThrottledAccumulatingConsumer) EventHandlers(com.google.cloud.tools.jib.event.EventHandlers)

Example 2 with ThrottledAccumulatingConsumer

use of com.google.cloud.tools.jib.event.progress.ThrottledAccumulatingConsumer in project jib by GoogleContainerTools.

the class LocalBaseImageSteps method compressAndCacheTarLayer.

private static PreparedLayer compressAndCacheTarLayer(Cache cache, DescriptorDigest diffId, Path layerFile, boolean layersAreCompressed, ProgressEventDispatcher.Factory progressEventDispatcherFactory) throws IOException, CacheCorruptedException {
    try (ProgressEventDispatcher childDispatcher = progressEventDispatcherFactory.create("compressing layer " + diffId, Files.size(layerFile));
        ThrottledAccumulatingConsumer throttledProgressReporter = new ThrottledAccumulatingConsumer(childDispatcher::dispatchProgress)) {
        // Retrieve pre-compressed layer from cache
        Optional<CachedLayer> optionalLayer = cache.retrieveTarLayer(diffId);
        if (optionalLayer.isPresent()) {
            return new PreparedLayer.Builder(optionalLayer.get()).build();
        }
        // Just write layers that are already compressed
        if (layersAreCompressed) {
            return new PreparedLayer.Builder(cache.writeTarLayer(diffId, Blobs.from(layerFile))).build();
        }
        // Compress uncompressed layers while writing
        Blob compressedBlob = Blobs.from(outputStream -> {
            try (GZIPOutputStream compressorStream = new GZIPOutputStream(outputStream);
                NotifyingOutputStream notifyingOutputStream = new NotifyingOutputStream(compressorStream, throttledProgressReporter)) {
                Blobs.from(layerFile).writeTo(notifyingOutputStream);
            }
        }, true);
        return new PreparedLayer.Builder(cache.writeTarLayer(diffId, compressedBlob)).build();
    }
}
Also used : NotifyingOutputStream(com.google.cloud.tools.jib.http.NotifyingOutputStream) Blob(com.google.cloud.tools.jib.blob.Blob) ProgressEventDispatcher(com.google.cloud.tools.jib.builder.ProgressEventDispatcher) GZIPOutputStream(java.util.zip.GZIPOutputStream) CachedLayer(com.google.cloud.tools.jib.cache.CachedLayer) ThrottledAccumulatingConsumer(com.google.cloud.tools.jib.event.progress.ThrottledAccumulatingConsumer)

Example 3 with ThrottledAccumulatingConsumer

use of com.google.cloud.tools.jib.event.progress.ThrottledAccumulatingConsumer in project jib by google.

the class LocalBaseImageSteps method compressAndCacheTarLayer.

private static PreparedLayer compressAndCacheTarLayer(Cache cache, DescriptorDigest diffId, Path layerFile, boolean layersAreCompressed, ProgressEventDispatcher.Factory progressEventDispatcherFactory) throws IOException, CacheCorruptedException {
    try (ProgressEventDispatcher childDispatcher = progressEventDispatcherFactory.create("compressing layer " + diffId, Files.size(layerFile));
        ThrottledAccumulatingConsumer throttledProgressReporter = new ThrottledAccumulatingConsumer(childDispatcher::dispatchProgress)) {
        // Retrieve pre-compressed layer from cache
        Optional<CachedLayer> optionalLayer = cache.retrieveTarLayer(diffId);
        if (optionalLayer.isPresent()) {
            return new PreparedLayer.Builder(optionalLayer.get()).build();
        }
        // Just write layers that are already compressed
        if (layersAreCompressed) {
            return new PreparedLayer.Builder(cache.writeTarLayer(diffId, Blobs.from(layerFile))).build();
        }
        // Compress uncompressed layers while writing
        Blob compressedBlob = Blobs.from(outputStream -> {
            try (GZIPOutputStream compressorStream = new GZIPOutputStream(outputStream);
                NotifyingOutputStream notifyingOutputStream = new NotifyingOutputStream(compressorStream, throttledProgressReporter)) {
                Blobs.from(layerFile).writeTo(notifyingOutputStream);
            }
        }, true);
        return new PreparedLayer.Builder(cache.writeTarLayer(diffId, compressedBlob)).build();
    }
}
Also used : NotifyingOutputStream(com.google.cloud.tools.jib.http.NotifyingOutputStream) Blob(com.google.cloud.tools.jib.blob.Blob) ProgressEventDispatcher(com.google.cloud.tools.jib.builder.ProgressEventDispatcher) GZIPOutputStream(java.util.zip.GZIPOutputStream) CachedLayer(com.google.cloud.tools.jib.cache.CachedLayer) ThrottledAccumulatingConsumer(com.google.cloud.tools.jib.event.progress.ThrottledAccumulatingConsumer)

Example 4 with ThrottledAccumulatingConsumer

use of com.google.cloud.tools.jib.event.progress.ThrottledAccumulatingConsumer in project jib by google.

the class ThrottledProgressEventDispatcherWrapper method setProgressTarget.

void setProgressTarget(long allocationUnits) {
    Preconditions.checkState(progressEventDispatcher == null);
    progressEventDispatcher = progressEventDispatcherFactory.create(description, allocationUnits);
    throttledDispatcher = new ThrottledAccumulatingConsumer(progressEventDispatcher::dispatchProgress);
}
Also used : ThrottledAccumulatingConsumer(com.google.cloud.tools.jib.event.progress.ThrottledAccumulatingConsumer)

Example 5 with ThrottledAccumulatingConsumer

use of com.google.cloud.tools.jib.event.progress.ThrottledAccumulatingConsumer in project jib by google.

the class NotifyingOutputStreamTest method testDelay.

@Test
public void testDelay() throws IOException {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    List<Long> byteCounts = new ArrayList<>();
    Queue<Instant> instantQueue = new ArrayDeque<>();
    instantQueue.add(Instant.EPOCH);
    try (ThrottledAccumulatingConsumer byteCounter = new ThrottledAccumulatingConsumer(byteCounts::add, Duration.ofSeconds(3), instantQueue::remove);
        NotifyingOutputStream notifyingOutputStream = new NotifyingOutputStream(byteArrayOutputStream, byteCounter)) {
        instantQueue.add(Instant.EPOCH);
        notifyingOutputStream.write(100);
        instantQueue.add(Instant.EPOCH);
        notifyingOutputStream.write(new byte[] { 101, 102, 103 });
        instantQueue.add(Instant.EPOCH.plusSeconds(4));
        notifyingOutputStream.write(new byte[] { 104, 105, 106 });
        instantQueue.add(Instant.EPOCH.plusSeconds(10));
        notifyingOutputStream.write(new byte[] { 107, 108 });
        instantQueue.add(Instant.EPOCH.plusSeconds(10));
        notifyingOutputStream.write(new byte[] { 109 });
        instantQueue.add(Instant.EPOCH.plusSeconds(13));
        notifyingOutputStream.write(new byte[] { 0, 110 }, 1, 1);
    }
    Assert.assertEquals(Arrays.asList(7L, 2L, 2L), byteCounts);
    Assert.assertArrayEquals(new byte[] { 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110 }, byteArrayOutputStream.toByteArray());
}
Also used : Instant(java.time.Instant) ArrayList(java.util.ArrayList) ThrottledAccumulatingConsumer(com.google.cloud.tools.jib.event.progress.ThrottledAccumulatingConsumer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ArrayDeque(java.util.ArrayDeque) Test(org.junit.Test)

Aggregations

ThrottledAccumulatingConsumer (com.google.cloud.tools.jib.event.progress.ThrottledAccumulatingConsumer)10 ProgressEventDispatcher (com.google.cloud.tools.jib.builder.ProgressEventDispatcher)6 TimerEventDispatcher (com.google.cloud.tools.jib.builder.TimerEventDispatcher)4 EventHandlers (com.google.cloud.tools.jib.event.EventHandlers)4 DescriptorDigest (com.google.cloud.tools.jib.api.DescriptorDigest)2 Blob (com.google.cloud.tools.jib.blob.Blob)2 CachedLayer (com.google.cloud.tools.jib.cache.CachedLayer)2 NotifyingOutputStream (com.google.cloud.tools.jib.http.NotifyingOutputStream)2 ImageTarball (com.google.cloud.tools.jib.image.ImageTarball)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Instant (java.time.Instant)2 ArrayDeque (java.util.ArrayDeque)2 ArrayList (java.util.ArrayList)2 GZIPOutputStream (java.util.zip.GZIPOutputStream)2 Test (org.junit.Test)2