Java Code Examples for org.apache.nifi.controller.service.ControllerServiceNode#performValidation()

The following examples show how to use org.apache.nifi.controller.service.ControllerServiceNode#performValidation() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestStandardProcessScheduler.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void validateDisablingOfTheFailedService() throws Exception {
    final StandardProcessScheduler scheduler = createScheduler();

    final ControllerServiceNode serviceNode = flowManager.createControllerService(FailingService.class.getName(),
            "1", systemBundle.getBundleDetails().getCoordinate(), null, false, true);
    serviceNode.performValidation();

    final Future<?> future = scheduler.enableControllerService(serviceNode);
    try {
        future.get();
    } catch (final Exception e) {
        // Expected behavior because the FailingService throws Exception when attempting to enable
    }

    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());
    assertSame(serviceNode.getState(), ControllerServiceState.ENABLING);
}
 
Example 2
Source File: TestStandardProcessScheduler.java    From nifi with Apache License 2.0 6 votes vote down vote up
/**
 * 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 ControllerServiceNode serviceNode = flowManager.createControllerService(LongEnablingService.class.getName(),
            "1", systemBundle.getBundleDetails().getCoordinate(), null, false, true);

    final LongEnablingService ts = (LongEnablingService) serviceNode.getControllerServiceImplementation();
    ts.setLimit(Long.MAX_VALUE);

    serviceNode.performValidation();
    scheduler.enableControllerService(serviceNode);

    assertTrue(serviceNode.isActive());
    final long maxTime = System.nanoTime() + TimeUnit.SECONDS.toNanos(10);
    while (ts.enableInvocationCount() != 1 && System.nanoTime() <= maxTime) {
        Thread.sleep(1L);
    }
    assertEquals(1, ts.enableInvocationCount());

    scheduler.disableControllerService(serviceNode);
    assertFalse(serviceNode.isActive());
    assertEquals(ControllerServiceState.DISABLING, serviceNode.getState());
    assertEquals(0, ts.disableInvocationCount());
}
 
Example 3
Source File: ControllerServiceReferenceIT.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testLongValidatingControllerService() {
    final ControllerServiceNode serviceNode = createControllerServiceNode(LongValidatingControllerService.class.getName());
    serviceNode.setProperties(Collections.singletonMap(LongValidatingControllerService.DELAY.getName(), "250 millis"));
    final ValidationStatus validationStatus = serviceNode.performValidation();
    final Collection<ValidationResult> validationErrors = serviceNode.getValidationErrors();
    assertSame(validationStatus, ValidationStatus.VALID);
    assertEquals(0, validationErrors.size());
}
 
Example 4
Source File: TestStandardProcessScheduler.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 60000)
public void testDisableControllerServiceWithProcessorTryingToStartUsingIt() throws InterruptedException, ExecutionException {
    final String uuid = UUID.randomUUID().toString();
    final Processor proc = new ServiceReferencingProcessor();
    proc.initialize(new StandardProcessorInitializationContext(uuid, null, null, null, KerberosConfig.NOT_CONFIGURED));

    final ReloadComponent reloadComponent = Mockito.mock(ReloadComponent.class);

    final ControllerServiceNode service = flowManager.createControllerService(NoStartServiceImpl.class.getName(), "service",
            systemBundle.getBundleDetails().getCoordinate(), null, true, true);

    rootGroup.addControllerService(service);

    final LoggableComponent<Processor> loggableComponent = new LoggableComponent<>(proc, systemBundle.getBundleDetails().getCoordinate(), null);
    final ValidationContextFactory validationContextFactory = new StandardValidationContextFactory(serviceProvider, variableRegistry);
    final ProcessorNode procNode = new StandardProcessorNode(loggableComponent, uuid, validationContextFactory, scheduler,
        serviceProvider, new StandardComponentVariableRegistry(VariableRegistry.EMPTY_REGISTRY), reloadComponent, extensionManager, new SynchronousValidationTrigger());

    rootGroup.addProcessor(procNode);

    Map<String, String> procProps = new HashMap<>();
    procProps.put(ServiceReferencingProcessor.SERVICE_DESC.getName(), service.getIdentifier());
    procNode.setProperties(procProps);

    service.performValidation();
    scheduler.enableControllerService(service);

    procNode.performValidation();
    scheduler.startProcessor(procNode, true);

    Thread.sleep(25L);

    scheduler.stopProcessor(procNode);
    assertTrue(service.isActive());
    assertSame(service.getState(), ControllerServiceState.ENABLING);
    scheduler.disableControllerService(service).get();
    assertFalse(service.isActive());
    assertSame(service.getState(), ControllerServiceState.DISABLED);
}
 
Example 5
Source File: TestStandardProcessScheduler.java    From nifi with Apache License 2.0 5 votes vote down vote up
/**
 * 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 ControllerServiceNode serviceNode = flowManager.createControllerService(SimpleTestService.class.getName(),
            "1", systemBundle.getBundleDetails().getCoordinate(), null, false, true);

    serviceNode.performValidation();

    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).get();
                    assertTrue(serviceNode.isActive());
                } catch (final Exception e) {
                    e.printStackTrace();
                    asyncFailed.set(true);
                }
            }
        });
    }

    executor.shutdown();
    executor.awaitTermination(10, TimeUnit.SECONDS);

    assertFalse(asyncFailed.get());
    assertEquals(1, ts.enableInvocationCount());
}
 
Example 6
Source File: ImportFlowIT.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Test
public void testImportFlowWithProcessorAndControllerService() throws ExecutionException, InterruptedException {
    // Build a Versioned Flow that consists of a Controller Service and a Processor
    // that references that Controller Service.
    final ControllerServiceNode controllerService = createControllerServiceNode(LongValidatingControllerService.class);
    controllerService.setProperties(Collections.singletonMap(LongValidatingControllerService.DELAY.getName(), "250 millis"));

    final ProcessorNode processor = createProcessorNode(NopServiceReferencingProcessor.class);
    processor.setAutoTerminatedRelationships(Collections.singleton(REL_SUCCESS));
    processor.setProperties(Collections.singletonMap(NopServiceReferencingProcessor.SERVICE.getName(), controllerService.getIdentifier()));

    final VersionedFlowSnapshot proposedFlow = createFlowSnapshot(Collections.singletonList(controllerService), Collections.singletonList(processor), null);

    // Create an Inner Process Group and update it to match the Versioned Flow.
    final ProcessGroup innerGroup = getFlowController().getFlowManager().createProcessGroup("inner-group-id");
    innerGroup.setName("Inner Group");
    getRootGroup().addProcessGroup(innerGroup);

    innerGroup.updateFlow(proposedFlow, null, false, true, false);

    // Ensure that the controller service is valid and enable it.
    final Set<ControllerServiceNode> serviceNodes = innerGroup.findAllControllerServices();
    assertEquals(1, serviceNodes.size());

    final ControllerServiceNode serviceNode = serviceNodes.iterator().next();
    final ValidationStatus validationStatus = serviceNode.performValidation();
    assertEquals(ValidationStatus.VALID, validationStatus);
    getFlowController().getControllerServiceProvider().enableControllerService(serviceNode).get();
    assertTrue(serviceNode.isActive());

    // Ensure that the processor is valid.
    final List<ProcessorNode> processorNodes = innerGroup.findAllProcessors();
    assertEquals(1, processorNodes.size());

    final ProcessorNode procNode = processorNodes.get(0);
    final ValidationStatus procValidationStatus = procNode.performValidation();
    final Collection<ValidationResult> validationErrors = procNode.getValidationErrors();
    System.out.println(validationErrors);
    assertEquals(Collections.emptyList(), validationErrors);
    assertEquals(ValidationStatus.VALID, procValidationStatus);

    // Ensure that the reference to the controller service was properly updated
    final String referencedServiceId = procNode.getEffectivePropertyValue(NopServiceReferencingProcessor.SERVICE);
    assertEquals(serviceNode.getIdentifier(), referencedServiceId);
    assertNotEquals("service-id", referencedServiceId);
}