Search in sources :

Example 56 with Connection

use of org.apache.nifi.connectable.Connection in project nifi by apache.

the class TestStandardProcessSession method testRoundRobinOnSessionGetWithFilter.

@Test
@SuppressWarnings("unchecked")
public void testRoundRobinOnSessionGetWithFilter() {
    final List<Connection> connList = new ArrayList<>();
    final Connection conn1 = createConnection();
    final Connection conn2 = createConnection();
    connList.add(conn1);
    connList.add(conn2);
    final FlowFileRecord flowFileRecord = new StandardFlowFileRecord.Builder().id(1000L).addAttribute("uuid", "12345678-1234-1234-1234-123456789012").entryDate(System.currentTimeMillis()).build();
    flowFileQueue.put(flowFileRecord);
    flowFileQueue.put(flowFileRecord);
    when(connectable.getIncomingConnections()).thenReturn(connList);
    final FlowFileFilter filter = ff -> FlowFileFilterResult.ACCEPT_AND_TERMINATE;
    session.get(filter);
    session.get(filter);
    verify(conn1, times(1)).poll(any(FlowFileFilter.class), any(Set.class));
    verify(conn2, times(1)).poll(any(FlowFileFilter.class), any(Set.class));
}
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) Set(java.util.Set) HashSet(java.util.HashSet) Connection(org.apache.nifi.connectable.Connection) ArrayList(java.util.ArrayList) FlowFileFilter(org.apache.nifi.processor.FlowFileFilter) Test(org.junit.Test)

Example 57 with Connection

use of org.apache.nifi.connectable.Connection in project nifi by apache.

the class TestStandardProcessSession method createConnection.

private Connection createConnection() {
    AtomicReference<FlowFileQueue> queueReference = new AtomicReference<>(flowFileQueue);
    Connection connection = createConnection(queueReference);
    flowFileQueue = queueReference.get();
    return connection;
}
Also used : Connection(org.apache.nifi.connectable.Connection) AtomicReference(java.util.concurrent.atomic.AtomicReference) StandardFlowFileQueue(org.apache.nifi.controller.StandardFlowFileQueue) FlowFileQueue(org.apache.nifi.controller.queue.FlowFileQueue)

Example 58 with Connection

use of org.apache.nifi.connectable.Connection in project nifi by apache.

the class StandardProcessGroup method remove.

@Override
public void remove(final Snippet snippet) {
    writeLock.lock();
    try {
        // ensure that all components are valid
        verifyContents(snippet);
        final Set<Connectable> connectables = getAllConnectables(snippet);
        final Set<String> connectionIdsToRemove = new HashSet<>(getKeys(snippet.getConnections()));
        // Remove all connections that are the output of any Connectable.
        for (final Connectable connectable : connectables) {
            for (final Connection conn : connectable.getConnections()) {
                if (!connections.containsKey(conn.getIdentifier())) {
                    throw new IllegalStateException("Connectable component " + connectable.getIdentifier() + " cannot be removed because it has incoming connections from the parent Process Group");
                }
                connectionIdsToRemove.add(conn.getIdentifier());
            }
        }
        // verify that all connections can be removed
        for (final String id : connectionIdsToRemove) {
            connections.get(id).verifyCanDelete();
        }
        // verify that all processors are stopped and have no active threads
        for (final String procId : snippet.getProcessors().keySet()) {
            final ProcessorNode procNode = getProcessor(procId);
            if (procNode.isRunning()) {
                throw new IllegalStateException("Processor " + procNode.getIdentifier() + " cannot be removed because it is running");
            }
            final int activeThreadCount = scheduler.getActiveThreadCount(procNode);
            if (activeThreadCount != 0) {
                throw new IllegalStateException("Processor " + procNode.getIdentifier() + " cannot be removed because it still has " + activeThreadCount + " active threads");
            }
        }
        // verify that none of the connectables have incoming connections that are not in the Snippet.
        final Set<String> connectionIds = snippet.getConnections().keySet();
        for (final Connectable connectable : connectables) {
            for (final Connection conn : connectable.getIncomingConnections()) {
                if (!connectionIds.contains(conn.getIdentifier()) && !connectables.contains(conn.getSource())) {
                    throw new IllegalStateException("Connectable component " + connectable.getIdentifier() + " cannot be removed because it has incoming connections " + "that are not selected to be deleted");
                }
            }
        }
        // verify that all of the ProcessGroups in the snippet are empty
        for (final String groupId : snippet.getProcessGroups().keySet()) {
            final ProcessGroup toRemove = getProcessGroup(groupId);
            toRemove.verifyCanDelete(true);
        }
        onComponentModified();
        for (final String id : connectionIdsToRemove) {
            removeConnection(connections.get(id));
        }
        for (final String id : getKeys(snippet.getInputPorts())) {
            removeInputPort(inputPorts.get(id));
        }
        for (final String id : getKeys(snippet.getOutputPorts())) {
            removeOutputPort(outputPorts.get(id));
        }
        for (final String id : getKeys(snippet.getFunnels())) {
            removeFunnel(funnels.get(id));
        }
        for (final String id : getKeys(snippet.getLabels())) {
            removeLabel(labels.get(id));
        }
        for (final String id : getKeys(snippet.getProcessors())) {
            removeProcessor(processors.get(id));
        }
        for (final String id : getKeys(snippet.getRemoteProcessGroups())) {
            removeRemoteProcessGroup(remoteGroups.get(id));
        }
        for (final String id : getKeys(snippet.getProcessGroups())) {
            removeProcessGroup(processGroups.get(id));
        }
    } finally {
        writeLock.unlock();
    }
}
Also used : ProcessorNode(org.apache.nifi.controller.ProcessorNode) Connectable(org.apache.nifi.connectable.Connectable) Connection(org.apache.nifi.connectable.Connection) VersionedConnection(org.apache.nifi.registry.flow.VersionedConnection) VersionedProcessGroup(org.apache.nifi.registry.flow.VersionedProcessGroup) VersionedRemoteProcessGroup(org.apache.nifi.registry.flow.VersionedRemoteProcessGroup) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet)

