Search in sources :

Example 1 with FlowEngine

use of org.apache.nifi.engine.FlowEngine in project nifi by apache.

the class TestStandardProcessScheduler method setup.

@Before
public void setup() throws InitializationException {
    final Map<String, String> overrideProperties = new HashMap<>();
    overrideProperties.put(NiFiProperties.ADMINISTRATIVE_YIELD_DURATION, "2 millis");
    overrideProperties.put(NiFiProperties.PROCESSOR_SCHEDULING_TIMEOUT, "10 millis");
    this.nifiProperties = NiFiProperties.createBasicNiFiProperties(propsFile, overrideProperties);
    // load the system bundle
    systemBundle = SystemBundle.create(nifiProperties);
    ExtensionManager.discoverExtensions(systemBundle, Collections.emptySet());
    scheduler = new StandardProcessScheduler(new FlowEngine(1, "Unit Test", true), Mockito.mock(FlowController.class), null, stateMgrProvider, nifiProperties);
    scheduler.setSchedulingAgent(SchedulingStrategy.TIMER_DRIVEN, Mockito.mock(SchedulingAgent.class));
    reportingTask = new TestReportingTask();
    final ReportingInitializationContext config = new StandardReportingInitializationContext(UUID.randomUUID().toString(), "Test", SchedulingStrategy.TIMER_DRIVEN, "5 secs", Mockito.mock(ComponentLog.class), null, nifiProperties, null);
    reportingTask.initialize(config);
    final ValidationContextFactory validationContextFactory = new StandardValidationContextFactory(null, variableRegistry);
    final TerminationAwareLogger logger = Mockito.mock(TerminationAwareLogger.class);
    final ReloadComponent reloadComponent = Mockito.mock(ReloadComponent.class);
    final LoggableComponent<ReportingTask> loggableComponent = new LoggableComponent<>(reportingTask, systemBundle.getBundleDetails().getCoordinate(), logger);
    taskNode = new StandardReportingTaskNode(loggableComponent, UUID.randomUUID().toString(), null, scheduler, validationContextFactory, new StandardComponentVariableRegistry(variableRegistry), reloadComponent);
    controller = Mockito.mock(FlowController.class);
    final ConcurrentMap<String, ProcessorNode> processorMap = new ConcurrentHashMap<>();
    Mockito.doAnswer(new Answer<ProcessorNode>() {

        @Override
        public ProcessorNode answer(InvocationOnMock invocation) throws Throwable {
            final String id = invocation.getArgumentAt(0, String.class);
            return processorMap.get(id);
        }
    }).when(controller).getProcessorNode(Mockito.anyString());
    Mockito.doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            final ProcessorNode procNode = invocation.getArgumentAt(0, ProcessorNode.class);
            processorMap.putIfAbsent(procNode.getIdentifier(), procNode);
            return null;
        }
    }).when(controller).onProcessorAdded(Mockito.any(ProcessorNode.class));
    rootGroup = new MockProcessGroup(controller);
    Mockito.when(controller.getGroup(Mockito.anyString())).thenReturn(rootGroup);
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) StandardReportingInitializationContext(org.apache.nifi.controller.reporting.StandardReportingInitializationContext) ValidationContextFactory(org.apache.nifi.controller.ValidationContextFactory) StandardValidationContextFactory(org.apache.nifi.processor.StandardValidationContextFactory) StandardValidationContextFactory(org.apache.nifi.processor.StandardValidationContextFactory) MockProcessGroup(org.apache.nifi.controller.service.mock.MockProcessGroup) StandardProcessorNode(org.apache.nifi.controller.StandardProcessorNode) ProcessorNode(org.apache.nifi.controller.ProcessorNode) ReportingInitializationContext(org.apache.nifi.reporting.ReportingInitializationContext) StandardReportingInitializationContext(org.apache.nifi.controller.reporting.StandardReportingInitializationContext) StandardReportingTaskNode(org.apache.nifi.controller.reporting.StandardReportingTaskNode) FlowController(org.apache.nifi.controller.FlowController) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) AbstractReportingTask(org.apache.nifi.reporting.AbstractReportingTask) ReportingTask(org.apache.nifi.reporting.ReportingTask) StandardComponentVariableRegistry(org.apache.nifi.registry.variable.StandardComponentVariableRegistry) FlowEngine(org.apache.nifi.engine.FlowEngine) ReloadComponent(org.apache.nifi.controller.ReloadComponent) ComponentLog(org.apache.nifi.logging.ComponentLog) LoggableComponent(org.apache.nifi.controller.LoggableComponent) InvocationOnMock(org.mockito.invocation.InvocationOnMock) TerminationAwareLogger(org.apache.nifi.controller.TerminationAwareLogger) Before(org.junit.Before)

Example 2 with FlowEngine

use of org.apache.nifi.engine.FlowEngine in project nifi by apache.

the class StandardFlowService method start.

