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();
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations