Skip to content

Commit

Permalink
GG-23528 Add command line option to see full stack trace and cause ex…
Browse files Browse the repository at this point in the history
…ception
  • Loading branch information
ktkalenko committed Mar 17, 2020
1 parent df929c0 commit 5b24f9c
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,15 @@
import org.jetbrains.annotations.Nullable;

import static java.lang.System.lineSeparator;
import static java.util.Objects.nonNull;
import static org.apache.ignite.internal.IgniteVersionUtils.ACK_VER_STR;
import static org.apache.ignite.internal.IgniteVersionUtils.COPYRIGHT;
import static org.apache.ignite.internal.commandline.CommandLogger.INDENT;
import static org.apache.ignite.internal.commandline.CommandLogger.errorMessage;
import static org.apache.ignite.internal.commandline.CommandLogger.DOUBLE_INDENT;
import static org.apache.ignite.internal.commandline.CommandLogger.optional;
import static org.apache.ignite.internal.commandline.CommonArgParser.CMD_AUTO_CONFIRMATION;
import static org.apache.ignite.internal.commandline.CommonArgParser.CMD_PRINT_ERR_STACK_TRACE;
import static org.apache.ignite.internal.commandline.CommonArgParser.getCommonOptions;
import static org.apache.ignite.internal.commandline.TaskExecutor.DFLT_HOST;
import static org.apache.ignite.internal.commandline.TaskExecutor.DFLT_PORT;
Expand Down Expand Up @@ -219,13 +222,18 @@ public int execute(List<String> rawArgs) {

String commandName = "";

Throwable err = null;
boolean printErrStackTrace = false;

try {
if (F.isEmpty(rawArgs) || (rawArgs.size() == 1 && CMD_HELP.equalsIgnoreCase(rawArgs.get(0)))) {
printHelp();

return EXIT_CODE_OK;
}

printErrStackTrace = F.exist(rawArgs, CMD_PRINT_ERR_STACK_TRACE::equalsIgnoreCase);

ConnectionAndSslParameters args = new CommonArgParser(logger).parseAndValidate(rawArgs.iterator());

Command command = args.command();
Expand Down Expand Up @@ -292,16 +300,20 @@ public int execute(List<String> rawArgs) {
return EXIT_CODE_OK;
}
catch (IllegalArgumentException e) {
logger.severe("Check arguments. " + CommandLogger.errorMessage(e));
logger.severe("Check arguments. " + errorMessage(e));
logger.info("Command [" + commandName + "] finished with code: " + EXIT_CODE_INVALID_ARGUMENTS);

err = e;

return EXIT_CODE_INVALID_ARGUMENTS;
}
catch (Throwable e) {
if (isAuthError(e)) {
logger.severe("Authentication error. " + CommandLogger.errorMessage(e));
logger.severe("Authentication error. " + errorMessage(e));
logger.info("Command [" + commandName + "] finished with code: " + ERR_AUTHENTICATION_FAILED);

err = e;

return ERR_AUTHENTICATION_FAILED;
}

Expand All @@ -311,22 +323,29 @@ public int execute(List<String> rawArgs) {
if (cause != null && cause.getMessage() != null && cause.getMessage().contains("SSL"))
e = cause;

logger.severe("Connection to cluster failed. " + CommandLogger.errorMessage(e));
logger.severe("Connection to cluster failed. " + errorMessage(e));
logger.info("Command [" + commandName + "] finished with code: " + EXIT_CODE_CONNECTION_FAILED);

err = e;

return EXIT_CODE_CONNECTION_FAILED;
}

logger.severe(CommandLogger.errorMessage(e));
logger.severe(errorMessage(e));
logger.info("Command [" + commandName + "] finished with code: " + EXIT_CODE_UNEXPECTED_ERROR);

err = e;

return EXIT_CODE_UNEXPECTED_ERROR;
}
finally {
LocalDateTime endTime = LocalDateTime.now();

Duration diff = Duration.between(startTime, endTime);

if (printErrStackTrace && nonNull(err))
logger.info("Error stack trace:" + System.lineSeparator() + X.getFullStackTrace(err));

logger.info("Control utility has completed execution at: " + endTime);
logger.info("Execution time: " + diff.toMillis() + " ms");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ public class CommonArgParser {
/** */
static final String CMD_PING_TIMEOUT = "--ping-timeout";

/** Option is used to print error trace stack. */
public static final String CMD_PRINT_ERR_STACK_TRACE = "--print-err-stack-trace";

// SSL configuration section

/** */
Expand Down Expand Up @@ -107,6 +110,7 @@ public class CommonArgParser {
AUX_COMMANDS.add(CMD_USER);

AUX_COMMANDS.add(CMD_AUTO_CONFIRMATION);
AUX_COMMANDS.add(CMD_PRINT_ERR_STACK_TRACE);

AUX_COMMANDS.add(CMD_PING_INTERVAL);
AUX_COMMANDS.add(CMD_PING_TIMEOUT);
Expand Down Expand Up @@ -158,6 +162,7 @@ public static String[] getCommonOptions() {
list.add(optional(CMD_PASSWORD, "PASSWORD"));
list.add(optional(CMD_PING_INTERVAL, "PING_INTERVAL"));
list.add(optional(CMD_PING_TIMEOUT, "PING_TIMEOUT"));
list.add(optional(CMD_PRINT_ERR_STACK_TRACE));
list.add(optional(CMD_SSL_PROTOCOL, "SSL_PROTOCOL[, SSL_PROTOCOL_2, ..., SSL_PROTOCOL_N]"));
list.add(optional(CMD_SSL_CIPHER_SUITES, "SSL_CIPHER_1[, SSL_CIPHER_2, ..., SSL_CIPHER_N]"));
list.add(optional(CMD_SSL_KEY_ALGORITHM, "SSL_KEY_ALGORITHM"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.UUID;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand All @@ -50,6 +51,7 @@
import org.apache.ignite.internal.IgniteInternalFuture;
import org.apache.ignite.internal.commandline.CommandHandler;
import org.apache.ignite.internal.commandline.CommandList;
import org.apache.ignite.internal.commandline.CommonArgParser;
import org.apache.ignite.internal.commandline.argument.CommandArg;
import org.apache.ignite.internal.commandline.cache.CacheSubcommands;
import org.apache.ignite.internal.processors.cache.CacheGroupContext;
Expand Down Expand Up @@ -77,9 +79,12 @@
import static org.apache.ignite.TestStorageUtils.corruptDataEntry;
import static org.apache.ignite.cache.CacheAtomicityMode.TRANSACTIONAL;
import static org.apache.ignite.cache.CacheWriteSynchronizationMode.FULL_SYNC;
import static org.apache.ignite.internal.commandline.CommandHandler.EXIT_CODE_INVALID_ARGUMENTS;
import static org.apache.ignite.internal.commandline.CommandHandler.EXIT_CODE_OK;
import static org.apache.ignite.internal.commandline.CommandHandler.UTILITY_NAME;
import static org.apache.ignite.internal.commandline.CommandList.BASELINE;
import static org.apache.ignite.internal.commandline.CommandList.WAL;
import static org.apache.ignite.internal.commandline.CommonArgParser.CMD_PRINT_ERR_STACK_TRACE;
import static org.apache.ignite.internal.commandline.OutputFormat.MULTI_LINE;
import static org.apache.ignite.internal.commandline.OutputFormat.SINGLE_LINE;
import static org.apache.ignite.internal.commandline.cache.CacheSubcommands.HELP;
Expand Down Expand Up @@ -1457,6 +1462,31 @@ public void testContainsWarnInsteadExecExperimentalCmdWhenEnableExperimentalFals
}));
}

/**
* Test print stack trace if an error occurs when option
* {@link CommonArgParser#CMD_PRINT_ERR_STACK_TRACE} is enabled.
*/
public void testPrintErrStackTraceWhenError() {
injectTestSystemOut();

execCmdWithError(CMD_PRINT_ERR_STACK_TRACE);

assertContains(log, testOut.toString(), "Error stack trace:");
}

/**
* The test checks that stack trace will not be displayed in case of an
* error without option {@link CommonArgParser#CMD_PRINT_ERR_STACK_TRACE}
* enabled.
*/
public void testNotPrintErrStackTraceWhenError() {
injectTestSystemOut();

execCmdWithError();

assertNotContains(log, testOut.toString(), "Error stack trace:");
}

/**
* Checking for contains or not of experimental commands in output of the
* --help control.sh command.
Expand Down Expand Up @@ -1501,4 +1531,19 @@ private void execHelpCmd(Consumer<String> consumer) {

consumer.accept(testOut.toString());
}

/**
* Executing a command with an error.
*
* @param addArgs Additional arguments for executing the command.
*/
private void execCmdWithError(String... addArgs) {
List<String> args = new ArrayList<>();

args.add(BASELINE.text());
args.add(UUID.randomUUID().toString());
args.addAll(asList(addArgs));

assertEquals(EXIT_CODE_INVALID_ARGUMENTS, execute(args.toArray(new String[0])));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Arguments: --cache help --yes
--------------------------------------------------------------------------------
The '--cache subcommand' is used to get information about and perform actions with caches. The command has the following syntax:

control.(sh|bat) [--host HOST_OR_IP] [--port PORT] [--user USER] [--password PASSWORD] [--ping-interval PING_INTERVAL] [--ping-timeout PING_TIMEOUT] [--ssl-protocol SSL_PROTOCOL[, SSL_PROTOCOL_2, ..., SSL_PROTOCOL_N]] [--ssl-cipher-suites SSL_CIPHER_1[, SSL_CIPHER_2, ..., SSL_CIPHER_N]] [--ssl-key-algorithm SSL_KEY_ALGORITHM] [--keystore-type KEYSTORE_TYPE] [--keystore KEYSTORE_PATH] [--keystore-password KEYSTORE_PASSWORD] [--truststore-type TRUSTSTORE_TYPE] [--truststore TRUSTSTORE_PATH] [--truststore-password TRUSTSTORE_PASSWORD] --cache [subcommand] <subcommand_parameters>
control.(sh|bat) [--host HOST_OR_IP] [--port PORT] [--user USER] [--password PASSWORD] [--ping-interval PING_INTERVAL] [--ping-timeout PING_TIMEOUT] [--print-err-stack-trace] [--ssl-protocol SSL_PROTOCOL[, SSL_PROTOCOL_2, ..., SSL_PROTOCOL_N]] [--ssl-cipher-suites SSL_CIPHER_1[, SSL_CIPHER_2, ..., SSL_CIPHER_N]] [--ssl-key-algorithm SSL_KEY_ALGORITHM] [--keystore-type KEYSTORE_TYPE] [--keystore KEYSTORE_PATH] [--keystore-password KEYSTORE_PASSWORD] [--truststore-type TRUSTSTORE_TYPE] [--truststore TRUSTSTORE_PATH] [--truststore-password TRUSTSTORE_PASSWORD] --cache [subcommand] <subcommand_parameters>

The subcommands that take [nodeId] as an argument ('list', 'find_garbage', 'contention' and 'validate_indexes') will be executed on the given node or on all server nodes if the option is not specified. Other commands will run on a random server node.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ User: <!any!>
Time: <!any!>
Control utility script is used to execute admin commands on cluster or get common cluster info. The command has the following syntax:

control.(sh|bat) [--host HOST_OR_IP] [--port PORT] [--user USER] [--password PASSWORD] [--ping-interval PING_INTERVAL] [--ping-timeout PING_TIMEOUT] [--ssl-protocol SSL_PROTOCOL[, SSL_PROTOCOL_2, ..., SSL_PROTOCOL_N]] [--ssl-cipher-suites SSL_CIPHER_1[, SSL_CIPHER_2, ..., SSL_CIPHER_N]] [--ssl-key-algorithm SSL_KEY_ALGORITHM] [--keystore-type KEYSTORE_TYPE] [--keystore KEYSTORE_PATH] [--keystore-password KEYSTORE_PASSWORD] [--truststore-type TRUSTSTORE_TYPE] [--truststore TRUSTSTORE_PATH] [--truststore-password TRUSTSTORE_PASSWORD] [command] <command_parameters>
control.(sh|bat) [--host HOST_OR_IP] [--port PORT] [--user USER] [--password PASSWORD] [--ping-interval PING_INTERVAL] [--ping-timeout PING_TIMEOUT] [--print-err-stack-trace] [--ssl-protocol SSL_PROTOCOL[, SSL_PROTOCOL_2, ..., SSL_PROTOCOL_N]] [--ssl-cipher-suites SSL_CIPHER_1[, SSL_CIPHER_2, ..., SSL_CIPHER_N]] [--ssl-key-algorithm SSL_KEY_ALGORITHM] [--keystore-type KEYSTORE_TYPE] [--keystore KEYSTORE_PATH] [--keystore-password KEYSTORE_PASSWORD] [--truststore-type TRUSTSTORE_TYPE] [--truststore TRUSTSTORE_PATH] [--truststore-password TRUSTSTORE_PASSWORD] [command] <command_parameters>


This utility can do the following commands:
Expand Down

0 comments on commit 5b24f9c

Please sign in to comment.