use of com.alibaba.nacos.config.server.model.capacity.Capacity in project nacos by alibaba.
the class CapacityService method autoExpansion.
/**
* Expand capacity automatically.
*
* @param group group string value.
* @param tenant tenant string value.
*/
private void autoExpansion(String group, String tenant) {
Capacity capacity = getCapacity(group, tenant);
int defaultQuota = getDefaultQuota(tenant != null);
Integer usage = capacity.getUsage();
if (usage < defaultQuota) {
return;
}
// Initialize the capacity information of the group. If the quota is reached,
// the capacity will be automatically expanded to reduce the operation and maintenance cost.
int initialExpansionPercent = PropertyUtil.getInitialExpansionPercent();
if (initialExpansionPercent > 0) {
int finalQuota = (int) (usage + defaultQuota * (1.0 * initialExpansionPercent / 100));
if (tenant != null) {
tenantCapacityPersistService.updateQuota(tenant, finalQuota);
LogUtil.DEFAULT_LOG.warn("[capacityManagement] The usage({}) already reach the upper limit({}) when init the tenant({}), " + "automatic upgrade to ({})", usage, defaultQuota, tenant, finalQuota);
} else {
groupCapacityPersistService.updateQuota(group, finalQuota);
LogUtil.DEFAULT_LOG.warn("[capacityManagement] The usage({}) already reach the upper limit({}) when init the group({}), " + "automatic upgrade to ({})", usage, defaultQuota, group, finalQuota);
}
}
}
use of com.alibaba.nacos.config.server.model.capacity.Capacity in project nacos by alibaba.
the class CapacityService method getCapacityWithDefault.
public Capacity getCapacityWithDefault(String group, String tenant) {
Capacity capacity;
boolean isTenant = StringUtils.isNotBlank(tenant);
if (isTenant) {
capacity = getTenantCapacity(tenant);
} else {
capacity = getGroupCapacity(group);
}
if (capacity == null) {
return null;
}
Integer quota = capacity.getQuota();
if (quota == 0) {
if (isTenant) {
capacity.setQuota(PropertyUtil.getDefaultTenantQuota());
} else {
if (GroupCapacityPersistService.CLUSTER.equals(group)) {
capacity.setQuota(PropertyUtil.getDefaultClusterQuota());
} else {
capacity.setQuota(PropertyUtil.getDefaultGroupQuota());
}
}
}
Integer maxSize = capacity.getMaxSize();
if (maxSize == 0) {
capacity.setMaxSize(PropertyUtil.getDefaultMaxSize());
}
Integer maxAggrCount = capacity.getMaxAggrCount();
if (maxAggrCount == 0) {
capacity.setMaxAggrCount(PropertyUtil.getDefaultMaxAggrCount());
}
Integer maxAggrSize = capacity.getMaxAggrSize();
if (maxAggrSize == 0) {
capacity.setMaxAggrSize(PropertyUtil.getDefaultMaxAggrSize());
}
return capacity;
}
use of com.alibaba.nacos.config.server.model.capacity.Capacity in project nacos by alibaba.
the class GroupCapacityPersistService method insertGroupCapacity.
private boolean insertGroupCapacity(final String sql, final GroupCapacity capacity) {
try {
GeneratedKeyHolder generatedKeyHolder = new GeneratedKeyHolder();
PreparedStatementCreator preparedStatementCreator = connection -> {
PreparedStatement ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
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;
}
}
use of com.alibaba.nacos.config.server.model.capacity.Capacity in project nacos by alibaba.
the class CapacityManagementAspect method do4Update.
/**
* Update operation: open the limitation of capacity management and it will check the size of content.
*
* @throws Throwable Throws Exception when actually operate.
*/
private Object do4Update(ProceedingJoinPoint pjp, HttpServletRequest request, HttpServletResponse response, String dataId, String group, String tenant, String content) throws Throwable {
if (!PropertyUtil.isCapacityLimitCheck()) {
return pjp.proceed();
}
try {
boolean hasTenant = hasTenant(tenant);
Capacity capacity = getCapacity(group, tenant, hasTenant);
if (isSizeLimited(group, tenant, getCurrentSize(content), hasTenant, false, capacity)) {
return response4Limit(request, response, LimitType.OVER_MAX_SIZE);
}
} catch (Exception e) {
LOGGER.error("[capacityManagement] do4Update ", e);
}
return pjp.proceed();
}
use of com.alibaba.nacos.config.server.model.capacity.Capacity in project nacos by alibaba.
the class CapacityManagementAspect method getGroupOrTenantLimitType.
private LimitType getGroupOrTenantLimitType(CounterMode counterMode, String group, String tenant, int currentSize, boolean hasTenant) {
if (group == null) {
return null;
}
Capacity capacity = getCapacity(group, tenant, hasTenant);
if (isSizeLimited(group, tenant, currentSize, hasTenant, false, capacity)) {
return LimitType.OVER_MAX_SIZE;
}
if (capacity == null) {
insertCapacity(group, tenant, hasTenant);
}
boolean updateSuccess = isUpdateSuccess(counterMode, group, tenant, hasTenant);
if (updateSuccess) {
return null;
}
if (hasTenant) {
return LimitType.OVER_TENANT_QUOTA;
}
return LimitType.OVER_GROUP_QUOTA;
}
Aggregations