Search in sources :

Example 11 with GroupCapacity

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

the class CapacityService method updateGroupUsage.

private boolean updateGroupUsage(CounterMode counterMode, String group, int defaultQuota, boolean ignoreQuotaLimit) {
    final Timestamp now = TimeUtils.getCurrentTime();
    GroupCapacity groupCapacity = new GroupCapacity();
    groupCapacity.setGroup(group);
    groupCapacity.setQuota(defaultQuota);
    groupCapacity.setGmtModified(now);
    if (CounterMode.INCREMENT == counterMode) {
        if (ignoreQuotaLimit) {
            return groupCapacityPersistService.incrementUsage(groupCapacity);
        }
        // The quota field in the default value table is 0
        return groupCapacityPersistService.incrementUsageWithDefaultQuotaLimit(groupCapacity) || groupCapacityPersistService.incrementUsageWithQuotaLimit(groupCapacity);
    }
    return groupCapacityPersistService.decrementUsage(groupCapacity);
}
Also used : GroupCapacity(com.alibaba.nacos.config.server.model.capacity.GroupCapacity) Timestamp(java.sql.Timestamp)

Example 12 with GroupCapacity

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

the class CapacityServiceTest method testInsertAndUpdateGroupUsage.

@Test
public void testInsertAndUpdateGroupUsage() {
    GroupCapacity groupCapacity = new GroupCapacity();
    groupCapacity.setGroup("testGroup");
    groupCapacity.setUsage(300);
    when(groupCapacityPersistService.getGroupCapacity("testGroup")).thenReturn(groupCapacity);
    when(groupCapacityPersistService.incrementUsage(any())).thenReturn(true);
    when(groupCapacityPersistService.incrementUsageWithDefaultQuotaLimit(any())).thenReturn(true);
    when(groupCapacityPersistService.decrementUsage(any())).thenReturn(true);
    service.insertAndUpdateGroupUsage(CounterMode.INCREMENT, "testGroup", true);
    Mockito.verify(groupCapacityPersistService, times(1)).incrementUsage(any());
    service.insertAndUpdateClusterUsage(CounterMode.INCREMENT, false);
    Mockito.verify(groupCapacityPersistService, times(1)).incrementUsageWithDefaultQuotaLimit(any());
    service.insertAndUpdateClusterUsage(CounterMode.DECREMENT, true);
    Mockito.verify(groupCapacityPersistService, times(1)).decrementUsage(any());
}
Also used : GroupCapacity(com.alibaba.nacos.config.server.model.capacity.GroupCapacity) Test(org.junit.Test)

Example 13 with GroupCapacity

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

the class CapacityServiceTest method testInitGroupCapacity.

@Test
public void testInitGroupCapacity() {
    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);
    service.initGroupCapacity("testGroup");
    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));
}
Also used : GroupCapacity(com.alibaba.nacos.config.server.model.capacity.GroupCapacity) Test(org.junit.Test)

Example 14 with GroupCapacity

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

the class CapacityServiceTest method testCorrectUsage.

@Test
public void testCorrectUsage() {
    List<GroupCapacity> groupCapacityList = new ArrayList<>();
    GroupCapacity groupCapacity = new GroupCapacity();
    groupCapacity.setId(1L);
    groupCapacity.setGroup("testGroup");
    groupCapacityList.add(groupCapacity);
    when(groupCapacityPersistService.getCapacityList4CorrectUsage(0L, 100)).thenReturn(groupCapacityList);
    when(groupCapacityPersistService.getCapacityList4CorrectUsage(1L, 100)).thenReturn(new ArrayList<>());
    when(groupCapacityPersistService.correctUsage(eq("testGroup"), any())).thenReturn(true);
    List<TenantCapacity> tenantCapacityList = new ArrayList<>();
    TenantCapacity tenantCapacity = new TenantCapacity();
    tenantCapacity.setId(1L);
    tenantCapacity.setTenant("testTenant");
    tenantCapacityList.add(tenantCapacity);
    when(tenantCapacityPersistService.getCapacityList4CorrectUsage(0L, 100)).thenReturn(tenantCapacityList);
    when(tenantCapacityPersistService.getCapacityList4CorrectUsage(1L, 100)).thenReturn(new ArrayList<>());
    when(tenantCapacityPersistService.correctUsage(eq("testTenant"), any())).thenReturn(true);
    service.correctUsage();
    Mockito.verify(groupCapacityPersistService, times(1)).getCapacityList4CorrectUsage(0L, 100);
    Mockito.verify(groupCapacityPersistService, times(1)).getCapacityList4CorrectUsage(1L, 100);
    Mockito.verify(groupCapacityPersistService, times(1)).correctUsage(eq("testGroup"), any());
    Mockito.verify(tenantCapacityPersistService, times(1)).getCapacityList4CorrectUsage(0L, 100);
    Mockito.verify(tenantCapacityPersistService, times(1)).getCapacityList4CorrectUsage(1L, 100);
    Mockito.verify(tenantCapacityPersistService, times(1)).correctUsage(eq("testTenant"), any());
}
Also used : TenantCapacity(com.alibaba.nacos.config.server.model.capacity.TenantCapacity) GroupCapacity(com.alibaba.nacos.config.server.model.capacity.GroupCapacity) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 15 with GroupCapacity

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

the class CapacityServiceTest method testInitCapacityV1.

@Test
public void testInitCapacityV1() {
    GroupCapacity groupCapacity = new GroupCapacity();
    groupCapacity.setUsage(300);
    when(groupCapacityPersistService.getGroupCapacity(eq("testGroup"))).thenReturn(groupCapacity);
    when(groupCapacityPersistService.insertGroupCapacity(any())).thenReturn(true);
    when(groupCapacityPersistService.updateQuota(eq("testGroup"), eq(500))).thenReturn(true);
    TenantCapacity tenantCapacity = new TenantCapacity();
    tenantCapacity.setUsage(300);
    when(tenantCapacityPersistService.getTenantCapacity(eq("testTenant"))).thenReturn(tenantCapacity);
    when(tenantCapacityPersistService.insertTenantCapacity(any())).thenReturn(true);
    when(tenantCapacityPersistService.updateQuota(eq("testTenant"), eq(500))).thenReturn(true);
    service.initCapacity("testGroup", null);
    Mockito.verify(groupCapacityPersistService, times(1)).getGroupCapacity(eq("testGroup"));
    Mockito.verify(groupCapacityPersistService, times(1)).insertGroupCapacity(any());
    Mockito.verify(groupCapacityPersistService, times(1)).updateQuota(eq("testGroup"), eq(500));
    service.initCapacity(null, "testTenant");
    Mockito.verify(tenantCapacityPersistService, times(1)).getTenantCapacity(eq("testTenant"));
    Mockito.verify(tenantCapacityPersistService, times(1)).insertTenantCapacity(any());
    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) 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