Example 59 with Connection

use of org.apache.nifi.connectable.Connection in project nifi by apache.

the class StandardProcessGroup method removeConnection.

@Override
public void removeConnection(final Connection connectionToRemove) {
    writeLock.lock();
    try {
        // verify that Connection belongs to this group
        final Connection connection = connections.get(requireNonNull(connectionToRemove).getIdentifier());
        if (connection == null) {
            throw new IllegalStateException("Connection " + connectionToRemove.getIdentifier() + " is not a member of this Process Group");
        }
        connectionToRemove.verifyCanDelete();
        final Connectable source = connectionToRemove.getSource();
        final Connectable dest = connectionToRemove.getDestination();
        // update the source & destination
        source.removeConnection(connection);
        if (source != dest) {
            dest.removeConnection(connection);
        }
        // remove the connection from our map
        connections.remove(connection.getIdentifier());
        LOG.info("{} removed from flow", connection);
        onComponentModified();
        flowController.onConnectionRemoved(connection);
    } finally {
        writeLock.unlock();
    }
}
Also used : Connectable(org.apache.nifi.connectable.Connectable) Connection(org.apache.nifi.connectable.Connection) VersionedConnection(org.apache.nifi.registry.flow.VersionedConnection)

Example 60 with Connection

use of org.apache.nifi.connectable.Connection in project nifi by apache.

the class StandardProcessGroup method isDisconnected.

private boolean isDisconnected(final Snippet snippet) {
    final Set<Connectable> connectables = getAllConnectables(snippet);
    for (final String id : getKeys(snippet.getRemoteProcessGroups())) {
        final RemoteProcessGroup remoteGroup = getRemoteProcessGroup(id);
        connectables.addAll(remoteGroup.getInputPorts());
        connectables.addAll(remoteGroup.getOutputPorts());
    }
    final Set<String> connectionIds = snippet.getConnections().keySet();
    for (final Connectable connectable : connectables) {
        for (final Connection conn : connectable.getIncomingConnections()) {
            if (!connectionIds.contains(conn.getIdentifier())) {
                return false;
            }
        }
        for (final Connection conn : connectable.getConnections()) {
            if (!connectionIds.contains(conn.getIdentifier())) {
                return false;
            }
        }
    }
    final Set<Connectable> recursiveConnectables = new HashSet<>(connectables);
    for (final String id : snippet.getProcessGroups().keySet()) {
        final ProcessGroup childGroup = getProcessGroup(id);
        recursiveConnectables.addAll(findAllConnectables(childGroup, true));
    }
    for (final String id : connectionIds) {
        final Connection connection = getConnection(id);
        if (!recursiveConnectables.contains(connection.getSource()) || !recursiveConnectables.contains(connection.getDestination())) {
            return false;
        }
    }
    return true;
}
Also used : VersionedRemoteProcessGroup(org.apache.nifi.registry.flow.VersionedRemoteProcessGroup) Connectable(org.apache.nifi.connectable.Connectable) Connection(org.apache.nifi.connectable.Connection) VersionedConnection(org.apache.nifi.registry.flow.VersionedConnection) VersionedProcessGroup(org.apache.nifi.registry.flow.VersionedProcessGroup) VersionedRemoteProcessGroup(org.apache.nifi.registry.flow.VersionedRemoteProcessGroup) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet)

Aggregations

Connection (org.apache.nifi.connectable.Connection)95 ArrayList (java.util.ArrayList)35 HashSet (java.util.HashSet)35 VersionedConnection (org.apache.nifi.registry.flow.VersionedConnection)30 FlowFileQueue (org.apache.nifi.controller.queue.FlowFileQueue)28 Connectable (org.apache.nifi.connectable.Connectable)27 ProcessGroup (org.apache.nifi.groups.ProcessGroup)26 Relationship (org.apache.nifi.processor.Relationship)23 Port (org.apache.nifi.connectable.Port)21 RemoteProcessGroup (org.apache.nifi.groups.RemoteProcessGroup)21 ProcessorNode (org.apache.nifi.controller.ProcessorNode)19 RootGroupPort (org.apache.nifi.remote.RootGroupPort)19 LinkedHashSet (java.util.LinkedHashSet)18 Set (java.util.Set)17 RemoteGroupPort (org.apache.nifi.remote.RemoteGroupPort)17 Funnel (org.apache.nifi.connectable.Funnel)16 HashMap (java.util.HashMap)15 VersionedProcessGroup (org.apache.nifi.registry.flow.VersionedProcessGroup)15 IOException (java.io.IOException)14 Map (java.util.Map)14