hive-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From na...@apache.org
Subject svn commit: r1607220 - in /hive/trunk: jdbc/src/java/org/apache/hive/jdbc/ ql/src/java/org/apache/hadoop/hive/ql/ ql/src/java/org/apache/hadoop/hive/ql/processors/ service/src/java/org/apache/hive/service/cli/ service/src/java/org/apache/hive/service/c...
Date Wed, 02 Jul 2014 01:11:46 GMT
Author: navis
Date: Wed Jul  2 01:11:45 2014
New Revision: 1607220

URL: http://svn.apache.org/r1607220
Log:
HIVE-7127 : Handover more details on exception in hiveserver2 (Navis reviewed by Szehon Ho)

Added:
    hive/trunk/service/src/test/org/apache/hive/service/cli/TestHiveSQLException.java
Modified:
    hive/trunk/jdbc/src/java/org/apache/hive/jdbc/Utils.java
    hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/Driver.java
    hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/processors/CommandProcessorResponse.java
    hive/trunk/service/src/java/org/apache/hive/service/cli/HiveSQLException.java
    hive/trunk/service/src/java/org/apache/hive/service/cli/operation/HiveCommandOperation.java
    hive/trunk/service/src/java/org/apache/hive/service/cli/operation/Operation.java
    hive/trunk/service/src/java/org/apache/hive/service/cli/operation/SQLOperation.java

Modified: hive/trunk/jdbc/src/java/org/apache/hive/jdbc/Utils.java
URL: http://svn.apache.org/viewvc/hive/trunk/jdbc/src/java/org/apache/hive/jdbc/Utils.java?rev=1607220&r1=1607219&r2=1607220&view=diff
==============================================================================
--- hive/trunk/jdbc/src/java/org/apache/hive/jdbc/Utils.java (original)
+++ hive/trunk/jdbc/src/java/org/apache/hive/jdbc/Utils.java Wed Jul  2 01:11:45 2014
@@ -25,6 +25,7 @@ import java.util.Map;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
+import org.apache.hive.service.cli.HiveSQLException;
 import org.apache.hive.service.cli.thrift.TStatus;
 import org.apache.hive.service.cli.thrift.TStatusCode;
 
