bejancsaba commented on code in PR #6075: URL: https://github.com/apache/nifi/pull/6075#discussion_r891077674
########## minifi/minifi-bootstrap/src/main/java/org/apache/nifi/minifi/bootstrap/service/MiNiFiConfigurationChangeListener.java: ########## @@ -0,0 +1,215 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.nifi.minifi.bootstrap.service; + +import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; +import static org.apache.nifi.minifi.bootstrap.RunMiNiFi.CONF_DIR_KEY; +import static org.apache.nifi.minifi.bootstrap.RunMiNiFi.MINIFI_CONFIG_FILE_KEY; +import static org.apache.nifi.minifi.bootstrap.configuration.ingestors.PullHttpChangeIngestor.OVERRIDE_SECURITY; +import static org.apache.nifi.minifi.bootstrap.configuration.ingestors.PullHttpChangeIngestor.PULL_HTTP_BASE_KEY; +import static org.apache.nifi.minifi.bootstrap.util.ConfigTransformer.generateConfigFiles; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.nio.ByteBuffer; +import java.nio.file.Files; +import java.util.Properties; +import java.util.concurrent.locks.ReentrantLock; +import org.apache.commons.io.IOUtils; +import org.apache.nifi.minifi.bootstrap.RunMiNiFi; +import org.apache.nifi.minifi.bootstrap.configuration.ConfigurationChangeException; +import org.apache.nifi.minifi.bootstrap.configuration.ConfigurationChangeListener; +import org.apache.nifi.minifi.bootstrap.util.ByteBufferInputStream; +import org.apache.nifi.minifi.bootstrap.util.ConfigTransformer; +import org.apache.nifi.minifi.commons.schema.ConfigSchema; +import org.apache.nifi.minifi.commons.schema.common.ConvertableSchema; +import org.apache.nifi.minifi.commons.schema.serialization.SchemaLoader; +import org.slf4j.Logger; + +public class MiNiFiConfigurationChangeListener implements ConfigurationChangeListener { + + private final RunMiNiFi runner; + private final Logger logger; + private final BootstrapFileProvider bootstrapFileProvider; + + private static final ReentrantLock handlingLock = new ReentrantLock(); + + public MiNiFiConfigurationChangeListener(RunMiNiFi runner, Logger logger, BootstrapFileProvider bootstrapFileProvider) { + this.runner = runner; + this.logger = logger; + this.bootstrapFileProvider = bootstrapFileProvider; + } + + @Override + public void handleChange(InputStream configInputStream) throws ConfigurationChangeException { + logger.info("Received notification of a change"); + + if (!handlingLock.tryLock()) { + throw new ConfigurationChangeException("Instance is already handling another change"); + } + // Store the incoming stream as a byte array to be shared among components that need it + try(ByteArrayOutputStream bufferedConfigOs = new ByteArrayOutputStream()) { + + Properties bootstrapProperties = bootstrapFileProvider.getBootstrapProperties(); + File configFile = new File(bootstrapProperties.getProperty(MINIFI_CONFIG_FILE_KEY)); + + byte[] copyArray = new byte[1024]; + int available; + while ((available = configInputStream.read(copyArray)) > 0) { + bufferedConfigOs.write(copyArray, 0, available); + } + + File swapConfigFile = bootstrapFileProvider.getSwapFile(); + logger.info("Persisting old configuration to {}", swapConfigFile.getAbsolutePath()); + + try (FileInputStream configFileInputStream = new FileInputStream(configFile)) { + Files.copy(configFileInputStream, swapConfigFile.toPath(), REPLACE_EXISTING); + } + + persistBackNonFlowSectionsFromOriginalSchema(bufferedConfigOs.toByteArray(), bootstrapProperties, configFile); + + // Create an input stream to feed to the config transformer + try (ByteArrayInputStream newConfigBais = new ByteArrayInputStream(IOUtils.toByteArray(new FileInputStream(configFile)))) { + newConfigBais.mark(-1); + + try { + String confDir = bootstrapProperties.getProperty(CONF_DIR_KEY); + + try { + logger.info("Performing transformation for input and saving outputs to {}", confDir); + ByteBuffer tempConfigFile = generateConfigFiles(newConfigBais, confDir, bootstrapFileProvider.getBootstrapProperties()); + runner.getConfigFileReference().set(tempConfigFile.asReadOnlyBuffer()); + + try { + logger.info("Reloading instance with new configuration"); + restartInstance(); + } catch (Exception e) { + logger.debug("Transformation of new config file failed after transformation into Flow.xml and nifi.properties, reverting."); + try (FileInputStream swapConfigFileStream = new FileInputStream(swapConfigFile)) { + ByteBuffer resetConfigFile = generateConfigFiles(swapConfigFileStream, confDir, bootstrapFileProvider.getBootstrapProperties()); + runner.getConfigFileReference().set(resetConfigFile.asReadOnlyBuffer()); + } + throw e; + } + } catch (Exception e) { + logger.debug("Transformation of new config file failed after replacing original with the swap file, reverting."); + try (FileInputStream swapConfigFileStream = new FileInputStream(swapConfigFile)) { + Files.copy(swapConfigFileStream, configFile.toPath(), REPLACE_EXISTING); + } + throw e; + } + } catch (Exception e) { + logger.debug("Transformation of new config file failed after swap file was created, deleting it."); + if (!swapConfigFile.delete()) { + logger.warn("The swap file failed to delete after a failed handling of a change. It should be cleaned up manually."); + } + throw e; + } + } + } catch (ConfigurationChangeException e){ + logger.error("Unable to carry out reloading of configuration on receipt of notification event", e); + throw e; + } catch (IOException ioe) { + logger.error("Unable to carry out reloading of configuration on receipt of notification event", ioe); + throw new ConfigurationChangeException("Unable to perform reload of received configuration change", ioe); + } finally { + try { + if (configInputStream != null) { + configInputStream.close() ; + } + } catch (IOException e) { + // Quietly close + } Review Comment: Used IOUtils.closeQuitely, thanks for the suggestion, wasn't aware of that -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
