-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add retryable AOSS HTTP response (#951)
* add retryable response message Signed-off-by: Sean Kao <[email protected]> * check retryable response only if 400 Signed-off-by: Sean Kao <[email protected]> * add handler for aoss only Signed-off-by: Sean Kao <[email protected]> * edit comment Signed-off-by: Sean Kao <[email protected]> * bugfix: aoss result predicate not used Signed-off-by: Sean Kao <[email protected]> --------- Signed-off-by: Sean Kao <[email protected]> (cherry picked from commit 5720e54) Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
321020d
commit a8bbd19
Showing
3 changed files
with
111 additions
and
3 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
66 changes: 66 additions & 0 deletions
66
...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,66 @@ | ||
/* | ||
* 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.logging.Logger; | ||
|
||
/** | ||
* 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, so that this retry test does not consume the content stream, | ||
// resulting in the request caller getting empty response | ||
BufferedHttpEntity bufferedEntity = new BufferedHttpEntity(entity); | ||
response.setEntity(bufferedEntity); | ||
|
||
try { | ||
String responseContent = EntityUtils.toString(bufferedEntity); | ||
// Effectively restores the content stream of the response | ||
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; | ||
} | ||
} | ||
} |
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