Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1367,10 +1367,13 @@ public long recalculateDomainResourceCount(final long domainId, final ResourceTy
}
}

// TODO domainRC may be null if there are no resource counts for the domain found in the loop above
if (oldResourceCount != newResourceCount) {
domainRC.setCount(newResourceCount);
_resourceCountDao.update(domainRC.getId(), domainRC);
if (domainRC == null) {
_resourceCountDao.persist(new ResourceCountVO(type, newResourceCount, domainId, ResourceOwnerType.Domain, tag));
} else {
domainRC.setCount(newResourceCount);
_resourceCountDao.update(domainRC.getId(), domainRC);
}
logger.warn("Discrepancy in the resource count has been detected (original count = {} correct count = {}) for Type = {} for Domain ID = {} is fixed during resource count recalculation.",
oldResourceCount, newResourceCount, type, domainId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockedConstruction;
Expand Down Expand Up @@ -1434,4 +1435,55 @@ public void testDecrementVmMemoryResourceCount() {
Mockito.verify(resourceLimitManager, Mockito.times(1))
.decrementResourceCountWithTag(accountId, Resource.ResourceType.memory, tag, Long.valueOf(memory));
}

private void mockDomainWithOneAccountForRecalculation(long domainId, long accountId, Resource.ResourceType type, String tag, List<ResourceCountVO> lockedCounts) {
AccountVO account = Mockito.mock(AccountVO.class);
Mockito.when(account.getId()).thenReturn(accountId);
Mockito.when(accountDao.findActiveAccountsForDomain(domainId)).thenReturn(List.of(account));
Mockito.when(domainDao.findImmediateChildrenForParent(domainId)).thenReturn(new ArrayList<>());
Mockito.doReturn(3L).when(resourceLimitManager).recalculateAccountResourceCount(accountId, type, tag);
Mockito.when(resourceCountDao.lockRows(Mockito.anySet())).thenReturn(lockedCounts);
}

@Test
public void testRecalculateDomainResourceCountCreatesAMissingTaggedDomainCount() {
long domainId = 2L;
Resource.ResourceType type = Resource.ResourceType.volume;
String tag = "stag1";
ResourceCountVO accountCount = new ResourceCountVO(type, 3L, 10L, Resource.ResourceOwnerType.Account, tag);
mockDomainWithOneAccountForRecalculation(domainId, 10L, type, tag, List.of(accountCount));

long result = resourceLimitManager.recalculateDomainResourceCount(domainId, type, tag);

Assert.assertEquals(3L, result);
ArgumentCaptor<ResourceCountVO> created = ArgumentCaptor.forClass(ResourceCountVO.class);
Mockito.verify(resourceCountDao).persist(created.capture());
Assert.assertEquals(Resource.ResourceOwnerType.Domain, created.getValue().getResourceOwnerType());
Assert.assertEquals(Long.valueOf(domainId), created.getValue().getDomainId());
Assert.assertEquals(type, created.getValue().getType());
Assert.assertEquals(tag, created.getValue().getTag());
Assert.assertEquals(3L, created.getValue().getCount());
Mockito.verify(resourceCountDao, Mockito.never()).update(Mockito.anyLong(), Mockito.any(ResourceCountVO.class));
}

@Test
public void testRecalculateDomainResourceCountUpdatesAnExistingDomainCount() {
long domainId = 2L;
Resource.ResourceType type = Resource.ResourceType.volume;
String tag = "stag1";
ResourceCountVO accountCount = new ResourceCountVO(type, 3L, 10L, Resource.ResourceOwnerType.Account, tag);
ResourceCountVO domainCount = Mockito.mock(ResourceCountVO.class);
Mockito.when(domainCount.getId()).thenReturn(50L);
Mockito.when(domainCount.getResourceOwnerType()).thenReturn(Resource.ResourceOwnerType.Domain);
Mockito.when(domainCount.getDomainId()).thenReturn(domainId);
Mockito.when(domainCount.getCount()).thenReturn(1L);
mockDomainWithOneAccountForRecalculation(domainId, 10L, type, tag, List.of(accountCount, domainCount));

long result = resourceLimitManager.recalculateDomainResourceCount(domainId, type, tag);

Assert.assertEquals(3L, result);
Mockito.verify(domainCount).setCount(3L);
Mockito.verify(resourceCountDao).update(50L, domainCount);
Mockito.verify(resourceCountDao, Mockito.never()).persist(Mockito.any(ResourceCountVO.class));
}
}
Loading