Search in sources :

Example 1 with RepositoryContextFactory

use of org.apache.nifi.controller.scheduling.RepositoryContextFactory in project nifi by apache.

the class FlowController method initializeFlow.

public void initializeFlow() throws IOException {
    writeLock.lock();
    try {
        // get all connections/queues and recover from swap files.
        final List<Connection> connections = getGroup(getRootGroupId()).findAllConnections();
        long maxIdFromSwapFiles = -1L;
        if (flowFileRepository.isVolatile()) {
            for (final Connection connection : connections) {
                final FlowFileQueue queue = connection.getFlowFileQueue();
                queue.purgeSwapFiles();
            }
        } else {
            for (final Connection connection : connections) {
                final FlowFileQueue queue = connection.getFlowFileQueue();
                final SwapSummary swapSummary = queue.recoverSwappedFlowFiles();
                if (swapSummary != null) {
                    final Long maxFlowFileId = swapSummary.getMaxFlowFileId();
                    if (maxFlowFileId != null && maxFlowFileId > maxIdFromSwapFiles) {
                        maxIdFromSwapFiles = maxFlowFileId;
                    }
                    for (final ResourceClaim resourceClaim : swapSummary.getResourceClaims()) {
                        resourceClaimManager.incrementClaimantCount(resourceClaim);
                    }
                }
            }
        }
        flowFileRepository.loadFlowFiles(this, maxIdFromSwapFiles + 1);
        // Begin expiring FlowFiles that are old
        final RepositoryContextFactory contextFactory = new RepositoryContextFactory(contentRepository, flowFileRepository, flowFileEventRepository, counterRepositoryRef.get(), provenanceRepository);
        processScheduler.scheduleFrameworkTask(new ExpireFlowFiles(this, contextFactory), "Expire FlowFiles", 30L, 30L, TimeUnit.SECONDS);
        // now that we've loaded the FlowFiles, this has restored our ContentClaims' states, so we can tell the
        // ContentRepository to purge superfluous files
        contentRepository.cleanup();
        for (final RemoteSiteListener listener : externalSiteListeners) {
            listener.start();
        }
        notifyComponentsConfigurationRestored();
        timerDrivenEngineRef.get().scheduleWithFixedDelay(new Runnable() {

            @Override
            public void run() {
                try {
                    updateRemoteProcessGroups();
                } catch (final Throwable t) {
                    LOG.warn("Unable to update Remote Process Groups due to " + t);
                    if (LOG.isDebugEnabled()) {
                        LOG.warn("", t);
                    }
                }
            }
        }, 0L, 30L, TimeUnit.SECONDS);
        timerDrivenEngineRef.get().scheduleWithFixedDelay(new Runnable() {

            @Override
            public void run() {
                final ProcessGroup rootGroup = getRootGroup();
                final List<ProcessGroup> allGroups = rootGroup.findAllProcessGroups();
                allGroups.add(rootGroup);
                for (final ProcessGroup group : allGroups) {
                    try {
                        group.synchronizeWithFlowRegistry(flowRegistryClient);
                    } catch (final Exception e) {
                        LOG.error("Failed to synchronize {} with Flow Registry", group, e);
                    }
                }
            }
        }, 5, 60, TimeUnit.SECONDS);
        initialized.set(true);
    } finally {
        writeLock.unlock();
    }
}
Also used : Connection(org.apache.nifi.connectable.Connection) VersionedConnection(org.apache.nifi.registry.flow.VersionedConnection) StandardConnection(org.apache.nifi.connectable.StandardConnection) SwapSummary(org.apache.nifi.controller.repository.SwapSummary) ExpireFlowFiles(org.apache.nifi.controller.tasks.ExpireFlowFiles) FlowFileQueue(org.apache.nifi.controller.queue.FlowFileQueue) ConfigException(org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException) IOException(java.io.IOException) ProcessorInstantiationException(org.apache.nifi.controller.exception.ProcessorInstantiationException) ComponentLifeCycleException(org.apache.nifi.controller.exception.ComponentLifeCycleException) UnknownServiceAddressException(org.apache.nifi.cluster.protocol.UnknownServiceAddressException) FlowSerializationException(org.apache.nifi.controller.serialization.FlowSerializationException) ResourceNotFoundException(org.apache.nifi.web.ResourceNotFoundException) InitializationException(org.apache.nifi.reporting.InitializationException) ReportingTaskInstantiationException(org.apache.nifi.controller.reporting.ReportingTaskInstantiationException) CommunicationsException(org.apache.nifi.controller.exception.CommunicationsException) FlowSynchronizationException(org.apache.nifi.controller.serialization.FlowSynchronizationException) ControllerServiceInstantiationException(org.apache.nifi.controller.exception.ControllerServiceInstantiationException) SocketRemoteSiteListener(org.apache.nifi.remote.SocketRemoteSiteListener) RemoteSiteListener(org.apache.nifi.remote.RemoteSiteListener) HttpRemoteSiteListener(org.apache.nifi.remote.HttpRemoteSiteListener) RepositoryContextFactory(org.apache.nifi.controller.scheduling.RepositoryContextFactory) VersionedProcessGroup(org.apache.nifi.registry.flow.VersionedProcessGroup) StandardProcessGroup(org.apache.nifi.groups.StandardProcessGroup) RemoteProcessGroup(org.apache.nifi.groups.RemoteProcessGroup) ProcessGroup(org.apache.nifi.groups.ProcessGroup) StandardRemoteProcessGroup(org.apache.nifi.remote.StandardRemoteProcessGroup) ResourceClaim(org.apache.nifi.controller.repository.claim.ResourceClaim) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with RepositoryContextFactory

