Search in sources :

Example 26 with FlowFile

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

the class TestStandardProcessSession method testProvenanceEventsEmittedForRemove.

@Test
public void testProvenanceEventsEmittedForRemove() throws IOException {
    final FlowFileRecord flowFileRecord = new StandardFlowFileRecord.Builder().addAttribute("uuid", "12345678-1234-1234-1234-123456789012").entryDate(System.currentTimeMillis()).build();
    flowFileQueue.put(flowFileRecord);
    final FlowFile orig = session.get();
    final FlowFile newFlowFile = session.create(orig);
    final FlowFile secondNewFlowFile = session.create(orig);
    session.remove(newFlowFile);
    session.transfer(secondNewFlowFile, new Relationship.Builder().name("A").build());
    session.commit();
    assertEquals(1, provenanceRepo.getEvents(0L, 100000).size());
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) MockFlowFile(org.apache.nifi.util.MockFlowFile) Relationship(org.apache.nifi.processor.Relationship) Test(org.junit.Test)

Example 27 with FlowFile

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

the class TestStandardProcessSession method testAppendToChildThrowsIOExceptionThenRemove.

@Test
public void testAppendToChildThrowsIOExceptionThenRemove() throws IOException {
    final FlowFileRecord flowFileRecord = new StandardFlowFileRecord.Builder().id(1000L).addAttribute("uuid", "12345678-1234-1234-1234-123456789012").entryDate(System.currentTimeMillis()).build();
    flowFileQueue.put(flowFileRecord);
    FlowFile original = session.get();
    assertNotNull(original);
    FlowFile child = session.create(original);
    child = session.append(child, out -> out.write("hello".getBytes()));
    // Force an IOException. This will decrement out claim count for the resource claim.
    try {
        child = session.append(child, out -> {
            throw new IOException();
        });
        Assert.fail("append() callback threw IOException but it was not wrapped in ProcessException");
    } catch (final ProcessException pe) {
    // expected
    }
    session.remove(child);
    session.transfer(original);
    session.commit();
    final int numClaims = contentRepo.getExistingClaims().size();
    assertEquals(0, numClaims);
}
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) ProcessException(org.apache.nifi.processor.exception.ProcessException) IOException(java.io.IOException) Test(org.junit.Test)

Example 28 with FlowFile

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

the class TestStandardPropertyValue method testFlowFileEntryYear.

@Test
public void testFlowFileEntryYear() {
    final Calendar now = Calendar.getInstance();
    final int year = now.get(Calendar.YEAR);
    final PropertyValue value = new StandardPropertyValue("${entryDate:toNumber():toDate():format('yyyy')}", lookup);
    final FlowFile flowFile = new StandardFlowFileRecord.Builder().entryDate(now.getTimeInMillis()).build();
    final int val = value.evaluateAttributeExpressions(flowFile).asInteger().intValue();
    assertEquals(year, val);
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) Calendar(java.util.Calendar) StandardPropertyValue(org.apache.nifi.attribute.expression.language.StandardPropertyValue) StandardPropertyValue(org.apache.nifi.attribute.expression.language.StandardPropertyValue) PropertyValue(org.apache.nifi.components.PropertyValue) Test(org.junit.Test)

Example 29 with FlowFile

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

the class TestStandardPropertyValue method testFileSize.

@Test
public void testFileSize() {
    final PropertyValue value = new StandardPropertyValue("${fileSize}", lookup);
    final FlowFile flowFile = new StandardFlowFileRecord.Builder().size(1024 * 1024L).build();
    final long val = value.evaluateAttributeExpressions(flowFile).asLong().longValue();
    assertEquals(1024 * 1024L, val);
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) StandardPropertyValue(org.apache.nifi.attribute.expression.language.StandardPropertyValue) StandardPropertyValue(org.apache.nifi.attribute.expression.language.StandardPropertyValue) PropertyValue(org.apache.nifi.components.PropertyValue) Test(org.junit.Test)

Example 30 with FlowFile

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

