-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle more AD exceptions thrown over the wire/network (#157)
* Handle more AD exceptions thrown over the wire OpenSearch restricts the kind of exceptions that can be thrown over the wire (Read OpenSearchException.OpenSearchExceptionHandle). Since we cannot add our exception like ResourceNotFoundException without modifying OpenSearch's code, we must unwrap the NotSerializableExceptionWrapper and check its root cause message. This PR adds checks all AD exceptions in such cases. Previously, we only checked a couple of them. Previously, we wrap all exceptions thrown in AD using one of AnomalyDetectionExceptions. The wrap brings a complication to NotSerializableExceptionWrapper thrown over the wire as NotSerializableExceptionWrapper won't keep the original AnomalyDetectionExceptions object and only keeps the cause message. Therefore, we won't be able to restore the original exception encapsulated inside AnomalyDetectionExceptions. This PR stops wrapping the original exception inside AnomalyDetectionExceptions. This PR also adds a null check inside EntityModel in case of a potential null pointer exception. Testing done: 1. Added unit tests to check if exceptions wrapped inside NotSerializableExceptionWrapper can be decoded. 2. Did e2e testing for basic single-stream and HCAD workflow.
- Loading branch information
Showing
13 changed files
with
446 additions
and
64 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
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
102 changes: 102 additions & 0 deletions
102
src/main/java/org/opensearch/ad/common/exception/NotSerializedADExceptionName.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,102 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
package org.opensearch.ad.common.exception; | ||
|
||
import java.util.Optional; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.apache.logging.log4j.message.ParameterizedMessage; | ||
import org.opensearch.OpenSearchException; | ||
import org.opensearch.common.io.stream.NotSerializableExceptionWrapper; | ||
|
||
/** | ||
* OpenSearch restricts the kind of exceptions that can be thrown over the wire | ||
* (Read OpenSearchException.OpenSearchExceptionHandle https://tinyurl.com/wv6c6t7x). | ||
* Since we cannot add our own exception like ResourceNotFoundException without modifying | ||
* OpenSearch's code, we have to unwrap the NotSerializableExceptionWrapper and | ||
* check its root cause message. | ||
* | ||
*/ | ||
public enum NotSerializedADExceptionName { | ||
|
||
RESOURCE_NOT_FOUND_EXCEPTION_NAME_UNDERSCORE(OpenSearchException.getExceptionName(new ResourceNotFoundException("", ""))), | ||
LIMIT_EXCEEDED_EXCEPTION_NAME_UNDERSCORE(OpenSearchException.getExceptionName(new LimitExceededException("", "", false))), | ||
END_RUN_EXCEPTION_NAME_UNDERSCORE(OpenSearchException.getExceptionName(new EndRunException("", "", false))), | ||
ANOMALY_DETECTION_EXCEPTION_NAME_UNDERSCORE(OpenSearchException.getExceptionName(new AnomalyDetectionException("", ""))), | ||
INTERNAL_FAILURE_NAME_UNDERSCORE(OpenSearchException.getExceptionName(new InternalFailure("", ""))), | ||
CLIENT_EXCEPTION_NAME_UNDERSCORE(OpenSearchException.getExceptionName(new ClientException("", ""))), | ||
CANCELLATION_EXCEPTION_NAME_UNDERSCORE(OpenSearchException.getExceptionName(new ADTaskCancelledException("", ""))), | ||
DUPLICATE_TASK_EXCEPTION_NAME_UNDERSCORE(OpenSearchException.getExceptionName(new DuplicateTaskException(""))); | ||
|
||
private static final Logger LOG = LogManager.getLogger(NotSerializedADExceptionName.class); | ||
private final String name; | ||
|
||
NotSerializedADExceptionName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
/** | ||
* Convert from a NotSerializableExceptionWrapper to an AnomalyDetectionException. | ||
* Since NotSerializableExceptionWrapper does not keep some details we need, we | ||
* initialize the exception with default values. | ||
* @param exception an NotSerializableExceptionWrapper exception. | ||
* @param adID Detector Id. | ||
* @return converted AnomalyDetectionException | ||
*/ | ||
public static Optional<AnomalyDetectionException> convertWrappedAnomalyDetectionException( | ||
NotSerializableExceptionWrapper exception, | ||
String adID | ||
) { | ||
String exceptionMsg = exception.getMessage().trim(); | ||
|
||
AnomalyDetectionException convertedException = null; | ||
for (NotSerializedADExceptionName adException : values()) { | ||
if (exceptionMsg.startsWith(adException.getName())) { | ||
switch (adException) { | ||
case RESOURCE_NOT_FOUND_EXCEPTION_NAME_UNDERSCORE: | ||
convertedException = new ResourceNotFoundException(adID, exceptionMsg); | ||
break; | ||
case LIMIT_EXCEEDED_EXCEPTION_NAME_UNDERSCORE: | ||
convertedException = new LimitExceededException(adID, exceptionMsg, false); | ||
break; | ||
case END_RUN_EXCEPTION_NAME_UNDERSCORE: | ||
convertedException = new EndRunException(adID, exceptionMsg, false); | ||
break; | ||
case ANOMALY_DETECTION_EXCEPTION_NAME_UNDERSCORE: | ||
convertedException = new AnomalyDetectionException(adID, exceptionMsg); | ||
break; | ||
case INTERNAL_FAILURE_NAME_UNDERSCORE: | ||
convertedException = new InternalFailure(adID, exceptionMsg); | ||
break; | ||
case CLIENT_EXCEPTION_NAME_UNDERSCORE: | ||
convertedException = new ClientException(adID, exceptionMsg); | ||
break; | ||
case CANCELLATION_EXCEPTION_NAME_UNDERSCORE: | ||
convertedException = new ADTaskCancelledException(exceptionMsg, ""); | ||
break; | ||
case DUPLICATE_TASK_EXCEPTION_NAME_UNDERSCORE: | ||
convertedException = new DuplicateTaskException(exceptionMsg); | ||
break; | ||
default: | ||
LOG.warn(new ParameterizedMessage("Unexpected AD exception {}", adException)); | ||
break; | ||
} | ||
} | ||
} | ||
return Optional.ofNullable(convertedException); | ||
} | ||
} |
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.