Skip to content

Commit 414d415

Browse files
committed
CLOUDSTACK-5281:
Resource limit shouldnt be counted for resources with display flag = 0. Adding functions to resourcelimitmanager and doing it for the volumes at the moment.
1 parent 8ce6d52 commit 414d415

7 files changed

Lines changed: 137 additions & 26 deletions

File tree

api/src/com/cloud/storage/VolumeApiService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public interface VolumeApiService {
8787

8888
Snapshot allocSnapshot(Long volumeId, Long policyId) throws ResourceAllocationException;
8989

90-
Volume updateVolume(long volumeId, String path, String state, Long storageId, Boolean displayVolume, String customId);
90+
Volume updateVolume(long volumeId, String path, String state, Long storageId, Boolean displayVolume, String customId, long owner);
9191

9292
/**
9393
* Extracts the volume to a particular location.

api/src/com/cloud/user/ResourceLimitService.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,40 @@ public interface ResourceLimitService {
139139
*/
140140
public long getResourceCount(Account account, ResourceType type);
141141

142+
143+
/**
144+
* Checks if a limit has been exceeded for an account depending on the displayResource flag
145+
*
146+
* @param account
147+
* @param type
148+
* @param displayResource
149+
* @param count
150+
* the number of resources being allocated, count will be added to current allocation and compared
151+
* against maximum allowed allocation
152+
* @throws ResourceAllocationException
153+
*/
154+
void checkResourceLimit(Account account, ResourceType type, Boolean displayResource, long... count) throws ResourceAllocationException;
155+
156+
157+
/**
158+
* Increments the resource count depending on the displayResource flag
159+
*
160+
* @param accountId
161+
* @param type
162+
* @param displayResource
163+
* @param delta
164+
*/
165+
void incrementResourceCount(long accountId, ResourceType type, Boolean displayResource, Long... delta);
166+
167+
/**
168+
* Increments/Decrements the resource count depending on the displayResource flag
169+
*
170+
* @param accountId
171+
* @param type
172+
* @param displayResource
173+
* @param delta
174+
*/
175+
void changeResourceCount(long accountId, ResourceType type, Boolean displayResource, Long... delta);
176+
177+
void decrementResourceCount(long accountId, ResourceType type, Boolean displayResource, Long... delta);
142178
}

