Search in sources :

Example 36 with ControllerServiceNode

use of org.apache.nifi.controller.service.ControllerServiceNode in project nifi by apache.

the class StandardProcessorNode method verifyCanStart.

@Override
public void verifyCanStart(final Set<ControllerServiceNode> ignoredReferences) {
    final ScheduledState currentState = getPhysicalScheduledState();
    if (currentState != ScheduledState.STOPPED && currentState != ScheduledState.DISABLED) {
        throw new IllegalStateException(this.getIdentifier() + " cannot be started because it is not stopped. Current state is " + currentState.name());
    }
    verifyNoActiveThreads();
    if (ignoredReferences != null) {
        final Set<String> ids = new HashSet<>();
        for (final ControllerServiceNode node : ignoredReferences) {
            ids.add(node.getIdentifier());
        }
        final Collection<ValidationResult> validationResults = getValidationErrors(ids);
        for (final ValidationResult result : validationResults) {
            if (!result.isValid()) {
                throw new IllegalStateException(this.getIdentifier() + " cannot be started because it is not valid: " + result);
            }
        }
    } else {
        if (!isValid()) {
            throw new IllegalStateException(this.getIdentifier() + " is not in a valid state");
        }
    }
}
Also used : ControllerServiceNode(org.apache.nifi.controller.service.ControllerServiceNode) ValidationResult(org.apache.nifi.components.ValidationResult) HashSet(java.util.HashSet)

Example 37 with ControllerServiceNode

use of org.apache.nifi.controller.service.ControllerServiceNode in project nifi by apache.

the class AbstractReportingTaskNode method verifyCanStart.

@Override
public void verifyCanStart(final Set<ControllerServiceNode> ignoredReferences) {
    switch(getScheduledState()) {
        case DISABLED:
            throw new IllegalStateException(this.getIdentifier() + " cannot be started because it is disabled");
        case RUNNING:
            throw new IllegalStateException(this.getIdentifier() + " cannot be started because it is already running");
        case STOPPED:
            break;
    }
    final int activeThreadCount = getActiveThreadCount();
    if (activeThreadCount > 0) {
        throw new IllegalStateException(this.getIdentifier() + " cannot be started because it has " + activeThreadCount + " active threads already");
    }
    final Set<String> ids = new HashSet<>();
    for (final ControllerServiceNode node : ignoredReferences) {
        ids.add(node.getIdentifier());
    }
    final Collection<ValidationResult> validationResults = getValidationErrors(ids);
    for (final ValidationResult result : validationResults) {
        if (!result.isValid()) {
            throw new IllegalStateException(this.getIdentifier() + " cannot be started because it is not valid: " + result);
        }
    }
}
Also used : ControllerServiceNode(org.apache.nifi.controller.service.ControllerServiceNode) ValidationResult(org.apache.nifi.components.ValidationResult) HashSet(java.util.HashSet)

Example 38 with ControllerServiceNode

use of org.apache.nifi.controller.service.ControllerServiceNode in project nifi by apache.

the class StandardFlowSerializer method serialize.

