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;
}
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();
}
}
}
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;
}
}
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());
}
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));
}
Aggregations