From ae6027ce50718b43e9bee03fdce9097ebd97b4bf Mon Sep 17 00:00:00 2001 From: Marcus Linke Date: Tue, 28 Jul 2015 21:08:40 +0200 Subject: [PATCH 1/2] Added GZIP compression for build context creation --- .../github/dockerjava/core/CompressArchiveUtil.java | 5 ++++- .../core/async/ResultCallbackTemplate.java | 2 +- .../dockerjava/core/CompressArchiveUtilTest.java | 5 ++++- .../core/command/BuildImageCmdImplTest.java | 13 +++++++++++++ 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/github/dockerjava/core/CompressArchiveUtil.java b/src/main/java/com/github/dockerjava/core/CompressArchiveUtil.java index 6a1a54809..cf089e33c 100644 --- a/src/main/java/com/github/dockerjava/core/CompressArchiveUtil.java +++ b/src/main/java/com/github/dockerjava/core/CompressArchiveUtil.java @@ -2,9 +2,11 @@ import static com.github.dockerjava.core.FilePathUtil.relativize; +import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; +import java.util.zip.GZIPOutputStream; import org.apache.commons.compress.archivers.tar.TarArchiveEntry; import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream; @@ -16,7 +18,8 @@ public static File archiveTARFiles(File base, Iterable files, String archi throws IOException { File tarFile = new File(FileUtils.getTempDirectoryPath(), archiveNameWithOutExtension + ".tar"); tarFile.deleteOnExit(); - TarArchiveOutputStream tos = new TarArchiveOutputStream(new FileOutputStream(tarFile)); + TarArchiveOutputStream tos = new TarArchiveOutputStream(new GZIPOutputStream( + new BufferedOutputStream(new FileOutputStream(tarFile)))); try { tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); for (File file : files) { diff --git a/src/main/java/com/github/dockerjava/core/async/ResultCallbackTemplate.java b/src/main/java/com/github/dockerjava/core/async/ResultCallbackTemplate.java index d94ce1950..857ca9177 100644 --- a/src/main/java/com/github/dockerjava/core/async/ResultCallbackTemplate.java +++ b/src/main/java/com/github/dockerjava/core/async/ResultCallbackTemplate.java @@ -63,10 +63,10 @@ public void onComplete() { @Override public void close() throws IOException { + closed = true; if (stream != null) stream.close(); completed.countDown(); - closed = true; } /** diff --git a/src/test/java/com/github/dockerjava/core/CompressArchiveUtilTest.java b/src/test/java/com/github/dockerjava/core/CompressArchiveUtilTest.java index 827f8d3be..90ca91fca 100644 --- a/src/test/java/com/github/dockerjava/core/CompressArchiveUtilTest.java +++ b/src/test/java/com/github/dockerjava/core/CompressArchiveUtilTest.java @@ -5,10 +5,12 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; +import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; +import java.util.zip.GZIPInputStream; import org.apache.commons.compress.archivers.tar.TarArchiveEntry; import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; @@ -43,7 +45,8 @@ private File extractFileByName(File archive, String filenameToExtract) throws IO expectedFile.delete(); assertThat(expectedFile.exists(), is(false)); - TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(new FileInputStream(archive)); + TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(new GZIPInputStream( + new BufferedInputStream(new FileInputStream(archive)))); TarArchiveEntry entry; while ((entry = tarArchiveInputStream.getNextTarEntry()) != null) { String individualFiles = entry.getName(); diff --git a/src/test/java/com/github/dockerjava/core/command/BuildImageCmdImplTest.java b/src/test/java/com/github/dockerjava/core/command/BuildImageCmdImplTest.java index 6d2b4c805..5385287ef 100644 --- a/src/test/java/com/github/dockerjava/core/command/BuildImageCmdImplTest.java +++ b/src/test/java/com/github/dockerjava/core/command/BuildImageCmdImplTest.java @@ -225,4 +225,17 @@ public void testAddAndCopySubstitution() throws Exception { String response = dockerfileBuild(baseDir); assertThat(response, containsString("testENVSubstitution successfully completed")); } + + @Test + public void testBuilderPerformance() throws Exception { + File baseDir = new File(Thread.currentThread().getContextClassLoader().getResource("nginx").getFile()); + + String imageId = buildImage(baseDir); + + InspectImageResponse inspectImageResponse = dockerClient.inspectImageCmd(imageId).exec(); + assertThat(inspectImageResponse, not(nullValue())); + LOG.info("Image Inspect: {}", inspectImageResponse.toString()); + + assertThat(inspectImageResponse.getAuthor(), equalTo("Guillaume J. Charmes \"guillaume@dotcloud.com\"")); + } } From d8d394630d1baebf7da165236a22a2a2e070064f Mon Sep 17 00:00:00 2001 From: Marcus Linke Date: Thu, 30 Jul 2015 21:05:12 +0200 Subject: [PATCH 2/2] use try-with-resource --- .../com/github/dockerjava/core/CompressArchiveUtil.java | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/github/dockerjava/core/CompressArchiveUtil.java b/src/main/java/com/github/dockerjava/core/CompressArchiveUtil.java index cf089e33c..0a93af3d8 100644 --- a/src/main/java/com/github/dockerjava/core/CompressArchiveUtil.java +++ b/src/main/java/com/github/dockerjava/core/CompressArchiveUtil.java @@ -18,9 +18,8 @@ public static File archiveTARFiles(File base, Iterable files, String archi throws IOException { File tarFile = new File(FileUtils.getTempDirectoryPath(), archiveNameWithOutExtension + ".tar"); tarFile.deleteOnExit(); - TarArchiveOutputStream tos = new TarArchiveOutputStream(new GZIPOutputStream( - new BufferedOutputStream(new FileOutputStream(tarFile)))); - try { + try(TarArchiveOutputStream tos = new TarArchiveOutputStream(new GZIPOutputStream( + new BufferedOutputStream(new FileOutputStream(tarFile))))) { tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); for (File file : files) { TarArchiveEntry tarEntry = new TarArchiveEntry(file); @@ -39,8 +38,6 @@ public static File archiveTARFiles(File base, Iterable files, String archi } tos.closeArchiveEntry(); } - } finally { - tos.close(); } return tarFile;