Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[optimize] use okhttpclient connection pool #2529

Merged
merged 2 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -69,11 +70,10 @@ public ResponseEntity<Message<Void>> modifyAlertConverge(@Valid @RequestBody Ale
public ResponseEntity<Message<AlertConverge>> 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));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -76,11 +77,10 @@ public ResponseEntity<Message<AlertDefine>> 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}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -69,11 +70,10 @@ public ResponseEntity<Message<Void>> modifyAlertSilence(@Valid @RequestBody Aler
public ResponseEntity<Message<AlertSilence>> 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));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,6 @@ public List<ExportAlertDefineDTO> 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<TagItem> extractTagDataFromRow(Cell cell) {
String jsonStr = getCellValueAsString(cell);
if (StringUtils.hasText(jsonStr)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,4 @@ public class AiProperties {
*/
private String apiSecret;



}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down
Loading