Search in sources :

Example 11 with NarCloseable

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

the class FlowController method reload.

@Override
public void reload(final ControllerServiceNode existingNode, final String newType, final BundleCoordinate bundleCoordinate, final Set<URL> additionalUrls) throws ControllerServiceInstantiationException {
    if (existingNode == null) {
        throw new IllegalStateException("Existing ControllerServiceNode cannot be null");
    }
    final String id = existingNode.getIdentifier();
    // ghost components will have a null logger
    if (existingNode.getLogger() != null) {
        existingNode.getLogger().debug("Reloading component {} to type {} from bundle {}", new Object[] { id, newType, bundleCoordinate });
    }
    // createControllerService will create a new instance class loader for the same id so
    // save the instance class loader to use it for calling OnRemoved on the existing service
    final ClassLoader existingInstanceClassLoader = ExtensionManager.getInstanceClassLoader(id);
    // create a new node with firstTimeAdded as true so lifecycle methods get called
    // attempt the creation to make sure it works before firing the OnRemoved methods below
    final ControllerServiceNode newNode = controllerServiceProvider.createControllerService(newType, id, bundleCoordinate, additionalUrls, true);
    // call OnRemoved for the existing service using the previous instance class loader
    try (final NarCloseable x = NarCloseable.withComponentNarLoader(existingInstanceClassLoader)) {
        final ConfigurationContext configurationContext = new StandardConfigurationContext(existingNode, controllerServiceProvider, null, variableRegistry);
        ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnRemoved.class, existingNode.getControllerServiceImplementation(), configurationContext);
    } finally {
        ExtensionManager.closeURLClassLoader(id, existingInstanceClassLoader);
    }
    // take the invocation handler that was created for new proxy and is set to look at the new node,
    // and set it to look at the existing node
    final ControllerServiceInvocationHandler invocationHandler = newNode.getInvocationHandler();
    invocationHandler.setServiceNode(existingNode);
    // create LoggableComponents for the proxy and implementation
    final ComponentLog componentLogger = new SimpleProcessLogger(id, newNode.getControllerServiceImplementation());
    final TerminationAwareLogger terminationAwareLogger = new TerminationAwareLogger(componentLogger);
    LogRepositoryFactory.getRepository(id).setLogger(terminationAwareLogger);
    final LoggableComponent<ControllerService> loggableProxy = new LoggableComponent<>(newNode.getProxiedControllerService(), bundleCoordinate, terminationAwareLogger);
    final LoggableComponent<ControllerService> loggableImplementation = new LoggableComponent<>(newNode.getControllerServiceImplementation(), bundleCoordinate, terminationAwareLogger);
    // set the new impl, proxy, and invocation handler into the existing node
    existingNode.setControllerServiceAndProxy(loggableImplementation, loggableProxy, invocationHandler);
    existingNode.setExtensionMissing(newNode.isExtensionMissing());
    // need to refresh the properties in case we are changing from ghost component to real component
    existingNode.refreshProperties();
}
Also used : NarCloseable(org.apache.nifi.nar.NarCloseable) StandardConfigurationContext(org.apache.nifi.controller.service.StandardConfigurationContext) ControllerServiceInvocationHandler(org.apache.nifi.controller.service.ControllerServiceInvocationHandler) ComponentLog(org.apache.nifi.logging.ComponentLog) StandardConfigurationContext(org.apache.nifi.controller.service.StandardConfigurationContext) ControllerServiceNode(org.apache.nifi.controller.service.ControllerServiceNode) NarThreadContextClassLoader(org.apache.nifi.nar.NarThreadContextClassLoader) SimpleProcessLogger(org.apache.nifi.processor.SimpleProcessLogger)

Example 12 with NarCloseable

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

the class FlowController method createConnection.

/**
 * Creates a connection between two Connectable objects.
 *
 * @param id required ID of the connection
 * @param name the name of the connection, or <code>null</code> to leave the
 * connection unnamed
 * @param source required source
 * @param destination required destination
 * @param relationshipNames required collection of relationship names
 * @return
 *
 * @throws NullPointerException if the ID, source, destination, or set of
 * relationships is null.
 * @throws IllegalArgumentException if <code>relationships</code> is an
 * empty collection
 */
public Connection createConnection(final String id, final String name, final Connectable source, final Connectable destination, final Collection<String> relationshipNames) {
    final StandardConnection.Builder builder = new StandardConnection.Builder(processScheduler);
    final List<Relationship> relationships = new ArrayList<>();
    for (final String relationshipName : requireNonNull(relationshipNames)) {
        relationships.add(new Relationship.Builder().name(relationshipName).build());
    }
    // Create and initialize a FlowFileSwapManager for this connection
    final FlowFileSwapManager swapManager = createSwapManager(nifiProperties);
    final EventReporter eventReporter = createEventReporter(getBulletinRepository());
    try (final NarCloseable narCloseable = NarCloseable.withNarLoader()) {
        final SwapManagerInitializationContext initializationContext = new SwapManagerInitializationContext() {

            @Override
            public ResourceClaimManager getResourceClaimManager() {
                return resourceClaimManager;
            }

            @Override
            public FlowFileRepository getFlowFileRepository() {
                return flowFileRepository;
            }

            @Override
            public EventReporter getEventReporter() {
                return eventReporter;
            }
        };
        swapManager.initialize(initializationContext);
    }
    return builder.id(requireNonNull(id).intern()).name(name == null ? null : name.intern()).relationships(relationships).source(requireNonNull(source)).destination(destination).swapManager(swapManager).queueSwapThreshold(nifiProperties.getQueueSwapThreshold()).eventReporter(eventReporter).resourceClaimManager(resourceClaimManager).flowFileRepository(flowFileRepository).provenanceRepository(provenanceRepository).build();
}
Also used : NarCloseable(org.apache.nifi.nar.NarCloseable) Relationship(org.apache.nifi.processor.Relationship) ArrayList(java.util.ArrayList) FlowFileSwapManager(org.apache.nifi.controller.repository.FlowFileSwapManager) StandardConnection(org.apache.nifi.connectable.StandardConnection) SwapManagerInitializationContext(org.apache.nifi.controller.repository.SwapManagerInitializationContext) EventReporter(org.apache.nifi.events.EventReporter)

