exceptionfactory commented on code in PR #7388: URL: https://github.com/apache/nifi/pull/7388#discussion_r1236186070
########## nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/diagnostics/bootstrap/shell/ShellCommandExecutor.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.diagnostics.bootstrap.shell; + +import org.apache.nifi.diagnostics.bootstrap.shell.command.AbstractShellCommand; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.util.Collection; +import java.util.Collections; + +public class ShellCommandExecutor { + private static final Logger logger = LoggerFactory.getLogger(ShellCommandExecutor.class); + + public static Collection<String> execute(final AbstractShellCommand command) { Review Comment: Instead of having this method as a public utility, recommend moving it into `AbstractShellCommand` and defining a `PlatformShellCommand` interface that returns a collection of strings. That hides the invocation details from callers and provides better encapsulation. ########## nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/diagnostics/bootstrap/shell/ShellCommandExecutor.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.diagnostics.bootstrap.shell; + +import org.apache.nifi.diagnostics.bootstrap.shell.command.AbstractShellCommand; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.util.Collection; +import java.util.Collections; + +public class ShellCommandExecutor { + private static final Logger logger = LoggerFactory.getLogger(ShellCommandExecutor.class); + + public static Collection<String> execute(final AbstractShellCommand command) { + final ProcessBuilder processBuilder = new ProcessBuilder(); + processBuilder.command(command.getCommand()); + try { + final Process process = processBuilder.start(); + return command.getParser().createResult(process); + } catch (IndexOutOfBoundsException e) { Review Comment: This does not seem like a good way to catch unsupported operating systems, recommend re-evaluating the approach to avoid attempting to run a command that will fail on an unknown OS. ########## nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/diagnostics/bootstrap/shell/command/AbstractShellCommand.java: ########## @@ -0,0 +1,56 @@ +/* + * 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.diagnostics.bootstrap.shell.command; + +import org.apache.commons.lang3.SystemUtils; +import org.apache.nifi.diagnostics.bootstrap.shell.result.ShellCommandResult; + +public abstract class AbstractShellCommand { Review Comment: As mentioned on ShellCommandExecutor, recommend defining an interface named something like `DiagnosticShellCommand` or `PlatformShellCommand` with a signature of `Collection<String> execute()`. This abstract class can implement that interface and provide the shared Process execution methods, which will encapsulate the Process execution details. ########## nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/diagnostics/bootstrap/shell/result/ShellCommandResult.java: ########## @@ -0,0 +1,23 @@ +/* + * 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.diagnostics.bootstrap.shell.result; + +import java.util.Collection; + +public interface ShellCommandResult { + Collection<String> createResult(final Process process); Review Comment: It seems like a better abstraction would be passing `InputStream` instead of `Process`. That would allow implementations to be unit-tested with examples. ########## nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/diagnostics/bootstrap/shell/result/ShellCommandResult.java: ########## @@ -0,0 +1,23 @@ +/* + * 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.diagnostics.bootstrap.shell.result; + +import java.util.Collection; + +public interface ShellCommandResult { + Collection<String> createResult(final Process process); Review Comment: ```suggestion Collection<String> createResult(final InputStream inputStream); ``` ########## nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/diagnostics/bootstrap/shell/result/LineSplittingResult.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.diagnostics.bootstrap.shell.result; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +public class LineSplittingResult implements ShellCommandResult { + private final String regexStringForSplitting; + private final List<String> labels; + private final String commandName; + + public LineSplittingResult(final String regexStringForSplitting, final List<String> labels, final String commandName) { Review Comment: The `regexStringForSplitting` argument should be changed to use the `Pattern` class, which precompiles the regular expression pattern. ########## nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/diagnostics/bootstrap/shell/ShellCommandExecutor.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.diagnostics.bootstrap.shell; + +import org.apache.nifi.diagnostics.bootstrap.shell.command.AbstractShellCommand; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.util.Collection; +import java.util.Collections; + +public class ShellCommandExecutor { + private static final Logger logger = LoggerFactory.getLogger(ShellCommandExecutor.class); + + public static Collection<String> execute(final AbstractShellCommand command) { + final ProcessBuilder processBuilder = new ProcessBuilder(); + processBuilder.command(command.getCommand()); + try { + final Process process = processBuilder.start(); + return command.getParser().createResult(process); + } catch (IndexOutOfBoundsException e) { + logger.warn(String.format("Operating system is not supported, failed to execute command: %s, ", command.getName())); + return Collections.EMPTY_LIST; + } catch (IOException | NullPointerException e) { Review Comment: It seems like this should be changed to a general `Exception` to cover any other unexpected issues. ########## nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/diagnostics/bootstrap/shell/command/AbstractShellCommand.java: ########## @@ -0,0 +1,56 @@ +/* + * 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.diagnostics.bootstrap.shell.command; + +import org.apache.commons.lang3.SystemUtils; +import org.apache.nifi.diagnostics.bootstrap.shell.result.ShellCommandResult; + +public abstract class AbstractShellCommand { + private final String name; + private final String[] windowsCommand; + private final String[] linuxCommand; + private final String[] macCommand; + private final ShellCommandResult parser; + + public AbstractShellCommand(final String name, final String[] windowsCommand, final String[] linuxCommand, final String[] macCommand, final ShellCommandResult parser) { + this.name = name; + this.windowsCommand = windowsCommand; + this.linuxCommand = linuxCommand; + this.macCommand = macCommand; + this.parser = parser; + } + + public String getName() { + return name; + } + + public String[] getCommand() { + if (SystemUtils.IS_OS_MAC) { + return macCommand; + } else if (SystemUtils.IS_OS_UNIX || SystemUtils.IS_OS_LINUX) { + return linuxCommand; + } else if (SystemUtils.IS_OS_WINDOWS) { + return windowsCommand; + } else { + return new String[] {}; + } + } + + public ShellCommandResult getParser() { Review Comment: This method name does not align with the returned object, is it necessary for the method to be public? ########## nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/diagnostics/bootstrap/tasks/OperatingSystemDiagnosticTask.java: ########## @@ -69,11 +74,26 @@ public DiagnosticsDumpElement captureDump(final boolean verbose) { } attributes.forEach((key, value) -> details.add(key + " : " + value)); + details.addAll(getPhysicalCPUCores()); + details.addAll(getTotalPhysicalRam()); + details.addAll(getDiskLayout()); } catch (final Exception e) { logger.error("Failed to obtain Operating System details", e); return new StandardDiagnosticsDumpElement("Operating System / Hardware", Collections.singletonList("Failed to obtain Operating System details")); } return new StandardDiagnosticsDumpElement("Operating System / Hardware", details); } + + private Collection<String> getPhysicalCPUCores() { + return ShellCommandExecutor.execute(new GetPhysicalCpuCoresCommand()); Review Comment: After refactoring, this should be changed to create the command instance and then call `execute()` instead of calling `ShellCommandExecutor.execute()`. -- 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]
