Skip to content

Commit 75da982

Browse files
Updated resource counter to include correct size after volume creation/resize and other improvements (#6587)
* Updated resource counter to include correct size after volume creation/resize and other improvements - Recalculate resource counters for root domain in the periodic task - Update correct size in the primary_storage resource counter after volume creation/resize - Some code improvements * review and sonarcloud issues Co-authored-by: Suresh Kumar Anaparti <suresh.anaparti@shapeblue.com> Co-authored-by: Daan Hoogland <daan@onecht.net>
1 parent 152a274 commit 75da982

File tree

6 files changed

+96
-62
lines changed

6 files changed

+96
-62
lines changed

api/src/main/java/com/cloud/configuration/Resource.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818

1919
public interface Resource {
2020

21-
public static final short RESOURCE_UNLIMITED = -1;
21+
short RESOURCE_UNLIMITED = -1;
22+
String UNLIMITED = "Unlimited";
2223

23-
public enum ResourceType { // Primary and Secondary storage are allocated_storage and not the physical storage.
24+
enum ResourceType { // Primary and Secondary storage are allocated_storage and not the physical storage.
2425
user_vm("user_vm", 0, ResourceOwnerType.Account, ResourceOwnerType.Domain),
2526
public_ip("public_ip", 1, ResourceOwnerType.Account, ResourceOwnerType.Domain),
2627
volume("volume", 2, ResourceOwnerType.Account, ResourceOwnerType.Domain),

engine/storage/volume/src/main/java/org/apache/cloudstack/storage/volume/VolumeObject.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import javax.inject.Inject;
2222

23+
import com.cloud.configuration.Resource.ResourceType;
2324
import com.cloud.dc.VsphereStoragePolicyVO;
2425
import com.cloud.dc.dao.VsphereStoragePolicyDao;
2526
import com.cloud.service.dao.ServiceOfferingDetailsDao;
@@ -28,6 +29,7 @@
2829
import com.cloud.storage.VolumeDetailVO;
2930
import com.cloud.storage.dao.VMTemplateDao;
3031
import com.cloud.storage.dao.VolumeDetailsDao;
32+
import com.cloud.user.ResourceLimitService;
3133
import com.cloud.vm.VmDetailConstants;
3234

3335
import org.apache.cloudstack.resourcedetail.dao.DiskOfferingDetailsDao;
@@ -88,6 +90,8 @@ public class VolumeObject implements VolumeInfo {
8890
@Inject
8991
ObjectInDataStoreManager objectInStoreMgr;
9092
@Inject
93+
ResourceLimitService resourceLimitMgr;
94+
@Inject
9195
VMInstanceDao vmInstanceDao;
9296
@Inject
9397
DiskOfferingDao diskOfferingDao;
@@ -678,6 +682,22 @@ protected void updateVolumeInfo(VolumeObjectTO newVolume, VolumeVO volumeVo, boo
678682
s_logger.debug(String.format("Updated %s from %s to %s ", volumeVo.getVolumeDescription(), previousValues, newValues));
679683
}
680684

685+
protected void updateResourceCount(VolumeObjectTO newVolume, VolumeVO oldVolume) {
686+
if (newVolume == null || newVolume.getSize() == null || oldVolume == null || oldVolume.getSize() == null) {
687+
return;
688+
}
689+
690+
long newVolumeSize = newVolume.getSize();
691+
long oldVolumeSize = oldVolume.getSize();
692+
if (newVolumeSize != oldVolumeSize) {
693+
if (oldVolumeSize < newVolumeSize) {
694+
resourceLimitMgr.incrementResourceCount(oldVolume.getAccountId(), ResourceType.primary_storage, oldVolume.isDisplayVolume(), newVolumeSize - oldVolumeSize);
695+
} else {
696+
resourceLimitMgr.decrementResourceCount(oldVolume.getAccountId(), ResourceType.primary_storage, oldVolume.isDisplayVolume(), oldVolumeSize - newVolumeSize);
697+
}
698+
}
699+
}
700+
681701
protected void handleProcessEventCopyCmdAnswerNotPrimaryStore(VolumeObjectTO newVolume) {
682702
VolumeDataStoreVO volStore = volumeStoreDao.findByStoreVolume(dataStore.getId(), getId());
683703

@@ -709,6 +729,7 @@ protected void handleProcessEventAnswer(CreateObjectAnswer createObjectAnswer, b
709729
VolumeObjectTO newVolume = (VolumeObjectTO)createObjectAnswer.getData();
710730
VolumeVO volumeVo = volumeDao.findById(getId());
711731
updateVolumeInfo(newVolume, volumeVo, true, setFormat);
732+
updateResourceCount(newVolume, volumeVo);
712733
}
713734

714735
protected void handleProcessEventAnswer(DownloadAnswer downloadAnswer) {

server/src/main/java/com/cloud/api/query/dao/AccountJoinDaoImpl.java

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import com.cloud.api.query.ViewResponseHelper;
3535
import com.cloud.api.query.vo.AccountJoinVO;
3636
import com.cloud.api.query.vo.UserAccountJoinVO;
37+
import com.cloud.configuration.Resource;
3738
import com.cloud.configuration.Resource.ResourceType;
3839
import com.cloud.user.Account;
3940
import com.cloud.user.AccountManager;
@@ -85,9 +86,9 @@ public AccountResponse newAccountResponse(ResponseView view, EnumSet<DomainDetai
8586

8687
//get resource limits for projects
8788
long projectLimit = ApiDBUtils.findCorrectResourceLimit(account.getProjectLimit(), account.getId(), ResourceType.project);
88-
String projectLimitDisplay = (fullView || projectLimit == -1) ? "Unlimited" : String.valueOf(projectLimit);
89+
String projectLimitDisplay = (fullView || projectLimit == -1) ? Resource.UNLIMITED : String.valueOf(projectLimit);
8990
long projectTotal = (account.getProjectTotal() == null) ? 0 : account.getProjectTotal();
90-
String projectAvail = (fullView || projectLimit == -1) ? "Unlimited" : String.valueOf(projectLimit - projectTotal);
91+
String projectAvail = (fullView || projectLimit == -1) ? Resource.UNLIMITED : String.valueOf(projectLimit - projectTotal);
9192
accountResponse.setProjectLimit(projectLimitDisplay);
9293
accountResponse.setProjectTotal(projectTotal);
9394
accountResponse.setProjectAvailable(projectAvail);
@@ -118,15 +119,15 @@ public AccountResponse newAccountResponse(ResponseView view, EnumSet<DomainDetai
118119
public void setResourceLimits(AccountJoinVO account, boolean fullView, ResourceLimitAndCountResponse response) {
119120
// Get resource limits and counts
120121
long vmLimit = ApiDBUtils.findCorrectResourceLimit(account.getVmLimit(), account.getId(), ResourceType.user_vm);
121-
String vmLimitDisplay = (fullView || vmLimit == -1) ? "Unlimited" : String.valueOf(vmLimit);
122+
String vmLimitDisplay = (fullView || vmLimit == -1) ? Resource.UNLIMITED : String.valueOf(vmLimit);
122123
long vmTotal = (account.getVmTotal() == null) ? 0 : account.getVmTotal();
123-
String vmAvail = (fullView || vmLimit == -1) ? "Unlimited" : String.valueOf(vmLimit - vmTotal);
124+
String vmAvail = (fullView || vmLimit == -1) ? Resource.UNLIMITED : String.valueOf(vmLimit - vmTotal);
124125
response.setVmLimit(vmLimitDisplay);
125126
response.setVmTotal(vmTotal);
126127
response.setVmAvailable(vmAvail);
127128

128129
long ipLimit = ApiDBUtils.findCorrectResourceLimit(account.getIpLimit(), account.getId(), ResourceType.public_ip);
129-
String ipLimitDisplay = (fullView || ipLimit == -1) ? "Unlimited" : String.valueOf(ipLimit);
130+
String ipLimitDisplay = (fullView || ipLimit == -1) ? Resource.UNLIMITED : String.valueOf(ipLimit);
130131
long ipTotal = (account.getIpTotal() == null) ? 0 : account.getIpTotal();
131132

132133
Long ips = ipLimit - ipTotal;
@@ -139,32 +140,32 @@ public void setResourceLimits(AccountJoinVO account, boolean fullView, ResourceL
139140
unlimited = false;
140141
}
141142

142-
String ipAvail = ((fullView || ipLimit == -1) && unlimited) ? "Unlimited" : String.valueOf(ips);
143+
String ipAvail = ((fullView || ipLimit == -1) && unlimited) ? Resource.UNLIMITED : String.valueOf(ips);
143144

144145
response.setIpLimit(ipLimitDisplay);
145146
response.setIpTotal(ipTotal);
146147
response.setIpAvailable(ipAvail);
147148

148149
long volumeLimit = ApiDBUtils.findCorrectResourceLimit(account.getVolumeLimit(), account.getId(), ResourceType.volume);
149-
String volumeLimitDisplay = (fullView || volumeLimit == -1) ? "Unlimited" : String.valueOf(volumeLimit);
150+
String volumeLimitDisplay = (fullView || volumeLimit == -1) ? Resource.UNLIMITED : String.valueOf(volumeLimit);
150151
long volumeTotal = (account.getVolumeTotal() == null) ? 0 : account.getVolumeTotal();
151-
String volumeAvail = (fullView || volumeLimit == -1) ? "Unlimited" : String.valueOf(volumeLimit - volumeTotal);
152+
String volumeAvail = (fullView || volumeLimit == -1) ? Resource.UNLIMITED : String.valueOf(volumeLimit - volumeTotal);
152153
response.setVolumeLimit(volumeLimitDisplay);
153154
response.setVolumeTotal(volumeTotal);
154155
response.setVolumeAvailable(volumeAvail);
155156

156157
long snapshotLimit = ApiDBUtils.findCorrectResourceLimit(account.getSnapshotLimit(), account.getId(), ResourceType.snapshot);
157-
String snapshotLimitDisplay = (fullView || snapshotLimit == -1) ? "Unlimited" : String.valueOf(snapshotLimit);
158+
String snapshotLimitDisplay = (fullView || snapshotLimit == -1) ? Resource.UNLIMITED : String.valueOf(snapshotLimit);
158159
long snapshotTotal = (account.getSnapshotTotal() == null) ? 0 : account.getSnapshotTotal();
159-
String snapshotAvail = (fullView || snapshotLimit == -1) ? "Unlimited" : String.valueOf(snapshotLimit - snapshotTotal);
160+
String snapshotAvail = (fullView || snapshotLimit == -1) ? Resource.UNLIMITED : String.valueOf(snapshotLimit - snapshotTotal);
160161
response.setSnapshotLimit(snapshotLimitDisplay);
161162
response.setSnapshotTotal(snapshotTotal);
162163
response.setSnapshotAvailable(snapshotAvail);
163164

164165
Long templateLimit = ApiDBUtils.findCorrectResourceLimit(account.getTemplateLimit(), account.getId(), ResourceType.template);
165-
String templateLimitDisplay = (fullView || templateLimit == -1) ? "Unlimited" : String.valueOf(templateLimit);
166+
String templateLimitDisplay = (fullView || templateLimit == -1) ? Resource.UNLIMITED : String.valueOf(templateLimit);
166167
Long templateTotal = (account.getTemplateTotal() == null) ? 0 : account.getTemplateTotal();
167-
String templateAvail = (fullView || templateLimit == -1) ? "Unlimited" : String.valueOf(templateLimit - templateTotal);
168+
String templateAvail = (fullView || templateLimit == -1) ? Resource.UNLIMITED : String.valueOf(templateLimit - templateTotal);
168169
response.setTemplateLimit(templateLimitDisplay);
169170
response.setTemplateTotal(templateTotal);
170171
response.setTemplateAvailable(templateAvail);
@@ -175,55 +176,55 @@ public void setResourceLimits(AccountJoinVO account, boolean fullView, ResourceL
175176

176177
//get resource limits for networks
177178
long networkLimit = ApiDBUtils.findCorrectResourceLimit(account.getNetworkLimit(), account.getId(), ResourceType.network);
178-
String networkLimitDisplay = (fullView || networkLimit == -1) ? "Unlimited" : String.valueOf(networkLimit);
179+
String networkLimitDisplay = (fullView || networkLimit == -1) ? Resource.UNLIMITED : String.valueOf(networkLimit);
179180
long networkTotal = (account.getNetworkTotal() == null) ? 0 : account.getNetworkTotal();
180-
String networkAvail = (fullView || networkLimit == -1) ? "Unlimited" : String.valueOf(networkLimit - networkTotal);
181+
String networkAvail = (fullView || networkLimit == -1) ? Resource.UNLIMITED : String.valueOf(networkLimit - networkTotal);
181182
response.setNetworkLimit(networkLimitDisplay);
182183
response.setNetworkTotal(networkTotal);
183184
response.setNetworkAvailable(networkAvail);
184185

185186
//get resource limits for vpcs
186187
long vpcLimit = ApiDBUtils.findCorrectResourceLimit(account.getVpcLimit(), account.getId(), ResourceType.vpc);
187-
String vpcLimitDisplay = (fullView || vpcLimit == -1) ? "Unlimited" : String.valueOf(vpcLimit);
188+
String vpcLimitDisplay = (fullView || vpcLimit == -1) ? Resource.UNLIMITED : String.valueOf(vpcLimit);
188189
long vpcTotal = (account.getVpcTotal() == null) ? 0 : account.getVpcTotal();
189-
String vpcAvail = (fullView || vpcLimit == -1) ? "Unlimited" : String.valueOf(vpcLimit - vpcTotal);
190+
String vpcAvail = (fullView || vpcLimit == -1) ? Resource.UNLIMITED : String.valueOf(vpcLimit - vpcTotal);
190191
response.setVpcLimit(vpcLimitDisplay);
191192
response.setVpcTotal(vpcTotal);
192193
response.setVpcAvailable(vpcAvail);
193194

194195
//get resource limits for cpu cores
195196
long cpuLimit = ApiDBUtils.findCorrectResourceLimit(account.getCpuLimit(), account.getId(), ResourceType.cpu);
196-
String cpuLimitDisplay = (fullView || cpuLimit == -1) ? "Unlimited" : String.valueOf(cpuLimit);
197+
String cpuLimitDisplay = (fullView || cpuLimit == -1) ? Resource.UNLIMITED : String.valueOf(cpuLimit);
197198
long cpuTotal = (account.getCpuTotal() == null) ? 0 : account.getCpuTotal();
198-
String cpuAvail = (fullView || cpuLimit == -1) ? "Unlimited" : String.valueOf(cpuLimit - cpuTotal);
199+
String cpuAvail = (fullView || cpuLimit == -1) ? Resource.UNLIMITED : String.valueOf(cpuLimit - cpuTotal);
199200
response.setCpuLimit(cpuLimitDisplay);
200201
response.setCpuTotal(cpuTotal);
201202
response.setCpuAvailable(cpuAvail);
202203

203204
//get resource limits for memory
204205
long memoryLimit = ApiDBUtils.findCorrectResourceLimit(account.getMemoryLimit(), account.getId(), ResourceType.memory);
205-
String memoryLimitDisplay = (fullView || memoryLimit == -1) ? "Unlimited" : String.valueOf(memoryLimit);
206+
String memoryLimitDisplay = (fullView || memoryLimit == -1) ? Resource.UNLIMITED : String.valueOf(memoryLimit);
206207
long memoryTotal = (account.getMemoryTotal() == null) ? 0 : account.getMemoryTotal();
207-
String memoryAvail = (fullView || memoryLimit == -1) ? "Unlimited" : String.valueOf(memoryLimit - memoryTotal);
208+
String memoryAvail = (fullView || memoryLimit == -1) ? Resource.UNLIMITED : String.valueOf(memoryLimit - memoryTotal);
208209
response.setMemoryLimit(memoryLimitDisplay);
209210
response.setMemoryTotal(memoryTotal);
210211
response.setMemoryAvailable(memoryAvail);
211212

212213
//get resource limits for primary storage space and convert it from Bytes to GiB
213214
long primaryStorageLimit = ApiDBUtils.findCorrectResourceLimit(account.getPrimaryStorageLimit(), account.getId(), ResourceType.primary_storage);
214-
String primaryStorageLimitDisplay = (fullView || primaryStorageLimit == -1) ? "Unlimited" : String.valueOf(primaryStorageLimit / ResourceType.bytesToGiB);
215+
String primaryStorageLimitDisplay = (fullView || primaryStorageLimit == -1) ? Resource.UNLIMITED : String.valueOf(primaryStorageLimit / ResourceType.bytesToGiB);
215216
long primaryStorageTotal = (account.getPrimaryStorageTotal() == null) ? 0 : (account.getPrimaryStorageTotal() / ResourceType.bytesToGiB);
216-
String primaryStorageAvail = (fullView || primaryStorageLimit == -1) ? "Unlimited" : String.valueOf((primaryStorageLimit / ResourceType.bytesToGiB) - primaryStorageTotal);
217+
String primaryStorageAvail = (fullView || primaryStorageLimit == -1) ? Resource.UNLIMITED : String.valueOf((primaryStorageLimit / ResourceType.bytesToGiB) - primaryStorageTotal);
217218

218219
response.setPrimaryStorageLimit(primaryStorageLimitDisplay);
219220
response.setPrimaryStorageTotal(primaryStorageTotal);
220221
response.setPrimaryStorageAvailable(primaryStorageAvail);
221222

222223
//get resource limits for secondary storage space and convert it from Bytes to GiB
223224
long secondaryStorageLimit = ApiDBUtils.findCorrectResourceLimit(account.getSecondaryStorageLimit(), account.getId(), ResourceType.secondary_storage);
224-
String secondaryStorageLimitDisplay = (fullView || secondaryStorageLimit == -1) ? "Unlimited" : String.valueOf(secondaryStorageLimit / ResourceType.bytesToGiB);
225+
String secondaryStorageLimitDisplay = (fullView || secondaryStorageLimit == -1) ? Resource.UNLIMITED : String.valueOf(secondaryStorageLimit / ResourceType.bytesToGiB);
225226
float secondaryStorageTotal = (account.getSecondaryStorageTotal() == null) ? 0 : (account.getSecondaryStorageTotal() / (ResourceType.bytesToGiB * 1f));
226-
String secondaryStorageAvail = (fullView || secondaryStorageLimit == -1) ? "Unlimited" : String.valueOf((secondaryStorageLimit / ResourceType.bytesToGiB)
227+
String secondaryStorageAvail = (fullView || secondaryStorageLimit == -1) ? Resource.UNLIMITED : String.valueOf(( (double)secondaryStorageLimit / ResourceType.bytesToGiB)
227228
- secondaryStorageTotal);
228229

229230
response.setSecondaryStorageLimit(secondaryStorageLimitDisplay);

0 commit comments

Comments
 (0)