Search in sources :

Example 26 with NarCloseable

use of org.apache.nifi.nar.NarCloseable in project nifi by apache.

the class StandardProcessScheduler method schedule.

@Override
public void schedule(final ReportingTaskNode taskNode) {
    final LifecycleState lifecycleState = getLifecycleState(requireNonNull(taskNode), true);
    if (lifecycleState.isScheduled()) {
        return;
    }
    final int activeThreadCount = lifecycleState.getActiveThreadCount();
    if (activeThreadCount > 0) {
        throw new IllegalStateException("Reporting Task " + taskNode.getName() + " cannot be started because it has " + activeThreadCount + " threads still running");
    }
    if (!taskNode.isValid()) {
        throw new IllegalStateException("Reporting Task " + taskNode.getName() + " is not in a valid state for the following reasons: " + taskNode.getValidationErrors());
    }
    final SchedulingAgent agent = getSchedulingAgent(taskNode.getSchedulingStrategy());
    lifecycleState.setScheduled(true);
    final Runnable startReportingTaskRunnable = new Runnable() {

        @Override
        public void run() {
            final long lastStopTime = lifecycleState.getLastStopTime();
            final ReportingTask reportingTask = taskNode.getReportingTask();
            // Continually attempt to start the Reporting Task, and if we fail sleep for a bit each time.
            while (true) {
                try {
                    synchronized (lifecycleState) {
                        // bail; another thread will be responsible for invoking the @OnScheduled methods.
                        if (!lifecycleState.isScheduled() || lifecycleState.getLastStopTime() != lastStopTime) {
                            return;
                        }
                        try (final NarCloseable x = NarCloseable.withComponentNarLoader(reportingTask.getClass(), reportingTask.getIdentifier())) {
                            ReflectionUtils.invokeMethodsWithAnnotation(OnScheduled.class, reportingTask, taskNode.getConfigurationContext());
                        }
                        agent.schedule(taskNode, lifecycleState);
                        return;
                    }
                } catch (final Exception e) {
                    final Throwable cause = e instanceof InvocationTargetException ? e.getCause() : e;
                    final ComponentLog componentLog = new SimpleProcessLogger(reportingTask.getIdentifier(), reportingTask);
                    componentLog.error("Failed to invoke @OnEnabled method due to {}", cause);
                    LOG.error("Failed to invoke the On-Scheduled Lifecycle methods of {} due to {}; administratively yielding this " + "ReportingTask and will attempt to schedule it again after {}", new Object[] { reportingTask, e.toString(), administrativeYieldDuration }, e);
                    ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnUnscheduled.class, reportingTask, taskNode.getConfigurationContext());
                    ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnStopped.class, reportingTask, taskNode.getConfigurationContext());
                    try {
                        Thread.sleep(administrativeYieldMillis);
                    } catch (final InterruptedException ie) {
                    }
                }
            }
        }
    };
    componentLifeCycleThreadPool.execute(startReportingTaskRunnable);
    taskNode.setScheduledState(ScheduledState.RUNNING);
}
Also used : NarCloseable(org.apache.nifi.nar.NarCloseable) OnStopped(org.apache.nifi.annotation.lifecycle.OnStopped) ComponentLog(org.apache.nifi.logging.ComponentLog) ProcessorInstantiationException(org.apache.nifi.controller.exception.ProcessorInstantiationException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) OnUnscheduled(org.apache.nifi.annotation.lifecycle.OnUnscheduled) SimpleProcessLogger(org.apache.nifi.processor.SimpleProcessLogger) ReportingTask(org.apache.nifi.reporting.ReportingTask)

Example 27 with NarCloseable

use of org.apache.nifi.nar.NarCloseable in project nifi by apache.

the class StandardProcessScheduler method stopConnectable.

private synchronized void stopConnectable(final Connectable connectable) {
    final LifecycleState state = getLifecycleState(requireNonNull(connectable), false);
    if (!state.isScheduled()) {
        return;
    }
    state.setScheduled(false);
    getSchedulingAgent(connectable).unschedule(connectable, state);
    if (!state.isScheduled() && state.getActiveThreadCount() == 0 && state.mustCallOnStoppedMethods()) {
        final ConnectableProcessContext processContext = new ConnectableProcessContext(connectable, encryptor, getStateManager(connectable.getIdentifier()));
        try (final NarCloseable x = NarCloseable.withComponentNarLoader(connectable.getClass(), connectable.getIdentifier())) {
            ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnStopped.class, connectable, processContext);
        }
    }
}
Also used : NarCloseable(org.apache.nifi.nar.NarCloseable)

Example 28 with NarCloseable

use of org.apache.nifi.nar.NarCloseable in project nifi by apache.

