Skip to content

Commit

Permalink
Merge pull request #2403 from lkxiaolou/develop
Browse files Browse the repository at this point in the history
fix checksum bug #2401
  • Loading branch information
nkorange authored Mar 29, 2020
2 parents da4a414 + 83c098b commit b7a2c75
Show file tree
Hide file tree
Showing 13 changed files with 175 additions and 249 deletions.
47 changes: 24 additions & 23 deletions common/src/main/java/com/alibaba/nacos/common/utils/Md5Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
*/
package com.alibaba.nacos.common.utils;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
* MD5 generator
Expand All @@ -24,35 +26,34 @@
*/
public class Md5Utils {

private static ThreadLocal<MessageDigest> MESSAGE_DIGEST_LOCAL = new ThreadLocal<MessageDigest>() {
@Override
protected MessageDigest initialValue() {
try {
return MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
return null;
}
}
};

private static final int HEX_VALUE_COUNT = 16;

public static String getMD5(byte[] bytes) {
char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
char[] str = new char[16 * 2];
try {
java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
md.update(bytes);
byte[] tmp = md.digest();
int k = 0;
for (int i = 0; i < HEX_VALUE_COUNT; i++) {
byte byte0 = tmp[i];
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
} catch (Exception e) {
e.printStackTrace();
public static String getMD5(byte[] bytes) throws NoSuchAlgorithmException {

MessageDigest messageDigest = MESSAGE_DIGEST_LOCAL.get();
if (messageDigest != null) {
return new BigInteger(1, messageDigest.digest(bytes)).toString(HEX_VALUE_COUNT);
}
return new String(str);

throw new NoSuchAlgorithmException("MessageDigest get MD5 instance error");
}

public static String getMD5(String value, String encode) {
String result = "";
try {
result = getMD5(value.getBytes(encode));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return getMD5(value.getBytes(encode));
} catch (Exception e) {
throw new RuntimeException(e);
}

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
*/
package com.alibaba.nacos.config.server.aspect;

import com.alibaba.nacos.common.utils.Md5Utils;
import com.alibaba.nacos.config.server.constant.Constants;
import com.alibaba.nacos.config.server.monitor.MetricsMonitor;
import com.alibaba.nacos.config.server.service.ConfigService;
import com.alibaba.nacos.config.server.utils.GroupKey2;
import com.alibaba.nacos.config.server.utils.LogUtil;
import com.alibaba.nacos.config.server.utils.MD5;
import com.alibaba.nacos.config.server.utils.RequestUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
Expand Down Expand Up @@ -66,7 +67,7 @@ public class RequestLogAspect {
public Object interfacePublishSingle(ProceedingJoinPoint pjp, HttpServletRequest request,
HttpServletResponse response, String dataId, String group, String tenant,
String content) throws Throwable {
final String md5 = content == null ? null : MD5.getInstance().getMD5String(content);
final String md5 = content == null ? null : Md5Utils.getMD5(content, Constants.ENCODE);
MetricsMonitor.getPublishMonitor().incrementAndGet();
return logClientRequest("publish", pjp, request, response, dataId, group, tenant, md5);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
*/
package com.alibaba.nacos.config.server.model;

import com.alibaba.nacos.config.server.utils.MD5;
import com.alibaba.nacos.common.utils.Md5Utils;
import com.alibaba.nacos.config.server.constant.Constants;

import java.io.PrintWriter;
import java.io.Serializable;
Expand Down Expand Up @@ -46,7 +47,7 @@ public ConfigInfoBase(String dataId, String group, String content) {
this.group = group;
this.content = content;
if (this.content != null) {
this.md5 = MD5.getInstance().getMD5String(this.content);
this.md5 = Md5Utils.getMD5(this.content, Constants.ENCODE);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
*/
package com.alibaba.nacos.config.server.service;

import com.alibaba.nacos.common.utils.Md5Utils;
import com.alibaba.nacos.config.server.constant.Constants;
import com.alibaba.nacos.config.server.model.CacheItem;
import com.alibaba.nacos.config.server.model.ConfigInfoBase;
import com.alibaba.nacos.config.server.utils.GroupKey;
import com.alibaba.nacos.config.server.utils.GroupKey2;
import com.alibaba.nacos.config.server.utils.MD5;
import com.alibaba.nacos.config.server.utils.PropertyUtil;
import com.alibaba.nacos.config.server.utils.event.EventDispatcher;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -70,7 +70,8 @@ static public boolean dump(String dataId, String group, String tenant, String co
}

try {
final String md5 = MD5.getInstance().getMD5String(content);
final String md5 = Md5Utils.getMD5(content, Constants.ENCODE);

if (md5.equals(ConfigService.getContentMd5(groupKey))) {
dumpLog.warn(
"[dump-ignore] ignore to save cache file. groupKey={}, md5={}, lastModifiedOld={}, "
Expand Down Expand Up @@ -115,7 +116,7 @@ static public boolean dumpBeta(String dataId, String group, String tenant, Strin
}

try {
final String md5 = MD5.getInstance().getMD5String(content);
final String md5 = Md5Utils.getMD5(content, Constants.ENCODE);
if (md5.equals(ConfigService.getContentBetaMd5(groupKey))) {
dumpLog.warn(
"[dump-beta-ignore] ignore to save cache file. groupKey={}, md5={}, lastModifiedOld={}, "
Expand Down Expand Up @@ -154,7 +155,7 @@ static public boolean dumpTag(String dataId, String group, String tenant, String
}

try {
final String md5 = MD5.getInstance().getMD5String(content);
final String md5 = Md5Utils.getMD5(content, Constants.ENCODE);
if (md5.equals(ConfigService.getContentTagMd5(groupKey, tag))) {
dumpLog.warn(
"[dump-tag-ignore] ignore to save cache file. groupKey={}, md5={}, lastModifiedOld={}, "
Expand Down Expand Up @@ -191,7 +192,7 @@ static public boolean dumpChange(String dataId, String group, String tenant, Str
}

try {
final String md5 = MD5.getInstance().getMD5String(content);
final String md5 = Md5Utils.getMD5(content, Constants.ENCODE);
if (!STANDALONE_MODE || PropertyUtil.isStandaloneUseMysql()) {
String loacalMd5 = DiskUtil.getLocalConfigMd5(dataId, group, tenant);
if (md5.equals(loacalMd5)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
package com.alibaba.nacos.config.server.service;

import com.alibaba.nacos.common.utils.IoUtils;
import com.alibaba.nacos.common.utils.Md5Utils;
import com.alibaba.nacos.config.server.constant.Constants;
import com.alibaba.nacos.config.server.utils.LogUtil;
import com.alibaba.nacos.config.server.utils.MD5;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
Expand Down Expand Up @@ -172,7 +172,7 @@ static public String getConfig(String dataId, String group, String tenant)

static public String getLocalConfigMd5(String dataId, String group, String tenant)
throws IOException {
return MD5.getInstance().getMD5String(getConfig(dataId, group, tenant));
return Md5Utils.getMD5(getConfig(dataId, group, tenant), Constants.ENCODE);
}

static public File heartBeatFile() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
*/
package com.alibaba.nacos.config.server.service;

import com.alibaba.nacos.common.utils.Md5Utils;
import com.alibaba.nacos.config.server.constant.Constants;
import com.alibaba.nacos.config.server.enums.FileTypeEnum;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.config.server.model.*;
import com.alibaba.nacos.config.server.utils.LogUtil;
import com.alibaba.nacos.config.server.utils.MD5;
import com.alibaba.nacos.config.server.utils.PaginationHelper;
import com.alibaba.nacos.config.server.utils.ParamUtils;
import com.alibaba.nacos.config.server.utils.event.EventDispatcher;
Expand Down Expand Up @@ -537,7 +538,7 @@ public void addConfigInfo4Beta(ConfigInfo configInfo, String betaIps,
String appNameTmp = StringUtils.isBlank(configInfo.getAppName()) ? StringUtils.EMPTY : configInfo.getAppName();
String tenantTmp = StringUtils.isBlank(configInfo.getTenant()) ? StringUtils.EMPTY : configInfo.getTenant();
try {
String md5 = MD5.getInstance().getMD5String(configInfo.getContent());
String md5 = Md5Utils.getMD5(configInfo.getContent(), Constants.ENCODE);
jt.update(
"INSERT INTO config_info_beta(data_id,group_id,tenant_id,app_name,content,md5,beta_ips,src_ip,"
+ "src_user,gmt_create,gmt_modified) VALUES(?,?,?,?,?,?,?,?,?,?,?)",
Expand All @@ -563,7 +564,7 @@ public void addConfigInfo4Tag(ConfigInfo configInfo, String tag, String srcIp, S
String tenantTmp = StringUtils.isBlank(configInfo.getTenant()) ? StringUtils.EMPTY : configInfo.getTenant();
String tagTmp = StringUtils.isBlank(tag) ? StringUtils.EMPTY : tag.trim();
try {
String md5 = MD5.getInstance().getMD5String(configInfo.getContent());
String md5 = Md5Utils.getMD5(configInfo.getContent(), Constants.ENCODE);
jt.update(
"INSERT INTO config_info_tag(data_id,group_id,tenant_id,tag_id,app_name,content,md5,src_ip,src_user,"
+ "gmt_create,gmt_modified) VALUES(?,?,?,?,?,?,?,?,?,?,?)",
Expand Down Expand Up @@ -627,7 +628,7 @@ public void updateConfigInfo4Beta(ConfigInfo configInfo, String srcIp, String sr
String appNameTmp = StringUtils.isBlank(configInfo.getAppName()) ? StringUtils.EMPTY : configInfo.getAppName();
String tenantTmp = StringUtils.isBlank(configInfo.getTenant()) ? StringUtils.EMPTY : configInfo.getTenant();
try {
String md5 = MD5.getInstance().getMD5String(configInfo.getContent());
String md5 = Md5Utils.getMD5(configInfo.getContent(), Constants.ENCODE);
jt.update(
"UPDATE config_info_beta SET content=?, md5 = ?, src_ip=?,src_user=?,gmt_modified=?,app_name=? WHERE "
+ "data_id=? AND group_id=? AND tenant_id=?",
Expand All @@ -653,7 +654,7 @@ public void updateConfigInfo4Tag(ConfigInfo configInfo, String tag, String srcIp
String tenantTmp = StringUtils.isBlank(configInfo.getTenant()) ? StringUtils.EMPTY : configInfo.getTenant();
String tagTmp = StringUtils.isBlank(tag) ? StringUtils.EMPTY : tag.trim();
try {
String md5 = MD5.getInstance().getMD5String(configInfo.getContent());
String md5 = Md5Utils.getMD5(configInfo.getContent(), Constants.ENCODE);
jt.update(
"UPDATE config_info_tag SET content=?, md5 = ?, src_ip=?,src_user=?,gmt_modified=?,app_name=? WHERE "
+ "data_id=? AND group_id=? AND tenant_id=? AND tag_id=?",
Expand Down Expand Up @@ -2694,7 +2695,7 @@ private long addConfigInfoAtomic(final String srcIp, final String srcUser, final
final String type = configAdvanceInfo == null ? null : (String) configAdvanceInfo.get("type");
final String schema = configAdvanceInfo == null ? null : (String) configAdvanceInfo.get("schema");

final String md5Tmp = MD5.getInstance().getMD5String(configInfo.getContent());
final String md5Tmp = Md5Utils.getMD5(configInfo.getContent(), Constants.ENCODE);

KeyHolder keyHolder = new GeneratedKeyHolder();

Expand Down Expand Up @@ -2903,7 +2904,7 @@ private void updateConfigInfoAtomic(final ConfigInfo configInfo, final String sr
final Timestamp time, Map<String, Object> configAdvanceInfo) {
String appNameTmp = StringUtils.isBlank(configInfo.getAppName()) ? StringUtils.EMPTY : configInfo.getAppName();
String tenantTmp = StringUtils.isBlank(configInfo.getTenant()) ? StringUtils.EMPTY : configInfo.getTenant();
final String md5Tmp = MD5.getInstance().getMD5String(configInfo.getContent());
final String md5Tmp = Md5Utils.getMD5(configInfo.getContent(), Constants.ENCODE);
String desc = configAdvanceInfo == null ? null : (String) configAdvanceInfo.get("desc");
String use = configAdvanceInfo == null ? null : (String) configAdvanceInfo.get("use");
String effect = configAdvanceInfo == null ? null : (String) configAdvanceInfo.get("effect");
Expand Down Expand Up @@ -3060,7 +3061,7 @@ private void insertConfigHistoryAtomic(long id, ConfigInfo configInfo, String sr
final Timestamp time, String ops) {
String appNameTmp = StringUtils.isBlank(configInfo.getAppName()) ? StringUtils.EMPTY : configInfo.getAppName();
String tenantTmp = StringUtils.isBlank(configInfo.getTenant()) ? StringUtils.EMPTY : configInfo.getTenant();
final String md5Tmp = MD5.getInstance().getMD5String(configInfo.getContent());
final String md5Tmp = Md5Utils.getMD5(configInfo.getContent(), Constants.ENCODE);
try {
jt.update(
"INSERT INTO his_config_info (id,data_id,group_id,tenant_id,app_name,content,md5,src_ip,src_user,gmt_modified,op_type) VALUES(?,?,?,?,?,?,?,?,?,?,?)",
Expand Down Expand Up @@ -3354,8 +3355,7 @@ public Boolean completeMd5() {
String md5InDb = cf.getMd5();
final String content = cf.getContent();
final String tenant = cf.getTenant();
final String md5 = MD5.getInstance().getMD5String(
content);
final String md5 = Md5Utils.getMD5(content, Constants.ENCODE);
if (StringUtils.isBlank(md5InDb)) {
try {
updateMd5(cf.getDataId(), cf.getGroup(), tenant, md5, new Timestamp(cf.getLastModified()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.alibaba.nacos.config.server.service.dump;

import com.alibaba.nacos.common.utils.IoUtils;
import com.alibaba.nacos.common.utils.Md5Utils;
import com.alibaba.nacos.config.server.constant.Constants;
import com.alibaba.nacos.config.server.manager.TaskManager;
import com.alibaba.nacos.config.server.model.ConfigInfo;
Expand Down Expand Up @@ -346,7 +347,8 @@ public void run() {
ConfigInfo cf = MergeTaskProcessor.merge(dataId, group, tenant, datumList);
String aggrContent = cf.getContent();
String localContentMD5 = ConfigService.getContentMd5(GroupKey.getKey(dataId, group));
String aggrConetentMD5 = MD5.getInstance().getMD5String(aggrContent);
String aggrConetentMD5 = Md5Utils.getMD5(aggrContent, Constants.ENCODE);

if (!StringUtils.equals(localContentMD5, aggrConetentMD5)) {
persistService.insertOrUpdate(null, null, cf, time, null, false);
log.info("[merge-ok] {}, {}, size={}, length={}, md5={}, content={}", dataId, group,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.alibaba.nacos.config.server.service.dump;

import com.alibaba.nacos.common.utils.Md5Utils;
import com.alibaba.nacos.config.server.constant.Constants;
import com.alibaba.nacos.config.server.manager.AbstractTask;
import com.alibaba.nacos.config.server.manager.TaskProcessor;
import com.alibaba.nacos.config.server.model.ConfigInfo;
Expand All @@ -28,7 +30,6 @@
import com.alibaba.nacos.config.server.service.trace.ConfigTraceService;
import com.alibaba.nacos.config.server.utils.GroupKey2;
import com.alibaba.nacos.config.server.utils.LogUtil;
import com.alibaba.nacos.config.server.utils.MD5;
import org.apache.commons.lang3.StringUtils;

import java.sql.Timestamp;
Expand Down Expand Up @@ -264,7 +265,7 @@ public boolean process(String taskType, AbstractTask task) {
cf.getLastModified(), cf.getType());

final String content = cf.getContent();
final String md5 = MD5.getInstance().getMD5String(content);
final String md5 = Md5Utils.getMD5(content, Constants.ENCODE);
LogUtil.dumpLog.info("[dump-all-ok] {}, {}, length={}, md5={}",
GroupKey2.getKey(cf.getDataId(), cf.getGroup()), cf.getLastModified(), content.length(), md5);
}
Expand Down Expand Up @@ -409,7 +410,7 @@ public boolean process(String taskType, AbstractTask task) {
boolean result = ConfigService.dumpChange(cf.getDataId(), cf.getGroup(), cf.getTenant(),
cf.getContent(), cf.getLastModified());
final String content = cf.getContent();
final String md5 = MD5.getInstance().getMD5String(content);
final String md5 = Md5Utils.getMD5(content, Constants.ENCODE);
LogUtil.defaultLog.info(
"[dump-change-ok] {}, {}, length={}, md5={}",
new Object[] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
*/
package com.alibaba.nacos.config.server.service.trace;

import com.alibaba.nacos.common.utils.Md5Utils;
import com.alibaba.nacos.config.server.constant.Constants;
import com.alibaba.nacos.config.server.monitor.MetricsMonitor;
import com.alibaba.nacos.config.server.utils.LogUtil;
import com.alibaba.nacos.config.server.utils.MD5;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

Expand Down Expand Up @@ -61,7 +62,8 @@ public static void logPersistenceEvent(String dataId, String group, String tenan
}
//localIp | dataid | group | tenant | requestIpAppName | ts | handleIp | event | type | [delayed = -1] | ext
// (md5)
String md5 = content == null ? null : MD5.getInstance().getMD5String(content);
String md5 = content == null ? null : Md5Utils.getMD5(content, Constants.ENCODE);

LogUtil.traceLog.info("{}|{}|{}|{}|{}|{}|{}|{}|{}|{}|{}", LOCAL_IP, dataId, group, tenant,
requestIpAppName, ts, handleIp, "persist", type, -1, md5);
}
Expand Down
Loading

0 comments on commit b7a2c75

Please sign in to comment.