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 833e6ca commit 6fe4d0f
Show file tree
Hide file tree
Showing 28 changed files with 807 additions and 285 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@
import com.alibaba.nacos.common.utils.CollectionUtils;
import com.alibaba.nacos.config.server.model.capacity.Capacity;
import com.alibaba.nacos.config.server.model.capacity.GroupCapacity;
import com.alibaba.nacos.config.server.utils.TimeUtils;
import com.alibaba.nacos.persistence.datasource.DataSourceService;
import com.alibaba.nacos.persistence.datasource.DynamicDataSource;
import com.alibaba.nacos.config.server.utils.TimeUtils;
import com.alibaba.nacos.plugin.datasource.MapperManager;
import com.alibaba.nacos.plugin.datasource.constants.CommonConstant;
import com.alibaba.nacos.plugin.datasource.constants.FieldConstant;
import com.alibaba.nacos.plugin.datasource.constants.TableConstant;
import com.alibaba.nacos.plugin.datasource.enums.TrustedSqlFunctionEnum;
import com.alibaba.nacos.plugin.datasource.mapper.ConfigInfoMapper;
import com.alibaba.nacos.plugin.datasource.mapper.GroupCapacityMapper;
import com.alibaba.nacos.plugin.datasource.model.ColumnFunctionPair;
import com.alibaba.nacos.plugin.datasource.model.MapperContext;
import com.alibaba.nacos.plugin.datasource.model.MapperResult;
import com.alibaba.nacos.sys.env.EnvUtil;
Expand Down Expand Up @@ -257,33 +259,33 @@ public boolean decrementUsage(GroupCapacity groupCapacity) {
public boolean updateGroupCapacity(String group, Integer quota, Integer maxSize, Integer maxAggrCount,
Integer maxAggrSize) {
List<Object> argList = CollectionUtils.list();
List<String> columnList = CollectionUtils.list();
List<ColumnFunctionPair> columnAndFuncctionList = CollectionUtils.list();
if (quota != null) {
columnList.add("quota");
columnAndFuncctionList.add(ColumnFunctionPair.withColumn("quota"));
argList.add(quota);
}
if (maxSize != null) {
columnList.add("max_size");
columnAndFuncctionList.add(ColumnFunctionPair.withColumn("max_size"));
argList.add(maxSize);
}
if (maxAggrCount != null) {
columnList.add("max_aggr_count");
columnAndFuncctionList.add(ColumnFunctionPair.withColumn("max_aggr_count"));
argList.add(maxAggrCount);
}
if (maxAggrSize != null) {
columnList.add("max_aggr_size");
columnAndFuncctionList.add(ColumnFunctionPair.withColumn("max_aggr_size"));
argList.add(maxAggrSize);
}
columnList.add("gmt_modified");
columnAndFuncctionList.add(ColumnFunctionPair.withColumnAndFunction("gmt_modified", TrustedSqlFunctionEnum.CURRENT_TIMESTAMP));
argList.add(TimeUtils.getCurrentTime());

List<String> whereList = CollectionUtils.list();
whereList.add("group_id");
argList.add(group);

GroupCapacityMapper groupCapacityMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(),
TableConstant.GROUP_CAPACITY);
String sql = groupCapacityMapper.update(columnList, whereList);
String sql = groupCapacityMapper.update(columnAndFuncctionList, whereList);
try {
return jdbcTemplate.update(sql, argList.toArray()) == 1;
} catch (CannotGetJdbcConnectionException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.alibaba.nacos.config.server.model.capacity.TenantCapacity;
import com.alibaba.nacos.config.server.utils.TimeUtils;
import com.alibaba.nacos.persistence.datasource.DataSourceService;
import com.alibaba.nacos.persistence.datasource.DynamicDataSource;
import com.alibaba.nacos.config.server.utils.TimeUtils;
import com.alibaba.nacos.plugin.datasource.MapperManager;
import com.alibaba.nacos.plugin.datasource.constants.CommonConstant;
import com.alibaba.nacos.plugin.datasource.constants.FieldConstant;
import com.alibaba.nacos.plugin.datasource.constants.TableConstant;
import com.alibaba.nacos.plugin.datasource.enums.TrustedSqlFunctionEnum;
import com.alibaba.nacos.plugin.datasource.mapper.TenantCapacityMapper;
import com.alibaba.nacos.plugin.datasource.model.ColumnFunctionPair;
import com.alibaba.nacos.plugin.datasource.model.MapperContext;
import com.alibaba.nacos.plugin.datasource.model.MapperResult;
import com.alibaba.nacos.sys.env.EnvUtil;
Expand Down Expand Up @@ -238,35 +240,35 @@ public boolean decrementUsage(TenantCapacity tenantCapacity) {
public boolean updateTenantCapacity(String tenant, Integer quota, Integer maxSize, Integer maxAggrCount,
Integer maxAggrSize) {
List<Object> argList = CollectionUtils.list();
List<String> columns = new ArrayList<>();

List<ColumnFunctionPair> columnAndFunctionPairs = new ArrayList<>();
if (quota != null) {
columns.add("quota");
columnAndFunctionPairs.add(ColumnFunctionPair.withColumn("quota"));
argList.add(quota);
}
if (maxSize != null) {
columns.add("max_size");
columnAndFunctionPairs.add(ColumnFunctionPair.withColumn("max_size"));
argList.add(maxSize);
}
if (maxAggrCount != null) {
columns.add("max_aggr_count");
columnAndFunctionPairs.add(ColumnFunctionPair.withColumn("max_aggr_count"));
argList.add(maxAggrCount);
}
if (maxAggrSize != null) {
columns.add("max_aggr_size");
columnAndFunctionPairs.add(ColumnFunctionPair.withColumn("max_aggr_size"));
argList.add(maxAggrSize);
}
columns.add("gmt_modified");
columnAndFunctionPairs.add(ColumnFunctionPair.withColumnAndFunction("gmt_modified", TrustedSqlFunctionEnum.CURRENT_TIMESTAMP));
argList.add(TimeUtils.getCurrentTime());

List<String> where = new ArrayList<>();
where.add("tenant_id");

argList.add(tenant);

TenantCapacityMapper tenantCapacityMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(),
TableConstant.TENANT_CAPACITY);
String sql = tenantCapacityMapper.update(columns, where);
String sql = tenantCapacityMapper.update(columnAndFunctionPairs, where);
try {
return jdbcTemplate.update(sql, argList.toArray()) == 1;
} catch (CannotGetJdbcConnectionException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@
import com.alibaba.nacos.plugin.datasource.constants.CommonConstant;
import com.alibaba.nacos.plugin.datasource.constants.FieldConstant;
import com.alibaba.nacos.plugin.datasource.constants.TableConstant;
import com.alibaba.nacos.plugin.datasource.enums.TrustedSqlFunctionEnum;
import com.alibaba.nacos.plugin.datasource.mapper.ConfigInfoAggrMapper;
import com.alibaba.nacos.plugin.datasource.model.ColumnFunctionPair;
import com.alibaba.nacos.plugin.datasource.model.MapperContext;
import com.alibaba.nacos.plugin.datasource.model.MapperResult;
import com.alibaba.nacos.sys.env.EnvUtil;
import org.springframework.context.annotation.Conditional;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -93,28 +94,37 @@ public boolean addAggrConfigInfo(final String dataId, final String group, String
String appNameTmp = StringUtils.isBlank(appName) ? StringUtils.EMPTY : appName;
String tenantTmp = StringUtils.isBlank(tenant) ? StringUtils.EMPTY : tenant;
String contentTmp = StringUtils.isBlank(content) ? StringUtils.EMPTY : content;
final Timestamp now = new Timestamp(System.currentTimeMillis());


ConfigInfoAggrMapper configInfoAggrMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(),
TableConstant.CONFIG_INFO_AGGR);
final String select = configInfoAggrMapper.select(Collections.singletonList("content"),
Arrays.asList("data_id", "group_id", "tenant_id", "datum_id"));
final String insert = configInfoAggrMapper.insert(
Arrays.asList("data_id", "group_id", "tenant_id", "datum_id", "app_name", "content", "gmt_modified"));
final String update = configInfoAggrMapper.update(Arrays.asList("content", "gmt_modified"),
Arrays.asList(
ColumnFunctionPair.withColumn("data_id"),
ColumnFunctionPair.withColumn("group_id"),
ColumnFunctionPair.withColumn("tenant_id"),
ColumnFunctionPair.withColumn("datum_id"),
ColumnFunctionPair.withColumn("app_name"),
ColumnFunctionPair.withColumn("content"),
ColumnFunctionPair.withColumnAndFunction("gmt_modified", TrustedSqlFunctionEnum.CURRENT_TIMESTAMP)));
final String update = configInfoAggrMapper.update(
Arrays.asList(
ColumnFunctionPair.withColumn("content"),
ColumnFunctionPair.withColumnAndFunction("gmt_modified", TrustedSqlFunctionEnum.CURRENT_TIMESTAMP)),
Arrays.asList("data_id", "group_id", "tenant_id", "datum_id"));
String dbContent = databaseOperate.queryOne(select, new Object[] {dataId, group, tenantTmp, datumId},

String dbContent = databaseOperate.queryOne(select, new Object[]{dataId, group, tenantTmp, datumId},
String.class);

if (Objects.isNull(dbContent)) {
final Object[] args = new Object[] {dataId, group, tenantTmp, datumId, appNameTmp, contentTmp, now};
final Object[] args = new Object[]{dataId, group, tenantTmp, datumId, appNameTmp, contentTmp};
EmbeddedStorageContextHolder.addSqlContext(insert, args);
} else if (!dbContent.equals(content)) {
final Object[] args = new Object[] {contentTmp, now, dataId, group, tenantTmp, datumId};
final Object[] args = new Object[]{contentTmp, dataId, group, tenantTmp, datumId};
EmbeddedStorageContextHolder.addSqlContext(update, args);
}

try {
boolean result = databaseOperate.update(EmbeddedStorageContextHolder.getCurrentSqlContext());
if (!result) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
import com.alibaba.nacos.plugin.datasource.constants.CommonConstant;
import com.alibaba.nacos.plugin.datasource.constants.FieldConstant;
import com.alibaba.nacos.plugin.datasource.constants.TableConstant;
import com.alibaba.nacos.plugin.datasource.enums.TrustedSqlFunctionEnum;
import com.alibaba.nacos.plugin.datasource.mapper.ConfigInfoBetaMapper;
import com.alibaba.nacos.plugin.datasource.model.ColumnFunctionPair;
import com.alibaba.nacos.plugin.datasource.model.MapperContext;
import com.alibaba.nacos.plugin.datasource.model.MapperResult;
import com.alibaba.nacos.sys.env.EnvUtil;
Expand Down Expand Up @@ -126,19 +128,30 @@ public ConfigOperateResult addConfigInfo4Beta(ConfigInfo configInfo, String beta
ConfigInfoBetaMapper configInfoBetaMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(),
TableConstant.CONFIG_INFO_BETA);
Timestamp time = new Timestamp(System.currentTimeMillis());

final String sql = configInfoBetaMapper.insert(
Arrays.asList("data_id", "group_id", "tenant_id", "app_name", "content", "md5", "beta_ips",
"src_ip", "src_user", "gmt_create", "gmt_modified", "encrypted_data_key"));
final Object[] args = new Object[] {configInfo.getDataId(), configInfo.getGroup(), tenantTmp, appNameTmp,
configInfo.getContent(), md5, betaIps, srcIp, srcUser, time, time, encryptedDataKey};

Arrays.asList(
ColumnFunctionPair.withColumn("data_id"),
ColumnFunctionPair.withColumn("group_id"),
ColumnFunctionPair.withColumn("tenant_id"),
ColumnFunctionPair.withColumn("app_name"),
ColumnFunctionPair.withColumn("content"),
ColumnFunctionPair.withColumn("md5"),
ColumnFunctionPair.withColumn("beta_ips"),
ColumnFunctionPair.withColumn("src_ip"),
ColumnFunctionPair.withColumn("src_user"),
ColumnFunctionPair.withColumnAndFunction("gmt_create", TrustedSqlFunctionEnum.CURRENT_TIMESTAMP),
ColumnFunctionPair.withColumnAndFunction("gmt_modified", TrustedSqlFunctionEnum.CURRENT_TIMESTAMP),
ColumnFunctionPair.withColumn("encrypted_data_key")));
final Object[] args = new Object[]{configInfo.getDataId(), configInfo.getGroup(), tenantTmp, appNameTmp,
configInfo.getContent(), md5, betaIps, srcIp, srcUser, encryptedDataKey};

EmbeddedStorageContextUtils.onModifyConfigBetaInfo(configInfo, betaIps, srcIp, time);
EmbeddedStorageContextHolder.addSqlContext(sql, args);

databaseOperate.blockUpdate();
return getBetaOperateResult(configInfo.getDataId(), configInfo.getGroup(), tenantTmp);

} finally {
EmbeddedStorageContextHolder.cleanAllContext();
}
Expand Down Expand Up @@ -197,26 +210,29 @@ public ConfigOperateResult updateConfigInfo4Beta(ConfigInfo configInfo, String b
String appNameTmp = StringUtils.defaultEmptyIfBlank(configInfo.getAppName());
String tenantTmp = StringUtils.defaultEmptyIfBlank(configInfo.getTenant());
String encryptedDataKey = StringUtils.defaultEmptyIfBlank(configInfo.getEncryptedDataKey());

configInfo.setTenant(tenantTmp);
try {
String md5 = MD5Utils.md5Hex(configInfo.getContent(), Constants.ENCODE);
ConfigInfoBetaMapper configInfoBetaMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(),
TableConstant.CONFIG_INFO_BETA);
Timestamp time = new Timestamp(System.currentTimeMillis());
final String sql = configInfoBetaMapper.update(
Arrays.asList("content", "md5", "beta_ips", "src_ip", "src_user", "gmt_modified", "app_name",
"encrypted_data_key"), Arrays.asList("data_id", "group_id", "tenant_id"));

final Object[] args = new Object[] {configInfo.getContent(), md5, betaIps, srcIp, srcUser, time, appNameTmp,
Arrays.asList(
ColumnFunctionPair.withColumn("content"),
ColumnFunctionPair.withColumn("md5"),
ColumnFunctionPair.withColumn("beta_ips"),
ColumnFunctionPair.withColumn("src_ip"),
ColumnFunctionPair.withColumn("src_user"),
ColumnFunctionPair.withColumnAndFunction("gmt_modified", TrustedSqlFunctionEnum.CURRENT_TIMESTAMP),
ColumnFunctionPair.withColumn("app_name"),
ColumnFunctionPair.withColumn("encrypted_data_key")),
Arrays.asList("data_id", "group_id", "tenant_id"));
final Object[] args = new Object[]{configInfo.getContent(), md5, betaIps, srcIp, srcUser, appNameTmp,
encryptedDataKey, configInfo.getDataId(), configInfo.getGroup(), tenantTmp};

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

databaseOperate.blockUpdate();
return getBetaOperateResult(configInfo.getDataId(), configInfo.getGroup(), tenantTmp);

} finally {
EmbeddedStorageContextHolder.cleanAllContext();
}
Expand Down
Loading

0 comments on commit 6fe4d0f

Please sign in to comment.