rmetzger commented on a change in pull request #16166:
URL: https://github.com/apache/flink/pull/16166#discussion_r681012996



##########
File path: 
flink-runtime/src/main/java/org/apache/flink/runtime/rest/handler/logbundler/LogArchiver.java
##########
@@ -0,0 +1,146 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.runtime.rest.handler.logbundler;
+
+import org.apache.flink.annotation.VisibleForTesting;
+
+import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
+import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
+import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.nio.file.Files;
+import java.nio.file.StandardOpenOption;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import static org.apache.flink.util.Preconditions.checkState;
+
+/** Simple util for creating a log archive. */
+public class LogArchiver {
+    private static final Logger log = 
LoggerFactory.getLogger(LogArchiver.class);
+    private static final Pattern namePattern = Pattern.compile("\\.([0-9]+)$");
+
+    private final TarArchiveOutputStream archiveOutputStream;
+    private final File archive;
+    private boolean isClosed;
+    private final Set<String> entryNames = new HashSet<>();
+
+    public LogArchiver(File file) throws IOException {
+        this.archive = file;
+
+        OutputStream fo =
+                Files.newOutputStream(
+                        file.toPath(),
+                        StandardOpenOption.CREATE,
+                        StandardOpenOption.WRITE,
+                        StandardOpenOption.TRUNCATE_EXISTING);
+        OutputStream gzo = new GzipCompressorOutputStream(fo);
+        this.archiveOutputStream = new TarArchiveOutputStream(gzo);
+        isClosed = false;
+        entryNames.clear();
+    }
+
+    public File getArchive() {
+        checkState(isClosed, "Can not download archive before it has been 
closed.");
+        return archive;
+    }
+
+    public synchronized void addArchiveEntry(
+            String entryName, InputStream input, long size, long modTime) 
throws IOException {
+        checkState(!isClosed, "Can not add archive entry to closed archiver");
+        String internalEntryName = entryName;
+        while (entryNames.contains(internalEntryName)) {
+            internalEntryName = getNextEntryName(internalEntryName);
+        }
+        log.debug("Adding archive entry name:{} size:{}", internalEntryName, 
size);
+        entryNames.add(internalEntryName);
+        TarArchiveEntry entry = new TarArchiveEntry(internalEntryName);
+        entry.setSize(size);
+        entry.setMode(TarArchiveEntry.DEFAULT_FILE_MODE);
+        entry.setModTime(modTime);
+        entry.setUserName("");
+        try {
+            archiveOutputStream.putArchiveEntry(entry);
+            // copy with limit: the input log file could have additional data, 
which would confuse
+            // the archiver

Review comment:
       Exactly.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to