Search in sources :

Example 6 with PrimaryDataStore

use of org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore in project cloudstack by apache.

the class VolumeServiceImpl method copyManagedVolume.

private AsyncCallFuture<VolumeApiResult> copyManagedVolume(VolumeInfo srcVolume, DataStore destStore) {
    AsyncCallFuture<VolumeApiResult> future = new AsyncCallFuture<VolumeApiResult>();
    VolumeApiResult res = new VolumeApiResult(srcVolume);
    try {
        if (!snapshotMgr.canOperateOnVolume(srcVolume)) {
            s_logger.debug("There are snapshots creating for this volume, can not move this volume");
            res.setResult("There are snapshots creating for this volume, can not move this volume");
            future.complete(res);
            return future;
        }
        if (snapshotMgr.backedUpSnapshotsExistsForVolume(srcVolume)) {
            s_logger.debug("There are backed up snapshots for this volume, can not move.");
            res.setResult("[UNSUPPORTED] There are backed up snapshots for this volume, can not move. Please try again after removing them.");
            future.complete(res);
            return future;
        }
        List<Long> poolIds = new ArrayList<Long>();
        poolIds.add(srcVolume.getPoolId());
        poolIds.add(destStore.getId());
        Host hostWithPoolsAccess = _storageMgr.findUpAndEnabledHostWithAccessToStoragePools(poolIds);
        if (hostWithPoolsAccess == null) {
            s_logger.debug("No host(s) available with pool access, can not move this volume");
            res.setResult("No host(s) available with pool access, can not move this volume");
            future.complete(res);
            return future;
        }
        VolumeVO destVol = duplicateVolumeOnAnotherStorage(srcVolume, (StoragePool) destStore);
        VolumeInfo destVolume = volFactory.getVolume(destVol.getId(), destStore);
        // Create a volume on managed storage.
        AsyncCallFuture<VolumeApiResult> createVolumeFuture = createVolumeAsync(destVolume, destStore);
        VolumeApiResult createVolumeResult = createVolumeFuture.get();
        if (createVolumeResult.isFailed()) {
            s_logger.debug("Failed to create dest volume " + destVolume.getId() + ", volume can be removed");
            destroyVolume(destVolume.getId());
            destVolume.processEvent(Event.ExpungeRequested);
            destVolume.processEvent(Event.OperationSuccessed);
            volDao.remove(destVolume.getId());
            throw new CloudRuntimeException("Creation of a dest volume failed: " + createVolumeResult.getResult());
        }
        // Refresh the volume info from the DB.
        destVolume = volFactory.getVolume(destVolume.getId(), destStore);
        PrimaryDataStore srcPrimaryDataStore = (PrimaryDataStore) srcVolume.getDataStore();
        if (srcPrimaryDataStore.isManaged()) {
            Map<String, String> srcPrimaryDataStoreDetails = new HashMap<String, String>();
            srcPrimaryDataStoreDetails.put(PrimaryDataStore.MANAGED, Boolean.TRUE.toString());
            srcPrimaryDataStoreDetails.put(PrimaryDataStore.STORAGE_HOST, srcPrimaryDataStore.getHostAddress());
            srcPrimaryDataStoreDetails.put(PrimaryDataStore.STORAGE_PORT, String.valueOf(srcPrimaryDataStore.getPort()));
            srcPrimaryDataStoreDetails.put(PrimaryDataStore.MANAGED_STORE_TARGET, srcVolume.get_iScsiName());
            srcPrimaryDataStoreDetails.put(PrimaryDataStore.MANAGED_STORE_TARGET_ROOT_VOLUME, srcVolume.getName());
            srcPrimaryDataStoreDetails.put(PrimaryDataStore.VOLUME_SIZE, String.valueOf(srcVolume.getSize()));
            srcPrimaryDataStoreDetails.put(StorageManager.STORAGE_POOL_DISK_WAIT.toString(), String.valueOf(StorageManager.STORAGE_POOL_DISK_WAIT.valueIn(srcPrimaryDataStore.getId())));
            srcPrimaryDataStore.setDetails(srcPrimaryDataStoreDetails);
            grantAccess(srcVolume, hostWithPoolsAccess, srcVolume.getDataStore());
        }
        PrimaryDataStore destPrimaryDataStore = (PrimaryDataStore) destStore;
        Map<String, String> destPrimaryDataStoreDetails = new HashMap<String, String>();
        destPrimaryDataStoreDetails.put(PrimaryDataStore.MANAGED, Boolean.TRUE.toString());
        destPrimaryDataStoreDetails.put(PrimaryDataStore.STORAGE_HOST, destPrimaryDataStore.getHostAddress());
        destPrimaryDataStoreDetails.put(PrimaryDataStore.STORAGE_PORT, String.valueOf(destPrimaryDataStore.getPort()));
        destPrimaryDataStoreDetails.put(PrimaryDataStore.MANAGED_STORE_TARGET, destVolume.get_iScsiName());
        destPrimaryDataStoreDetails.put(PrimaryDataStore.MANAGED_STORE_TARGET_ROOT_VOLUME, destVolume.getName());
        destPrimaryDataStoreDetails.put(PrimaryDataStore.VOLUME_SIZE, String.valueOf(destVolume.getSize()));
        destPrimaryDataStoreDetails.put(StorageManager.STORAGE_POOL_DISK_WAIT.toString(), String.valueOf(StorageManager.STORAGE_POOL_DISK_WAIT.valueIn(destPrimaryDataStore.getId())));
        destPrimaryDataStore.setDetails(destPrimaryDataStoreDetails);
        grantAccess(destVolume, hostWithPoolsAccess, destStore);
        destVolume.processEvent(Event.CreateRequested);
        srcVolume.processEvent(Event.MigrationRequested);
        CopyManagedVolumeContext<VolumeApiResult> context = new CopyManagedVolumeContext<VolumeApiResult>(null, future, srcVolume, destVolume, hostWithPoolsAccess);
        AsyncCallbackDispatcher<VolumeServiceImpl, CopyCommandResult> caller = AsyncCallbackDispatcher.create(this);
        caller.setCallback(caller.getTarget().copyManagedVolumeCallBack(null, null)).setContext(context);
        motionSrv.copyAsync(srcVolume, destVolume, hostWithPoolsAccess, caller);
    } catch (Exception e) {
        s_logger.error("Copy to managed volume failed due to: " + e);
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Copy to managed volume failed.", e);
        }
        res.setResult(e.toString());
        future.complete(res);
    }
    return future;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Host(com.cloud.host.Host) VolumeInfo(org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo) 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) VolumeVO(com.cloud.storage.VolumeVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) CopyCommandResult(org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult) PrimaryDataStore(org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore)