@@ -117,9 +118,8 @@ public class Utils {
   public static void verifySuccess(TStatus status, boolean withInfo) throws SQLException
{
     if ((status.getStatusCode() != TStatusCode.SUCCESS_STATUS) &&
         (withInfo && (status.getStatusCode() != TStatusCode.SUCCESS_WITH_INFO_STATUS)))
{
-      throw new SQLException(status.getErrorMessage(),
-           status.getSqlState(), status.getErrorCode());
-      }
+        throw new HiveSQLException(status);
+    }
   }
 
   /**
@@ -229,12 +229,12 @@ public class Utils {
   }
 
   /**
-   * Takes a version string delmited by '.' and '-' characters
+   * Takes a version string delimited by '.' and '-' characters
    * and returns a partial version.
    *
    * @param fullVersion
    *          version string.
-   * @param tokenPosition
+   * @param position
    *          position of version string to get starting at 0. eg, for a X.x.xxx
    *          string, 0 will return the major version, 1 will return minor
    *          version.

Modified: hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/Driver.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/Driver.java?rev=1607220&r1=1607219&r2=1607220&view=diff
==============================================================================
--- hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/Driver.java (original)
+++ hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/Driver.java Wed Jul  2 01:11:45 2014
@@ -1,4 +1,3 @@
-
 /**
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
@@ -952,8 +951,7 @@ public class Driver implements CommandPr
   }
 
   public CommandProcessorResponse compileAndRespond(String command) {
-    return new CommandProcessorResponse(compileInternal(command),
-        errorMessage, SQLState);
+    return createProcessorResponse(compileInternal(command));
   }
 
   private int compileInternal(String command) {
@@ -979,7 +977,7 @@ public class Driver implements CommandPr
     downstreamError = null;
 
     if (!validateConfVariables()) {
-      return new CommandProcessorResponse(12, errorMessage, SQLState);
+      return createProcessorResponse(12);
     }
 
     HiveDriverRunHookContext hookContext = new HiveDriverRunHookContextImpl(conf, command);
@@ -997,7 +995,7 @@ public class Driver implements CommandPr
       downstreamError = e;
       console.printError(errorMessage + "\n"
           + org.apache.hadoop.util.StringUtils.stringifyException(e));
-      return new CommandProcessorResponse(12, errorMessage, SQLState);
+      return createProcessorResponse(12);
     }
 
     // Reset the perf logger
@@ -1005,7 +1003,6 @@ public class Driver implements CommandPr
     perfLogger.PerfLogBegin(CLASS_NAME, PerfLogger.DRIVER_RUN);
     perfLogger.PerfLogBegin(CLASS_NAME, PerfLogger.TIME_TO_SUBMIT);
 
-    int ret;
     boolean requireLock = false;
     boolean ckLock = false;
     try {
@@ -1017,16 +1014,17 @@ public class Driver implements CommandPr
       downstreamError = e;
       console.printError(errorMessage, "\n"
           + org.apache.hadoop.util.StringUtils.stringifyException(e));
-      ret = 10;
-      return new CommandProcessorResponse(ret, errorMessage, SQLState);
+      return createProcessorResponse(10);
+    }
+    int ret = recordValidTxns();
+    if (ret != 0) {
+      return createProcessorResponse(ret);
     }
-    ret = recordValidTxns();
-    if (ret != 0) return new CommandProcessorResponse(ret, errorMessage, SQLState);
 
     if (!alreadyCompiled) {
       ret = compileInternal(command);
       if (ret != 0) {
-        return new CommandProcessorResponse(ret, errorMessage, SQLState);
+        return createProcessorResponse(ret);
       }
     }
 
@@ -1068,7 +1066,7 @@ public class Driver implements CommandPr
         } catch (LockException e) {
           // Not much to do here
         }
-        return new CommandProcessorResponse(ret, errorMessage, SQLState);
+        return createProcessorResponse(ret);
       }
     }
 
@@ -1080,7 +1078,7 @@ public class Driver implements CommandPr
       } catch (LockException e) {
         // Nothing to do here
       }
-      return new CommandProcessorResponse(ret, errorMessage, SQLState);
+      return createProcessorResponse(ret);
     }
 
     //if needRequireLock is false, the release here will do nothing because there is no lock
@@ -1092,7 +1090,7 @@ public class Driver implements CommandPr
       downstreamError = e;
       console.printError(errorMessage + "\n"
           + org.apache.hadoop.util.StringUtils.stringifyException(e));
-      return new CommandProcessorResponse(12, errorMessage, SQLState);
+      return createProcessorResponse(12);
     }
 
     perfLogger.PerfLogEnd(CLASS_NAME, PerfLogger.DRIVER_RUN);
@@ -1109,10 +1107,14 @@ public class Driver implements CommandPr
       downstreamError = e;
       console.printError(errorMessage + "\n"
           + org.apache.hadoop.util.StringUtils.stringifyException(e));
-      return new CommandProcessorResponse(12, errorMessage, SQLState);
+      return createProcessorResponse(12);
     }
 
-    return new CommandProcessorResponse(ret);
+    return createProcessorResponse(ret);
+  }
+
+  private CommandProcessorResponse createProcessorResponse(int ret) {
+    return new CommandProcessorResponse(ret, errorMessage, SQLState, downstreamError);
   }
 
   /**
@@ -1431,6 +1433,7 @@ public class Driver implements CommandPr
 
     return (0);
   }
+
   private void setErrorMsgAndDetail(int exitVal, Throwable downstreamError, Task tsk) {
     this.downstreamError = downstreamError;
     errorMessage = "FAILED: Execution Error, return code " + exitVal + " from " + tsk.getClass().getName();
@@ -1495,6 +1498,7 @@ public class Driver implements CommandPr
     return plan != null && plan.getFetchTask() != null;
   }
 
+  @SuppressWarnings("unchecked")
   public boolean getResults(List res) throws IOException, CommandNeedRetryException {
     if (destroyed) {
       throw new IOException("FAILED: Operation cancelled");

Modified: hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/processors/CommandProcessorResponse.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/processors/CommandProcessorResponse.java?rev=1607220&r1=1607219&r2=1607220&view=diff
==============================================================================
--- hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/processors/CommandProcessorResponse.java
(original)
+++ hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/processors/CommandProcessorResponse.java
Wed Jul  2 01:11:45 2014
@@ -22,7 +22,7 @@ import org.apache.hadoop.hive.metastore.
 
 /**
  * Encapsulates the basic response info returned by classes the implement the
- * <code>CommandProcessor</code> interfaace. Typically <code>errorMessage</code>
+ * <code>CommandProcessor</code> interface. Typically <code>errorMessage</code>
  * and <code>SQLState</code> will only be set if the <code>responseCode</code>
  * is not 0.
  */
