Search in sources :

Example 1 with RecordReaderFactory

use of org.apache.nifi.provenance.store.RecordReaderFactory in project nifi by apache.

the class TestSelectiveRecordReaderEventIterator method testPerformanceOfRandomAccessReads.

@Test
@Ignore("For local testing only. Runs indefinitely")
public void testPerformanceOfRandomAccessReads() throws Exception {
    final File dir = new File("target/storage/" + UUID.randomUUID().toString());
    final File journalFile = new File(dir, "/4.prov.gz");
    final File tocFile = TocUtil.getTocFile(journalFile);
    final int blockSize = 1024 * 32;
    try (final RecordWriter writer = createWriter(journalFile, new StandardTocWriter(tocFile, true, false), true, blockSize)) {
        writer.writeHeader(0L);
        for (int i = 0; i < 100_000; i++) {
            writer.writeRecord(TestUtil.createEvent());
        }
    }
    final Long[] eventIds = new Long[] { 4L, 80L, 1024L, 1025L, 1026L, 1027L, 1028L, 1029L, 1030L, 40_000L, 80_000L, 99_000L };
    final RecordReaderFactory readerFactory = (file, logs, maxChars) -> RecordReaders.newRecordReader(file, logs, maxChars);
    final List<File> files = new ArrayList<>();
    files.add(new File(dir, "0.prov"));
    files.add(new File(dir, "0.prov"));
    files.add(new File(dir, "1.prov"));
    files.add(new File(dir, "2.prov"));
    files.add(new File(dir, "3.prov"));
    files.add(journalFile);
    files.add(new File(dir, "100000000.prov"));
    boolean loopForever = true;
    while (loopForever) {
        final long start = System.nanoTime();
        for (int i = 0; i < 1000; i++) {
            final SelectiveRecordReaderEventIterator iterator = new SelectiveRecordReaderEventIterator(Collections.singletonList(journalFile), readerFactory, Arrays.asList(eventIds), 32 * 1024);
            for (final long id : eventIds) {
                time(() -> {
                    return iterator.nextEvent().orElse(null);
                }, id);
            }
        }
        final long ms = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start);
        System.out.println(ms + " ms total");
    }
}
Also used : Arrays(java.util.Arrays) StandardTocWriter(org.apache.nifi.provenance.toc.StandardTocWriter) IdentifierLookup(org.apache.nifi.provenance.IdentifierLookup) Callable(java.util.concurrent.Callable) RecordWriter(org.apache.nifi.provenance.serialization.RecordWriter) RecordReaders(org.apache.nifi.provenance.serialization.RecordReaders) ArrayList(java.util.ArrayList) TocWriter(org.apache.nifi.provenance.toc.TocWriter) ProvenanceEventRecord(org.apache.nifi.provenance.ProvenanceEventRecord) RecordReaderFactory(org.apache.nifi.provenance.store.RecordReaderFactory) TocUtil(org.apache.nifi.provenance.toc.TocUtil) IOException(java.io.IOException) Test(org.junit.Test) UUID(java.util.UUID) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Ignore(org.junit.Ignore) Assert.assertFalse(org.junit.Assert.assertFalse) Optional(java.util.Optional) TestUtil(org.apache.nifi.provenance.TestUtil) Assert(org.junit.Assert) Collections(java.util.Collections) EventIdFirstSchemaRecordWriter(org.apache.nifi.provenance.EventIdFirstSchemaRecordWriter) Assert.assertEquals(org.junit.Assert.assertEquals) ArrayList(java.util.ArrayList) RecordReaderFactory(org.apache.nifi.provenance.store.RecordReaderFactory) StandardTocWriter(org.apache.nifi.provenance.toc.StandardTocWriter) RecordWriter(org.apache.nifi.provenance.serialization.RecordWriter) EventIdFirstSchemaRecordWriter(org.apache.nifi.provenance.EventIdFirstSchemaRecordWriter) AtomicLong(java.util.concurrent.atomic.AtomicLong) File(java.io.File) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with RecordReaderFactory

use of org.apache.nifi.provenance.store.RecordReaderFactory in project nifi by apache.

the class TestSelectiveRecordReaderEventIterator method testFileNotFound.

