Search in sources :

Example 1 with Capacity

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);
        }
    }
}
Also used : TenantCapacity(com.alibaba.nacos.config.server.model.capacity.TenantCapacity) Capacity(com.alibaba.nacos.config.server.model.capacity.Capacity) GroupCapacity(com.alibaba.nacos.config.server.model.capacity.GroupCapacity)

Example 2 with Capacity

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;
}
Also used : TenantCapacity(com.alibaba.nacos.config.server.model.capacity.TenantCapacity) Capacity(com.alibaba.nacos.config.server.model.capacity.Capacity) GroupCapacity(com.alibaba.nacos.config.server.model.capacity.GroupCapacity)

Example 3 with 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;
    }
}
Also used : GeneratedKeyHolder(org.springframework.jdbc.support.GeneratedKeyHolder) CollectionUtils(com.alibaba.nacos.common.utils.CollectionUtils) PropertyUtil(com.alibaba.nacos.config.server.utils.PropertyUtil) CannotGetJdbcConnectionException(org.springframework.jdbc.CannotGetJdbcConnectionException) TimeUtils(com.alibaba.nacos.config.server.utils.TimeUtils) DynamicDataSource(com.alibaba.nacos.config.server.service.datasource.DynamicDataSource) Timestamp(java.sql.Timestamp) PreparedStatementCreator(org.springframework.jdbc.core.PreparedStatementCreator) PreparedStatement(java.sql.PreparedStatement) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) GeneratedKeyHolder(org.springframework.jdbc.support.GeneratedKeyHolder) SQLException(java.sql.SQLException) List(java.util.List) Capacity(com.alibaba.nacos.config.server.model.capacity.Capacity) Service(org.springframework.stereotype.Service) ResultSet(java.sql.ResultSet) FATAL_LOG(com.alibaba.nacos.config.server.utils.LogUtil.FATAL_LOG) RowMapper(org.springframework.jdbc.core.RowMapper) PostConstruct(javax.annotation.PostConstruct) Statement(java.sql.Statement) GroupCapacity(com.alibaba.nacos.config.server.model.capacity.GroupCapacity) DataSourceService(com.alibaba.nacos.config.server.service.datasource.DataSourceService) CannotGetJdbcConnectionException(org.springframework.jdbc.CannotGetJdbcConnectionException) PreparedStatementCreator(org.springframework.jdbc.core.PreparedStatementCreator) PreparedStatement(java.sql.PreparedStatement)

Example 4 with Capacity

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();
}
Also used : Capacity(com.alibaba.nacos.config.server.model.capacity.Capacity)

Example 5 with Capacity

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;
}
Also used : Capacity(com.alibaba.nacos.config.server.model.capacity.Capacity)

Aggregations

Capacity (com.alibaba.nacos.config.server.model.capacity.Capacity)11 GroupCapacity (com.alibaba.nacos.config.server.model.capacity.GroupCapacity)7 TenantCapacity (com.alibaba.nacos.config.server.model.capacity.TenantCapacity)4 Test (org.junit.Test)4 Timestamp (java.sql.Timestamp)2 RowMapper (org.springframework.jdbc.core.RowMapper)2 RestResult (com.alibaba.nacos.common.model.RestResult)1 CollectionUtils (com.alibaba.nacos.common.utils.CollectionUtils)1 DataSourceService (com.alibaba.nacos.config.server.service.datasource.DataSourceService)1 DynamicDataSource (com.alibaba.nacos.config.server.service.datasource.DynamicDataSource)1 FATAL_LOG (com.alibaba.nacos.config.server.utils.LogUtil.FATAL_LOG)1 PropertyUtil (com.alibaba.nacos.config.server.utils.PropertyUtil)1 TimeUtils (com.alibaba.nacos.config.server.utils.TimeUtils)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 PostConstruct (javax.annotation.PostConstruct)1