@@ -32,23 +32,36 @@ public class CommandProcessorResponse {
   private String SQLState;
   private Schema resSchema;
 
+  private Throwable exception;
+
   public CommandProcessorResponse(int responseCode) {
-    this(responseCode, null, null);
+    this(responseCode, null, null, null, null);
   }
 
   public CommandProcessorResponse(int responseCode, String errorMessage, String SQLState)
{
-    this(responseCode, errorMessage, SQLState, null);
+    this(responseCode, errorMessage, SQLState, null, null);
+  }
+
+  public CommandProcessorResponse(int responseCode, String errorMessage, String SQLState,
Throwable exception) {
+    this(responseCode, errorMessage, SQLState, null, exception);
   }
 
   public CommandProcessorResponse(int responseCode, String errorMessage, String SQLState,
Schema schema) {
+    this(responseCode, errorMessage, SQLState, schema, null);
+  }
+
+  public CommandProcessorResponse(int responseCode, String errorMessage, String SQLState,
+      Schema schema, Throwable exception) {
     this.responseCode = responseCode;
     this.errorMessage = errorMessage;
     this.SQLState = SQLState;
     this.resSchema = schema;
+    this.exception = exception;
   }
 
   public int getResponseCode() { return responseCode; }
   public String getErrorMessage() { return errorMessage; }
   public String getSQLState() { return SQLState; }
   public Schema getSchema() { return resSchema; }
+  public Throwable getException() { return exception; }
 }

Modified: hive/trunk/service/src/java/org/apache/hive/service/cli/HiveSQLException.java
URL: http://svn.apache.org/viewvc/hive/trunk/service/src/java/org/apache/hive/service/cli/HiveSQLException.java?rev=1607220&r1=1607219&r2=1607220&view=diff
==============================================================================
--- hive/trunk/service/src/java/org/apache/hive/service/cli/HiveSQLException.java (original)
+++ hive/trunk/service/src/java/org/apache/hive/service/cli/HiveSQLException.java Wed Jul
 2 01:11:45 2014
@@ -19,6 +19,8 @@
 package org.apache.hive.service.cli;
 
 import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.List;
 
 import org.apache.hive.service.cli.thrift.TStatus;
 import org.apache.hive.service.cli.thrift.TStatusCode;
@@ -101,7 +103,10 @@ public class HiveSQLException extends SQ
 
   public HiveSQLException(TStatus status) {
     // TODO: set correct vendorCode field
-    super(status.getErrorMessage(), status.getSqlState(), 1);
+    super(status.getErrorMessage(), status.getSqlState(), status.getErrorCode());
+    if (status.getInfoMessages() != null) {
+      initCause(toCause(status.getInfoMessages()));
+    }
   }
 
   public TStatus toTStatus() {
@@ -110,6 +115,7 @@ public class HiveSQLException extends SQ
     tStatus.setSqlState(getSQLState());
     tStatus.setErrorCode(getErrorCode());
     tStatus.setErrorMessage(getMessage());
+    tStatus.setInfoMessages(toString(this));
     return tStatus;
   }
 
@@ -119,7 +125,101 @@ public class HiveSQLException extends SQ
     }
     TStatus tStatus = new TStatus(TStatusCode.ERROR_STATUS);
     tStatus.setErrorMessage(e.getMessage());
+    tStatus.setInfoMessages(toString(e));
     return tStatus;
   }
 