use of org.apache.nifi.controller.scheduling.RepositoryContextFactory in project nifi by apache.

the class TestConnectableTask method testIsWorkToDo.

@Test
public void testIsWorkToDo() {
    final ProcessorNode procNode = Mockito.mock(ProcessorNode.class);
    Mockito.when(procNode.hasIncomingConnection()).thenReturn(false);
    final Processor processor = Mockito.mock(Processor.class);
    Mockito.when(procNode.getIdentifier()).thenReturn("123");
    Mockito.when(procNode.getRunnableComponent()).thenReturn(processor);
    final FlowController flowController = Mockito.mock(FlowController.class);
    Mockito.when(flowController.getStateManagerProvider()).thenReturn(Mockito.mock(StateManagerProvider.class));
    final RepositoryContext repoContext = Mockito.mock(RepositoryContext.class);
    Mockito.when(repoContext.getFlowFileEventRepository()).thenReturn(Mockito.mock(FlowFileEventRepository.class));
    final RepositoryContextFactory contextFactory = Mockito.mock(RepositoryContextFactory.class);
    Mockito.when(contextFactory.newProcessContext(Mockito.any(Connectable.class), Mockito.any(AtomicLong.class))).thenReturn(repoContext);
    final LifecycleState scheduleState = new LifecycleState();
    final StringEncryptor encryptor = Mockito.mock(StringEncryptor.class);
    ConnectableTask task = new ConnectableTask(Mockito.mock(SchedulingAgent.class), procNode, flowController, contextFactory, scheduleState, encryptor);
    // There is work to do because there are no incoming connections.
    assertFalse(task.invoke().isYield());
    // Test with only a single connection that is self-looping and empty
    final Connection selfLoopingConnection = Mockito.mock(Connection.class);
    when(selfLoopingConnection.getSource()).thenReturn(procNode);
    when(selfLoopingConnection.getDestination()).thenReturn(procNode);
    when(procNode.hasIncomingConnection()).thenReturn(true);
    when(procNode.getIncomingConnections()).thenReturn(Collections.singletonList(selfLoopingConnection));
    assertFalse(task.invoke().isYield());
    // Test with only a single connection that is self-looping and empty
    final FlowFileQueue flowFileQueue = Mockito.mock(FlowFileQueue.class);
    when(flowFileQueue.isActiveQueueEmpty()).thenReturn(true);
    final FlowFileQueue nonEmptyQueue = Mockito.mock(FlowFileQueue.class);
    when(nonEmptyQueue.isActiveQueueEmpty()).thenReturn(false);
    when(selfLoopingConnection.getFlowFileQueue()).thenReturn(nonEmptyQueue);
    assertFalse(task.invoke().isYield());
    // Test with only a non-looping Connection that has no FlowFiles
    final Connection emptyConnection = Mockito.mock(Connection.class);
    when(emptyConnection.getSource()).thenReturn(Mockito.mock(ProcessorNode.class));
    when(emptyConnection.getDestination()).thenReturn(procNode);
    when(emptyConnection.getFlowFileQueue()).thenReturn(flowFileQueue);
    when(procNode.getIncomingConnections()).thenReturn(Collections.singletonList(emptyConnection));
    // Create a new ConnectableTask because we want to have a different value for the 'hasNonLoopConnection' value, which is calculated in the task's constructor.
    task = new ConnectableTask(Mockito.mock(SchedulingAgent.class), procNode, flowController, contextFactory, scheduleState, encryptor);
    assertTrue(task.invoke().isYield());
    // test when the queue has data
    final Connection nonEmptyConnection = Mockito.mock(Connection.class);
    when(nonEmptyConnection.getSource()).thenReturn(Mockito.mock(ProcessorNode.class));
    when(nonEmptyConnection.getDestination()).thenReturn(procNode);
    when(nonEmptyConnection.getFlowFileQueue()).thenReturn(nonEmptyQueue);
    when(procNode.getIncomingConnections()).thenReturn(Collections.singletonList(nonEmptyConnection));
    assertFalse(task.invoke().isYield());
}
Also used : RepositoryContext(org.apache.nifi.controller.repository.RepositoryContext) Processor(org.apache.nifi.processor.Processor) Connection(org.apache.nifi.connectable.Connection) LifecycleState(org.apache.nifi.controller.scheduling.LifecycleState) FlowFileQueue(org.apache.nifi.controller.queue.FlowFileQueue) AtomicLong(java.util.concurrent.atomic.AtomicLong) ProcessorNode(org.apache.nifi.controller.ProcessorNode) FlowFileEventRepository(org.apache.nifi.controller.repository.FlowFileEventRepository) RepositoryContextFactory(org.apache.nifi.controller.scheduling.RepositoryContextFactory) Connectable(org.apache.nifi.connectable.Connectable) FlowController(org.apache.nifi.controller.FlowController) StringEncryptor(org.apache.nifi.encrypt.StringEncryptor) SchedulingAgent(org.apache.nifi.controller.scheduling.SchedulingAgent) StateManagerProvider(org.apache.nifi.components.state.StateManagerProvider) Test(org.junit.Test)

