Search in sources :

Example 1 with ComponentLifeCycleException

use of org.apache.nifi.controller.exception.ComponentLifeCycleException in project nifi by apache.

the class StandardProcessorDAO method createProcessor.

@Override
public ProcessorNode createProcessor(final String groupId, ProcessorDTO processorDTO) {
    if (processorDTO.getParentGroupId() != null && !flowController.areGroupsSame(groupId, processorDTO.getParentGroupId())) {
        throw new IllegalArgumentException("Cannot specify a different Parent Group ID than the Group to which the Processor is being added.");
    }
    // ensure the type is specified
    if (processorDTO.getType() == null) {
        throw new IllegalArgumentException("The processor type must be specified.");
    }
    // get the group to add the processor to
    ProcessGroup group = locateProcessGroup(flowController, groupId);
    try {
        // attempt to create the processor
        ProcessorNode processor = flowController.createProcessor(processorDTO.getType(), processorDTO.getId(), BundleUtils.getBundle(processorDTO.getType(), processorDTO.getBundle()));
        // ensure we can perform the update before we add the processor to the flow
        verifyUpdate(processor, processorDTO);
        // add the processor to the group
        group.addProcessor(processor);
        // configure the processor
        configureProcessor(processor, processorDTO);
        return processor;
    } catch (ProcessorInstantiationException pse) {
        throw new NiFiCoreException(String.format("Unable to create processor of type %s due to: %s", processorDTO.getType(), pse.getMessage()), pse);
    } catch (IllegalStateException | ComponentLifeCycleException ise) {
        throw new NiFiCoreException(ise.getMessage(), ise);
    }
}
Also used : NiFiCoreException(org.apache.nifi.web.NiFiCoreException) ProcessorNode(org.apache.nifi.controller.ProcessorNode) ProcessGroup(org.apache.nifi.groups.ProcessGroup) ProcessorInstantiationException(org.apache.nifi.controller.exception.ProcessorInstantiationException) ComponentLifeCycleException(org.apache.nifi.controller.exception.ComponentLifeCycleException)

Example 2 with ComponentLifeCycleException

use of org.apache.nifi.controller.exception.ComponentLifeCycleException in project nifi by apache.

the class StandardReportingTaskDAO method updateReportingTask.

