fix: 修复规则频繁修改可能会导致内存泄漏的风险.

Signed-off-by: feihu.wang <wfh45678@163.com>
This commit is contained in:
feihu.wang
2019-11-19 14:25:37 +08:00
parent 2e184c00c5
commit 771a7d1e6e
5 changed files with 27 additions and 7 deletions

View File

@@ -17,8 +17,8 @@ The project code called Radar, like the code, monitor the transaction at the bac
* 配置简单,开箱即用!
## 相关站点
Github: https://github.com/wfh45678/radar
Gitee: https://gitee.com/freshday/radar // 码云为镜像网站,贡献代码请提交到 github
Github: https://github.com/wfh45678/radar
官网: http://radar.pgmmer.top
Wiki: https://gitee.com/freshday/radar/wikis/home
@@ -56,7 +56,7 @@ The project code called Radar, like the code, monitor the transaction at the bac
* Mysql 本项目中关系数据库,主要用于存放 风险模型的元信息。
* MongoDB 用于存放事件JSON 提供基本统计学计算例如max, min, sum, avg,
* MongoDB 用于存放事件JSON 提供基本统计学计算例如max, min, sum, avg,
复杂的统计学概念sd,variance, etc...)在内存中计算。
* Redis 提供缓存支持Engine 利用发布订阅特性监听管理端相关配置的更新
@@ -104,7 +104,7 @@ https://gitee.com/freshday/radar/wikis/manual?sort_id=1637446
建议大家自行注册用户,避免使用同样的测试账号受干扰.
## 未完待续
[Release Note:](https://gitee.com/freshday/radar/wikis/release%20note?sort_id=1723765) https://gitee.com/freshday/radar/wikis/release%20note?sort_id=1723765
### 重大特性
* 支持机器学习
* 数据分析平台

View File

@@ -47,8 +47,6 @@ public class RuleApiController {
@PutMapping
public CommonResult save(@RequestBody RuleVO rule, HttpServletRequest request) {
// HttpSession session = request.getSession();
// UserVO user = (UserVO) session.getAttribute("user");
return ruleService.save(rule, contextHolder.getContext().getUsername());
}

View File

@@ -3,7 +3,6 @@ package com.pgmmers.radar.util;
import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyObject;
import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
import java.util.HashMap;
import java.util.Map;
@@ -58,4 +57,11 @@ public class GroovyScriptUtil {
return null;
}
}
/**
* 删除不在使用的脚本关联的groovy object, 不然内存有溢出风险。
*/
public static void removeInactiveScript(String script){
passedClassMap.remove(script.hashCode() + "");
}
}

View File

@@ -9,6 +9,7 @@ import com.pgmmers.radar.service.cache.CacheService;
import com.pgmmers.radar.service.cache.SubscribeHandle;
import com.pgmmers.radar.service.common.CommonResult;
import com.pgmmers.radar.service.model.AbstractionService;
import com.pgmmers.radar.util.GroovyScriptUtil;
import com.pgmmers.radar.vo.model.AbstractionVO;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
@@ -79,10 +80,17 @@ public class AbstractionServiceImpl implements AbstractionService, SubscribeHand
@Override
public CommonResult save(AbstractionVO abstraction) {
CommonResult result = new CommonResult();
if (abstraction.getId() != null) {
AbstractionVO oldAbs = abstractionDal.get(abstraction.getId());
// 如果规则有更新以前的编译好的groovy 对象用不到了,应该删除。
if (!oldAbs.getRuleScript().equals(abstraction.getRuleScript())) {
GroovyScriptUtil.removeInactiveScript(oldAbs.getRuleScript());
}
}
int count = abstractionDal.save(abstraction);
if (count > 0) {
if(StringUtils.isEmpty(abstraction.getName())){
abstraction.setName("abstraction_"+abstraction.getId());
abstraction.setName("abstraction_" + abstraction.getId());
abstractionDal.save(abstraction);
}

View File

@@ -14,6 +14,7 @@ import com.pgmmers.radar.service.cache.SubscribeHandle;
import com.pgmmers.radar.service.common.CommonResult;
import com.pgmmers.radar.service.model.RuleService;
import com.pgmmers.radar.service.search.SearchEngineService;
import com.pgmmers.radar.util.GroovyScriptUtil;
import com.pgmmers.radar.vo.model.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
@@ -89,6 +90,13 @@ public class RuleServiceImpl implements RuleService, SubscribeHandle {
@Override
public CommonResult save(RuleVO rule,String merchantCode) {
CommonResult result = new CommonResult();
if (rule.getId() != null) {
RuleVO oldRule = ruleDal.get(rule.getId());
// 如果规则有更新以前的编译好的groovy 对象用不到了,应该删除。
if (!oldRule.getScripts().equals(rule.getScripts())) {
GroovyScriptUtil.removeInactiveScript(oldRule.getScripts());
}
}
int count = ruleDal.save(rule);
if (count > 0) {
if(StringUtils.isEmpty(rule.getName())){