Search in sources :

Example 6 with ConfigurationChangeException

use of org.apache.nifi.minifi.bootstrap.configuration.ConfigurationChangeException in project nifi-minifi by apache.

the class RunMiNiFi method start.

@SuppressWarnings({ "rawtypes", "unchecked" })
public void start() throws IOException, InterruptedException {
    final String confDir = getBootstrapProperties().getProperty(CONF_DIR_KEY);
    final File configFile = new File(getBootstrapProperties().getProperty(MINIFI_CONFIG_FILE_KEY));
    try (InputStream inputStream = new FileInputStream(configFile)) {
        ByteBuffer tempConfigFile = performTransformation(inputStream, confDir);
        currentConfigFileReference.set(tempConfigFile.asReadOnlyBuffer());
    } catch (ConfigurationChangeException e) {
        defaultLogger.error("The config file is malformed, unable to start.", e);
        return;
    }
    // Instantiate configuration listener and configured ingestors
    this.changeListener = new MiNiFiConfigurationChangeListener(this, defaultLogger);
    this.periodicStatusReporters = initializePeriodicNotifiers();
    startPeriodicNotifiers();
    try {
        this.changeCoordinator = initializeNotifier(this.changeListener);
    } catch (Exception e) {
        final String errorMsg = "Unable to start as {} is not properly configured due to: {}";
        cmdLogger.error(errorMsg, this.changeListener.getDescriptor(), e.getMessage());
        defaultLogger.error("Unable to initialize notifier.", e);
        // if we fail to initialize, exit without attempting to start
        System.exit(1);
    }
    Tuple<ProcessBuilder, Process> tuple = startMiNiFi();
    if (tuple == null) {
        cmdLogger.info("Start method returned null, ending start command.");
        return;
    }
    ProcessBuilder builder = tuple.getKey();
    Process process = tuple.getValue();
    try {
        while (true) {
            final boolean alive = isAlive(process);
            if (alive) {
                try {
                    Thread.sleep(1000L);
                    if (reloading.get() && getNifiStarted()) {
                        final File swapConfigFile = getSwapFile(defaultLogger);
                        if (swapConfigFile.exists()) {
                            defaultLogger.info("MiNiFi has finished reloading successfully and swap file exists. Deleting old configuration.");
                            if (swapConfigFile.delete()) {
                                defaultLogger.info("Swap file was successfully deleted.");
                            } else {
                                defaultLogger.error("Swap file was not deleted. It should be deleted manually.");
                            }
                        }
                        reloading.set(false);
                    }
                } catch (final InterruptedException ie) {
                }
            } else {
                final Runtime runtime = Runtime.getRuntime();
                try {
                    runtime.removeShutdownHook(shutdownHook);
                } catch (final IllegalStateException ise) {
                // happens when already shutting down
                }
                if (autoRestartNiFi) {
                    final File statusFile = getStatusFile(defaultLogger);
                    if (!statusFile.exists()) {
                        defaultLogger.info("Status File no longer exists. Will not restart MiNiFi");
                        return;
                    }
                    final File lockFile = getLockFile(defaultLogger);
                    if (lockFile.exists()) {
                        defaultLogger.info("A shutdown was initiated. Will not restart MiNiFi");
                        return;
                    }
                    final File reloadFile = getReloadFile(defaultLogger);
                    if (reloadFile.exists()) {
                        defaultLogger.info("Currently reloading configuration. Will wait to restart MiNiFi.");
                        Thread.sleep(5000L);
                        continue;
                    }
                    final boolean previouslyStarted = getNifiStarted();
                    if (!previouslyStarted) {
                        final File swapConfigFile = getSwapFile(defaultLogger);
                        if (swapConfigFile.exists()) {
                            defaultLogger.info("Swap file exists, MiNiFi failed trying to change configuration. Reverting to old configuration.");
                            try {
                                ByteBuffer tempConfigFile = performTransformation(new FileInputStream(swapConfigFile), confDir);
                                currentConfigFileReference.set(tempConfigFile.asReadOnlyBuffer());
                            } catch (ConfigurationChangeException e) {
                                defaultLogger.error("The swap file is malformed, unable to restart from prior state. Will not attempt to restart MiNiFi. Swap File should be cleaned up manually.");
                                return;
                            }
                            Files.copy(swapConfigFile.toPath(), Paths.get(getBootstrapProperties().getProperty(MINIFI_CONFIG_FILE_KEY)), REPLACE_EXISTING);
                            defaultLogger.info("Replacing config file with swap file and deleting swap file");
                            if (!swapConfigFile.delete()) {
                                defaultLogger.warn("The swap file failed to delete after replacing using it to revert to the old configuration. It should be cleaned up manually.");
                            }
                            reloading.set(false);
                        } else {
                            defaultLogger.info("MiNiFi either never started or failed to restart. Will not attempt to restart MiNiFi");
                            return;
                        }
                    } else {
                        setNiFiStarted(false);
                    }
                    process = builder.start();
                    handleLogging(process);
                    Long pid = getPid(process, defaultLogger);
                    if (pid != null) {
                        minifiPid = pid;
                        final Properties minifiProps = new Properties();
                        minifiProps.setProperty(PID_KEY, String.valueOf(minifiPid));
                        saveProperties(minifiProps, defaultLogger);
                    }
                    shutdownHook = new ShutdownHook(process, this, secretKey, gracefulShutdownSeconds, loggingExecutor);
                    runtime.addShutdownHook(shutdownHook);
                    final boolean started = waitForStart();
                    if (started) {
                        defaultLogger.info("Successfully spawned the thread to start Apache MiNiFi{}", (pid == null ? "" : " with PID " + pid));
                    } else {
                        defaultLogger.error("Apache MiNiFi does not appear to have started");
                    }
                } else {
                    return;
                }
            }
        }
    } finally {
        shutdownChangeNotifier();
        shutdownPeriodicStatusReporters();
    }
}
Also used : ObjectInputStream(java.io.ObjectInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) TeeInputStream(org.apache.commons.io.input.TeeInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ConfigurationChangeException(org.apache.nifi.minifi.bootstrap.configuration.ConfigurationChangeException) Properties(java.util.Properties) ByteBuffer(java.nio.ByteBuffer) FileInputStream(java.io.FileInputStream) ConfigurationChangeException(org.apache.nifi.minifi.bootstrap.configuration.ConfigurationChangeException) EOFException(java.io.EOFException) FileNotFoundException(java.io.FileNotFoundException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) File(java.io.File)

Example 7 with ConfigurationChangeException

use of org.apache.nifi.minifi.bootstrap.configuration.ConfigurationChangeException in project nifi-minifi by apache.

the class ConfigTransformer method addConnection.

protected static void addConnection(final Element parentElement, ConnectionSchema connectionProperties, ParentGroupIdResolver parentGroupIdResolver) throws ConfigurationChangeException {
    try {
        final Document doc = parentElement.getOwnerDocument();
        final Element element = doc.createElement("connection");
        parentElement.appendChild(element);
        addTextElement(element, "id", connectionProperties.getId());
        addTextElement(element, "name", connectionProperties.getName());
        final Element bendPointsElement = doc.createElement("bendPoints");
        element.appendChild(bendPointsElement);
        addTextElement(element, "labelIndex", "1");
        addTextElement(element, "zIndex", "0");
        addConnectionSourceOrDestination(element, "source", connectionProperties.getSourceId(), parentGroupIdResolver);
        addConnectionSourceOrDestination(element, "destination", connectionProperties.getDestinationId(), parentGroupIdResolver);
        List<String> sourceRelationshipNames = connectionProperties.getSourceRelationshipNames();
        if (sourceRelationshipNames.isEmpty()) {
            addTextElement(element, "relationship", null);
        } else {
            for (String relationshipName : sourceRelationshipNames) {
                addTextElement(element, "relationship", relationshipName);
            }
        }
        addTextElement(element, "maxWorkQueueSize", String.valueOf(connectionProperties.getMaxWorkQueueSize()));
        addTextElement(element, "maxWorkQueueDataSize", connectionProperties.getMaxWorkQueueDataSize());
        addTextElement(element, "flowFileExpiration", connectionProperties.getFlowfileExpiration());
        addTextElementIfNotNullOrEmpty(element, "queuePrioritizerClass", connectionProperties.getQueuePrioritizerClass());
        parentElement.appendChild(element);
    } catch (Exception e) {
        throw new ConfigurationChangeException("Failed to parse the config YAML while trying to add the connection from the Processor to the input port of the Remote Process Group", e);
    }
}
Also used : Element(org.w3c.dom.Element) ConfigurationChangeException(org.apache.nifi.minifi.bootstrap.configuration.ConfigurationChangeException) Document(org.w3c.dom.Document) InvalidConfigurationException(org.apache.nifi.minifi.bootstrap.exception.InvalidConfigurationException) ConfigurationChangeException(org.apache.nifi.minifi.bootstrap.configuration.ConfigurationChangeException) TransformerException(javax.xml.transform.TransformerException) DOMException(org.w3c.dom.DOMException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 8 with ConfigurationChangeException

use of org.apache.nifi.minifi.bootstrap.configuration.ConfigurationChangeException in project nifi-minifi by apache.

the class ConfigTransformer method addProcessor.

protected static void addProcessor(final Element parentElement, ProcessorSchema processorConfig) throws ConfigurationChangeException {
    try {
        final Document doc = parentElement.getOwnerDocument();
        final Element element = doc.createElement("processor");
        parentElement.appendChild(element);
        addTextElement(element, "id", processorConfig.getId());
        addTextElement(element, "name", processorConfig.getName());
        addPosition(element);
        addStyle(element);
        addTextElement(element, "comment", "");
        addTextElement(element, "class", processorConfig.getProcessorClass());
        addTextElement(element, "maxConcurrentTasks", String.valueOf(processorConfig.getMaxConcurrentTasks()));
        addTextElement(element, "schedulingPeriod", processorConfig.getSchedulingPeriod());
        addTextElement(element, "penalizationPeriod", processorConfig.getPenalizationPeriod());
        addTextElement(element, "yieldPeriod", processorConfig.getYieldPeriod());
        addTextElement(element, "bulletinLevel", "WARN");
        addTextElement(element, "lossTolerant", "false");
        addTextElement(element, "scheduledState", "RUNNING");
        addTextElement(element, "schedulingStrategy", processorConfig.getSchedulingStrategy());
        addTextElement(element, "runDurationNanos", String.valueOf(processorConfig.getRunDurationNanos()));
        String annotationData = processorConfig.getAnnotationData();
        if (annotationData != null && !annotationData.isEmpty()) {
            addTextElement(element, "annotationData", annotationData);
        }
        addConfiguration(element, processorConfig.getProperties());
        Collection<String> autoTerminatedRelationships = processorConfig.getAutoTerminatedRelationshipsList();
        if (autoTerminatedRelationships != null) {
            for (String rel : autoTerminatedRelationships) {
                addTextElement(element, "autoTerminatedRelationship", rel);
            }
        }
    } catch (Exception e) {
        throw new ConfigurationChangeException("Failed to parse the config YAML while trying to add a Processor", e);
    }
}
Also used : Element(org.w3c.dom.Element) ConfigurationChangeException(org.apache.nifi.minifi.bootstrap.configuration.ConfigurationChangeException) Document(org.w3c.dom.Document) InvalidConfigurationException(org.apache.nifi.minifi.bootstrap.exception.InvalidConfigurationException) ConfigurationChangeException(org.apache.nifi.minifi.bootstrap.configuration.ConfigurationChangeException) TransformerException(javax.xml.transform.TransformerException) DOMException(org.w3c.dom.DOMException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 9 with ConfigurationChangeException

use of org.apache.nifi.minifi.bootstrap.configuration.ConfigurationChangeException in project nifi-minifi by apache.

the class ConfigTransformer method addProvenanceReportingTask.

protected static void addProvenanceReportingTask(final Element element, ConfigSchema configSchema) throws ConfigurationChangeException {
    try {
        ProvenanceReportingSchema provenanceProperties = configSchema.getProvenanceReportingProperties();
        final Element taskElement = element.getOwnerDocument().createElement("reportingTask");
        addTextElement(taskElement, "id", "Provenance-Reporting");
        addTextElement(taskElement, "name", "Site-To-Site-Provenance-Reporting");
        addTextElement(taskElement, "comment", provenanceProperties.getComment());
        addTextElement(taskElement, "class", DEFAULT_PROV_REPORTING_TASK_CLASS);
        addTextElement(taskElement, "schedulingPeriod", provenanceProperties.getSchedulingPeriod());
        addTextElement(taskElement, "scheduledState", "RUNNING");
        addTextElement(taskElement, "schedulingStrategy", provenanceProperties.getSchedulingStrategy());
        Map<String, Object> attributes = new HashMap<>();
        attributes.put("Destination URL", provenanceProperties.getDestinationUrl());
        attributes.put("Input Port Name", provenanceProperties.getPortName());
        attributes.put("Instance URL", provenanceProperties.getOriginatingUrl());
        attributes.put("Compress Events", provenanceProperties.getUseCompression());
        attributes.put("Batch Size", provenanceProperties.getBatchSize());
        attributes.put("Communications Timeout", provenanceProperties.getTimeout());
        SecurityPropertiesSchema securityProps = configSchema.getSecurityProperties();
        if (securityProps.useSSL()) {
            attributes.put("SSL Context Service", "SSL-Context-Service");
        }
        addConfiguration(taskElement, attributes);
        element.appendChild(taskElement);
    } catch (Exception e) {
        throw new ConfigurationChangeException("Failed to parse the config YAML while trying to add the Provenance Reporting Task", e);
    }
}
Also used : HashMap(java.util.HashMap) SecurityPropertiesSchema(org.apache.nifi.minifi.commons.schema.SecurityPropertiesSchema) ProvenanceReportingSchema(org.apache.nifi.minifi.commons.schema.ProvenanceReportingSchema) Element(org.w3c.dom.Element) ConfigurationChangeException(org.apache.nifi.minifi.bootstrap.configuration.ConfigurationChangeException) InvalidConfigurationException(org.apache.nifi.minifi.bootstrap.exception.InvalidConfigurationException) ConfigurationChangeException(org.apache.nifi.minifi.bootstrap.configuration.ConfigurationChangeException) TransformerException(javax.xml.transform.TransformerException) DOMException(org.w3c.dom.DOMException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 10 with ConfigurationChangeException

use of org.apache.nifi.minifi.bootstrap.configuration.ConfigurationChangeException in project nifi-minifi by apache.

the class ConfigTransformer method addRemoteGroupPort.

protected static void addRemoteGroupPort(final Element parentElement, RemotePortSchema inputPort, String tagName) throws ConfigurationChangeException {
    try {
        final Document doc = parentElement.getOwnerDocument();
        final Element element = doc.createElement(tagName);
        parentElement.appendChild(element);
        addTextElement(element, "id", inputPort.getId());
        addTextElement(element, "name", inputPort.getName());
        addPosition(element);
        addTextElement(element, "comments", inputPort.getComment());
        addTextElement(element, "scheduledState", "RUNNING");
        addTextElement(element, "maxConcurrentTasks", String.valueOf(inputPort.getMax_concurrent_tasks()));
        addTextElement(element, "useCompression", String.valueOf(inputPort.getUseCompression()));
        parentElement.appendChild(element);
    } catch (Exception e) {
        throw new ConfigurationChangeException("Failed to parse the config YAML while trying to add the input port of the Remote Process Group", e);
    }
}
Also used : Element(org.w3c.dom.Element) ConfigurationChangeException(org.apache.nifi.minifi.bootstrap.configuration.ConfigurationChangeException) Document(org.w3c.dom.Document) InvalidConfigurationException(org.apache.nifi.minifi.bootstrap.exception.InvalidConfigurationException) ConfigurationChangeException(org.apache.nifi.minifi.bootstrap.configuration.ConfigurationChangeException) TransformerException(javax.xml.transform.TransformerException) DOMException(org.w3c.dom.DOMException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Aggregations

ConfigurationChangeException (org.apache.nifi.minifi.bootstrap.configuration.ConfigurationChangeException)11 IOException (java.io.IOException)10 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)9 TransformerException (javax.xml.transform.TransformerException)9 InvalidConfigurationException (org.apache.nifi.minifi.bootstrap.exception.InvalidConfigurationException)9 DOMException (org.w3c.dom.DOMException)9 Element (org.w3c.dom.Element)9 Document (org.w3c.dom.Document)5 HashMap (java.util.HashMap)3 SecurityPropertiesSchema (org.apache.nifi.minifi.commons.schema.SecurityPropertiesSchema)3 CorePropertiesSchema (org.apache.nifi.minifi.commons.schema.CorePropertiesSchema)2 ProcessGroupSchema (org.apache.nifi.minifi.commons.schema.ProcessGroupSchema)2 ProvenanceReportingSchema (org.apache.nifi.minifi.commons.schema.ProvenanceReportingSchema)2 RemotePortSchema (org.apache.nifi.minifi.commons.schema.RemotePortSchema)2 RemoteProcessGroupSchema (org.apache.nifi.minifi.commons.schema.RemoteProcessGroupSchema)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 EOFException (java.io.EOFException)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1