-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: Add dedicated thread pool for RestTemplate in alert notificati… #3481
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
base: master
Are you sure you want to change the base?
feat: Add dedicated thread pool for RestTemplate in alert notificati… #3481
Conversation
…into RestTemplateConfig-Thread-pool
| executor.setCorePoolSize(10); | ||
| executor.setMaxPoolSize(50); | ||
| executor.setQueueCapacity(200); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hi the http request require timeliness, suggest use the SynchronousQueue instead of executor.setQueueCapacity(200); , and executor.setCorePoolSize(2); executor.setMaxPoolSize(Integer.MAX_VALUE);
| protected CompletableFuture<Void> sendAsync(org.apache.hertzbeat.common.entity.alerter.NoticeReceiver receiver, | ||
| org.apache.hertzbeat.common.entity.alerter.NoticeTemplate noticeTemplate, | ||
| GroupAlert alert) { | ||
| return CompletableFuture.runAsync(() -> { | ||
| try { | ||
| send(receiver, noticeTemplate, alert); | ||
| } catch (Exception e) { | ||
| log.error("Async alert notification failed", e); | ||
| throw new RuntimeException(e); | ||
| } | ||
| }, restTemplateThreadPool); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seem no others use this method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have deleted this piece of code.
| CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])) | ||
| .whenComplete((result, exception) -> { | ||
| if (exception != null) { | ||
| log.warn("Some async notifications failed", exception); | ||
| } else { | ||
| log.debug("All notifications completed for alert: {}", alert.getGroupLabels()); | ||
| } | ||
| }); | ||
| }))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hi can you describe why use the CompleableFuture here, sendNoticeMsg does not need to return a result; it just needs to handle exceptions.
|
hi suggest merge this in 1.7.3 after 1.7.2 released. |
| matchNoticeRulesByAlert(alert).ifPresent(noticeRules -> noticeRules.forEach(rule -> workerPool.executeNotify(() -> { | ||
| rule.getReceiverId().forEach(receiverId -> { | ||
| restTemplateThreadPool.execute(() -> { | ||
| try { | ||
| sendNoticeMsg(getOneReceiverById(receiverId), | ||
| getOneTemplateById(rule.getTemplateId()), alert); | ||
| } catch (AlertNoticeException e) { | ||
| log.warn("DispatchTask sendNoticeMsg error, message: {}", e.getMessage()); | ||
| sendNoticeMsg(getOneReceiverById(receiverId), getOneTemplateById(rule.getTemplateId()), alert); | ||
| } catch (Exception e) { | ||
| log.warn("Async notification failed for receiver {}: {}", receiverId, e.getMessage()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking back at this PR again, it is not recommended that the alterer module use the manager module's bean, and the notification uses two thread pools. suggest that use the workerPool instead of restTemplateThreadPool directly.
matchNoticeRulesByAlert(alert).ifPresent(noticeRules -> noticeRules.forEach(rule -> rule.getReceiverId()
.forEach(receiverId -> {
workerPool.executeNotify(() -> {
try {
sendNoticeMsg(getOneReceiverById(receiverId),
getOneTemplateById(rule.getTemplateId()), alert);
} catch (AlertNoticeException e) {
log.warn("DispatchTask sendNoticeMsg error, message: {}", e.getMessage());
}
});
})));
feat: Add dedicated thread pool for RestTemplate in alert notification system
What's changed?
RestTemplate thread pool
Added a
restTemplateThreadPoolbean inhertzbeat-manager/src/main/java/org/apache/hertzbeat/manager/config/RestTemplateConfig.javawith:
RestTemplate-CallerRunsPolicyAsync alert notification
In
hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/notice/impl/AbstractAlertNotifyHandlerImpl.javaintroduced a
sendAsync()method and injected the new thread‐pool executor to offload HTTP calls.Parallel dispatch optimization
Updated
hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/notice/AlertNoticeDispatch.javato:
CompletableFuturefor sending notifications in parallelTest updates
In
hertzbeat-alerter/src/test/java/org/apache/hertzbeat/alert/notice/AlertNoticeDispatchTest.javaupdated the constructor calls to pass a mock executor and added test cases covering the async paths.
Checklist
Add or update API