Search in sources :

Example 1 with TerminationAwareLogger

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

use of org.apache.nifi.controller.TerminationAwareLogger in project nifi by apache.

the class StandardControllerServiceProvider method createControllerService.

@Override
public ControllerServiceNode createControllerService(final String type, final String id, final BundleCoordinate bundleCoordinate, final Set<URL> additionalUrls, final boolean firstTimeAdded) {
    if (type == null || id == null || bundleCoordinate == null) {
        throw new NullPointerException();
    }
    ClassLoader cl = null;
    final ClassLoader currentContextClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        final Class<?> rawClass;
        try {
            final Bundle csBundle = ExtensionManager.getBundle(bundleCoordinate);
            if (csBundle == null) {
                throw new ControllerServiceInstantiationException("Unable to find bundle for coordinate " + bundleCoordinate.getCoordinate());
            }
            cl = ExtensionManager.createInstanceClassLoader(type, id, csBundle, additionalUrls);
            Thread.currentThread().setContextClassLoader(cl);
            rawClass = Class.forName(type, false, cl);
        } catch (final Exception e) {
            logger.error("Could not create Controller Service of type " + type + " for ID " + id + "; creating \"Ghost\" implementation", e);
            Thread.currentThread().setContextClassLoader(currentContextClassLoader);
            return createGhostControllerService(type, id, bundleCoordinate);
        }
        final Class<? extends ControllerService> controllerServiceClass = rawClass.asSubclass(ControllerService.class);
        final ControllerService originalService = controllerServiceClass.newInstance();
        final StandardControllerServiceInvocationHandler invocationHandler = new StandardControllerServiceInvocationHandler(originalService);
        // extract all interfaces... controllerServiceClass is non null so getAllInterfaces is non null
        final List<Class<?>> interfaceList = ClassUtils.getAllInterfaces(controllerServiceClass);
        final Class<?>[] interfaces = interfaceList.toArray(new Class<?>[interfaceList.size()]);
        final ControllerService proxiedService;
        if (cl == null) {
            proxiedService = (ControllerService) Proxy.newProxyInstance(getClass().getClassLoader(), interfaces, invocationHandler);
        } else {
            proxiedService = (ControllerService) Proxy.newProxyInstance(cl, interfaces, invocationHandler);
        }
        logger.info("Created Controller Service of type {} with identifier {}", type, id);
        final ComponentLog serviceLogger = new SimpleProcessLogger(id, originalService);
        final TerminationAwareLogger terminationAwareLogger = new TerminationAwareLogger(serviceLogger);
        originalService.initialize(new StandardControllerServiceInitializationContext(id, terminationAwareLogger, this, getStateManager(id), nifiProperties));
        final ValidationContextFactory validationContextFactory = new StandardValidationContextFactory(this, variableRegistry);
        final LoggableComponent<ControllerService> originalLoggableComponent = new LoggableComponent<>(originalService, bundleCoordinate, terminationAwareLogger);
        final LoggableComponent<ControllerService> proxiedLoggableComponent = new LoggableComponent<>(proxiedService, bundleCoordinate, terminationAwareLogger);
        final ComponentVariableRegistry componentVarRegistry = new StandardComponentVariableRegistry(this.variableRegistry);
        final ControllerServiceNode serviceNode = new StandardControllerServiceNode(originalLoggableComponent, proxiedLoggableComponent, invocationHandler, id, validationContextFactory, this, componentVarRegistry, flowController);
        serviceNode.setName(rawClass.getSimpleName());
        invocationHandler.setServiceNode(serviceNode);
        if (firstTimeAdded) {
            try (final NarCloseable x = NarCloseable.withComponentNarLoader(originalService.getClass(), originalService.getIdentifier())) {
                ReflectionUtils.invokeMethodsWithAnnotation(OnAdded.class, originalService);
            } catch (final Exception e) {
                throw new ComponentLifeCycleException("Failed to invoke On-Added Lifecycle methods of " + originalService, e);
            }
        }
        serviceCache.putIfAbsent(id, serviceNode);
        return serviceNode;
    } catch (final Throwable t) {
        throw new ControllerServiceInstantiationException(t);
    } finally {
        if (currentContextClassLoader != null) {
            Thread.currentThread().setContextClassLoader(currentContextClassLoader);
        }
    }
}
Also used : ControllerServiceInstantiationException(org.apache.nifi.controller.exception.ControllerServiceInstantiationException) ValidationContextFactory(org.apache.nifi.controller.ValidationContextFactory) StandardValidationContextFactory(org.apache.nifi.processor.StandardValidationContextFactory) ControllerService(org.apache.nifi.controller.ControllerService) StandardValidationContextFactory(org.apache.nifi.processor.StandardValidationContextFactory) SimpleProcessLogger(org.apache.nifi.processor.SimpleProcessLogger) NarCloseable(org.apache.nifi.nar.NarCloseable) StandardComponentVariableRegistry(org.apache.nifi.registry.variable.StandardComponentVariableRegistry) Bundle(org.apache.nifi.bundle.Bundle) ComponentLifeCycleException(org.apache.nifi.controller.exception.ComponentLifeCycleException) ComponentLog(org.apache.nifi.logging.ComponentLog) TimeoutException(java.util.concurrent.TimeoutException) ComponentLifeCycleException(org.apache.nifi.controller.exception.ComponentLifeCycleException) ControllerServiceInstantiationException(org.apache.nifi.controller.exception.ControllerServiceInstantiationException) LoggableComponent(org.apache.nifi.controller.LoggableComponent) StandardComponentVariableRegistry(org.apache.nifi.registry.variable.StandardComponentVariableRegistry) ComponentVariableRegistry(org.apache.nifi.registry.ComponentVariableRegistry) TerminationAwareLogger(org.apache.nifi.controller.TerminationAwareLogger)

Aggregations

LoggableComponent (org.apache.nifi.controller.LoggableComponent)2 TerminationAwareLogger (org.apache.nifi.controller.TerminationAwareLogger)2 ValidationContextFactory (org.apache.nifi.controller.ValidationContextFactory)2 ComponentLog (org.apache.nifi.logging.ComponentLog)2 StandardValidationContextFactory (org.apache.nifi.processor.StandardValidationContextFactory)2 StandardComponentVariableRegistry (org.apache.nifi.registry.variable.StandardComponentVariableRegistry)2 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 TimeoutException (java.util.concurrent.TimeoutException)1 Bundle (org.apache.nifi.bundle.Bundle)1 ControllerService (org.apache.nifi.controller.ControllerService)1 FlowController (org.apache.nifi.controller.FlowController)1 ProcessorNode (org.apache.nifi.controller.ProcessorNode)1 ReloadComponent (org.apache.nifi.controller.ReloadComponent)1 StandardProcessorNode (org.apache.nifi.controller.StandardProcessorNode)1 ComponentLifeCycleException (org.apache.nifi.controller.exception.ComponentLifeCycleException)1 ControllerServiceInstantiationException (org.apache.nifi.controller.exception.ControllerServiceInstantiationException)1 StandardReportingInitializationContext (org.apache.nifi.controller.reporting.StandardReportingInitializationContext)1 StandardReportingTaskNode (org.apache.nifi.controller.reporting.StandardReportingTaskNode)1 MockProcessGroup (org.apache.nifi.controller.service.mock.MockProcessGroup)1