@Override
public ReportingTaskNode updateReportingTask(final ReportingTaskDTO reportingTaskDTO) {
    // get the reporting task
    final ReportingTaskNode reportingTask = locateReportingTask(reportingTaskDTO.getId());
    // ensure we can perform the update
    verifyUpdate(reportingTask, reportingTaskDTO);
    // perform the update
    configureReportingTask(reportingTask, reportingTaskDTO);
    // attempt to change the underlying processor if an updated bundle is specified
    // updating the bundle must happen after configuring so that any additional classpath resources are set first
    updateBundle(reportingTask, reportingTaskDTO);
    // see if an update is necessary
    if (isNotNull(reportingTaskDTO.getState())) {
        final ScheduledState purposedScheduledState = ScheduledState.valueOf(reportingTaskDTO.getState());
        // only attempt an action if it is changing
        if (!purposedScheduledState.equals(reportingTask.getScheduledState())) {
            try {
                // perform the appropriate action
                switch(purposedScheduledState) {
                    case RUNNING:
                        reportingTaskProvider.startReportingTask(reportingTask);
                        break;
                    case STOPPED:
                        switch(reportingTask.getScheduledState()) {
                            case RUNNING:
                                reportingTaskProvider.stopReportingTask(reportingTask);
                                break;
                            case DISABLED:
                                reportingTaskProvider.enableReportingTask(reportingTask);
                                break;
                        }
                        break;
                    case DISABLED:
                        reportingTaskProvider.disableReportingTask(reportingTask);
                        break;
                }
            } catch (IllegalStateException | ComponentLifeCycleException ise) {
                throw new NiFiCoreException(ise.getMessage(), ise);
            } catch (RejectedExecutionException ree) {
                throw new NiFiCoreException("Unable to schedule all tasks for the specified reporting task.", ree);
            } catch (NullPointerException npe) {
                throw new NiFiCoreException("Unable to update reporting task run state.", npe);
            } catch (Exception e) {
                throw new NiFiCoreException("Unable to update reporting task run state: " + e, e);
            }
        }
    }
    return reportingTask;
}
Also used : NiFiCoreException(org.apache.nifi.web.NiFiCoreException) ReportingTaskNode(org.apache.nifi.controller.ReportingTaskNode) ScheduledState(org.apache.nifi.controller.ScheduledState) ComponentLifeCycleException(org.apache.nifi.controller.exception.ComponentLifeCycleException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ReportingTaskInstantiationException(org.apache.nifi.controller.reporting.ReportingTaskInstantiationException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ResourceNotFoundException(org.apache.nifi.web.ResourceNotFoundException) ParseException(java.text.ParseException) ComponentLifeCycleException(org.apache.nifi.controller.exception.ComponentLifeCycleException) NiFiCoreException(org.apache.nifi.web.NiFiCoreException) ValidationException(org.apache.nifi.controller.exception.ValidationException)

Example 3 with ComponentLifeCycleException

use of org.apache.nifi.controller.exception.ComponentLifeCycleException 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)

Example 4 with ComponentLifeCycleException

use of org.apache.nifi.controller.exception.ComponentLifeCycleException in project nifi by apache.

the class FlowController method createProcessor.

/**
 * <p>
 * Creates a new ProcessorNode with the given type and identifier and
 * optionally initializes it.
 * </p>
 *
 * @param type the fully qualified Processor class name
 * @param id the unique ID of the Processor
 * @param coordinate the bundle coordinate for this processor
 * @param firstTimeAdded whether or not this is the first time this
 * Processor is added to the graph. If {@code true}, will invoke methods
 * annotated with the {@link OnAdded} annotation.
 * @return new processor node
 * @throws NullPointerException if either arg is null
 * @throws ProcessorInstantiationException if the processor cannot be
 * instantiated for any reason
 */
public ProcessorNode createProcessor(final String type, String id, final BundleCoordinate coordinate, final Set<URL> additionalUrls, final boolean firstTimeAdded, final boolean registerLogObserver) throws ProcessorInstantiationException {
    id = id.intern();
    boolean creationSuccessful;
    LoggableComponent<Processor> processor;
    try {
        processor = instantiateProcessor(type, id, coordinate, additionalUrls);
        creationSuccessful = true;
    } catch (final ProcessorInstantiationException pie) {
        LOG.error("Could not create Processor of type " + type + " for ID " + id + "; creating \"Ghost\" implementation", pie);
        final GhostProcessor ghostProc = new GhostProcessor();
        ghostProc.setIdentifier(id);
        ghostProc.setCanonicalClassName(type);
        processor = new LoggableComponent<>(ghostProc, coordinate, null);
        creationSuccessful = false;
    }
    final ComponentVariableRegistry componentVarRegistry = new StandardComponentVariableRegistry(this.variableRegistry);
    final ValidationContextFactory validationContextFactory = new StandardValidationContextFactory(controllerServiceProvider, componentVarRegistry);
    final ProcessorNode procNode;
    if (creationSuccessful) {
        procNode = new StandardProcessorNode(processor, id, validationContextFactory, processScheduler, controllerServiceProvider, nifiProperties, componentVarRegistry, this);
    } else {
        final String simpleClassName = type.contains(".") ? StringUtils.substringAfterLast(type, ".") : type;
        final String componentType = "(Missing) " + simpleClassName;
        procNode = new StandardProcessorNode(processor, id, validationContextFactory, processScheduler, controllerServiceProvider, componentType, type, nifiProperties, componentVarRegistry, this, true);
    }
    final LogRepository logRepository = LogRepositoryFactory.getRepository(id);
    if (registerLogObserver) {
        logRepository.addObserver(StandardProcessorNode.BULLETIN_OBSERVER_ID, LogLevel.WARN, new ProcessorLogObserver(getBulletinRepository(), procNode));
    }
    try {
        final Class<?> procClass = procNode.getProcessor().getClass();
        if (procClass.isAnnotationPresent(DefaultSettings.class)) {
            DefaultSettings ds = procClass.getAnnotation(DefaultSettings.class);
            try {
                procNode.setYieldPeriod(ds.yieldDuration());
            } catch (Throwable ex) {
                LOG.error(String.format("Error while setting yield period from DefaultSettings annotation:%s", ex.getMessage()), ex);
            }
            try {
                procNode.setPenalizationPeriod(ds.penaltyDuration());
            } catch (Throwable ex) {
                LOG.error(String.format("Error while setting penalty duration from DefaultSettings annotation:%s", ex.getMessage()), ex);
            }
            // the caller said to register the log observer, otherwise we could be changing the level when we didn't mean to
            if (registerLogObserver) {
                try {
                    procNode.setBulletinLevel(ds.bulletinLevel());
                } catch (Throwable ex) {
                    LOG.error(String.format("Error while setting bulletin level from DefaultSettings annotation:%s", ex.getMessage()), ex);
                }
            }
        }
    } catch (Throwable ex) {
        LOG.error(String.format("Error while setting default settings from DefaultSettings annotation: %s", ex.getMessage()), ex);
    }
    if (firstTimeAdded) {
        try (final NarCloseable x = NarCloseable.withComponentNarLoader(procNode.getProcessor().getClass(), procNode.getProcessor().getIdentifier())) {
            ReflectionUtils.invokeMethodsWithAnnotation(OnAdded.class, procNode.getProcessor());
        } catch (final Exception e) {
            if (registerLogObserver) {
                logRepository.removeObserver(StandardProcessorNode.BULLETIN_OBSERVER_ID);
            }
            throw new ComponentLifeCycleException("Failed to invoke @OnAdded methods of " + procNode.getProcessor(), e);
        }
        if (firstTimeAdded) {
            try (final NarCloseable nc = NarCloseable.withComponentNarLoader(procNode.getProcessor().getClass(), procNode.getProcessor().getIdentifier())) {
                ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnConfigurationRestored.class, procNode.getProcessor());
            }
        }
    }
    return procNode;
}
Also used : NarCloseable(org.apache.nifi.nar.NarCloseable) GhostProcessor(org.apache.nifi.processor.GhostProcessor) Processor(org.apache.nifi.processor.Processor) StandardComponentVariableRegistry(org.apache.nifi.registry.variable.StandardComponentVariableRegistry) LogRepository(org.apache.nifi.logging.LogRepository) DefaultSettings(org.apache.nifi.annotation.configuration.DefaultSettings) StandardValidationContextFactory(org.apache.nifi.processor.StandardValidationContextFactory) 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) StandardComponentVariableRegistry(org.apache.nifi.registry.variable.StandardComponentVariableRegistry) ComponentVariableRegistry(org.apache.nifi.registry.ComponentVariableRegistry) ProcessorInstantiationException(org.apache.nifi.controller.exception.ProcessorInstantiationException) GhostProcessor(org.apache.nifi.processor.GhostProcessor) ProcessorLogObserver(org.apache.nifi.logging.ProcessorLogObserver)

