Search in sources :

Example 6 with Capacity

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

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

the class CapacityControllerTest method testGetCapacity.

@Test
public void testGetCapacity() throws Exception {
    Capacity capacity = new Capacity();
    capacity.setId(1L);
    capacity.setMaxAggrCount(1);
    capacity.setMaxSize(1);
    capacity.setMaxAggrSize(1);
    capacity.setGmtCreate(new Timestamp(1));
    capacity.setGmtModified(new Timestamp(2));
    when(capacityService.getCapacityWithDefault(eq("test"), eq("test"))).thenReturn(capacity);
    MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(Constants.CAPACITY_CONTROLLER_PATH).param("group", "test").param("tenant", "test");
    String actualValue = mockMvc.perform(builder).andReturn().getResponse().getContentAsString();
    Capacity result = JacksonUtils.toObj(JacksonUtils.toObj(actualValue).get("data").toString(), Capacity.class);
    Assert.assertNotNull(result);
    Assert.assertEquals(capacity.getId(), result.getId());
    Assert.assertEquals(capacity.getMaxAggrCount(), result.getMaxAggrCount());
    Assert.assertEquals(capacity.getMaxSize(), result.getMaxSize());
    Assert.assertEquals(capacity.getMaxAggrSize(), result.getMaxAggrSize());
    Assert.assertEquals(capacity.getGmtCreate(), result.getGmtCreate());
    Assert.assertEquals(capacity.getGmtModified(), result.getGmtModified());
}
Also used : Capacity(com.alibaba.nacos.config.server.model.capacity.Capacity) MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) Timestamp(java.sql.Timestamp) Test(org.junit.Test)

Example 8 with Capacity

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

the class GroupCapacityPersistService method getClusterUsage.

public int getClusterUsage() {
    Capacity clusterCapacity = getClusterCapacity();
    if (clusterCapacity != null) {
        return clusterCapacity.getUsage();
    }
    String sql = "SELECT count(*) FROM config_info";
    Integer result = jdbcTemplate.queryForObject(sql, Integer.class);
    if (result == null) {
        throw new IllegalArgumentException("configInfoCount error");
    }
    return result.intValue();
}
Also used : Capacity(com.alibaba.nacos.config.server.model.capacity.Capacity) GroupCapacity(com.alibaba.nacos.config.server.model.capacity.GroupCapacity)

Example 9 with Capacity

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

the class CapacityController method getCapacity.

@GetMapping
public RestResult<Capacity> getCapacity(HttpServletResponse response, @RequestParam(required = false) String group, @RequestParam(required = false) String tenant) {
    if (group == null && tenant == null) {
        RestResult<Capacity> restResult = new RestResult<>();
        response.setStatus(STATUS400);
        restResult.setCode(STATUS400);
        restResult.setMessage("The parameter group and tenant cannot be empty at the same time");
        return restResult;
    }
    if (group == null && StringUtils.isBlank(tenant)) {
        RestResult<Capacity> restResult = new RestResult<>();
        response.setStatus(STATUS400);
        restResult.setCode(STATUS400);
        restResult.setMessage("tenant cannot be an empty string");
        return restResult;
    }
    RestResult<Capacity> restResult = new RestResult<>();
    try {
        response.setStatus(STATUS200);
        restResult.setCode(STATUS200);
        Capacity capacity = capacityService.getCapacityWithDefault(group, tenant);
        if (capacity == null) {
            LOGGER.warn("[getCapacity] capacity not exist,need init group: {}, tenant: {}", group, tenant);
            capacityService.initCapacity(group, tenant);
            capacity = capacityService.getCapacityWithDefault(group, tenant);
        }
        if (capacity != null) {
            restResult.setData(capacity);
        }
    } catch (Exception e) {
        LOGGER.error("[getCapacity] ", e);
        response.setStatus(STATUS500);
        restResult.setCode(STATUS500);
        restResult.setMessage(e.getMessage());
    }
    return restResult;
}
Also used : RestResult(com.alibaba.nacos.common.model.RestResult) Capacity(com.alibaba.nacos.config.server.model.capacity.Capacity) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 10 with Capacity

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

the class CapacityServiceTest method testGetCapacityWithDefault.

@Test
public void testGetCapacityWithDefault() {
    TenantCapacity tenantCapacity = new TenantCapacity();
    tenantCapacity.setQuota(0);
    tenantCapacity.setMaxSize(0);
    tenantCapacity.setMaxAggrCount(0);
    tenantCapacity.setMaxAggrSize(0);
    when(tenantCapacityPersistService.getTenantCapacity(anyString())).thenReturn(tenantCapacity);
    GroupCapacity groupCapacity1 = new GroupCapacity();
    groupCapacity1.setQuota(0);
    groupCapacity1.setMaxSize(0);
    groupCapacity1.setMaxAggrCount(0);
    groupCapacity1.setMaxAggrSize(0);
    when(groupCapacityPersistService.getGroupCapacity(anyString())).thenReturn(groupCapacity1);
    // group is null
    Capacity resCapacity1 = service.getCapacityWithDefault(null, "testTenant");
    Assert.assertEquals(PropertyUtil.getDefaultGroupQuota(), resCapacity1.getQuota().intValue());
    Assert.assertEquals(PropertyUtil.getDefaultMaxSize(), resCapacity1.getMaxSize().intValue());
    Assert.assertEquals(PropertyUtil.getDefaultMaxAggrCount(), resCapacity1.getMaxAggrCount().intValue());
    Assert.assertEquals(PropertyUtil.getDefaultMaxAggrSize(), resCapacity1.getMaxAggrSize().intValue());
    // group is GroupCapacityPersistService.CLUSTER
    Capacity resCapacity2 = service.getCapacityWithDefault(GroupCapacityPersistService.CLUSTER, null);
    Assert.assertEquals(PropertyUtil.getDefaultClusterQuota(), resCapacity2.getQuota().intValue());
    Assert.assertEquals(PropertyUtil.getDefaultMaxSize(), resCapacity2.getMaxSize().intValue());
    Assert.assertEquals(PropertyUtil.getDefaultMaxAggrCount(), resCapacity2.getMaxAggrCount().intValue());
    Assert.assertEquals(PropertyUtil.getDefaultMaxAggrSize(), resCapacity2.getMaxAggrSize().intValue());
    GroupCapacity groupCapacity2 = new GroupCapacity();
    groupCapacity2.setQuota(0);
    groupCapacity2.setMaxSize(0);
    groupCapacity2.setMaxAggrCount(0);
    groupCapacity2.setMaxAggrSize(0);
    when(groupCapacityPersistService.getGroupCapacity(anyString())).thenReturn(groupCapacity2);
    // tenant is null
    Capacity resCapacity3 = service.getCapacityWithDefault("testGroup", null);
    Assert.assertEquals(PropertyUtil.getDefaultGroupQuota(), resCapacity3.getQuota().intValue());
    Assert.assertEquals(PropertyUtil.getDefaultMaxSize(), resCapacity3.getMaxSize().intValue());
    Assert.assertEquals(PropertyUtil.getDefaultMaxAggrCount(), resCapacity3.getMaxAggrCount().intValue());
    Assert.assertEquals(PropertyUtil.getDefaultMaxAggrSize(), resCapacity3.getMaxAggrSize().intValue());
}
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)

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