@Test
public void testFileNotFound() throws IOException {
    final File file1 = new File("1.prov");
    // Filter out the first file.
    final List<File> files = new ArrayList<>();
    files.add(file1);
    List<Long> eventIds = new ArrayList<>();
    eventIds.add(1L);
    eventIds.add(5L);
    final RecordReaderFactory readerFactory = (file, logs, maxChars) -> {
        return RecordReaders.newRecordReader(file, logs, maxChars);
    };
    final SelectiveRecordReaderEventIterator itr = new SelectiveRecordReaderEventIterator(files, readerFactory, eventIds, 65536);
    final Optional<ProvenanceEventRecord> firstRecordOption = itr.nextEvent();
    assertFalse(firstRecordOption.isPresent());
}
Also used : Arrays(java.util.Arrays) StandardTocWriter(org.apache.nifi.provenance.toc.StandardTocWriter) IdentifierLookup(org.apache.nifi.provenance.IdentifierLookup) Callable(java.util.concurrent.Callable) RecordWriter(org.apache.nifi.provenance.serialization.RecordWriter) RecordReaders(org.apache.nifi.provenance.serialization.RecordReaders) ArrayList(java.util.ArrayList) TocWriter(org.apache.nifi.provenance.toc.TocWriter) ProvenanceEventRecord(org.apache.nifi.provenance.ProvenanceEventRecord) RecordReaderFactory(org.apache.nifi.provenance.store.RecordReaderFactory) TocUtil(org.apache.nifi.provenance.toc.TocUtil) IOException(java.io.IOException) Test(org.junit.Test) UUID(java.util.UUID) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Ignore(org.junit.Ignore) Assert.assertFalse(org.junit.Assert.assertFalse) Optional(java.util.Optional) TestUtil(org.apache.nifi.provenance.TestUtil) Assert(org.junit.Assert) Collections(java.util.Collections) EventIdFirstSchemaRecordWriter(org.apache.nifi.provenance.EventIdFirstSchemaRecordWriter) Assert.assertEquals(org.junit.Assert.assertEquals) ProvenanceEventRecord(org.apache.nifi.provenance.ProvenanceEventRecord) ArrayList(java.util.ArrayList) AtomicLong(java.util.concurrent.atomic.AtomicLong) File(java.io.File) RecordReaderFactory(org.apache.nifi.provenance.store.RecordReaderFactory) Test(org.junit.Test)

Example 3 with RecordReaderFactory

use of org.apache.nifi.provenance.store.RecordReaderFactory in project nifi by apache.

the class WriteAheadProvenanceRepository method initialize.

