Search in sources :

Example 1 with StandardControllerServiceProvider

use of org.apache.nifi.controller.service.StandardControllerServiceProvider in project nifi by apache.

the class TestStandardProcessScheduler method validateDisablingOfTheFailedService.

@Test
public void validateDisablingOfTheFailedService() throws Exception {
    final StandardProcessScheduler scheduler = createScheduler();
    final StandardControllerServiceProvider provider = new StandardControllerServiceProvider(controller, scheduler, null, stateMgrProvider, variableRegistry, nifiProperties);
    final ControllerServiceNode serviceNode = provider.createControllerService(FailingService.class.getName(), "1", systemBundle.getBundleDetails().getCoordinate(), null, false);
    scheduler.enableControllerService(serviceNode);
    Thread.sleep(1000);
    scheduler.shutdown();
    /*
         * Because it was never disabled it will remain active since its
         * enabling is being retried. This may actually be a bug in the
         * scheduler since it probably has to shut down all components (disable
         * services, shut down processors etc) before shutting down itself
         */
    assertTrue(serviceNode.isActive());
    assertTrue(serviceNode.getState() == ControllerServiceState.ENABLING);
}
Also used : ControllerServiceNode(org.apache.nifi.controller.service.ControllerServiceNode) StandardControllerServiceNode(org.apache.nifi.controller.service.StandardControllerServiceNode) StandardControllerServiceProvider(org.apache.nifi.controller.service.StandardControllerServiceProvider) Test(org.junit.Test)

Example 2 with StandardControllerServiceProvider

use of org.apache.nifi.controller.service.StandardControllerServiceProvider in project nifi by apache.

the class TestStandardProcessScheduler method validateDisabledServiceCantBeDisabled.

/**
 * Validates the atomic nature of ControllerServiceNode.disable(..) method
 * which must never trigger @OnDisabled, regardless of how many threads may
 * have a reference to the underlying ProcessScheduler and
 * ControllerServiceNode.
 */
@Test
public void validateDisabledServiceCantBeDisabled() throws Exception {
    final StandardProcessScheduler scheduler = createScheduler();
    final StandardControllerServiceProvider provider = new StandardControllerServiceProvider(controller, scheduler, null, stateMgrProvider, variableRegistry, nifiProperties);
    final ControllerServiceNode serviceNode = provider.createControllerService(SimpleTestService.class.getName(), "1", systemBundle.getBundleDetails().getCoordinate(), null, false);
    final SimpleTestService ts = (SimpleTestService) serviceNode.getControllerServiceImplementation();
    final ExecutorService executor = Executors.newCachedThreadPool();
    final AtomicBoolean asyncFailed = new AtomicBoolean();
    for (int i = 0; i < 1000; i++) {
        executor.execute(new Runnable() {

            @Override
            public void run() {
                try {
                    scheduler.disableControllerService(serviceNode);
                    assertFalse(serviceNode.isActive());
                } catch (final Exception e) {
                    e.printStackTrace();
                    asyncFailed.set(true);
                }
            }
        });
    }
    // need to sleep a while since we are emulating async invocations on
    // method that is also internally async
    Thread.sleep(500);
    executor.shutdown();
    assertFalse(asyncFailed.get());
    assertEquals(0, ts.disableInvocationCount());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ControllerServiceNode(org.apache.nifi.controller.service.ControllerServiceNode) StandardControllerServiceNode(org.apache.nifi.controller.service.StandardControllerServiceNode) StandardControllerServiceProvider(org.apache.nifi.controller.service.StandardControllerServiceProvider) ExecutorService(java.util.concurrent.ExecutorService) InitializationException(org.apache.nifi.reporting.InitializationException) ProcessException(org.apache.nifi.processor.exception.ProcessException) Test(org.junit.Test)

Example 3 with StandardControllerServiceProvider

use of org.apache.nifi.controller.service.StandardControllerServiceProvider in project nifi by apache.

the class TestStandardProcessScheduler method testDisableControllerServiceWithProcessorTryingToStartUsingIt.

@Test(timeout = 60000)
public void testDisableControllerServiceWithProcessorTryingToStartUsingIt() throws InterruptedException {
    final String uuid = UUID.randomUUID().toString();
    final Processor proc = new ServiceReferencingProcessor();
    proc.initialize(new StandardProcessorInitializationContext(uuid, null, null, null, null));
    final ReloadComponent reloadComponent = Mockito.mock(ReloadComponent.class);
    final StandardControllerServiceProvider serviceProvider = new StandardControllerServiceProvider(controller, scheduler, null, Mockito.mock(StateManagerProvider.class), variableRegistry, nifiProperties);
    final ControllerServiceNode service = serviceProvider.createControllerService(NoStartServiceImpl.class.getName(), "service", systemBundle.getBundleDetails().getCoordinate(), null, true);
    rootGroup.addControllerService(service);
    final LoggableComponent<Processor> loggableComponent = new LoggableComponent<>(proc, systemBundle.getBundleDetails().getCoordinate(), null);
    final ProcessorNode procNode = new StandardProcessorNode(loggableComponent, uuid, new StandardValidationContextFactory(serviceProvider, variableRegistry), scheduler, serviceProvider, nifiProperties, new StandardComponentVariableRegistry(VariableRegistry.EMPTY_REGISTRY), reloadComponent);
    rootGroup.addProcessor(procNode);
    Map<String, String> procProps = new HashMap<>();
    procProps.put(ServiceReferencingProcessor.SERVICE_DESC.getName(), service.getIdentifier());
    procNode.setProperties(procProps);
    scheduler.enableControllerService(service);
    scheduler.startProcessor(procNode, true);
    Thread.sleep(25L);
    scheduler.stopProcessor(procNode);
    assertTrue(service.isActive());
    assertTrue(service.getState() == ControllerServiceState.ENABLING);
    scheduler.disableControllerService(service);
    assertTrue(service.getState() == ControllerServiceState.DISABLING);
    assertFalse(service.isActive());
    while (service.getState() != ControllerServiceState.DISABLED) {
        Thread.sleep(5L);
    }
    assertTrue(service.getState() == ControllerServiceState.DISABLED);
}
Also used : Processor(org.apache.nifi.processor.Processor) FailOnScheduledProcessor(org.apache.nifi.controller.scheduling.processors.FailOnScheduledProcessor) AbstractProcessor(org.apache.nifi.processor.AbstractProcessor) StandardComponentVariableRegistry(org.apache.nifi.registry.variable.StandardComponentVariableRegistry) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) StandardProcessorInitializationContext(org.apache.nifi.processor.StandardProcessorInitializationContext) ReloadComponent(org.apache.nifi.controller.ReloadComponent) StandardValidationContextFactory(org.apache.nifi.processor.StandardValidationContextFactory) LoggableComponent(org.apache.nifi.controller.LoggableComponent) StandardProcessorNode(org.apache.nifi.controller.StandardProcessorNode) ProcessorNode(org.apache.nifi.controller.ProcessorNode) ControllerServiceNode(org.apache.nifi.controller.service.ControllerServiceNode) StandardControllerServiceNode(org.apache.nifi.controller.service.StandardControllerServiceNode) StandardControllerServiceProvider(org.apache.nifi.controller.service.StandardControllerServiceProvider) StandardProcessorNode(org.apache.nifi.controller.StandardProcessorNode) StateManagerProvider(org.apache.nifi.components.state.StateManagerProvider) Test(org.junit.Test)