Example 7 with PrimaryDataStore

use of org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore in project cloudstack by apache.

the class VolumeServiceImpl method volumeExistsOnPrimary.

private boolean volumeExistsOnPrimary(VolumeVO vol) {
    Long poolId = vol.getPoolId();
    if (poolId == null) {
        return false;
    }
    PrimaryDataStore primaryStore = dataStoreMgr.getPrimaryDataStore(poolId);
    if (primaryStore == null) {
        return false;
    }
    if (primaryStore.isManaged()) {
        return true;
    }
    String volumePath = vol.getPath();
    if (volumePath == null || volumePath.trim().isEmpty()) {
        return false;
    }
    return true;
}
Also used : PrimaryDataStore(org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore)

Example 8 with PrimaryDataStore

use of org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore in project cloudstack by apache.

the class VolumeServiceImpl method createVolumeFromTemplateAsync.

@DB
@Override
public AsyncCallFuture<VolumeApiResult> createVolumeFromTemplateAsync(VolumeInfo volume, long dataStoreId, TemplateInfo template) {
    PrimaryDataStore pd = dataStoreMgr.getPrimaryDataStore(dataStoreId);
    TemplateInfo templateOnPrimaryStore = pd.getTemplate(template.getId(), volume.getDeployAsIsConfiguration());
    AsyncCallFuture<VolumeApiResult> future = new AsyncCallFuture<>();
    if (templateOnPrimaryStore == null) {
        createBaseImageAsync(volume, pd, template, future);
        return future;
    }
    createVolumeFromBaseImageAsync(volume, templateOnPrimaryStore, pd, future);
    return future;
}
Also used : AsyncCallFuture(org.apache.cloudstack.framework.async.AsyncCallFuture) TemplateInfo(org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo) PrimaryDataStore(org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore) DB(com.cloud.utils.db.DB)

Example 9 with PrimaryDataStore

use of org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore in project cloudstack by apache.

the class TemplateManagerImpl method evictTemplateFromStoragePool.

