1:合并maat类配置删除接口

2:修改首页流量统计实体类中pps与gbps的数据类型,并修改从clickhouse中查询的sql
This commit is contained in:
renkaige
2018-12-24 22:18:12 +06:00
parent c50d92d265
commit b4fcccddaf
11 changed files with 458 additions and 608 deletions

View File

@@ -1011,11 +1011,11 @@ public class ConfigJedisServiceimpl implements ConfigRedisService {
list.add(i);
}
serviceConfigMap.put(129, list);
delMaatConfig(serviceConfigMap);
delMaatConfig(serviceConfigMap,false);
}
@Override
public boolean delMaatConfig(Map<Integer, List<Long>> serviceConfigMap) {
public boolean delMaatConfig(Map<Integer, List<Long>> serviceConfigMap,boolean isStart) {
if (serviceConfigMap != null && serviceConfigMap.size() > 0) {
Jedis resource = JedisUtils.getResource(0);
Transaction transaction = resource.multi();
@@ -1036,82 +1036,7 @@ public class ConfigJedisServiceimpl implements ConfigRedisService {
// 按序号选择Redis数据库
transaction.select(redisDb);
removeConfig(id, maatXmlConfig, maatVersion, service, transaction, redisDb,
idRelaRedisDBIndex);
}
} else {
throw new ServiceRuntimeException("删除配置时,未发现对应的配置id信息,请检查配置参数是否正确",
RestBusinessCode.ConfigSourceIsNull.getValue());
}
transaction.incrBy("MAAT_VERSION", 1l);
logger.info("向{}号redis数据库更新了MAAT_VERSION,更新后版本是{}", redisDb,
Integer.valueOf(maatVersionStr) + 1);
} else {
throw new ServiceRuntimeException("" + redisDb
+ "号redis库中获取MAAT_VERSION的值为null,redis中不存在该值,请联系开发人员检查删除逻辑是否正确或redis数据是否出现了异常",
RestBusinessCode.GetMaatVersionFailure.getValue());
}
}
}
} else {
throw new ServiceRuntimeException("删除maat类配置时,未发现对应的配置信息,请检查配置参数是否正确",
RestBusinessCode.ConfigSourceIsNull.getValue());
}
if (removeMaatRelation(serviceConfigMap, transaction, null)
&& removeStatisticsReal(serviceConfigMap, transaction, null)) {
transaction.exec();
return true;
} else {
transaction.discard();
}
} catch (JedisConnectionException e) {
String error = "连接redis异常,删除maat配置失败," + e.getMessage();
logger.error(error + " " + ExceptionUtil.getExceptionMsg(e));
throw new ServiceRuntimeException(error, RestBusinessCode.CannotConnectionRedis.getValue());
} catch (Exception e) {
int businessCode = RestBusinessCode.service_runtime_error.getValue();
if (e instanceof ServiceRuntimeException) {
businessCode = ((ServiceRuntimeException) e).getErrorCode();
}
logger.error("删除maat配置发生了异常" + ExceptionUtil.getExceptionMsg(e));
transaction.discard();
throw new ServiceRuntimeException("删除maat配置发生了异常," + e.getMessage(), businessCode);
} finally {
// 释放连接到连接池
JedisUtils.returnResource(resource);
}
} else
{
throw new ServiceRuntimeException("Map参数信息不能为空,请检查!", RestBusinessCode.ConfigInfoMapIsNull.getValue());
}
return false;
}
public boolean startStopMaatConfig(Map<Integer, List<Long>> serviceConfigMap, boolean isStart) {
if (serviceConfigMap != null && serviceConfigMap.size() > 0) {
Jedis resource = JedisUtils.getResource(0);
Transaction transaction = resource.multi();
try {
int idRelaRedisDBIndex = Configurations.getIntProperty("idRelaRedisDBIndex", 15);
if (serviceConfigMap != null && serviceConfigMap.size() > 0) {
for (Integer service : serviceConfigMap.keySet()) {
Map<Integer, Set<Long>> compileIdAndDBReal = getCompileIdAndDBReal(
serviceConfigMap.get(service), service, idRelaRedisDBIndex);
for (Integer redisDb : compileIdAndDBReal.keySet()) {
String maatVersionStr = JedisUtils.get("MAAT_VERSION", redisDb);
if (maatVersionStr != null) {
Double maatVersion = Double.valueOf(maatVersionStr) + 1D;
MaatXmlConfig maatXmlConfig = ReadMaatXmlUtil.getMaatConfigByService(service);
Set<Long> list = compileIdAndDBReal.get(redisDb);
if (list != null && list.size() > 0) {
for (Long id : list) {
// 按序号选择Redis数据库
transaction.select(redisDb);
startStopConfig(id, maatXmlConfig, maatVersion, service, transaction, redisDb,
idRelaRedisDBIndex, isStart);
idRelaRedisDBIndex,isStart);
}
} else {
throw new ServiceRuntimeException("删除配置时,未发现对应的配置id信息,请检查配置参数是否正确",
@@ -1165,6 +1090,8 @@ public class ConfigJedisServiceimpl implements ConfigRedisService {
return false;
}
/**
* 获取关联关系中每个关系中的-前部分
*
@@ -1188,120 +1115,9 @@ public class ConfigJedisServiceimpl implements ConfigRedisService {
* @param service 业务类型
* @param redisTemplate
* @param maatRelation id对应关系对象
* @param isStart 是否将配置置为生效
*/
private void removeConfig(Long id, MaatXmlConfig maatXmlConfig, Double maatVersion, int service,
Transaction transaction, int redisDBIndex, int idRelaRedisDBIndex) {
if (maatXmlConfig != null) {
Map<String, String> keyMap = new HashMap<>();
// 删除(重命名)编译配置
removeCompileAndGroupConfig(maatXmlConfig, id + "", 10, maatVersion, service, transaction, redisDBIndex,
null, null);// 10代表是编译配置
// 拼接编译与分组关系的Redis Key
String compileStr = "COMPILEGROUP:" + id;
// 获取当前编译配置与分组配置的关联关系
String compileStrVal = JedisUtils.get(compileStr, idRelaRedisDBIndex);// 根据编译id获取该编译下的分组关系
if (compileStrVal != null && !compileStrVal.trim().equals("")) {
String[] compileGroupStrArr = org.apache.commons.lang.StringUtils.split(compileStrVal, ";");
for (String compileGroup : compileGroupStrArr) {
if (compileGroup != null && !compileGroup.trim().equals("")) {
String groupCompileStrs = getRegionInfo(compileGroup);// 获取编译对应的分组信息,去除后面的redisdb信息
if (groupCompileStrs != null && !groupCompileStrs.trim().equals("")) {// 遍历编译和分组的信息
String[] split = org.apache.commons.lang.StringUtils.split(groupCompileStrs, ";");
for (String groupId : split) {// GROUPCOMPILE:groupid-redisdb
if (groupId != null && !groupId.trim().equals("")) {
String groupCompileAndDBStrs = null;
if (!keyMap.containsKey(groupId)) {
groupCompileAndDBStrs = JedisUtils.get(groupId, idRelaRedisDBIndex);
keyMap.put(groupId, groupCompileAndDBStrs);
} else {
groupCompileAndDBStrs = keyMap.get(groupId);
}
String[] compileGroupArr = org.apache.commons.lang.StringUtils
.split(groupCompileAndDBStrs, ";");// 获取组对应的编译id
for (String groupAndCompileStr : compileGroupArr) {
if (groupAndCompileStr != null && !groupAndCompileStr.trim().equals("")) {
String compileId = getRegionInfo(groupAndCompileStr);
if (compileId != null && !compileId.trim().equals("")) {
// 被分组复用的业务,不能将域置为失效
if (!ServiceAndRDBIndexReal.serviceIsReuse(service)) {// 如果当前业务不允许被分组复用(普通的maat类配置),则可以将域置为失效,否则不将域置为失效
// 如果只有一个编译id且与上面的编译id相同则说明未被分组复用,可以将其下的所有域置失效,否则不处理域配置,只把编译,分组关系置为无效
if (compileId.equals(compileStr)) {//
String groupRegionKey = groupId.replace("GROUPCOMPILE",
"GROUPREGION");// groupregion里面value是region的信息,key是group的信息,所以可以直接将GROUPCOMPILE替换为GROUPREGION
String regionStrs = null;
if (!keyMap.containsKey(groupRegionKey)) {
regionStrs = JedisUtils.get(groupRegionKey,
idRelaRedisDBIndex);
keyMap.put(groupRegionKey, regionStrs);
} else {
regionStrs = keyMap.get(groupRegionKey);
}
if (regionStrs != null && !regionStrs.trim().equals("")) {
String[] regionStrArr = regionStrs.split(";");
for (String str : regionStrArr) {
if (str != null && !str.trim().equals("")) {
String regionStr = getRegionInfo(str);
if (regionStr != null
&& !regionStr.trim().equals("")) {
String[] regionKeyArr = regionStr.split(";");
if (regionKeyArr != null
&& regionKeyArr.length > 0) {
// 根据分组与域关联关系找到对应域配置然后删除(重命名)
removeRegionConfig(maatXmlConfig,
regionKeyArr, maatVersion, service,
transaction, redisDBIndex, null);
}
} else {
throw new ServiceRuntimeException(""
+ idRelaRedisDBIndex
+ "号redis库中无法获取MAAT配置分组与域的关联关系key为"
+ groupRegionKey,
RestBusinessCode.KeyNotExistsInRedis
.getValue());
}
}
}
}
}
}
// 根据分组与编译关联关系找到对应分组配置然后删除(重命名)
removeCompileAndGroupConfig(maatXmlConfig,
groupId.replace("GROUPCOMPILE:", ""), 11, maatVersion, service,
transaction, redisDBIndex, id + "", null);// 11代表是分组配置
} else {
throw new ServiceRuntimeException(
"" + idRelaRedisDBIndex + "号redis库中无法获取MAAT配置分组与编译的关联关系key为"
+ groupId,
RestBusinessCode.KeyNotExistsInRedis.getValue());
}
}
}
}
}
} else {
throw new ServiceRuntimeException(
"" + idRelaRedisDBIndex + "号redis库中无法获取MAAT配置编译与分组关联关系key为" + compileStr,
RestBusinessCode.KeyNotExistsInRedis.getValue());
}
}
}
}
} else
{
throw new ServiceRuntimeException(
"无法从applicationConfig-maatRedis.xml配置文件中获取service为" + service + "对应的规则,请检查业务类型是否正确",
RestBusinessCode.NotFoundRedisRule.getValue());
}
}
private void startStopConfig(Long id, MaatXmlConfig maatXmlConfig, Double maatVersion, int service,
Transaction transaction, int redisDBIndex, int idRelaRedisDBIndex, boolean isStart) {
if (maatXmlConfig != null) {
Map<String, String> keyMap = new HashMap<>();
@@ -1457,9 +1273,9 @@ public class ConfigJedisServiceimpl implements ConfigRedisService {
}
if (isStart == null) {// 置为失效
maatKey = oldKey.replace("EFFECTIVE_RULE", "OBSOLETE_RULE");
} else if (isStart == true) {// 将失效置为生效
} else if (isStart) {// 将失效置为生效
maatKey = oldKey;
oldKey=oldKey.replace("EFFECTIVE_RULE", "OBSOLETE_RULE");
oldKey = oldKey.replace("EFFECTIVE_RULE", "OBSOLETE_RULE");
} else {// 将生效变为失效
maatKey = oldKey.replace("EFFECTIVE_RULE", "OBSOLETE_RULE");
}
@@ -1505,7 +1321,7 @@ public class ConfigJedisServiceimpl implements ConfigRedisService {
if (oldKey != null && !oldKey.trim().equals("")) {
if (isStart == null) {// 置为失效
maatKey = oldKey.replace("EFFECTIVE_RULE", "OBSOLETE_RULE");
} else if (isStart == true) {// 将失效置为生效
} else if (isStart) {// 将失效置为生效
maatKey = oldKey.replace("OBSOLETE_RULE", "EFFECTIVE_RULE");
} else {// 将生效变为失效
maatKey = oldKey.replace("EFFECTIVE_RULE", "OBSOLETE_RULE");
@@ -1552,14 +1368,20 @@ public class ConfigJedisServiceimpl implements ConfigRedisService {
if (idList != null && idList.size() > 0) {
for (Long compileId : idList) {
String effectiveRuleKey = "EFFECTIVE_RULE:" + maatTableName + "," + compileId;
transaction.rename(effectiveRuleKey.toUpperCase(),
effectiveRuleKey.toUpperCase().replace("EFFECTIVE_RULE", "OBSOLETE_RULE"));
if (isStart) {
transaction.rename(
effectiveRuleKey.toUpperCase().replace("EFFECTIVE_RULE", "OBSOLETE_RULE"),
effectiveRuleKey.toUpperCase());
} else {
transaction.rename(effectiveRuleKey.toUpperCase(),
effectiveRuleKey.toUpperCase().replace("EFFECTIVE_RULE", "OBSOLETE_RULE"));
}
logger.info("向{}号redis数据库修改了一条配置,修改前key是{},修改后key是:{}", redisStatisticsRealDBIndex,
effectiveRuleKey.toUpperCase(),
effectiveRuleKey.toUpperCase().replace("EFFECTIVE_RULE", "OBSOLETE_RULE"));
String zset = effectiveRuleKey.replace("EFFECTIVE_RULE:", "DEL,");
if (isStart == null || isStart == false) {
} else if (isStart == true) {
} else if (isStart) {
zset = effectiveRuleKey.replace("EFFECTIVE_RULE:", "ADD,");
}
transaction.zadd("MAAT_UPDATE_STATUS", maatVersion, zset);
@@ -1640,23 +1462,63 @@ public class ConfigJedisServiceimpl implements ConfigRedisService {
String groupRegion = groupCompile.replace("GROUPCOMPILE",
"GROUPREGION");// groupregion里面value是region的信息,key是group的信息,所以可以直接将GROUPCOMPILE替换为GROUPREGION
String groupRegionVal = JedisUtils.get(groupRegion,
idRelaRedisDBIndex);
try {
// 为了提高效率,不判断了,如果报错直接捕捉异常处理
// 删除分组与域的关联关系
if (isStart == null) {// 删除
transaction.del(groupRegion);
} else if (isStart == true) {// 将失效置为生效
String groupRegionVal = JedisUtils.get(groupRegion,
idRelaRedisDBIndex);
} else if (isStart) {// 将失效置为生效
groupRegionVal = groupRegionVal
.replace("OBSOLETE_RULE", "EFFECTIVE_RULE");
transaction.set(groupRegion, groupRegionVal);
String[] split = org.apache.commons.lang.StringUtils
.split(groupRegionVal, ";");
if (split.length > 1) {
Set<String> set = new HashSet<>();
for (String regionKeyStr : split) {
if (regionKeyStr != null && !regionKeyStr
.trim().equals("")) {
set.add(regionKeyStr);
}
}
StringBuffer sb = new StringBuffer();
for (String regionKeyStr : set) {
sb.append(";");
sb.append(regionKeyStr);
}
transaction.set(groupRegion, sb.toString());// 避免有多个重复的region-key
} else {
transaction.set(groupRegion.toUpperCase(),
groupRegionVal);
}
} else {// 将生效变为失效
String groupRegionVal = JedisUtils.get(groupRegion,
idRelaRedisDBIndex);
groupRegionVal = groupRegionVal
.replace("EFFECTIVE_RULE", "OBSOLETE_RULE");
transaction.set(groupRegion, groupRegionVal);
String[] split = org.apache.commons.lang.StringUtils
.split(groupRegionVal, ";");
if (split.length > 1) {
Set<String> set = new HashSet<>();
for (String regionKeyStr : split) {
if (regionKeyStr != null && !regionKeyStr
.trim().equals("")) {
set.add(regionKeyStr);
}
}
StringBuffer sb = new StringBuffer();
for (String regionKeyStr : set) {
sb.append(";");
sb.append(regionKeyStr);
}
transaction.set(groupRegion, sb.toString());// 避免有多个重复的region-key
} else {
transaction.set(groupRegion.toUpperCase(),
groupRegionVal);
}
}
} catch (Exception e) {
@@ -1676,9 +1538,9 @@ public class ConfigJedisServiceimpl implements ConfigRedisService {
// 删除分组与编译的关联关系
if (groupCompileStrVal != null
&& !groupCompileStrVal.trim().equals("")) {
StringBuffer sb = new StringBuffer();
String[] split = org.apache.commons.lang.StringUtils
.split(groupCompileStrVal, ";");
Set<String> set = new HashSet<>();
for (String groupCompileAndDBStr : split) {
if (groupCompileAndDBStr != null
&& !groupCompileAndDBStr.trim().equals("")) {
@@ -1689,25 +1551,68 @@ public class ConfigJedisServiceimpl implements ConfigRedisService {
try {
// 为了提高效率,不判断了,如果报错直接捕捉异常处理
// 删除当前组所对应的编译
String groupRegionVal = JedisUtils.get(
groupCompile.toUpperCase(),
idRelaRedisDBIndex);
if (isStart == null) {// 删除
transaction.del(groupCompile.toUpperCase());// 删除编译与分组的关联关系
} else if (isStart == true) {// 将失效置为生效
String groupRegionVal = JedisUtils.get(
groupCompile.toUpperCase(),
idRelaRedisDBIndex);
} else if (isStart) {// 将失效置为生效
groupRegionVal = groupRegionVal.replace(
"OBSOLETE_RULE", "EFFECTIVE_RULE");
transaction.set(groupCompile.toUpperCase(),
groupRegionVal);
String[] split2 = org.apache.commons.lang.StringUtils
.split(groupRegionVal, ";");
if (split2.length > 1) {
Set<String> set1 = new HashSet<>();
for (String regionKeyStr : split2) {
if (regionKeyStr != null
&& !regionKeyStr.trim()
.equals("")) {
set1.add(regionKeyStr);
}
}
StringBuffer sb = new StringBuffer();
for (String regionKeyStr : set1) {
sb.append(";");
sb.append(regionKeyStr);
}
transaction.set(
groupCompile.toUpperCase(),
sb.toString());
} else {
transaction.set(
groupCompile.toUpperCase(),
groupRegionVal);
}
} else {// 将生效变为失效
String groupRegionVal = JedisUtils.get(
groupCompile.toUpperCase(),
idRelaRedisDBIndex);
groupRegionVal = groupRegionVal.replace(
"EFFECTIVE_RULE", "OBSOLETE_RULE");
transaction.set(groupCompile.toUpperCase(),
groupRegionVal);
String[] split2 = org.apache.commons.lang.StringUtils
.split(groupRegionVal, ";");
if (split2.length > 1) {
Set<String> set2 = new HashSet<>();
for (String regionKeyStr : split2) {
if (regionKeyStr != null
&& !regionKeyStr.trim()
.equals("")) {
set2.add(regionKeyStr);
}
}
StringBuffer sb = new StringBuffer();
for (String regionKeyStr : set2) {
sb.append(";");
sb.append(regionKeyStr);
}
transaction.set(
groupCompile.toUpperCase(),
sb.toString());
} else {
transaction.set(
groupCompile.toUpperCase(),
groupRegionVal);
}
}
} catch (Exception e) {
throw new ServiceRuntimeException(""
@@ -1718,15 +1623,20 @@ public class ConfigJedisServiceimpl implements ConfigRedisService {
.getValue());
}
} else if (!groupCompileAndDBStr
.contains(compileStr.toUpperCase())) {
sb.append(groupCompileAndDBStr + ";");
// } else if (!groupCompileAndDBStr
// .contains(compileStr.toUpperCase())) {
} else {
set.add(groupCompileAndDBStr);
}
}
}
if (sb.length() > 0) {
transaction.set(groupCompile.toUpperCase(),
sb.substring(0, sb.length() - 1));// 重新设置分组与编译
if (set.size() > 0) {
StringBuffer sb = new StringBuffer();
for (String regionKeyStr : set) {
sb.append(";");
sb.append(regionKeyStr);
}
transaction.set(groupCompile.toUpperCase(), sb.toString());// 重新设置分组与编译
}
} else {
@@ -1750,7 +1660,7 @@ public class ConfigJedisServiceimpl implements ConfigRedisService {
try {
if (isStart == null) {// 删除
transaction.del(compileStr.toUpperCase());// 删除编译与分组的关联关系
} else if (isStart == true) {// 将失效置为生效
} else if (isStart) {// 将失效置为生效
String groupRegionVal = JedisUtils.get(compileStr.toUpperCase(),
idRelaRedisDBIndex);
groupRegionVal = groupRegionVal.replace("OBSOLETE_RULE",