api/src/org/apache/cloudstack/api/command/user/volume/UpdateVolumeCmd.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public String getEventDescription() {
141141
@Override
142142
public void execute() {
143143
CallContext.current().setEventDetails("Volume Id: " + getId());
144-
Volume result = _volumeService.updateVolume(getId(), getPath(), getState(), getStorageId(), getDisplayVolume(), getCustomId());
144+
Volume result = _volumeService.updateVolume(getId(), getPath(), getState(), getStorageId(), getDisplayVolume(), getCustomId(), getEntityOwnerId());
145145
if (result != null) {
146146
VolumeResponse response = _responseGenerator.createVolumeResponse(result);
147147
response.setResponseName(getCommandName());

engine/schema/src/com/cloud/storage/dao/VolumeDaoImpl.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ public VolumeDaoImpl() {
346346
CountByAccount.select(null, Func.COUNT, null);
347347
CountByAccount.and("account", CountByAccount.entity().getAccountId(), SearchCriteria.Op.EQ);
348348
CountByAccount.and("state", CountByAccount.entity().getState(), SearchCriteria.Op.NIN);
349+
CountByAccount.and("displayVolume", CountByAccount.entity().isDisplayVolume(), Op.EQ);
349350
CountByAccount.done();
350351

351352
primaryStorageSearch = createSearchBuilder(SumCount.class);
@@ -355,6 +356,7 @@ public VolumeDaoImpl() {
355356
primaryStorageSearch.and().op("path", primaryStorageSearch.entity().getPath(), Op.NNULL);
356357
primaryStorageSearch.or("states", primaryStorageSearch.entity().getState(), Op.IN);
357358
primaryStorageSearch.cp();
359+
primaryStorageSearch.and("displayVolume", primaryStorageSearch.entity().isDisplayVolume(), Op.EQ);
358360
primaryStorageSearch.and("isRemoved", primaryStorageSearch.entity().getRemoved(), Op.NULL);
359361
primaryStorageSearch.done();
360362

@@ -382,6 +384,7 @@ public Long countAllocatedVolumesForAccount(long accountId) {
382384
SearchCriteria<Long> sc = CountByAccount.create();
383385
sc.setParameters("account", accountId);
384386
sc.setParameters("state", Volume.State.Destroy);
387+
sc.setParameters("displayVolume", 1);
385388
return customSearch(sc, null).get(0);
386389
}
387390

@@ -393,6 +396,7 @@ public long primaryStorageUsedForAccount(long accountId, List<Long> virtualRoute
393396
sc.setParameters("virtualRouterVmIds", virtualRouters.toArray(new Object[virtualRouters.size()]));
394397
}
395398
sc.setParameters("states", State.Allocated);
399+
sc.setParameters("displayVolume", 1);
396400
List<SumCount> storageSpace = customSearch(sc, null);
397401
if (storageSpace != null) {
398402
return storageSpace.get(0).sum;

server/src/com/cloud/resourcelimit/ResourceLimitManagerImpl.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,53 @@ public long getResourceCount(Account account, ResourceType type) {
954954
return _resourceCountDao.getResourceCount(account.getId(), ResourceOwnerType.Account, type);
955955
}
956956

957+
@Override
958+
public void checkResourceLimit(Account account, ResourceType type, Boolean displayResource, long... count) throws ResourceAllocationException {
959+
960+
// By default its always on.
961+
// TODO boilerplate code.
962+
boolean displayflag = (displayResource == null) || (displayResource != null && displayResource);
963+
964+
if(displayflag){
965+
checkResourceLimit(account, type, count);
966+
}
967+
}
968+
969+
@Override
970+
public void incrementResourceCount(long accountId, ResourceType type, Boolean displayResource, Long... delta) {
971+
972+
// 1. If its null assume displayResource = 1
973+
// 2. If its not null then increment if displayResource = 1
974+
if(displayResource == null || (displayResource != null && displayResource)){
975+
incrementResourceCount(accountId, type, delta);
976+
}
977+
}
978+
979+
@Override
980+
public void decrementResourceCount(long accountId, ResourceType type, Boolean displayResource, Long... delta) {
981+
982+
// 1. If its null assume displayResource = 1
983+
// 2. If its not null then decrement if displayResource = 1
984+
if(displayResource == null || (displayResource != null && displayResource)){
985+
decrementResourceCount(accountId, type, delta);
986+
}
987+
}
988+
989+
@Override
990+
public void changeResourceCount(long accountId, ResourceType type, Boolean displayResource, Long... delta) {
991+
992+
// meaning that the display flag is not changed so neither increment or decrement
993+
if(displayResource == null) return;
994+
995+
// Increment because the display is turned on.
996+
if(displayResource){
997+
// checkResourceLimit((Account)_accountDao.findById(accountId), type, delta);
998+
incrementResourceCount(accountId, type, delta);
999+
}else{
1000+
decrementResourceCount(accountId, type, delta);
1001+
}
1002+
}
1003+
9571004
protected class ResourceCountCheckTask extends ManagedContextRunnable {
9581005
public ResourceCountCheckTask() {
9591006

server/src/com/cloud/storage/VolumeApiServiceImpl.java

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -458,8 +458,16 @@ public VolumeVO allocVolume(CreateVolumeCmd cmd) throws ResourceAllocationExcept
458458
// permission check
459459
_accountMgr.checkAccess(caller, null, true, _accountMgr.getActiveAccountById(ownerId));
460460

461+
if (displayVolumeEnabled == null) {
462+
displayVolumeEnabled = true;
463+
} else {
464+
if (!_accountMgr.isRootAdmin(caller.getType())) {
465+
throw new PermissionDeniedException("Cannot update parameter displayvolume, only admin permitted ");
466+
}
467+
}
468+
461469
// Check that the resource limit for volumes won't be exceeded
462-
_resourceLimitMgr.checkResourceLimit(_accountMgr.getAccount(ownerId), ResourceType.volume);
470+
_resourceLimitMgr.checkResourceLimit(_accountMgr.getAccount(ownerId), ResourceType.volume, displayVolumeEnabled);
463471

464472
Long zoneId = cmd.getZoneId();
465473
Long diskOfferingId = null;
@@ -574,16 +582,8 @@ public VolumeVO allocVolume(CreateVolumeCmd cmd) throws ResourceAllocationExcept
574582
_accountMgr.checkAccess(caller, null, true, snapshotCheck);
575583
}
576584

577-
if (displayVolumeEnabled == null) {
578-
displayVolumeEnabled = true;
579-
} else {
580-
if (!_accountMgr.isRootAdmin(caller.getType())) {
581-
throw new PermissionDeniedException("Cannot update parameter displayvolume, only admin permitted ");
582-
}
583-
}
584-
585585
// Check that the resource limit for primary storage won't be exceeded
586-
_resourceLimitMgr.checkResourceLimit(_accountMgr.getAccount(ownerId), ResourceType.primary_storage, new Long(size));
586+
_resourceLimitMgr.checkResourceLimit(_accountMgr.getAccount(ownerId), ResourceType.primary_storage, displayVolumeEnabled, new Long(size));
587587

588588
// Verify that zone exists
589589
DataCenterVO zone = _dcDao.findById(zoneId);
@@ -652,8 +652,8 @@ public VolumeVO doInTransaction(TransactionStatus status) {
652652

653653
// Increment resource count during allocation; if actual creation fails,
654654
// decrement it
655-
_resourceLimitMgr.incrementResourceCount(volume.getAccountId(), ResourceType.volume);
656-
_resourceLimitMgr.incrementResourceCount(volume.getAccountId(), ResourceType.primary_storage, new Long(volume.getSize()));
655+
_resourceLimitMgr.incrementResourceCount(volume.getAccountId(), ResourceType.volume, displayVolumeEnabled);
656+
_resourceLimitMgr.incrementResourceCount(volume.getAccountId(), ResourceType.primary_storage, displayVolumeEnabled, new Long(volume.getSize()));
657657
return volume;
658658
}
659659
});
@@ -691,8 +691,8 @@ public VolumeVO createVolume(CreateVolumeCmd cmd) {
691691
} finally {
692692
if (!created) {
693693
s_logger.trace("Decrementing volume resource count for account id=" + volume.getAccountId() + " as volume failed to create on the backend");
694-
_resourceLimitMgr.decrementResourceCount(volume.getAccountId(), ResourceType.volume);
695-
_resourceLimitMgr.decrementResourceCount(volume.getAccountId(), ResourceType.primary_storage, new Long(volume.getSize()));
694+
_resourceLimitMgr.decrementResourceCount(volume.getAccountId(), ResourceType.volume, cmd.getDisplayVolume());
695+
_resourceLimitMgr.decrementResourceCount(volume.getAccountId(), ResourceType.primary_storage, cmd.getDisplayVolume(), new Long(volume.getSize()));
696696
}
697697
}
698698
}
@@ -825,7 +825,7 @@ public VolumeVO resizeVolume(ResizeVolumeCmd cmd) throws ResourceAllocationExcep
825825

826826
if (!shrinkOk) {
827827
/* Check resource limit for this account on primary storage resource */
828-
_resourceLimitMgr.checkResourceLimit(_accountMgr.getAccount(volume.getAccountId()), ResourceType.primary_storage, new Long(newSize - currentSize));
828+
_resourceLimitMgr.checkResourceLimit(_accountMgr.getAccount(volume.getAccountId()), ResourceType.primary_storage, volume.isDisplayVolume(), new Long(newSize - currentSize));
829829
}
830830

831831
/*
@@ -875,9 +875,9 @@ public VolumeVO resizeVolume(ResizeVolumeCmd cmd) throws ResourceAllocationExcep
875875

876876
/* Update resource count for the account on primary storage resource */
877877
if (!shrinkOk) {
878-
_resourceLimitMgr.incrementResourceCount(volume.getAccountId(), ResourceType.primary_storage, new Long(newSize - currentSize));
878+
_resourceLimitMgr.incrementResourceCount(volume.getAccountId(), ResourceType.primary_storage, volume.isDisplayVolume(), new Long(newSize - currentSize));
879879
} else {
880-
_resourceLimitMgr.decrementResourceCount(volume.getAccountId(), ResourceType.primary_storage, new Long(currentSize - newSize));
880+
_resourceLimitMgr.decrementResourceCount(volume.getAccountId(), ResourceType.primary_storage, volume.isDisplayVolume(), new Long(currentSize - newSize));
881881
}
882882
return volume;
883883
} catch (InterruptedException e) {
@@ -928,11 +928,11 @@ public boolean deleteVolume(long volumeId, Account caller) throws ConcurrentOper
928928
VMInstanceVO vmInstance = _vmInstanceDao.findById(instanceId);
929929
if (instanceId == null || (vmInstance.getType().equals(VirtualMachine.Type.User))) {
930930
// Decrement the resource count for volumes and primary storage belonging user VM's only
931-
_resourceLimitMgr.decrementResourceCount(volume.getAccountId(), ResourceType.volume);
931+
_resourceLimitMgr.decrementResourceCount(volume.getAccountId(), ResourceType.volume, volume.isDisplayVolume());
932932
/* If volume is in primary storage, decrement primary storage count else decrement secondary
933933
storage count (in case of upload volume). */
934934
if (volume.getFolder() != null || volume.getPath() != null || volume.getState() == Volume.State.Allocated) {
935-
_resourceLimitMgr.decrementResourceCount(volume.getAccountId(), ResourceType.primary_storage, new Long(volume.getSize()));
935+
_resourceLimitMgr.decrementResourceCount(volume.getAccountId(), ResourceType.primary_storage, volume.isDisplayVolume(), new Long(volume.getSize()));
936936
} else {
937937
_resourceLimitMgr.recalculateResourceCount(volume.getAccountId(), volume.getDomainId(), ResourceType.secondary_storage.getOrdinal());
938938
}
@@ -1140,17 +1140,14 @@ public Volume attachVolumeToVM(AttachVolumeCmd command) {
11401140

11411141
@Override
11421142
@ActionEvent(eventType = EventTypes.EVENT_VOLUME_UPDATE, eventDescription = "updating volume", async = true)
1143-
public Volume updateVolume(long volumeId, String path, String state, Long storageId, Boolean displayVolume, String customId) {
1143+
public Volume updateVolume(long volumeId, String path, String state, Long storageId, Boolean displayVolume, String customId, long entityOwnerId) {
1144+
11441145
VolumeVO volume = _volumeDao.findById(volumeId);
11451146

11461147
if (path != null) {
11471148
volume.setPath(path);
11481149
}
11491150

1150-
if (displayVolume != null) {
1151-
volume.setDisplayVolume(displayVolume);
1152-
}
1153-
11541151
if (state != null) {
11551152
try {
11561153
Volume.State volumeState = Volume.State.valueOf(state);
@@ -1173,6 +1170,12 @@ public Volume updateVolume(long volumeId, String path, String state, Long storag
11731170
volume.setUuid(customId);
11741171
}
11751172

1173+
if (displayVolume != null && displayVolume != volume.isDisplayVolume()) { // No need to check permissions since only Admin allowed to call this API.
1174+
volume.setDisplayVolume(displayVolume);
1175+
_resourceLimitMgr.changeResourceCount(entityOwnerId, ResourceType.volume, displayVolume);
1176+
_resourceLimitMgr.changeResourceCount(entityOwnerId, ResourceType.primary_storage, displayVolume, new Long(volume.getSize()));
1177+
}
1178+
11761179
_volumeDao.update(volumeId, volume);
11771180

11781181
return volume;

server/test/com/cloud/vpc/MockResourceLimitManagerImpl.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import javax.ejb.Local;
2323
import javax.naming.ConfigurationException;
2424

25+
import com.cloud.configuration.Resource;
2526
import org.springframework.stereotype.Component;
2627

2728
import com.cloud.configuration.Resource.ResourceType;
@@ -148,6 +149,26 @@ public long getResourceCount(Account account, ResourceType type) {
148149
return 0;
149150
}
150151

152+
@Override
153+
public void checkResourceLimit(Account account, ResourceType type, Boolean displayResource, long... count) throws ResourceAllocationException {
154+
//To change body of implemented methods use File | Settings | File Templates.
155+
}
156+
157+
@Override
158+
public void incrementResourceCount(long accountId, ResourceType type, Boolean displayResource, Long... delta) {
159+
//To change body of implemented methods use File | Settings | File Templates.
160+
}
161+
162+
@Override
163+
public void changeResourceCount(long accountId, ResourceType type, Boolean displayResource, Long... delta) {
164+
//To change body of implemented methods use File | Settings | File Templates.
165+
}
166+
167+
@Override
168+
public void decrementResourceCount(long accountId, ResourceType type, Boolean displayResource, Long... delta) {
169+
//To change body of implemented methods use File | Settings | File Templates.
170+
}
171+
151172
/* (non-Javadoc)
152173
* @see com.cloud.utils.component.Manager#configure(java.lang.String, java.util.Map)
153174
*/

0 commit comments

Comments
 (0)