+  public static List<String> toString(Throwable ex) {
+    return toString(ex, null);
+  }
+
+  static List<String> toString(Throwable cause, StackTraceElement[] parent) {
+    StackTraceElement[] trace = cause.getStackTrace();
+    int m = trace.length - 1;
+    if (parent != null) {
+      int n = parent.length - 1;
+      while (m >= 0 && n >= 0 && trace[m].equals(parent[n])) {
+        m--; n--;
+      }
+    }
+    List<String> detail = enroll(cause, trace, m);
+    cause = cause.getCause();
+    if (cause != null) {
+      detail.addAll(toString(cause, trace));
+    }
+    return detail;
+  }
+
+  static List<String> enroll(Throwable ex, StackTraceElement[] trace, int max) {
+    List<String> details = new ArrayList<String>();
+    StringBuilder builder = new StringBuilder();
+    builder.append('*').append(ex.getClass().getName()).append(':');
+    builder.append(ex.getMessage()).append(':');
+    builder.append(trace.length).append(':').append(max);
+    details.add(builder.toString());
+    for (int i = 0; i <= max; i++) {
+      builder.setLength(0);
+      builder.append(trace[i].getClassName()).append(':');
+      builder.append(trace[i].getMethodName()).append(':');
+      String fileName = trace[i].getFileName();
+      builder.append(fileName == null ? "" : fileName).append(':');
+      builder.append(trace[i].getLineNumber());
+      details.add(builder.toString());
+    }
+    return details;
+  }
+
+  public static Throwable toCause(List<String> details) {
+    return toStackTrace(details, null, 0);
+  }
+
+  static Throwable toStackTrace(List<String> details, StackTraceElement[] parent, int
index) {
+    String detail = details.get(index++);
+    if (!detail.startsWith("*")) {
+      return null;  // should not be happened. ignore remaining
+    }
+    int i1 = detail.indexOf(':');
+    int i3 = detail.lastIndexOf(':');
+    int i2 = detail.substring(0, i3).lastIndexOf(':');
+    String exceptionClass = detail.substring(1, i1);
+    String exceptionMessage = detail.substring(i1 + 1, i2);
+    Throwable ex = newInstance(exceptionClass, exceptionMessage);
+
+    Integer length = Integer.valueOf(detail.substring(i2 + 1, i3));
+    Integer unique = Integer.valueOf(detail.substring(i3 + 1));
+
+    int i = 0;
+    StackTraceElement[] trace = new StackTraceElement[length];
+    for (; i <= unique; i++) {
+      detail = details.get(index++);
+      int j1 = detail.indexOf(':');
+      int j3 = detail.lastIndexOf(':');
+      int j2 = detail.substring(0, j3).lastIndexOf(':');
+      String className = detail.substring(0, j1);
+      String methodName = detail.substring(j1 + 1, j2);
+      String fileName = detail.substring(j2 + 1, j3);
+      if (fileName.isEmpty()) {
+        fileName = null;
+      }
+      int lineNumber = Integer.valueOf(detail.substring(j3 + 1));
+      trace[i] = new StackTraceElement(className, methodName, fileName, lineNumber);
+    }
+    int common = trace.length - i;
+    if (common > 0) {
+      System.arraycopy(parent, parent.length - common, trace, trace.length - common, common);
+    }
+    if (details.size() > index) {
+      ex.initCause(toStackTrace(details, trace, index));
+    }
+    ex.setStackTrace(trace);
+    return ex;
+  }
+
+  private static Throwable newInstance(String className, String message) {
+    try {
+      return (Throwable)Class.forName(className).getConstructor(String.class).newInstance(message);
+    } catch (Exception e) {
+      return new RuntimeException(className + ":" + message);
+    }
+  }
 }

Modified: hive/trunk/service/src/java/org/apache/hive/service/cli/operation/HiveCommandOperation.java
URL: http://svn.apache.org/viewvc/hive/trunk/service/src/java/org/apache/hive/service/cli/operation/HiveCommandOperation.java?rev=1607220&r1=1607219&r2=1607220&view=diff
==============================================================================
--- hive/trunk/service/src/java/org/apache/hive/service/cli/operation/HiveCommandOperation.java
(original)
+++ hive/trunk/service/src/java/org/apache/hive/service/cli/operation/HiveCommandOperation.java
Wed Jul  2 01:11:45 2014
@@ -47,7 +47,6 @@ import org.apache.hive.service.cli.sessi
  * Executes a HiveCommand
  */
 public class HiveCommandOperation extends ExecuteStatementOperation {
-  private CommandProcessorResponse response;
   private CommandProcessor commandProcessor;
   private TableSchema resultSchema = null;
 
@@ -106,11 +105,10 @@ public class HiveCommandOperation extend
       String[] tokens = statement.split("\\s");
       String commandArgs = command.substring(tokens[0].length()).trim();
 
-      response = commandProcessor.run(commandArgs);
+      CommandProcessorResponse response = commandProcessor.run(commandArgs);
       int returnCode = response.getResponseCode();
       if (returnCode != 0) {
-        throw new HiveSQLException("Error while processing statement: "
-            + response.getErrorMessage(), response.getSQLState(), response.getResponseCode());
+        throw toSQLException("Error while processing statement", response);
       }
       Schema schema = response.getSchema();
       if (schema != null) {
@@ -169,7 +167,6 @@ public class HiveCommandOperation extend
   /**
    * Reads the temporary results for non-Hive (non-Driver) commands to the
    * resulting List of strings.
-   * @param results list of strings containing the results
    * @param nLines number of lines read at once. If it is <= 0, then read all lines.
    */
   private List<String> readResults(int nLines) throws HiveSQLException {

Modified: hive/trunk/service/src/java/org/apache/hive/service/cli/operation/Operation.java
URL: http://svn.apache.org/viewvc/hive/trunk/service/src/java/org/apache/hive/service/cli/operation/Operation.java?rev=1607220&r1=1607219&r2=1607220&view=diff
==============================================================================
--- hive/trunk/service/src/java/org/apache/hive/service/cli/operation/Operation.java (original)
+++ hive/trunk/service/src/java/org/apache/hive/service/cli/operation/Operation.java Wed Jul
 2 01:11:45 2014
@@ -23,6 +23,7 @@ import java.util.concurrent.Future;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.hadoop.hive.conf.HiveConf;
+import org.apache.hadoop.hive.ql.processors.CommandProcessorResponse;
 import org.apache.hive.service.cli.FetchOrientation;
 import org.apache.hive.service.cli.HiveSQLException;
 import org.apache.hive.service.cli.OperationHandle;
@@ -178,4 +179,13 @@ public abstract class Operation {
           " is not supported for this resultset", "HY106");
     }
   }
+
+  protected HiveSQLException toSQLException(String prefix, CommandProcessorResponse response)
{
+    HiveSQLException ex = new HiveSQLException(prefix + ": " + response.getErrorMessage(),
+        response.getSQLState(), response.getResponseCode());
+    if (response.getException() != null) {
+      ex.initCause(response.getException());
+    }
+    return ex;
+  }
 }

