修改删除maat配置时支持删除分组复用配置

This commit is contained in:
RenKaiGe-Office
2018-08-28 19:53:34 +08:00
parent b2c1912f91
commit bce040fe4f
4 changed files with 223 additions and 244 deletions

View File

@@ -1,6 +1,5 @@
package com.nis.util; package com.nis.util;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;

View File

@@ -84,29 +84,29 @@ public class MaatTestController {
FileUtils.addStrToFile(sdf.format(new Date()) + "\t" + "开始删除业务类型" + serviceType + "下的maat配置" + configId + "\n", FileUtils.addStrToFile(sdf.format(new Date()) + "\t" + "开始删除业务类型" + serviceType + "下的maat配置" + configId + "\n",
Configurations.getStringProperty("maatTestLogPath", ""), true); Configurations.getStringProperty("maatTestLogPath", ""), true);
Map<Integer, Map<Integer, List<Long>>> restMap = new HashMap<Integer, Map<Integer, List<Long>>>(); // Map<Integer, Map<Integer, List<Long>>> restMap = new HashMap<Integer, Map<Integer, List<Long>>>();
Iterator<Integer> serviceIterator = compileMap.keySet().iterator(); // Iterator<Integer> serviceIterator = compileMap.keySet().iterator();
while (serviceIterator.hasNext()) { // while (serviceIterator.hasNext()) {
Integer service = Integer.valueOf(serviceIterator.next().toString()); // Integer service = Integer.valueOf(serviceIterator.next().toString());
List<Integer> dbIndexList = ServiceAndRDBIndexReal.getRedisDBByService(service); // List<Integer> dbIndexList = ServiceAndRDBIndexReal.getRedisDBByService(service);
if (!StringUtil.isEmpty(dbIndexList) && dbIndexList.size() > 0) { // if (!StringUtil.isEmpty(dbIndexList) && dbIndexList.size() > 0) {
for (Integer dbIndex : dbIndexList) { // for (Integer dbIndex : dbIndexList) {
if (restMap.containsKey(dbIndex)) { // if (restMap.containsKey(dbIndex)) {
restMap.get(dbIndex).put(service, compileMap.get(service)); // restMap.get(dbIndex).put(service, compileMap.get(service));
} else { // } else {
Map<Integer, List<Long>> map = new HashMap<Integer, List<Long>>(); // Map<Integer, List<Long>> map = new HashMap<Integer, List<Long>>();
map.put(service, compileMap.get(service)); // map.put(service, compileMap.get(service));
restMap.put(dbIndex, map); // restMap.put(dbIndex, map);
} // }
} // }
} else { // } else {
FileUtils.addStrToFile(sdf.format(new Date()) + "\t" + "error:获取业务类型" + service + "对应的redisDb失败\n", // FileUtils.addStrToFile(sdf.format(new Date()) + "\t" + "error:获取业务类型" + service + "对应的redisDb失败\n",
Configurations.getStringProperty("maatTestLogPath", ""), true); // Configurations.getStringProperty("maatTestLogPath", ""), true);
return "error"; // return "error";
} // }
} // }
try { try {
if (!configRedisService.delMaatConfig(restMap)) { if (!configRedisService.delMaatConfig(compileMap)) {
FileUtils.addStrToFile( FileUtils.addStrToFile(
sdf.format(new Date()) + "\t" + "error:删除业务类型" + serviceType + "下的maat配置" + configId + "失败\n", sdf.format(new Date()) + "\t" + "error:删除业务类型" + serviceType + "下的maat配置" + configId + "失败\n",
Configurations.getStringProperty("maatTestLogPath", ""), true); Configurations.getStringProperty("maatTestLogPath", ""), true);

View File

@@ -51,10 +51,10 @@ public class ConfigJedisServiceimpl implements ConfigRedisService {
maatVersionStr = "0"; maatVersionStr = "0";
} }
if (maatVersionStr != null) { if (maatVersionStr != null) {
Long maatVersion = Long.valueOf(maatVersionStr) + 1l; Double maatVersion = Double.valueOf(maatVersionStr) + 1D;
for (Map<String, String> map : listMap) { for (Map<String, String> map : listMap) {
String serviceStr = map.get("service"); String serviceStr = map.get("service");
int service = Integer.parseInt(serviceStr); int service = Integer.parseInt(serviceStr.trim());
MaatXmlConfig maatXmlConfig = ReadMaatXmlUtil.getMaatConfigByService(service); MaatXmlConfig maatXmlConfig = ReadMaatXmlUtil.getMaatConfigByService(service);
if (maatXmlConfig != null) { if (maatXmlConfig != null) {
List<MaatXmlExpr> expressionList = maatXmlConfig.getExpressionList(); List<MaatXmlExpr> expressionList = maatXmlConfig.getExpressionList();
@@ -312,7 +312,8 @@ public class ConfigJedisServiceimpl implements ConfigRedisService {
maatVersionStr = "0"; maatVersionStr = "0";
} }
if (maatVersionStr != null) { if (maatVersionStr != null) {
Long maatVersion = Long.valueOf(maatVersionStr) + 1l; Double maatVersion = Double.valueOf(maatVersionStr) + 1D;
for (MaatConfig maatConfig : list) { for (MaatConfig maatConfig : list) {
int service = maatConfig.getService(); int service = maatConfig.getService();
MaatXmlConfig maatXmlConfig = ReadMaatXmlUtil.getMaatConfigByService(service); MaatXmlConfig maatXmlConfig = ReadMaatXmlUtil.getMaatConfigByService(service);
@@ -484,7 +485,7 @@ public class ConfigJedisServiceimpl implements ConfigRedisService {
maatVersionStr = "0"; maatVersionStr = "0";
} }
if (maatVersionStr != null) { if (maatVersionStr != null) {
Long maatVersion = Long.valueOf(maatVersionStr) + 1l; Double maatVersion = Double.valueOf(maatVersionStr) + 1D;
List<MaatConfig> maatConfigList = configMap.get(redisDBIndex); List<MaatConfig> maatConfigList = configMap.get(redisDBIndex);
if (maatConfigList != null && maatConfigList.size() > 0) { if (maatConfigList != null && maatConfigList.size() > 0) {
for (MaatConfig maatConfig : maatConfigList) { for (MaatConfig maatConfig : maatConfigList) {
@@ -545,13 +546,12 @@ public class ConfigJedisServiceimpl implements ConfigRedisService {
* @param redisTemplate * @param redisTemplate
* @param redisDBIndex * @param redisDBIndex
*/ */
public void setConfig(MaatConfig maatConfig, MaatXmlConfig maatXmlConfig, Long maatVersion, int service, public void setConfig(MaatConfig maatConfig, MaatXmlConfig maatXmlConfig, Double maatVersion, int service,
Transaction transaction, Integer redisDBIndex) { Transaction transaction, Integer redisDBIndex) {
Map<String, String> compileMap = maatConfig.getCompileMap(); Map<String, String> compileMap = maatConfig.getCompileMap();
String compileId = compileMap.get("compile_id"); String compileId = compileMap.get("compile_id");
if (compileMap != null && compileMap.size() > 0) { if (compileMap != null && compileMap.size() > 0) {
setCommonConfig(maatXmlConfig, compileMap, 10, maatVersion.doubleValue(), service, transaction, setCommonConfig(maatXmlConfig, compileMap, 10, maatVersion, service, transaction, redisDBIndex, null);// 10代表是编译配置
redisDBIndex, null);// 10代表是编译配置
} else { } else {
throw new ServiceRuntimeException( throw new ServiceRuntimeException(
"无法从参数中获取" + redisDBIndex + "号redis库,业务类型为:" + service + "的编译配置信息,请检查配置参数是否正确", "无法从参数中获取" + redisDBIndex + "号redis库,业务类型为:" + service + "的编译配置信息,请检查配置参数是否正确",
@@ -561,8 +561,7 @@ public class ConfigJedisServiceimpl implements ConfigRedisService {
List<Map<String, String>> groupMapList = maatConfig.getGroupMapList(); List<Map<String, String>> groupMapList = maatConfig.getGroupMapList();
if (groupMapList != null && groupMapList.size() > 0) { if (groupMapList != null && groupMapList.size() > 0) {
for (Map<String, String> map : groupMapList) { for (Map<String, String> map : groupMapList) {
setCommonConfig(maatXmlConfig, map, 11, maatVersion.doubleValue(), service, transaction, redisDBIndex, setCommonConfig(maatXmlConfig, map, 11, maatVersion, service, transaction, redisDBIndex, compileId);// 11代表是分组配置
compileId);// 11代表是分组配置
} }
} else { } else {
throw new ServiceRuntimeException( throw new ServiceRuntimeException(
@@ -572,7 +571,7 @@ public class ConfigJedisServiceimpl implements ConfigRedisService {
/** /**
* 保存域配置信息 * 保存域配置信息
*/ */
addRegion(maatConfig, maatXmlConfig, maatVersion, service, transaction, redisDBIndex); addRegion(maatConfig, maatXmlConfig, maatVersion, service, transaction, redisDBIndex, false);
} }
/** /**
@@ -749,7 +748,7 @@ public class ConfigJedisServiceimpl implements ConfigRedisService {
RestBusinessCode.GetMaatVersionFailure.getValue()); RestBusinessCode.GetMaatVersionFailure.getValue());
} }
if (maatVersionStr != null) { if (maatVersionStr != null) {
Long maatVersion = Long.valueOf(maatVersionStr) + 1l; Double maatVersion = Double.valueOf(maatVersionStr) + 1D;
for (Integer service : serviceConfigMap.keySet()) { for (Integer service : serviceConfigMap.keySet()) {
List<Long> compileIdList = serviceConfigMap.get(service); List<Long> compileIdList = serviceConfigMap.get(service);
if (compileIdList != null && compileIdList.size() > 0) { if (compileIdList != null && compileIdList.size() > 0) {
@@ -923,73 +922,90 @@ public class ConfigJedisServiceimpl implements ConfigRedisService {
return false; return false;
} }
private Map<Integer, List<Long>> getCompileIdAndDBReal(List<Long> idList, int service, int idRelaRedisDBIndex) {
// 记录所有需要删除的配置信息key是相同的redisdb,value是需要删除的id集合
Map<Integer, List<Long>> idAndDBMap = new HashMap<Integer, List<Long>>();
for (Long configId : idList) {
String compileValStr = JedisUtils.get("COMPILEGROUP:" + configId, idRelaRedisDBIndex);
if (compileValStr != null && !compileValStr.trim().equals("")) {
String[] keyAndDBArr = compileValStr.split(";");
for (String keyAndDB : keyAndDBArr) {
String[] split = keyAndDB.split("-");
if (split != null && split.length > 0) {
String[] dbArr = split[1].split(",");
for (String db : dbArr) {
if (db != null && !db.trim().equals("")) {
int redisDb = Integer.parseInt(db.trim());
if (idAndDBMap.containsKey(redisDb)) {
idAndDBMap.get(redisDb).add(configId);
} else {
List<Long> list = new ArrayList<Long>();
list.add(configId);
idAndDBMap.put(redisDb, list);
}
}
}
}
}
} else {
throw new ServiceRuntimeException(
"未从" + idRelaRedisDBIndex + "号redis库(编译,分组,域关系)中获取配置compileId=" + configId
+ ",对应的redisdb信息,请联系开发人员检查删除逻辑是否正确或redis数据是否出现了异常",
RestBusinessCode.KeyNotExistsInRedis.getValue());
}
}
return idAndDBMap;
}
@Override @Override
public boolean delMaatConfig(Map<Integer, Map<Integer, List<Long>>> idMap) { public boolean delMaatConfig(Map<Integer, List<Long>> serviceConfigMap) {
if (idMap != null && idMap.size() > 0) { if (serviceConfigMap != null && serviceConfigMap.size() > 0) {
Jedis resource = JedisUtils.getResource(0); Jedis resource = JedisUtils.getResource(0);
Transaction transaction = resource.multi(); Transaction transaction = resource.multi();
try { try {
int idRelaRedisDBIndex = Configurations.getIntProperty("idRelaRedisDBIndex", 15); int idRelaRedisDBIndex = Configurations.getIntProperty("idRelaRedisDBIndex", 15);
for (Integer redisDBIndex : idMap.keySet()) {
Map<Integer, List<Long>> serviceConfigMap = idMap.get(redisDBIndex);
if (serviceConfigMap != null && serviceConfigMap.size() > 0) { if (serviceConfigMap != null && serviceConfigMap.size() > 0) {
String maatVersionStr = JedisUtils.get("MAAT_VERSION", redisDBIndex);
if (maatVersionStr != null) {
Long maatVersion = Long.valueOf(maatVersionStr) + 1l;
for (Integer service : serviceConfigMap.keySet()) { for (Integer service : serviceConfigMap.keySet()) {
Map<Integer, List<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); MaatXmlConfig maatXmlConfig = ReadMaatXmlUtil.getMaatConfigByService(service);
List<Long> list = serviceConfigMap.get(service); List<Long> list = compileIdAndDBReal.get(redisDb);
if (list != null && list.size() > 0) { if (list != null && list.size() > 0) {
for (Long id : list) { for (Long id : list) {
String compileStrVal = JedisUtils.get("COMPILEGROUP:" + id, idRelaRedisDBIndex);/// 从编译,分组,域的关系中获取当前编译id对应的的分组关系,及redisdb信息
if (compileStrVal != null && compileStrVal.contains("-")) {
String[] split = compileStrVal.split("-");
String redisDBStr = split[1];// 获取redisdb信息
if (redisDBStr != null && !redisDBStr.equals("")) {
String[] redisDBArr = redisDBStr.split(",");
for (String dbStr : redisDBArr) {
int dbIndex = Integer.parseInt(dbStr.trim());
// 按序号选择Redis数据库 // 按序号选择Redis数据库
transaction.select(dbIndex); transaction.select(redisDb);
removeConfig(id, maatXmlConfig, maatVersion, service, transaction, removeConfig(id, maatXmlConfig, maatVersion, service, transaction, redisDb,
dbIndex, idRelaRedisDBIndex); idRelaRedisDBIndex);
transaction.incrBy("MAAT_VERSION", 1l);
logger.info("向{}号redis数据库更新了MAAT_VERSION,更新后版本是{}", redisDBIndex,
Integer.valueOf(maatVersionStr) + 1);
}
} else {
throw new ServiceRuntimeException(
"未从" + idRelaRedisDBIndex + "号redis库(编译,分组,域关系)中获取配置id=" + id
+ ",对应的redisdb信息,请联系开发人员检查删除逻辑是否正确或redis数据是否出现了异常",
RestBusinessCode.RedisDBRelationNotExistsInRedis.getValue());
}
} else {
throw new ServiceRuntimeException(
"未从" + idRelaRedisDBIndex + "号redis库(编译,分组,域关系)中获取配置id=" + id
+ ",对应的redisdb信息,请联系开发人员检查删除逻辑是否正确或redis数据是否出现了异常",
RestBusinessCode.RedisDBRelationNotExistsInRedis.getValue());
}
} }
} else { } else {
throw new ServiceRuntimeException("删除配置时,未发现对应的配置id信息,请检查配置参数是否正确", throw new ServiceRuntimeException("删除配置时,未发现对应的配置id信息,请检查配置参数是否正确",
RestBusinessCode.ConfigSourceIsNull.getValue()); RestBusinessCode.ConfigSourceIsNull.getValue());
} }
} transaction.incrBy("MAAT_VERSION", 1l);
logger.info("向{}号redis数据库更新了MAAT_VERSION,更新后版本是{}", redisDb,
Integer.valueOf(maatVersionStr) + 1);
} else { } else {
throw new ServiceRuntimeException("" + redisDBIndex throw new ServiceRuntimeException("" + redisDb
+ "号redis库中获取MAAT_VERSION的值为null,redis中不存在该值,请联系开发人员检查删除逻辑是否正确或redis数据是否出现了异常", + "号redis库中获取MAAT_VERSION的值为null,redis中不存在该值,请联系开发人员检查删除逻辑是否正确或redis数据是否出现了异常",
RestBusinessCode.GetMaatVersionFailure.getValue()); RestBusinessCode.GetMaatVersionFailure.getValue());
} }
}
}
} else { } else {
throw new ServiceRuntimeException("" + redisDBIndex + "号redis库删除配置时,未发现对应的配置信息,请检查配置参数是否正确", throw new ServiceRuntimeException("删除maat类配置时,未发现对应的配置信息,请检查配置参数是否正确",
RestBusinessCode.ConfigSourceIsNull.getValue()); RestBusinessCode.ConfigSourceIsNull.getValue());
} }
break; if (removeMaatRelation(serviceConfigMap, transaction)
} && removeStatisticsReal(serviceConfigMap, transaction)) {
if (removeMaatRelation(idMap, transaction) && removeStatisticsReal(idMap, transaction)) {
transaction.exec(); transaction.exec();
return true; return true;
} else { } else {
@@ -1045,12 +1061,12 @@ public class ConfigJedisServiceimpl implements ConfigRedisService {
* @param redisTemplate * @param redisTemplate
* @param maatRelation id对应关系对象 * @param maatRelation id对应关系对象
*/ */
private void removeConfig(Long id, MaatXmlConfig maatXmlConfig, Long maatVersion, int service, private void removeConfig(Long id, MaatXmlConfig maatXmlConfig, Double maatVersion, int service,
Transaction transaction, int redisDBIndex, int idRelaRedisDBIndex) { Transaction transaction, int redisDBIndex, int idRelaRedisDBIndex) {
if (maatXmlConfig != null) { if (maatXmlConfig != null) {
// 删除(重命名)编译配置 // 删除(重命名)编译配置
removeCompileAndGroupConfig(maatXmlConfig, id + "", 10, maatVersion.doubleValue(), service, transaction, removeCompileAndGroupConfig(maatXmlConfig, id + "", 10, maatVersion, service, transaction, redisDBIndex,
redisDBIndex, null);// 10代表是编译配置 null);// 10代表是编译配置
// 拼接编译与分组关系的Redis Key // 拼接编译与分组关系的Redis Key
// String compileStr = redisDBIndex + ":COMPILEGROUP:" + id; // String compileStr = redisDBIndex + ":COMPILEGROUP:" + id;
String compileStr = "COMPILEGROUP:" + id; String compileStr = "COMPILEGROUP:" + id;
@@ -1081,8 +1097,8 @@ public class ConfigJedisServiceimpl implements ConfigRedisService {
String[] regionKeyArr = regionStr.split(";"); String[] regionKeyArr = regionStr.split(";");
if (regionKeyArr != null && regionKeyArr.length > 0) { if (regionKeyArr != null && regionKeyArr.length > 0) {
// 根据分组与域关联关系找到对应域配置然后删除(重命名) // 根据分组与域关联关系找到对应域配置然后删除(重命名)
removeRegionConfig(maatXmlConfig, regionKeyArr, maatVersion.doubleValue(), removeRegionConfig(maatXmlConfig, regionKeyArr, maatVersion, service,
service, transaction, redisDBIndex); transaction, redisDBIndex);
} }
} else { } else {
throw new ServiceRuntimeException( throw new ServiceRuntimeException(
@@ -1095,7 +1111,7 @@ public class ConfigJedisServiceimpl implements ConfigRedisService {
} }
// 根据分组与编译关联关系找到对应分组配置然后删除(重命名) // 根据分组与编译关联关系找到对应分组配置然后删除(重命名)
removeCompileAndGroupConfig(maatXmlConfig, groupId.replace("GROUPCOMPILE:", ""), 11, removeCompileAndGroupConfig(maatXmlConfig, groupId.replace("GROUPCOMPILE:", ""), 11,
maatVersion.doubleValue(), service, transaction, redisDBIndex, id + "");// 11代表是分组配置 maatVersion, service, transaction, redisDBIndex, id + "");// 11代表是分组配置
} else { } else {
throw new ServiceRuntimeException( throw new ServiceRuntimeException(
"" + idRelaRedisDBIndex + "号redis库中无法获取MAAT配置分组与编译的关联关系key为" + groupId, "" + idRelaRedisDBIndex + "号redis库中无法获取MAAT配置分组与编译的关联关系key为" + groupId,
@@ -1275,8 +1291,8 @@ public class ConfigJedisServiceimpl implements ConfigRedisService {
* 编译配置用于实时统计 * 编译配置用于实时统计
* 取消分类性质,标签等信息 * 取消分类性质,标签等信息
*/ */
private boolean removeStatisticsReal(Map<Integer, Map<Integer, List<Long>>> idMap, Transaction transaction) { private boolean removeStatisticsReal(Map<Integer, List<Long>> map, Transaction transaction) {
if (idMap != null && idMap.size() > 0) { if (map != null && map.size() > 0) {
int redisStatisticsRealDBIndex = Configurations.getIntProperty("redisStatisticsRealDBIndex", 14); int redisStatisticsRealDBIndex = Configurations.getIntProperty("redisStatisticsRealDBIndex", 14);
transaction.select(redisStatisticsRealDBIndex); transaction.select(redisStatisticsRealDBIndex);
String maatVersionStr = JedisUtils.get("MAAT_VERSION", redisStatisticsRealDBIndex); String maatVersionStr = JedisUtils.get("MAAT_VERSION", redisStatisticsRealDBIndex);
@@ -1284,12 +1300,9 @@ public class ConfigJedisServiceimpl implements ConfigRedisService {
throw new ServiceRuntimeException("" + redisStatisticsRealDBIndex + "号redis库中获取MAAT_VERSION失败", throw new ServiceRuntimeException("" + redisStatisticsRealDBIndex + "号redis库中获取MAAT_VERSION失败",
RestBusinessCode.KeyNotExistsInRedis.getValue()); RestBusinessCode.KeyNotExistsInRedis.getValue());
} }
double maatVersion = Double.valueOf(maatVersionStr) + 1D; double maatVersion = Double.valueOf(maatVersionStr) + 1d;
for (Integer redisDBIndex : idMap.keySet()) {
if (redisStatisticsRealDBIndex >= 0 if (redisStatisticsRealDBIndex >= 0
&& redisStatisticsRealDBIndex < Configurations.getIntProperty("maxRedisDBIndex", 16)) { && redisStatisticsRealDBIndex < Configurations.getIntProperty("maxRedisDBIndex", 16)) {
Map<Integer, List<Long>> map = idMap.get(redisDBIndex);
if (map != null && map.size() > 0) {
for (Integer service : map.keySet()) { for (Integer service : map.keySet()) {
String maatTableName = ServiceAndRDBIndexReal.getMaatTableName(service, 10, null); String maatTableName = ServiceAndRDBIndexReal.getMaatTableName(service, 10, null);
List<Long> idList = map.get(service); List<Long> idList = map.get(service);
@@ -1297,12 +1310,11 @@ public class ConfigJedisServiceimpl implements ConfigRedisService {
for (Long compileId : idList) { for (Long compileId : idList) {
String effectiveRuleKey = "EFFECTIVE_RULE:" + maatTableName + "," + compileId; String effectiveRuleKey = "EFFECTIVE_RULE:" + maatTableName + "," + compileId;
if (JedisUtils.exists(effectiveRuleKey.toUpperCase(), redisStatisticsRealDBIndex)) { if (JedisUtils.exists(effectiveRuleKey.toUpperCase(), redisStatisticsRealDBIndex)) {
transaction.rename(effectiveRuleKey.toUpperCase(), effectiveRuleKey transaction.rename(effectiveRuleKey.toUpperCase(),
.toUpperCase().replace("EFFECTIVE_RULE", "OBSOLETE_RULE")); effectiveRuleKey.toUpperCase().replace("EFFECTIVE_RULE", "OBSOLETE_RULE"));
logger.info("向{}号redis数据库修改了一条配置,修改前key是{},修改后key是:{}", logger.info("向{}号redis数据库修改了一条配置,修改前key是{},修改后key是:{}", redisStatisticsRealDBIndex,
redisStatisticsRealDBIndex, effectiveRuleKey.toUpperCase(), effectiveRuleKey.toUpperCase(),
effectiveRuleKey.toUpperCase().replace("EFFECTIVE_RULE", effectiveRuleKey.toUpperCase().replace("EFFECTIVE_RULE", "OBSOLETE_RULE"));
"OBSOLETE_RULE"));
String zset = effectiveRuleKey.replace("EFFECTIVE_RULE:", "DEL,"); String zset = effectiveRuleKey.replace("EFFECTIVE_RULE:", "DEL,");
transaction.zadd("MAAT_UPDATE_STATUS", maatVersion, zset); transaction.zadd("MAAT_UPDATE_STATUS", maatVersion, zset);
@@ -1325,19 +1337,12 @@ public class ConfigJedisServiceimpl implements ConfigRedisService {
transaction.incrBy("MAAT_VERSION", 1l); transaction.incrBy("MAAT_VERSION", 1l);
logger.info("向{}号redis数据库更新了MAAT_VERSION,更新后版本是{}", redisStatisticsRealDBIndex, logger.info("向{}号redis数据库更新了MAAT_VERSION,更新后版本是{}", redisStatisticsRealDBIndex,
Integer.valueOf(maatVersionStr) + 1); Integer.valueOf(maatVersionStr) + 1);
return true;
} else {
throw new ServiceRuntimeException("删除redis库中配置时,未发现对应的配置信息,请检查配置参数是否正确",
RestBusinessCode.ConfigSourceIsNull.getValue());
}
} else { } else {
throw new ServiceRuntimeException("redis数据库编号:" + redisStatisticsRealDBIndex + "不正确,请检查数据库编号", throw new ServiceRuntimeException("redis数据库编号:" + redisStatisticsRealDBIndex + "不正确,请检查数据库编号",
RestBusinessCode.DbIndexNotInRange.getValue()); RestBusinessCode.DbIndexNotInRange.getValue());
} }
break;// 因为所有的value都是相同的所以只取消一次就可以了
}
return true;
} else { } else {
throw new ServiceRuntimeException("状态更新操作Map参数信息不能为空,请检查配置参数是否正确", throw new ServiceRuntimeException("状态更新操作Map参数信息不能为空,请检查配置参数是否正确",
RestBusinessCode.ConfigInfoMapIsNull.getValue()); RestBusinessCode.ConfigInfoMapIsNull.getValue());
@@ -1349,14 +1354,10 @@ public class ConfigJedisServiceimpl implements ConfigRedisService {
* 删除配置成功后,需要更新编译,组,域等配置id的对应关系 * 删除配置成功后,需要更新编译,组,域等配置id的对应关系
* @param idMap * @param idMap
*/ */
private boolean removeMaatRelation(Map<Integer, Map<Integer, List<Long>>> idMap, Transaction transaction) { private boolean removeMaatRelation(Map<Integer, List<Long>> map, Transaction transaction) {
if (idMap != null && idMap.size() > 0) { if (map != null && map.size() > 0) {
int idRelaRedisDBIndex = Configurations.getIntProperty("idRelaRedisDBIndex", 15); int idRelaRedisDBIndex = Configurations.getIntProperty("idRelaRedisDBIndex", 15);
transaction.select(idRelaRedisDBIndex); transaction.select(idRelaRedisDBIndex);
for (Integer redisDBIndex : idMap.keySet()) {
if (redisDBIndex >= 0 && redisDBIndex < Configurations.getIntProperty("maxRedisDBIndex", 6)) {
Map<Integer, List<Long>> map = idMap.get(redisDBIndex);// 获取service和配置id的对应关系
if (map != null && map.size() > 0) {
for (Integer service : map.keySet()) { for (Integer service : map.keySet()) {
List<Long> idList = map.get(service); List<Long> idList = map.get(service);
if (idList != null && idList.size() > 0) { if (idList != null && idList.size() > 0) {
@@ -1382,16 +1383,15 @@ public class ConfigJedisServiceimpl implements ConfigRedisService {
// == 1 // == 1
// && compileGroupStr.equals(compileStr.toUpperCase())) {// // && compileGroupStr.equals(compileStr.toUpperCase())) {//
// 当前的分组关系只属于当前的compileid,说明没有被分组复用,需要将编译配置,分组关系,域配置,置无效 // 当前的分组关系只属于当前的compileid,说明没有被分组复用,需要将编译配置,分组关系,域配置,置无效
if (compileGroupStrSplit[0].toUpperCase() if (compileGroupStrSplit[0].toUpperCase().equals(compileStr.toUpperCase())) {
.equals(compileStr.toUpperCase())) { String groupRegion = groupCompile.replace("GROUPCOMPILE", "GROUPREGION");// groupregion里面value是region的信息,key是group的信息,所以可以直接将GROUPCOMPILE替换为GROUPREGION
String groupRegion = groupCompile.replace("GROUPCOMPILE",
"GROUPREGION");// groupregion里面value是region的信息,key是group的信息,所以可以直接将GROUPCOMPILE替换为GROUPREGION
// 删除分组与域的关联关系 // 删除分组与域的关联关系
if (JedisUtils.exists(groupRegion, idRelaRedisDBIndex)) { if (JedisUtils.exists(groupRegion, idRelaRedisDBIndex)) {
transaction.del(groupRegion); transaction.del(groupRegion);
} else { } else {
throw new ServiceRuntimeException("" + idRelaRedisDBIndex throw new ServiceRuntimeException(
+ "号redis库中无法获取MAAT配置分组与域的关联关系key为" + groupRegion, "" + idRelaRedisDBIndex + "号redis库中无法获取MAAT配置分组与域的关联关系key为"
+ groupRegion,
RestBusinessCode.KeyNotExistsInRedis.getValue()); RestBusinessCode.KeyNotExistsInRedis.getValue());
} }
@@ -1402,8 +1402,7 @@ public class ConfigJedisServiceimpl implements ConfigRedisService {
transaction.del(groupCompile.toUpperCase());// 删除当前组所对应的编译 transaction.del(groupCompile.toUpperCase());// 删除当前组所对应的编译
} else { } else {
throw new ServiceRuntimeException( throw new ServiceRuntimeException(
"" + idRelaRedisDBIndex "" + idRelaRedisDBIndex + "号redis库中无法获取MAAT配置分组与编译的关联关系key为"
+ "号redis库中无法获取MAAT配置分组与编译的关联关系key为"
+ groupCompile.toUpperCase(), + groupCompile.toUpperCase(),
RestBusinessCode.KeyNotExistsInRedis.getValue()); RestBusinessCode.KeyNotExistsInRedis.getValue());
} }
@@ -1421,30 +1420,17 @@ public class ConfigJedisServiceimpl implements ConfigRedisService {
} else { } else {
throw new ServiceRuntimeException( throw new ServiceRuntimeException(
"" + idRelaRedisDBIndex + "号redis库中获取" + compileStr "" + idRelaRedisDBIndex + "号redis关联关系库中获取" + compileStr
+ "的值为null,redis中不存在该值,请联系开发人员检查删除逻辑是否正确或redis数据是否出现了异常", + "的值为null,redis中不存在该值,请联系开发人员检查删除逻辑是否正确或redis数据是否出现了异常",
RestBusinessCode.KeyNotExistsInRedis.getValue()); RestBusinessCode.KeyNotExistsInRedis.getValue());
} }
} }
} else { } else {
throw new ServiceRuntimeException( throw new ServiceRuntimeException(
"未从redisdb与配置id的对应关系中找到redisdb=" + redisDBIndex + "service=" + service "删除配置时,未从service与配置id的对应关系中找到service=" + service + "的配置id,请检查传入的参数是否正确",
+ "对应的配置id,请联系开发人员检查redisdb与配置id的对应关系",
RestBusinessCode.ConfigSourceIsNull.getValue()); RestBusinessCode.ConfigSourceIsNull.getValue());
} }
} }
} else {
throw new ServiceRuntimeException(
"未从redisdb与配置id的对应关系中找到redisdb=" + redisDBIndex + "对应的配置id,请联系开发人员检查redisdb与配置id的对应关系",
RestBusinessCode.ConfigSourceIsNull.getValue());
}
} else {
throw new ServiceRuntimeException("redis数据库编号:" + redisDBIndex + "不正确,请检查数据库编号",
RestBusinessCode.DbIndexNotInRange.getValue());
}
break;// map中的每个value都是一样的,而且关联关系里面只存储了一份信息,所以不需要再遍历了
}
return true; return true;
} else { } else {
throw new ServiceRuntimeException("状态更新操作Map参数信息不能为空,请检查配置参数是否正确", throw new ServiceRuntimeException("状态更新操作Map参数信息不能为空,请检查配置参数是否正确",
@@ -1469,12 +1455,12 @@ public class ConfigJedisServiceimpl implements ConfigRedisService {
maatVersionStr = "0"; maatVersionStr = "0";
} }
if (maatVersionStr != null) { if (maatVersionStr != null) {
Long maatVersion = Long.valueOf(maatVersionStr) + 1l; Double maatVersion = Double.valueOf(maatVersionStr) + 1D;
for (MaatConfig maatConfig : list) { for (MaatConfig maatConfig : list) {
Integer service = maatConfig.getService(); Integer service = maatConfig.getService();
if (ServiceAndRDBIndexReal.serviceIsReuse(service)) { if (ServiceAndRDBIndexReal.serviceIsReuse(service)) {
MaatXmlConfig maatXmlConfig = ReadMaatXmlUtil.getMaatConfigByService(service); MaatXmlConfig maatXmlConfig = ReadMaatXmlUtil.getMaatConfigByService(service);
addRegion(maatConfig, maatXmlConfig, maatVersion, service, transaction, redisDb); addRegion(maatConfig, maatXmlConfig, maatVersion, service, transaction, redisDb, true);
/** /**
* 添加组和域的关联关系信息 * 添加组和域的关联关系信息
*/ */
@@ -1498,12 +1484,12 @@ public class ConfigJedisServiceimpl implements ConfigRedisService {
transaction.exec(); transaction.exec();
return true; return true;
} catch (JedisConnectionException e) { } catch (JedisConnectionException e) {
String error = "连接redis异常,保存maat类配置失败" + e.getMessage(); String error = "连接redis异常,保存分组复用maat类配置失败" + e.getMessage();
logger.error(error); logger.error(error);
throw new ServiceRuntimeException(error, RestBusinessCode.CannotConnectionRedis.getValue()); throw new ServiceRuntimeException(error, RestBusinessCode.CannotConnectionRedis.getValue());
} catch (Exception e) { } catch (Exception e) {
transaction.discard(); transaction.discard();
String error = "保存maat类配置发生了异常" + e.getMessage(); String error = "保存分组复用maat类配置发生了异常" + e.getMessage();
logger.error(error); logger.error(error);
throw new ServiceRuntimeException(error, RestBusinessCode.SaveDataInError.getValue()); throw new ServiceRuntimeException(error, RestBusinessCode.SaveDataInError.getValue());
} finally { } finally {
@@ -1563,68 +1549,62 @@ public class ConfigJedisServiceimpl implements ConfigRedisService {
* @param service 业务类型 * @param service 业务类型
* @param transaction redis连接 * @param transaction redis连接
* @param redisDBIndex redis编号 * @param redisDBIndex redis编号
* @param isReuseSaveRegion 是否是分组复用单独添加域配置
*/ */
private void addRegion(MaatConfig maatConfig, MaatXmlConfig maatXmlConfig, Long maatVersion, int service, private void addRegion(MaatConfig maatConfig, MaatXmlConfig maatXmlConfig, Double maatVersion, int service,
Transaction transaction, Integer redisDBIndex) { Transaction transaction, Integer redisDBIndex, boolean isReuseSaveRegion) {
int count = 0;// 计算下所有的域是不是都没有值,如果没有值则给出提示 int count = 0;// 计算下所有的域是不是都没有值,如果没有值则给出提示
List<Map<String, String>> ipRegionMapList = maatConfig.getIpRegionMapList(); List<Map<String, String>> ipRegionMapList = maatConfig.getIpRegionMapList();
if (ipRegionMapList != null && ipRegionMapList.size() > 0) { if (ipRegionMapList != null && ipRegionMapList.size() > 0) {
count += ipRegionMapList.size(); count += ipRegionMapList.size();
for (Map<String, String> map : ipRegionMapList) { for (Map<String, String> map : ipRegionMapList) {
setCommonConfig(maatXmlConfig, map, 12, maatVersion.doubleValue(), service, transaction, redisDBIndex, setCommonConfig(maatXmlConfig, map, 12, maatVersion, service, transaction, redisDBIndex, null);// 12代表是ip类域配置
null);// 12代表是ip类域配置
} }
} }
List<Map<String, String>> numRegionMapList = maatConfig.getNumRegionMapList(); List<Map<String, String>> numRegionMapList = maatConfig.getNumRegionMapList();
if (numRegionMapList != null && numRegionMapList.size() > 0) { if (numRegionMapList != null && numRegionMapList.size() > 0) {
count += numRegionMapList.size(); count += numRegionMapList.size();
for (Map<String, String> map : numRegionMapList) { for (Map<String, String> map : numRegionMapList) {
setCommonConfig(maatXmlConfig, map, 13, maatVersion.doubleValue(), service, transaction, redisDBIndex, setCommonConfig(maatXmlConfig, map, 13, maatVersion, service, transaction, redisDBIndex, null);// 13代表是数值类配置
null);// 13代表是数值类配置
} }
} }
List<Map<String, String>> strRegionMapList = maatConfig.getStrRegionMapList(); List<Map<String, String>> strRegionMapList = maatConfig.getStrRegionMapList();
if (strRegionMapList != null && strRegionMapList.size() > 0) { if (strRegionMapList != null && strRegionMapList.size() > 0) {
count += strRegionMapList.size(); count += strRegionMapList.size();
for (Map<String, String> map : strRegionMapList) { for (Map<String, String> map : strRegionMapList) {
setCommonConfig(maatXmlConfig, map, 14, maatVersion.doubleValue(), service, transaction, redisDBIndex, setCommonConfig(maatXmlConfig, map, 14, maatVersion, service, transaction, redisDBIndex, null);// 14代表是字符串类域配置
null);// 14代表是字符串类域配置
} }
} }
List<Map<String, String>> strStrRegionMapList = maatConfig.getStrStrRegionMapList(); List<Map<String, String>> strStrRegionMapList = maatConfig.getStrStrRegionMapList();
if (strStrRegionMapList != null && strStrRegionMapList.size() > 0) { if (strStrRegionMapList != null && strStrRegionMapList.size() > 0) {
count += strStrRegionMapList.size(); count += strStrRegionMapList.size();
for (Map<String, String> map : strStrRegionMapList) { for (Map<String, String> map : strStrRegionMapList) {
setCommonConfig(maatXmlConfig, map, 15, maatVersion.doubleValue(), service, transaction, redisDBIndex, setCommonConfig(maatXmlConfig, map, 15, maatVersion, service, transaction, redisDBIndex, null);// 15代表是增强字符串类域配置
null);// 15代表是增强字符串类域配置
} }
} }
List<Map<String, String>> fileDigestRegionMapList = maatConfig.getFileDigestRegionMapList(); List<Map<String, String>> fileDigestRegionMapList = maatConfig.getFileDigestRegionMapList();
if (fileDigestRegionMapList != null && fileDigestRegionMapList.size() > 0) { if (fileDigestRegionMapList != null && fileDigestRegionMapList.size() > 0) {
count += fileDigestRegionMapList.size(); count += fileDigestRegionMapList.size();
for (Map<String, String> map : fileDigestRegionMapList) { for (Map<String, String> map : fileDigestRegionMapList) {
setCommonConfig(maatXmlConfig, map, 16, maatVersion.doubleValue(), service, transaction, redisDBIndex, setCommonConfig(maatXmlConfig, map, 16, maatVersion, service, transaction, redisDBIndex, null);// 16代表是文件摘要类域配置
null);// 16代表是文件摘要类域配置
} }
} }
List<Map<String, String>> fileLikeRegionMapList = maatConfig.getFileLikeRegionMapList(); List<Map<String, String>> fileLikeRegionMapList = maatConfig.getFileLikeRegionMapList();
if (fileLikeRegionMapList != null && fileLikeRegionMapList.size() > 0) { if (fileLikeRegionMapList != null && fileLikeRegionMapList.size() > 0) {
count += fileLikeRegionMapList.size(); count += fileLikeRegionMapList.size();
for (Map<String, String> map : fileLikeRegionMapList) { for (Map<String, String> map : fileLikeRegionMapList) {
setCommonConfig(maatXmlConfig, map, 17, maatVersion.doubleValue(), service, transaction, redisDBIndex, setCommonConfig(maatXmlConfig, map, 17, maatVersion, service, transaction, redisDBIndex, null);// 17代表是文本相似性域配置
null);// 17代表是文本相似性域配置
} }
} }
List<Map<String, String>> ipclientList = maatConfig.getIpClientRangeMapList(); List<Map<String, String>> ipclientList = maatConfig.getIpClientRangeMapList();
if (ipclientList != null && ipclientList.size() > 0) { if (ipclientList != null && ipclientList.size() > 0) {
count += ipclientList.size(); count += ipclientList.size();
for (Map<String, String> map : ipclientList) { for (Map<String, String> map : ipclientList) {
setCommonConfig(maatXmlConfig, map, 18, maatVersion.doubleValue(), service, transaction, redisDBIndex, setCommonConfig(maatXmlConfig, map, 18, maatVersion, service, transaction, redisDBIndex, null);// 18代表是区域ip域配置
null);// 18代表是区域ip域配置
} }
} }
if (count == 0) { if (count == 0 && isReuseSaveRegion) {
throw new ServiceRuntimeException("添加分组复用域配置时,所有的域配置都为空,请检查配置参数是否正确", throw new ServiceRuntimeException("添加分组复用域配置时,所有的域配置都为空,请检查配置参数是否正确",
RestBusinessCode.ConfigSourceIsNull.getValue()); RestBusinessCode.ConfigSourceIsNull.getValue());
} }
@@ -1665,12 +1645,12 @@ public class ConfigJedisServiceimpl implements ConfigRedisService {
} }
} catch (JedisConnectionException e) { } catch (JedisConnectionException e) {
String error = "连接redis异常,保存maat类配置失败" + e.getMessage(); String error = "连接redis异常,删除分组复用maat类配置失败" + e.getMessage();
logger.error(error); logger.error(error);
throw new ServiceRuntimeException(error, RestBusinessCode.CannotConnectionRedis.getValue()); throw new ServiceRuntimeException(error, RestBusinessCode.CannotConnectionRedis.getValue());
} catch (Exception e) { } catch (Exception e) {
transaction.discard(); transaction.discard();
String error = "保存maat类配置发生了异常" + e.getMessage(); String error = "删除分组复用maat类配置发生了异常" + e.getMessage();
logger.error(error); logger.error(error);
throw new ServiceRuntimeException(error, RestBusinessCode.SaveDataInError.getValue()); throw new ServiceRuntimeException(error, RestBusinessCode.SaveDataInError.getValue());
} finally { } finally {
@@ -1719,17 +1699,17 @@ public class ConfigJedisServiceimpl implements ConfigRedisService {
if (redisDb != null && !redisDb.trim().equals("")) { if (redisDb != null && !redisDb.trim().equals("")) {
// 关联关系中存在需要删除的region信息,开始删除域配置 // 关联关系中存在需要删除的region信息,开始删除域配置
// 根据分组与域关联关系找到对应域配置然后删除(重命名) // 根据分组与域关联关系找到对应域配置然后删除(重命名)
int redisDBIndex = Integer.parseInt(redisDb); int redisDBIndex = Integer.parseInt(redisDb.trim());
String maatVersionStr = JedisUtils.get("MAAT_VERSION", redisDBIndex); String maatVersionStr = JedisUtils.get("MAAT_VERSION", redisDBIndex);
if (maatVersionStr == null) { if (maatVersionStr == null) {
maatVersionStr = "0"; maatVersionStr = "0";
} }
if (maatVersionStr != null) { if (maatVersionStr != null) {
Long maatVersion = Long.valueOf(maatVersionStr) + 1l; Double maatVersion = Double.valueOf(maatVersionStr) + 1D;
removeRegionConfig(maatXmlConfig, removeRegionConfig(maatXmlConfig,
delRegionMap.get(redisDbArr) delRegionMap.get(redisDbArr)
.toArray(new String[delRegionMap.get(redisDbArr).size()]), .toArray(new String[delRegionMap.get(redisDbArr).size()]),
maatVersion.doubleValue(), service, transaction, redisDBIndex); maatVersion, service, transaction, redisDBIndex);
transaction.incrBy("MAAT_VERSION", 1l); transaction.incrBy("MAAT_VERSION", 1l);
logger.info("向{}号redis数据库更新了MAAT_VERSION,更新后版本是{}", redisDBIndex, logger.info("向{}号redis数据库更新了MAAT_VERSION,更新后版本是{}", redisDBIndex,
Integer.valueOf(maatVersionStr) + 1); Integer.valueOf(maatVersionStr) + 1);
@@ -1757,7 +1737,7 @@ public class ConfigJedisServiceimpl implements ConfigRedisService {
} }
} else { } else {
throw new ServiceRuntimeException("无法从" + idRelaRedisDBIndex + "号redis库中获取groupId=" + groupId + ",service=" throw new ServiceRuntimeException("无法从" + idRelaRedisDBIndex + "号redis库中获取groupId=" + groupId + ",service="
+ service + "的域配置关系,请检查数据是否正确" + groupRegionKey, RestBusinessCode.KeyNotExistsInRedis.getValue()); + service + "的域配置关系,请检查数据是否正确", RestBusinessCode.KeyNotExistsInRedis.getValue());
} }
} }

View File

@@ -52,11 +52,11 @@ public interface ConfigRedisService {
public boolean delUnMaatConfig(Map<Integer, Map<Integer, List<Long>>> idMap, boolean isInvalid); public boolean delUnMaatConfig(Map<Integer, Map<Integer, List<Long>>> idMap, boolean isInvalid);
/** /**
* 删除maat类配置,第一个key是redisDBIndex,第二个key是业务类型,value是配置id集合 * 删除maat类配置,key是业务类型,value是配置id集合
* @param idMap * @param serviceConfigMap
* @return 成功返回true,失败返回false或抛出异常 * @return 成功返回true,失败返回false或抛出异常
*/ */
public boolean delMaatConfig(Map<Integer, Map<Integer, List<Long>>> idMap); public boolean delMaatConfig(Map<Integer, List<Long>> serviceConfigMap);
/** /**
* 删除分组复用的域配置 * 删除分组复用的域配置