Search in sources :

Example 1 with StandardProcessContext

use of org.apache.nifi.processor.StandardProcessContext in project nifi by apache.

the class StandardProcessGroup method shutdown.

private void shutdown(final ProcessGroup procGroup) {
    for (final ProcessorNode node : procGroup.getProcessors()) {
        try (final NarCloseable x = NarCloseable.withComponentNarLoader(node.getProcessor().getClass(), node.getIdentifier())) {
            final StandardProcessContext processContext = new StandardProcessContext(node, controllerServiceProvider, encryptor, getStateManager(node.getIdentifier()), () -> false);
            ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnShutdown.class, node.getProcessor(), processContext);
        }
    }
    for (final RemoteProcessGroup rpg : procGroup.getRemoteProcessGroups()) {
        rpg.shutdown();
    }
    // Recursively shutdown child groups.
    for (final ProcessGroup group : procGroup.getProcessGroups()) {
        shutdown(group);
    }
}
Also used : NarCloseable(org.apache.nifi.nar.NarCloseable) VersionedRemoteProcessGroup(org.apache.nifi.registry.flow.VersionedRemoteProcessGroup) ProcessorNode(org.apache.nifi.controller.ProcessorNode) VersionedProcessGroup(org.apache.nifi.registry.flow.VersionedProcessGroup) VersionedRemoteProcessGroup(org.apache.nifi.registry.flow.VersionedRemoteProcessGroup) StandardProcessContext(org.apache.nifi.processor.StandardProcessContext)

Example 2 with StandardProcessContext

use of org.apache.nifi.processor.StandardProcessContext in project nifi by apache.

the class StandardProcessScheduler method startProcessor.

/**
 * Starts the given {@link Processor} by invoking its
 * {@link ProcessorNode#start(ScheduledExecutorService, long, org.apache.nifi.processor.ProcessContext, Runnable)}
 * method.
 *
 * @see StandardProcessorNode#start(ScheduledExecutorService, long, org.apache.nifi.processor.ProcessContext, Runnable)
 */
@Override
public synchronized CompletableFuture<Void> startProcessor(final ProcessorNode procNode, final boolean failIfStopping) {
    final LifecycleState lifecycleState = getLifecycleState(requireNonNull(procNode), true);
    final StandardProcessContext processContext = new StandardProcessContext(procNode, this.controllerServiceProvider, this.encryptor, getStateManager(procNode.getIdentifier()), lifecycleState::isTerminated);
    final CompletableFuture<Void> future = new CompletableFuture<>();
    final SchedulingAgentCallback callback = new SchedulingAgentCallback() {

        @Override
        public void trigger() {
            lifecycleState.clearTerminationFlag();
            getSchedulingAgent(procNode).schedule(procNode, lifecycleState);
            future.complete(null);
        }

        @Override
        public Future<?> scheduleTask(final Callable<?> task) {
            lifecycleState.incrementActiveThreadCount(null);
            return componentMonitoringThreadPool.submit(task);
        }

        @Override
        public void onTaskComplete() {
            lifecycleState.decrementActiveThreadCount(null);
        }
    };
    LOG.info("Starting {}", procNode);
    procNode.start(this.componentMonitoringThreadPool, this.administrativeYieldMillis, processContext, callback, failIfStopping);
    return future;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) SchedulingAgentCallback(org.apache.nifi.controller.SchedulingAgentCallback) StandardProcessContext(org.apache.nifi.processor.StandardProcessContext) Callable(java.util.concurrent.Callable)

Example 3 with StandardProcessContext

use of org.apache.nifi.processor.StandardProcessContext in project nifi by apache.

the class TestStandardProcessorNode method testStart.

@Test(timeout = 10000)
public void testStart() throws InterruptedException {
    final ProcessorThatThrowsExceptionOnScheduled processor = new ProcessorThatThrowsExceptionOnScheduled();
    final String uuid = UUID.randomUUID().toString();
    ProcessorInitializationContext initContext = new StandardProcessorInitializationContext(uuid, null, null, null, null);
    processor.initialize(initContext);
    final ReloadComponent reloadComponent = Mockito.mock(ReloadComponent.class);
    final BundleCoordinate coordinate = Mockito.mock(BundleCoordinate.class);
    final LoggableComponent<Processor> loggableComponent = new LoggableComponent<>(processor, coordinate, null);
    final StandardProcessorNode procNode = new StandardProcessorNode(loggableComponent, uuid, createValidationContextFactory(), null, null, NiFiProperties.createBasicNiFiProperties(null, null), new StandardComponentVariableRegistry(VariableRegistry.EMPTY_REGISTRY), reloadComponent);
    final ScheduledExecutorService taskScheduler = new FlowEngine(1, "TestClasspathResources", true);
    final StandardProcessContext processContext = new StandardProcessContext(procNode, null, null, null, () -> false);
    final SchedulingAgentCallback schedulingAgentCallback = new SchedulingAgentCallback() {

        @Override
        public void onTaskComplete() {
        }

        @Override
        public Future<?> scheduleTask(final Callable<?> task) {
            return taskScheduler.submit(task);
        }

        @Override
        public void trigger() {
            Assert.fail("Should not have completed");
        }
    };
    procNode.start(taskScheduler, 20000L, processContext, schedulingAgentCallback, true);
    Thread.sleep(1000L);
    assertEquals(1, processor.onScheduledCount);
    assertEquals(1, processor.onUnscheduledCount);
    assertEquals(1, processor.onStoppedCount);
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ModifiesClasspathNoAnnotationProcessor(org.apache.nifi.test.processors.ModifiesClasspathNoAnnotationProcessor) Processor(org.apache.nifi.processor.Processor) ModifiesClasspathProcessor(org.apache.nifi.test.processors.ModifiesClasspathProcessor) AbstractProcessor(org.apache.nifi.processor.AbstractProcessor) StandardComponentVariableRegistry(org.apache.nifi.registry.variable.StandardComponentVariableRegistry) FlowEngine(org.apache.nifi.engine.FlowEngine) StandardProcessorInitializationContext(org.apache.nifi.processor.StandardProcessorInitializationContext) BundleCoordinate(org.apache.nifi.bundle.BundleCoordinate) ProcessorInitializationContext(org.apache.nifi.processor.ProcessorInitializationContext) StandardProcessorInitializationContext(org.apache.nifi.processor.StandardProcessorInitializationContext) Callable(java.util.concurrent.Callable) StandardProcessContext(org.apache.nifi.processor.StandardProcessContext) Test(org.junit.Test)

Example 4 with StandardProcessContext

use of org.apache.nifi.processor.StandardProcessContext in project nifi by apache.

the class StandardProcessGroup method removeProcessor.

@Override
public void removeProcessor(final ProcessorNode processor) {
    boolean removed = false;
    final String id = requireNonNull(processor).getIdentifier();
    writeLock.lock();
    try {
        if (!processors.containsKey(id)) {
            throw new IllegalStateException(processor.getIdentifier() + " is not a member of this Process Group");
        }
        processor.verifyCanDelete();
        for (final Connection conn : processor.getConnections()) {
            conn.verifyCanDelete();
        }
        try (final NarCloseable x = NarCloseable.withComponentNarLoader(processor.getProcessor().getClass(), processor.getIdentifier())) {
            final StandardProcessContext processContext = new StandardProcessContext(processor, controllerServiceProvider, encryptor, getStateManager(processor.getIdentifier()), () -> false);
            ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnRemoved.class, processor.getProcessor(), processContext);
        } catch (final Exception e) {
            throw new ComponentLifeCycleException("Failed to invoke 'OnRemoved' methods of processor with id " + processor.getIdentifier(), e);
        }
        for (final Map.Entry<PropertyDescriptor, String> entry : processor.getProperties().entrySet()) {
            final PropertyDescriptor descriptor = entry.getKey();
            if (descriptor.getControllerServiceDefinition() != null) {
                final String value = entry.getValue() == null ? descriptor.getDefaultValue() : entry.getValue();
                if (value != null) {
                    final ControllerServiceNode serviceNode = controllerServiceProvider.getControllerServiceNode(value);
                    if (serviceNode != null) {
                        serviceNode.removeReference(processor);
                    }
                }
            }
        }
        processors.remove(id);
        onComponentModified();
        flowController.onProcessorRemoved(processor);
        LogRepositoryFactory.getRepository(processor.getIdentifier()).removeAllObservers();
        final StateManagerProvider stateManagerProvider = flowController.getStateManagerProvider();
        scheduler.submitFrameworkTask(new Runnable() {

            @Override
            public void run() {
                stateManagerProvider.onComponentRemoved(processor.getIdentifier());
            }
        });
        // must copy to avoid a concurrent modification
        final Set<Connection> copy = new HashSet<>(processor.getConnections());
        for (final Connection conn : copy) {
            removeConnection(conn);
        }
        removed = true;
        LOG.info("{} removed from flow", processor);
    } finally {
        if (removed) {
            try {
                ExtensionManager.removeInstanceClassLoader(id);
            } catch (Throwable t) {
            }
        }
        writeLock.unlock();
    }
}
Also used : NarCloseable(org.apache.nifi.nar.NarCloseable) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) VersionedPropertyDescriptor(org.apache.nifi.registry.flow.VersionedPropertyDescriptor) Connection(org.apache.nifi.connectable.Connection) VersionedConnection(org.apache.nifi.registry.flow.VersionedConnection) ComponentLifeCycleException(org.apache.nifi.controller.exception.ComponentLifeCycleException) NiFiRegistryException(org.apache.nifi.registry.client.NiFiRegistryException) IOException(java.io.IOException) ProcessorInstantiationException(org.apache.nifi.controller.exception.ProcessorInstantiationException) ComponentLifeCycleException(org.apache.nifi.controller.exception.ComponentLifeCycleException) ControllerServiceNode(org.apache.nifi.controller.service.ControllerServiceNode) StandardProcessContext(org.apache.nifi.processor.StandardProcessContext) Map(java.util.Map) HashMap(java.util.HashMap) StateManagerProvider(org.apache.nifi.components.state.StateManagerProvider) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet)