@Override
public synchronized void initialize(final EventReporter eventReporter, final Authorizer authorizer, final ProvenanceAuthorizableFactory resourceFactory, final IdentifierLookup idLookup) throws IOException {
    final RecordWriterFactory recordWriterFactory = (file, idGenerator, compressed, createToc) -> {
        final TocWriter tocWriter = createToc ? new StandardTocWriter(TocUtil.getTocFile(file), false, false) : null;
        return new EventIdFirstSchemaRecordWriter(file, idGenerator, tocWriter, compressed, BLOCK_SIZE, idLookup);
    };
    final EventFileManager fileManager = new EventFileManager();
    final RecordReaderFactory recordReaderFactory = (file, logs, maxChars) -> {
        fileManager.obtainReadLock(file);
        try {
            return RecordReaders.newRecordReader(file, logs, maxChars);
        } finally {
            fileManager.releaseReadLock(file);
        }
    };
    init(recordWriterFactory, recordReaderFactory, eventReporter, authorizer, resourceFactory);
}
Also used : LuceneEventIndex(org.apache.nifi.provenance.index.lucene.LuceneEventIndex) CloseableUtil(org.apache.nifi.provenance.util.CloseableUtil) StandardTocWriter(org.apache.nifi.provenance.toc.StandardTocWriter) Query(org.apache.nifi.provenance.search.Query) LoggerFactory(org.slf4j.LoggerFactory) RecordReaders(org.apache.nifi.provenance.serialization.RecordReaders) HashSet(java.util.HashSet) EventFileManager(org.apache.nifi.provenance.store.EventFileManager) TocWriter(org.apache.nifi.provenance.toc.TocWriter) StorageResult(org.apache.nifi.provenance.store.StorageResult) NiFiUser(org.apache.nifi.authorization.user.NiFiUser) Map(java.util.Map) EventStore(org.apache.nifi.provenance.store.EventStore) RecordReaderFactory(org.apache.nifi.provenance.store.RecordReaderFactory) TocUtil(org.apache.nifi.provenance.toc.TocUtil) UserEventAuthorizer(org.apache.nifi.provenance.authorization.UserEventAuthorizer) QuerySubmission(org.apache.nifi.provenance.search.QuerySubmission) SearchableField(org.apache.nifi.provenance.search.SearchableField) EventIndex(org.apache.nifi.provenance.index.EventIndex) Logger(org.slf4j.Logger) Authorizable(org.apache.nifi.authorization.resource.Authorizable) FileUtils(org.apache.nifi.util.file.FileUtils) StorageSummary(org.apache.nifi.provenance.serialization.StorageSummary) Files(java.nio.file.Files) RequestAction(org.apache.nifi.authorization.RequestAction) Set(java.util.Set) IOException(java.io.IOException) PartitionedWriteAheadEventStore(org.apache.nifi.provenance.store.PartitionedWriteAheadEventStore) SimpleIndexManager(org.apache.nifi.provenance.lucene.SimpleIndexManager) File(java.io.File) List(java.util.List) Authorizer(org.apache.nifi.authorization.Authorizer) EventReporter(org.apache.nifi.events.EventReporter) ComputeLineageSubmission(org.apache.nifi.provenance.lineage.ComputeLineageSubmission) NiFiProperties(org.apache.nifi.util.NiFiProperties) IndexManager(org.apache.nifi.provenance.lucene.IndexManager) Severity(org.apache.nifi.reporting.Severity) RecordWriterFactory(org.apache.nifi.provenance.store.RecordWriterFactory) Collections(java.util.Collections) EventAuthorizer(org.apache.nifi.provenance.authorization.EventAuthorizer) StandardTocWriter(org.apache.nifi.provenance.toc.StandardTocWriter) StandardTocWriter(org.apache.nifi.provenance.toc.StandardTocWriter) TocWriter(org.apache.nifi.provenance.toc.TocWriter) EventFileManager(org.apache.nifi.provenance.store.EventFileManager) RecordWriterFactory(org.apache.nifi.provenance.store.RecordWriterFactory) RecordReaderFactory(org.apache.nifi.provenance.store.RecordReaderFactory)

Example 4 with RecordReaderFactory

use of org.apache.nifi.provenance.store.RecordReaderFactory in project nifi by apache.

the class EncryptedWriteAheadProvenanceRepository method initialize.

/**
 * This method initializes the repository. It first builds the key provider and event encryptor
 * from the config values, then creates the encrypted record writer and reader, then delegates
 * back to the superclass for the common implementation.
 *
 * @param eventReporter   the event reporter
 * @param authorizer      the authorizer
 * @param resourceFactory the authorizable factory
 * @param idLookup        the lookup provider
 * @throws IOException if there is an error initializing this repository
 */
