Search in sources :

Example 36 with NarCloseable

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

the class StandardControllerServiceNode method enable.

/**
 * Will atomically enable this service by invoking its @OnEnabled operation.
 * It uses CAS operation on {@link #stateRef} to transition this service
 * from DISABLED to ENABLING state. If such transition succeeds the service
 * will be marked as 'active' (see {@link ControllerServiceNode#isActive()}).
 * If such transition doesn't succeed then no enabling logic will be
 * performed and the method will exit. In other words it is safe to invoke
 * this operation multiple times and from multiple threads.
 * <br>
 * This operation will also perform re-try of service enabling in the event
 * of exception being thrown by previous invocation of @OnEnabled.
 * <br>
 * Upon successful invocation of @OnEnabled this service will be transitioned to
 * ENABLED state.
 * <br>
 * In the event where enabling took longer then expected by the user and such user
 * initiated disable operation, this service will be automatically disabled as soon
 * as it reached ENABLED state.
 */
@Override
public CompletableFuture<Void> enable(final ScheduledExecutorService scheduler, final long administrativeYieldMillis) {
    final CompletableFuture<Void> future = new CompletableFuture<>();
    if (this.stateTransition.transitionToEnabling(ControllerServiceState.DISABLED, future)) {
        synchronized (active) {
            this.active.set(true);
        }
        final StandardControllerServiceNode service = this;
        final ConfigurationContext configContext = new StandardConfigurationContext(this, this.serviceProvider, null, getVariableRegistry());
        scheduler.execute(new Runnable() {

            @Override
            public void run() {
                try {
                    try (final NarCloseable nc = NarCloseable.withComponentNarLoader(getControllerServiceImplementation().getClass(), getIdentifier())) {
                        ReflectionUtils.invokeMethodsWithAnnotation(OnEnabled.class, getControllerServiceImplementation(), configContext);
                    }
                    boolean shouldEnable;
                    synchronized (active) {
                        shouldEnable = active.get() && stateTransition.enable();
                    }
                    if (!shouldEnable) {
                        LOG.debug("Disabling service {} after it has been enabled due to disable action being initiated.", service);
                        // Can only happen if user initiated DISABLE operation before service finished enabling. It's state will be
                        // set to DISABLING (see disable() operation)
                        invokeDisable(configContext);
                        stateTransition.disable();
                    } else {
                        LOG.debug("Successfully enabled {}", service);
                    }
                } catch (Exception e) {
                    future.completeExceptionally(e);
                    final Throwable cause = e instanceof InvocationTargetException ? e.getCause() : e;
                    final ComponentLog componentLog = new SimpleProcessLogger(getIdentifier(), StandardControllerServiceNode.this);
                    componentLog.error("Failed to invoke @OnEnabled method due to {}", cause);
                    LOG.error("Failed to invoke @OnEnabled method of {} due to {}", getControllerServiceImplementation(), cause.toString());
                    invokeDisable(configContext);
                    if (isActive()) {
                        scheduler.schedule(this, administrativeYieldMillis, TimeUnit.MILLISECONDS);
                    } else {
                        try (final NarCloseable nc = NarCloseable.withComponentNarLoader(getControllerServiceImplementation().getClass(), getIdentifier())) {
                            ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnDisabled.class, getControllerServiceImplementation(), configContext);
                        }
                        stateTransition.disable();
                    }
                }
            }
        });
    } else {
        future.complete(null);
    }
    return future;
}
Also used : ConfigurationContext(org.apache.nifi.controller.ConfigurationContext) NarCloseable(org.apache.nifi.nar.NarCloseable) OnEnabled(org.apache.nifi.annotation.lifecycle.OnEnabled) ComponentLog(org.apache.nifi.logging.ComponentLog) InvocationTargetException(java.lang.reflect.InvocationTargetException) ControllerServiceInstantiationException(org.apache.nifi.controller.exception.ControllerServiceInstantiationException) InvocationTargetException(java.lang.reflect.InvocationTargetException) CompletableFuture(java.util.concurrent.CompletableFuture) SimpleProcessLogger(org.apache.nifi.processor.SimpleProcessLogger)

