From c7d6374f5b316f21e9612cd692ef3738b592f543 Mon Sep 17 00:00:00 2001 From: rootnomad Date: Wed, 2 Dec 2026 12:42:59 -0500 Subject: [PATCH] server: create a missing domain resource count during recalculation recalculateDomainResourceCount() assumed the domain already has a count row for the type and tag. When a tagged domain count is missing and the recalculated usage is not zero, it dereferenced null, so updateResourceCount failed with a NullPointerException and the periodic ResourceCountChecker failed the same way on every run. Create the missing domain count with the recalculated value, the same way recalculateAccountResourceCount() does for accounts. Fixes #14729 --- .../ResourceLimitManagerImpl.java | 9 ++-- .../ResourceLimitManagerImplTest.java | 52 +++++++++++++++++++ 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/server/src/main/java/com/cloud/resourcelimit/ResourceLimitManagerImpl.java b/server/src/main/java/com/cloud/resourcelimit/ResourceLimitManagerImpl.java index 210c70e607a6..af52a8e65f5c 100644 --- a/server/src/main/java/com/cloud/resourcelimit/ResourceLimitManagerImpl.java +++ b/server/src/main/java/com/cloud/resourcelimit/ResourceLimitManagerImpl.java @@ -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); } diff --git a/server/src/test/java/com/cloud/resourcelimit/ResourceLimitManagerImplTest.java b/server/src/test/java/com/cloud/resourcelimit/ResourceLimitManagerImplTest.java index 78d69365abbc..662a1e7839cf 100644 --- a/server/src/test/java/com/cloud/resourcelimit/ResourceLimitManagerImplTest.java +++ b/server/src/test/java/com/cloud/resourcelimit/ResourceLimitManagerImplTest.java @@ -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; @@ -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 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 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)); + } }