Search in sources :

Example 6 with StorageAccessException

use of com.cloud.exception.StorageAccessException in project cloudstack by apache.

the class VolumeServiceImpl method copyTemplateToManagedTemplateVolume.

/**
 * This function copies a template from secondary storage to a template volume
 * created on managed storage. This template volume will be used as a cache.
 * Instead of copying the template to a ROOT volume every time, a clone is performed instead.
 *
 * @param srcTemplateInfo Source from which to copy the template
 * @param templateOnPrimary Dest to copy to
 * @param templatePoolRef Template reference on primary storage (entry in the template_spool_ref)
 * @param destPrimaryDataStore The managed primary storage
 * @param destHost The host that we will use for the copy
 */
private void copyTemplateToManagedTemplateVolume(TemplateInfo srcTemplateInfo, TemplateInfo templateOnPrimary, VMTemplateStoragePoolVO templatePoolRef, PrimaryDataStore destPrimaryDataStore, Host destHost) throws StorageAccessException {
    AsyncCallFuture<VolumeApiResult> copyTemplateFuture = new AsyncCallFuture<>();
    int storagePoolMaxWaitSeconds = NumbersUtil.parseInt(configDao.getValue(Config.StoragePoolMaxWaitSeconds.key()), 3600);
    long templatePoolRefId = templatePoolRef.getId();
    try {
        templatePoolRef = _tmpltPoolDao.acquireInLockTable(templatePoolRefId, storagePoolMaxWaitSeconds);
        if (templatePoolRef == null) {
            throw new CloudRuntimeException("Unable to acquire lock on VMTemplateStoragePool: " + templatePoolRefId);
        }
        if (templatePoolRef.getDownloadState() == Status.DOWNLOADED) {
            // There can be cases where we acquired the lock, but the template
            // was already copied by a previous thread. Just return in that case.
            s_logger.debug("Template already downloaded, nothing to do");
            return;
        }
        // copy the template from sec storage to the created volume
        CreateBaseImageContext<CreateCmdResult> copyContext = new CreateBaseImageContext<>(null, null, destPrimaryDataStore, srcTemplateInfo, copyTemplateFuture, templateOnPrimary, templatePoolRefId);
        AsyncCallbackDispatcher<VolumeServiceImpl, CopyCommandResult> copyCaller = AsyncCallbackDispatcher.create(this);
        copyCaller.setCallback(copyCaller.getTarget().copyManagedTemplateCallback(null, null)).setContext(copyContext);
        // Populate details which will be later read by the storage subsystem.
        Map<String, String> details = new HashMap<>();
        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, templateOnPrimary.getInstallPath());
        details.put(PrimaryDataStore.MANAGED_STORE_TARGET_ROOT_VOLUME, srcTemplateInfo.getUniqueName());
        details.put(PrimaryDataStore.REMOVE_AFTER_COPY, Boolean.TRUE.toString());
        details.put(PrimaryDataStore.VOLUME_SIZE, String.valueOf(templateOnPrimary.getSize()));
        details.put(StorageManager.STORAGE_POOL_DISK_WAIT.toString(), String.valueOf(StorageManager.STORAGE_POOL_DISK_WAIT.valueIn(destPrimaryDataStore.getId())));
        ChapInfo chapInfo = getChapInfo(templateOnPrimary, destPrimaryDataStore);
        if (chapInfo != null) {
            details.put(PrimaryDataStore.CHAP_INITIATOR_USERNAME, chapInfo.getInitiatorUsername());
            details.put(PrimaryDataStore.CHAP_INITIATOR_SECRET, chapInfo.getInitiatorSecret());
            details.put(PrimaryDataStore.CHAP_TARGET_USERNAME, chapInfo.getTargetUsername());
            details.put(PrimaryDataStore.CHAP_TARGET_SECRET, chapInfo.getTargetSecret());
        }
        destPrimaryDataStore.setDetails(details);
        try {
            grantAccess(templateOnPrimary, destHost, destPrimaryDataStore);
        } catch (Exception e) {
            throw new StorageAccessException("Unable to grant access to template: " + templateOnPrimary.getId() + " on host: " + destHost.getId());
        }
        templateOnPrimary.processEvent(Event.CopyingRequested);
        VolumeApiResult result;
        try {
            motionSrv.copyAsync(srcTemplateInfo, templateOnPrimary, destHost, copyCaller);
            result = copyTemplateFuture.get();
        } finally {
            revokeAccess(templateOnPrimary, destHost, destPrimaryDataStore);
            if (HypervisorType.VMware.equals(destHost.getHypervisorType())) {
                details.put(ModifyTargetsCommand.IQN, templateOnPrimary.getInstallPath());
                List<Map<String, String>> targets = new ArrayList<>();
                targets.add(details);
                removeDynamicTargets(destHost.getId(), targets);
            }
        }
        if (result.isFailed()) {
            throw new CloudRuntimeException("Failed to copy template " + templateOnPrimary.getId() + " to primary storage " + destPrimaryDataStore.getId() + ": " + result.getResult());
        // XXX: I find it is useful to destroy the volume on primary storage instead of another thread trying the copy again because I've seen
        // something weird happens to the volume (XenServer creates an SR, but the VDI copy can fail).
        // For now, I just retry the copy.
        }
    } catch (StorageAccessException e) {
        throw e;
    } catch (Throwable e) {
        s_logger.debug("Failed to create a template on primary storage", e);
        templateOnPrimary.processEvent(Event.OperationFailed);
        throw new CloudRuntimeException(e.getMessage());
    } finally {
        _tmpltPoolDao.releaseFromLockTable(templatePoolRefId);
    }
}
Also used : HashMap(java.util.HashMap) ChapInfo(org.apache.cloudstack.engine.subsystem.api.storage.ChapInfo) ArrayList(java.util.ArrayList) StorageAccessException(com.cloud.exception.StorageAccessException) CreateCmdResult(org.apache.cloudstack.engine.subsystem.api.storage.CreateCmdResult) EndPoint(org.apache.cloudstack.engine.subsystem.api.storage.EndPoint) RemoteHostEndPoint(org.apache.cloudstack.storage.RemoteHostEndPoint) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) StorageAccessException(com.cloud.exception.StorageAccessException) ExecutionException(java.util.concurrent.ExecutionException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) AsyncCallFuture(org.apache.cloudstack.framework.async.AsyncCallFuture) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) CopyCommandResult(org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

StorageAccessException (com.cloud.exception.StorageAccessException)6 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)6 ExecutionException (java.util.concurrent.ExecutionException)5 Host (com.cloud.host.Host)4 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)3 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)3 StoragePool (com.cloud.storage.StoragePool)3 VolumeVO (com.cloud.storage.VolumeVO)3 NoTransitionException (com.cloud.utils.fsm.NoTransitionException)3 HashMap (java.util.HashMap)3 PrimaryDataStore (org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore)3 InsufficientStorageCapacityException (com.cloud.exception.InsufficientStorageCapacityException)2 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)2 StorageUnavailableException (com.cloud.exception.StorageUnavailableException)2 VMTemplateStoragePoolVO (com.cloud.storage.VMTemplateStoragePoolVO)2 ConfigurationException (javax.naming.ConfigurationException)2 CopyCommandResult (org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult)2 DataStore (org.apache.cloudstack.engine.subsystem.api.storage.DataStore)2 EndPoint (org.apache.cloudstack.engine.subsystem.api.storage.EndPoint)2 VolumeInfo (org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo)2