Example 37 with NarCloseable

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

the class StandardControllerServiceNode method invokeDisable.

/**
 */
private void invokeDisable(ConfigurationContext configContext) {
    try (final NarCloseable nc = NarCloseable.withComponentNarLoader(getControllerServiceImplementation().getClass(), getIdentifier())) {
        ReflectionUtils.invokeMethodsWithAnnotation(OnDisabled.class, StandardControllerServiceNode.this.getControllerServiceImplementation(), configContext);
        LOG.debug("Successfully disabled {}", this);
    } catch (Exception e) {
        final Throwable cause = e instanceof InvocationTargetException ? e.getCause() : e;
        final ComponentLog componentLog = new SimpleProcessLogger(getIdentifier(), StandardControllerServiceNode.this);
        componentLog.error("Failed to invoke @OnDisabled method due to {}", cause);
        LOG.error("Failed to invoke @OnDisabled method of {} due to {}", getControllerServiceImplementation(), cause.toString());
    }
}
Also used : NarCloseable(org.apache.nifi.nar.NarCloseable) SimpleProcessLogger(org.apache.nifi.processor.SimpleProcessLogger) ComponentLog(org.apache.nifi.logging.ComponentLog) InvocationTargetException(java.lang.reflect.InvocationTargetException) ControllerServiceInstantiationException(org.apache.nifi.controller.exception.ControllerServiceInstantiationException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 38 with NarCloseable

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

the class FlowController method setPrimary.

public void setPrimary(final boolean primary) {
    final PrimaryNodeState nodeState = primary ? PrimaryNodeState.ELECTED_PRIMARY_NODE : PrimaryNodeState.PRIMARY_NODE_REVOKED;
    final ProcessGroup rootGroup = getGroup(getRootGroupId());
    for (final ProcessorNode procNode : rootGroup.findAllProcessors()) {
        try (final NarCloseable narCloseable = NarCloseable.withComponentNarLoader(procNode.getProcessor().getClass(), procNode.getIdentifier())) {
            ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnPrimaryNodeStateChange.class, procNode.getProcessor(), nodeState);
        }
    }
    for (final ControllerServiceNode serviceNode : getAllControllerServices()) {
        try (final NarCloseable narCloseable = NarCloseable.withComponentNarLoader(serviceNode.getControllerServiceImplementation().getClass(), serviceNode.getIdentifier())) {
            ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnPrimaryNodeStateChange.class, serviceNode.getControllerServiceImplementation(), nodeState);
        }
    }
    for (final ReportingTaskNode reportingTaskNode : getAllReportingTasks()) {
        try (final NarCloseable narCloseable = NarCloseable.withComponentNarLoader(reportingTaskNode.getReportingTask().getClass(), reportingTaskNode.getIdentifier())) {
            ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnPrimaryNodeStateChange.class, reportingTaskNode.getReportingTask(), nodeState);
        }
    }
    // update primary
    eventDrivenWorkerQueue.setPrimary(primary);
    // update the heartbeat bean
    final HeartbeatBean oldBean = this.heartbeatBeanRef.getAndSet(new HeartbeatBean(rootGroup, primary));
    // Emit a bulletin detailing the fact that the primary node state has changed
    if (oldBean == null || oldBean.isPrimary() != primary) {
        final String message = primary ? "This node has been elected Primary Node" : "This node is no longer Primary Node";
        final Bulletin bulletin = BulletinFactory.createBulletin("Primary Node", Severity.INFO.name(), message);
        bulletinRepository.addBulletin(bulletin);
        LOG.info(message);
    }
}
Also used : NarCloseable(org.apache.nifi.nar.NarCloseable) Bulletin(org.apache.nifi.reporting.Bulletin) ControllerServiceNode(org.apache.nifi.controller.service.ControllerServiceNode) StandardReportingTaskNode(org.apache.nifi.controller.reporting.StandardReportingTaskNode) VersionedProcessGroup(org.apache.nifi.registry.flow.VersionedProcessGroup) StandardProcessGroup(org.apache.nifi.groups.StandardProcessGroup) RemoteProcessGroup(org.apache.nifi.groups.RemoteProcessGroup) ProcessGroup(org.apache.nifi.groups.ProcessGroup) StandardRemoteProcessGroup(org.apache.nifi.remote.StandardRemoteProcessGroup) PrimaryNodeState(org.apache.nifi.annotation.notification.PrimaryNodeState)

