Skip to content

Commit

Permalink
补充完整剩余dataSource的MapperContext改造
Browse files Browse the repository at this point in the history
  • Loading branch information
vividfish committed Mar 20, 2023
1 parent c5c2456 commit 394c4b3
Show file tree
Hide file tree
Showing 57 changed files with 3,774 additions and 1,976 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,17 @@
import com.alibaba.nacos.config.server.service.datasource.DynamicDataSource;
import com.alibaba.nacos.config.server.utils.TimeUtils;
import com.alibaba.nacos.plugin.datasource.MapperManager;
import com.alibaba.nacos.plugin.datasource.constants.FieldConstant;
import com.alibaba.nacos.plugin.datasource.constants.TableConstant;
import com.alibaba.nacos.plugin.datasource.mapper.ConfigInfoMapper;
import com.alibaba.nacos.plugin.datasource.mapper.GroupCapacityMapper;
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.jdbc.CannotGetJdbcConnectionException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
Expand Down Expand Up @@ -71,7 +73,8 @@ public class GroupCapacityPersistService {
public void init() {
this.dataSourceService = DynamicDataSource.getInstance().getDataSource();
this.jdbcTemplate = dataSourceService.getJdbcTemplate();
Boolean isDataSourceLogEnable = EnvUtil.getProperty(Constants.NACOS_PLUGIN_DATASOURCE_LOG, Boolean.class, false);
Boolean isDataSourceLogEnable = EnvUtil.getProperty(Constants.NACOS_PLUGIN_DATASOURCE_LOG, Boolean.class,
false);
this.mapperManager = MapperManager.instance(isDataSourceLogEnable);
}

Expand Down Expand Up @@ -117,41 +120,24 @@ public Capacity getClusterCapacity() {
public boolean insertGroupCapacity(final GroupCapacity capacity) {
GroupCapacityMapper groupCapacityMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(),
TableConstant.GROUP_CAPACITY);
String sql;
MapperResult mapperResult;
MapperContext context = new MapperContext();
context.putUpdateParameter(FieldConstant.GROUP_ID, capacity.getGroup());
context.putUpdateParameter(FieldConstant.QUOTA, capacity.getQuota());
context.putUpdateParameter(FieldConstant.MAX_SIZE, capacity.getMaxSize());
context.putUpdateParameter(FieldConstant.MAX_AGGR_SIZE, capacity.getMaxAggrSize());
context.putUpdateParameter(FieldConstant.MAX_AGGR_COUNT, capacity.getMaxAggrCount());
context.putUpdateParameter(FieldConstant.GMT_CREATE, capacity.getGmtCreate());
context.putUpdateParameter(FieldConstant.GMT_MODIFIED, capacity.getGmtModified());

context.putWhereParameter(FieldConstant.GROUP_ID, capacity.getGroup());
if (CLUSTER.equals(capacity.getGroup())) {
sql = groupCapacityMapper.insertIntoSelect();
mapperResult = groupCapacityMapper.insertIntoSelect(context);
} else {
// Note: add "tenant_id = ''" condition.
sql = groupCapacityMapper.insertIntoSelectByWhere();
}
String[] primaryKeyGeneratedKeys = groupCapacityMapper.getPrimaryKeyGeneratedKeys();
return insertGroupCapacity(sql, capacity, primaryKeyGeneratedKeys);
}

private boolean insertGroupCapacity(final String sql, final GroupCapacity capacity, String[] primaryKeyGeneratedKeys) {
try {
GeneratedKeyHolder generatedKeyHolder = new GeneratedKeyHolder();
PreparedStatementCreator preparedStatementCreator = connection -> {
PreparedStatement ps = connection.prepareStatement(sql, primaryKeyGeneratedKeys);
String group = capacity.getGroup();
ps.setString(1, group);
ps.setInt(2, capacity.getQuota());
ps.setInt(3, capacity.getMaxSize());
ps.setInt(4, capacity.getMaxAggrCount());
ps.setInt(5, capacity.getMaxAggrSize());
ps.setTimestamp(6, capacity.getGmtCreate());
ps.setTimestamp(7, capacity.getGmtModified());
if (!CLUSTER.equals(group)) {
ps.setString(8, group);
}
return ps;
};
jdbcTemplate.update(preparedStatementCreator, generatedKeyHolder);
return generatedKeyHolder.getKey() != null;
} catch (CannotGetJdbcConnectionException e) {
FATAL_LOG.error("[db-error]", e);
throw e;
mapperResult = groupCapacityMapper.insertIntoSelectByWhere(context);
}
return jdbcTemplate.update(mapperResult.getSql(), mapperResult.getParamList().toArray()) > 0;
}

public int getClusterUsage() {
Expand All @@ -178,10 +164,13 @@ public int getClusterUsage() {
public boolean incrementUsageWithDefaultQuotaLimit(GroupCapacity groupCapacity) {
GroupCapacityMapper groupCapacityMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(),
TableConstant.GROUP_CAPACITY);
String sql = groupCapacityMapper.incrementUsageByWhereQuotaEqualZero();
MapperContext context = new MapperContext();
context.putWhereParameter(FieldConstant.GMT_MODIFIED, groupCapacity.getGmtModified());
context.putWhereParameter(FieldConstant.GROUP_ID, groupCapacity.getGroup());
context.putWhereParameter(FieldConstant.QUOTA, groupCapacity.getQuota());
MapperResult mapperResult = groupCapacityMapper.incrementUsageByWhereQuotaEqualZero(context);
try {
int affectRow = jdbcTemplate
.update(sql, groupCapacity.getGmtModified(), groupCapacity.getGroup(), groupCapacity.getQuota());
int affectRow = jdbcTemplate.update(mapperResult.getSql(), mapperResult.getParamList().toArray());
return affectRow == 1;
} catch (CannotGetJdbcConnectionException e) {
FATAL_LOG.error("[db-error]", e);
Expand All @@ -198,9 +187,12 @@ public boolean incrementUsageWithDefaultQuotaLimit(GroupCapacity groupCapacity)
public boolean incrementUsageWithQuotaLimit(GroupCapacity groupCapacity) {
GroupCapacityMapper groupCapacityMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(),
TableConstant.GROUP_CAPACITY);
String sql = groupCapacityMapper.incrementUsageByWhereQuotaNotEqualZero();
MapperContext context = new MapperContext();
context.putUpdateParameter(FieldConstant.GMT_MODIFIED, groupCapacity.getGmtModified());
context.putWhereParameter(FieldConstant.GROUP_ID, groupCapacity.getGroup());
MapperResult mapperResult = groupCapacityMapper.incrementUsageByWhereQuotaNotEqualZero(context);
try {
return jdbcTemplate.update(sql, groupCapacity.getGmtModified(), groupCapacity.getGroup()) == 1;
return jdbcTemplate.update(mapperResult.getSql(), mapperResult.getParamList().toArray()) == 1;
} catch (CannotGetJdbcConnectionException e) {
FATAL_LOG.error("[db-error]", e);
throw e;
Expand All @@ -217,9 +209,12 @@ public boolean incrementUsageWithQuotaLimit(GroupCapacity groupCapacity) {
public boolean incrementUsage(GroupCapacity groupCapacity) {
GroupCapacityMapper groupCapacityMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(),
TableConstant.GROUP_CAPACITY);
String sql = groupCapacityMapper.incrementUsageByWhere();
MapperContext context = new MapperContext();
context.putUpdateParameter(FieldConstant.GMT_MODIFIED, groupCapacity.getGmtModified());
context.putWhereParameter(FieldConstant.GROUP_ID, groupCapacity.getGroup());
MapperResult mapperResult = groupCapacityMapper.incrementUsageByWhere(context);
try {
int affectRow = jdbcTemplate.update(sql, groupCapacity.getGmtModified(), groupCapacity.getGroup());
int affectRow = jdbcTemplate.update(mapperResult.getSql(), mapperResult.getParamList().toArray());
return affectRow == 1;
} catch (CannotGetJdbcConnectionException e) {
FATAL_LOG.error("[db-error]", e);
Expand All @@ -236,9 +231,12 @@ public boolean incrementUsage(GroupCapacity groupCapacity) {
public boolean decrementUsage(GroupCapacity groupCapacity) {
GroupCapacityMapper groupCapacityMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(),
TableConstant.GROUP_CAPACITY);
String sql = groupCapacityMapper.decrementUsageByWhere();
MapperContext context = new MapperContext();
context.putUpdateParameter(FieldConstant.GMT_MODIFIED, groupCapacity.getGmtModified());
context.putWhereParameter(FieldConstant.GROUP_ID, groupCapacity.getGroup());
MapperResult mapperResult = groupCapacityMapper.decrementUsageByWhere(context);
try {
return jdbcTemplate.update(sql, groupCapacity.getGmtModified(), groupCapacity.getGroup()) == 1;
return jdbcTemplate.update(mapperResult.getSql(), mapperResult.getParamList().toArray()) == 1;
} catch (CannotGetJdbcConnectionException e) {
FATAL_LOG.error("[db-error]", e);
throw e;
Expand Down Expand Up @@ -277,11 +275,11 @@ public boolean updateGroupCapacity(String group, Integer quota, Integer maxSize,
}
columnList.add("gmt_modified");
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);
Expand Down Expand Up @@ -311,20 +309,23 @@ public boolean updateMaxSize(String group, Integer maxSize) {
public boolean correctUsage(String group, Timestamp gmtModified) {
GroupCapacityMapper groupCapacityMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(),
TableConstant.GROUP_CAPACITY);
String sql;
MapperResult mapperResult;
MapperContext context = new MapperContext();
context.putWhereParameter(FieldConstant.GMT_MODIFIED, gmtModified);
context.putWhereParameter(FieldConstant.GROUP_ID, group);
if (CLUSTER.equals(group)) {
sql = groupCapacityMapper.updateUsage();
mapperResult = groupCapacityMapper.updateUsage(context);
try {
return jdbcTemplate.update(sql, gmtModified, group) == 1;
return jdbcTemplate.update(mapperResult.getSql(), mapperResult.getParamList().toArray()) == 1;
} catch (CannotGetJdbcConnectionException e) {
FATAL_LOG.error("[db-error]", e);
throw e;
}
} else {
// Note: add "tenant_id = ''" condition.
sql = groupCapacityMapper.updateUsageByWhere();
mapperResult = groupCapacityMapper.updateUsageByWhere(context);
try {
return jdbcTemplate.update(sql, group, gmtModified, group) == 1;
return jdbcTemplate.update(mapperResult.getSql(), mapperResult.getParamList().toArray()) == 1;
} catch (CannotGetJdbcConnectionException e) {
FATAL_LOG.error("[db-error]", e);
throw e;
Expand All @@ -342,16 +343,18 @@ public boolean correctUsage(String group, Timestamp gmtModified) {
public List<GroupCapacity> getCapacityList4CorrectUsage(long lastId, int pageSize) {
GroupCapacityMapper groupCapacityMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(),
TableConstant.GROUP_CAPACITY);
String sql = groupCapacityMapper.selectGroupInfoBySize();

MapperContext context = new MapperContext();
context.putWhereParameter(FieldConstant.ID, lastId);
context.setPageSize(pageSize);

MapperResult mapperResult = groupCapacityMapper.selectGroupInfoBySize(context);
try {
return jdbcTemplate.query(sql, new Object[] {lastId, pageSize}, new RowMapper<GroupCapacity>() {
@Override
public GroupCapacity mapRow(ResultSet rs, int rowNum) throws SQLException {
GroupCapacity groupCapacity = new GroupCapacity();
groupCapacity.setId(rs.getLong("id"));
groupCapacity.setGroup(rs.getString("group_id"));
return groupCapacity;
}
return jdbcTemplate.query(mapperResult.getSql(), mapperResult.getParamList().toArray(), (rs, rowNum) -> {
GroupCapacity groupCapacity = new GroupCapacity();
groupCapacity.setId(rs.getLong("id"));
groupCapacity.setGroup(rs.getString("group_id"));
return groupCapacity;
});
} catch (CannotGetJdbcConnectionException e) {
FATAL_LOG.error("[db-error]", e);
Expand Down
Loading

0 comments on commit 394c4b3

Please sign in to comment.