diff --git a/server/src/com/cloud/storage/StorageManagerImpl.java b/server/src/com/cloud/storage/StorageManagerImpl.java index 508dd59501c..a6fd9a8592f 100644 --- a/server/src/com/cloud/storage/StorageManagerImpl.java +++ b/server/src/com/cloud/storage/StorageManagerImpl.java @@ -676,39 +676,26 @@ public class StorageManagerImpl implements StorageManager { dskCh = createDiskCharacteristics(volume, template, dc, diskOffering); } - Transaction txn = Transaction.currentTxn(); - - VolumeType volType = volume.getVolumeType(); - VolumeTO created = null; int retry = _retry; while (--retry >= 0) { created = null; - txn.start(); - long podId = pod.getId(); - pod = _podDao.lock(podId, true); + pod = _podDao.findById(podId); if (pod == null) { - txn.rollback(); - throw new CloudRuntimeException("Unable to acquire lock on the pod " + podId); + throw new CloudRuntimeException("Unable to find pod " + podId); } pool = findStoragePool(dskCh, dc, pod, clusterId, offering, vm, template, avoidPools); if (pool == null) { - txn.rollback(); - break; + throw new CloudRuntimeException("Unable to find storgae pool in cluster " + clusterId + " when create volume " + volume.getName()); } avoidPools.add(pool); if (s_logger.isDebugEnabled()) { s_logger.debug("Trying to create " + volume + " on " + pool); } - - volume.setPoolId(pool.getId()); - _volsDao.persist(volume); - - txn.commit(); CreateCommand cmd = null; VMTemplateStoragePoolVO tmpltStoredOn = null; @@ -728,12 +715,10 @@ public class StorageManagerImpl implements StorageManager { break; } - volume.setPoolId(pool.getId()); - _volsDao.persist(volume); - s_logger.debug("Retrying the create because it failed on pool " + pool); } - + Transaction txn = Transaction.currentTxn(); + txn.start(); if (created == null) { if (s_logger.isDebugEnabled()) { s_logger.debug("Unable to create a volume for " + volume); @@ -741,17 +726,21 @@ public class StorageManagerImpl implements StorageManager { volume.setStatus(AsyncInstanceCreateStatus.Failed); volume.setDestroyed(true); _volsDao.persist(volume); + _volsDao.remove(volume.getId()); + volume = null; - return null; + } else { + + volume.setStatus(AsyncInstanceCreateStatus.Created); + volume.setFolder(pool.getPath()); + volume.setPath(created.getPath()); + volume.setSize(created.getSize()); + volume.setPoolType(pool.getPoolType()); + volume.setPoolId(pool.getId()); + volume.setPodId(pod.getId()); + _volsDao.persist(volume); } - - volume.setStatus(AsyncInstanceCreateStatus.Created); - volume.setFolder(pool.getPath()); - volume.setPath(created.getPath()); - volume.setSize(created.getSize()); - volume.setPoolType(pool.getPoolType()); - volume.setPodId(pod.getId()); - _volsDao.persist(volume); + txn.commit(); return volume; } @@ -996,6 +985,7 @@ public class StorageManagerImpl implements StorageManager { SearchBuilder volSearch = _volsDao.createSearchBuilder(); PoolsUsedByVmSearch.join("volumes", volSearch, volSearch.entity().getPoolId(), PoolsUsedByVmSearch.entity().getId()); volSearch.and("vm", volSearch.entity().getInstanceId(), SearchCriteria.Op.EQ); + volSearch.and("status", volSearch.entity().getStatus(), SearchCriteria.Op.EQ); volSearch.done(); PoolsUsedByVmSearch.done(); @@ -1838,7 +1828,7 @@ public class StorageManagerImpl implements StorageManager { public List getStoragePoolsForVm(long vmId) { SearchCriteria sc = PoolsUsedByVmSearch.create(); sc.setJoinParameters("volumes", "vm", vmId); - + sc.setJoinParameters("volumes", "status", AsyncInstanceCreateStatus.Created.toString()); return _storagePoolDao.search(sc, null); } diff --git a/server/src/com/cloud/vm/UserVmManagerImpl.java b/server/src/com/cloud/vm/UserVmManagerImpl.java index 82e683d2020..8d8f43d4feb 100755 --- a/server/src/com/cloud/vm/UserVmManagerImpl.java +++ b/server/src/com/cloud/vm/UserVmManagerImpl.java @@ -625,8 +625,20 @@ public class UserVmManagerImpl implements UserVmManager { DataCenterVO dc = _dcDao.findById(vm.getDataCenterId()); HostPodVO pod = _podDao.findById(vm.getPodId()); + List sps = _storageMgr.getStoragePoolsForVm(vm.getId()); - StoragePoolVO sp = sps.get(0); // FIXME + if( sps.size() == 0 ) { + throw new RuntimeException("Volume is not created for VM " + vm.getName()); + } + StoragePoolVO sp = sps.get(0); + for (StoragePoolVO tsp: sps ) { + // use the local storage pool to choose host, + // shared storage pool should be in the same cluster as local storage pool + if( tsp.isLocal()) { + sp = tsp; + break; + } + } VMTemplateVO template = _templateDao.findById(vm.getTemplateId()); ServiceOffering offering = _offeringDao.findById(vm.getServiceOfferingId());