未验证 提交 2f010258 编写于 作者: F felix.wang 提交者: GitHub

[Fix-3299][dao&server] Fix 3299,when Json string parsing problem caused by...

[Fix-3299][dao&server] Fix 3299,when Json string parsing problem caused by non-standard json format. (#3552)

* #3299  Json string parsing problem caused by non-standard json format.

* #3299  Json string parsing problem caused by non-standard json format.

* #3299  Json string parsing problem caused by non-standard json format. fix  code style

* #3299  Json string parsing problem caused by non-standard json format. fix  code style
Co-authored-by: Nwangjianda <Felix@thinkingdata.com>
上级 d30bc7bc
...@@ -14,15 +14,14 @@ ...@@ -14,15 +14,14 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.apache.dolphinscheduler.dao;
package org.apache.dolphinscheduler.dao;
import org.apache.dolphinscheduler.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.apache.dolphinscheduler.common.enums.AlertStatus; import org.apache.dolphinscheduler.common.enums.AlertStatus;
import org.apache.dolphinscheduler.common.enums.AlertType; import org.apache.dolphinscheduler.common.enums.AlertType;
import org.apache.dolphinscheduler.common.enums.ShowType; import org.apache.dolphinscheduler.common.enums.ShowType;
import org.apache.dolphinscheduler.common.utils.JSONUtils;
import org.apache.dolphinscheduler.common.utils.StringUtils;
import org.apache.dolphinscheduler.dao.datasource.ConnectionFactory; import org.apache.dolphinscheduler.dao.datasource.ConnectionFactory;
import org.apache.dolphinscheduler.dao.entity.Alert; import org.apache.dolphinscheduler.dao.entity.Alert;
import org.apache.dolphinscheduler.dao.entity.ProcessDefinition; import org.apache.dolphinscheduler.dao.entity.ProcessDefinition;
...@@ -30,13 +29,17 @@ import org.apache.dolphinscheduler.dao.entity.ProcessInstance; ...@@ -30,13 +29,17 @@ import org.apache.dolphinscheduler.dao.entity.ProcessInstance;
import org.apache.dolphinscheduler.dao.entity.User; import org.apache.dolphinscheduler.dao.entity.User;
import org.apache.dolphinscheduler.dao.mapper.AlertMapper; import org.apache.dolphinscheduler.dao.mapper.AlertMapper;
import org.apache.dolphinscheduler.dao.mapper.UserAlertGroupMapper; import org.apache.dolphinscheduler.dao.mapper.UserAlertGroupMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component @Component
public class AlertDao extends AbstractBaseDao { public class AlertDao extends AbstractBaseDao {
...@@ -56,21 +59,23 @@ public class AlertDao extends AbstractBaseDao { ...@@ -56,21 +59,23 @@ public class AlertDao extends AbstractBaseDao {
/** /**
* insert alert * insert alert
*
* @param alert alert * @param alert alert
* @return add alert result * @return add alert result
*/ */
public int addAlert(Alert alert){ public int addAlert(Alert alert) {
return alertMapper.insert(alert); return alertMapper.insert(alert);
} }
/** /**
* update alert * update alert
*
* @param alertStatus alertStatus * @param alertStatus alertStatus
* @param log log * @param log log
* @param id id * @param id id
* @return update alert result * @return update alert result
*/ */
public int updateAlert(AlertStatus alertStatus,String log,int id){ public int updateAlert(AlertStatus alertStatus, String log, int id) {
Alert alert = alertMapper.selectById(id); Alert alert = alertMapper.selectById(id);
alert.setAlertStatus(alertStatus); alert.setAlertStatus(alertStatus);
alert.setUpdateTime(new Date()); alert.setUpdateTime(new Date());
...@@ -80,46 +85,61 @@ public class AlertDao extends AbstractBaseDao { ...@@ -80,46 +85,61 @@ public class AlertDao extends AbstractBaseDao {
/** /**
* query user list by alert group id * query user list by alert group id
*
* @param alerGroupId alerGroupId * @param alerGroupId alerGroupId
* @return user list * @return user list
*/ */
public List<User> queryUserByAlertGroupId(int alerGroupId){ public List<User> queryUserByAlertGroupId(int alerGroupId) {
return userAlertGroupMapper.listUserByAlertgroupId(alerGroupId); return userAlertGroupMapper.listUserByAlertgroupId(alerGroupId);
} }
/** /**
* MasterServer or WorkerServer stoped * MasterServer or WorkerServer stoped
*
* @param alertgroupId alertgroupId * @param alertgroupId alertgroupId
* @param host host * @param host host
* @param serverType serverType * @param serverType serverType
*/ */
public void sendServerStopedAlert(int alertgroupId,String host,String serverType){ public void sendServerStopedAlert(int alertgroupId, String host, String serverType) {
Alert alert = new Alert(); Alert alert = new Alert();
String content = String.format("[{'type':'%s','host':'%s','event':'server down','warning level':'serious'}]", List<LinkedHashMap> serverStopList = new ArrayList<>(1);
serverType, host); LinkedHashMap<String, String> serverStopedMap = new LinkedHashMap();
serverStopedMap.put("type", serverType);
serverStopedMap.put("host", host);
serverStopedMap.put("event", "server down");
serverStopedMap.put("warning level", "serious");
serverStopList.add(serverStopedMap);
String content = JSONUtils.toJsonString(serverStopList);
alert.setTitle("Fault tolerance warning"); alert.setTitle("Fault tolerance warning");
saveTaskTimeoutAlert(alert, content, alertgroupId, null, null); saveTaskTimeoutAlert(alert, content, alertgroupId, null, null);
} }
/** /**
* process time out alert * process time out alert
*
* @param processInstance processInstance * @param processInstance processInstance
* @param processDefinition processDefinition * @param processDefinition processDefinition
*/ */
public void sendProcessTimeoutAlert(ProcessInstance processInstance, ProcessDefinition processDefinition){ public void sendProcessTimeoutAlert(ProcessInstance processInstance, ProcessDefinition processDefinition) {
int alertgroupId = processInstance.getWarningGroupId(); int alertgroupId = processInstance.getWarningGroupId();
String receivers = processDefinition.getReceivers(); String receivers = processDefinition.getReceivers();
String receiversCc = processDefinition.getReceiversCc(); String receiversCc = processDefinition.getReceiversCc();
Alert alert = new Alert(); Alert alert = new Alert();
String content = String.format("[{'id':'%d','name':'%s','event':'timeout','warnLevel':'middle'}]", List<LinkedHashMap> processTimeoutList = new ArrayList<>(1);
processInstance.getId(), processInstance.getName()); LinkedHashMap<String, String> processTimeoutMap = new LinkedHashMap();
processTimeoutMap.put("id", String.valueOf(processInstance.getId()));
processTimeoutMap.put("name", processInstance.getName());
processTimeoutMap.put("event", "timeout");
processTimeoutMap.put("warnLevel", "middle");
processTimeoutList.add(processTimeoutMap);
String content = JSONUtils.toJsonString(processTimeoutList);
alert.setTitle("Process Timeout Warn"); alert.setTitle("Process Timeout Warn");
saveTaskTimeoutAlert(alert, content, alertgroupId, receivers, receiversCc); saveTaskTimeoutAlert(alert, content, alertgroupId, receivers, receiversCc);
} }
private void saveTaskTimeoutAlert(Alert alert, String content, int alertgroupId, private void saveTaskTimeoutAlert(Alert alert, String content, int alertgroupId,
String receivers, String receiversCc){ String receivers, String receiversCc) {
alert.setShowType(ShowType.TABLE); alert.setShowType(ShowType.TABLE);
alert.setContent(content); alert.setContent(content);
alert.setAlertType(AlertType.EMAIL); alert.setAlertType(AlertType.EMAIL);
...@@ -135,9 +155,9 @@ public class AlertDao extends AbstractBaseDao { ...@@ -135,9 +155,9 @@ public class AlertDao extends AbstractBaseDao {
alertMapper.insert(alert); alertMapper.insert(alert);
} }
/** /**
* task timeout warn * task timeout warn
*
* @param alertgroupId alertgroupId * @param alertgroupId alertgroupId
* @param receivers receivers * @param receivers receivers
* @param receiversCc receiversCc * @param receiversCc receiversCc
...@@ -146,34 +166,45 @@ public class AlertDao extends AbstractBaseDao { ...@@ -146,34 +166,45 @@ public class AlertDao extends AbstractBaseDao {
* @param taskId taskId * @param taskId taskId
* @param taskName taskName * @param taskName taskName
*/ */
public void sendTaskTimeoutAlert(int alertgroupId,String receivers,String receiversCc, int processInstanceId, public void sendTaskTimeoutAlert(int alertgroupId, String receivers, String receiversCc, int processInstanceId,
String processInstanceName, int taskId,String taskName){ String processInstanceName, int taskId, String taskName) {
Alert alert = new Alert(); Alert alert = new Alert();
String content = String.format("[{'process instance id':'%d','task name':'%s','task id':'%d','task name':'%s'," + List<LinkedHashMap> taskTimeoutList = new ArrayList<>(1);
"'event':'timeout','warnLevel':'middle'}]", processInstanceId, processInstanceName, taskId, taskName); LinkedHashMap<String, String> taskTimeoutMap = new LinkedHashMap();
taskTimeoutMap.put("process instance id", String.valueOf(processInstanceId));
taskTimeoutMap.put("process name", processInstanceName);
taskTimeoutMap.put("task id", String.valueOf(taskId));
taskTimeoutMap.put("task name", taskName);
taskTimeoutMap.put("event", "timeout");
taskTimeoutMap.put("warnLevel", "middle");
taskTimeoutList.add(taskTimeoutMap);
String content = JSONUtils.toJsonString(taskTimeoutList);
alert.setTitle("Task Timeout Warn"); alert.setTitle("Task Timeout Warn");
saveTaskTimeoutAlert(alert, content, alertgroupId, receivers, receiversCc); saveTaskTimeoutAlert(alert, content, alertgroupId, receivers, receiversCc);
} }
/** /**
* list the alert information of waiting to be executed * list the alert information of waiting to be executed
*
* @return alert list * @return alert list
*/ */
public List<Alert> listWaitExecutionAlert(){ public List<Alert> listWaitExecutionAlert() {
return alertMapper.listAlertByStatus(AlertStatus.WAIT_EXECUTION); return alertMapper.listAlertByStatus(AlertStatus.WAIT_EXECUTION);
} }
/** /**
* list user information by alert group id * list user information by alert group id
*
* @param alertgroupId alertgroupId * @param alertgroupId alertgroupId
* @return user list * @return user list
*/ */
public List<User> listUserByAlertgroupId(int alertgroupId){ public List<User> listUserByAlertgroupId(int alertgroupId) {
return userAlertGroupMapper.listUserByAlertgroupId(alertgroupId); return userAlertGroupMapper.listUserByAlertgroupId(alertgroupId);
} }
/** /**
* for test * for test
*
* @return AlertMapper * @return AlertMapper
*/ */
public AlertMapper getAlertMapper() { public AlertMapper getAlertMapper() {
......
...@@ -14,29 +14,30 @@ ...@@ -14,29 +14,30 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.apache.dolphinscheduler.server.utils;
package org.apache.dolphinscheduler.server.utils;
import org.apache.dolphinscheduler.common.enums.AlertType; import org.apache.dolphinscheduler.common.enums.AlertType;
import org.apache.dolphinscheduler.common.enums.CommandType; import org.apache.dolphinscheduler.common.enums.CommandType;
import org.apache.dolphinscheduler.common.enums.ShowType; import org.apache.dolphinscheduler.common.enums.ShowType;
import org.apache.dolphinscheduler.common.enums.WarningType; import org.apache.dolphinscheduler.common.enums.WarningType;
import org.apache.dolphinscheduler.common.utils.DateUtils; import org.apache.dolphinscheduler.common.utils.DateUtils;
import org.apache.dolphinscheduler.common.utils.*; import org.apache.dolphinscheduler.common.utils.JSONUtils;
import org.apache.dolphinscheduler.dao.AlertDao; import org.apache.dolphinscheduler.dao.AlertDao;
import org.apache.dolphinscheduler.dao.DaoFactory; import org.apache.dolphinscheduler.dao.DaoFactory;
import org.apache.dolphinscheduler.dao.entity.Alert; import org.apache.dolphinscheduler.dao.entity.Alert;
import org.apache.dolphinscheduler.dao.entity.ProcessDefinition; import org.apache.dolphinscheduler.dao.entity.ProcessDefinition;
import org.apache.dolphinscheduler.dao.entity.ProcessInstance; import org.apache.dolphinscheduler.dao.entity.ProcessInstance;
import org.apache.dolphinscheduler.dao.entity.TaskInstance; import org.apache.dolphinscheduler.dao.entity.TaskInstance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
* alert manager * alert manager
*/ */
...@@ -50,8 +51,7 @@ public class AlertManager { ...@@ -50,8 +51,7 @@ public class AlertManager {
/** /**
* alert dao * alert dao
*/ */
private AlertDao alertDao = DaoFactory.getDaoInstance(AlertDao.class); private final AlertDao alertDao = DaoFactory.getDaoInstance(AlertDao.class);
/** /**
* command type convert chinese * command type convert chinese
...@@ -86,50 +86,37 @@ public class AlertManager { ...@@ -86,50 +86,37 @@ public class AlertManager {
} }
} }
/**
* process instance format
*/
private static final String PROCESS_INSTANCE_FORMAT =
"\"id:%d\"," +
"\"name:%s\"," +
"\"job type: %s\"," +
"\"state: %s\"," +
"\"recovery:%s\"," +
"\"run time: %d\"," +
"\"start time: %s\"," +
"\"end time: %s\"," +
"\"host: %s\"" ;
/** /**
* get process instance content * get process instance content
* @param processInstance process instance *
* @param taskInstances task instance list * @param processInstance process instance
* @param taskInstances task instance list
* @return process instance format content * @return process instance format content
*/ */
public String getContentProcessInstance(ProcessInstance processInstance, public String getContentProcessInstance(ProcessInstance processInstance,
List<TaskInstance> taskInstances){ List<TaskInstance> taskInstances) {
String res = ""; String res = "";
if(processInstance.getState().typeIsSuccess()){ if (processInstance.getState().typeIsSuccess()) {
res = String.format(PROCESS_INSTANCE_FORMAT, List<LinkedHashMap> successTaskList = new ArrayList<>(1);
processInstance.getId(), LinkedHashMap<String, String> successTaskMap = new LinkedHashMap();
processInstance.getName(), successTaskMap.put("id", String.valueOf(processInstance.getId()));
getCommandCnName(processInstance.getCommandType()), successTaskMap.put("name", processInstance.getName());
processInstance.getState().toString(), successTaskMap.put("job type", getCommandCnName(processInstance.getCommandType()));
processInstance.getRecovery().toString(), successTaskMap.put("state", processInstance.getState().toString());
processInstance.getRunTimes(), successTaskMap.put("recovery", processInstance.getRecovery().toString());
DateUtils.dateToString(processInstance.getStartTime()), successTaskMap.put("run time", String.valueOf(processInstance.getRunTimes()));
DateUtils.dateToString(processInstance.getEndTime()), successTaskMap.put("start time", DateUtils.dateToString(processInstance.getStartTime()));
processInstance.getHost() successTaskMap.put("end time", DateUtils.dateToString(processInstance.getEndTime()));
successTaskMap.put("host", processInstance.getHost());
); successTaskList.add(successTaskMap);
res = "[" + res + "]"; res = JSONUtils.toJsonString(successTaskList);
}else if(processInstance.getState().typeIsFailure()){ } else if (processInstance.getState().typeIsFailure()) {
List<LinkedHashMap> failedTaskList = new ArrayList<>(); List<LinkedHashMap> failedTaskList = new ArrayList<>();
for(TaskInstance task : taskInstances){ for (TaskInstance task : taskInstances) {
if(task.getState().typeIsSuccess()){ if (task.getState().typeIsSuccess()) {
continue; continue;
} }
LinkedHashMap<String, String> failedTaskMap = new LinkedHashMap(); LinkedHashMap<String, String> failedTaskMap = new LinkedHashMap();
...@@ -154,15 +141,15 @@ public class AlertManager { ...@@ -154,15 +141,15 @@ public class AlertManager {
/** /**
* getting worker fault tolerant content * getting worker fault tolerant content
* *
* @param processInstance process instance * @param processInstance process instance
* @param toleranceTaskList tolerance task list * @param toleranceTaskList tolerance task list
* @return worker tolerance content * @return worker tolerance content
*/ */
private String getWorkerToleranceContent(ProcessInstance processInstance, List<TaskInstance> toleranceTaskList){ private String getWorkerToleranceContent(ProcessInstance processInstance, List<TaskInstance> toleranceTaskList) {
List<LinkedHashMap<String, String>> toleranceTaskInstanceList = new ArrayList<>(); List<LinkedHashMap<String, String>> toleranceTaskInstanceList = new ArrayList<>();
for(TaskInstance taskInstance: toleranceTaskList){ for (TaskInstance taskInstance : toleranceTaskList) {
LinkedHashMap<String, String> toleranceWorkerContentMap = new LinkedHashMap(); LinkedHashMap<String, String> toleranceWorkerContentMap = new LinkedHashMap();
toleranceWorkerContentMap.put("process name", processInstance.getName()); toleranceWorkerContentMap.put("process name", processInstance.getName());
toleranceWorkerContentMap.put("task name", taskInstance.getName()); toleranceWorkerContentMap.put("task name", taskInstance.getName());
...@@ -176,11 +163,11 @@ public class AlertManager { ...@@ -176,11 +163,11 @@ public class AlertManager {
/** /**
* send worker alert fault tolerance * send worker alert fault tolerance
* *
* @param processInstance process instance * @param processInstance process instance
* @param toleranceTaskList tolerance task list * @param toleranceTaskList tolerance task list
*/ */
public void sendAlertWorkerToleranceFault(ProcessInstance processInstance, List<TaskInstance> toleranceTaskList){ public void sendAlertWorkerToleranceFault(ProcessInstance processInstance, List<TaskInstance> toleranceTaskList) {
try{ try {
Alert alert = new Alert(); Alert alert = new Alert();
alert.setTitle("worker fault tolerance"); alert.setTitle("worker fault tolerance");
alert.setShowType(ShowType.TABLE); alert.setShowType(ShowType.TABLE);
...@@ -188,13 +175,13 @@ public class AlertManager { ...@@ -188,13 +175,13 @@ public class AlertManager {
alert.setContent(content); alert.setContent(content);
alert.setAlertType(AlertType.EMAIL); alert.setAlertType(AlertType.EMAIL);
alert.setCreateTime(new Date()); alert.setCreateTime(new Date());
alert.setAlertGroupId(processInstance.getWarningGroupId() == null ? 1:processInstance.getWarningGroupId()); alert.setAlertGroupId(processInstance.getWarningGroupId() == null ? 1 : processInstance.getWarningGroupId());
alert.setReceivers(processInstance.getProcessDefinition().getReceivers()); alert.setReceivers(processInstance.getProcessDefinition().getReceivers());
alert.setReceiversCc(processInstance.getProcessDefinition().getReceiversCc()); alert.setReceiversCc(processInstance.getProcessDefinition().getReceiversCc());
alertDao.addAlert(alert); alertDao.addAlert(alert);
logger.info("add alert to db , alert : {}", alert.toString()); logger.info("add alert to db , alert : {}", alert.toString());
}catch (Exception e){ } catch (Exception e) {
logger.error("send alert failed:{} ", e.getMessage()); logger.error("send alert failed:{} ", e.getMessage());
} }
...@@ -202,40 +189,40 @@ public class AlertManager { ...@@ -202,40 +189,40 @@ public class AlertManager {
/** /**
* send process instance alert * send process instance alert
* @param processInstance process instance *
* @param taskInstances task instance list * @param processInstance process instance
* @param taskInstances task instance list
*/ */
public void sendAlertProcessInstance(ProcessInstance processInstance, public void sendAlertProcessInstance(ProcessInstance processInstance,
List<TaskInstance> taskInstances){ List<TaskInstance> taskInstances) {
boolean sendWarnning = false; boolean sendWarnning = false;
WarningType warningType = processInstance.getWarningType(); WarningType warningType = processInstance.getWarningType();
switch (warningType){ switch (warningType) {
case ALL: case ALL:
if(processInstance.getState().typeIsFinished()){ if (processInstance.getState().typeIsFinished()) {
sendWarnning = true; sendWarnning = true;
} }
break; break;
case SUCCESS: case SUCCESS:
if(processInstance.getState().typeIsSuccess()){ if (processInstance.getState().typeIsSuccess()) {
sendWarnning = true; sendWarnning = true;
} }
break; break;
case FAILURE: case FAILURE:
if(processInstance.getState().typeIsFailure()){ if (processInstance.getState().typeIsFailure()) {
sendWarnning = true; sendWarnning = true;
} }
break; break;
default: default:
} }
if(!sendWarnning){ if (!sendWarnning) {
return; return;
} }
Alert alert = new Alert(); Alert alert = new Alert();
String cmdName = getCommandCnName(processInstance.getCommandType()); String cmdName = getCommandCnName(processInstance.getCommandType());
String success = processInstance.getState().typeIsSuccess() ? "success" :"failed"; String success = processInstance.getState().typeIsSuccess() ? "success" : "failed";
alert.setTitle(cmdName + " " + success); alert.setTitle(cmdName + " " + success);
ShowType showType = processInstance.getState().typeIsSuccess() ? ShowType.TEXT : ShowType.TABLE; ShowType showType = processInstance.getState().typeIsSuccess() ? ShowType.TEXT : ShowType.TABLE;
alert.setShowType(showType); alert.setShowType(showType);
...@@ -254,7 +241,7 @@ public class AlertManager { ...@@ -254,7 +241,7 @@ public class AlertManager {
/** /**
* send process timeout alert * send process timeout alert
* *
* @param processInstance process instance * @param processInstance process instance
* @param processDefinition process definition * @param processDefinition process definition
*/ */
public void sendProcessTimeoutAlert(ProcessInstance processInstance, ProcessDefinition processDefinition) { public void sendProcessTimeoutAlert(ProcessInstance processInstance, ProcessDefinition processDefinition) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册