Aggregations

Connection (org.apache.nifi.connectable.Connection)2 FlowFileQueue (org.apache.nifi.controller.queue.FlowFileQueue)2 RepositoryContextFactory (org.apache.nifi.controller.scheduling.RepositoryContextFactory)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 UnknownServiceAddressException (org.apache.nifi.cluster.protocol.UnknownServiceAddressException)1 StateManagerProvider (org.apache.nifi.components.state.StateManagerProvider)1 Connectable (org.apache.nifi.connectable.Connectable)1 StandardConnection (org.apache.nifi.connectable.StandardConnection)1 FlowController (org.apache.nifi.controller.FlowController)1 ProcessorNode (org.apache.nifi.controller.ProcessorNode)1 CommunicationsException (org.apache.nifi.controller.exception.CommunicationsException)1 ComponentLifeCycleException (org.apache.nifi.controller.exception.ComponentLifeCycleException)1 ControllerServiceInstantiationException (org.apache.nifi.controller.exception.ControllerServiceInstantiationException)1 ProcessorInstantiationException (org.apache.nifi.controller.exception.ProcessorInstantiationException)1 ReportingTaskInstantiationException (org.apache.nifi.controller.reporting.ReportingTaskInstantiationException)1 FlowFileEventRepository (org.apache.nifi.controller.repository.FlowFileEventRepository)1 RepositoryContext (org.apache.nifi.controller.repository.RepositoryContext)1