@Override
public synchronized void initialize(final EventReporter eventReporter, final Authorizer authorizer, final ProvenanceAuthorizableFactory resourceFactory, final IdentifierLookup idLookup) throws IOException {
    // Initialize the encryption-specific fields
    ProvenanceEventEncryptor provenanceEventEncryptor;
    if (getConfig().supportsEncryption()) {
        try {
            KeyProvider keyProvider;
            if (KeyProviderFactory.requiresMasterKey(getConfig().getKeyProviderImplementation())) {
                SecretKey masterKey = getMasterKey();
                keyProvider = buildKeyProvider(masterKey);
            } else {
                keyProvider = buildKeyProvider();
            }
            provenanceEventEncryptor = new AESProvenanceEventEncryptor();
            provenanceEventEncryptor.initialize(keyProvider);
        } catch (KeyManagementException e) {
            String msg = "Encountered an error building the key provider";
            logger.error(msg, e);
            throw new IOException(msg, e);
        }
    } else {
        throw new IOException("The provided configuration does not support a encrypted repository");
    }
    // Build a factory using lambda which injects the encryptor
    final RecordWriterFactory recordWriterFactory = (file, idGenerator, compressed, createToc) -> {
        try {
            final TocWriter tocWriter = createToc ? new StandardTocWriter(TocUtil.getTocFile(file), false, false) : null;
            return new EncryptedSchemaRecordWriter(file, idGenerator, tocWriter, compressed, BLOCK_SIZE, idLookup, provenanceEventEncryptor, getConfig().getDebugFrequency());
        } catch (EncryptionException e) {
            logger.error("Encountered an error building the schema record writer factory: ", e);
            throw new IOException(e);
        }
    };
    // Build a factory using lambda which injects the encryptor
    final EventFileManager fileManager = new EventFileManager();
    final RecordReaderFactory recordReaderFactory = (file, logs, maxChars) -> {
        fileManager.obtainReadLock(file);
        try {
            EncryptedSchemaRecordReader tempReader = (EncryptedSchemaRecordReader) RecordReaders.newRecordReader(file, logs, maxChars);
            tempReader.setProvenanceEventEncryptor(provenanceEventEncryptor);
            return tempReader;
        } finally {
            fileManager.releaseReadLock(file);
        }
    };
    // Delegate the init to the parent impl
    super.init(recordWriterFactory, recordReaderFactory, eventReporter, authorizer, resourceFactory);
}
Also used : KeyProvider(org.apache.nifi.security.kms.KeyProvider) StandardTocWriter(org.apache.nifi.provenance.toc.StandardTocWriter) Logger(org.slf4j.Logger) DecoderException(org.apache.commons.codec.DecoderException) NiFiPropertiesLoader(org.apache.nifi.properties.NiFiPropertiesLoader) LoggerFactory(org.slf4j.LoggerFactory) SecretKeySpec(javax.crypto.spec.SecretKeySpec) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) Hex(org.apache.commons.codec.binary.Hex) RecordReaders(org.apache.nifi.provenance.serialization.RecordReaders) EventFileManager(org.apache.nifi.provenance.store.EventFileManager) TocWriter(org.apache.nifi.provenance.toc.TocWriter) Authorizer(org.apache.nifi.authorization.Authorizer) EventReporter(org.apache.nifi.events.EventReporter) NiFiProperties(org.apache.nifi.util.NiFiProperties) RecordReaderFactory(org.apache.nifi.provenance.store.RecordReaderFactory) SecretKey(javax.crypto.SecretKey) RecordWriterFactory(org.apache.nifi.provenance.store.RecordWriterFactory) TocUtil(org.apache.nifi.provenance.toc.TocUtil) KeyProvider(org.apache.nifi.security.kms.KeyProvider) KeyProviderFactory(org.apache.nifi.security.kms.KeyProviderFactory) EventFileManager(org.apache.nifi.provenance.store.EventFileManager) IOException(java.io.IOException) RecordWriterFactory(org.apache.nifi.provenance.store.RecordWriterFactory) KeyManagementException(java.security.KeyManagementException) RecordReaderFactory(org.apache.nifi.provenance.store.RecordReaderFactory) StandardTocWriter(org.apache.nifi.provenance.toc.StandardTocWriter) SecretKey(javax.crypto.SecretKey) StandardTocWriter(org.apache.nifi.provenance.toc.StandardTocWriter) TocWriter(org.apache.nifi.provenance.toc.TocWriter)

Aggregations

IOException (java.io.IOException)4 RecordReaders (org.apache.nifi.provenance.serialization.RecordReaders)4 RecordReaderFactory (org.apache.nifi.provenance.store.RecordReaderFactory)4 StandardTocWriter (org.apache.nifi.provenance.toc.StandardTocWriter)4 TocUtil (org.apache.nifi.provenance.toc.TocUtil)4 TocWriter (org.apache.nifi.provenance.toc.TocWriter)4 File (java.io.File)3 Collections (java.util.Collections)3 List (java.util.List)3 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Optional (java.util.Optional)2 UUID (java.util.UUID)2 Callable (java.util.concurrent.Callable)2 TimeUnit (java.util.concurrent.TimeUnit)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 Authorizer (org.apache.nifi.authorization.Authorizer)2 EventReporter (org.apache.nifi.events.EventReporter)2 EventIdFirstSchemaRecordWriter (org.apache.nifi.provenance.EventIdFirstSchemaRecordWriter)2 IdentifierLookup (org.apache.nifi.provenance.IdentifierLookup)2