the class StandardRemoteGroupPort method onTrigger.

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) {
    if (!remoteGroup.isTransmitting()) {
        logger.debug("{} {} is not transmitting; will not send/receive", this, remoteGroup);
        return;
    }
    if (getConnectableType() == ConnectableType.REMOTE_INPUT_PORT && session.getQueueSize().getObjectCount() == 0) {
        logger.debug("{} No data to send", this);
        return;
    }
    final String url = getRemoteProcessGroup().getTargetUri();
    // If we are sending data, we need to ensure that we have at least 1 FlowFile to send. Otherwise,
    // we don't want to create a transaction at all.
    final FlowFile firstFlowFile;
    if (getConnectableType() == ConnectableType.REMOTE_INPUT_PORT) {
        firstFlowFile = session.get();
        if (firstFlowFile == null) {
            return;
        }
    } else {
        firstFlowFile = null;
    }
    final SiteToSiteClient client = getSiteToSiteClient();
    final Transaction transaction;
    try {
        transaction = client.createTransaction(transferDirection);
    } catch (final PortNotRunningException e) {
        context.yield();
        this.targetRunning.set(false);
        final String message = String.format("%s failed to communicate with %s because the remote instance indicates that the port is not in a valid state", this, url);
        logger.error(message);
        session.rollback();
        remoteGroup.getEventReporter().reportEvent(Severity.ERROR, CATEGORY, message);
        return;
    } catch (final UnknownPortException e) {
        context.yield();
        this.targetExists.set(false);
        final String message = String.format("%s failed to communicate with %s because the remote instance indicates that the port no longer exists", this, url);
        logger.error(message);
        session.rollback();
        remoteGroup.getEventReporter().reportEvent(Severity.ERROR, CATEGORY, message);
        return;
    } catch (final UnreachableClusterException e) {
        context.yield();
        final String message = String.format("%s failed to communicate with %s due to %s", this, url, e.toString());
        logger.error(message);
        session.rollback();
        remoteGroup.getEventReporter().reportEvent(Severity.ERROR, CATEGORY, message);
        return;
    } catch (final IOException e) {
        // we do not yield here because the 'peer' will be penalized, and we won't communicate with that particular nifi instance
        // for a while due to penalization, but we can continue to talk to other nifi instances
        final String message = String.format("%s failed to communicate with %s due to %s", this, url, e.toString());
        logger.error(message);
        if (logger.isDebugEnabled()) {
            logger.error("", e);
        }
        session.rollback();
        remoteGroup.getEventReporter().reportEvent(Severity.ERROR, CATEGORY, message);
        return;
    }
    if (transaction == null) {
        logger.debug("{} Unable to create transaction to communicate with; all peers must be penalized, so yielding context", this);
        session.rollback();
        context.yield();
        return;
    }
    try {
        if (getConnectableType() == ConnectableType.REMOTE_INPUT_PORT) {
            transferFlowFiles(transaction, context, session, firstFlowFile);
        } else {
            final int numReceived = receiveFlowFiles(transaction, context, session);
            if (numReceived == 0) {
                context.yield();
            }
        }
        session.commit();
    } catch (final Throwable t) {
        final String message = String.format("%s failed to communicate with remote NiFi instance due to %s", this, t.toString());
        logger.error("{} failed to communicate with remote NiFi instance due to {}", this, t.toString());
        if (logger.isDebugEnabled()) {
            logger.error("", t);
        }
        remoteGroup.getEventReporter().reportEvent(Severity.ERROR, CATEGORY, message);
        transaction.error();
        session.rollback();
    }
}
Also used : SiteToSiteClient(org.apache.nifi.remote.client.SiteToSiteClient) FlowFile(org.apache.nifi.flowfile.FlowFile) UnknownPortException(org.apache.nifi.remote.exception.UnknownPortException) PortNotRunningException(org.apache.nifi.remote.exception.PortNotRunningException) IOException(java.io.IOException) UnreachableClusterException(org.apache.nifi.remote.exception.UnreachableClusterException)

Aggregations

FlowFile (org.apache.nifi.flowfile.FlowFile)500 IOException (java.io.IOException)236 ProcessException (org.apache.nifi.processor.exception.ProcessException)193 HashMap (java.util.HashMap)160 InputStream (java.io.InputStream)145 OutputStream (java.io.OutputStream)131 ComponentLog (org.apache.nifi.logging.ComponentLog)119 Test (org.junit.Test)116 ArrayList (java.util.ArrayList)113 Map (java.util.Map)105 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)78 InputStreamCallback (org.apache.nifi.processor.io.InputStreamCallback)78 HashSet (java.util.HashSet)75 List (java.util.List)67 StopWatch (org.apache.nifi.util.StopWatch)59 Set (java.util.Set)56 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)55