Search in sources :

Example 6 with StandardControllerServiceProvider

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

the class TestStandardProcessScheduler method validateEnabledServiceCanOnlyBeDisabledOnce.

/**
 * Validates the atomic nature of ControllerServiceNode.disable() method
 * which must only trigger @OnDisabled once, regardless of how many threads
 * may have a reference to the underlying ProcessScheduler and
 * ControllerServiceNode.
 */
@Test
public void validateEnabledServiceCanOnlyBeDisabledOnce() 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();
    scheduler.enableControllerService(serviceNode);
    assertTrue(serviceNode.isActive());
    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(1, 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 7 with StandardControllerServiceProvider

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

the class TestStandardProcessScheduler method validateServiceEnablementLogicHappensOnlyOnce.

/**
 * Validates the atomic nature of ControllerServiceNode.enable() method
 * which must only trigger @OnEnabled once, regardless of how many threads
 * may have a reference to the underlying ProcessScheduler and
 * ControllerServiceNode.
 */
@Test
public void validateServiceEnablementLogicHappensOnlyOnce() 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);
    assertFalse(serviceNode.isActive());
    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.enableControllerService(serviceNode);
                    assertTrue(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(1, ts.enableInvocationCount());
}
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 8 with StandardControllerServiceProvider

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

the class TestStandardProcessScheduler method validateEnabledDisableMultiThread.

/**
 * Validates that in multi threaded environment enabling service can still
 * be disabled. This test is set up in such way that disabling of the
 * service could be initiated by both disable and enable methods. In other
 * words it tests two conditions in
 * {@link StandardControllerServiceNode#disable(java.util.concurrent.ScheduledExecutorService, Heartbeater)}
 * where the disabling of the service can be initiated right there (if
 * ENABLED), or if service is still enabling its disabling will be deferred
 * to the logic in
 * {@link StandardControllerServiceNode#enable(java.util.concurrent.ScheduledExecutorService, long, Heartbeater)}
 * IN any even the resulting state of the service is DISABLED
 */
@Test
@Ignore
public void validateEnabledDisableMultiThread() throws Exception {
    final StandardProcessScheduler scheduler = createScheduler();
    final StandardControllerServiceProvider provider = new StandardControllerServiceProvider(controller, scheduler, null, stateMgrProvider, variableRegistry, nifiProperties);
    final ExecutorService executor = Executors.newCachedThreadPool();
    for (int i = 0; i < 200; i++) {
        final ControllerServiceNode serviceNode = provider.createControllerService(RandomShortDelayEnablingService.class.getName(), "1", systemBundle.getBundleDetails().getCoordinate(), null, false);
        executor.execute(new Runnable() {

            @Override
            public void run() {
                scheduler.enableControllerService(serviceNode);
            }
        });
        // ensure that enable gets initiated before disable
        Thread.sleep(10);
        executor.execute(new Runnable() {

            @Override
            public void run() {
                scheduler.disableControllerService(serviceNode);
            }
        });
        Thread.sleep(100);
        assertFalse(serviceNode.isActive());
        assertTrue(serviceNode.getState() == ControllerServiceState.DISABLED);
    }
    // need to sleep a while since we are emulating async invocations on
    // method that is also internally async
    Thread.sleep(500);
    executor.shutdown();
    executor.awaitTermination(5000, TimeUnit.MILLISECONDS);
}
Also used : 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) Ignore(org.junit.Ignore) 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