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;
}
}
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();
}
}
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();
}
}
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);
}
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());
}
Aggregations