@Override
public void start() throws LifeCycleStartException {
    writeLock.lock();
    try {
        if (isRunning()) {
            return;
        }
        running.set(true);
        final ScheduledExecutorService newExecutor = new FlowEngine(2, "Flow Service Tasks");
        newExecutor.scheduleWithFixedDelay(new SaveReportingTask(), 0L, 500L, TimeUnit.MILLISECONDS);
        this.executor.set(newExecutor);
        if (configuredForClustering) {
            senderListener.start();
        }
    } catch (final IOException ioe) {
        try {
            stop(/* force */
            true);
        } catch (final Exception e) {
        }
        throw new LifeCycleStartException("Failed to start Flow Service due to: " + ioe, ioe);
    } finally {
        writeLock.unlock();
    }
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) FlowEngine(org.apache.nifi.engine.FlowEngine) LifeCycleStartException(org.apache.nifi.lifecycle.LifeCycleStartException) IOException(java.io.IOException) FlowSerializationException(org.apache.nifi.controller.serialization.FlowSerializationException) ConnectionException(org.apache.nifi.cluster.ConnectionException) FlowSynchronizationException(org.apache.nifi.controller.serialization.FlowSynchronizationException) LifeCycleStartException(org.apache.nifi.lifecycle.LifeCycleStartException) NoClusterCoordinatorException(org.apache.nifi.cluster.exception.NoClusterCoordinatorException) IOException(java.io.IOException) ProtocolException(org.apache.nifi.cluster.protocol.ProtocolException)

Example 3 with FlowEngine

use of org.apache.nifi.engine.FlowEngine 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 FlowEngine

use of org.apache.nifi.engine.FlowEngine in project nifi by apache.

the class StandardProcessSchedulerIT method validateLongEnablingServiceCanStillBeDisabled.

/**
 * Validates that the service that is currently in ENABLING state can be
 * disabled and that its @OnDisabled operation will be invoked as soon as
 *
 * @OnEnable finishes.
 */
@Test
public void validateLongEnablingServiceCanStillBeDisabled() throws Exception {
    final StandardProcessScheduler scheduler = new StandardProcessScheduler(new FlowEngine(1, "Unit Test", true), null, null, stateMgrProvider, nifiProperties);
    final StandardControllerServiceProvider provider = new StandardControllerServiceProvider(controller, scheduler, null, stateMgrProvider, variableRegistry, nifiProperties);
    final ControllerServiceNode serviceNode = provider.createControllerService(LongEnablingService.class.getName(), "1", systemBundle.getBundleDetails().getCoordinate(), null, false);
    final LongEnablingService ts = (LongEnablingService) serviceNode.getControllerServiceImplementation();
    ts.setLimit(3000);
    scheduler.enableControllerService(serviceNode);
    Thread.sleep(2000);
    assertTrue(serviceNode.isActive());
    assertEquals(1, ts.enableInvocationCount());
    Thread.sleep(500);
    scheduler.disableControllerService(serviceNode);
    assertFalse(serviceNode.isActive());
    assertEquals(ControllerServiceState.DISABLING, serviceNode.getState());
    assertEquals(0, ts.disableInvocationCount());
    // wait a bit. . . Enabling will finish and @OnDisabled will be invoked
    // automatically
    Thread.sleep(4000);
    assertEquals(ControllerServiceState.DISABLED, serviceNode.getState());
    assertEquals(1, ts.disableInvocationCount());
}
Also used : ControllerServiceNode(org.apache.nifi.controller.service.ControllerServiceNode) FlowEngine(org.apache.nifi.engine.FlowEngine) StandardControllerServiceProvider(org.apache.nifi.controller.service.StandardControllerServiceProvider) Test(org.junit.Test)

Aggregations

FlowEngine (org.apache.nifi.engine.FlowEngine)4 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)2 StandardComponentVariableRegistry (org.apache.nifi.registry.variable.StandardComponentVariableRegistry)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Callable (java.util.concurrent.Callable)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 BundleCoordinate (org.apache.nifi.bundle.BundleCoordinate)1 ConnectionException (org.apache.nifi.cluster.ConnectionException)1 NoClusterCoordinatorException (org.apache.nifi.cluster.exception.NoClusterCoordinatorException)1 ProtocolException (org.apache.nifi.cluster.protocol.ProtocolException)1 FlowController (org.apache.nifi.controller.FlowController)1 LoggableComponent (org.apache.nifi.controller.LoggableComponent)1 ProcessorNode (org.apache.nifi.controller.ProcessorNode)1 ReloadComponent (org.apache.nifi.controller.ReloadComponent)1 StandardProcessorNode (org.apache.nifi.controller.StandardProcessorNode)1 TerminationAwareLogger (org.apache.nifi.controller.TerminationAwareLogger)1 ValidationContextFactory (org.apache.nifi.controller.ValidationContextFactory)1 StandardReportingInitializationContext (org.apache.nifi.controller.reporting.StandardReportingInitializationContext)1