Search in sources :

Example 41 with NarCloseable

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

the class FlowController method removeReportingTask.

@Override
public void removeReportingTask(final ReportingTaskNode reportingTaskNode) {
    final ReportingTaskNode existing = reportingTasks.get(reportingTaskNode.getIdentifier());
    if (existing == null || existing != reportingTaskNode) {
        throw new IllegalStateException("Reporting Task " + reportingTaskNode + " does not exist in this Flow");
    }
    reportingTaskNode.verifyCanDelete();
    try (final NarCloseable x = NarCloseable.withComponentNarLoader(reportingTaskNode.getReportingTask().getClass(), reportingTaskNode.getReportingTask().getIdentifier())) {
        ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnRemoved.class, reportingTaskNode.getReportingTask(), reportingTaskNode.getConfigurationContext());
    }
    for (final Map.Entry<PropertyDescriptor, String> entry : reportingTaskNode.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(reportingTaskNode);
                }
            }
        }
    }
    reportingTasks.remove(reportingTaskNode.getIdentifier());
    ExtensionManager.removeInstanceClassLoader(reportingTaskNode.getIdentifier());
}
Also used : NarCloseable(org.apache.nifi.nar.NarCloseable) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) StandardReportingTaskNode(org.apache.nifi.controller.reporting.StandardReportingTaskNode) ControllerServiceNode(org.apache.nifi.controller.service.ControllerServiceNode) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) HashMap(java.util.HashMap)

Example 42 with NarCloseable

use of org.apache.nifi.nar.NarCloseable 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)

Example 43 with NarCloseable

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

the class FlowController method shutdown.

/**
 * Triggers the controller to begin shutdown, stopping all processors and
 * terminating the scheduling engine. After calling this method, the
 * {@link #isTerminated()} method will indicate whether or not the shutdown
 * has finished.
 *
 * @param kill if <code>true</code>, attempts to stop all active threads,
 * but makes no guarantee that this will happen
 *
 * @throws IllegalStateException if the controller is already stopped or
 * currently in the processor of stopping
 */
public void shutdown(final boolean kill) {
    this.shutdown = true;
    stopAllProcessors();
    readLock.lock();
    try {
        if (isTerminated() || timerDrivenEngineRef.get().isTerminating()) {
            throw new IllegalStateException("Controller already stopped or still stopping...");
        }
        if (leaderElectionManager != null) {
            leaderElectionManager.stop();
        }
        if (heartbeatMonitor != null) {
            heartbeatMonitor.stop();
        }
        if (kill) {
            this.timerDrivenEngineRef.get().shutdownNow();
            this.eventDrivenEngineRef.get().shutdownNow();
            LOG.info("Initiated immediate shutdown of flow controller...");
        } else {
            this.timerDrivenEngineRef.get().shutdown();
            this.eventDrivenEngineRef.get().shutdown();
            LOG.info("Initiated graceful shutdown of flow controller...waiting up to " + gracefulShutdownSeconds + " seconds");
        }
        clusterTaskExecutor.shutdownNow();
        if (zooKeeperStateServer != null) {
            zooKeeperStateServer.shutdown();
        }
        // Trigger any processors' methods marked with @OnShutdown to be called
        getRootGroup().shutdown();
        stateManagerProvider.shutdown();
        // invoke any methods annotated with @OnShutdown on Controller Services
        for (final ControllerServiceNode serviceNode : getAllControllerServices()) {
            try (final NarCloseable narCloseable = NarCloseable.withComponentNarLoader(serviceNode.getControllerServiceImplementation().getClass(), serviceNode.getIdentifier())) {
                final ConfigurationContext configContext = new StandardConfigurationContext(serviceNode, controllerServiceProvider, null, variableRegistry);
                ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnShutdown.class, serviceNode.getControllerServiceImplementation(), configContext);
            }
        }
        // invoke any methods annotated with @OnShutdown on Reporting Tasks
        for (final ReportingTaskNode taskNode : getAllReportingTasks()) {
            final ConfigurationContext configContext = taskNode.getConfigurationContext();
            try (final NarCloseable narCloseable = NarCloseable.withComponentNarLoader(taskNode.getReportingTask().getClass(), taskNode.getIdentifier())) {
                ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnShutdown.class, taskNode.getReportingTask(), configContext);
            }
        }
        try {
            this.timerDrivenEngineRef.get().awaitTermination(gracefulShutdownSeconds / 2, TimeUnit.SECONDS);
            this.eventDrivenEngineRef.get().awaitTermination(gracefulShutdownSeconds / 2, TimeUnit.SECONDS);
        } catch (final InterruptedException ie) {
            LOG.info("Interrupted while waiting for controller termination.");
        }
        try {
            flowFileRepository.close();
        } catch (final Throwable t) {
            LOG.warn("Unable to shut down FlowFileRepository due to {}", new Object[] { t });
        }
        if (this.timerDrivenEngineRef.get().isTerminated() && eventDrivenEngineRef.get().isTerminated()) {
            LOG.info("Controller has been terminated successfully.");
        } else {
            LOG.warn("Controller hasn't terminated properly.  There exists an uninterruptable thread that " + "will take an indeterminate amount of time to stop.  Might need to kill the program manually.");
        }
        for (final RemoteSiteListener listener : externalSiteListeners) {
            listener.stop();
        }
        if (processScheduler != null) {
            processScheduler.shutdown();
        }
        if (contentRepository != null) {
            contentRepository.shutdown();
        }
        if (provenanceRepository != null) {
            try {
                provenanceRepository.close();
            } catch (final IOException ioe) {
                LOG.warn("There was a problem shutting down the Provenance Repository: " + ioe.toString());
                if (LOG.isDebugEnabled()) {
                    LOG.warn("", ioe);
                }
            }
        }
    } finally {
        readLock.unlock();
    }
}
Also used : StandardConfigurationContext(org.apache.nifi.controller.service.StandardConfigurationContext) NarCloseable(org.apache.nifi.nar.NarCloseable) StandardConfigurationContext(org.apache.nifi.controller.service.StandardConfigurationContext) SocketRemoteSiteListener(org.apache.nifi.remote.SocketRemoteSiteListener) RemoteSiteListener(org.apache.nifi.remote.RemoteSiteListener) HttpRemoteSiteListener(org.apache.nifi.remote.HttpRemoteSiteListener) ControllerServiceNode(org.apache.nifi.controller.service.ControllerServiceNode) StandardReportingTaskNode(org.apache.nifi.controller.reporting.StandardReportingTaskNode) IOException(java.io.IOException)

