Browse Source

Improve stability of build complete archive creation (#106578)

This is an attempt to fix some errors creating the diagnostic archive at
the end of builds in CI. We've seen errors where we attempt to write
more bytes than expected into the archive. The assumption here is that
we are bundling files that are possibly still being written to. This
_shouldn't_ happen, but it's useful to have these diagnostic bundles in
either case.

Closes https://github.com/elastic/elasticsearch/issues/106576
Mark Vieira 1 year ago
parent
commit
a41e11f034

+ 11 - 3
build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/ElasticsearchBuildCompletePlugin.java

@@ -28,7 +28,12 @@ import org.gradle.api.provider.Property;
 import org.gradle.api.tasks.Input;
 import org.gradle.api.tasks.Input;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.NotNull;
 
 
-import java.io.*;
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.OutputStream;
 import java.nio.file.Files;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Path;
 import java.util.ArrayList;
 import java.util.ArrayList;
@@ -210,12 +215,15 @@ public abstract class ElasticsearchBuildCompletePlugin implements Plugin<Project
                         throw new IOException("Support only file!");
                         throw new IOException("Support only file!");
                     }
                     }
 
 
+                    long entrySize = Files.size(path);
                     TarArchiveEntry tarEntry = new TarArchiveEntry(path.toFile(), calculateArchivePath(path, projectPath));
                     TarArchiveEntry tarEntry = new TarArchiveEntry(path.toFile(), calculateArchivePath(path, projectPath));
-                    tarEntry.setSize(Files.size(path));
+                    tarEntry.setSize(entrySize);
                     tOut.putArchiveEntry(tarEntry);
                     tOut.putArchiveEntry(tarEntry);
 
 
                     // copy file to TarArchiveOutputStream
                     // copy file to TarArchiveOutputStream
-                    Files.copy(path, tOut);
+                    try (BufferedInputStream bin = new BufferedInputStream(Files.newInputStream(path))) {
+                        IOUtils.copyLarge(bin, tOut, 0, entrySize);
+                    }
                     tOut.closeArchiveEntry();
                     tOut.closeArchiveEntry();
 
 
                 }
                 }