Przeglądaj źródła

HADOOP-15596. Stack trace should not be printed out when running hadoop key commands. Contributed by Kitti Nanasi.

Xiao Chen 6 lat temu
rodzic
commit
993ec026d1

+ 21 - 11
hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/crypto/key/KeyShell.java

@@ -265,8 +265,7 @@ public class KeyShell extends CommandShell {
           }
         }
       } 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 class KeyShell extends CommandShell {
           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 class KeyShell extends CommandShell {
           }
           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 class KeyShell extends CommandShell {
           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 class KeyShell extends CommandShell {
             "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 class KeyShell extends CommandShell {
         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 class KeyShell extends CommandShell {
     }
   }
 
+  @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

+ 1 - 1
hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/crypto/key/kms/LoadBalancingKMSClientProvider.java

@@ -145,7 +145,7 @@ public class LoadBalancingKMSClientProvider extends KeyProvider implements
         // 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,

+ 5 - 1
hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/tools/CommandShell.java

@@ -76,7 +76,7 @@ public abstract class CommandShell extends Configured implements Tool {
       }
     } catch (Exception e) {
       printShellUsage();
-      e.printStackTrace(err);
+      printException(e);
       return 1;
     }
     return exitCode;
@@ -98,6 +98,10 @@ public abstract class CommandShell extends Configured implements Tool {
     out.flush();
   }
 
+  protected void printException(Exception ex){
+    ex.printStackTrace(err);
+  }
+
   /**
    * Base class for any subcommands of this shell command.
    */