Unverified Commit b5b99f67 authored by gaoyan's avatar gaoyan Committed by GitHub

Fix some npe (#2597)

* fix alert some npe * fix alert some npe
parent 17ed721a
...@@ -31,6 +31,9 @@ import org.dinky.data.options.JobAlertRuleOptions; ...@@ -31,6 +31,9 @@ import org.dinky.data.options.JobAlertRuleOptions;
import org.dinky.job.JobConfig; import org.dinky.job.JobConfig;
import org.dinky.utils.TimeUtil; import org.dinky.utils.TimeUtil;
import java.time.LocalDateTime;
import java.util.Optional;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import cn.hutool.core.text.StrFormatter; import cn.hutool.core.text.StrFormatter;
...@@ -144,6 +147,10 @@ public class JobAlertData { ...@@ -144,6 +147,10 @@ public class JobAlertData {
jobInstance.getTaskId()); jobInstance.getTaskId());
} }
private static String getTime(LocalDateTime time) {
return time == null ? "" : TimeUtil.convertTimeToString(time);
}
public static JobAlertData buildData(JobInfoDetail jobInfoDetail) { public static JobAlertData buildData(JobInfoDetail jobInfoDetail) {
JobAlertDataBuilder builder = JobAlertData.builder(); JobAlertDataBuilder builder = JobAlertData.builder();
builder.alertTime(TimeUtil.nowStr()); builder.alertTime(TimeUtil.nowStr());
...@@ -163,12 +170,13 @@ public class JobAlertData { ...@@ -163,12 +170,13 @@ public class JobAlertData {
.taskUrl(buildTaskUrl(jobInstance)) .taskUrl(buildTaskUrl(jobInstance))
.jobName(jobInstance.getName()) .jobName(jobInstance.getName())
.jobId(jobInstance.getJid()) .jobId(jobInstance.getJid())
.duration(jobInstance.getDuration()) .duration(Optional.ofNullable(jobInstance.getDuration()).orElse(0L))
.jobStartTime(TimeUtil.convertTimeToString(jobInstance.getCreateTime())) .jobStartTime(getTime(jobInstance.getCreateTime()))
.jobEndTime(TimeUtil.convertTimeToString(jobInstance.getFinishTime())); .jobEndTime(getTime(jobInstance.getFinishTime()));
if (job != null) { if (job != null) {
builder.batchModel(job.isBatchModel()); builder.batchModel(job.isBatchModel());
} }
if (clusterInstance != null) { if (clusterInstance != null) {
builder.clusterName(clusterInstance.getName()) builder.clusterName(clusterInstance.getName())
.clusterType(clusterInstance.getType()) .clusterType(clusterInstance.getType())
...@@ -180,7 +188,13 @@ public class JobAlertData { ...@@ -180,7 +188,13 @@ public class JobAlertData {
} else if (exceptions != null && ExceptionRule.isException(id, exceptions)) { } else if (exceptions != null && ExceptionRule.isException(id, exceptions)) {
// The error message is too long to send an alarm, // The error message is too long to send an alarm,
// and only the first line of abnormal information is used // and only the first line of abnormal information is used
builder.isException(true).errorMsg(exceptions.getRootException().split("\n")[0]); String err = Optional.ofNullable(exceptions.getRootException())
.orElse("dinky didn't get any ERROR!")
.split("\n")[0];
if (err.length() > 100) {
err = err.substring(0, 100) + "...";
}
builder.isException(true).errorMsg(err);
} }
if (checkpoints != null) { if (checkpoints != null) {
......
...@@ -28,7 +28,9 @@ import org.dinky.context.SpringContextUtils; ...@@ -28,7 +28,9 @@ import org.dinky.context.SpringContextUtils;
import org.dinky.daemon.pool.FlinkJobThreadPool; import org.dinky.daemon.pool.FlinkJobThreadPool;
import org.dinky.data.dto.AlertRuleDTO; import org.dinky.data.dto.AlertRuleDTO;
import org.dinky.data.dto.TaskDTO; import org.dinky.data.dto.TaskDTO;
import org.dinky.data.enums.JobLifeCycle;
import org.dinky.data.enums.Status; import org.dinky.data.enums.Status;
import org.dinky.data.exception.DinkyException;
import org.dinky.data.model.alert.AlertGroup; import org.dinky.data.model.alert.AlertGroup;
import org.dinky.data.model.alert.AlertHistory; import org.dinky.data.model.alert.AlertHistory;
import org.dinky.data.model.alert.AlertInstance; import org.dinky.data.model.alert.AlertInstance;
...@@ -45,6 +47,7 @@ import java.io.IOException; ...@@ -45,6 +47,7 @@ import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.jeasy.rules.api.Facts; import org.jeasy.rules.api.Facts;
...@@ -117,7 +120,13 @@ public class JobAlertHandler { ...@@ -117,7 +120,13 @@ public class JobAlertHandler {
public void check(JobInfoDetail jobInfoDetail) { public void check(JobInfoDetail jobInfoDetail) {
Facts ruleFacts = new Facts(); Facts ruleFacts = new Facts();
JobAlertData jobAlertData = JobAlertData.buildData(jobInfoDetail); JobAlertData jobAlertData = JobAlertData.buildData(jobInfoDetail);
JsonUtils.toMap(jobAlertData).forEach(ruleFacts::put); JsonUtils.toMap(jobAlertData).forEach((k, v) -> {
if (v == null) {
throw new DinkyException(StrFormatter.format(
"When deal alert job data, the key [{}] value is null, its maybe dinky bug,please report", k));
}
ruleFacts.put(k, v);
});
rulesEngine.fire(rules, ruleFacts); rulesEngine.fire(rules, ruleFacts);
} }
...@@ -171,6 +180,10 @@ public class JobAlertHandler { ...@@ -171,6 +180,10 @@ public class JobAlertHandler {
*/ */
private void executeAlertAction(Facts facts, AlertRuleDTO alertRuleDTO) { private void executeAlertAction(Facts facts, AlertRuleDTO alertRuleDTO) {
TaskDTO task = taskService.getTaskInfoById(facts.get(JobAlertRuleOptions.FIELD_TASK_ID)); TaskDTO task = taskService.getTaskInfoById(facts.get(JobAlertRuleOptions.FIELD_TASK_ID));
if (!Objects.equals(task.getStep(), JobLifeCycle.PUBLISH.getValue())) {
// Only publish job can be alerted
return;
}
Map<String, Object> dataModel = new HashMap<>(facts.asMap()); Map<String, Object> dataModel = new HashMap<>(facts.asMap());
dataModel.put(JobAlertRuleOptions.OPTIONS_JOB_ALERT_RULE, alertRuleDTO); dataModel.put(JobAlertRuleOptions.OPTIONS_JOB_ALERT_RULE, alertRuleDTO);
String alertContent; String alertContent;
......
...@@ -39,10 +39,12 @@ import cn.hutool.core.util.URLUtil; ...@@ -39,10 +39,12 @@ import cn.hutool.core.util.URLUtil;
import cn.hutool.json.JSONArray; import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject; import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
/** /**
* The Job Metrics Handler class is used to process operations related to job metrics。 * The Job Metrics Handler class is used to process operations related to job metrics。
*/ */
@Slf4j
public class JobMetricsHandler { public class JobMetricsHandler {
/** /**
...@@ -63,13 +65,17 @@ public class JobMetricsHandler { ...@@ -63,13 +65,17 @@ public class JobMetricsHandler {
() -> fetchFlinkMetrics(e.getKey(), e.getValue(), jobManagerUrls, jobId))) () -> fetchFlinkMetrics(e.getKey(), e.getValue(), jobManagerUrls, jobId)))
.toArray(CompletableFuture[]::new); .toArray(CompletableFuture[]::new);
// Wait for all Completable Future executions to finish // Wait for all Completable Future executions to finish
AsyncUtil.waitAll(array); try {
MetricsVO metricsVO = new MetricsVO(); AsyncUtil.waitAll(array);
metricsVO.setContent(customMetricsList); MetricsVO metricsVO = new MetricsVO();
metricsVO.setHeartTime(LocalDateTime.now()); metricsVO.setContent(customMetricsList);
metricsVO.setModel(jobId); metricsVO.setHeartTime(LocalDateTime.now());
metricsVO.setDate(TimeUtil.nowStr("yyyy-MM-dd")); metricsVO.setModel(jobId);
MetricsContextHolder.getInstances().sendAsync(metricsVO.getModel(), metricsVO); metricsVO.setDate(TimeUtil.nowStr("yyyy-MM-dd"));
MetricsContextHolder.getInstances().sendAsync(metricsVO.getModel(), metricsVO);
} catch (Exception e) {
log.error("Get and save Flink metrics error", e);
}
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment