Search in sources :

Example 11 with ReportingTask

use of org.apache.nifi.reporting.ReportingTask 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 12 with ReportingTask

use of org.apache.nifi.reporting.ReportingTask in project nifi by apache.

the class ControllerServiceAuditor method getUpdateActionsForReferencingComponents.

/**
 * Gets the update actions for all specified referencing components.
 *
 * @param user user
 * @param actions actions
 * @param visitedServices services
 * @param referencingComponents components
 */
private void getUpdateActionsForReferencingComponents(final NiFiUser user, final Collection<Action> actions, final Collection<String> visitedServices, final Set<ConfiguredComponent> referencingComponents) {
    // consider each component updates
    for (final ConfiguredComponent component : referencingComponents) {
        if (component instanceof ProcessorNode) {
            final ProcessorNode processor = ((ProcessorNode) component);
            // create the processor details
            FlowChangeExtensionDetails processorDetails = new FlowChangeExtensionDetails();
            processorDetails.setType(processor.getComponentType());
            // create a processor action
            FlowChangeAction processorAction = new FlowChangeAction();
            processorAction.setUserIdentity(user.getIdentity());
            processorAction.setTimestamp(new Date());
            processorAction.setSourceId(processor.getIdentifier());
            processorAction.setSourceName(processor.getName());
            processorAction.setSourceType(Component.Processor);
            processorAction.setComponentDetails(processorDetails);
            processorAction.setOperation(ScheduledState.RUNNING.equals(processor.getScheduledState()) ? Operation.Start : Operation.Stop);
            actions.add(processorAction);
        } else if (component instanceof ReportingTask) {
            final ReportingTaskNode reportingTask = ((ReportingTaskNode) component);
            // create the reporting task details
            FlowChangeExtensionDetails taskDetails = new FlowChangeExtensionDetails();
            taskDetails.setType(reportingTask.getComponentType());
            // create a reporting task action
            FlowChangeAction reportingTaskAction = new FlowChangeAction();
            reportingTaskAction.setUserIdentity(user.getIdentity());
            reportingTaskAction.setTimestamp(new Date());
            reportingTaskAction.setSourceId(reportingTask.getIdentifier());
            reportingTaskAction.setSourceName(reportingTask.getName());
            reportingTaskAction.setSourceType(Component.ReportingTask);
            reportingTaskAction.setComponentDetails(taskDetails);
            reportingTaskAction.setOperation(ScheduledState.RUNNING.equals(reportingTask.getScheduledState()) ? Operation.Start : Operation.Stop);
            actions.add(reportingTaskAction);
        } else if (component instanceof ControllerServiceNode) {
            final ControllerServiceNode controllerService = ((ControllerServiceNode) component);
            // create the controller service details
            FlowChangeExtensionDetails serviceDetails = new FlowChangeExtensionDetails();
            serviceDetails.setType(controllerService.getComponentType());
            // create a controller service action
            FlowChangeAction serviceAction = new FlowChangeAction();
            serviceAction.setUserIdentity(user.getIdentity());
            serviceAction.setTimestamp(new Date());
            serviceAction.setSourceId(controllerService.getIdentifier());
            serviceAction.setSourceName(controllerService.getName());
            serviceAction.setSourceType(Component.ControllerService);
            serviceAction.setComponentDetails(serviceDetails);
            serviceAction.setOperation(isDisabled(controllerService) ? Operation.Disable : Operation.Enable);
            actions.add(serviceAction);
            // need to consider components referencing this controller service (transitive)
            if (!visitedServices.contains(controllerService.getIdentifier())) {
                getUpdateActionsForReferencingComponents(user, actions, visitedServices, controllerService.getReferences().getReferencingComponents());
            }
        }
    }
}
Also used : ProcessorNode(org.apache.nifi.controller.ProcessorNode) ReportingTaskNode(org.apache.nifi.controller.ReportingTaskNode) ControllerServiceNode(org.apache.nifi.controller.service.ControllerServiceNode) ConfiguredComponent(org.apache.nifi.controller.ConfiguredComponent) FlowChangeExtensionDetails(org.apache.nifi.action.component.details.FlowChangeExtensionDetails) Date(java.util.Date) FlowChangeAction(org.apache.nifi.action.FlowChangeAction) ReportingTask(org.apache.nifi.reporting.ReportingTask)

