This repository has been archived by the owner on Aug 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle the elasticsearch exceptions in JDBC formatted outputs (#362)
* Caught ES exception * Added details in errMsgs to enrich the behavior; added IT * Handled cases where ES exceptions are wrapped up; added default fetching details method * Added factory method to construct ErrorMessage; extended exception type for ErrorMessage * Added UT for ErrorMessageFactory * addressed comments
- Loading branch information
Showing
14 changed files
with
285 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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
67 changes: 67 additions & 0 deletions
67
.../com/amazon/opendistroforelasticsearch/sql/executor/format/ElasticsearchErrorMessage.java
This file contains 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,67 @@ | ||
/* | ||
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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 com.amazon.opendistroforelasticsearch.sql.executor.format; | ||
|
||
import com.amazon.opendistroforelasticsearch.sql.utils.StringUtils; | ||
import org.elasticsearch.ElasticsearchException; | ||
import org.elasticsearch.action.search.SearchPhaseExecutionException; | ||
import org.elasticsearch.action.search.ShardSearchFailure; | ||
|
||
public class ElasticsearchErrorMessage extends ErrorMessage<ElasticsearchException> { | ||
|
||
ElasticsearchErrorMessage(ElasticsearchException exception, int status) { | ||
super(exception, status); | ||
} | ||
|
||
@Override | ||
protected String fetchReason() { | ||
return "Error occurred in Elasticsearch engine: " + exception.getMessage(); | ||
} | ||
|
||
/** Currently Sql-Jdbc plugin only supports string type as reason and details in the error messages */ | ||
@Override | ||
protected String fetchDetails() { | ||
StringBuilder details = new StringBuilder(); | ||
if (exception instanceof SearchPhaseExecutionException) { | ||
details.append(fetchSearchPhaseExecutionExceptionDetails((SearchPhaseExecutionException) exception)); | ||
} else { | ||
details.append(defaultDetails(exception)); | ||
} | ||
details.append("\nFor more details, please send request for Json format to see the raw response from " | ||
+ "elasticsearch engine."); | ||
return details.toString(); | ||
} | ||
|
||
private String defaultDetails(ElasticsearchException exception) { | ||
return exception.getDetailedMessage(); | ||
} | ||
|
||
/** | ||
* Could not deliver the exactly same error messages due to the limit of JDBC types. | ||
* Currently our cases occurred only SearchPhaseExecutionException instances among all types of ES exceptions | ||
* according to the survey, see all types: ElasticsearchException.ElasticsearchExceptionHandle. | ||
* Either add methods of fetching details for different types, or re-make a consistent message by not giving | ||
* detailed messages/root causes but only a suggestion message. | ||
*/ | ||
private String fetchSearchPhaseExecutionExceptionDetails(SearchPhaseExecutionException exception) { | ||
StringBuilder details = new StringBuilder(); | ||
ShardSearchFailure[] shardFailures = exception.shardFailures(); | ||
for (ShardSearchFailure failure : shardFailures) { | ||
details.append(StringUtils.format("Shard[%d]: %s\n", failure.shardId(), failure.getCause().toString())); | ||
} | ||
return details.toString(); | ||
} | ||
} |
This file contains 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
53 changes: 53 additions & 0 deletions
53
...n/java/com/amazon/opendistroforelasticsearch/sql/executor/format/ErrorMessageFactory.java
This file contains 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,53 @@ | ||
/* | ||
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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 com.amazon.opendistroforelasticsearch.sql.executor.format; | ||
|
||
import org.elasticsearch.ElasticsearchException; | ||
|
||
public class ErrorMessageFactory { | ||
/** | ||
* Create error message based on the exception type | ||
* Exceptions of ES exception type and exceptions with wrapped ES exception causes | ||
* should create {@link ElasticsearchErrorMessage} | ||
* | ||
* @param e exception to create error message | ||
* @param status exception status code | ||
* @return error message | ||
*/ | ||
|
||
public static ErrorMessage createErrorMessage(Exception e, int status) { | ||
if (e instanceof ElasticsearchException) { | ||
return new ElasticsearchErrorMessage((ElasticsearchException) e, | ||
((ElasticsearchException) e).status().getStatus()); | ||
} else if (unwrapCause(e) instanceof ElasticsearchException) { | ||
ElasticsearchException exception = (ElasticsearchException) unwrapCause(e); | ||
return new ElasticsearchErrorMessage(exception, exception.status().getStatus()); | ||
} | ||
return new ErrorMessage(e, status); | ||
} | ||
|
||
public static Throwable unwrapCause(Throwable t) { | ||
Throwable result = t; | ||
if (result instanceof ElasticsearchException) { | ||
return result; | ||
} | ||
if (result.getCause() == null) { | ||
return result; | ||
} | ||
result = unwrapCause(result.getCause()); | ||
return result; | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.