Search in sources :

Example 1 with FlowFile

use of org.apache.nifi.flowfile.FlowFile in project kylo by Teradata.

the class InitializeFeed method onTrigger.

/* (non-Javadoc)
     * @see org.apache.nifi.processor.AbstractProcessor#onTrigger(org.apache.nifi.processor.ProcessContext, org.apache.nifi.processor.ProcessSession)
     */
@Override
public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException {
    FlowFile inputFF = session.get();
    if (inputFF != null) {
        inputFF = initialize(context, session, inputFF);
        InitializationStatus status = getMetadataRecorder().getInitializationStatus(getFeedId(context, inputFF)).orElse(new InitializationStatus(State.PENDING));
        switch(status.getState()) {
            case PENDING:
                pending(context, session, inputFF);
                break;
            case IN_PROGRESS:
                inProgress(context, session, inputFF);
                break;
            case FAILED:
                failed(context, session, inputFF, status.getTimestamp(), false);
                break;
            case REINITIALIZE:
                reinitialize(context, session, inputFF);
                break;
            case REINITIALIZE_FAILED:
                reinitializeFailed(context, session, inputFF, status.getTimestamp());
                break;
            case SUCCESS:
                success(context, session, inputFF);
        }
    }
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) InitializationStatus(com.thinkbiganalytics.metadata.rest.model.feed.InitializationStatus)

Example 2 with FlowFile

use of org.apache.nifi.flowfile.FlowFile in project kylo by Teradata.

the class SetSavepoint method getNextFlowFilex.

private Optional<FlowFile> getNextFlowFilex(ProcessContext context, ProcessSession session, SavepointProvider provider, PropertyValue pvSavepointId) {
    long expirationDuration = context.getProperty(EXPIRATION_DURATION).asTimePeriod(TimeUnit.MILLISECONDS);
    List<FlowFile> match = new ArrayList<>();
    List<FlowFile> noMatch = new LinkedList<>();
    session.get(session.getQueueSize().getObjectCount()).stream().sorted(Comparator.comparing(FlowFile::getLastQueueDate).reversed()).forEach(f -> {
        boolean isMatch = false;
        if (match.isEmpty()) {
            final String savepointIdStr = pvSavepointId.evaluateAttributeExpressions(f).getValue();
            String processorId = getIdentifier();
            SavepointEntry entry = provider.lookupEntry(savepointIdStr);
            if (entry == null || entry.getState(processorId) == null || isExpired(f, expirationDuration)) {
                isMatch = true;
            } else if (SavepointEntry.SavePointState.WAIT != entry.getState(processorId)) {
                isMatch = true;
            }
            // add it
            if (isMatch) {
                match.add(f);
            } else {
                noMatch.add(f);
            }
        } else {
            noMatch.add(f);
        }
    });
    // clear those that failed
    session.transfer(noMatch);
    return match.isEmpty() ? Optional.empty() : Optional.of(match.get(0));
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) ArrayList(java.util.ArrayList) SavepointEntry(com.thinkbiganalytics.nifi.v2.core.savepoint.SavepointEntry) LinkedList(java.util.LinkedList)

Example 3 with FlowFile

use of org.apache.nifi.flowfile.FlowFile in project nifi by apache.

the class TestStandardProcessSession method testWriteToOutputStreamWhileReading.

@Test
public void testWriteToOutputStreamWhileReading() throws IOException {
    final ContentClaim claim = contentRepo.create(false);
    try (final OutputStream out = contentRepo.write(claim)) {
        out.write("hello, world".getBytes());
    }
    final FlowFileRecord flowFileRecord = new StandardFlowFileRecord.Builder().contentClaim(claim).addAttribute("uuid", "12345678-1234-1234-1234-123456789012").entryDate(System.currentTimeMillis()).size(12L).build();
    flowFileQueue.put(flowFileRecord);
    final FlowFile flowFile = session.get();
    InputStream in = session.read(flowFile);
    try {
        session.write(flowFile);
        Assert.fail("Was able to obtain an OutputStream for a FlowFile while also holding an InputStream for it");
    } catch (final IllegalStateException e) {
    // expected
    } finally {
        in.close();
    }
    // Should now be okay
    try (final OutputStream out = session.write(flowFile)) {
    }
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) MockFlowFile(org.apache.nifi.util.MockFlowFile) StandardContentClaim(org.apache.nifi.controller.repository.claim.StandardContentClaim) ContentClaim(org.apache.nifi.controller.repository.claim.ContentClaim) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FilterOutputStream(java.io.FilterOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) Test(org.junit.Test)