Example 4 with StandardControllerServiceProvider

use of org.apache.nifi.controller.service.StandardControllerServiceProvider in project nifi by apache.

the class TestStandardProcessScheduler method validateNeverEnablingServiceCanStillBeDisabled.

/**
 * Validates that service that is infinitely blocking in @OnEnabled can
 * still have DISABLE operation initiated. The service itself will be set to
 * DISABLING state at which point UI and all will know that such service can
 * not be transitioned any more into any other state until it finishes
 * enabling (which will never happen in our case thus should be addressed by
 * user). However, regardless of user's mistake NiFi will remain
 * functioning.
 */
@Test
public void validateNeverEnablingServiceCanStillBeDisabled() throws Exception {
    final StandardProcessScheduler scheduler = createScheduler();
    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(Long.MAX_VALUE);
    scheduler.enableControllerService(serviceNode);
    Thread.sleep(100);
    assertTrue(serviceNode.isActive());
    assertEquals(1, ts.enableInvocationCount());
    Thread.sleep(1000);
    scheduler.disableControllerService(serviceNode);
    assertFalse(serviceNode.isActive());
    assertEquals(ControllerServiceState.DISABLING, serviceNode.getState());
    assertEquals(0, ts.disableInvocationCount());
}
Also used : ControllerServiceNode(org.apache.nifi.controller.service.ControllerServiceNode) StandardControllerServiceNode(org.apache.nifi.controller.service.StandardControllerServiceNode) StandardControllerServiceProvider(org.apache.nifi.controller.service.StandardControllerServiceProvider) Test(org.junit.Test)

Example 5 with StandardControllerServiceProvider

use of org.apache.nifi.controller.service.StandardControllerServiceProvider 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

ControllerServiceNode (org.apache.nifi.controller.service.ControllerServiceNode)8 StandardControllerServiceProvider (org.apache.nifi.controller.service.StandardControllerServiceProvider)8 Test (org.junit.Test)8 StandardControllerServiceNode (org.apache.nifi.controller.service.StandardControllerServiceNode)7 ExecutorService (java.util.concurrent.ExecutorService)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 ProcessException (org.apache.nifi.processor.exception.ProcessException)3 InitializationException (org.apache.nifi.reporting.InitializationException)3 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 StateManagerProvider (org.apache.nifi.components.state.StateManagerProvider)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 FailOnScheduledProcessor (org.apache.nifi.controller.scheduling.processors.FailOnScheduledProcessor)1 FlowEngine (org.apache.nifi.engine.FlowEngine)1 AbstractProcessor (org.apache.nifi.processor.AbstractProcessor)1 Processor (org.apache.nifi.processor.Processor)1 StandardProcessorInitializationContext (org.apache.nifi.processor.StandardProcessorInitializationContext)1