From 2dbf2d6e7f203e9954488b4bcbbe52a34499b809 Mon Sep 17 00:00:00 2001 From: yuluo-yx Date: Thu, 15 Aug 2024 17:45:08 +0800 Subject: [PATCH] [optimize] use okhttpclient connection pool Signed-off-by: yuluo-yx --- .../controller/AlertConvergeController.java | 10 +++++----- .../controller/AlertDefineController.java | 10 +++++----- .../controller/AlertSilenceController.java | 10 +++++----- .../AlertDefineExcelImExportServiceImpl.java | 11 ---------- .../dispatch/unit/impl/TimeLengthConvert.java | 1 - .../common/constants/NetworkConstants.java | 18 +++++++++++++++++ .../impl/AbstractAlertNotifyHandlerImpl.java | 3 --- .../manager/config/AiProperties.java | 2 -- .../manager/config/RestTemplateConfig.java | 20 +++++++++++++------ .../history/influxdb/InfluxdbDataStorage.java | 20 ++++++++++++------- 10 files changed, 60 insertions(+), 45 deletions(-) diff --git a/alerter/src/main/java/org/apache/hertzbeat/alert/controller/AlertConvergeController.java b/alerter/src/main/java/org/apache/hertzbeat/alert/controller/AlertConvergeController.java index df38ea5a95c..f3b97409db9 100644 --- a/alerter/src/main/java/org/apache/hertzbeat/alert/controller/AlertConvergeController.java +++ b/alerter/src/main/java/org/apache/hertzbeat/alert/controller/AlertConvergeController.java @@ -23,6 +23,7 @@ import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.validation.Valid; +import java.util.Objects; import org.apache.hertzbeat.alert.service.AlertConvergeService; import org.apache.hertzbeat.common.entity.alerter.AlertConverge; import org.apache.hertzbeat.common.entity.dto.Message; @@ -69,11 +70,10 @@ public ResponseEntity> modifyAlertConverge(@Valid @RequestBody Ale public ResponseEntity> getAlertConverge( @Parameter(description = "Alarm Converge ID", example = "6565463543") @PathVariable("id") long id) { AlertConverge alertConverge = alertConvergeService.getAlertConverge(id); - if (alertConverge == null) { - return ResponseEntity.ok(Message.fail(MONITOR_NOT_EXIST_CODE, "AlertConverge not exist.")); - } else { - return ResponseEntity.ok(Message.success(alertConverge)); - } + + return Objects.isNull(alertConverge) + ? ResponseEntity.ok(Message.fail(MONITOR_NOT_EXIST_CODE, "AlertConverge not exist.")) + : ResponseEntity.ok(Message.success(alertConverge)); } } diff --git a/alerter/src/main/java/org/apache/hertzbeat/alert/controller/AlertDefineController.java b/alerter/src/main/java/org/apache/hertzbeat/alert/controller/AlertDefineController.java index 33f4292bacb..9da2e018640 100644 --- a/alerter/src/main/java/org/apache/hertzbeat/alert/controller/AlertDefineController.java +++ b/alerter/src/main/java/org/apache/hertzbeat/alert/controller/AlertDefineController.java @@ -24,6 +24,7 @@ import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.validation.Valid; import java.util.List; +import java.util.Objects; import java.util.stream.Collectors; import org.apache.hertzbeat.alert.service.AlertDefineService; import org.apache.hertzbeat.common.entity.alerter.AlertDefine; @@ -76,11 +77,10 @@ public ResponseEntity> getAlertDefine( @Parameter(description = "Alarm Definition ID", example = "6565463543") @PathVariable("id") long id) { // Obtaining Monitoring Information AlertDefine alertDefine = alertDefineService.getAlertDefine(id); - if (alertDefine == null) { - return ResponseEntity.ok(Message.fail(MONITOR_NOT_EXIST_CODE, "AlertDefine not exist.")); - } else { - return ResponseEntity.ok(Message.success(alertDefine)); - } + + return Objects.isNull(alertDefine) + ? ResponseEntity.ok(Message.fail(MONITOR_NOT_EXIST_CODE, "AlertDefine not exist.")) + : ResponseEntity.ok(Message.success(alertDefine)); } @DeleteMapping(path = "/{id}") diff --git a/alerter/src/main/java/org/apache/hertzbeat/alert/controller/AlertSilenceController.java b/alerter/src/main/java/org/apache/hertzbeat/alert/controller/AlertSilenceController.java index 56e9af3f022..6e6e1d7f458 100644 --- a/alerter/src/main/java/org/apache/hertzbeat/alert/controller/AlertSilenceController.java +++ b/alerter/src/main/java/org/apache/hertzbeat/alert/controller/AlertSilenceController.java @@ -23,6 +23,7 @@ import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.validation.Valid; +import java.util.Objects; import org.apache.hertzbeat.alert.service.AlertSilenceService; import org.apache.hertzbeat.common.entity.alerter.AlertSilence; import org.apache.hertzbeat.common.entity.dto.Message; @@ -69,11 +70,10 @@ public ResponseEntity> modifyAlertSilence(@Valid @RequestBody Aler public ResponseEntity> getAlertSilence( @Parameter(description = "Alarm Silence ID", example = "6565463543") @PathVariable("id") long id) { AlertSilence alertSilence = alertSilenceService.getAlertSilence(id); - if (alertSilence == null) { - return ResponseEntity.ok(Message.fail(MONITOR_NOT_EXIST_CODE, "AlertSilence not exist.")); - } else { - return ResponseEntity.ok(Message.success(alertSilence)); - } + + return Objects.isNull(alertSilence) + ? ResponseEntity.ok(Message.fail(MONITOR_NOT_EXIST_CODE, "AlertSilence not exist.")) + : ResponseEntity.ok(Message.success(alertSilence)); } } diff --git a/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineExcelImExportServiceImpl.java b/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineExcelImExportServiceImpl.java index c89eb4d417e..5ea86d30d63 100644 --- a/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineExcelImExportServiceImpl.java +++ b/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineExcelImExportServiceImpl.java @@ -98,17 +98,6 @@ public List parseImport(InputStream is) { } } - private TagItem extractTagDataFromRow(Row row) { - String name = getCellValueAsString(row.getCell(7)); - if (StringUtils.hasText(name)) { - TagItem tagItem = new TagItem(); - tagItem.setName(name); - tagItem.setValue(getCellValueAsString(row.getCell(8))); - return tagItem; - } - return null; - } - private List extractTagDataFromRow(Cell cell) { String jsonStr = getCellValueAsString(cell); if (StringUtils.hasText(jsonStr)) { diff --git a/collector/src/main/java/org/apache/hertzbeat/collector/dispatch/unit/impl/TimeLengthConvert.java b/collector/src/main/java/org/apache/hertzbeat/collector/dispatch/unit/impl/TimeLengthConvert.java index 18fb266ada2..a185d6b1a13 100644 --- a/collector/src/main/java/org/apache/hertzbeat/collector/dispatch/unit/impl/TimeLengthConvert.java +++ b/collector/src/main/java/org/apache/hertzbeat/collector/dispatch/unit/impl/TimeLengthConvert.java @@ -29,7 +29,6 @@ @Component public final class TimeLengthConvert implements UnitConvert { - @Override public String convert(String value, String originUnit, String newUnit) { if (value == null || value.isEmpty()) { diff --git a/common/src/main/java/org/apache/hertzbeat/common/constants/NetworkConstants.java b/common/src/main/java/org/apache/hertzbeat/common/constants/NetworkConstants.java index 17252765e91..de58a2e1fe7 100644 --- a/common/src/main/java/org/apache/hertzbeat/common/constants/NetworkConstants.java +++ b/common/src/main/java/org/apache/hertzbeat/common/constants/NetworkConstants.java @@ -47,4 +47,22 @@ public interface NetworkConstants { String LOCATION = "Location"; + /** + * HttpClient Configuration Constants. + */ + interface HttpClientConstants { + + int READ_TIME_OUT = 6 * 1000; + int WRITE_TIME_OUT = 6 * 1000; + int CONNECT_TIME_OUT = 6 * 1000; + int CHUNK_SIZE = 8196; + int MAX_IDLE_CONNECTIONS = 20; + int KEEP_ALIVE_TIMEOUT = 30 * 1000; + int HTTP_CLIENT_CONNECTION_MANAGER_MAX_PER_ROUTE = 20; + int HTTP_CLIENT_CONNECTION_MANAGER_MAX_TOTAL = 20; + int HTTPCLIENT_KEEP_ALIVE_DURATION = 30 * 1000; + int HTTP_CLIENT_CONNECTION_MANAGER_CLOSE_WAIT_TIME_MS = 1000; + int HTTP_CLIENT_CONNECTION_MANAGER_CLOSE_IDLE_TIME_S = 30; + } + } diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/component/alerter/impl/AbstractAlertNotifyHandlerImpl.java b/manager/src/main/java/org/apache/hertzbeat/manager/component/alerter/impl/AbstractAlertNotifyHandlerImpl.java index a44af67211f..492e340d9e8 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/component/alerter/impl/AbstractAlertNotifyHandlerImpl.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/component/alerter/impl/AbstractAlertNotifyHandlerImpl.java @@ -38,7 +38,6 @@ import org.apache.hertzbeat.common.support.event.SystemConfigChangeEvent; import org.apache.hertzbeat.common.util.ResourceBundleUtil; import org.apache.hertzbeat.manager.component.alerter.AlertNotifyHandler; -import org.apache.hertzbeat.manager.service.NoticeConfigService; import org.springframework.context.event.EventListener; import org.springframework.ui.freemarker.FreeMarkerTemplateUtils; import org.springframework.web.client.RestTemplate; @@ -56,8 +55,6 @@ abstract class AbstractAlertNotifyHandlerImpl implements AlertNotifyHandler { protected RestTemplate restTemplate; @Resource protected AlerterProperties alerterProperties; - @Resource - protected NoticeConfigService noticeConfigService; protected String renderContent(NoticeTemplate noticeTemplate, Alert alert) throws TemplateException, IOException { diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/config/AiProperties.java b/manager/src/main/java/org/apache/hertzbeat/manager/config/AiProperties.java index a3a1040ffbf..d8628493b75 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/config/AiProperties.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/config/AiProperties.java @@ -49,6 +49,4 @@ public class AiProperties { */ private String apiSecret; - - } diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/config/RestTemplateConfig.java b/manager/src/main/java/org/apache/hertzbeat/manager/config/RestTemplateConfig.java index d07735c2aee..d856c1d9b78 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/config/RestTemplateConfig.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/config/RestTemplateConfig.java @@ -19,7 +19,9 @@ import java.util.Collections; import java.util.concurrent.TimeUnit; +import okhttp3.ConnectionPool; import okhttp3.OkHttpClient; +import org.apache.hertzbeat.common.constants.NetworkConstants; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.client.ClientHttpRequestFactory; @@ -42,12 +44,18 @@ public RestTemplate restTemplate(ClientHttpRequestFactory factory) { @Bean public ClientHttpRequestFactory simpleClientHttpRequestFactory() { - OkHttpClient client = new OkHttpClient.Builder() - .connectTimeout(15, TimeUnit.SECONDS) - .readTimeout(20, TimeUnit.SECONDS) - .writeTimeout(20, TimeUnit.SECONDS) - .build(); - return new OkHttp3ClientHttpRequestFactory(client); + + return new OkHttp3ClientHttpRequestFactory( + new OkHttpClient.Builder() + .readTimeout(NetworkConstants.HttpClientConstants.READ_TIME_OUT, TimeUnit.SECONDS) + .writeTimeout(NetworkConstants.HttpClientConstants.WRITE_TIME_OUT, TimeUnit.SECONDS) + .connectTimeout(NetworkConstants.HttpClientConstants.CONNECT_TIME_OUT, TimeUnit.SECONDS) + .connectionPool(new ConnectionPool( + NetworkConstants.HttpClientConstants.MAX_IDLE_CONNECTIONS, + NetworkConstants.HttpClientConstants.KEEP_ALIVE_TIMEOUT, + TimeUnit.SECONDS) + ).build() + ); } } diff --git a/warehouse/src/main/java/org/apache/hertzbeat/warehouse/store/history/influxdb/InfluxdbDataStorage.java b/warehouse/src/main/java/org/apache/hertzbeat/warehouse/store/history/influxdb/InfluxdbDataStorage.java index 65aded4adc6..85a21f3f0f4 100644 --- a/warehouse/src/main/java/org/apache/hertzbeat/warehouse/store/history/influxdb/InfluxdbDataStorage.java +++ b/warehouse/src/main/java/org/apache/hertzbeat/warehouse/store/history/influxdb/InfluxdbDataStorage.java @@ -35,8 +35,10 @@ import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import lombok.extern.slf4j.Slf4j; +import okhttp3.ConnectionPool; import okhttp3.OkHttpClient; import org.apache.hertzbeat.common.constants.CommonConstants; +import org.apache.hertzbeat.common.constants.NetworkConstants; import org.apache.hertzbeat.common.entity.dto.Value; import org.apache.hertzbeat.common.entity.message.CollectRep; import org.apache.hertzbeat.common.util.JsonUtil; @@ -83,14 +85,18 @@ public InfluxdbDataStorage(InfluxdbProperties influxdbProperties) { } public void initInfluxDb(InfluxdbProperties influxdbProperties) { - OkHttpClient.Builder client = new OkHttpClient.Builder() - .connectTimeout(10, TimeUnit.SECONDS) - .writeTimeout(10, TimeUnit.SECONDS) - .readTimeout(10, TimeUnit.SECONDS) - .retryOnConnectionFailure(true); - client.sslSocketFactory(defaultSslSocketFactory(), defaultTrustManager()); - client.hostnameVerifier(noopHostnameVerifier()); + var client = new OkHttpClient.Builder() + .readTimeout(NetworkConstants.HttpClientConstants.READ_TIME_OUT, TimeUnit.SECONDS) + .writeTimeout(NetworkConstants.HttpClientConstants.WRITE_TIME_OUT, TimeUnit.SECONDS) + .connectTimeout(NetworkConstants.HttpClientConstants.CONNECT_TIME_OUT, TimeUnit.SECONDS) + .connectionPool(new ConnectionPool( + NetworkConstants.HttpClientConstants.MAX_IDLE_CONNECTIONS, + NetworkConstants.HttpClientConstants.KEEP_ALIVE_TIMEOUT, + TimeUnit.SECONDS) + ).sslSocketFactory(defaultSslSocketFactory(), defaultTrustManager()) + .hostnameVerifier(noopHostnameVerifier()) + .retryOnConnectionFailure(true); this.influxDb = InfluxDBFactory.connect(influxdbProperties.serverUrl(), influxdbProperties.username(), influxdbProperties.password(), client); // Close it if your application is terminating, or you are not using it anymore.