Skip to content

Commit 50ffa95

Browse files
committed
ifix CS-15609 Volumes can be created as a part of VM creation when
un-allocated space is insufficient on primary storage check the availability of un-allocated primary storage space during planning stage, for multiple-volume VM creation scenario modification in StorageManagerImpl.java and StorageManager.java: add a new method storagePoolHasEnoughSpace(List<Volumes>, StoragePool), check if storagePool has enough space for all requested volumes modification in FirstfitPlanner.findPotentialDeploymentResources: handle multiple volume case, keep track of allocated volumes for pools and call storagePoolHasEnoughSpace to check space availability modification in AbstractStoragePoolAllocator.java: extract capacity computation logic and make a new method in StorageManagerImpl RB: https://reviews.apache.org/r/6028/ Send-by: mice_xia@tcloudcomputing.com
1 parent 58a9b94 commit 50ffa95

4 files changed

Lines changed: 146 additions & 74 deletions

File tree

‎server/src/com/cloud/deploy/FirstFitPlanner.java‎

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717
package com.cloud.deploy;
1818

1919
import java.util.ArrayList;
20+
import java.util.Comparator;
2021
import java.util.Enumeration;
2122
import java.util.HashMap;
2223
import java.util.List;
2324
import java.util.Map;
25+
import java.util.TreeSet;
2426

