Search in sources :

Example 1 with TenantCapacity

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

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

Example 3 with TenantCapacity

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;
}
Also used : TenantCapacity(com.alibaba.nacos.config.server.model.capacity.TenantCapacity) Timestamp(java.sql.Timestamp) DuplicateKeyException(org.springframework.dao.DuplicateKeyException)

Example 4 with TenantCapacity

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;
    }
}
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) TenantCapacity(com.alibaba.nacos.config.server.model.capacity.TenantCapacity) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) GeneratedKeyHolder(org.springframework.jdbc.support.GeneratedKeyHolder) SQLException(java.sql.SQLException) List(java.util.List) 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) 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 5 with TenantCapacity

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

Aggregations

TenantCapacity (com.alibaba.nacos.config.server.model.capacity.TenantCapacity)21 Test (org.junit.Test)17 Timestamp (java.sql.Timestamp)7 GroupCapacity (com.alibaba.nacos.config.server.model.capacity.GroupCapacity)6 ArrayList (java.util.ArrayList)5 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)3 RowMapper (org.springframework.jdbc.core.RowMapper)3 Capacity (com.alibaba.nacos.config.server.model.capacity.Capacity)2 PreparedStatementCreator (org.springframework.jdbc.core.PreparedStatementCreator)2 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 HashMap (java.util.HashMap)1