Example 4 with FlowFile

use of org.apache.nifi.flowfile.FlowFile in project nifi by apache.

the class TestStandardProcessSession method testMigrateWithAppendableStream.

@Test
public void testMigrateWithAppendableStream() throws IOException {
    FlowFile flowFile = session.create();
    flowFile = session.append(flowFile, out -> out.write("1".getBytes()));
    flowFile = session.append(flowFile, out -> out.write("2".getBytes()));
    final StandardProcessSession newSession = new StandardProcessSession(context, () -> false);
    assertTrue(session.isFlowFileKnown(flowFile));
    assertFalse(newSession.isFlowFileKnown(flowFile));
    session.migrate(newSession, Collections.singleton(flowFile));
    assertFalse(session.isFlowFileKnown(flowFile));
    assertTrue(newSession.isFlowFileKnown(flowFile));
    flowFile = newSession.append(flowFile, out -> out.write("3".getBytes()));
    final byte[] buff = new byte[3];
    try (final InputStream in = newSession.read(flowFile)) {
        StreamUtils.fillBuffer(in, buff, true);
        assertEquals(-1, in.read());
    }
    assertTrue(Arrays.equals(new byte[] { '1', '2', '3' }, buff));
    newSession.remove(flowFile);
    newSession.commit();
    session.commit();
}
Also used : OutputStreamCallback(org.apache.nifi.processor.io.OutputStreamCallback) Arrays(java.util.Arrays) FlowFileFilter(org.apache.nifi.processor.FlowFileFilter) ProcessGroup(org.apache.nifi.groups.ProcessGroup) ConnectableType(org.apache.nifi.connectable.ConnectableType) Mockito.doThrow(org.mockito.Mockito.doThrow) ByteArrayInputStream(java.io.ByteArrayInputStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StandardContentClaim(org.apache.nifi.controller.repository.claim.StandardContentClaim) FlowFileFilterResult(org.apache.nifi.processor.FlowFileFilter.FlowFileFilterResult) Map(java.util.Map) After(org.junit.After) MockProvenanceRepository(org.apache.nifi.provenance.MockProvenanceRepository) Connectable(org.apache.nifi.connectable.Connectable) Connection(org.apache.nifi.connectable.Connection) Path(java.nio.file.Path) InputStreamCallback(org.apache.nifi.processor.io.InputStreamCallback) FlowFileAccessException(org.apache.nifi.processor.exception.FlowFileAccessException) FlowFile(org.apache.nifi.flowfile.FlowFile) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) FilterOutputStream(java.io.FilterOutputStream) MissingFlowFileException(org.apache.nifi.processor.exception.MissingFlowFileException) FileNotFoundException(java.io.FileNotFoundException) Matchers.any(org.mockito.Matchers.any) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) Pattern(java.util.regex.Pattern) StreamCallback(org.apache.nifi.processor.io.StreamCallback) MockFlowFile(org.apache.nifi.util.MockFlowFile) ContentClaim(org.apache.nifi.controller.repository.claim.ContentClaim) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ProcessScheduler(org.apache.nifi.controller.ProcessScheduler) HashMap(java.util.HashMap) ProvenanceEventRepository(org.apache.nifi.provenance.ProvenanceEventRepository) AtomicReference(java.util.concurrent.atomic.AtomicReference) ProcessException(org.apache.nifi.processor.exception.ProcessException) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) HashSet(java.util.HashSet) StandardFlowFileQueue(org.apache.nifi.controller.StandardFlowFileQueue) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Relationship(org.apache.nifi.processor.Relationship) ResourceClaim(org.apache.nifi.controller.repository.claim.ResourceClaim) ProvenanceEventRecord(org.apache.nifi.provenance.ProvenanceEventRecord) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) Before(org.junit.Before) OutputStream(java.io.OutputStream) StandardResourceClaimManager(org.apache.nifi.controller.repository.claim.StandardResourceClaimManager) Files(java.nio.file.Files) Assert.assertNotNull(org.junit.Assert.assertNotNull) ProvenanceEventType(org.apache.nifi.provenance.ProvenanceEventType) Assert.assertTrue(org.junit.Assert.assertTrue) Matchers.notNull(org.mockito.Matchers.notNull) FileOutputStream(java.io.FileOutputStream) Mockito.times(org.mockito.Mockito.times) IOException(java.io.IOException) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) FileInputStream(java.io.FileInputStream) File(java.io.File) Mockito.verify(org.mockito.Mockito.verify) AtomicLong(java.util.concurrent.atomic.AtomicLong) ResourceClaimManager(org.apache.nifi.controller.repository.claim.ResourceClaimManager) Mockito(org.mockito.Mockito) NiFiProperties(org.apache.nifi.util.NiFiProperties) Ignore(org.junit.Ignore) Paths(java.nio.file.Paths) StreamUtils(org.apache.nifi.stream.io.StreamUtils) CoreAttributes(org.apache.nifi.flowfile.attributes.CoreAttributes) Assert(org.junit.Assert) Collections(java.util.Collections) FlowFileQueue(org.apache.nifi.controller.queue.FlowFileQueue) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) FlowFile(org.apache.nifi.flowfile.FlowFile) MockFlowFile(org.apache.nifi.util.MockFlowFile) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Test(org.junit.Test)

