From 81e2b82788d635c4085fad9723ad427b0f65e689 Mon Sep 17 00:00:00 2001 From: CalvinKirs Date: Fri, 29 Jan 2021 10:39:28 +0800 Subject: [PATCH] Store plug-in information when loading plug-in, query database when removing alert --- .../alert/plugin/AlertPluginManager.java | 9 ++++++++- .../alert/runner/AlertSender.java | 2 +- .../apache/dolphinscheduler/dao/PluginDao.java | 18 +++++++++--------- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/plugin/AlertPluginManager.java b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/plugin/AlertPluginManager.java index a660087c89..7b26a821c3 100644 --- a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/plugin/AlertPluginManager.java +++ b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/plugin/AlertPluginManager.java @@ -31,6 +31,7 @@ import org.apache.dolphinscheduler.spi.classloader.ThreadContextClassLoader; import org.apache.dolphinscheduler.spi.params.PluginParamsTransfer; import org.apache.dolphinscheduler.spi.params.base.PluginParams; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -47,6 +48,11 @@ public class AlertPluginManager extends AbstractDolphinPluginManager { private final Map alertChannelFactoryMap = new ConcurrentHashMap<>(); private final Map alertChannelMap = new ConcurrentHashMap<>(); + /** + * k->pluginDefineId v->pluginDefineName + */ + public static Map pluginDefineMap = new HashMap<>(); + public void addAlertChannelFactory(AlertChannelFactory alertChannelFactory) { requireNonNull(alertChannelFactory, "alertChannelFactory is null"); @@ -93,7 +99,8 @@ public class AlertPluginManager extends AbstractDolphinPluginManager { String paramsJson = PluginParamsTransfer.transferParamsToJson(params); PluginDefine pluginDefine = new PluginDefine(nameEn, PluginType.ALERT.getDesc(), paramsJson); - pluginDao.addOrUpdatePluginDefine(pluginDefine); + int id = pluginDao.addOrUpdatePluginDefine(pluginDefine); + pluginDefineMap.put(id, pluginDefine.getPluginName()); } } } diff --git a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/runner/AlertSender.java b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/runner/AlertSender.java index f0c96bb518..7042a0dd37 100644 --- a/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/runner/AlertSender.java +++ b/dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/runner/AlertSender.java @@ -144,7 +144,7 @@ public class AlertSender { * @return AlertResult */ private AlertResult alertResultHandler(AlertPluginInstance instance, AlertData alertData) { - String pluginName = pluginDao.getPluginDefineById(instance.getPluginDefineId()).getPluginName(); + String pluginName = AlertPluginManager.pluginDefineMap.get(instance.getPluginDefineId()); AlertChannel alertChannel = alertPluginManager.getAlertChannelMap().get(pluginName); AlertResult alertResultExtend = new AlertResult(); String pluginInstanceName = instance.getInstanceName(); diff --git a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/PluginDao.java b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/PluginDao.java index ab82997bc2..93df36aadc 100644 --- a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/PluginDao.java +++ b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/PluginDao.java @@ -58,22 +58,22 @@ public class PluginDao extends AbstractBaseDao { * * @param pluginDefine new pluginDefine */ - public void addOrUpdatePluginDefine(PluginDefine pluginDefine) { + public int addOrUpdatePluginDefine(PluginDefine pluginDefine) { requireNonNull(pluginDefine, "pluginDefine is null"); requireNonNull(pluginDefine.getPluginName(), "pluginName is null"); requireNonNull(pluginDefine.getPluginType(), "pluginType is null"); List pluginDefineList = pluginDefineMapper.queryByNameAndType(pluginDefine.getPluginName(), pluginDefine.getPluginType()); if (pluginDefineList == null || pluginDefineList.size() == 0) { - pluginDefineMapper.insert(pluginDefine); - } else { - PluginDefine currPluginDefine = pluginDefineList.get(0); - if (!currPluginDefine.getPluginParams().equals(pluginDefine.getPluginParams())) { - currPluginDefine.setUpdateTime(pluginDefine.getUpdateTime()); - currPluginDefine.setPluginParams(pluginDefine.getPluginParams()); - pluginDefineMapper.updateById(currPluginDefine); - } + return pluginDefineMapper.insert(pluginDefine); } + PluginDefine currPluginDefine = pluginDefineList.get(0); + if (!currPluginDefine.getPluginParams().equals(pluginDefine.getPluginParams())) { + currPluginDefine.setUpdateTime(pluginDefine.getUpdateTime()); + currPluginDefine.setPluginParams(pluginDefine.getPluginParams()); + pluginDefineMapper.updateById(currPluginDefine); + } + return currPluginDefine.getId(); } /**