the class StandardControllerServiceInvocationHandler method invoke.

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    final String methodName = method.getName();
    if ("initialize".equals(methodName) || "onPropertyModified".equals(methodName)) {
        throw new UnsupportedOperationException(method + " may only be invoked by the NiFi framework");
    }
    final ControllerServiceNode node = serviceNodeHolder.get();
    final ControllerServiceState state = node.getState();
    // only allow method call if service state is ENABLED.
    final boolean disabled = state != ControllerServiceState.ENABLED;
    if (disabled && !validDisabledMethods.contains(method)) {
        // Use nar class loader here because we are implicitly calling toString() on the original implementation.
        try (final NarCloseable narCloseable = NarCloseable.withComponentNarLoader(originalService.getClass(), originalService.getIdentifier())) {
            throw new IllegalStateException("Cannot invoke method " + method + " on Controller Service " + originalService.getIdentifier() + " because the Controller Service is disabled");
        } catch (final Throwable e) {
            throw new IllegalStateException("Cannot invoke method " + method + " on Controller Service with identifier " + originalService.getIdentifier() + " because the Controller Service is disabled");
        }
    }
    try (final NarCloseable narCloseable = NarCloseable.withComponentNarLoader(originalService.getClass(), originalService.getIdentifier())) {
        return method.invoke(originalService, args);
    } catch (final InvocationTargetException e) {
        // to instead re-throw what the ControllerService threw, so we pull it out of the InvocationTargetException.
        throw e.getCause();
    }
}
Also used : NarCloseable(org.apache.nifi.nar.NarCloseable) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 29 with NarCloseable

use of org.apache.nifi.nar.NarCloseable in project nifi by apache.

the class ControllerServiceInitializer method initialize.

@Override
public void initialize(ConfigurableComponent component) throws InitializationException {
    ControllerService controllerService = (ControllerService) component;
    ControllerServiceInitializationContext context = new MockControllerServiceInitializationContext();
    try (NarCloseable narCloseable = NarCloseable.withComponentNarLoader(component.getClass(), context.getIdentifier())) {
        controllerService.initialize(context);
    }
}
Also used : NarCloseable(org.apache.nifi.nar.NarCloseable) ControllerServiceInitializationContext(org.apache.nifi.controller.ControllerServiceInitializationContext) MockControllerServiceInitializationContext(org.apache.nifi.mock.MockControllerServiceInitializationContext) MockControllerServiceInitializationContext(org.apache.nifi.mock.MockControllerServiceInitializationContext) ControllerService(org.apache.nifi.controller.ControllerService)

Example 30 with NarCloseable

use of org.apache.nifi.nar.NarCloseable in project nifi by apache.

the class ControllerServiceInitializer method teardown.

@Override
public void teardown(ConfigurableComponent component) {
    try (NarCloseable narCloseable = NarCloseable.withComponentNarLoader(component.getClass(), component.getIdentifier())) {
        ControllerService controllerService = (ControllerService) component;
        final ComponentLog logger = new MockComponentLogger();
        final MockConfigurationContext context = new MockConfigurationContext();
        ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnShutdown.class, controllerService, logger, context);
    } finally {
        ExtensionManager.removeInstanceClassLoader(component.getIdentifier());
    }
}
Also used : NarCloseable(org.apache.nifi.nar.NarCloseable) MockConfigurationContext(org.apache.nifi.mock.MockConfigurationContext) MockComponentLogger(org.apache.nifi.mock.MockComponentLogger) ControllerService(org.apache.nifi.controller.ControllerService) ComponentLog(org.apache.nifi.logging.ComponentLog)

Aggregations

NarCloseable (org.apache.nifi.nar.NarCloseable)48 ComponentLog (org.apache.nifi.logging.ComponentLog)13 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)12 Processor (org.apache.nifi.processor.Processor)12 HashMap (java.util.HashMap)9 ControllerServiceNode (org.apache.nifi.controller.service.ControllerServiceNode)9 SimpleProcessLogger (org.apache.nifi.processor.SimpleProcessLogger)9 URL (java.net.URL)8 LinkedHashMap (java.util.LinkedHashMap)8 ProcessorInstantiationException (org.apache.nifi.controller.exception.ProcessorInstantiationException)8 ReportingTask (org.apache.nifi.reporting.ReportingTask)8 Map (java.util.Map)7 ControllerService (org.apache.nifi.controller.ControllerService)6 IOException (java.io.IOException)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)5 ArrayList (java.util.ArrayList)5 ComponentLifeCycleException (org.apache.nifi.controller.exception.ComponentLifeCycleException)5 ControllerServiceInstantiationException (org.apache.nifi.controller.exception.ControllerServiceInstantiationException)5 Relationship (org.apache.nifi.processor.Relationship)5 ComponentVariableRegistry (org.apache.nifi.registry.ComponentVariableRegistry)5