-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
check retryable response only if 400
Signed-off-by: Sean Kao <seankao@amazon.com>
- Loading branch information
1 parent
54c426f
commit 4212dac
Showing
7 changed files
with
76 additions
and
96 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
68 changes: 68 additions & 0 deletions
68
...t-core/src/main/scala/org/opensearch/flint/core/http/handler/HttpAOSSResultPredicate.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,68 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.flint.core.http.handler; | ||
|
||
import dev.failsafe.function.CheckedPredicate; | ||
import org.apache.http.HttpEntity; | ||
import org.apache.http.HttpResponse; | ||
import org.apache.http.entity.BufferedHttpEntity; | ||
import org.apache.http.util.EntityUtils; | ||
|
||
import java.util.Arrays; | ||
import java.util.Set; | ||
import java.util.logging.Logger; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Failure handler based on HTTP response from AOSS. | ||
* | ||
* @param <T> result type (supposed to be HttpResponse for OS client) | ||
*/ | ||
public class HttpAOSSResultPredicate<T> implements CheckedPredicate<T> { | ||
|
||
private static final Logger LOG = Logger.getLogger(HttpAOSSResultPredicate.class.getName()); | ||
|
||
public static final int BAD_REQUEST_STATUS_CODE = 400; | ||
public static final String RESOURCE_ALREADY_EXISTS_EXCEPTION_MESSAGE = "resource_already_exists_exception"; | ||
|
||
public HttpAOSSResultPredicate() { } | ||
|
||
@Override | ||
public boolean test(T result) throws Throwable { | ||
LOG.info("Checking if response is retryable"); | ||
|
||
int statusCode = ((HttpResponse) result).getStatusLine().getStatusCode(); | ||
if (statusCode != BAD_REQUEST_STATUS_CODE) { | ||
LOG.info("Status code " + statusCode + " is not " + BAD_REQUEST_STATUS_CODE + ". Check result: false"); | ||
return false; | ||
} | ||
|
||
HttpResponse response = (HttpResponse) result; | ||
HttpEntity entity = response.getEntity(); | ||
if (entity == null) { | ||
LOG.info("No response entity found. Check result: false"); | ||
return false; | ||
} | ||
|
||
// Buffer the entity to make it repeatable | ||
BufferedHttpEntity bufferedEntity = new BufferedHttpEntity(entity); | ||
response.setEntity(bufferedEntity); | ||
|
||
try { | ||
String responseContent = EntityUtils.toString(bufferedEntity); | ||
// Reset the entity's content | ||
bufferedEntity.getContent().reset(); | ||
|
||
boolean isRetryable = responseContent.contains(RESOURCE_ALREADY_EXISTS_EXCEPTION_MESSAGE); | ||
|
||
LOG.info("Check retryable response result: " + isRetryable); | ||
return isRetryable; | ||
} catch (Exception e) { | ||
LOG.info("Unable to parse response body. Check result: false"); | ||
return false; | ||
} | ||
} | ||
} |
70 changes: 0 additions & 70 deletions
70
...main/scala/org/opensearch/flint/core/http/handler/HttpResponseMessageResultPredicate.java
This file was deleted.
Oops, something went wrong.
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