Example 44 with NarCloseable

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

the class AbstractConfiguredComponent method getProperties.

@Override
public Map<PropertyDescriptor, String> getProperties() {
    try (final NarCloseable narCloseable = NarCloseable.withComponentNarLoader(getComponent().getClass(), getComponent().getIdentifier())) {
        final List<PropertyDescriptor> supported = getComponent().getPropertyDescriptors();
        if (supported == null || supported.isEmpty()) {
            return Collections.unmodifiableMap(properties);
        } else {
            final Map<PropertyDescriptor, String> props = new LinkedHashMap<>();
            for (final PropertyDescriptor descriptor : supported) {
                props.put(descriptor, null);
            }
            props.putAll(properties);
            return props;
        }
    }
}
Also used : NarCloseable(org.apache.nifi.nar.NarCloseable) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) LinkedHashMap(java.util.LinkedHashMap)

Example 45 with NarCloseable

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

the class AbstractConfiguredComponent method setProperties.

@Override
public void setProperties(final Map<String, String> properties, final boolean allowRemovalOfRequiredProperties) {
    if (properties == null) {
        return;
    }
    lock.lock();
    try {
        verifyModifiable();
        try (final NarCloseable narCloseable = NarCloseable.withComponentNarLoader(getComponent().getClass(), id)) {
            boolean classpathChanged = false;
            for (final Map.Entry<String, String> entry : properties.entrySet()) {
                // determine if any of the property changes require resetting the InstanceClassLoader
                final PropertyDescriptor descriptor = getComponent().getPropertyDescriptor(entry.getKey());
                if (descriptor.isDynamicClasspathModifier()) {
                    classpathChanged = true;
                }
                if (entry.getKey() != null && entry.getValue() == null) {
                    removeProperty(entry.getKey(), allowRemovalOfRequiredProperties);
                } else if (entry.getKey() != null) {
                    setProperty(entry.getKey(), CharacterFilterUtils.filterInvalidXmlCharacters(entry.getValue()));
                }
            }
            // if at least one property with dynamicallyModifiesClasspath(true) was set, then reload the component with the new urls
            if (classpathChanged) {
                logger.info("Updating classpath for " + this.componentType + " with the ID " + this.getIdentifier());
                final Set<URL> additionalUrls = getAdditionalClasspathResources(getComponent().getPropertyDescriptors());
                try {
                    reload(additionalUrls);
                } catch (Exception e) {
                    getLogger().error("Error reloading component with id " + id + ": " + e.getMessage(), e);
                }
            }
        }
    } finally {
        lock.unlock();
    }
}
Also used : NarCloseable(org.apache.nifi.nar.NarCloseable) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) ConcurrentMap(java.util.concurrent.ConcurrentMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) URL(java.net.URL) MalformedURLException(java.net.MalformedURLException)

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