Search in sources :

Example 21 with StandardTocWriter

use of org.apache.nifi.provenance.toc.StandardTocWriter in project nifi by apache.

the class EventFileCompressor method run.

@Override
public void run() {
    while (!shutdown) {
        File uncompressedEventFile = null;
        try {
            final long start = System.nanoTime();
            uncompressedEventFile = filesToCompress.poll(1, TimeUnit.SECONDS);
            if (uncompressedEventFile == null || shutdown) {
                continue;
            }
            File outputFile = null;
            long bytesBefore = 0L;
            StandardTocReader tocReader = null;
            File tmpTocFile = null;
            eventFileManager.obtainReadLock(uncompressedEventFile);
            try {
                StandardTocWriter tocWriter = null;
                final File tocFile = TocUtil.getTocFile(uncompressedEventFile);
                try {
                    tocReader = new StandardTocReader(tocFile);
                } catch (final IOException e) {
                    logger.error("Failed to read TOC File {}", tocFile, e);
                    continue;
                }
                bytesBefore = uncompressedEventFile.length();
                try {
                    outputFile = new File(uncompressedEventFile.getParentFile(), uncompressedEventFile.getName() + ".gz");
                    try {
                        tmpTocFile = new File(tocFile.getParentFile(), tocFile.getName() + ".tmp");
                        tocWriter = new StandardTocWriter(tmpTocFile, true, false);
                        compress(uncompressedEventFile, tocReader, outputFile, tocWriter);
                        tocWriter.close();
                    } catch (final IOException ioe) {
                        logger.error("Failed to compress {} on rollover", uncompressedEventFile, ioe);
                    }
                } finally {
                    CloseableUtil.closeQuietly(tocReader, tocWriter);
                }
            } finally {
                eventFileManager.releaseReadLock(uncompressedEventFile);
            }
            eventFileManager.obtainWriteLock(uncompressedEventFile);
            try {
                // Attempt to delete the input file and associated toc file
                if (uncompressedEventFile.delete()) {
                    if (tocReader != null) {
                        final File tocFile = tocReader.getFile();
                        if (!tocFile.delete()) {
                            logger.warn("Failed to delete {}; this file should be cleaned up manually", tocFile);
                        }
                        if (tmpTocFile != null) {
                            tmpTocFile.renameTo(tocFile);
                        }
                    }
                } else {
                    logger.warn("Failed to delete {}; this file should be cleaned up manually", uncompressedEventFile);
                }
            } finally {
                eventFileManager.releaseWriteLock(uncompressedEventFile);
            }
            final long millis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start);
            final long bytesAfter = outputFile.length();
            final double reduction = 100 * (1 - (double) bytesAfter / (double) bytesBefore);
            final String reductionTwoDecimals = String.format("%.2f", reduction);
            logger.debug("Successfully compressed Provenance Event File {} in {} millis from {} to {}, a reduction of {}%", uncompressedEventFile, millis, FormatUtils.formatDataSize(bytesBefore), FormatUtils.formatDataSize(bytesAfter), reductionTwoDecimals);
        } catch (final InterruptedException e) {
            Thread.currentThread().interrupt();
            return;
        } catch (final Exception e) {
            logger.error("Failed to compress {}", uncompressedEventFile, e);
        }
    }
}
Also used : StandardTocWriter(org.apache.nifi.provenance.toc.StandardTocWriter) StandardTocReader(org.apache.nifi.provenance.toc.StandardTocReader) IOException(java.io.IOException) File(java.io.File) IOException(java.io.IOException)

Aggregations

StandardTocWriter (org.apache.nifi.provenance.toc.StandardTocWriter)21 File (java.io.File)18 TocWriter (org.apache.nifi.provenance.toc.TocWriter)18 Test (org.junit.Test)17 StandardTocReader (org.apache.nifi.provenance.toc.StandardTocReader)15 RecordWriter (org.apache.nifi.provenance.serialization.RecordWriter)14 FileInputStream (java.io.FileInputStream)12 RecordReader (org.apache.nifi.provenance.serialization.RecordReader)12 TocReader (org.apache.nifi.provenance.toc.TocReader)12 HashMap (java.util.HashMap)8 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)5 RecordReaders (org.apache.nifi.provenance.serialization.RecordReaders)5 TocUtil (org.apache.nifi.provenance.toc.TocUtil)5 Collections (java.util.Collections)4 List (java.util.List)4 EventReporter (org.apache.nifi.events.EventReporter)4 Ignore (org.junit.Ignore)4 InputStream (java.io.InputStream)3 Map (java.util.Map)3