use of org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult in project cloudstack by apache.
the class VolumeServiceImpl method copyVolume.
@Override
public AsyncCallFuture<VolumeApiResult> copyVolume(VolumeInfo srcVolume, DataStore destStore) {
DataStore srcStore = srcVolume.getDataStore();
if (s_logger.isDebugEnabled()) {
String srcRole = (srcStore != null && srcStore.getRole() != null ? srcVolume.getDataStore().getRole().toString() : "<unknown role>");
String msg = String.format("copying %s(id=%d, role=%s) to %s (id=%d, role=%s)", srcVolume.getName(), srcVolume.getId(), srcRole, destStore.getName(), destStore.getId(), destStore.getRole());
s_logger.debug(msg);
}
if (srcVolume.getState() == Volume.State.Uploaded) {
return copyVolumeFromImageToPrimary(srcVolume, destStore);
}
if (destStore.getRole() == DataStoreRole.Image) {
return copyVolumeFromPrimaryToImage(srcVolume, destStore);
}
if (srcStore.getRole() == DataStoreRole.Primary && destStore.getRole() == DataStoreRole.Primary && ((PrimaryDataStore) destStore).isManaged() && requiresNewManagedVolumeInDestStore((PrimaryDataStore) srcStore, (PrimaryDataStore) destStore)) {
return copyManagedVolume(srcVolume, destStore);
}
// OfflineVmwareMigration: aren't we missing secondary to secondary in this logic?
AsyncCallFuture<VolumeApiResult> future = new AsyncCallFuture<VolumeApiResult>();
VolumeApiResult res = new VolumeApiResult(srcVolume);
try {
if (!snapshotMgr.canOperateOnVolume(srcVolume)) {
s_logger.debug("There are snapshots creating on this volume, can not move this volume");
res.setResult("There are snapshots creating on this volume, can not move this volume");
future.complete(res);
return future;
}
VolumeVO destVol = duplicateVolumeOnAnotherStorage(srcVolume, (StoragePool) destStore);
VolumeInfo destVolume = volFactory.getVolume(destVol.getId(), destStore);
destVolume.processEvent(Event.MigrationCopyRequested);
srcVolume.processEvent(Event.MigrationRequested);
CopyVolumeContext<VolumeApiResult> context = new CopyVolumeContext<VolumeApiResult>(null, future, srcVolume, destVolume, destStore);
AsyncCallbackDispatcher<VolumeServiceImpl, CopyCommandResult> caller = AsyncCallbackDispatcher.create(this);
caller.setCallback(caller.getTarget().copyVolumeCallBack(null, null)).setContext(context);
motionSrv.copyAsync(srcVolume, destVolume, caller);
} catch (Exception e) {
s_logger.error("Failed to copy volume:" + e);
if (s_logger.isDebugEnabled()) {
s_logger.debug("Failed to copy volume.", e);
}
res.setResult(e.toString());
future.complete(res);
}
return future;
}
use of org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult in project cloudstack by apache.
the class VolumeServiceImpl method copyVolumeFromImageToPrimary.
protected AsyncCallFuture<VolumeApiResult> copyVolumeFromImageToPrimary(VolumeInfo srcVolume, DataStore destStore) {
AsyncCallFuture<VolumeApiResult> future = new AsyncCallFuture<VolumeApiResult>();
VolumeApiResult res = new VolumeApiResult(srcVolume);
VolumeInfo destVolume = null;
try {
destVolume = (VolumeInfo) destStore.create(srcVolume);
destVolume.processEvent(Event.CopyingRequested);
srcVolume.processEvent(Event.CopyingRequested);
CopyVolumeContext<VolumeApiResult> context = new CopyVolumeContext<VolumeApiResult>(null, future, srcVolume, destVolume, destStore);
AsyncCallbackDispatcher<VolumeServiceImpl, CopyCommandResult> caller = AsyncCallbackDispatcher.create(this);
caller.setCallback(caller.getTarget().copyVolumeFromImageToPrimaryCallback(null, null)).setContext(context);
motionSrv.copyAsync(srcVolume, destVolume, caller);
return future;
} catch (Exception e) {
s_logger.error("failed to copy volume from image store", e);
if (destVolume != null) {
destVolume.processEvent(Event.OperationFailed);
}
srcVolume.processEvent(Event.OperationFailed);
res.setResult(e.toString());
future.complete(res);
return future;
}
}
use of org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult in project cloudstack by apache.
the class VolumeServiceImpl method migrateVolumeCallBack.
protected Void migrateVolumeCallBack(AsyncCallbackDispatcher<VolumeServiceImpl, CopyCommandResult> callback, MigrateVolumeContext<VolumeApiResult> context) {
VolumeInfo srcVolume = context.srcVolume;
CopyCommandResult result = callback.getResult();
AsyncCallFuture<VolumeApiResult> future = context.future;
VolumeApiResult res = new VolumeApiResult(srcVolume);
try {
if (result.isFailed()) {
res.setResult(result.getResult());
srcVolume.processEvent(Event.OperationFailed);
future.complete(res);
} else {
srcVolume.processEvent(Event.OperationSuccessed);
if (srcVolume.getStoragePoolType() == StoragePoolType.PowerFlex) {
future.complete(res);
return null;
}
snapshotMgr.cleanupSnapshotsByVolume(srcVolume.getId());
future.complete(res);
}
} catch (Exception e) {
s_logger.error("Failed to process migrate volume callback", e);
res.setResult(e.toString());
future.complete(res);
}
return null;
}
use of org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult in project cloudstack by apache.
the class VolumeServiceImpl method createManagedVolumeCopyManagedTemplateAsync.
private void createManagedVolumeCopyManagedTemplateAsync(VolumeInfo volumeInfo, PrimaryDataStore destPrimaryDataStore, TemplateInfo srcTemplateOnPrimary, Host destHost, AsyncCallFuture<VolumeApiResult> future) throws StorageAccessException {
VMTemplateStoragePoolVO templatePoolRef = _tmpltPoolDao.findByPoolTemplate(destPrimaryDataStore.getId(), srcTemplateOnPrimary.getId(), null);
if (templatePoolRef == null) {
throw new CloudRuntimeException("Failed to find template " + srcTemplateOnPrimary.getUniqueName() + " in storage pool " + srcTemplateOnPrimary.getId());
}
if (templatePoolRef.getDownloadState() == Status.NOT_DOWNLOADED) {
throw new CloudRuntimeException("Template " + srcTemplateOnPrimary.getUniqueName() + " has not been downloaded to primary storage.");
}
String volumeDetailKey = "POOL_TEMPLATE_ID_COPY_ON_HOST_" + destHost.getId();
try {
try {
grantAccess(srcTemplateOnPrimary, destHost, destPrimaryDataStore);
} catch (Exception e) {
throw new StorageAccessException("Unable to grant access to src template: " + srcTemplateOnPrimary.getId() + " on host: " + destHost.getId());
}
_volumeDetailsDao.addDetail(volumeInfo.getId(), volumeDetailKey, String.valueOf(templatePoolRef.getId()), false);
// Create a volume on managed storage.
AsyncCallFuture<VolumeApiResult> createVolumeFuture = createVolumeAsync(volumeInfo, destPrimaryDataStore);
VolumeApiResult createVolumeResult = createVolumeFuture.get();
if (createVolumeResult.isFailed()) {
throw new CloudRuntimeException("Creation of a volume failed: " + createVolumeResult.getResult());
}
// Refresh the volume info from the DB.
volumeInfo = volFactory.getVolume(volumeInfo.getId(), destPrimaryDataStore);
Map<String, String> details = new HashMap<String, String>();
details.put(PrimaryDataStore.MANAGED, Boolean.TRUE.toString());
details.put(PrimaryDataStore.STORAGE_HOST, destPrimaryDataStore.getHostAddress());
details.put(PrimaryDataStore.STORAGE_PORT, String.valueOf(destPrimaryDataStore.getPort()));
details.put(PrimaryDataStore.MANAGED_STORE_TARGET, volumeInfo.get_iScsiName());
details.put(PrimaryDataStore.MANAGED_STORE_TARGET_ROOT_VOLUME, volumeInfo.getName());
details.put(PrimaryDataStore.VOLUME_SIZE, String.valueOf(volumeInfo.getSize()));
details.put(StorageManager.STORAGE_POOL_DISK_WAIT.toString(), String.valueOf(StorageManager.STORAGE_POOL_DISK_WAIT.valueIn(destPrimaryDataStore.getId())));
destPrimaryDataStore.setDetails(details);
grantAccess(volumeInfo, destHost, destPrimaryDataStore);
volumeInfo.processEvent(Event.CreateRequested);
CreateVolumeFromBaseImageContext<VolumeApiResult> context = new CreateVolumeFromBaseImageContext<>(null, volumeInfo, destPrimaryDataStore, srcTemplateOnPrimary, future, null, null);
AsyncCallbackDispatcher<VolumeServiceImpl, CopyCommandResult> caller = AsyncCallbackDispatcher.create(this);
caller.setCallback(caller.getTarget().createVolumeFromBaseManagedImageCallBack(null, null));
caller.setContext(context);
try {
motionSrv.copyAsync(srcTemplateOnPrimary, volumeInfo, destHost, caller);
} finally {
revokeAccess(volumeInfo, destHost, destPrimaryDataStore);
}
} catch (StorageAccessException e) {
throw e;
} catch (Throwable e) {
s_logger.debug("Failed to copy managed template on primary storage", e);
String errMsg = "Failed due to " + e.toString();
try {
destroyAndReallocateManagedVolume(volumeInfo);
} catch (CloudRuntimeException ex) {
s_logger.warn("Failed to destroy managed volume: " + volumeInfo.getId());
errMsg += " : " + ex.getMessage();
}
VolumeApiResult result = new VolumeApiResult(volumeInfo);
result.setResult(errMsg);
future.complete(result);
} finally {
_volumeDetailsDao.removeDetail(volumeInfo.getId(), volumeDetailKey);
List<VolumeDetailVO> volumeDetails = _volumeDetailsDao.findDetails(volumeDetailKey, String.valueOf(templatePoolRef.getId()), false);
if (volumeDetails == null || volumeDetails.isEmpty()) {
revokeAccess(srcTemplateOnPrimary, destHost, destPrimaryDataStore);
}
}
}
use of org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult in project cloudstack by apache.
the class VolumeServiceImpl method copyVolumeFromImageToPrimaryCallback.
protected Void copyVolumeFromImageToPrimaryCallback(AsyncCallbackDispatcher<VolumeServiceImpl, CopyCommandResult> callback, CopyVolumeContext<VolumeApiResult> context) {
VolumeInfo srcVolume = context.srcVolume;
VolumeInfo destVolume = context.destVolume;
CopyCommandResult result = callback.getResult();
AsyncCallFuture<VolumeApiResult> future = context.future;
VolumeApiResult res = new VolumeApiResult(destVolume);
try {
if (result.isFailed()) {
destVolume.processEvent(Event.OperationFailed);
srcVolume.processEvent(Event.OperationFailed);
res.setResult(result.getResult());
future.complete(res);
return null;
}
srcVolume.processEvent(Event.OperationSuccessed);
destVolume.processEvent(Event.OperationSuccessed, result.getAnswer());
srcVolume.getDataStore().delete(srcVolume);
future.complete(res);
} catch (Exception e) {
res.setResult(e.toString());
future.complete(res);
}
return null;
}
Aggregations