From 993ec026d10c7566fd358c022c061bca118c92f0 Mon Sep 17 00:00:00 2001 From: Xiao Chen Date: Thu, 19 Jul 2018 14:25:38 -0700 Subject: [PATCH] HADOOP-15596. Stack trace should not be printed out when running hadoop key commands. Contributed by Kitti Nanasi. --- .../apache/hadoop/crypto/key/KeyShell.java | 32 ++++++++++++------- .../kms/LoadBalancingKMSClientProvider.java | 2 +- .../org/apache/hadoop/tools/CommandShell.java | 6 +++- 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/crypto/key/KeyShell.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/crypto/key/KeyShell.java index fa84c47d26c..3f8b337f357 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/crypto/key/KeyShell.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/crypto/key/KeyShell.java @@ -265,8 +265,7 @@ public void execute() throws IOException { } } } catch (IOException e) { - getOut().println("Cannot list keys for KeyProvider: " + provider - + ": " + e.toString()); + getOut().println("Cannot list keys for KeyProvider: " + provider); throw e; } } @@ -318,12 +317,12 @@ public void execute() throws NoSuchAlgorithmException, IOException { printProviderWritten(); } catch (NoSuchAlgorithmException e) { getOut().println("Cannot roll key: " + keyName + - " within KeyProvider: " + provider + ". " + e.toString()); + " within KeyProvider: " + provider + "."); throw e; } } catch (IOException e1) { getOut().println("Cannot roll key: " + keyName + " within KeyProvider: " - + provider + ". " + e1.toString()); + + provider + "."); throw e1; } } @@ -374,8 +373,8 @@ public boolean validate() { } return cont; } catch (IOException e) { - getOut().println(keyName + " will not be deleted."); - e.printStackTrace(getErr()); + getOut().println(keyName + " will not be deleted. " + + prettifyException(e)); } } return true; @@ -392,7 +391,7 @@ public void execute() throws IOException { getOut().println(keyName + " has been successfully deleted."); printProviderWritten(); } catch (IOException e) { - getOut().println(keyName + " has not been deleted. " + e.toString()); + getOut().println(keyName + " has not been deleted."); throw e; } } @@ -463,13 +462,13 @@ public void execute() throws IOException, NoSuchAlgorithmException { "with options " + options.toString() + "."); printProviderWritten(); } catch (InvalidParameterException e) { - getOut().println(keyName + " has not been created. " + e.toString()); + getOut().println(keyName + " has not been created."); throw e; } catch (IOException e) { - getOut().println(keyName + " has not been created. " + e.toString()); + getOut().println(keyName + " has not been created."); throw e; } catch (NoSuchAlgorithmException e) { - getOut().println(keyName + " has not been created. " + e.toString()); + getOut().println(keyName + " has not been created."); throw e; } } @@ -520,7 +519,7 @@ public void execute() throws NoSuchAlgorithmException, IOException { printProviderWritten(); } catch (IOException e) { getOut().println("Cannot invalidate cache for key: " + keyName + - " within KeyProvider: " + provider + ". " + e.toString()); + " within KeyProvider: " + provider + "."); throw e; } } @@ -531,6 +530,17 @@ public String getUsage() { } } + @Override + protected void printException(Exception e){ + getErr().println("Executing command failed with " + + "the following exception: " + prettifyException(e)); + } + + private String prettifyException(Exception e) { + return e.getClass().getSimpleName() + ": " + + e.getLocalizedMessage().split("\n")[0]; + } + /** * main() entry point for the KeyShell. While strictly speaking the * return is void, it will System.exit() with a return code: 0 is for diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/crypto/key/kms/LoadBalancingKMSClientProvider.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/crypto/key/kms/LoadBalancingKMSClientProvider.java index 42cd47dd7a5..1ac3fd3b528 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/crypto/key/kms/LoadBalancingKMSClientProvider.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/crypto/key/kms/LoadBalancingKMSClientProvider.java @@ -145,7 +145,7 @@ private T doOp(ProviderCallable op, int currPos) // compatible with earlier versions of LBKMSCP if (action.action == RetryAction.RetryDecision.FAIL && numFailovers >= providers.length - 1) { - LOG.warn("Aborting since the Request has failed with all KMS" + LOG.error("Aborting since the Request has failed with all KMS" + " providers(depending on {}={} setting and numProviders={})" + " in the group OR the exception is not recoverable", CommonConfigurationKeysPublic.KMS_CLIENT_FAILOVER_MAX_RETRIES_KEY, diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/tools/CommandShell.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/tools/CommandShell.java index 70c8eaf936f..a53e2259e0e 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/tools/CommandShell.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/tools/CommandShell.java @@ -76,7 +76,7 @@ public int run(String[] args) throws Exception { } } catch (Exception e) { printShellUsage(); - e.printStackTrace(err); + printException(e); return 1; } return exitCode; @@ -98,6 +98,10 @@ protected final void printShellUsage() { out.flush(); } + protected void printException(Exception ex){ + ex.printStackTrace(err); + } + /** * Base class for any subcommands of this shell command. */