@Override
@DB
public void evictTemplateFromStoragePool(VMTemplateStoragePoolVO templatePoolVO) {
    // Need to hold the lock; otherwise, another thread may create a volume from the template at the same time.
    // Assumption here is that we will hold the same lock during create volume from template.
    VMTemplateStoragePoolVO templatePoolRef = _tmpltPoolDao.acquireInLockTable(templatePoolVO.getId());
    if (templatePoolRef == null) {
        s_logger.debug("Can't aquire the lock for template pool ref: " + templatePoolVO.getId());
        return;
    }
    PrimaryDataStore pool = (PrimaryDataStore) _dataStoreMgr.getPrimaryDataStore(templatePoolVO.getPoolId());
    TemplateInfo template = _tmplFactory.getTemplateOnPrimaryStorage(templatePoolRef.getTemplateId(), pool, templatePoolRef.getDeploymentOption());
    try {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Evicting " + templatePoolVO);
        }
        if (pool.isManaged()) {
            // For managed store, just delete the template volume.
            AsyncCallFuture<TemplateApiResult> future = _tmpltSvr.deleteTemplateOnPrimary(template, pool);
            TemplateApiResult result = future.get();
            if (result.isFailed()) {
                s_logger.debug("Failed to delete template " + template.getId() + " from storage pool " + pool.getId());
            } else {
                // Remove the templatePoolVO.
                if (_tmpltPoolDao.remove(templatePoolVO.getId())) {
                    s_logger.debug("Successfully evicted template " + template.getName() + " from storage pool " + pool.getName());
                }
            }
        } else {
            DestroyCommand cmd = new DestroyCommand(pool, templatePoolVO);
            Answer answer = _storageMgr.sendToPool(pool, cmd);
            if (answer != null && answer.getResult()) {
                // Remove the templatePoolVO.
                if (_tmpltPoolDao.remove(templatePoolVO.getId())) {
                    s_logger.debug("Successfully evicted template " + template.getName() + " from storage pool " + pool.getName());
                }
            } else {
                s_logger.info("Will retry evict template " + template.getName() + " from storage pool " + pool.getName());
            }
        }
    } catch (StorageUnavailableException | InterruptedException | ExecutionException e) {
        s_logger.info("Storage is unavailable currently. Will retry evicte template " + template.getName() + " from storage pool " + pool.getName());
    } finally {
        _tmpltPoolDao.releaseFromLockTable(templatePoolRef.getId());
    }
}
Also used : VMTemplateStoragePoolVO(com.cloud.storage.VMTemplateStoragePoolVO) Answer(com.cloud.agent.api.Answer) TemplateInfo(org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo) StorageUnavailableException(com.cloud.exception.StorageUnavailableException) DestroyCommand(com.cloud.agent.api.storage.DestroyCommand) ExecutionException(java.util.concurrent.ExecutionException) TemplateApiResult(org.apache.cloudstack.engine.subsystem.api.storage.TemplateService.TemplateApiResult) PrimaryDataStore(org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore) DB(com.cloud.utils.db.DB)

Example 10 with PrimaryDataStore

use of org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore in project cloudstack by apache.

the class TemplateManagerImplTest method testPrepareTemplateNotDownloaded.

@Test
public void testPrepareTemplateNotDownloaded() {
    VMTemplateVO mockTemplate = mock(VMTemplateVO.class);
    when(mockTemplate.getId()).thenReturn(202l);
    StoragePoolVO mockPool = mock(StoragePoolVO.class);
    when(mockPool.getId()).thenReturn(2l);
    PrimaryDataStore mockPrimaryDataStore = mock(PrimaryDataStore.class);
    when(mockPrimaryDataStore.getId()).thenReturn(2l);
    when(mockPrimaryDataStore.getDataCenterId()).thenReturn(1l);
    when(dataStoreManager.getPrimaryDataStore(anyLong())).thenReturn(mockPrimaryDataStore);
    when(vmTemplateDao.findById(anyLong(), anyBoolean())).thenReturn(mockTemplate);
    when(vmTemplatePoolDao.findByPoolTemplate(anyLong(), anyLong(), nullable(String.class))).thenReturn(null);
    when(templateDataStoreDao.findByTemplateZoneDownloadStatus(202l, 1l, VMTemplateStorageResourceAssoc.Status.DOWNLOADED)).thenReturn(null);
    VMTemplateStoragePoolVO returnObject = templateManager.prepareTemplateForCreate(mockTemplate, (StoragePool) mockPrimaryDataStore);
    assertTrue("Test template is not ready", returnObject == null);
}
Also used : VMTemplateStoragePoolVO(com.cloud.storage.VMTemplateStoragePoolVO) VMTemplateVO(com.cloud.storage.VMTemplateVO) VMTemplateStoragePoolVO(com.cloud.storage.VMTemplateStoragePoolVO) StoragePoolVO(org.apache.cloudstack.storage.datastore.db.StoragePoolVO) PrimaryDataStore(org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore) Test(org.junit.Test)

Aggregations

PrimaryDataStore (org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore)25 VMTemplateStoragePoolVO (com.cloud.storage.VMTemplateStoragePoolVO)14 StoragePoolVO (org.apache.cloudstack.storage.datastore.db.StoragePoolVO)13 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)11 DataStore (org.apache.cloudstack.engine.subsystem.api.storage.DataStore)9 ExecutionException (java.util.concurrent.ExecutionException)8 Test (org.junit.Test)8 VMTemplateVO (com.cloud.storage.VMTemplateVO)7 VolumeVO (com.cloud.storage.VolumeVO)7 VolumeInfo (org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo)7 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)6 StorageAccessException (com.cloud.exception.StorageAccessException)6 Host (com.cloud.host.Host)6 TemplateInfo (org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo)6 Answer (com.cloud.agent.api.Answer)5 AsyncCallFuture (org.apache.cloudstack.framework.async.AsyncCallFuture)5 ArrayList (java.util.ArrayList)4 StorageUnavailableException (com.cloud.exception.StorageUnavailableException)3 CopyCommandResult (org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult)3 EndPoint (org.apache.cloudstack.engine.subsystem.api.storage.EndPoint)3