Modified: hive/trunk/service/src/java/org/apache/hive/service/cli/operation/SQLOperation.java
URL: http://svn.apache.org/viewvc/hive/trunk/service/src/java/org/apache/hive/service/cli/operation/SQLOperation.java?rev=1607220&r1=1607219&r2=1607220&view=diff
==============================================================================
--- hive/trunk/service/src/java/org/apache/hive/service/cli/operation/SQLOperation.java (original)
+++ hive/trunk/service/src/java/org/apache/hive/service/cli/operation/SQLOperation.java Wed
Jul  2 01:11:45 2014
@@ -98,8 +98,7 @@ public class SQLOperation extends Execut
       String subStatement = new VariableSubstitution().substitute(sqlOperationConf, statement);
       response = driver.compileAndRespond(subStatement);
       if (0 != response.getResponseCode()) {
-        throw new HiveSQLException("Error while compiling statement: "
-            + response.getErrorMessage(), response.getSQLState(), response.getResponseCode());
+        throw toSQLException("Error while compiling statement", response);
       }
 
       mResultSchema = driver.getSchema();
@@ -143,8 +142,7 @@ public class SQLOperation extends Execut
       driver.setTryCount(Integer.MAX_VALUE);
       response = driver.run();
       if (0 != response.getResponseCode()) {
-        throw new HiveSQLException("Error while processing statement: "
-            + response.getErrorMessage(), response.getSQLState(), response.getResponseCode());
+        throw toSQLException("Error while processing statement", response);
       }
     } catch (HiveSQLException e) {
       // If the operation was cancelled by another thread,

Added: hive/trunk/service/src/test/org/apache/hive/service/cli/TestHiveSQLException.java
URL: http://svn.apache.org/viewvc/hive/trunk/service/src/test/org/apache/hive/service/cli/TestHiveSQLException.java?rev=1607220&view=auto
==============================================================================
--- hive/trunk/service/src/test/org/apache/hive/service/cli/TestHiveSQLException.java (added)
+++ hive/trunk/service/src/test/org/apache/hive/service/cli/TestHiveSQLException.java Wed
Jul  2 01:11:45 2014
@@ -0,0 +1,44 @@
+/**
+ * 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.hive.service.cli;
+
+import junit.framework.Assert;
+import org.junit.Test;
+
+public class TestHiveSQLException {
+
+  @Test
+  public void testExceptionMarshalling() throws Exception {
+    Exception ex1 = ex1();
+    ex1.initCause(ex2());
+    Throwable ex = HiveSQLException.toCause(HiveSQLException.toString(ex1));
+    Assert.assertSame(RuntimeException.class, ex.getClass());
+    Assert.assertEquals("exception1", ex.getMessage());
+    Assert.assertSame(UnsupportedOperationException.class, ex.getCause().getClass());
+    Assert.assertEquals("exception2", ex.getCause().getMessage());
+  }
+
+  private static Exception ex1() {
+    return new RuntimeException("exception1");
+  }
+
+  private static Exception ex2() {
+    return new UnsupportedOperationException("exception2");
+  }
+}



Mime
View raw message