[ISSUE #9449]Add Tenant Capacity mapper. (#9456)

This commit is contained in:
The-Gamer-01 2022-11-05 00:44:21 +08:00 committed by GitHub
parent d5fb300dd2
commit ff2497d999
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 307 additions and 32 deletions

View File

@ -20,8 +20,11 @@ import com.alibaba.nacos.common.utils.CollectionUtils;
import com.alibaba.nacos.config.server.model.capacity.TenantCapacity;
import com.alibaba.nacos.config.server.service.datasource.DataSourceService;
import com.alibaba.nacos.config.server.service.datasource.DynamicDataSource;
import com.alibaba.nacos.config.server.utils.PropertyUtil;
import com.alibaba.nacos.config.server.utils.TimeUtils;
import com.alibaba.nacos.plugin.datasource.MapperManager;
import com.alibaba.nacos.plugin.datasource.constants.TableConstant;
import com.alibaba.nacos.plugin.datasource.mapper.TenantCapacityMapper;
import com.alibaba.nacos.sys.env.EnvUtil;
import org.springframework.jdbc.CannotGetJdbcConnectionException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
@ -35,6 +38,9 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static com.alibaba.nacos.config.server.utils.LogUtil.FATAL_LOG;
@ -54,10 +60,23 @@ public class TenantCapacityPersistService {
private DataSourceService dataSourceService;
private MapperManager mapperManager;
private String dataSource;
private static final String DATASOURCE_PLATFORM_PROPERTY = "spring.datasource.platform";
private static final String DEFAULT_DATASOURCE_PLATFORM = "derby";
/**
* init method.
*/
@PostConstruct
public void init() {
this.dataSourceService = DynamicDataSource.getInstance().getDataSource();
this.jdbcTemplate = dataSourceService.getJdbcTemplate();
this.mapperManager = MapperManager.instance();
this.dataSource = EnvUtil.getProperty(DATASOURCE_PLATFORM_PROPERTY, DEFAULT_DATASOURCE_PLATFORM);
}
private static final class TenantCapacityRowMapper implements RowMapper<TenantCapacity> {
@ -77,9 +96,9 @@ public class TenantCapacityPersistService {
}
public TenantCapacity getTenantCapacity(String tenantId) {
String sql =
"SELECT id, quota, `usage`, `max_size`, max_aggr_count, max_aggr_size, tenant_id FROM tenant_capacity "
+ "WHERE tenant_id=?";
TenantCapacityMapper tenantCapacityMapper = mapperManager.findMapper(dataSource, TableConstant.TENANT_CAPACITY);
String sql = tenantCapacityMapper.select(Arrays.asList("id", "quota", "`usage`",
"`max_size`", "max_aggr_count", "max_aggr_size", "tenant_id"), Collections.singletonList("tenant_id"));
List<TenantCapacity> list = jdbcTemplate.query(sql, new Object[] {tenantId}, TENANT_CAPACITY_ROW_MAPPER);
if (list.isEmpty()) {
return null;
@ -94,9 +113,8 @@ public class TenantCapacityPersistService {
* @return operate result.
*/
public boolean insertTenantCapacity(final TenantCapacity tenantCapacity) {
final String sql =
"INSERT INTO tenant_capacity (tenant_id, quota, `usage`, `max_size`, max_aggr_count, max_aggr_size, "
+ "gmt_create, gmt_modified) SELECT ?, ?, count(*), ?, ?, ?, ?, ? FROM config_info WHERE tenant_id=?;";
TenantCapacityMapper tenantCapacityMapper = mapperManager.findMapper(dataSource, TableConstant.TENANT_CAPACITY);
final String sql = tenantCapacityMapper.insertTenantCapacity();
try {
GeneratedKeyHolder generatedKeyHolder = new GeneratedKeyHolder();
PreparedStatementCreator preparedStatementCreator = connection -> {
@ -128,9 +146,9 @@ public class TenantCapacityPersistService {
* @return operate result.
*/
public boolean incrementUsageWithDefaultQuotaLimit(TenantCapacity tenantCapacity) {
String sql =
"UPDATE tenant_capacity SET `usage` = `usage` + 1, gmt_modified = ? WHERE tenant_id = ? AND `usage` <"
+ " ? AND quota = 0";
TenantCapacityMapper tenantCapacityMapper = mapperManager.findMapper(dataSource, TableConstant.TENANT_CAPACITY);
String sql = tenantCapacityMapper.incrementUsageWithDefaultQuotaLimit();
try {
int affectRow = jdbcTemplate.update(sql, tenantCapacity.getGmtModified(), tenantCapacity.getTenant(),
tenantCapacity.getQuota());
@ -148,9 +166,8 @@ public class TenantCapacityPersistService {
* @return operate result.
*/
public boolean incrementUsageWithQuotaLimit(TenantCapacity tenantCapacity) {
String sql =
"UPDATE tenant_capacity SET `usage` = `usage` + 1, gmt_modified = ? WHERE tenant_id = ? AND `usage` < "
+ "quota AND quota != 0";
TenantCapacityMapper tenantCapacityMapper = mapperManager.findMapper(dataSource, TableConstant.TENANT_CAPACITY);
String sql = tenantCapacityMapper.incrementUsageWithQuotaLimit();
try {
return jdbcTemplate.update(sql, tenantCapacity.getGmtModified(), tenantCapacity.getTenant()) == 1;
} catch (CannotGetJdbcConnectionException e) {
@ -167,7 +184,8 @@ public class TenantCapacityPersistService {
* @return operate result.
*/
public boolean incrementUsage(TenantCapacity tenantCapacity) {
String sql = "UPDATE tenant_capacity SET `usage` = `usage` + 1, gmt_modified = ? WHERE tenant_id = ?";
TenantCapacityMapper tenantCapacityMapper = mapperManager.findMapper(dataSource, TableConstant.TENANT_CAPACITY);
String sql = tenantCapacityMapper.incrementUsage();
try {
int affectRow = jdbcTemplate.update(sql, tenantCapacity.getGmtModified(), tenantCapacity.getTenant());
return affectRow == 1;
@ -184,7 +202,8 @@ public class TenantCapacityPersistService {
* @return operate result.
*/
public boolean decrementUsage(TenantCapacity tenantCapacity) {
String sql = "UPDATE tenant_capacity SET `usage` = `usage` - 1, gmt_modified = ? WHERE tenant_id = ? AND `usage` > 0";
TenantCapacityMapper tenantCapacityMapper = mapperManager.findMapper(dataSource, TableConstant.TENANT_CAPACITY);
String sql = tenantCapacityMapper.decrementUsage();
try {
return jdbcTemplate.update(sql, tenantCapacity.getGmtModified(), tenantCapacity.getTenant()) == 1;
} catch (CannotGetJdbcConnectionException e) {
@ -206,30 +225,36 @@ public class TenantCapacityPersistService {
public boolean updateTenantCapacity(String tenant, Integer quota, Integer maxSize, Integer maxAggrCount,
Integer maxAggrSize) {
List<Object> argList = CollectionUtils.list();
StringBuilder sql = new StringBuilder("UPDATE tenant_capacity SET");
List<String> columns = new ArrayList<>();
if (quota != null) {
sql.append(" quota = ?,");
columns.add("quota");
argList.add(quota);
}
if (maxSize != null) {
sql.append(" max_size = ?,");
columns.add("max_size");
argList.add(maxSize);
}
if (maxAggrCount != null) {
sql.append(" max_aggr_count = ?,");
columns.add("max_aggr_count");
argList.add(maxAggrCount);
}
if (maxAggrSize != null) {
sql.append(" max_aggr_size = ?,");
columns.add("max_aggr_size");
argList.add(maxAggrSize);
}
sql.append(" gmt_modified = ?");
columns.add("gmt_modified");
argList.add(TimeUtils.getCurrentTime());
sql.append(" WHERE tenant_id = ?");
List<String> where = new ArrayList<>();
where.add("tenant_id");
argList.add(tenant);
TenantCapacityMapper tenantCapacityMapper = mapperManager.findMapper(dataSource, TableConstant.TENANT_CAPACITY);
String sql = tenantCapacityMapper.update(columns, where);
try {
return jdbcTemplate.update(sql.toString(), argList.toArray()) == 1;
return jdbcTemplate.update(sql, argList.toArray()) == 1;
} catch (CannotGetJdbcConnectionException e) {
FATAL_LOG.error("[db-error]", e);
throw e;
@ -248,8 +273,8 @@ public class TenantCapacityPersistService {
* @return operate result.
*/
public boolean correctUsage(String tenant, Timestamp gmtModified) {
String sql = "UPDATE tenant_capacity SET `usage` = (SELECT count(*) FROM config_info WHERE tenant_id = ?), "
+ "gmt_modified = ? WHERE tenant_id = ?";
TenantCapacityMapper tenantCapacityMapper = mapperManager.findMapper(dataSource, TableConstant.TENANT_CAPACITY);
String sql = tenantCapacityMapper.correctUsage();
try {
return jdbcTemplate.update(sql, tenant, gmtModified, tenant) == 1;
} catch (CannotGetJdbcConnectionException e) {
@ -266,11 +291,8 @@ public class TenantCapacityPersistService {
* @return TenantCapacity List.
*/
public List<TenantCapacity> getCapacityList4CorrectUsage(long lastId, int pageSize) {
String sql = "SELECT id, tenant_id FROM tenant_capacity WHERE id>? LIMIT ?";
if (PropertyUtil.isEmbeddedStorage()) {
sql = "SELECT id, tenant_id FROM tenant_capacity WHERE id>? OFFSET 0 ROWS FETCH NEXT ? ROWS ONLY";
}
TenantCapacityMapper tenantCapacityMapper = mapperManager.findMapper(dataSource, TableConstant.TENANT_CAPACITY);
String sql = tenantCapacityMapper.getCapacityList4CorrectUsage();
try {
return jdbcTemplate.query(sql, new Object[] {lastId, pageSize}, new RowMapper<TenantCapacity>() {
@ -296,9 +318,11 @@ public class TenantCapacityPersistService {
*/
public boolean deleteTenantCapacity(final String tenant) {
try {
TenantCapacityMapper tenantCapacityMapper = mapperManager.findMapper(dataSource,
TableConstant.TENANT_CAPACITY);
PreparedStatementCreator preparedStatementCreator = connection -> {
PreparedStatement ps = connection
.prepareStatement("DELETE FROM tenant_capacity WHERE tenant_id = ?;");
.prepareStatement(tenantCapacityMapper.delete(Collections.singletonList("tenant_id")));
ps.setString(1, tenant);
return ps;
};

View File

@ -0,0 +1,80 @@
/*
* Copyright 1999-2022 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.nacos.plugin.datasource.impl.derby;
import com.alibaba.nacos.plugin.datasource.constants.DataSourceConstant;
import com.alibaba.nacos.plugin.datasource.constants.TableConstant;
import com.alibaba.nacos.plugin.datasource.mapper.AbstractMapper;
import com.alibaba.nacos.plugin.datasource.mapper.TenantCapacityMapper;
/**
* The derby implementation of TenantCapacityMapper.
*
* @author hyx
**/
public class TenantCapacityMapperByDerby extends AbstractMapper implements TenantCapacityMapper {
@Override
public String getTableName() {
return TableConstant.TENANT_CAPACITY;
}
@Override
public String getDataSource() {
return DataSourceConstant.DERBY;
}
@Override
public String incrementUsageWithDefaultQuotaLimit() {
return "UPDATE tenant_capacity SET `usage` = `usage` + 1, gmt_modified = ? WHERE tenant_id = ? AND `usage` <"
+ " ? AND quota = 0";
}
@Override
public String incrementUsageWithQuotaLimit() {
return "UPDATE tenant_capacity SET `usage` = `usage` + 1, gmt_modified = ? WHERE tenant_id = ? AND `usage` < "
+ "quota AND quota != 0";
}
@Override
public String incrementUsage() {
return "UPDATE tenant_capacity SET `usage` = `usage` + 1, gmt_modified = ? WHERE tenant_id = ?";
}
@Override
public String decrementUsage() {
return "UPDATE tenant_capacity SET `usage` = `usage` - 1, gmt_modified = ? WHERE tenant_id = ? AND `usage` > 0";
}
@Override
public String correctUsage() {
return "UPDATE tenant_capacity SET `usage` = (SELECT count(*) FROM config_info WHERE tenant_id = ?), "
+ "gmt_modified = ? WHERE tenant_id = ?";
}
@Override
public String getCapacityList4CorrectUsage() {
return "SELECT id, tenant_id FROM tenant_capacity WHERE id>? OFFSET 0 ROWS FETCH NEXT ? ROWS ONLY";
}
@Override
public String insertTenantCapacity() {
return "INSERT INTO tenant_capacity (tenant_id, quota, `usage`, `max_size`, max_aggr_count, max_aggr_size, "
+ "gmt_create, gmt_modified) SELECT ?, ?, count(*), ?, ?, ?, ?, ? FROM config_info WHERE tenant_id=?;";
}
}

View File

@ -0,0 +1,80 @@
/*
* Copyright 1999-2022 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.nacos.plugin.datasource.impl.mysql;
import com.alibaba.nacos.plugin.datasource.constants.DataSourceConstant;
import com.alibaba.nacos.plugin.datasource.constants.TableConstant;
import com.alibaba.nacos.plugin.datasource.mapper.AbstractMapper;
import com.alibaba.nacos.plugin.datasource.mapper.TenantCapacityMapper;
/**
* The mysql implementation of TenantCapacityMapper.
*
* @author hyx
**/
public class TenantCapacityMapperByMySql extends AbstractMapper implements TenantCapacityMapper {
@Override
public String getTableName() {
return TableConstant.TENANT_CAPACITY;
}
@Override
public String getDataSource() {
return DataSourceConstant.MYSQL;
}
@Override
public String incrementUsageWithDefaultQuotaLimit() {
return "UPDATE tenant_capacity SET `usage` = `usage` + 1, gmt_modified = ? WHERE tenant_id = ? AND `usage` <"
+ " ? AND quota = 0";
}
@Override
public String incrementUsageWithQuotaLimit() {
return "UPDATE tenant_capacity SET `usage` = `usage` + 1, gmt_modified = ? WHERE tenant_id = ? AND `usage` < "
+ "quota AND quota != 0";
}
@Override
public String incrementUsage() {
return "UPDATE tenant_capacity SET `usage` = `usage` + 1, gmt_modified = ? WHERE tenant_id = ?";
}
@Override
public String decrementUsage() {
return "UPDATE tenant_capacity SET `usage` = `usage` - 1, gmt_modified = ? WHERE tenant_id = ? AND `usage` > 0";
}
@Override
public String correctUsage() {
return "UPDATE tenant_capacity SET `usage` = (SELECT count(*) FROM config_info WHERE tenant_id = ?), "
+ "gmt_modified = ? WHERE tenant_id = ?";
}
@Override
public String getCapacityList4CorrectUsage() {
return "SELECT id, tenant_id FROM tenant_capacity WHERE id>? LIMIT ?";
}
@Override
public String insertTenantCapacity() {
return "INSERT INTO tenant_capacity (tenant_id, quota, `usage`, `max_size`, max_aggr_count, max_aggr_size, "
+ "gmt_create, gmt_modified) SELECT ?, ?, count(*), ?, ?, ?, ?, ? FROM config_info WHERE tenant_id=?;";
}
}

View File

@ -0,0 +1,89 @@
/*
* Copyright 1999-2022 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.nacos.plugin.datasource.mapper;
/**
* The tenant capacity info mapper.
*
* @author hyx
**/
public interface TenantCapacityMapper extends Mapper {
/**
* Increment UsageWithDefaultQuotaLimit.
* The default sql:
* UPDATE tenant_capacity SET `usage` = `usage` + 1, gmt_modified = ? WHERE tenant_id = ? AND `usage` < ? AND quota = 0
*
* @return The sql of increment UsageWithDefaultQuotaLimit.
*/
String incrementUsageWithDefaultQuotaLimit();
/**
* Increment UsageWithQuotaLimit.
* The default sql:
* UPDATE tenant_capacity SET `usage` = `usage` + 1, gmt_modified = ? WHERE tenant_id = ? AND `usage` < quota AND quota != 0
*
* @return The sql of Increment UsageWithQuotaLimit.
*/
String incrementUsageWithQuotaLimit();
/**
* Increment Usage.
* The default sql:
* UPDATE tenant_capacity SET `usage` = `usage` + 1, gmt_modified = ? WHERE tenant_id = ?
*
* @return The sql of increment UsageWithQuotaLimit.
*/
String incrementUsage();
/**
* DecrementUsage.
* The default sql:
* UPDATE tenant_capacity SET `usage` = `usage` - 1, gmt_modified = ? WHERE tenant_id = ? AND `usage` > 0
*
* @return The sql of decrementUsage.
*/
String decrementUsage();
/**
* Correct Usage.
* The default sql:
* UPDATE tenant_capacity SET `usage` = (SELECT count(*) FROM config_info WHERE tenant_id = ?), gmt_modified = ? WHERE tenant_id = ?
*
* @return The sql of correcting Usage.
*/
String correctUsage();
/**
* Get TenantCapacity List, only including id and tenantId value.
* The default sql:
* SELECT id, tenant_id FROM tenant_capacity WHERE id>? LIMIT ?
*
* @return The sql of getting TenantCapacity List, only including id and tenantId value.
*/
String getCapacityList4CorrectUsage();
/**
* Insert TenantCapacity.
* The default sql:
* INSERT INTO tenant_capacity (tenant_id, quota, `usage`, `max_size`, max_aggr_count, max_aggr_size,
* gmt_create, gmt_modified) SELECT ?, ?, count(*), ?, ?, ?, ?, ? FROM config_info WHERE tenant_id=?;
* @return The sql of inserting TenantCapacity.
*/
String insertTenantCapacity();
}

View File

@ -21,6 +21,7 @@ com.alibaba.nacos.plugin.datasource.impl.mysql.ConfigInfoTagMapperByMySql
com.alibaba.nacos.plugin.datasource.impl.mysql.ConfigTagsRelationMapperByMySql
com.alibaba.nacos.plugin.datasource.impl.mysql.HistoryConfigInfoMapperByMySql
com.alibaba.nacos.plugin.datasource.impl.mysql.TenantInfoMapperByMySql
com.alibaba.nacos.plugin.datasource.impl.mysql.TenantCapacityMapperByMySql
com.alibaba.nacos.plugin.datasource.impl.derby.ConfigInfoAggrMapperByDerby
com.alibaba.nacos.plugin.datasource.impl.derby.ConfigInfoBetaMapperByDerby
@ -29,3 +30,4 @@ com.alibaba.nacos.plugin.datasource.impl.derby.ConfigInfoTagMapperByDerby
com.alibaba.nacos.plugin.datasource.impl.derby.ConfigInfoTagsRelationMapperByDerby
com.alibaba.nacos.plugin.datasource.impl.derby.HistoryConfigInfoMapperByDerby
com.alibaba.nacos.plugin.datasource.impl.derby.TenantInfoMapperByDerby
com.alibaba.nacos.plugin.datasource.impl.derby.TenantCapacityMapperByDerby