ferencerdei commented on code in PR #6160:
URL: https://github.com/apache/nifi/pull/6160#discussion_r911639888


##########
minifi/minifi-bootstrap/src/main/java/org/apache/nifi/minifi/bootstrap/util/ConfigTransformer.java:
##########
@@ -750,6 +756,72 @@ protected static void addTextElement(final Element 
element, final String name, f
         element.appendChild(toAdd);
     }
 
+    public static ByteBuffer overrideNonFlowSectionsFromOriginalSchema(byte[] 
newSchema, ByteBuffer currentConfigScheme, Properties bootstrapProperties)
+        throws IOException, InvalidConfigurationException, 
SchemaLoaderException {
+        try {
+            boolean overrideCoreProperties = 
ConfigTransformer.overrideCoreProperties(bootstrapProperties);
+            boolean overrideSecurityProperties = 
ConfigTransformer.overrideSecurityProperties(bootstrapProperties);
+            if (overrideCoreProperties && overrideSecurityProperties) {
+                return ByteBuffer.wrap(newSchema);
+            } else {
+                ConvertableSchema<ConfigSchema> schemaNew = ConfigTransformer
+                    
.throwIfInvalid(SchemaLoader.loadConvertableSchemaFromYaml(new 
ByteArrayInputStream(newSchema)));
+                ConfigSchema configSchemaNew = 
ConfigTransformer.throwIfInvalid(schemaNew.convert());
+                ConvertableSchema<ConfigSchema> schemaOld = ConfigTransformer
+                    
.throwIfInvalid(SchemaLoader.loadConvertableSchemaFromYaml(new 
ByteBufferInputStream(currentConfigScheme)));
+                ConfigSchema configSchemaOld = 
ConfigTransformer.throwIfInvalid(schemaOld.convert());
+
+                
configSchemaNew.setNifiPropertiesOverrides(configSchemaOld.getNifiPropertiesOverrides());
+
+                if (!overrideCoreProperties) {
+                    logger.debug("Preserving previous core properties...");
+                    
configSchemaNew.setCoreProperties(configSchemaOld.getCoreProperties());
+                }
+
+                if (!overrideSecurityProperties) {
+                    logger.debug("Preserving previous security properties...");
+                    
configSchemaNew.setSecurityProperties(configSchemaOld.getSecurityProperties());
+                }
+
+                StringWriter writer = new StringWriter();
+                SchemaLoader.toYaml(configSchemaNew, writer);
+                return 
ByteBuffer.wrap(writer.toString().getBytes()).asReadOnlyBuffer();
+            }
+        } catch (Exception e) {
+            logger.error("Loading the old and the new schema for merging was 
not successful", e);
+            throw e;
+        }
+    }
+
+    private static boolean overrideSecurityProperties(Properties properties) {
+        String overrideSecurityProperties = (String) 
properties.getOrDefault(OVERRIDE_SECURITY, "false");
+        boolean overrideSecurity;
+        if ("true".equalsIgnoreCase(overrideSecurityProperties) || 
"false".equalsIgnoreCase(overrideSecurityProperties)) {
+            overrideSecurity = 
Boolean.parseBoolean(overrideSecurityProperties);
+        } else {
+            throw new IllegalArgumentException(
+                "Property, " + OVERRIDE_SECURITY + ", to specify whether to 
override security properties must either be a value boolean value (\"true\" or 
\"false\")" +
+                    " or left to the default value of \"false\". It is set to 
\"" + overrideSecurityProperties + "\".");
+        }

Review Comment:
   no reason, it was a leftover from the existing code. Changing both to simple 
parseBoolean



##########
minifi/minifi-bootstrap/src/main/java/org/apache/nifi/minifi/bootstrap/util/ConfigTransformer.java:
##########
@@ -750,6 +756,72 @@ protected static void addTextElement(final Element 
element, final String name, f
         element.appendChild(toAdd);
     }
 
+    public static ByteBuffer overrideNonFlowSectionsFromOriginalSchema(byte[] 
newSchema, ByteBuffer currentConfigScheme, Properties bootstrapProperties)
+        throws IOException, InvalidConfigurationException, 
SchemaLoaderException {
+        try {
+            boolean overrideCoreProperties = 
ConfigTransformer.overrideCoreProperties(bootstrapProperties);
+            boolean overrideSecurityProperties = 
ConfigTransformer.overrideSecurityProperties(bootstrapProperties);
+            if (overrideCoreProperties && overrideSecurityProperties) {
+                return ByteBuffer.wrap(newSchema);
+            } else {
+                ConvertableSchema<ConfigSchema> schemaNew = ConfigTransformer
+                    
.throwIfInvalid(SchemaLoader.loadConvertableSchemaFromYaml(new 
ByteArrayInputStream(newSchema)));
+                ConfigSchema configSchemaNew = 
ConfigTransformer.throwIfInvalid(schemaNew.convert());
+                ConvertableSchema<ConfigSchema> schemaOld = ConfigTransformer
+                    
.throwIfInvalid(SchemaLoader.loadConvertableSchemaFromYaml(new 
ByteBufferInputStream(currentConfigScheme)));
+                ConfigSchema configSchemaOld = 
ConfigTransformer.throwIfInvalid(schemaOld.convert());
+
+                
configSchemaNew.setNifiPropertiesOverrides(configSchemaOld.getNifiPropertiesOverrides());
+
+                if (!overrideCoreProperties) {
+                    logger.debug("Preserving previous core properties...");
+                    
configSchemaNew.setCoreProperties(configSchemaOld.getCoreProperties());
+                }
+
+                if (!overrideSecurityProperties) {
+                    logger.debug("Preserving previous security properties...");
+                    
configSchemaNew.setSecurityProperties(configSchemaOld.getSecurityProperties());
+                }
+
+                StringWriter writer = new StringWriter();
+                SchemaLoader.toYaml(configSchemaNew, writer);
+                return 
ByteBuffer.wrap(writer.toString().getBytes()).asReadOnlyBuffer();
+            }
+        } catch (Exception e) {
+            logger.error("Loading the old and the new schema for merging was 
not successful", e);
+            throw e;
+        }
+    }
+
+    private static boolean overrideSecurityProperties(Properties properties) {
+        String overrideSecurityProperties = (String) 
properties.getOrDefault(OVERRIDE_SECURITY, "false");
+        boolean overrideSecurity;
+        if ("true".equalsIgnoreCase(overrideSecurityProperties) || 
"false".equalsIgnoreCase(overrideSecurityProperties)) {
+            overrideSecurity = 
Boolean.parseBoolean(overrideSecurityProperties);
+        } else {
+            throw new IllegalArgumentException(
+                "Property, " + OVERRIDE_SECURITY + ", to specify whether to 
override security properties must either be a value boolean value (\"true\" or 
\"false\")" +
+                    " or left to the default value of \"false\". It is set to 
\"" + overrideSecurityProperties + "\".");
+        }
+
+        return overrideSecurity;
+    }
+
+    private static boolean overrideCoreProperties(Properties properties) {
+        String overrideCorePropertiesKey = PULL_HTTP_BASE_KEY + 
".override.core";
+        String overrideCoreProps = (String) 
properties.getOrDefault(overrideCorePropertiesKey, "false");
+        boolean overrideCoreProperties;
+        if ("true".equalsIgnoreCase(overrideCoreProps) || 
"false".equalsIgnoreCase(overrideCoreProps)) {
+            overrideCoreProperties = Boolean.parseBoolean(overrideCoreProps);
+        } else {
+            throw new IllegalArgumentException(
+                "Property, " + overrideCorePropertiesKey + ", to specify 
whether to override core properties must either be a value boolean value 
(\"true\" or \"false\")" +
+                    " or left to the default value of \"false\". It is set to 
\"" + overrideCoreProps + "\".");
+        }

Review Comment:
   changed



##########
minifi/minifi-bootstrap/src/main/java/org/apache/nifi/minifi/bootstrap/util/ProcessUtils.java:
##########
@@ -0,0 +1,43 @@
+/*
+ * 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.util;
+
+import java.io.IOException;
+
+public interface ProcessUtils {
+    boolean isProcessRunning(Long pid);
+
+    void gracefulShutDownMiNiFiProcess(Long pid, String s, int 
gracefulShutdownSeconds);

Review Comment:
   done



##########
minifi/minifi-bootstrap/src/main/java/org/apache/nifi/minifi/bootstrap/util/ProcessUtils.java:
##########
@@ -0,0 +1,43 @@
+/*
+ * 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.util;
+
+import java.io.IOException;
+
+public interface ProcessUtils {
+    boolean isProcessRunning(Long pid);
+
+    void gracefulShutDownMiNiFiProcess(Long pid, String s, int 
gracefulShutdownSeconds);
+
+    void killProcessTree(Long pid) throws IOException;
+
+    /**
+     * Checks the status of the given process.
+     *
+     * @param process the process object what we want to check
+     * @return true if the process is Alive
+     */
+     default boolean isAlive(Process process) {
+        try {
+            process.exitValue();
+            return false;
+        } catch (IllegalStateException | IllegalThreadStateException itse) {
+            return true;
+        }

Review Comment:
   removed the method, and using the Process.isAlive directly.



##########
minifi/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/TestLogAppender.java:
##########
@@ -0,0 +1,51 @@
+/*
+ * 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;
+
+import ch.qos.logback.classic.spi.ILoggingEvent;
+import ch.qos.logback.core.AppenderBase;
+import java.util.ArrayList;
+
+/**
+ * Helper class which makes possible to subscribe to logging events, and make 
assertions based on it.
+ * With this approach we can avoid static mocking of loggers, which is 
impossible in some cases.
+ */
+public class TestLogAppender extends AppenderBase<ILoggingEvent> {

Review Comment:
   The result of the Dump, Env and FlowStatus commands are written to the 
console, so this was the only way to test the proper functionality, but I 
dropped it and relying on the status code now.



##########
minifi/minifi-bootstrap/src/test/java/org/apache/nifi/minifi/bootstrap/ShutdownHookTest.java:
##########
@@ -0,0 +1,58 @@
+/*
+ * 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;
+
+import static org.apache.nifi.minifi.bootstrap.BootstrapCommand.STOP;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import org.apache.nifi.minifi.bootstrap.service.MiNiFiStdLogHandler;
+import org.apache.nifi.minifi.bootstrap.service.PeriodicStatusReporterManager;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+@ExtendWith(MockitoExtension.class)
+class ShutdownHookTest {
+
+    @Mock
+    private RunMiNiFi runner;
+    @Mock
+    private MiNiFiStdLogHandler miNiFiStdLogHandler;
+
+    @InjectMocks
+    private ShutdownHook shutdownHook;
+
+    @Test
+    void testRunShouldShutdownSchedulersAndProcesses() {
+        PeriodicStatusReporterManager periodicStatusReporterManager = 
mock(PeriodicStatusReporterManager.class);

Review Comment:
   changed



-- 
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: issues-unsubscr...@nifi.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to