Example 5 with StandardProcessContext

use of org.apache.nifi.processor.StandardProcessContext in project nifi by apache.

the class StandardProcessScheduler method stopProcessor.

/**
 * Stops the given {@link Processor} by invoking its
 * {@link ProcessorNode#stop(ScheduledExecutorService, org.apache.nifi.processor.ProcessContext, SchedulingAgent, LifecycleState)}
 * method.
 *
 * @see StandardProcessorNode#stop(ScheduledExecutorService, org.apache.nifi.processor.ProcessContext, SchedulingAgent, LifecycleState)
 */
@Override
public synchronized CompletableFuture<Void> stopProcessor(final ProcessorNode procNode) {
    final LifecycleState lifecycleState = getLifecycleState(procNode, false);
    StandardProcessContext processContext = new StandardProcessContext(procNode, this.controllerServiceProvider, this.encryptor, getStateManager(procNode.getIdentifier()), lifecycleState::isTerminated);
    LOG.info("Stopping {}", procNode);
    return procNode.stop(this, this.componentLifeCycleThreadPool, processContext, getSchedulingAgent(procNode), lifecycleState);
}
Also used : StandardProcessContext(org.apache.nifi.processor.StandardProcessContext)

Aggregations

StandardProcessContext (org.apache.nifi.processor.StandardProcessContext)6 NarCloseable (org.apache.nifi.nar.NarCloseable)3 Callable (java.util.concurrent.Callable)2 Processor (org.apache.nifi.processor.Processor)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 Map (java.util.Map)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 BundleCoordinate (org.apache.nifi.bundle.BundleCoordinate)1 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)1 StateManager (org.apache.nifi.components.state.StateManager)1 StateManagerProvider (org.apache.nifi.components.state.StateManagerProvider)1 Connection (org.apache.nifi.connectable.Connection)1 ProcessorNode (org.apache.nifi.controller.ProcessorNode)1 SchedulingAgentCallback (org.apache.nifi.controller.SchedulingAgentCallback)1 ComponentLifeCycleException (org.apache.nifi.controller.exception.ComponentLifeCycleException)1 ProcessorInstantiationException (org.apache.nifi.controller.exception.ProcessorInstantiationException)1