Example 5 with ComponentLifeCycleException

use of org.apache.nifi.controller.exception.ComponentLifeCycleException in project nifi by apache.

the class StandardProcessGroup method removeProcessor.

@Override
public void removeProcessor(final ProcessorNode processor) {
    boolean removed = false;
    final String id = requireNonNull(processor).getIdentifier();
    writeLock.lock();
    try {
        if (!processors.containsKey(id)) {
            throw new IllegalStateException(processor.getIdentifier() + " is not a member of this Process Group");
        }
        processor.verifyCanDelete();
        for (final Connection conn : processor.getConnections()) {
            conn.verifyCanDelete();
        }
        try (final NarCloseable x = NarCloseable.withComponentNarLoader(processor.getProcessor().getClass(), processor.getIdentifier())) {
            final StandardProcessContext processContext = new StandardProcessContext(processor, controllerServiceProvider, encryptor, getStateManager(processor.getIdentifier()), () -> false);
            ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnRemoved.class, processor.getProcessor(), processContext);
        } catch (final Exception e) {
            throw new ComponentLifeCycleException("Failed to invoke 'OnRemoved' methods of processor with id " + processor.getIdentifier(), e);
        }
        for (final Map.Entry<PropertyDescriptor, String> entry : processor.getProperties().entrySet()) {
            final PropertyDescriptor descriptor = entry.getKey();
            if (descriptor.getControllerServiceDefinition() != null) {
                final String value = entry.getValue() == null ? descriptor.getDefaultValue() : entry.getValue();
                if (value != null) {
                    final ControllerServiceNode serviceNode = controllerServiceProvider.getControllerServiceNode(value);
                    if (serviceNode != null) {
                        serviceNode.removeReference(processor);
                    }
                }
            }
        }
        processors.remove(id);
        onComponentModified();
        flowController.onProcessorRemoved(processor);
        LogRepositoryFactory.getRepository(processor.getIdentifier()).removeAllObservers();
        final StateManagerProvider stateManagerProvider = flowController.getStateManagerProvider();
        scheduler.submitFrameworkTask(new Runnable() {

            @Override
            public void run() {
                stateManagerProvider.onComponentRemoved(processor.getIdentifier());
            }
        });
        // must copy to avoid a concurrent modification
        final Set<Connection> copy = new HashSet<>(processor.getConnections());
        for (final Connection conn : copy) {
            removeConnection(conn);
        }
        removed = true;
        LOG.info("{} removed from flow", processor);
    } finally {
        if (removed) {
            try {
                ExtensionManager.removeInstanceClassLoader(id);
            } catch (Throwable t) {
            }
        }
        writeLock.unlock();
    }
}
Also used : NarCloseable(org.apache.nifi.nar.NarCloseable) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) VersionedPropertyDescriptor(org.apache.nifi.registry.flow.VersionedPropertyDescriptor) Connection(org.apache.nifi.connectable.Connection) VersionedConnection(org.apache.nifi.registry.flow.VersionedConnection) ComponentLifeCycleException(org.apache.nifi.controller.exception.ComponentLifeCycleException) NiFiRegistryException(org.apache.nifi.registry.client.NiFiRegistryException) IOException(java.io.IOException) ProcessorInstantiationException(org.apache.nifi.controller.exception.ProcessorInstantiationException) ComponentLifeCycleException(org.apache.nifi.controller.exception.ComponentLifeCycleException) ControllerServiceNode(org.apache.nifi.controller.service.ControllerServiceNode) StandardProcessContext(org.apache.nifi.processor.StandardProcessContext) Map(java.util.Map) HashMap(java.util.HashMap) StateManagerProvider(org.apache.nifi.components.state.StateManagerProvider) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet)

