@Override public Volume.State getState() { return volumeVO.getState(); }
private boolean canVolumeBeRemoved(long volumeId) { VolumeVO vol = volDao.findById(volumeId); if (vol == null) { // already removed from volumes table return false; } VolumeDataStoreVO volumeStore = _volumeStoreDao.findByVolume(volumeId); if ((vol.getState() == State.Expunged || (vol.getPodId() == null && vol.getState() == State.Destroy)) && volumeStore == null) { // volume is expunged from primary, as well as on secondary return true; } else { return false; } }
@Override public void doInTransactionWithoutResult(TransactionStatus status) { for (VolumeVO vol : volumesForVm) { if (vol.getVolumeType().equals(Type.ROOT)) { // Destroy volume if not already destroyed boolean volumeAlreadyDestroyed = (vol.getState() == Volume.State.Destroy || vol.getState() == Volume.State.Expunged || vol.getState() == Volume.State.Expunging); if (!volumeAlreadyDestroyed) { volService.destroyVolume(vol.getId()); } else { s_logger.debug("Skipping destroy for the volume " + vol + " as its in state " + vol.getState().toString()); } toBeExpunged.add(vol); } else { if (s_logger.isDebugEnabled()) { s_logger.debug("Detaching " + vol); } _volsDao.detachVolume(vol.getId()); } } } });
private void addVolumeMetrics(final List<Item> metricsList, final long dcId, final String zoneName, final String zoneUuid) { int total = 0; int ready = 0; int destroyed = 0; for (final VolumeVO volume : volumeDao.findByDc(dcId)) { if (volume == null) { continue; } total++; if (volume.getState() == Volume.State.Ready) { ready++; } else if (volume.getState() == Volume.State.Destroy) { destroyed++; } } metricsList.add(new ItemVolume(zoneName, zoneUuid, Volume.State.Ready.name().toLowerCase(), ready)); metricsList.add(new ItemVolume(zoneName, zoneUuid, Volume.State.Destroy.name().toLowerCase(), destroyed)); metricsList.add(new ItemVolume(zoneName, zoneUuid, TOTAL, total)); }
@Override public boolean updateState(com.cloud.storage.Volume.State currentState, Event event, com.cloud.storage.Volume.State nextState, Volume vo, Object data) { Long oldUpdated = vo.getUpdatedCount(); Date oldUpdatedTime = vo.getUpdated(); SearchCriteria<VolumeVO> sc = AllFieldsSearch.create(); sc.setParameters("id", vo.getId()); sc.setParameters("state", currentState); sc.setParameters("updatedCount", vo.getUpdatedCount()); vo.incrUpdatedCount(); UpdateBuilder builder = getUpdateBuilder(vo); builder.set(vo, "state", nextState); builder.set(vo, "updated", new Date()); int rows = update((VolumeVO)vo, sc); if (rows == 0 && s_logger.isDebugEnabled()) { VolumeVO dbVol = findByIdIncludingRemoved(vo.getId()); if (dbVol != null) { StringBuilder str = new StringBuilder("Unable to update ").append(vo.toString()); str.append(": DB Data={id=").append(dbVol.getId()).append("; state=").append(dbVol.getState()).append("; updatecount=").append(dbVol.getUpdatedCount()).append(";updatedTime=") .append(dbVol.getUpdated()); str.append(": New Data={id=").append(vo.getId()).append("; state=").append(nextState).append("; event=").append(event).append("; updatecount=").append(vo.getUpdatedCount()) .append("; updatedTime=").append(vo.getUpdated()); str.append(": stale Data={id=").append(vo.getId()).append("; state=").append(currentState).append("; event=").append(event).append("; updatecount=").append(oldUpdated) .append("; updatedTime=").append(oldUpdatedTime); } else { s_logger.debug("Unable to update volume: id=" + vo.getId() + ", as there is no such volume exists in the database anymore"); } } return rows > 0; }
private void cleanupVolumeDuringSnapshotFailure(Long volumeId, Long snapshotId) { _snapshotSrv.cleanupVolumeDuringSnapshotFailure(volumeId, snapshotId); VolumeVO volume = _volsDao.findById(volumeId); if (volume.getState() == Volume.State.Snapshotting) { s_logger.debug("change volume state back to Ready: " + volume.getId()); volume.setState(Volume.State.Ready); _volsDao.update(volume.getId(), volume); } }
/** * Destroy the volume if possible and then decrement the following resource types. * <ul> * <li> {@link ResourceType#volume}; * <li> {@link ResourceType#primary_storage} * </ul> * * A volume can be destroyed if it is not in any of the following states. * <ul> * <li> {@value Volume.State#Destroy}; * <li> {@value Volume.State#Expunging}; * <li> {@value Volume.State#Expunged}. * </ul> * * The volume is destroyed via {@link VolumeService#destroyVolume(long)} method. */ protected void destroyVolumeIfPossible(VolumeVO volume) { if (volume.getState() != Volume.State.Destroy && volume.getState() != Volume.State.Expunging && volume.getState() != Volume.State.Expunged) { volService.destroyVolume(volume.getId()); // Decrement the resource count for volumes and primary storage belonging user VM's only _resourceLimitMgr.decrementResourceCount(volume.getAccountId(), ResourceType.volume, volume.isDisplayVolume()); _resourceLimitMgr.decrementResourceCount(volume.getAccountId(), ResourceType.primary_storage, volume.isDisplayVolume(), volume.getSize()); } }
private void cleanupVolumeDuringAttachFailure(Long volumeId, Long vmId) { VolumeVO volume = _volsDao.findById(volumeId); if (volume == null) { return; } if (volume.getState().equals(Volume.State.Creating)) { s_logger.debug("Remove volume: " + volume.getId() + ", as it's leftover from last mgt server stop"); _volsDao.remove(volume.getId()); } if (volume.getState().equals(Volume.State.Attaching)) { s_logger.warn("Vol: " + volume.getName() + " failed to attach to VM: " + _userVmDao.findById(vmId).getHostName() + " on last mgt server stop, changing state back to Ready"); volume.setState(Volume.State.Ready); _volsDao.update(volumeId, volume); } }
@Override public long getUsedIops(StoragePool storagePool) { long usedIops = 0; List<VolumeVO> volumes = volumeDao.findByPoolId(storagePool.getId(), null); if (volumes != null) { for (VolumeVO volume : volumes) { if (!Volume.State.Creating.equals(volume.getState())) { usedIops += volume.getMinIops() != null ? volume.getMinIops() : 0; } } } return usedIops; }
private void cleanupVolumeDuringMigrationFailure(Long volumeId, Long destPoolId) { StoragePool destPool = (StoragePool)dataStoreMgr.getDataStore(destPoolId, DataStoreRole.Primary); if (destPool == null) { return; } VolumeVO volume = _volsDao.findById(volumeId); if (volume.getState() == Volume.State.Migrating) { VolumeVO duplicateVol = _volsDao.findByPoolIdName(destPoolId, volume.getName()); if (duplicateVol != null) { s_logger.debug("Remove volume " + duplicateVol.getId() + " on storage pool " + destPoolId); _volsDao.remove(duplicateVol.getId()); } s_logger.debug("change volume state to ready from migrating in case migration failure for vol: " + volumeId); volume.setState(Volume.State.Ready); _volsDao.update(volumeId, volume); } }
private VMSnapshot orchestrateCreateVMSnapshot(Long vmId, Long vmSnapshotId, Boolean quiescevm) { UserVmVO userVm = _userVMDao.findById(vmId); if (userVm == null) { throw new InvalidParameterValueException("Create vm to snapshot failed due to vm: " + vmId + " is not found"); } List<VolumeVO> volumeVos = _volumeDao.findByInstanceAndType(vmId, Type.ROOT); if(volumeVos == null ||volumeVos.isEmpty()) { throw new CloudRuntimeException("Create vm to snapshot failed due to no root disk found"); } VolumeVO rootVolume = volumeVos.get(0); if(!rootVolume.getState().equals(Volume.State.Ready)) { throw new CloudRuntimeException("Create vm to snapshot failed due to vm: " + vmId + " has root disk in " + rootVolume.getState() + " state"); } VMSnapshotVO vmSnapshot = _vmSnapshotDao.findById(vmSnapshotId); if (vmSnapshot == null) { throw new CloudRuntimeException("VM snapshot id: " + vmSnapshotId + " can not be found"); } VMSnapshotOptions options = new VMSnapshotOptions(quiescevm); vmSnapshot.setOptions(options); try { VMSnapshotStrategy strategy = findVMSnapshotStrategy(vmSnapshot); VMSnapshot snapshot = strategy.takeVMSnapshot(vmSnapshot); return snapshot; } catch (Exception e) { s_logger.debug("Failed to create vm snapshot: " + vmSnapshotId, e); return null; } }
throw new InvalidParameterValueException("Please specify a volume that is not attached to any VM."); if (volume.getState() == Volume.State.UploadOp) { VolumeDataStoreVO volumeStore = _volumeStoreDao.findByVolume(volume.getId()); if (volumeStore.getDownloadState() == VMTemplateStorageResourceAssoc.Status.DOWNLOAD_IN_PROGRESS) { if (volume.getState() == Volume.State.NotUploaded || volume.getState() == Volume.State.UploadInProgress) { throw new InvalidParameterValueException("The volume is either getting uploaded or it may be initiated shortly, please wait for it to be completed");
@DB @Override @ActionEvent(eventType = EventTypes.EVENT_VOLUME_DELETE, eventDescription = "deleting volume") /** * Executes the removal of the volume. If the volume is only allocated we do not try to remove it from primary and secondary storage. * Otherwise, after the removal in the database, we will try to remove the volume from both primary and secondary storage. */ public boolean deleteVolume(long volumeId, Account caller) throws ConcurrentOperationException { VolumeVO volume = retrieveAndValidateVolume(volumeId, caller); try { destroyVolumeIfPossible(volume); // Mark volume as removed if volume has not been created on primary or secondary if (volume.getState() == Volume.State.Allocated) { _volsDao.remove(volumeId); stateTransitTo(volume, Volume.Event.DestroyRequested); return true; } expungeVolumesInPrimaryStorageIfNeeded(volume); expungeVolumesInSecondaryStorageIfNeeded(volume); cleanVolumesCache(volume); return true; } catch (InterruptedException | ExecutionException | NoTransitionException e) { s_logger.warn("Failed to expunge volume: " + volume.getUuid(), e); return false; } }
private void updateVmStateForFailedVmCreation(Long vmId, Long hostId) { UserVmVO vm = _vmDao.findById(vmId); if (vm != null) { if (vm.getState().equals(State.Stopped)) { s_logger.debug("Destroying vm " + vm + " as it failed to create on Host with Id:" + hostId); try { _itMgr.stateTransitTo(vm, VirtualMachine.Event.OperationFailedToError, null); } catch (NoTransitionException e1) { s_logger.warn(e1.getMessage()); } // destroy associated volumes for vm in error state // get all volumes in non destroyed state List<VolumeVO> volumesForThisVm = _volsDao.findUsableVolumesForInstance(vm.getId()); for (VolumeVO volume : volumesForThisVm) { if (volume.getState() != Volume.State.Destroy) { volumeMgr.destroyVolume(volume); } } String msg = "Failed to deploy Vm with Id: " + vmId + ", on Host with Id: " + hostId; _alertMgr.sendAlert(AlertManager.AlertType.ALERT_TYPE_USERVM, vm.getDataCenterId(), vm.getPodIdToDeployIn(), msg, msg); // Get serviceOffering for Virtual Machine ServiceOfferingVO offering = _serviceOfferingDao.findById(vm.getId(), vm.getServiceOfferingId()); // Update Resource Count for the given account resourceCountDecrement(vm.getAccountId(), vm.isDisplayVm(), new Long(offering.getCpu()), new Long(offering.getRamSize())); } } }
if (volume.getState() != Volume.State.Ready) { throw new InvalidParameterValueException("The volume is not in Ready state.");
private String orchestrateExtractVolume(long volumeId, long zoneId) { if (volume == null || volume.getState() != Volume.State.Ready) { throw new InvalidParameterValueException("Volume to be extracted has been removed or not in right state!");
volumeSearch.and("volumeType", volumeSearch.entity().getVolumeType(), SearchCriteria.Op.EQ); volumeSearch.and("poolId", volumeSearch.entity().getPoolId(), SearchCriteria.Op.EQ); volumeSearch.and("state", volumeSearch.entity().getState(), SearchCriteria.Op.EQ); StoragePoolSearch.join("vmVolume", volumeSearch, volumeSearch.entity().getInstanceId(), StoragePoolSearch.entity().getId(), JoinBuilder.JoinType.INNER); StoragePoolSearch.done();
@Override public boolean isLocalStorageActiveOnHost(Long hostId) { List<StoragePoolHostVO> storagePoolHostRefs = _storagePoolHostDao.listByHostId(hostId); for (StoragePoolHostVO storagePoolHostRef : storagePoolHostRefs) { StoragePoolVO PrimaryDataStoreVO = _storagePoolDao.findById(storagePoolHostRef.getPoolId()); if (PrimaryDataStoreVO.getPoolType() == StoragePoolType.LVM || PrimaryDataStoreVO.getPoolType() == StoragePoolType.EXT) { SearchBuilder<VolumeVO> volumeSB = _volsDao.createSearchBuilder(); volumeSB.and("poolId", volumeSB.entity().getPoolId(), SearchCriteria.Op.EQ); volumeSB.and("removed", volumeSB.entity().getRemoved(), SearchCriteria.Op.NULL); volumeSB.and("state", volumeSB.entity().getState(), SearchCriteria.Op.NIN); SearchBuilder<VMInstanceVO> activeVmSB = _vmInstanceDao.createSearchBuilder(); activeVmSB.and("state", activeVmSB.entity().getState(), SearchCriteria.Op.IN); volumeSB.join("activeVmSB", activeVmSB, volumeSB.entity().getInstanceId(), activeVmSB.entity().getId(), JoinBuilder.JoinType.INNER); SearchCriteria<VolumeVO> volumeSC = volumeSB.create(); volumeSC.setParameters("poolId", PrimaryDataStoreVO.getId()); volumeSC.setParameters("state", Volume.State.Expunging, Volume.State.Destroy); volumeSC.setJoinParameters("activeVmSB", "state", State.Starting, State.Running, State.Stopping, State.Migrating); List<VolumeVO> volumes = _volsDao.search(volumeSC, null); if (volumes.size() > 0) { return true; } } } return false; }
volumeOnlySearch.and("states", volumeOnlySearch.entity().getState(), Op.IN); uploadVolumeStateSearch = createSearchBuilder(); uploadVolumeStateSearch.join("volumeOnlySearch", volumeOnlySearch, volumeOnlySearch.entity().getId(), uploadVolumeStateSearch.entity().getVolumeId(), JoinType.LEFT);