Example 13 with NarCloseable

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

the class FlowController method removeRootControllerService.

public void removeRootControllerService(final ControllerServiceNode service) {
    final ControllerServiceNode existing = rootControllerServices.get(requireNonNull(service).getIdentifier());
    if (existing == null) {
        throw new IllegalStateException(service + " is not a member of this Process Group");
    }
    service.verifyCanDelete();
    try (final NarCloseable x = NarCloseable.withComponentNarLoader(service.getControllerServiceImplementation().getClass(), service.getIdentifier())) {
        final ConfigurationContext configurationContext = new StandardConfigurationContext(service, controllerServiceProvider, null, variableRegistry);
        ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnRemoved.class, service.getControllerServiceImplementation(), configurationContext);
    }
    for (final Map.Entry<PropertyDescriptor, String> entry : service.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 referencedNode = getRootControllerService(value);
                if (referencedNode != null) {
                    referencedNode.removeReference(service);
                }
            }
        }
    }
    rootControllerServices.remove(service.getIdentifier());
    getStateManagerProvider().onComponentRemoved(service.getIdentifier());
    ExtensionManager.removeInstanceClassLoader(service.getIdentifier());
    LOG.info("{} removed from Flow Controller", service, this);
}
Also used : StandardConfigurationContext(org.apache.nifi.controller.service.StandardConfigurationContext) NarCloseable(org.apache.nifi.nar.NarCloseable) StandardConfigurationContext(org.apache.nifi.controller.service.StandardConfigurationContext) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) 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 14 with NarCloseable

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

the class FlowController method reload.

@Override
public void reload(final ReportingTaskNode existingNode, final String newType, final BundleCoordinate bundleCoordinate, final Set<URL> additionalUrls) throws ReportingTaskInstantiationException {
    if (existingNode == null) {
        throw new IllegalStateException("Existing ReportingTaskNode cannot be null");
    }
    final String id = existingNode.getReportingTask().getIdentifier();
    // ghost components will have a null logger
    if (existingNode.getLogger() != null) {
        existingNode.getLogger().debug("Reloading component {} to type {} from bundle {}", new Object[] { id, newType, bundleCoordinate });
    }
    // createReportingTask will create a new instance class loader for the same id so
    // save the instance class loader to use it for calling OnRemoved on the existing processor
    final ClassLoader existingInstanceClassLoader = ExtensionManager.getInstanceClassLoader(id);
    // set firstTimeAdded to true so lifecycle annotations get fired, but don't register this node
    // attempt the creation to make sure it works before firing the OnRemoved methods below
    final ReportingTaskNode newNode = createReportingTask(newType, id, bundleCoordinate, additionalUrls, true, false);
    // call OnRemoved for the existing reporting task using the previous instance class loader
    try (final NarCloseable x = NarCloseable.withComponentNarLoader(existingInstanceClassLoader)) {
        ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnRemoved.class, existingNode.getReportingTask(), existingNode.getConfigurationContext());
    } finally {
        ExtensionManager.closeURLClassLoader(id, existingInstanceClassLoader);
    }
    // set the new reporting task into the existing node
    final ComponentLog componentLogger = new SimpleProcessLogger(id, existingNode.getReportingTask());
    final TerminationAwareLogger terminationAwareLogger = new TerminationAwareLogger(componentLogger);
    LogRepositoryFactory.getRepository(id).setLogger(terminationAwareLogger);
    final LoggableComponent<ReportingTask> newReportingTask = new LoggableComponent<>(newNode.getReportingTask(), newNode.getBundleCoordinate(), terminationAwareLogger);
    existingNode.setReportingTask(newReportingTask);
    existingNode.setExtensionMissing(newNode.isExtensionMissing());
    // need to refresh the properties in case we are changing from ghost component to real component
    existingNode.refreshProperties();
}
Also used : NarCloseable(org.apache.nifi.nar.NarCloseable) StandardReportingTaskNode(org.apache.nifi.controller.reporting.StandardReportingTaskNode) NarThreadContextClassLoader(org.apache.nifi.nar.NarThreadContextClassLoader) SimpleProcessLogger(org.apache.nifi.processor.SimpleProcessLogger) ComponentLog(org.apache.nifi.logging.ComponentLog) ReportingTask(org.apache.nifi.reporting.ReportingTask) GhostReportingTask(org.apache.nifi.reporting.GhostReportingTask)

Example 15 with NarCloseable

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

the class ProcessorInitializer method teardown.

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

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