Aggregations

ComponentLifeCycleException (org.apache.nifi.controller.exception.ComponentLifeCycleException)7 ProcessorInstantiationException (org.apache.nifi.controller.exception.ProcessorInstantiationException)5 NarCloseable (org.apache.nifi.nar.NarCloseable)4 ResourceNotFoundException (org.apache.nifi.web.ResourceNotFoundException)4 IOException (java.io.IOException)3 ControllerServiceInstantiationException (org.apache.nifi.controller.exception.ControllerServiceInstantiationException)3 ReportingTaskInstantiationException (org.apache.nifi.controller.reporting.ReportingTaskInstantiationException)3 StandardValidationContextFactory (org.apache.nifi.processor.StandardValidationContextFactory)3 ComponentVariableRegistry (org.apache.nifi.registry.ComponentVariableRegistry)3 StandardComponentVariableRegistry (org.apache.nifi.registry.variable.StandardComponentVariableRegistry)3 ParseException (java.text.ParseException)2 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)2 UnknownServiceAddressException (org.apache.nifi.cluster.protocol.UnknownServiceAddressException)2 ProcessorNode (org.apache.nifi.controller.ProcessorNode)2 ScheduledState (org.apache.nifi.controller.ScheduledState)2 CommunicationsException (org.apache.nifi.controller.exception.CommunicationsException)2 ValidationException (org.apache.nifi.controller.exception.ValidationException)2 FlowSerializationException (org.apache.nifi.controller.serialization.FlowSerializationException)2 FlowSynchronizationException (org.apache.nifi.controller.serialization.FlowSynchronizationException)2 ProcessGroup (org.apache.nifi.groups.ProcessGroup)2