2527
import javax.ejb.Local;
2628
import javax.naming.ConfigurationException;
@@ -55,6 +57,7 @@
5557
import com.cloud.org.Grouping;
5658
import com.cloud.resource.ResourceState;
5759
import com.cloud.storage.DiskOfferingVO;
60+
import com.cloud.storage.StorageManager;
5861
import com.cloud.storage.StoragePool;
5962
import com.cloud.storage.StoragePoolHostVO;
6063
import com.cloud.storage.StoragePoolVO;
@@ -98,6 +101,7 @@ public class FirstFitPlanner extends PlannerBase implements DeploymentPlanner {
98101
@Inject protected StoragePoolDao _storagePoolDao;
99102
@Inject protected CapacityDao _capacityDao;
100103
@Inject protected AccountManager _accountMgr;
104+
@Inject protected StorageManager _storageMgr;
101105

102106
@Inject(adapter=StoragePoolAllocator.class)
103107
protected Adapters<StoragePoolAllocator> _storagePoolAllocators;
@@ -638,25 +642,56 @@ protected Pair<Host, Map<Volume, StoragePool>> findPotentialDeploymentResources(
638642
s_logger.debug("Trying to find a potenial host and associated storage pools from the suitable host/pool lists for this VM");
639643

640644
boolean hostCanAccessPool = false;
645+
boolean haveEnoughSpace = false;
641646
Map<Volume, StoragePool> storage = new HashMap<Volume, StoragePool>();
647+
TreeSet<Volume> volumesOrderBySizeDesc = new TreeSet<Volume>(new Comparator<Volume>() {
648+
@Override
649+
public int compare(Volume v1, Volume v2) {
650+
if(v1.getSize() < v2.getSize())
651+
return 1;
652+
else
653+
return -1;
654+
}
655+
});
656+
volumesOrderBySizeDesc.addAll(suitableVolumeStoragePools.keySet());
657+
boolean multipleVolume = volumesOrderBySizeDesc.size() > 1;
642658
for(Host potentialHost : suitableHosts){
643-
for(Volume vol : suitableVolumeStoragePools.keySet()){
659+
Map<StoragePool,List<Volume>> volumeAllocationMap = new HashMap<StoragePool,List<Volume>>();
660+
for(Volume vol : volumesOrderBySizeDesc){
661+
haveEnoughSpace = false;
644662
s_logger.debug("Checking if host: "+potentialHost.getId() +" can access any suitable storage pool for volume: "+ vol.getVolumeType());
645663
List<StoragePool> volumePoolList = suitableVolumeStoragePools.get(vol);
646664
hostCanAccessPool = false;
647665
for(StoragePool potentialSPool : volumePoolList){
648666
if(hostCanAccessSPool(potentialHost, potentialSPool)){
649-
storage.put(vol, potentialSPool);
650667
hostCanAccessPool = true;
668+
if(multipleVolume){
669+
List<Volume> requestVolumes = null;
670+
if(volumeAllocationMap.containsKey(potentialSPool))
671+
requestVolumes = volumeAllocationMap.get(potentialSPool);
672+
else
673+
requestVolumes = new ArrayList<Volume>();
674+
requestVolumes.add(vol);
675+
676+
if(!_storageMgr.storagePoolHasEnoughSpace(requestVolumes, potentialSPool))
677+
continue;
678+
volumeAllocationMap.put(potentialSPool,requestVolumes);
679+
}
680+
storage.put(vol, potentialSPool);
681+
haveEnoughSpace = true;
651682
break;
652683
}
653684
}
654685
if(!hostCanAccessPool){
655686
break;
656687
}
688+
if(!haveEnoughSpace) {
689+
s_logger.warn("insufficient capacity to allocate all volumes");
690+
break;
691+
}
657692
}
658-
if(hostCanAccessPool){
659-
s_logger.debug("Found a potential host " + "id: "+potentialHost.getId() + " name: " +potentialHost.getName()+ " and associated storage pools for this VM");
693+
if(hostCanAccessPool && haveEnoughSpace){
694+
s_logger.debug("Found a potential host " + "id: "+potentialHost.getId() + " name: " +potentialHost.getName() + " and associated storage pools for this VM");
660695
return new Pair<Host, Map<Volume, StoragePool>>(potentialHost, storage);
661696
}
662697
}

‎server/src/com/cloud/storage/StorageManager.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,4 +235,6 @@ VolumeVO copyVolumeFromSecToPrimary(VolumeVO volume, VMInstanceVO vm,
235235
String getSupportedImageFormatForCluster(Long clusterId);
236236

237237
HypervisorType getHypervisorTypeFromFormat(ImageFormat format);
238-
}
238+
239+
boolean storagePoolHasEnoughSpace(List<Volume> volume, StoragePool pool);
240+
}

‎server/src/com/cloud/storage/StorageManagerImpl.java‎

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
import com.cloud.api.commands.UploadVolumeCmd;
8181
import com.cloud.async.AsyncJobManager;
8282
import com.cloud.capacity.Capacity;
83+
import com.cloud.capacity.CapacityManager;
8384
import com.cloud.capacity.CapacityState;
8485
import com.cloud.capacity.CapacityVO;
8586
import com.cloud.capacity.dao.CapacityDao;
@@ -133,6 +134,7 @@
133134
import com.cloud.resource.ResourceState;
134135
import com.cloud.server.ManagementServer;
135136
import com.cloud.server.ResourceTag.TaggedResourceType;
137+
import com.cloud.server.StatsCollector;
136138
import com.cloud.service.ServiceOfferingVO;
137139
import com.cloud.service.dao.ServiceOfferingDao;
138140
import com.cloud.storage.Storage.ImageFormat;
@@ -274,6 +276,8 @@ public class StorageManagerImpl implements StorageManager, Manager, ClusterManag
274276
@Inject
275277
protected CapacityDao _capacityDao;
276278
@Inject
279+
protected CapacityManager _capacityMgr;
280+
@Inject
277281
protected DiskOfferingDao _diskOfferingDao;
278282
@Inject
279283
protected AccountDao _accountDao;
@@ -352,6 +356,9 @@ public class StorageManagerImpl implements StorageManager, Manager, ClusterManag
352356
private StateMachine2<Volume.State, Volume.Event, Volume> _volStateMachine;
353357
private int _customDiskOfferingMinSize = 1;
354358
private int _customDiskOfferingMaxSize = 1024;
359+
private double _storageUsedThreshold = 1.0d;
360+
private double _storageAllocatedThreshold = 1.0d;
361+
protected BigDecimal _storageOverprovisioningFactor = new BigDecimal(1);
355362

356363
public boolean share(VMInstanceVO vm, List<VolumeVO> vols, HostVO host, boolean cancelPreviousShare) throws StorageUnavailableException {
357364

@@ -955,6 +962,19 @@ public boolean configure(String name, Map<String, Object> params) throws Configu
955962
String time = configs.get("storage.cleanup.interval");
956963
_storageCleanupInterval = NumbersUtil.parseInt(time, 86400);
957964

965+
String storageUsedThreshold = configDao.getValue(Config.StorageCapacityDisableThreshold.key());
966+
if (storageUsedThreshold != null) {
967+
_storageUsedThreshold = Double.parseDouble(storageUsedThreshold);
968+
}
969+
970+
String storageAllocatedThreshold = configDao.getValue(Config.StorageAllocatedCapacityDisableThreshold.key());
971+
if (storageAllocatedThreshold != null) {
972+
_storageAllocatedThreshold = Double.parseDouble(storageAllocatedThreshold);
973+
}
974+
975+
String globalStorageOverprovisioningFactor = configs.get("storage.overprovisioning.factor");
976+
_storageOverprovisioningFactor = new BigDecimal(NumbersUtil.parseFloat(globalStorageOverprovisioningFactor, 2.0f));
977+
958978
s_logger.info("Storage cleanup enabled: " + _storageCleanupEnabled + ", interval: " + _storageCleanupInterval + ", template cleanup enabled: " + _templateCleanupEnabled);
959979

960980
String workers = configs.get("expunge.workers");
@@ -3906,5 +3926,81 @@ public HypervisorType getHypervisorTypeFromFormat(ImageFormat format) {
39063926
return HypervisorType.None;
39073927
}
39083928
}
3929+
3930+
private boolean checkUsagedSpace(StoragePool pool){
3931+
StatsCollector sc = StatsCollector.getInstance();
3932+
if (sc != null) {
3933+
long totalSize = pool.getCapacityBytes();
3934+
StorageStats stats = sc.getStoragePoolStats(pool.getId());
3935+
if(stats == null){
3936+
stats = sc.getStorageStats(pool.getId());
3937+
}
3938+
if (stats != null) {
3939+
double usedPercentage = ((double)stats.getByteUsed() / (double)totalSize);
3940+
if (s_logger.isDebugEnabled()) {
3941+
s_logger.debug("Checking pool " + pool.getId() + " for storage, totalSize: " + pool.getCapacityBytes() + ", usedBytes: " + stats.getByteUsed() + ", usedPct: " + usedPercentage + ", disable threshold: " + _storageUsedThreshold);
3942+
}
3943+
if (usedPercentage >= _storageUsedThreshold) {
3944+
if (s_logger.isDebugEnabled()) {
3945+
s_logger.debug("Insufficient space on pool: " + pool.getId() + " since its usage percentage: " +usedPercentage + " has crossed the pool.storage.capacity.disablethreshold: " + _storageUsedThreshold);
3946+
}
3947+
return false;
3948+
}
3949+
}
3950+
return true;
3951+
}
3952+
return false;
3953+
}
3954+
3955+
@Override
3956+
public boolean storagePoolHasEnoughSpace(List<Volume> volumes, StoragePool pool) {
3957+
if(volumes == null || volumes.isEmpty())
3958+
return false;
3959+
3960+
if(!checkUsagedSpace(pool))
3961+
return false;
3962+
3963+
// allocated space includes template of specified volume
3964+
StoragePoolVO poolVO = _storagePoolDao.findById(pool.getId());
3965+
long allocatedSizeWithtemplate = _capacityMgr.getAllocatedPoolCapacity(poolVO, null);
3966+
long totalAskingSize = 0;
3967+
for (Volume volume : volumes) {
3968+
if(volume.getTemplateId()!=null){
3969+
VMTemplateVO tmpl = _templateDao.findById(volume.getTemplateId());
3970+
if (tmpl.getFormat() != ImageFormat.ISO){
3971+
allocatedSizeWithtemplate = _capacityMgr.getAllocatedPoolCapacity(poolVO, tmpl);
3972+
}
3973+
}
3974+
if(volume.getState() != Volume.State.Ready)
3975+
totalAskingSize = totalAskingSize + volume.getSize();
3976+
}
3977+
3978+
long totalOverProvCapacity;
3979+
if (pool.getPoolType() == StoragePoolType.NetworkFilesystem) {
3980+
totalOverProvCapacity = _storageOverprovisioningFactor.multiply(new BigDecimal(pool.getCapacityBytes())).longValue();// All this for the inaccuracy of floats for big number multiplication.
3981+
}else {
3982+
totalOverProvCapacity = pool.getCapacityBytes();
3983+
}
3984+
3985+
if (s_logger.isDebugEnabled()) {
3986+
s_logger.debug("Checking pool: " + pool.getId() + " for volume allocation " + volumes.toString() + ", maxSize : " + totalOverProvCapacity + ", totalAllocatedSize : " + allocatedSizeWithtemplate + ", askingSize : " + totalAskingSize + ", allocated disable threshold: " + _storageAllocatedThreshold);
3987+
}
3988+
3989+
double usedPercentage = (allocatedSizeWithtemplate + totalAskingSize) / (double)(totalOverProvCapacity);
3990+
if (usedPercentage > _storageAllocatedThreshold){
3991+
if (s_logger.isDebugEnabled()) {
3992+
s_logger.debug("Insufficient un-allocated capacity on: " + pool.getId() + " for volume allocation: " + volumes.toString() + " since its allocated percentage: " +usedPercentage + " has crossed the allocated pool.storage.allocated.capacity.disablethreshold: " + _storageAllocatedThreshold + ", skipping this pool");
3993+
}
3994+
return false;
3995+
}
3996+
3997+
if (totalOverProvCapacity < (allocatedSizeWithtemplate + totalAskingSize)) {
3998+
if (s_logger.isDebugEnabled()) {
3999+
s_logger.debug("Insufficient un-allocated capacity on: " + pool.getId() + " for volume allocation: " + volumes.toString() + ", not enough storage, maxSize : " + totalOverProvCapacity + ", totalAllocatedSize : " + allocatedSizeWithtemplate + ", askingSize : " + totalAskingSize);
4000+
}
4001+
return false;
4002+
}
4003+
return true;
4004+
}
39094005

39104006
}

‎server/src/com/cloud/storage/allocator/AbstractStoragePoolAllocator.java‎

Lines changed: 8 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.cloud.storage.allocator;
1818

1919
import java.math.BigDecimal;
20+
import java.util.ArrayList;
2021
import java.util.List;
2122
import java.util.Map;
2223
import java.util.Random;
@@ -27,7 +28,6 @@
2728
import org.apache.log4j.Logger;
2829

2930
import com.cloud.capacity.CapacityManager;
30-
import com.cloud.configuration.Config;
3131
import com.cloud.configuration.dao.ConfigurationDao;
3232
import com.cloud.dc.ClusterVO;
3333
import com.cloud.dc.dao.ClusterDao;
@@ -41,13 +41,11 @@
4141
import com.cloud.storage.StoragePool;
4242
import com.cloud.storage.StoragePoolStatus;
4343
import com.cloud.storage.StoragePoolVO;
44-
import com.cloud.storage.StorageStats;
45-
import com.cloud.storage.VMTemplateHostVO;
4644
import com.cloud.storage.VMTemplateStoragePoolVO;
4745
import com.cloud.storage.VMTemplateStorageResourceAssoc;
4846
import com.cloud.storage.VMTemplateStorageResourceAssoc.Status;
49-
import com.cloud.storage.VMTemplateSwiftVO;
5047
import com.cloud.storage.VMTemplateVO;
48+
import com.cloud.storage.Volume;
5149
import com.cloud.storage.Volume.Type;
5250
import com.cloud.storage.dao.StoragePoolDao;
5351
import com.cloud.storage.dao.StoragePoolHostDao;
@@ -58,7 +56,6 @@
5856
import com.cloud.storage.swift.SwiftManager;
5957
import com.cloud.template.TemplateManager;
6058
import com.cloud.utils.NumbersUtil;
61-
import com.cloud.utils.Pair;
6259
import com.cloud.utils.component.AdapterBase;
6360
import com.cloud.utils.component.Inject;
6461
import com.cloud.vm.DiskProfile;
@@ -83,8 +80,6 @@ public abstract class AbstractStoragePoolAllocator extends AdapterBase implement
8380
long _extraBytesPerVolume = 0;
8481
Random _rand;
8582
boolean _dontMatter;
86-
double _storageUsedThreshold = 1.0d;
87-
double _storageAllocatedThreshold = 1.0d;
8883

8984
@Override
9085
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
@@ -97,17 +92,6 @@ public boolean configure(String name, Map<String, Object> params) throws Configu
9792

9893
_extraBytesPerVolume = 0;
9994

100-
101-
String storageUsedThreshold = _configDao.getValue(Config.StorageCapacityDisableThreshold.key());
102-
if (storageUsedThreshold != null) {
103-
_storageUsedThreshold = Double.parseDouble(storageUsedThreshold);
104-
}
105-
106-
String storageAllocatedThreshold = _configDao.getValue(Config.StorageAllocatedCapacityDisableThreshold.key());
107-
if (storageAllocatedThreshold != null) {
108-
_storageAllocatedThreshold = Double.parseDouble(storageAllocatedThreshold);
109-
}
110-
11195
_rand = new Random(System.currentTimeMillis());
11296

11397
_dontMatter = Boolean.parseBoolean(configs.get("storage.overwrite.provisioning"));
@@ -192,60 +176,15 @@ protected boolean checkPool(ExcludeList avoid, StoragePoolVO pool, DiskProfile d
192176
return false;
193177
}
194178

195-
// check the used size against the total size, skip this host if it's greater than the configured
196-
// capacity check "storage.capacity.threshold"
197-
if (sc != null) {
198-
long totalSize = pool.getCapacityBytes();
199-
StorageStats stats = sc.getStoragePoolStats(pool.getId());
200-
if(stats == null){
201-
stats = sc.getStorageStats(pool.getId());
202-
}
203-
if (stats != null) {
204-
double usedPercentage = ((double)stats.getByteUsed() / (double)totalSize);
205-
if (s_logger.isDebugEnabled()) {
206-
s_logger.debug("Attempting to look for pool " + pool.getId() + " for storage, totalSize: " + pool.getCapacityBytes() + ", usedBytes: " + stats.getByteUsed() + ", usedPct: " + usedPercentage + ", disable threshold: " + _storageUsedThreshold);
207-
}
208-
if (usedPercentage >= _storageUsedThreshold) {
209-
if (s_logger.isDebugEnabled()) {
210-
s_logger.debug("Cannot allocate this pool " + pool.getId() + " for storage since its usage percentage: " +usedPercentage + " has crossed the pool.storage.capacity.disablethreshold: " + _storageUsedThreshold + ", skipping this pool");
211-
}
212-
return false;
213-
}
214-
}
215-
}
216-
217-
long totalAllocatedSize = _capacityMgr.getAllocatedPoolCapacity(pool, null);
218-
long askingSize = dskCh.getSize();
219-
220-
long totalOverProvCapacity;
221-
if (pool.getPoolType() == StoragePoolType.NetworkFilesystem) {
222-
totalOverProvCapacity = _storageOverprovisioningFactor.multiply(new BigDecimal(pool.getCapacityBytes())).longValue();// All this for the inaccuracy of floats for big number multiplication.
223-
}else {
224-
totalOverProvCapacity = pool.getCapacityBytes();
225-
}
226179

227-
if (s_logger.isDebugEnabled()) {
228-
s_logger.debug("Attempting to look for pool " + pool.getId() + " for storage, maxSize : " + totalOverProvCapacity + ", totalAllocatedSize : " + totalAllocatedSize + ", askingSize : " + askingSize + ", allocated disable threshold: " + _storageAllocatedThreshold);
229-
}
230-
231-
double usedPercentage = (totalAllocatedSize + askingSize) / (double)(totalOverProvCapacity);
232-
if (usedPercentage > _storageAllocatedThreshold){
233-
if (s_logger.isDebugEnabled()) {
234-
s_logger.debug("Cannot allocate this pool " + pool.getId() + " for storage since its allocated percentage: " +usedPercentage + " has crossed the allocated pool.storage.allocated.capacity.disablethreshold: " + _storageAllocatedThreshold + ", skipping this pool");
235-
}
236-
return false;
237-
}
238-
239-
if (totalOverProvCapacity < (totalAllocatedSize + askingSize)) {
240-
if (s_logger.isDebugEnabled()) {
241-
s_logger.debug("Cannot allocate this pool " + pool.getId() + " for storage, not enough storage, maxSize : " + totalOverProvCapacity + ", totalAllocatedSize : " + totalAllocatedSize + ", askingSize : " + askingSize);
242-
}
180+
// check capacity
181+
Volume volume = _volumeDao.findById(dskCh.getVolumeId());
182+
List<Volume> requestVolumes = new ArrayList<Volume>();
183+
requestVolumes.add(volume);
184+
return _storageMgr.storagePoolHasEnoughSpace(requestVolumes, pool);
185+
}
243186

244-
return false;
245-
}
246187

247-
return true;
248-
}
249188

250189
@Override
251190
public String chooseStorageIp(VirtualMachine vm, Host host, Host storage) {

0 commit comments

Comments
 (0)