-
Notifications
You must be signed in to change notification settings - Fork 2.8k
[ZEPPELIN-3014] NPE bug fix and Error message enhancement with Kylin Interpreter #2645
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
kylin/src/main/java/org/apache/zeppelin/kylin/KylinErrorResponse.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| /* | ||
| * 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.zeppelin.kylin; | ||
|
|
||
| import com.google.gson.Gson; | ||
| import com.google.gson.JsonSyntaxException; | ||
| import org.apache.zeppelin.common.JsonSerializable; | ||
|
|
||
| /** | ||
| * class for Kylin Error Response. | ||
| */ | ||
| class KylinErrorResponse implements JsonSerializable { | ||
| private static final Gson gson = new Gson(); | ||
|
|
||
| private String stacktrace; | ||
| private String exception; | ||
| private String url; | ||
| private String code; | ||
| private Object data; | ||
| private String msg; | ||
|
|
||
| public KylinErrorResponse(String stacktrace, String exception, String url, | ||
| String code, Object data, String msg) { | ||
| this.stacktrace = stacktrace; | ||
| this.exception = exception; | ||
| this.url = url; | ||
| this.code = code; | ||
| this.data = data; | ||
| this.msg = msg; | ||
| } | ||
|
|
||
| public String getException() { | ||
| return exception; | ||
| } | ||
|
|
||
| public String toJson() { | ||
| return gson.toJson(this); | ||
| } | ||
|
|
||
| public static KylinErrorResponse fromJson(String json) { | ||
| try { | ||
| return gson.fromJson(json, KylinErrorResponse.class); | ||
| } catch (JsonSyntaxException ex) { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ | |
| package org.apache.zeppelin.kylin; | ||
|
|
||
| import org.apache.commons.codec.binary.Base64; | ||
| import org.apache.commons.io.IOUtils; | ||
| import org.apache.http.HttpResponse; | ||
| import org.apache.http.client.HttpClient; | ||
| import org.apache.http.client.methods.HttpPost; | ||
|
|
@@ -30,9 +31,7 @@ | |
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import java.io.BufferedReader; | ||
| import java.io.IOException; | ||
| import java.io.InputStreamReader; | ||
| import java.util.List; | ||
| import java.util.Properties; | ||
| import java.util.regex.Matcher; | ||
|
|
@@ -166,28 +165,42 @@ public String getSQL(String cmd) { | |
| } | ||
|
|
||
| private InterpreterResult executeQuery(String sql) throws IOException { | ||
|
|
||
| HttpResponse response = prepareRequest(sql); | ||
| String result; | ||
|
|
||
| if (response.getStatusLine().getStatusCode() != 200) { | ||
| logger.error("failed to execute query: " + response.getEntity().getContent().toString()); | ||
| return new InterpreterResult(InterpreterResult.Code.ERROR, | ||
| "Failed : HTTP error code " + response.getStatusLine().getStatusCode()); | ||
| } | ||
|
|
||
| BufferedReader br = new BufferedReader( | ||
| new InputStreamReader((response.getEntity().getContent()))); | ||
| StringBuilder sb = new StringBuilder(); | ||
| try { | ||
| int code = response.getStatusLine().getStatusCode(); | ||
| result = IOUtils.toString(response.getEntity().getContent(), "UTF-8"); | ||
|
|
||
| if (code != 200) { | ||
| StringBuilder errorMessage = new StringBuilder("Failed : HTTP error code " + code + " ."); | ||
| logger.error("Failed to execute query: " + result); | ||
|
|
||
| KylinErrorResponse kylinErrorResponse = KylinErrorResponse.fromJson(result); | ||
| if (kylinErrorResponse == null) { | ||
| logger.error("Cannot get json from string: " + result); | ||
| // when code is 401, the response is html, not json | ||
| if (code == 401) { | ||
| errorMessage.append(" Error message: Unauthorized. This request requires " | ||
| + "HTTP authentication. Please make sure your have set your credentials " | ||
| + "correctly."); | ||
| } else { | ||
| errorMessage.append(" Error message: " + result + " ."); | ||
| } | ||
| } else { | ||
| String exception = kylinErrorResponse.getException(); | ||
| logger.error("The exception is " + exception); | ||
| errorMessage.append(" Error message: " + exception + " ."); | ||
| } | ||
|
|
||
| String output; | ||
| logger.info("Output from Server .... \n"); | ||
| while ((output = br.readLine()) != null) { | ||
| logger.info(output); | ||
| sb.append(output).append('\n'); | ||
| return new InterpreterResult(InterpreterResult.Code.ERROR, errorMessage.toString()); | ||
| } | ||
| } catch (NullPointerException | IOException e) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NullPointerException shouldn't happen then, if kylinErrorResponse == null is checked?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @felixcheung yes, NPE shouldn't happen now. |
||
| throw new IOException(e); | ||
| } | ||
| InterpreterResult rett = new InterpreterResult(InterpreterResult.Code.SUCCESS, | ||
| formatResult(sb.toString())); | ||
| return rett; | ||
|
|
||
| return new InterpreterResult(InterpreterResult.Code.SUCCESS, | ||
| formatResult(result)); | ||
| } | ||
|
|
||
| String formatResult(String msg) { | ||
|
|
@@ -205,16 +218,18 @@ String formatResult(String msg) { | |
| table = mr.group(1); | ||
| } | ||
|
|
||
| String[] row = table.split("],\\["); | ||
| for (int i = 0; i < row.length; i++) { | ||
| String[] col = row[i].split(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)", -1); | ||
| for (int j = 0; j < col.length; j++) { | ||
| if (col[j] != null) { | ||
| col[j] = col[j].replaceAll("^\"|\"$", ""); | ||
| if (table != null && !table.isEmpty()) { | ||
| String[] row = table.split("],\\["); | ||
| for (int i = 0; i < row.length; i++) { | ||
| String[] col = row[i].split(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)", -1); | ||
| for (int j = 0; j < col.length; j++) { | ||
| if (col[j] != null) { | ||
| col[j] = col[j].replaceAll("^\"|\"$", ""); | ||
| } | ||
| res.append(col[j] + " \t"); | ||
| } | ||
| res.append(col[j] + " \t"); | ||
| res.append(" \n"); | ||
| } | ||
| res.append(" \n"); | ||
| } | ||
| return res.toString(); | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this acceptable for kylin to ignore the exception and return null ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, it is acceptable. It is known that when 401 error, the response is in xml format and not json, in this case, just return it as null json, and handle it in KylinInterpreter::executeQuery.