Search in sources :

Example 1 with GroupCapacity

use of com.alibaba.nacos.config.server.model.capacity.GroupCapacity in project nacos by alibaba.

the class CapacityService method insertGroupCapacity.

private boolean insertGroupCapacity(String group, Integer quota, Integer maxSize, Integer maxAggrCount, Integer maxAggrSize) {
    try {
        final Timestamp now = TimeUtils.getCurrentTime();
        GroupCapacity groupCapacity = new GroupCapacity();
        groupCapacity.setGroup(group);
        // 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.
        groupCapacity.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.
        groupCapacity.setMaxSize(maxSize == null ? ZERO : maxSize);
        groupCapacity.setMaxAggrCount(maxAggrCount == null ? ZERO : maxAggrCount);
        groupCapacity.setMaxAggrSize(maxAggrSize == null ? ZERO : maxAggrSize);
        groupCapacity.setGmtCreate(now);
        groupCapacity.setGmtModified(now);
        return groupCapacityPersistService.insertGroupCapacity(groupCapacity);
    } catch (DuplicateKeyException e) {
        // this exception will meet when concurrent insert,ignore it
        LogUtil.DEFAULT_LOG.warn("group: {}, message: {}", group, e.getMessage());
    }
    return false;
}
Also used : GroupCapacity(com.alibaba.nacos.config.server.model.capacity.GroupCapacity) Timestamp(java.sql.Timestamp) DuplicateKeyException(org.springframework.dao.DuplicateKeyException)

Example 2 with GroupCapacity

use of com.alibaba.nacos.config.server.model.capacity.GroupCapacity in project nacos by alibaba.

the class CapacityService method correctGroupUsage.

/**
 * Correct the usage of group capacity.
 */
private void correctGroupUsage() {
    long lastId = 0;
    int pageSize = 100;
    while (true) {
        List<GroupCapacity> groupCapacityList = groupCapacityPersistService.getCapacityList4CorrectUsage(lastId, pageSize);
        if (groupCapacityList.isEmpty()) {
            break;
        }
        lastId = groupCapacityList.get(groupCapacityList.size() - 1).getId();
        for (GroupCapacity groupCapacity : groupCapacityList) {
            String group = groupCapacity.getGroup();
            groupCapacityPersistService.correctUsage(group, TimeUtils.getCurrentTime());
        }
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            // ignore
            // set the interrupted flag
            Thread.currentThread().interrupt();
        }
    }
}
Also used : GroupCapacity(com.alibaba.nacos.config.server.model.capacity.GroupCapacity)

Example 3 with GroupCapacity

use of com.alibaba.nacos.config.server.model.capacity.GroupCapacity 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 GroupCapacity

use of com.alibaba.nacos.config.server.model.capacity.GroupCapacity in project nacos by alibaba.

the class CapacityServiceTest method testGetCapacity.

@Test
public void testGetCapacity() {
    GroupCapacity groupCapacity = new GroupCapacity();
    groupCapacity.setId(1L);
    when(groupCapacityPersistService.getGroupCapacity(eq("testGroup"))).thenReturn(groupCapacity);
    TenantCapacity tenantCapacity = new TenantCapacity();
    tenantCapacity.setId(2L);
    when(tenantCapacityPersistService.getTenantCapacity(eq("testTenant"))).thenReturn(tenantCapacity);
    Capacity resCapacity1 = service.getCapacity("testGroup", null);
    Assert.assertEquals(1L, resCapacity1.getId().longValue());
    Capacity resCapacity2 = service.getCapacity(null, "testTenant");
    Assert.assertEquals(2L, resCapacity2.getId().longValue());
}
Also used : TenantCapacity(com.alibaba.nacos.config.server.model.capacity.TenantCapacity) GroupCapacity(com.alibaba.nacos.config.server.model.capacity.GroupCapacity) 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) Test(org.junit.Test)

Example 5 with GroupCapacity

use of com.alibaba.nacos.config.server.model.capacity.GroupCapacity in project nacos by alibaba.

the class CapacityServiceTest method testInitAllCapacity.

@Test
public void testInitAllCapacity() {
    List<String> groupList = new ArrayList<>();
    groupList.add("testGroup");
    when(persistService.getGroupIdList(eq(1), eq(500))).thenReturn(groupList);
    List<String> tenantList = new ArrayList<>();
    tenantList.add("testTenant");
    when(persistService.getTenantIdList(eq(1), eq(500))).thenReturn(tenantList);
    GroupCapacity groupCapacity = new GroupCapacity();
    groupCapacity.setGroup("testGroup");
    groupCapacity.setUsage(300);
    when(groupCapacityPersistService.insertGroupCapacity(any())).thenReturn(true);
    when(groupCapacityPersistService.getGroupCapacity(eq("testGroup"))).thenReturn(groupCapacity);
    when(groupCapacityPersistService.updateQuota(eq("testGroup"), eq(500))).thenReturn(true);
    TenantCapacity tenantCapacity = new TenantCapacity();
    tenantCapacity.setTenant("testTenant");
    tenantCapacity.setUsage(300);
    when(tenantCapacityPersistService.insertTenantCapacity(any())).thenReturn(true);
    when(tenantCapacityPersistService.getTenantCapacity(eq("testTenant"))).thenReturn(tenantCapacity);
    when(tenantCapacityPersistService.updateQuota(eq("testTenant"), eq(500))).thenReturn(true);
    service.initAllCapacity();
    Mockito.verify(groupCapacityPersistService, times(1)).insertGroupCapacity(any());
    Mockito.verify(groupCapacityPersistService, times(1)).getGroupCapacity(eq("testGroup"));
    Mockito.verify(groupCapacityPersistService, times(1)).updateQuota(eq("testGroup"), eq(500));
    Mockito.verify(tenantCapacityPersistService, times(1)).insertTenantCapacity(any());
    Mockito.verify(tenantCapacityPersistService, times(1)).getTenantCapacity(eq("testTenant"));
    Mockito.verify(tenantCapacityPersistService, times(1)).updateQuota(eq("testTenant"), eq(500));
}
Also used : TenantCapacity(com.alibaba.nacos.config.server.model.capacity.TenantCapacity) GroupCapacity(com.alibaba.nacos.config.server.model.capacity.GroupCapacity) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Aggregations

GroupCapacity (com.alibaba.nacos.config.server.model.capacity.GroupCapacity)22 Test (org.junit.Test)18 Timestamp (java.sql.Timestamp)7 ArrayList (java.util.ArrayList)7 TenantCapacity (com.alibaba.nacos.config.server.model.capacity.TenantCapacity)6 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)5 RowMapper (org.springframework.jdbc.core.RowMapper)5 Capacity (com.alibaba.nacos.config.server.model.capacity.Capacity)4 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