|
96 | 96 | import com.cloud.utils.db.SearchCriteria.Op; |
97 | 97 | import com.cloud.utils.db.Transaction; |
98 | 98 | import com.cloud.utils.db.TransactionCallback; |
| 99 | +import com.cloud.utils.db.TransactionCallbackNoReturn; |
99 | 100 | import com.cloud.utils.db.TransactionCallbackWithExceptionNoReturn; |
100 | 101 | import com.cloud.utils.db.TransactionStatus; |
101 | 102 | import com.cloud.utils.exception.CloudRuntimeException; |
@@ -886,60 +887,62 @@ public Long doInTransaction(TransactionStatus status) { |
886 | 887 |
|
887 | 888 | @DB |
888 | 889 | protected long recalculateAccountResourceCount(final long accountId, final ResourceType type) { |
889 | | - Long newCount = Transaction.execute(new TransactionCallback<Long>() { |
| 890 | + final Long newCount; |
| 891 | + if (type == Resource.ResourceType.user_vm) { |
| 892 | + newCount = _userVmDao.countAllocatedVMsForAccount(accountId); |
| 893 | + } else if (type == Resource.ResourceType.volume) { |
| 894 | + long virtualRouterCount = _vmDao.findIdsOfAllocatedVirtualRoutersForAccount(accountId).size(); |
| 895 | + newCount = _volumeDao.countAllocatedVolumesForAccount(accountId) - virtualRouterCount; // don't count the volumes of virtual router |
| 896 | + } else if (type == Resource.ResourceType.snapshot) { |
| 897 | + newCount = _snapshotDao.countSnapshotsForAccount(accountId); |
| 898 | + } else if (type == Resource.ResourceType.public_ip) { |
| 899 | + newCount = calculatePublicIpForAccount(accountId); |
| 900 | + } else if (type == Resource.ResourceType.template) { |
| 901 | + newCount = _vmTemplateDao.countTemplatesForAccount(accountId); |
| 902 | + } else if (type == Resource.ResourceType.project) { |
| 903 | + newCount = _projectAccountDao.countByAccountIdAndRole(accountId, Role.Admin); |
| 904 | + } else if (type == Resource.ResourceType.network) { |
| 905 | + newCount = _networkDao.countNetworksUserCanCreate(accountId); |
| 906 | + } else if (type == Resource.ResourceType.vpc) { |
| 907 | + newCount = _vpcDao.countByAccountId(accountId); |
| 908 | + } else if (type == Resource.ResourceType.cpu) { |
| 909 | + newCount = countCpusForAccount(accountId); |
| 910 | + } else if (type == Resource.ResourceType.memory) { |
| 911 | + newCount = calculateMemoryForAccount(accountId); |
| 912 | + } else if (type == Resource.ResourceType.primary_storage) { |
| 913 | + List<Long> virtualRouters = _vmDao.findIdsOfAllocatedVirtualRoutersForAccount(accountId); |
| 914 | + newCount = _volumeDao.primaryStorageUsedForAccount(accountId, virtualRouters); |
| 915 | + } else if (type == Resource.ResourceType.secondary_storage) { |
| 916 | + newCount = calculateSecondaryStorageForAccount(accountId); |
| 917 | + } else { |
| 918 | + throw new InvalidParameterValueException("Unsupported resource type " + type); |
| 919 | + } |
| 920 | + |
| 921 | + long oldCount = 0; |
| 922 | + final ResourceCountVO accountRC = _resourceCountDao.findByOwnerAndType(accountId, ResourceOwnerType.Account, type); |
| 923 | + if (accountRC != null) { |
| 924 | + oldCount = accountRC.getCount(); |
| 925 | + } |
| 926 | + |
| 927 | + if (newCount == null || !newCount.equals(oldCount)) { |
| 928 | + Transaction.execute(new TransactionCallbackNoReturn() { |
890 | 929 | @Override |
891 | | - public Long doInTransaction(TransactionStatus status) { |
892 | | - Long newCount = null; |
| 930 | + public void doInTransactionWithoutResult(TransactionStatus status) { |
893 | 931 | lockAccountAndOwnerDomainRows(accountId, type); |
894 | | - ResourceCountVO accountRC = _resourceCountDao.findByOwnerAndType(accountId, ResourceOwnerType.Account, type); |
895 | | - long oldCount = 0; |
896 | | - if (accountRC != null) |
897 | | - oldCount = accountRC.getCount(); |
898 | | - |
899 | | - if (type == Resource.ResourceType.user_vm) { |
900 | | - newCount = _userVmDao.countAllocatedVMsForAccount(accountId); |
901 | | - } else if (type == Resource.ResourceType.volume) { |
902 | | - newCount = _volumeDao.countAllocatedVolumesForAccount(accountId); |
903 | | - long virtualRouterCount = _vmDao.findIdsOfAllocatedVirtualRoutersForAccount(accountId).size(); |
904 | | - newCount = newCount - virtualRouterCount; // don't count the volumes of virtual router |
905 | | - } else if (type == Resource.ResourceType.snapshot) { |
906 | | - newCount = _snapshotDao.countSnapshotsForAccount(accountId); |
907 | | - } else if (type == Resource.ResourceType.public_ip) { |
908 | | - newCount = calculatePublicIpForAccount(accountId); |
909 | | - } else if (type == Resource.ResourceType.template) { |
910 | | - newCount = _vmTemplateDao.countTemplatesForAccount(accountId); |
911 | | - } else if (type == Resource.ResourceType.project) { |
912 | | - newCount = _projectAccountDao.countByAccountIdAndRole(accountId, Role.Admin); |
913 | | - } else if (type == Resource.ResourceType.network) { |
914 | | - newCount = _networkDao.countNetworksUserCanCreate(accountId); |
915 | | - } else if (type == Resource.ResourceType.vpc) { |
916 | | - newCount = _vpcDao.countByAccountId(accountId); |
917 | | - } else if (type == Resource.ResourceType.cpu) { |
918 | | - newCount = countCpusForAccount(accountId); |
919 | | - } else if (type == Resource.ResourceType.memory) { |
920 | | - newCount = calculateMemoryForAccount(accountId); |
921 | | - } else if (type == Resource.ResourceType.primary_storage) { |
922 | | - List<Long> virtualRouters = _vmDao.findIdsOfAllocatedVirtualRoutersForAccount(accountId); |
923 | | - newCount = _volumeDao.primaryStorageUsedForAccount(accountId, virtualRouters); |
924 | | - } else if (type == Resource.ResourceType.secondary_storage) { |
925 | | - newCount = calculateSecondaryStorageForAccount(accountId); |
926 | | - } else { |
927 | | - throw new InvalidParameterValueException("Unsupported resource type " + type); |
928 | | - } |
929 | | - _resourceCountDao.setResourceCount(accountId, ResourceOwnerType.Account, type, (newCount == null) ? 0 : newCount.longValue()); |
930 | | - |
931 | | - // No need to log message for primary and secondary storage because both are recalculating the |
932 | | - // resource count which will not lead to any discrepancy. |
933 | | - if (!Long.valueOf(oldCount).equals(newCount) && |
934 | | - (type != Resource.ResourceType.primary_storage && type != Resource.ResourceType.secondary_storage)) { |
935 | | - s_logger.warn("Discrepency in the resource count " + "(original count=" + oldCount + " correct count = " + newCount + ") for type " + type + |
936 | | - " for account ID " + accountId + " is fixed during resource count recalculation."); |
937 | | - } |
938 | | - return newCount; |
| 932 | + _resourceCountDao.setResourceCount(accountId, ResourceOwnerType.Account, type, (newCount == null) ? 0 : newCount); |
939 | 933 | } |
940 | 934 | }); |
| 935 | + } |
| 936 | + |
| 937 | + // No need to log message for primary and secondary storage because both are recalculating the |
| 938 | + // resource count which will not lead to any discrepancy. |
| 939 | + if (newCount != null && !newCount.equals(oldCount) && |
| 940 | + type != Resource.ResourceType.primary_storage && type != Resource.ResourceType.secondary_storage) { |
| 941 | + s_logger.warn("Discrepancy in the resource count " + "(original count=" + oldCount + " correct count = " + newCount + ") for type " + type + |
| 942 | + " for account ID " + accountId + " is fixed during resource count recalculation."); |
| 943 | + } |
941 | 944 |
|
942 | | - return (newCount == null) ? 0 : newCount.longValue(); |
| 945 | + return (newCount == null) ? 0 : newCount; |
943 | 946 | } |
944 | 947 |
|
945 | 948 | public long countCpusForAccount(long accountId) { |
|
0 commit comments