use of com.alibaba.nacos.config.server.model.capacity.TenantCapacity in project nacos by alibaba.
the class CapacityService method updateTenantUsage.
private boolean updateTenantUsage(CounterMode counterMode, String tenant, boolean ignoreQuotaLimit) {
final Timestamp now = TimeUtils.getCurrentTime();
TenantCapacity tenantCapacity = new TenantCapacity();
tenantCapacity.setTenant(tenant);
tenantCapacity.setQuota(PropertyUtil.getDefaultTenantQuota());
tenantCapacity.setGmtModified(now);
if (CounterMode.INCREMENT == counterMode) {
if (ignoreQuotaLimit) {
return tenantCapacityPersistService.incrementUsage(tenantCapacity);
}
// The quota field in the default value table is 0.
return tenantCapacityPersistService.incrementUsageWithDefaultQuotaLimit(tenantCapacity) || tenantCapacityPersistService.incrementUsageWithQuotaLimit(tenantCapacity);
}
return tenantCapacityPersistService.decrementUsage(tenantCapacity);
}
use of com.alibaba.nacos.config.server.model.capacity.TenantCapacity in project nacos by alibaba.
the class CapacityService method correctTenantUsage.
/**
* Correct the usage of group capacity.
*/
private void correctTenantUsage() {
long lastId = 0;
int pageSize = 100;
while (true) {
List<TenantCapacity> tenantCapacityList = tenantCapacityPersistService.getCapacityList4CorrectUsage(lastId, pageSize);
if (tenantCapacityList.isEmpty()) {
break;
}
lastId = tenantCapacityList.get(tenantCapacityList.size() - 1).getId();
try {
Thread.sleep(100);
} catch (InterruptedException ignored) {
}
for (TenantCapacity tenantCapacity : tenantCapacityList) {
String tenant = tenantCapacity.getTenant();
tenantCapacityPersistService.correctUsage(tenant, TimeUtils.getCurrentTime());
}
}
}
use of com.alibaba.nacos.config.server.model.capacity.TenantCapacity in project nacos by alibaba.
the class CapacityService method insertTenantCapacity.
private boolean insertTenantCapacity(String tenant, Integer quota, Integer maxSize, Integer maxAggrCount, Integer maxAggrSize) {
try {
final Timestamp now = TimeUtils.getCurrentTime();
TenantCapacity tenantCapacity = new TenantCapacity();
tenantCapacity.setTenant(tenant);
// When adding a new quota, quota = 0 means that the quota is the default value.
// In order to update the default quota, only the Nacos configuration needs to be modified,
// and most of the data in the table need not be updated.
tenantCapacity.setQuota(quota == null ? ZERO : quota);
// When adding new data, maxsize = 0 means that the size is the default value.
// In order to update the default size, you only need to modify the Nacos configuration without updating most of the data in the table.
tenantCapacity.setMaxSize(maxSize == null ? ZERO : maxSize);
tenantCapacity.setMaxAggrCount(maxAggrCount == null ? ZERO : maxAggrCount);
tenantCapacity.setMaxAggrSize(maxAggrSize == null ? ZERO : maxAggrSize);
tenantCapacity.setGmtCreate(now);
tenantCapacity.setGmtModified(now);
return tenantCapacityPersistService.insertTenantCapacity(tenantCapacity);
} catch (DuplicateKeyException e) {
// this exception will meet when concurrent insert,ignore it
LogUtil.DEFAULT_LOG.warn("tenant: {}, message: {}", tenant, e.getMessage());
}
return false;
}
use of com.alibaba.nacos.config.server.model.capacity.TenantCapacity in project nacos by alibaba.
the class TenantCapacityPersistService method insertTenantCapacity.
/**
* Insert TenantCapacity.
*
* @param tenantCapacity tenantCapacity object instance.
* @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=?;";
try {
GeneratedKeyHolder generatedKeyHolder = new GeneratedKeyHolder();
PreparedStatementCreator preparedStatementCreator = connection -> {
PreparedStatement ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
String tenant = tenantCapacity.getTenant();
ps.setString(1, tenant);
ps.setInt(2, tenantCapacity.getQuota());
ps.setInt(3, tenantCapacity.getMaxSize());
ps.setInt(4, tenantCapacity.getMaxAggrCount());
ps.setInt(5, tenantCapacity.getMaxAggrSize());
ps.setTimestamp(6, tenantCapacity.getGmtCreate());
ps.setTimestamp(7, tenantCapacity.getGmtModified());
ps.setString(8, tenantCapacity.getTenant());
return ps;
};
jdbcTemplate.update(preparedStatementCreator, generatedKeyHolder);
return generatedKeyHolder.getKey() != null;
} catch (CannotGetJdbcConnectionException e) {
FATAL_LOG.error("[db-error]", e);
throw e;
}
}
use of com.alibaba.nacos.config.server.model.capacity.TenantCapacity in project nacos by alibaba.
the class CapacityServiceTest method testInsertAndUpdateTenantUsage.
@Test
public void testInsertAndUpdateTenantUsage() {
TenantCapacity tenantCapacity = new TenantCapacity();
tenantCapacity.setTenant("testTenant");
tenantCapacity.setUsage(300);
when(tenantCapacityPersistService.getTenantCapacity(eq("testTenant"))).thenReturn(tenantCapacity);
when(tenantCapacityPersistService.incrementUsage(any())).thenReturn(true);
when(tenantCapacityPersistService.incrementUsageWithDefaultQuotaLimit(any())).thenReturn(true);
when(tenantCapacityPersistService.decrementUsage(any())).thenReturn(true);
service.insertAndUpdateTenantUsage(CounterMode.INCREMENT, "testTenant", true);
Mockito.verify(tenantCapacityPersistService, times(1)).incrementUsage(any());
service.insertAndUpdateTenantUsage(CounterMode.INCREMENT, "testTenant", false);
Mockito.verify(tenantCapacityPersistService, times(1)).incrementUsageWithDefaultQuotaLimit(any());
service.insertAndUpdateTenantUsage(CounterMode.DECREMENT, "testTenant", true);
Mockito.verify(tenantCapacityPersistService, times(1)).decrementUsage(any());
}
Aggregations