This is an automated email from the ASF dual-hosted git repository.

weizhou pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cloudstack.git


The following commit(s) were added to refs/heads/main by this push:
     new 819dd7b75c1 server: remove supportedOwner from Resource.ResourceType 
(#7416)
819dd7b75c1 is described below

commit 819dd7b75c1b61ae444c45476f5834dbfb9094d0
Author: GaOrtiga <[email protected]>
AuthorDate: Wed Aug 30 06:29:16 2023 -0300

    server: remove supportedOwner from Resource.ResourceType (#7416)
---
 .../java/com/cloud/configuration/Resource.java     | 47 +++++------------
 .../configuration/dao/ResourceCountDaoImpl.java    | 15 ------
 .../resourcelimit/ResourceLimitManagerImpl.java    | 59 +++++++++-------------
 .../com/cloud/server/ConfigurationServerImpl.java  | 27 +++-------
 4 files changed, 43 insertions(+), 105 deletions(-)

diff --git a/api/src/main/java/com/cloud/configuration/Resource.java 
b/api/src/main/java/com/cloud/configuration/Resource.java
index fefeeb15ef5..32db2fcafea 100644
--- a/api/src/main/java/com/cloud/configuration/Resource.java
+++ b/api/src/main/java/com/cloud/configuration/Resource.java
@@ -22,29 +22,27 @@ public interface Resource {
     String UNLIMITED = "Unlimited";
 
     enum ResourceType { // Primary and Secondary storage are allocated_storage 
and not the physical storage.
-        user_vm("user_vm", 0, ResourceOwnerType.Account, 
ResourceOwnerType.Domain),
-        public_ip("public_ip", 1, ResourceOwnerType.Account, 
ResourceOwnerType.Domain),
-        volume("volume", 2, ResourceOwnerType.Account, 
ResourceOwnerType.Domain),
-        snapshot("snapshot", 3, ResourceOwnerType.Account, 
ResourceOwnerType.Domain),
-        template("template", 4, ResourceOwnerType.Account, 
ResourceOwnerType.Domain),
-        project("project", 5, ResourceOwnerType.Account, 
ResourceOwnerType.Domain),
-        network("network", 6, ResourceOwnerType.Account, 
ResourceOwnerType.Domain),
-        vpc("vpc", 7, ResourceOwnerType.Account, ResourceOwnerType.Domain),
-        cpu("cpu", 8, ResourceOwnerType.Account, ResourceOwnerType.Domain),
-        memory("memory", 9, ResourceOwnerType.Account, 
ResourceOwnerType.Domain),
-        primary_storage("primary_storage", 10, ResourceOwnerType.Account, 
ResourceOwnerType.Domain),
-        secondary_storage("secondary_storage", 11, ResourceOwnerType.Account, 
ResourceOwnerType.Domain);
+        user_vm("user_vm", 0),
+        public_ip("public_ip", 1),
+        volume("volume", 2),
+        snapshot("snapshot", 3),
+        template("template", 4),
+        project("project", 5),
+        network("network", 6),
+        vpc("vpc", 7),
+        cpu("cpu", 8),
+        memory("memory", 9),
+        primary_storage("primary_storage", 10),
+        secondary_storage("secondary_storage", 11);
 
         private String name;
-        private ResourceOwnerType[] supportedOwners;
         private int ordinal;
         public static final long bytesToKiB = 1024;
         public static final long bytesToMiB = bytesToKiB * 1024;
         public static final long bytesToGiB = bytesToMiB * 1024;
 
-        ResourceType(String name, int ordinal, ResourceOwnerType... 
supportedOwners) {
+        ResourceType(String name, int ordinal) {
             this.name = name;
-            this.supportedOwners = supportedOwners;
             this.ordinal = ordinal;
         }
 
@@ -52,25 +50,6 @@ public interface Resource {
             return name;
         }
 
-        public ResourceOwnerType[] getSupportedOwners() {
-            return supportedOwners;
-        }
-
-        public boolean supportsOwner(ResourceOwnerType ownerType) {
-            boolean success = false;
-            if (supportedOwners != null) {
-                int length = supportedOwners.length;
-                for (int i = 0; i < length; i++) {
-                    if 
(supportedOwners[i].getName().equalsIgnoreCase(ownerType.getName())) {
-                        success = true;
-                        break;
-                    }
-                }
-            }
-
-            return success;
-        }
-
         public int getOrdinal() {
             return ordinal;
         }
diff --git 
a/engine/schema/src/main/java/com/cloud/configuration/dao/ResourceCountDaoImpl.java
 
b/engine/schema/src/main/java/com/cloud/configuration/dao/ResourceCountDaoImpl.java
index f14cef9d92c..ca6f13d2d64 100644
--- 
a/engine/schema/src/main/java/com/cloud/configuration/dao/ResourceCountDaoImpl.java
+++ 
b/engine/schema/src/main/java/com/cloud/configuration/dao/ResourceCountDaoImpl.java
@@ -36,7 +36,6 @@ import com.cloud.configuration.ResourceCountVO;
 import com.cloud.configuration.ResourceLimit;
 import com.cloud.domain.DomainVO;
 import com.cloud.domain.dao.DomainDao;
-import com.cloud.exception.UnsupportedServiceException;
 import com.cloud.user.AccountVO;
 import com.cloud.user.dao.AccountDao;
 import com.cloud.utils.db.DB;
@@ -171,9 +170,6 @@ public class ResourceCountDaoImpl extends 
GenericDaoBase<ResourceCountVO, Long>
 
         ResourceType[] resourceTypes = Resource.ResourceType.values();
         for (ResourceType resourceType : resourceTypes) {
-            if (!resourceType.supportsOwner(ownerType)) {
-                continue;
-            }
             ResourceCountVO resourceCountVO = new 
ResourceCountVO(resourceType, 0, ownerId, ownerType);
             persist(resourceCountVO);
         }
@@ -217,17 +213,6 @@ public class ResourceCountDaoImpl extends 
GenericDaoBase<ResourceCountVO, Long>
         }
     }
 
-    @Override
-    public ResourceCountVO persist(ResourceCountVO resourceCountVO) {
-        ResourceOwnerType ownerType = resourceCountVO.getResourceOwnerType();
-        ResourceType resourceType = resourceCountVO.getType();
-        if (!resourceType.supportsOwner(ownerType)) {
-            throw new UnsupportedServiceException("Resource type " + 
resourceType + " is not supported for owner of type " + ownerType.getName());
-        }
-
-        return super.persist(resourceCountVO);
-    }
-
     @Override
     public long removeEntriesByOwner(long ownerId, ResourceOwnerType 
ownerType) {
         SearchCriteria<ResourceCountVO> sc = TypeSearch.create();
diff --git 
a/server/src/main/java/com/cloud/resourcelimit/ResourceLimitManagerImpl.java 
b/server/src/main/java/com/cloud/resourcelimit/ResourceLimitManagerImpl.java
index 37c135d3462..903b851f918 100644
--- a/server/src/main/java/com/cloud/resourcelimit/ResourceLimitManagerImpl.java
+++ b/server/src/main/java/com/cloud/resourcelimit/ResourceLimitManagerImpl.java
@@ -701,7 +701,7 @@ public class ResourceLimitManagerImpl extends ManagerBase 
implements ResourceLim
                 if (isAccount) {
                     if (accountLimitStr.size() < resourceTypes.length) {
                         for (ResourceType rt : resourceTypes) {
-                            if (!accountLimitStr.contains(rt.toString()) && 
rt.supportsOwner(ResourceOwnerType.Account)) {
+                            if (!accountLimitStr.contains(rt.toString())) {
                                 limits.add(new ResourceLimitVO(rt, 
findCorrectResourceLimitForAccount(_accountMgr.getAccount(accountId), rt), 
accountId, ResourceOwnerType.Account));
                             }
                         }
@@ -710,7 +710,7 @@ public class ResourceLimitManagerImpl extends ManagerBase 
implements ResourceLim
                 } else {
                     if (domainLimitStr.size() < resourceTypes.length) {
                         for (ResourceType rt : resourceTypes) {
-                            if (!domainLimitStr.contains(rt.toString()) && 
rt.supportsOwner(ResourceOwnerType.Domain)) {
+                            if (!domainLimitStr.contains(rt.toString())) {
                                 limits.add(new ResourceLimitVO(rt, 
findCorrectResourceLimitForDomain(_domainDao.findById(domainId), rt), domainId, 
ResourceOwnerType.Domain));
                             }
                         }
@@ -855,16 +855,12 @@ public class ResourceLimitManagerImpl extends ManagerBase 
implements ResourceLim
 
         for (ResourceType type : resourceTypes) {
             if (accountId != null) {
-                if (type.supportsOwner(ResourceOwnerType.Account)) {
-                    count = recalculateAccountResourceCount(accountId, type);
-                    counts.add(new ResourceCountVO(type, count, accountId, 
ResourceOwnerType.Account));
-                }
+                count = recalculateAccountResourceCount(accountId, type);
+                counts.add(new ResourceCountVO(type, count, accountId, 
ResourceOwnerType.Account));
 
             } else {
-                if (type.supportsOwner(ResourceOwnerType.Domain)) {
-                    count = recalculateDomainResourceCount(domainId, type);
-                    counts.add(new ResourceCountVO(type, count, domainId, 
ResourceOwnerType.Domain));
-                }
+                count = recalculateDomainResourceCount(domainId, type);
+                counts.add(new ResourceCountVO(type, count, domainId, 
ResourceOwnerType.Domain));
             }
         }
 
@@ -921,25 +917,20 @@ public class ResourceLimitManagerImpl extends ManagerBase 
implements ResourceLim
 
                 List<DomainVO> domainChildren = 
_domainDao.findImmediateChildrenForParent(domainId);
                 // for each child domain update the resource count
-                if (type.supportsOwner(ResourceOwnerType.Domain)) {
 
-                    // calculate project count here
-                    if (type == ResourceType.project) {
-                        newResourceCount += 
_projectDao.countProjectsForDomain(domainId);
-                    }
-
-                    for (DomainVO childDomain : domainChildren) {
-                        long childDomainResourceCount = 
recalculateDomainResourceCount(childDomain.getId(), type);
-                        newResourceCount += childDomainResourceCount; // add 
the child domain count to parent domain count
-                    }
+                // calculate project count here
+                if (type == ResourceType.project) {
+                    newResourceCount += 
_projectDao.countProjectsForDomain(domainId);
                 }
 
-                if (type.supportsOwner(ResourceOwnerType.Account)) {
-                    List<AccountVO> accounts = 
_accountDao.findActiveAccountsForDomain(domainId);
-                    for (AccountVO account : accounts) {
-                        long accountResourceCount = 
recalculateAccountResourceCount(account.getId(), type);
-                        newResourceCount += accountResourceCount; // add 
account's resource count to parent domain count
-                    }
+                for (DomainVO childDomain : domainChildren) {
+                    long childDomainResourceCount = 
recalculateDomainResourceCount(childDomain.getId(), type);
+                    newResourceCount += childDomainResourceCount; // add the 
child domain count to parent domain count
+                }
+                List<AccountVO> accounts = 
_accountDao.findActiveAccountsForDomain(domainId);
+                for (AccountVO account : accounts) {
+                    long accountResourceCount = 
recalculateAccountResourceCount(account.getId(), type);
+                    newResourceCount += accountResourceCount; // add account's 
resource count to parent domain count
                 }
                 _resourceCountDao.setResourceCount(domainId, 
ResourceOwnerType.Domain, type, newResourceCount);
 
@@ -1201,18 +1192,14 @@ public class ResourceLimitManagerImpl extends 
ManagerBase implements ResourceLim
             }
 
             for (ResourceType type : ResourceType.values()) {
-                if (type.supportsOwner(ResourceOwnerType.Domain)) {
-                    
recalculateDomainResourceCountInContext(Domain.ROOT_DOMAIN, type);
-                    for (Domain domain : domains) {
-                        recalculateDomainResourceCount(domain.getId(), type);
-                    }
+                recalculateDomainResourceCountInContext(Domain.ROOT_DOMAIN, 
type);
+                for (Domain domain : domains) {
+                    recalculateDomainResourceCount(domain.getId(), type);
                 }
 
-                if (type.supportsOwner(ResourceOwnerType.Account)) {
-                    // run through the accounts in the root domain
-                    for (AccountVO account : accounts) {
-                        
recalculateAccountResourceCountInContext(account.getId(), type);
-                    }
+                // run through the accounts in the root domain
+                for (AccountVO account : accounts) {
+                    recalculateAccountResourceCountInContext(account.getId(), 
type);
                 }
             }
         }
diff --git a/server/src/main/java/com/cloud/server/ConfigurationServerImpl.java 
b/server/src/main/java/com/cloud/server/ConfigurationServerImpl.java
index 3f9447812a7..cb6f9961b64 100644
--- a/server/src/main/java/com/cloud/server/ConfigurationServerImpl.java
+++ b/server/src/main/java/com/cloud/server/ConfigurationServerImpl.java
@@ -1315,22 +1315,9 @@ public class ConfigurationServerImpl extends ManagerBase 
implements Configuratio
         List<ResourceCountVO> domainResourceCount = 
_resourceCountDao.listResourceCountByOwnerType(ResourceOwnerType.Domain);
         List<ResourceCountVO> accountResourceCount = 
_resourceCountDao.listResourceCountByOwnerType(ResourceOwnerType.Account);
 
-        final List<ResourceType> accountSupportedResourceTypes = new 
ArrayList<ResourceType>();
-        final List<ResourceType> domainSupportedResourceTypes = new 
ArrayList<ResourceType>();
+        final int expectedCount = resourceTypes.length;
 
-        for (ResourceType resourceType : resourceTypes) {
-            if (resourceType.supportsOwner(ResourceOwnerType.Account)) {
-                accountSupportedResourceTypes.add(resourceType);
-            }
-            if (resourceType.supportsOwner(ResourceOwnerType.Domain)) {
-                domainSupportedResourceTypes.add(resourceType);
-            }
-        }
-
-        final int accountExpectedCount = accountSupportedResourceTypes.size();
-        final int domainExpectedCount = domainSupportedResourceTypes.size();
-
-        if ((domainResourceCount.size() < domainExpectedCount * 
domains.size())) {
+        if ((domainResourceCount.size() < expectedCount * domains.size())) {
             s_logger.debug("resource_count table has records missing for some 
domains...going to insert them");
             for (final DomainVO domain : domains) {
                 // Lock domain
@@ -1344,8 +1331,8 @@ public class ConfigurationServerImpl extends ManagerBase 
implements Configuratio
                             
domainCountStr.add(domainCount.getType().toString());
                         }
 
-                        if (domainCountStr.size() < domainExpectedCount) {
-                            for (ResourceType resourceType : 
domainSupportedResourceTypes) {
+                        if (domainCountStr.size() < expectedCount) {
+                            for (ResourceType resourceType : resourceTypes) {
                                 if 
(!domainCountStr.contains(resourceType.toString())) {
                                     ResourceCountVO resourceCountVO = new 
ResourceCountVO(resourceType, 0, domain.getId(), ResourceOwnerType.Domain);
                                     s_logger.debug("Inserting resource count 
of type " + resourceType + " for domain id=" + domain.getId());
@@ -1359,7 +1346,7 @@ public class ConfigurationServerImpl extends ManagerBase 
implements Configuratio
             }
         }
 
-        if ((accountResourceCount.size() < accountExpectedCount * 
accounts.size())) {
+        if ((accountResourceCount.size() < expectedCount * accounts.size())) {
             s_logger.debug("resource_count table has records missing for some 
accounts...going to insert them");
             for (final AccountVO account : accounts) {
                 // lock account
@@ -1373,8 +1360,8 @@ public class ConfigurationServerImpl extends ManagerBase 
implements Configuratio
                             
accountCountStr.add(accountCount.getType().toString());
                         }
 
-                        if (accountCountStr.size() < accountExpectedCount) {
-                            for (ResourceType resourceType : 
accountSupportedResourceTypes) {
+                        if (accountCountStr.size() < expectedCount) {
+                            for (ResourceType resourceType : resourceTypes) {
                                 if 
(!accountCountStr.contains(resourceType.toString())) {
                                     ResourceCountVO resourceCountVO = new 
ResourceCountVO(resourceType, 0, account.getId(), ResourceOwnerType.Account);
                                     s_logger.debug("Inserting resource count 
of type " + resourceType + " for account id=" + account.getId());

Reply via email to