@Override
public void serialize(final FlowController controller, final OutputStream os, final ScheduledStateLookup scheduledStateLookup) throws FlowSerializationException {
    try {
        // create a new, empty document
        final DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        docFactory.setNamespaceAware(true);
        final DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        final Document doc = docBuilder.newDocument();
        // populate document with controller state
        final Element rootNode = doc.createElement("flowController");
        rootNode.setAttribute("encoding-version", MAX_ENCODING_VERSION);
        doc.appendChild(rootNode);
        addTextElement(rootNode, "maxTimerDrivenThreadCount", controller.getMaxTimerDrivenThreadCount());
        addTextElement(rootNode, "maxEventDrivenThreadCount", controller.getMaxEventDrivenThreadCount());
        final Element registriesElement = doc.createElement("registries");
        rootNode.appendChild(registriesElement);
        addFlowRegistries(registriesElement, controller.getFlowRegistryClient());
        addProcessGroup(rootNode, controller.getGroup(controller.getRootGroupId()), "rootGroup", scheduledStateLookup);
        // Add root-level controller services
        final Element controllerServicesNode = doc.createElement("controllerServices");
        rootNode.appendChild(controllerServicesNode);
        for (final ControllerServiceNode serviceNode : controller.getRootControllerServices()) {
            addControllerService(controllerServicesNode, serviceNode);
        }
        final Element reportingTasksNode = doc.createElement("reportingTasks");
        rootNode.appendChild(reportingTasksNode);
        for (final ReportingTaskNode taskNode : controller.getAllReportingTasks()) {
            addReportingTask(reportingTasksNode, taskNode, encryptor);
        }
        final DOMSource domSource = new DOMSource(doc);
        final StreamResult streamResult = new StreamResult(new BufferedOutputStream(os));
        // configure the transformer and convert the DOM
        final TransformerFactory transformFactory = TransformerFactory.newInstance();
        final Transformer transformer = transformFactory.newTransformer();
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        // transform the document to byte stream
        transformer.transform(domSource, streamResult);
    } catch (final ParserConfigurationException | DOMException | TransformerFactoryConfigurationError | IllegalArgumentException | TransformerException e) {
        throw new FlowSerializationException(e);
    }
}
Also used : TransformerFactoryConfigurationError(javax.xml.transform.TransformerFactoryConfigurationError) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) DOMException(org.w3c.dom.DOMException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ControllerServiceNode(org.apache.nifi.controller.service.ControllerServiceNode) ReportingTaskNode(org.apache.nifi.controller.ReportingTaskNode) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) BufferedOutputStream(java.io.BufferedOutputStream) TransformerException(javax.xml.transform.TransformerException)

Example 39 with ControllerServiceNode

use of org.apache.nifi.controller.service.ControllerServiceNode in project nifi by apache.

the class AbstractConfiguredComponent method removeProperty.

/**
 * Removes the property and value for the given property name if a
 * descriptor and value exists for the given name. If the property is
 * optional its value might be reset to default or will be removed entirely
 * if was a dynamic property.
 *
 * @param name the property to remove
 * @param allowRemovalOfRequiredProperties whether or not the property should be removed if it's required
 * @return true if removed; false otherwise
 * @throws java.lang.IllegalArgumentException if the name is null
 */
private boolean removeProperty(final String name, final boolean allowRemovalOfRequiredProperties) {
    if (null == name) {
        throw new IllegalArgumentException("Name can not be null");
    }
    final PropertyDescriptor descriptor = getComponent().getPropertyDescriptor(name);
    String value = null;
    final boolean allowRemoval = allowRemovalOfRequiredProperties || !descriptor.isRequired();
    if (allowRemoval && (value = properties.remove(descriptor)) != null) {
        if (descriptor.getControllerServiceDefinition() != null) {
            if (value != null) {
                final ControllerServiceNode oldNode = serviceProvider.getControllerServiceNode(value);
                if (oldNode != null) {
                    oldNode.removeReference(this);
                }
            }
        }
        try {
            onPropertyModified(descriptor, value, null);
        } catch (final Exception e) {
            getLogger().error(e.getMessage(), e);
        }
        return true;
    }
    return false;
}
Also used : PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) ControllerServiceNode(org.apache.nifi.controller.service.ControllerServiceNode) MalformedURLException(java.net.MalformedURLException)

Example 40 with ControllerServiceNode

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

Aggregations

ControllerServiceNode (org.apache.nifi.controller.service.ControllerServiceNode)88 HashSet (java.util.HashSet)29 ProcessGroup (org.apache.nifi.groups.ProcessGroup)26 HashMap (java.util.HashMap)25 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)25 ArrayList (java.util.ArrayList)24 Map (java.util.Map)24 LinkedHashSet (java.util.LinkedHashSet)22 Test (org.junit.Test)19 BundleCoordinate (org.apache.nifi.bundle.BundleCoordinate)18 ProcessorNode (org.apache.nifi.controller.ProcessorNode)18 ConfiguredComponent (org.apache.nifi.controller.ConfiguredComponent)17 VersionedProcessGroup (org.apache.nifi.registry.flow.VersionedProcessGroup)17 Set (java.util.Set)16 Connection (org.apache.nifi.connectable.Connection)16 List (java.util.List)15 Port (org.apache.nifi.connectable.Port)15 Label (org.apache.nifi.controller.label.Label)15 RemoteProcessGroup (org.apache.nifi.groups.RemoteProcessGroup)15 RemoteGroupPort (org.apache.nifi.remote.RemoteGroupPort)15