Example 13 with ReportingTask

use of org.apache.nifi.reporting.ReportingTask in project nifi by apache.

the class ReportingTaskingInitializer method teardown.

@Override
public void teardown(ConfigurableComponent component) {
    ReportingTask reportingTask = (ReportingTask) component;
    try (NarCloseable narCloseable = NarCloseable.withComponentNarLoader(component.getClass(), component.getIdentifier())) {
        final MockConfigurationContext context = new MockConfigurationContext();
        ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnShutdown.class, reportingTask, new MockComponentLogger(), 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) ReportingTask(org.apache.nifi.reporting.ReportingTask)

Example 14 with ReportingTask

use of org.apache.nifi.reporting.ReportingTask in project nifi by apache.

the class FlowController method createReportingTask.

public ReportingTaskNode createReportingTask(final String type, final String id, final BundleCoordinate bundleCoordinate, final Set<URL> additionalUrls, final boolean firstTimeAdded, final boolean register) throws ReportingTaskInstantiationException {
    if (type == null || id == null || bundleCoordinate == null) {
        throw new NullPointerException();
    }
    LoggableComponent<ReportingTask> task = null;
    boolean creationSuccessful = true;
    try {
        task = instantiateReportingTask(type, id, bundleCoordinate, additionalUrls);
    } catch (final Exception e) {
        LOG.error("Could not create Reporting Task of type " + type + " for ID " + id + "; creating \"Ghost\" implementation", e);
        final GhostReportingTask ghostTask = new GhostReportingTask();
        ghostTask.setIdentifier(id);
        ghostTask.setCanonicalClassName(type);
        task = new LoggableComponent<>(ghostTask, bundleCoordinate, null);
        creationSuccessful = false;
    }
    final ComponentVariableRegistry componentVarRegistry = new StandardComponentVariableRegistry(this.variableRegistry);
    final ValidationContextFactory validationContextFactory = new StandardValidationContextFactory(controllerServiceProvider, componentVarRegistry);
    final ReportingTaskNode taskNode;
    if (creationSuccessful) {
        taskNode = new StandardReportingTaskNode(task, id, this, processScheduler, validationContextFactory, componentVarRegistry, this);
    } else {
        final String simpleClassName = type.contains(".") ? StringUtils.substringAfterLast(type, ".") : type;
        final String componentType = "(Missing) " + simpleClassName;
        taskNode = new StandardReportingTaskNode(task, id, this, processScheduler, validationContextFactory, componentType, type, componentVarRegistry, this, true);
    }
    taskNode.setName(taskNode.getReportingTask().getClass().getSimpleName());
    if (firstTimeAdded) {
        final ReportingInitializationContext config = new StandardReportingInitializationContext(id, taskNode.getName(), SchedulingStrategy.TIMER_DRIVEN, "1 min", taskNode.getLogger(), this, nifiProperties, this);
        try {
            taskNode.getReportingTask().initialize(config);
        } catch (final InitializationException ie) {
            throw new ReportingTaskInstantiationException("Failed to initialize reporting task of type " + type, ie);
        }
        try (final NarCloseable x = NarCloseable.withComponentNarLoader(taskNode.getReportingTask().getClass(), taskNode.getReportingTask().getIdentifier())) {
            ReflectionUtils.invokeMethodsWithAnnotation(OnAdded.class, taskNode.getReportingTask());
            ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnConfigurationRestored.class, taskNode.getReportingTask());
        } catch (final Exception e) {
            throw new ComponentLifeCycleException("Failed to invoke On-Added Lifecycle methods of " + taskNode.getReportingTask(), e);
        }
    }
    if (register) {
        reportingTasks.put(id, taskNode);
        // Register log observer to provide bulletins when reporting task logs anything at WARN level or above
        final LogRepository logRepository = LogRepositoryFactory.getRepository(id);
        logRepository.addObserver(StandardProcessorNode.BULLETIN_OBSERVER_ID, LogLevel.WARN, new ReportingTaskLogObserver(getBulletinRepository(), taskNode));
    }
    return taskNode;
}
Also used : NarCloseable(org.apache.nifi.nar.NarCloseable) StandardComponentVariableRegistry(org.apache.nifi.registry.variable.StandardComponentVariableRegistry) LogRepository(org.apache.nifi.logging.LogRepository) StandardValidationContextFactory(org.apache.nifi.processor.StandardValidationContextFactory) StandardReportingInitializationContext(org.apache.nifi.controller.reporting.StandardReportingInitializationContext) InitializationException(org.apache.nifi.reporting.InitializationException) ComponentLifeCycleException(org.apache.nifi.controller.exception.ComponentLifeCycleException) StandardValidationContextFactory(org.apache.nifi.processor.StandardValidationContextFactory) ConfigException(org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException) IOException(java.io.IOException) ProcessorInstantiationException(org.apache.nifi.controller.exception.ProcessorInstantiationException) ComponentLifeCycleException(org.apache.nifi.controller.exception.ComponentLifeCycleException) UnknownServiceAddressException(org.apache.nifi.cluster.protocol.UnknownServiceAddressException) FlowSerializationException(org.apache.nifi.controller.serialization.FlowSerializationException) ResourceNotFoundException(org.apache.nifi.web.ResourceNotFoundException) InitializationException(org.apache.nifi.reporting.InitializationException) ReportingTaskInstantiationException(org.apache.nifi.controller.reporting.ReportingTaskInstantiationException) CommunicationsException(org.apache.nifi.controller.exception.CommunicationsException) FlowSynchronizationException(org.apache.nifi.controller.serialization.FlowSynchronizationException) ControllerServiceInstantiationException(org.apache.nifi.controller.exception.ControllerServiceInstantiationException) GhostReportingTask(org.apache.nifi.reporting.GhostReportingTask) ReportingTaskInstantiationException(org.apache.nifi.controller.reporting.ReportingTaskInstantiationException) ReportingInitializationContext(org.apache.nifi.reporting.ReportingInitializationContext) StandardReportingInitializationContext(org.apache.nifi.controller.reporting.StandardReportingInitializationContext) StandardReportingTaskNode(org.apache.nifi.controller.reporting.StandardReportingTaskNode) StandardReportingTaskNode(org.apache.nifi.controller.reporting.StandardReportingTaskNode) StandardComponentVariableRegistry(org.apache.nifi.registry.variable.StandardComponentVariableRegistry) ComponentVariableRegistry(org.apache.nifi.registry.ComponentVariableRegistry) ReportingTaskLogObserver(org.apache.nifi.logging.ReportingTaskLogObserver) ReportingTask(org.apache.nifi.reporting.ReportingTask) GhostReportingTask(org.apache.nifi.reporting.GhostReportingTask)

Aggregations

ReportingTask (org.apache.nifi.reporting.ReportingTask)14 NarCloseable (org.apache.nifi.nar.NarCloseable)8 ComponentLog (org.apache.nifi.logging.ComponentLog)5 ProcessorNode (org.apache.nifi.controller.ProcessorNode)4 SimpleProcessLogger (org.apache.nifi.processor.SimpleProcessLogger)4 ReportingInitializationContext (org.apache.nifi.reporting.ReportingInitializationContext)4 IOException (java.io.IOException)3 Date (java.util.Date)3 HashMap (java.util.HashMap)3 Bundle (org.apache.nifi.bundle.Bundle)3 FlowController (org.apache.nifi.controller.FlowController)3 MockReportingInitializationContext (org.apache.nifi.mock.MockReportingInitializationContext)3 Collator (java.text.Collator)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 Comparator (java.util.Comparator)2 HashSet (java.util.HashSet)2 List (java.util.List)2