Example 5 with FlowFile

use of org.apache.nifi.flowfile.FlowFile in project nifi by apache.

the class TestStandardProcessSession method testCommitFailureRequeuesFlowFiles.

@Test
public void testCommitFailureRequeuesFlowFiles() {
    final FlowFileRecord flowFileRecord = new StandardFlowFileRecord.Builder().addAttribute("uuid", "12345678-1234-1234-1234-123456789012").entryDate(System.currentTimeMillis()).contentClaim(new StandardContentClaim(resourceClaimManager.newResourceClaim("x", "x", "0", true, false), 0L)).contentClaimOffset(0L).size(0L).build();
    flowFileQueue.put(flowFileRecord);
    final FlowFile originalFlowFile = session.get();
    assertTrue(flowFileQueue.isActiveQueueEmpty());
    assertEquals(1, flowFileQueue.getUnacknowledgedQueueSize().getObjectCount());
    final FlowFile modified = session.write(originalFlowFile, new OutputStreamCallback() {

        @Override
        public void process(OutputStream out) throws IOException {
            out.write("Hello".getBytes());
        }
    });
    session.transfer(modified);
    // instruct flowfile repo to throw IOException on update
    flowFileRepo.setFailOnUpdate(true);
    try {
        session.commit();
        Assert.fail("Session commit completed, even though FlowFile Repo threw IOException");
    } catch (final ProcessException pe) {
    // expected behavior because FlowFile Repo will throw IOException
    }
    assertFalse(flowFileQueue.isActiveQueueEmpty());
    assertEquals(1, flowFileQueue.size().getObjectCount());
    assertEquals(0, flowFileQueue.getUnacknowledgedQueueSize().getObjectCount());
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) MockFlowFile(org.apache.nifi.util.MockFlowFile) StandardContentClaim(org.apache.nifi.controller.repository.claim.StandardContentClaim) ProcessException(org.apache.nifi.processor.exception.ProcessException) FilterOutputStream(java.io.FilterOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) OutputStreamCallback(org.apache.nifi.processor.io.OutputStreamCallback) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

FlowFile (org.apache.nifi.flowfile.FlowFile)505 IOException (java.io.IOException)235 ProcessException (org.apache.nifi.processor.exception.ProcessException)194 HashMap (java.util.HashMap)162 InputStream (java.io.InputStream)145 OutputStream (java.io.OutputStream)131 ComponentLog (org.apache.nifi.logging.ComponentLog)119 ArrayList (java.util.ArrayList)116 Test (org.junit.Test)116 Map (java.util.Map)106 MockFlowFile (org.apache.nifi.util.MockFlowFile)103 ProcessSession (org.apache.nifi.processor.ProcessSession)99 OutputStreamCallback (org.apache.nifi.processor.io.OutputStreamCallback)83 Relationship (org.apache.nifi.processor.Relationship)80 InputStreamCallback (org.apache.nifi.processor.io.InputStreamCallback)78 HashSet (java.util.HashSet)76 List (java.util.List)69 StopWatch (org.apache.nifi.util.StopWatch)60 Set (java.util.Set)57 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)56