Skip to content

Commit

Permalink
fix(alibaba#12231): When inserting and updating configurations in the…
Browse files Browse the repository at this point in the history
… database, the time-related field values need to be set using the time obtained from the database's built-in time function.
  • Loading branch information
Bo-Qiu committed Jun 15, 2024
1 parent 4abeb3f commit 74ba1b9
Show file tree
Hide file tree
Showing 16 changed files with 20 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -250,14 +250,12 @@ public ConfigOperateResult updateConfigInfo4BetaCas(ConfigInfo configInfo, Strin

ConfigInfoBetaMapper configInfoBetaMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(),
TableConstant.CONFIG_INFO_BETA);
Timestamp time = new Timestamp(System.currentTimeMillis());
MapperContext context = new MapperContext();
context.putUpdateParameter(FieldConstant.CONTENT, configInfo.getContent());
context.putUpdateParameter(FieldConstant.MD5, md5);
context.putUpdateParameter(FieldConstant.BETA_IPS, betaIps);
context.putUpdateParameter(FieldConstant.SRC_IP, srcIp);
context.putUpdateParameter(FieldConstant.SRC_USER, srcUser);
context.putUpdateParameter(FieldConstant.GMT_MODIFIED, time);
context.putUpdateParameter(FieldConstant.APP_NAME, appNameTmp);

context.putWhereParameter(FieldConstant.DATA_ID, configInfo.getDataId());
Expand All @@ -269,7 +267,7 @@ public ConfigOperateResult updateConfigInfo4BetaCas(ConfigInfo configInfo, Strin
final String sql = mapperResult.getSql();
List<Object> paramList = mapperResult.getParamList();
final Object[] args = paramList.toArray();

Timestamp time = new Timestamp(System.currentTimeMillis());
EmbeddedStorageContextUtils.onModifyConfigBetaInfo(configInfo, betaIps, srcIp, time);
EmbeddedStorageContextHolder.addSqlContext(sql, args);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -614,13 +614,11 @@ private ConfigOperateResult updateConfigInfoAtomicCas(final ConfigInfo configInf
configInfo.getEncryptedDataKey() == null ? StringUtils.EMPTY : configInfo.getEncryptedDataKey();
ConfigInfoMapper configInfoMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(),
TableConstant.CONFIG_INFO);
Timestamp time = new Timestamp(System.currentTimeMillis());
MapperContext context = new MapperContext();
context.putUpdateParameter(FieldConstant.CONTENT, configInfo.getContent());
context.putUpdateParameter(FieldConstant.MD5, md5Tmp);
context.putUpdateParameter(FieldConstant.SRC_IP, srcIp);
context.putUpdateParameter(FieldConstant.SRC_USER, srcUser);
context.putUpdateParameter(FieldConstant.GMT_MODIFIED, time);
context.putUpdateParameter(FieldConstant.APP_NAME, appNameTmp);
context.putUpdateParameter(FieldConstant.C_DESC, desc);
context.putUpdateParameter(FieldConstant.C_USE, use);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,14 +234,11 @@ public ConfigOperateResult updateConfigInfo4TagCas(ConfigInfo configInfo, String
String md5 = MD5Utils.md5Hex(configInfo.getContent(), Constants.ENCODE);
ConfigInfoTagMapper configInfoTagMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(),
TableConstant.CONFIG_INFO_TAG);
Timestamp time = new Timestamp(System.currentTimeMillis());

MapperContext context = new MapperContext();
context.putUpdateParameter(FieldConstant.CONTENT, configInfo.getContent());
context.putUpdateParameter(FieldConstant.MD5, md5);
context.putUpdateParameter(FieldConstant.SRC_IP, srcIp);
context.putUpdateParameter(FieldConstant.SRC_USER, srcUser);
context.putUpdateParameter(FieldConstant.GMT_MODIFIED, time);
context.putUpdateParameter(FieldConstant.APP_NAME, appNameTmp);

context.putWhereParameter(FieldConstant.DATA_ID, configInfo.getDataId());
Expand All @@ -251,7 +248,7 @@ public ConfigOperateResult updateConfigInfo4TagCas(ConfigInfo configInfo, String
context.putWhereParameter(FieldConstant.MD5, configInfo.getMd5());

final MapperResult mapperResult = configInfoTagMapper.updateConfigInfo4TagCas(context);

Timestamp time = new Timestamp(System.currentTimeMillis());
EmbeddedStorageContextUtils.onModifyConfigTagInfo(configInfo, tagTmp, srcIp, time);
EmbeddedStorageContextHolder.addSqlContext(mapperResult.getSql(), mapperResult.getParamList().toArray());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;

import java.sql.Timestamp;
import java.util.Arrays;
import java.util.List;

Expand Down Expand Up @@ -127,7 +126,6 @@ public ConfigOperateResult insertOrUpdateBeta(final ConfigInfo configInfo, final
configInfo.getGroup(), configInfo.getTenant());
if (configInfo4BetaState == null) {
return addConfigInfo4Beta(configInfo, betaIps, srcIp, srcUser);

} else {
return updateConfigInfo4Beta(configInfo, betaIps, srcIp, srcUser);
}
Expand Down Expand Up @@ -229,15 +227,13 @@ public ConfigOperateResult updateConfigInfo4BetaCas(ConfigInfo configInfo, Strin
try {
ConfigInfoBetaMapper configInfoBetaMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(),
TableConstant.CONFIG_INFO_BETA);
Timestamp time = new Timestamp(System.currentTimeMillis());


MapperContext context = new MapperContext();
context.putUpdateParameter(FieldConstant.CONTENT, configInfo.getContent());
context.putUpdateParameter(FieldConstant.MD5, md5);
context.putUpdateParameter(FieldConstant.BETA_IPS, betaIps);
context.putUpdateParameter(FieldConstant.SRC_IP, srcIp);
context.putUpdateParameter(FieldConstant.SRC_USER, srcUser);
context.putUpdateParameter(FieldConstant.GMT_MODIFIED, time);
context.putUpdateParameter(FieldConstant.APP_NAME, appNameTmp);

context.putWhereParameter(FieldConstant.DATA_ID, configInfo.getDataId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -629,14 +629,11 @@ private int updateConfigInfoAtomicCas(final ConfigInfo configInfo, final String
try {
ConfigInfoMapper configInfoMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(),
TableConstant.CONFIG_INFO);
Timestamp now = new Timestamp(System.currentTimeMillis());

MapperContext context = new MapperContext();
context.putUpdateParameter(FieldConstant.CONTENT, configInfo.getContent());
context.putUpdateParameter(FieldConstant.MD5, md5Tmp);
context.putUpdateParameter(FieldConstant.SRC_IP, srcIp);
context.putUpdateParameter(FieldConstant.SRC_USER, srcUser);
context.putUpdateParameter(FieldConstant.GMT_MODIFIED, now);
context.putUpdateParameter(FieldConstant.APP_NAME, appNameTmp);
context.putUpdateParameter(FieldConstant.C_DESC, desc);
context.putUpdateParameter(FieldConstant.C_USE, use);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,14 +219,11 @@ public ConfigOperateResult updateConfigInfo4TagCas(ConfigInfo configInfo, String
String md5 = MD5Utils.md5Hex(configInfo.getContent(), Constants.ENCODE);
ConfigInfoTagMapper configInfoTagMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(),
TableConstant.CONFIG_INFO_TAG);
Timestamp time = new Timestamp(System.currentTimeMillis());

MapperContext context = new MapperContext();
context.putUpdateParameter(FieldConstant.CONTENT, configInfo.getContent());
context.putUpdateParameter(FieldConstant.MD5, md5);
context.putUpdateParameter(FieldConstant.SRC_IP, srcIp);
context.putUpdateParameter(FieldConstant.SRC_USER, srcUser);
context.putUpdateParameter(FieldConstant.GMT_MODIFIED, time);
context.putUpdateParameter(FieldConstant.APP_NAME, appNameTmp);

context.putWhereParameter(FieldConstant.DATA_ID, configInfo.getDataId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ void testInsertOrUpdateCasOfUpdateConfigSuccess() {
//expect update config info invoked.
embeddedStorageContextHolderMockedStatic.verify(
() -> EmbeddedStorageContextHolder.addSqlContext(eq(Boolean.TRUE), anyString(), eq(content),
eq(MD5Utils.md5Hex(content, Constants.PERSIST_ENCODE)), eq(srcIp), eq(srcUser), any(Timestamp.class), eq(appName),
eq(MD5Utils.md5Hex(content, Constants.PERSIST_ENCODE)), eq(srcIp), eq(srcUser), eq(appName),
eq(desc), eq(use), eq(effect), eq(type), eq(schema), eq(encryptedDataKey), eq(dataId), eq(group), eq(tenant),
eq(casMd5)), times(1));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,6 @@ void testInsertOrUpdateOfUpdateConfigSuccess() {

@Test
void testInsertOrUpdateCasOfUpdateConfigSuccess() {

Map<String, Object> configAdvanceInfo = new HashMap<>();
configAdvanceInfo.put("config_tags", "tag1,tag2");
configAdvanceInfo.put("desc", "desc11");
Expand Down Expand Up @@ -458,7 +457,7 @@ void testInsertOrUpdateCasOfUpdateConfigSuccess() {
String srcUser = "srcUser";
//mock update config info cas
Mockito.when(jdbcTemplate.update(anyString(), eq(content), eq(MD5Utils.md5Hex(content, Constants.PERSIST_ENCODE)), eq(srcIp),
eq(srcUser), any(Timestamp.class), eq(configInfoWrapperOld.getAppName()), eq(configAdvanceInfo.get("desc")),
eq(srcUser), eq(configInfoWrapperOld.getAppName()), eq(configAdvanceInfo.get("desc")),
eq(configAdvanceInfo.get("use")), eq(configAdvanceInfo.get("effect")), eq(configAdvanceInfo.get("type")),
eq(configAdvanceInfo.get("schema")), eq(encryptedDataKey), eq(dataId), eq(group), eq(tenant), eq(casMd5))).thenReturn(1);

Expand All @@ -483,7 +482,7 @@ void testInsertOrUpdateCasOfUpdateConfigSuccess() {
//expect update config cas
Mockito.verify(jdbcTemplate, times(1))
.update(anyString(), eq(content), eq(MD5Utils.md5Hex(content, Constants.PERSIST_ENCODE)), eq(srcIp), eq(srcUser),
any(Timestamp.class), eq(configInfoWrapperOld.getAppName()), eq(configAdvanceInfo.get("desc")),
eq(configInfoWrapperOld.getAppName()), eq(configAdvanceInfo.get("desc")),
eq(configAdvanceInfo.get("use")), eq(configAdvanceInfo.get("effect")), eq(configAdvanceInfo.get("type")),
eq(configAdvanceInfo.get("schema")), eq(encryptedDataKey), eq(dataId), eq(group), eq(tenant), eq(casMd5));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public interface ConfigInfoBetaMapper extends Mapper {
* @return The result of updating beta configuration information.
*/
default MapperResult updateConfigInfo4BetaCas(MapperContext context) {
final String sql = "UPDATE config_info_beta SET content = ?,md5 = ?,beta_ips = ?,src_ip = ?,src_user = ?,gmt_modified = ?,app_name = ? "
final String sql = "UPDATE config_info_beta SET content = ?,md5 = ?,beta_ips = ?,src_ip = ?,src_user = ?,gmt_modified = CURRENT_TIMESTAMP,app_name = ? "
+ "WHERE data_id = ? AND group_id = ? AND tenant_id = ? AND (md5 = ? OR md5 is null OR md5 = '')";

List<Object> paramList = new ArrayList<>();
Expand All @@ -52,7 +52,6 @@ default MapperResult updateConfigInfo4BetaCas(MapperContext context) {
paramList.add(context.getUpdateParameter(FieldConstant.BETA_IPS));
paramList.add(context.getUpdateParameter(FieldConstant.SRC_IP));
paramList.add(context.getUpdateParameter(FieldConstant.SRC_USER));
paramList.add(context.getUpdateParameter(FieldConstant.GMT_MODIFIED));
paramList.add(context.getUpdateParameter(FieldConstant.APP_NAME));

paramList.add(context.getWhereParameter(FieldConstant.DATA_ID));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,6 @@ default MapperResult updateConfigInfoAtomicCas(MapperContext context) {
paramList.add(context.getUpdateParameter(FieldConstant.MD5));
paramList.add(context.getUpdateParameter(FieldConstant.SRC_IP));
paramList.add(context.getUpdateParameter(FieldConstant.SRC_USER));
paramList.add(context.getUpdateParameter(FieldConstant.GMT_MODIFIED));
paramList.add(context.getUpdateParameter(FieldConstant.APP_NAME));
paramList.add(context.getUpdateParameter(FieldConstant.C_DESC));
paramList.add(context.getUpdateParameter(FieldConstant.C_USE));
Expand All @@ -504,7 +503,7 @@ default MapperResult updateConfigInfoAtomicCas(MapperContext context) {
paramList.add(context.getWhereParameter(FieldConstant.GROUP_ID));
paramList.add(context.getWhereParameter(FieldConstant.TENANT_ID));
paramList.add(context.getWhereParameter(FieldConstant.MD5));
String sql = "UPDATE config_info SET " + "content=?, md5 = ?, src_ip=?,src_user=?,gmt_modified=?,"
String sql = "UPDATE config_info SET " + "content=?, md5 = ?, src_ip=?,src_user=?,gmt_modified=CURRENT_TIMESTAMP,"
+ " app_name=?,c_desc=?,c_use=?,effect=?,type=?,c_schema=?,encrypted_data_key=? "
+ "WHERE data_id=? AND group_id=? AND tenant_id=? AND (md5=? OR md5 IS NULL OR md5='')";
return new MapperResult(sql, paramList);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ default MapperResult updateConfigInfo4TagCas(MapperContext context) {
Object md5 = context.getUpdateParameter(FieldConstant.MD5);
Object srcIp = context.getUpdateParameter(FieldConstant.SRC_IP);
Object srcUser = context.getUpdateParameter(FieldConstant.SRC_USER);
Object gmtModified = context.getUpdateParameter(FieldConstant.GMT_MODIFIED);
Object appName = context.getUpdateParameter(FieldConstant.APP_NAME);

Object dataId = context.getWhereParameter(FieldConstant.DATA_ID);
Expand All @@ -53,10 +52,10 @@ default MapperResult updateConfigInfo4TagCas(MapperContext context) {
Object tagId = context.getWhereParameter(FieldConstant.TAG_ID);
Object oldMd5 = context.getWhereParameter(FieldConstant.MD5);
String sql =
"UPDATE config_info_tag SET content = ?, md5 = ?, src_ip = ?,src_user = ?,gmt_modified = ?,app_name = ? "
"UPDATE config_info_tag SET content = ?, md5 = ?, src_ip = ?,src_user = ?,gmt_modified = CURRENT_TIMESTAMP,app_name = ? "
+ "WHERE data_id = ? AND group_id = ? AND tenant_id = ? AND tag_id = ? AND (md5 = ? OR md5 IS NULL OR md5 = '')";
return new MapperResult(sql,
CollectionUtils.list(content, md5, srcIp, srcUser, gmtModified, appName, dataId, groupId, tenantId,
CollectionUtils.list(content, md5, srcIp, srcUser, appName, dataId, groupId, tenantId,
tagId, oldMd5));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ public void testUpdateConfigInfo4BetaCas() {
String newMD5 = "newMD5";
String srcIp = "1.1.1.1";
Object srcUser = "nacos";
Object time = new Timestamp(System.currentTimeMillis());
Object appNameTmp = "newAppName";
Object desc = "description";
Object use = "use";
Expand All @@ -86,7 +85,6 @@ public void testUpdateConfigInfo4BetaCas() {
context.putUpdateParameter(FieldConstant.BETA_IPS, betaIps);
context.putUpdateParameter(FieldConstant.SRC_IP, srcIp);
context.putUpdateParameter(FieldConstant.SRC_USER, srcUser);
context.putUpdateParameter(FieldConstant.GMT_MODIFIED, time);
context.putUpdateParameter(FieldConstant.APP_NAME, appNameTmp);
context.putUpdateParameter(FieldConstant.C_DESC, desc);
context.putUpdateParameter(FieldConstant.C_USE, use);
Expand All @@ -107,11 +105,10 @@ public void testUpdateConfigInfo4BetaCas() {

String sql = mapperResult.getSql();
Assert.assertEquals(sql, "UPDATE config_info_beta SET content = ?,md5 = ?,beta_ips = ?,src_ip = ?,src_user = ?,"
+ "gmt_modified = ?,app_name = ? WHERE data_id = ? AND group_id = ? AND tenant_id = ? AND "
+ "gmt_modified = CURRENT_TIMESTAMP,app_name = ? WHERE data_id = ? AND group_id = ? AND tenant_id = ? AND "
+ "(md5 = ? OR md5 is null OR md5 = '')");
Assert.assertArrayEquals(mapperResult.getParamList().toArray(),
new Object[] {newContent, newMD5, betaIps, srcIp, srcUser, time, appNameTmp, dataId, group, tenantId,
md5});
new Object[]{newContent, newMD5, betaIps, srcIp, srcUser, appNameTmp, dataId, group, tenantId, md5});
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,6 @@ public void testUpdateConfigInfoAtomicCas() {
String newMD5 = "newMD5";
String srcIp = "1.1.1.1";
Object srcUser = "nacos";
Object time = new Timestamp(System.currentTimeMillis());
Object appNameTmp = "newAppName";
Object desc = "description";
Object use = "use";
Expand All @@ -340,7 +339,6 @@ public void testUpdateConfigInfoAtomicCas() {
context.putUpdateParameter(FieldConstant.MD5, newMD5);
context.putUpdateParameter(FieldConstant.SRC_IP, srcIp);
context.putUpdateParameter(FieldConstant.SRC_USER, srcUser);
context.putUpdateParameter(FieldConstant.GMT_MODIFIED, time);
context.putUpdateParameter(FieldConstant.APP_NAME, appNameTmp);
context.putUpdateParameter(FieldConstant.C_DESC, desc);
context.putUpdateParameter(FieldConstant.C_USE, use);
Expand All @@ -359,11 +357,11 @@ public void testUpdateConfigInfoAtomicCas() {

MapperResult mapperResult = configInfoMapperByDerby.updateConfigInfoAtomicCas(context);
Assert.assertEquals(mapperResult.getSql(), "UPDATE config_info SET "
+ "content=?, md5 = ?, src_ip=?,src_user=?,gmt_modified=?, app_name=?,c_desc=?,c_use=?,"
+ "content=?, md5 = ?, src_ip=?,src_user=?,gmt_modified=CURRENT_TIMESTAMP, app_name=?,c_desc=?,c_use=?,"
+ "effect=?,type=?,c_schema=?,encrypted_data_key=? "
+ "WHERE data_id=? AND group_id=? AND tenant_id=? AND (md5=? OR md5 IS NULL OR md5='')");
Assert.assertArrayEquals(mapperResult.getParamList().toArray(),
new Object[] {newContent, newMD5, srcIp, srcUser, time, appNameTmp, desc, use, effect, type, schema,
new Object[] {newContent, newMD5, srcIp, srcUser, appNameTmp, desc, use, effect, type, schema,
encrypedDataKey, dataId, group, tenantId, md5});
}
}
Loading

0 comments on commit 74ba1b9

Please sign in to comment.