Search in sources :

Example 6 with UpdateType

use of org.wali.UpdateType in project nifi by apache.

the class HashMapSnapshot method recover.

@Override
public SnapshotRecovery<T> recover() throws IOException {
    final File partialFile = getPartialFile();
    final File snapshotFile = getSnapshotFile();
    final boolean partialExists = partialFile.exists();
    final boolean snapshotExists = snapshotFile.exists();
    // return an empty recovery.
    if (!partialExists && !snapshotExists) {
        return SnapshotRecovery.emptyRecovery();
    }
    if (partialExists && snapshotExists) {
        // both files exist -- assume NiFi crashed/died while checkpointing. Delete the partial file.
        Files.delete(partialFile.toPath());
    } else if (partialExists) {
        // partial exists but snapshot does not -- we must have completed
        // creating the partial, deleted the snapshot
        // but crashed before renaming the partial to the snapshot. Just
        // rename partial to snapshot
        Files.move(partialFile.toPath(), snapshotFile.toPath());
    }
    if (snapshotFile.length() == 0) {
        logger.warn("{} Found 0-byte Snapshot file; skipping Snapshot file in recovery", this);
        return SnapshotRecovery.emptyRecovery();
    }
    // or we renamed partialPath to snapshotPath. So just Recover from snapshotPath.
    try (final DataInputStream dataIn = new DataInputStream(new BufferedInputStream(new FileInputStream(snapshotFile)))) {
        // Ensure that the header contains the information that we expect and retrieve the relevant information from the header.
        final SnapshotHeader header = validateHeader(dataIn);
        final SerDe<T> serde = header.getSerDe();
        final int serdeVersion = header.getSerDeVersion();
        final int numRecords = header.getNumRecords();
        final long maxTransactionId = header.getMaxTransactionId();
        // Read all of the records that we expect to receive.
        for (int i = 0; i < numRecords; i++) {
            final T record = serde.deserializeRecord(dataIn, serdeVersion);
            if (record == null) {
                throw new EOFException();
            }
            final UpdateType updateType = serde.getUpdateType(record);
            if (updateType == UpdateType.DELETE) {
                logger.warn("While recovering from snapshot, found record with type 'DELETE'; this record will not be restored");
                continue;
            }
            logger.trace("Recovered from snapshot: {}", record);
            recordMap.put(serde.getRecordIdentifier(record), record);
        }
        // Determine the location of any swap files.
        final int numSwapRecords = dataIn.readInt();
        final Set<String> swapLocations = new HashSet<>();
        for (int i = 0; i < numSwapRecords; i++) {
            swapLocations.add(dataIn.readUTF());
        }
        this.swapLocations.addAll(swapLocations);
        logger.info("{} restored {} Records and {} Swap Files from Snapshot, ending with Transaction ID {}", new Object[] { this, numRecords, swapLocations.size(), maxTransactionId });
        return new StandardSnapshotRecovery<>(recordMap, swapLocations, snapshotFile, maxTransactionId);
    }
}
Also used : DataInputStream(java.io.DataInputStream) UpdateType(org.wali.UpdateType) FileInputStream(java.io.FileInputStream) BufferedInputStream(java.io.BufferedInputStream) EOFException(java.io.EOFException) File(java.io.File) HashSet(java.util.HashSet)

Aggregations

UpdateType (org.wali.UpdateType)6 HashMap (java.util.HashMap)3 BufferedInputStream (java.io.BufferedInputStream)2 DataInputStream (java.io.DataInputStream)2 EOFException (java.io.EOFException)2 FileInputStream (java.io.FileInputStream)2 HashSet (java.util.HashSet)2 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 DecimalFormat (java.text.DecimalFormat)1 Map (java.util.Map)1 FlowFileQueue (org.apache.nifi.controller.queue.FlowFileQueue)1 ContentClaim (org.apache.nifi.controller.repository.claim.ContentClaim)1 StandardContentClaim (org.apache.nifi.controller.repository.claim.StandardContentClaim)1 FlowFile (org.apache.nifi.flowfile.FlowFile)1 NamedValue (org.apache.nifi.repository.schema.NamedValue)1 ByteCountingInputStream (org.apache.nifi.stream.io.ByteCountingInputStream)1 LimitingInputStream (org.apache.nifi.stream.io.LimitingInputStream)1