[Improvement][Alert] HTTP Related Alert plugin add retry logic (#14972)

* add RetryHandler for http client

* fix spotless

* update
This commit is contained in:
旺阳 2023-10-10 21:20:43 -05:00 committed by GitHub
parent 4fe41ac57f
commit a866de1754
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 107 additions and 13 deletions

View File

@ -0,0 +1,73 @@
/*
* Licensed to Apache Software Foundation (ASF) under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Apache Software Foundation (ASF) licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License 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 org.apache.dolphinscheduler.alert.api;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpRequest;
import org.apache.http.NoHttpResponseException;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.protocol.HttpContext;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.SocketException;
import java.net.UnknownHostException;
import javax.net.ssl.SSLException;
public class HttpServiceRetryStrategy implements HttpRequestRetryHandler {
public static final HttpServiceRetryStrategy retryStrategy = new HttpServiceRetryStrategy();
private static final int RETRY_COUNT = 3;
private static final long RETRY_INTERVAL_TIME = 2000L;
@Override
public boolean retryRequest(IOException exception, int executionCount, HttpContext httpContext) {
if (executionCount > RETRY_COUNT) {
return false;
}
if (exception instanceof SSLException) {
return false;
}
if (exception instanceof UnknownHostException ||
exception instanceof InterruptedIOException
|| exception instanceof NoHttpResponseException
|| exception instanceof SocketException) {
// retry interval time
try {
Thread.sleep(RETRY_INTERVAL_TIME);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
return true;
}
HttpClientContext clientContext = HttpClientContext.adapt(httpContext);
HttpRequest request = clientContext.getRequest();
// Retry if the request is considered idempotent
return !(request instanceof HttpEntityEnclosingRequest);
}
}

View File

@ -18,6 +18,7 @@
package org.apache.dolphinscheduler.plugin.alert.dingtalk;
import org.apache.dolphinscheduler.alert.api.AlertResult;
import org.apache.dolphinscheduler.alert.api.HttpServiceRetryStrategy;
import org.apache.dolphinscheduler.common.utils.JSONUtils;
import org.apache.commons.codec.binary.Base64;
@ -108,11 +109,12 @@ public final class DingTalkSender {
HttpHost httpProxy = new HttpHost(proxy, port);
CredentialsProvider provider = new BasicCredentialsProvider();
provider.setCredentials(new AuthScope(httpProxy), new UsernamePasswordCredentials(user, password));
return HttpClients.custom().setDefaultCredentialsProvider(provider).build();
return HttpClients.custom().setRetryHandler(HttpServiceRetryStrategy.retryStrategy)
.setDefaultCredentialsProvider(provider).build();
}
private static CloseableHttpClient getDefaultClient() {
return HttpClients.createDefault();
return HttpClients.custom().setRetryHandler(HttpServiceRetryStrategy.retryStrategy).build();
}
private static RequestConfig getProxyConfig(String proxy, int port) {

View File

@ -17,6 +17,8 @@
package org.apache.dolphinscheduler.plugin.alert.feishu;
import org.apache.dolphinscheduler.alert.api.HttpServiceRetryStrategy;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
@ -40,9 +42,10 @@ public final class HttpRequestUtil {
HttpHost httpProxy = new HttpHost(proxy, port);
CredentialsProvider provider = new BasicCredentialsProvider();
provider.setCredentials(new AuthScope(httpProxy), new UsernamePasswordCredentials(user, password));
return HttpClients.custom().setDefaultCredentialsProvider(provider).build();
return HttpClients.custom().setRetryHandler(HttpServiceRetryStrategy.retryStrategy)
.setDefaultCredentialsProvider(provider).build();
} else {
return HttpClients.createDefault();
return HttpClients.custom().setRetryHandler(HttpServiceRetryStrategy.retryStrategy).build();
}
}

View File

@ -18,6 +18,7 @@
package org.apache.dolphinscheduler.plugin.alert.http;
import org.apache.dolphinscheduler.alert.api.AlertResult;
import org.apache.dolphinscheduler.alert.api.HttpServiceRetryStrategy;
import org.apache.dolphinscheduler.common.utils.JSONUtils;
import org.apache.commons.lang3.StringUtils;
@ -28,7 +29,7 @@ import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
@ -106,7 +107,8 @@ public final class HttpSender {
}
public String getResponseString(HttpRequestBase httpRequest) throws IOException {
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
CloseableHttpClient httpClient =
HttpClients.custom().setRetryHandler(HttpServiceRetryStrategy.retryStrategy).build();
CloseableHttpResponse response = httpClient.execute(httpRequest);
HttpEntity entity = response.getEntity();
return EntityUtils.toString(entity, DEFAULT_CHARSET);

View File

@ -18,6 +18,7 @@
package org.apache.dolphinscheduler.plugin.alert.pagerduty;
import org.apache.dolphinscheduler.alert.api.AlertResult;
import org.apache.dolphinscheduler.alert.api.HttpServiceRetryStrategy;
import org.apache.dolphinscheduler.common.utils.JSONUtils;
import org.apache.http.HttpStatus;
@ -69,7 +70,8 @@ public final class PagerDutySender {
private AlertResult send(AlertResult alertResult, String url, String requestBody) throws IOException {
HttpPost httpPost = constructHttpPost(url, requestBody);
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpClient httpClient =
HttpClients.custom().setRetryHandler(HttpServiceRetryStrategy.retryStrategy).build();
try {
CloseableHttpResponse response = httpClient.execute(httpPost);

View File

@ -17,6 +17,7 @@
package org.apache.dolphinscheduler.plugin.alert.slack;
import org.apache.dolphinscheduler.alert.api.HttpServiceRetryStrategy;
import org.apache.dolphinscheduler.common.utils.JSONUtils;
import org.apache.commons.lang3.StringUtils;
@ -65,7 +66,9 @@ public final class SlackSender {
* @return slack response
*/
public String sendMessage(String title, String content) {
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
try (
CloseableHttpClient httpClient =
HttpClients.custom().setRetryHandler(HttpServiceRetryStrategy.retryStrategy).build()) {
Map<String, Object> paramMap = new HashMap<>();
paramMap.put(SlackParamsConstants.SLACK_BOT_NAME, botName);
paramMap.put(SlackParamsConstants.TEXT, title);

View File

@ -19,6 +19,7 @@ package org.apache.dolphinscheduler.plugin.alert.telegram;
import org.apache.dolphinscheduler.alert.api.AlertData;
import org.apache.dolphinscheduler.alert.api.AlertResult;
import org.apache.dolphinscheduler.alert.api.HttpServiceRetryStrategy;
import org.apache.dolphinscheduler.common.utils.JSONUtils;
import org.apache.commons.lang3.StringUtils;
@ -239,14 +240,15 @@ public final class TelegramSender {
}
private static CloseableHttpClient getDefaultClient() {
return HttpClients.createDefault();
return HttpClients.custom().setRetryHandler(HttpServiceRetryStrategy.retryStrategy).build();
}
private static CloseableHttpClient getProxyClient(String proxy, int port, String user, String password) {
HttpHost httpProxy = new HttpHost(proxy, port);
CredentialsProvider provider = new BasicCredentialsProvider();
provider.setCredentials(new AuthScope(httpProxy), new UsernamePasswordCredentials(user, password));
return HttpClients.custom().setDefaultCredentialsProvider(provider).build();
return HttpClients.custom().setRetryHandler(HttpServiceRetryStrategy.retryStrategy)
.setDefaultCredentialsProvider(provider).build();
}
private static RequestConfig getProxyConfig(String proxy, int port) {

View File

@ -19,6 +19,7 @@ package org.apache.dolphinscheduler.plugin.alert.webexteams;
import org.apache.dolphinscheduler.alert.api.AlertData;
import org.apache.dolphinscheduler.alert.api.AlertResult;
import org.apache.dolphinscheduler.alert.api.HttpServiceRetryStrategy;
import org.apache.dolphinscheduler.common.utils.JSONUtils;
import org.apache.http.HttpStatus;
@ -77,7 +78,8 @@ public final class WebexTeamsSender {
}
private void send(AlertResult alertResult, AlertData alertData) throws IOException {
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpClient httpClient =
HttpClients.custom().setRetryHandler(HttpServiceRetryStrategy.retryStrategy).build();
try {
HttpPost httpPost = constructHttpPost(getMessage(alertData), botAccessToken);

View File

@ -24,6 +24,7 @@ import static org.apache.dolphinscheduler.plugin.alert.wechat.WeChatAlertConstan
import org.apache.dolphinscheduler.alert.api.AlertConstants;
import org.apache.dolphinscheduler.alert.api.AlertResult;
import org.apache.dolphinscheduler.alert.api.HttpServiceRetryStrategy;
import org.apache.dolphinscheduler.common.utils.JSONUtils;
import org.apache.commons.lang3.StringUtils;
@ -80,7 +81,9 @@ public final class WeChatSender {
}
private static String post(String url, String data) throws IOException {
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
try (
CloseableHttpClient httpClient =
HttpClients.custom().setRetryHandler(HttpServiceRetryStrategy.retryStrategy).build()) {
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new StringEntity(data, WeChatAlertConstants.CHARSET));
CloseableHttpResponse response = httpClient.execute(httpPost);
@ -133,7 +136,9 @@ public final class WeChatSender {
private static String get(String url) throws IOException {
String resp;
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
try (
CloseableHttpClient httpClient =
HttpClients.custom().setRetryHandler(HttpServiceRetryStrategy.retryStrategy).build();) {
HttpGet httpGet = new HttpGet(url);
try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
HttpEntity entity = response.getEntity();