Example 39 with NarCloseable

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

the class FlowController method reload.

@Override
public void reload(final ProcessorNode existingNode, final String newType, final BundleCoordinate bundleCoordinate, final Set<URL> additionalUrls) throws ProcessorInstantiationException {
    if (existingNode == null) {
        throw new IllegalStateException("Existing ProcessorNode cannot be null");
    }
    final String id = existingNode.getProcessor().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 });
    }
    // createProcessor 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);
    // create a new node with firstTimeAdded as true so lifecycle methods get fired
    // attempt the creation to make sure it works before firing the OnRemoved methods below
    final ProcessorNode newNode = createProcessor(newType, id, bundleCoordinate, additionalUrls, true, false);
    // call OnRemoved for the existing processor using the previous instance class loader
    try (final NarCloseable x = NarCloseable.withComponentNarLoader(existingInstanceClassLoader)) {
        final StateManager stateManager = getStateManagerProvider().getStateManager(id);
        final StandardProcessContext processContext = new StandardProcessContext(existingNode, controllerServiceProvider, encryptor, stateManager, () -> false);
        ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnRemoved.class, existingNode.getProcessor(), processContext);
    } finally {
        ExtensionManager.closeURLClassLoader(id, existingInstanceClassLoader);
    }
    // set the new processor in the existing node
    final ComponentLog componentLogger = new SimpleProcessLogger(id, newNode.getProcessor());
    final TerminationAwareLogger terminationAwareLogger = new TerminationAwareLogger(componentLogger);
    LogRepositoryFactory.getRepository(id).setLogger(terminationAwareLogger);
    final LoggableComponent<Processor> newProcessor = new LoggableComponent<>(newNode.getProcessor(), newNode.getBundleCoordinate(), terminationAwareLogger);
    existingNode.setProcessor(newProcessor);
    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) GhostProcessor(org.apache.nifi.processor.GhostProcessor) Processor(org.apache.nifi.processor.Processor) ComponentLog(org.apache.nifi.logging.ComponentLog) StateManager(org.apache.nifi.components.state.StateManager) NarThreadContextClassLoader(org.apache.nifi.nar.NarThreadContextClassLoader) StandardProcessContext(org.apache.nifi.processor.StandardProcessContext) SimpleProcessLogger(org.apache.nifi.processor.SimpleProcessLogger)

Example 40 with NarCloseable

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

the class FlowController method createControllerService.

@Override
public ControllerServiceNode createControllerService(final String type, final String id, final BundleCoordinate bundleCoordinate, final Set<URL> additionalUrls, final boolean firstTimeAdded) {
    final ControllerServiceNode serviceNode = controllerServiceProvider.createControllerService(type, id, bundleCoordinate, additionalUrls, firstTimeAdded);
    // 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 ControllerServiceLogObserver(getBulletinRepository(), serviceNode));
    if (firstTimeAdded) {
        final ControllerService service = serviceNode.getControllerServiceImplementation();
        try (final NarCloseable nc = NarCloseable.withComponentNarLoader(service.getClass(), service.getIdentifier())) {
            ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnConfigurationRestored.class, service);
        }
    }
    return serviceNode;
}
Also used : NarCloseable(org.apache.nifi.nar.NarCloseable) ControllerServiceNode(org.apache.nifi.controller.service.ControllerServiceNode) ControllerServiceLogObserver(org.apache.nifi.logging.ControllerServiceLogObserver) LogRepository(org.apache.nifi.logging.LogRepository)

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