-
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.
Signed-off-by: Sean Kao <[email protected]>
- Loading branch information
1 parent
3ff2ef2
commit d05fe0c
Showing
7 changed files
with
137 additions
and
1 deletion.
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
62 changes: 62 additions & 0 deletions
62
...main/scala/org/opensearch/flint/core/http/handler/HttpResponseMessageResultPredicate.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,62 @@ | ||
/* | ||
* 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.util.EntityUtils; | ||
|
||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.Set; | ||
import java.util.logging.Logger; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Failure handler based on content in HTTP response. | ||
* | ||
* @param <T> result type (supposed to be HttpResponse for OS client) | ||
*/ | ||
public class HttpResponseMessageResultPredicate<T> implements CheckedPredicate<T> { | ||
|
||
private static final Logger LOG = Logger.getLogger(HttpResponseMessageResultPredicate.class.getName()); | ||
|
||
/** | ||
* Retryable HTTP response message list | ||
*/ | ||
private final Set<String> retryableResponseMessages; | ||
|
||
public HttpResponseMessageResultPredicate(String messages) { | ||
this.retryableResponseMessages = | ||
Arrays.stream(messages.split(",")) | ||
.map(String::trim) | ||
.collect(Collectors.toSet()); | ||
} | ||
|
||
@Override | ||
public boolean test(T result) throws Throwable { | ||
HttpResponse response = (HttpResponse) result; | ||
HttpEntity entity = response.getEntity(); | ||
if (entity != null) { | ||
try { | ||
String responseContent = EntityUtils.toString(entity); | ||
LOG.info("Checking if response message is retryable: " + responseContent); | ||
|
||
boolean isRetryable = retryableResponseMessages.stream() | ||
.anyMatch(retryableMessage -> responseContent.contains(retryableMessage)); | ||
|
||
LOG.info("Check retryable result: " + isRetryable); | ||
return isRetryable; | ||
} catch (IOException e) { | ||
LOG.warning("Unable to parse response body. Retryable result: false"); | ||
return false; | ||
} | ||
} | ||
LOG.info("No response entity found. Retryable result: false"); | ||
return false; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
flint-core/src/main/scala/org/opensearch/flint/core/http/handler/HttpResultPredicate.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,29 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.flint.core.http.handler; | ||
|
||
import dev.failsafe.function.CheckedPredicate; | ||
|
||
/** | ||
* Failure handler based on HTTP response. | ||
* | ||
* @param <T> result type (supposed to be HttpResponse for OS client) | ||
*/ | ||
public class HttpResultPredicate<T> implements CheckedPredicate<T> { | ||
|
||
private final HttpStatusCodeResultPredicate<T> statusCodePredicate; | ||
private final HttpResponseMessageResultPredicate<T> responseMessagePredicate; | ||
|
||
public HttpResultPredicate(HttpStatusCodeResultPredicate<T> statusCodePredicate, HttpResponseMessageResultPredicate<T> responseMessagePredicate) { | ||
this.statusCodePredicate = statusCodePredicate; | ||
this.responseMessagePredicate = responseMessagePredicate; | ||
} | ||
|
||
@Override | ||
public boolean test(T result) throws Throwable { | ||
return statusCodePredicate.test(result) || responseMessagePredicate.test(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