From 72b4108828bc8499e9abbf85ca702b9f91a6c590 Mon Sep 17 00:00:00 2001 From: 0z5a Date: Wed, 18 Nov 2026 18:47:03 +0100 Subject: [PATCH] Create missing domain resource count row during recalculation recalculateDomainResourceCount() only selects existing resource_count rows since #8903. When a domain has no row for the type and tag, for example after a tag is added to resource.limit.storage.tags or resource.limit.host.tags, domainRC stays null. As soon as the accounts of the domain have usage for that tag, domainRC.setCount() throws a NullPointerException. updateResourceCount fails, and the periodic ResourceCountCheckTask stops at that domain on every run. Persist a new domain row with the recalculated count when none exists, like recalculateAccountResourceCount() already does for accounts. Fixes #14729 --- .../ResourceLimitManagerImpl.java | 7 +- .../ResourceLimitManagerImplTest.java | 71 +++++++++++++++++++ 2 files changed, 76 insertions(+), 2 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..6699a5337922 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) { + if (domainRC == null) { + _resourceCountDao.persist(new ResourceCountVO(type, newResourceCount, domainId, ResourceOwnerType.Domain, tag)); + } else if (oldResourceCount != newResourceCount) { domainRC.setCount(newResourceCount); _resourceCountDao.update(domainRC.getId(), domainRC); + } + if (oldResourceCount != newResourceCount) { 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..fae8f8969352 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,74 @@ public void testDecrementVmMemoryResourceCount() { Mockito.verify(resourceLimitManager, Mockito.times(1)) .decrementResourceCountWithTag(accountId, Resource.ResourceType.memory, tag, Long.valueOf(memory)); } + + private void mockDomainRecalculation(long domainId, Resource.ResourceType type, String tag, + ResourceCountVO domainRC, long accountCount) { + long accountId = 2L; + 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(accountCount).when(resourceLimitManager).recalculateAccountResourceCount(accountId, type, tag); + + ResourceCountVO accountRC = new ResourceCountVO(type, accountCount, accountId, Resource.ResourceOwnerType.Account, tag); + accountRC.setId(20L); + List domainRCs = domainRC == null ? new ArrayList<>() : List.of(domainRC); + Mockito.when(resourceCountDao.findByOwnersAndTypeAndTag(Mockito.anyList(), Mockito.eq(Resource.ResourceOwnerType.Domain), Mockito.eq(type), Mockito.eq(tag))) + .thenReturn(domainRCs); + Mockito.when(resourceCountDao.findByOwnersAndTypeAndTag(Mockito.anyList(), Mockito.eq(Resource.ResourceOwnerType.Account), Mockito.eq(type), Mockito.eq(tag))) + .thenReturn(List.of(accountRC)); + List locked = new ArrayList<>(domainRCs); + locked.add(accountRC); + Mockito.when(resourceCountDao.lockRows(Mockito.anySet())).thenReturn(locked); + } + + private void verifyDomainResourceCountPersisted(long domainId, Resource.ResourceType type, String tag, long count) { + ArgumentCaptor captor = ArgumentCaptor.forClass(ResourceCountVO.class); + Mockito.verify(resourceCountDao, Mockito.times(1)).persist(captor.capture()); + ResourceCountVO persisted = captor.getValue(); + Assert.assertEquals(Resource.ResourceOwnerType.Domain, persisted.getResourceOwnerType()); + Assert.assertEquals(Long.valueOf(domainId), persisted.getDomainId()); + Assert.assertEquals(type, persisted.getType()); + Assert.assertEquals(tag, persisted.getTag()); + Assert.assertEquals(count, persisted.getCount()); + Mockito.verify(resourceCountDao, Mockito.never()).update(Mockito.anyLong(), Mockito.any(ResourceCountVO.class)); + } + + @Test + public void testRecalculateDomainResourceCountCreatesMissingTaggedRow() { + long domainId = 1L; + Resource.ResourceType type = Resource.ResourceType.volume; + String tag = storageTags.get(0); + mockDomainRecalculation(domainId, type, tag, null, 10L); + + Assert.assertEquals(10L, resourceLimitManager.recalculateDomainResourceCount(domainId, type, tag)); + verifyDomainResourceCountPersisted(domainId, type, tag, 10L); + } + + @Test + public void testRecalculateDomainResourceCountCreatesMissingTaggedRowWithZeroCount() { + long domainId = 1L; + Resource.ResourceType type = Resource.ResourceType.volume; + String tag = storageTags.get(0); + mockDomainRecalculation(domainId, type, tag, null, 0L); + + Assert.assertEquals(0L, resourceLimitManager.recalculateDomainResourceCount(domainId, type, tag)); + verifyDomainResourceCountPersisted(domainId, type, tag, 0L); + } + + @Test + public void testRecalculateDomainResourceCountUpdatesExistingRow() { + long domainId = 1L; + Resource.ResourceType type = Resource.ResourceType.volume; + String tag = storageTags.get(0); + ResourceCountVO domainRC = new ResourceCountVO(type, 5L, domainId, Resource.ResourceOwnerType.Domain, tag); + domainRC.setId(10L); + mockDomainRecalculation(domainId, type, tag, domainRC, 10L); + + Assert.assertEquals(10L, resourceLimitManager.recalculateDomainResourceCount(domainId, type, tag)); + Assert.assertEquals(10L, domainRC.getCount()); + Mockito.verify(resourceCountDao, Mockito.times(1)).update(domainRC.getId(), domainRC); + Mockito.verify(resourceCountDao, Mockito.never()).persist(Mockito.any(ResourceCountVO.class)); + } }