Search in sources :

Example 11 with VMTemplateStoragePoolVO

use of com.cloud.storage.VMTemplateStoragePoolVO in project cloudstack by apache.

the class VMTemplatePoolDaoImpl method listByHostTemplate.

public List<VMTemplateStoragePoolVO> listByHostTemplate(long hostId, long templateId) {
    TransactionLegacy txn = TransactionLegacy.currentTxn();
    List<VMTemplateStoragePoolVO> result = new ArrayList<VMTemplateStoragePoolVO>();
    String sql = HOST_TEMPLATE_SEARCH;
    try (PreparedStatement pstmt = txn.prepareStatement(sql)) {
        pstmt.setLong(1, hostId);
        pstmt.setLong(2, templateId);
        try (ResultSet rs = pstmt.executeQuery()) {
            while (rs.next()) {
                // result.add(toEntityBean(rs, false)); TODO: this is buggy in
                // GenericDaoBase for hand constructed queries
                // ID column
                long id = rs.getLong(1);
                result.add(findById(id));
            }
        } catch (Exception e) {
            s_logger.warn("Exception: ", e);
        }
    } catch (Exception e) {
        s_logger.warn("Exception: ", e);
    }
    return result;
}
Also used : VMTemplateStoragePoolVO(com.cloud.storage.VMTemplateStoragePoolVO) TransactionLegacy(com.cloud.utils.db.TransactionLegacy) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 12 with VMTemplateStoragePoolVO

use of com.cloud.storage.VMTemplateStoragePoolVO in project cloudstack by apache.

the class VMTemplatePoolDaoImpl method updateState.

@Override
public boolean updateState(State currentState, Event event, State nextState, DataObjectInStore vo, Object data) {
    VMTemplateStoragePoolVO templatePool = (VMTemplateStoragePoolVO) vo;
    Long oldUpdated = templatePool.getUpdatedCount();
    Date oldUpdatedTime = templatePool.getUpdated();
    SearchCriteria<VMTemplateStoragePoolVO> sc = updateStateSearch.create();
    sc.setParameters("id", templatePool.getId());
    sc.setParameters("state", currentState);
    sc.setParameters("updatedCount", templatePool.getUpdatedCount());
    templatePool.incrUpdatedCount();
    UpdateBuilder builder = getUpdateBuilder(vo);
    builder.set(vo, "state", nextState);
    builder.set(vo, "updated", new Date());
    int rows = update((VMTemplateStoragePoolVO) vo, sc);
    if (rows == 0 && s_logger.isDebugEnabled()) {
        VMTemplateStoragePoolVO dbVol = findByIdIncludingRemoved(templatePool.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(templatePool.getId()).append("; state=").append(nextState).append("; event=").append(event).append("; updatecount=").append(templatePool.getUpdatedCount()).append("; updatedTime=").append(templatePool.getUpdated());
            str.append(": stale Data={id=").append(templatePool.getId()).append("; state=").append(currentState).append("; event=").append(event).append("; updatecount=").append(oldUpdated).append("; updatedTime=").append(oldUpdatedTime);
        } else {
            s_logger.debug("Unable to update objectIndatastore: id=" + templatePool.getId() + ", as there is no such object exists in the database anymore");
        }
    }
    return rows > 0;
}
Also used : VMTemplateStoragePoolVO(com.cloud.storage.VMTemplateStoragePoolVO) UpdateBuilder(com.cloud.utils.db.UpdateBuilder) Date(java.util.Date)

Example 13 with VMTemplateStoragePoolVO

use of com.cloud.storage.VMTemplateStoragePoolVO in project cloudstack by apache.

the class VolumeServiceImpl method createBaseImageAsync.

@DB
protected void createBaseImageAsync(VolumeInfo volume, PrimaryDataStore dataStore, TemplateInfo template, AsyncCallFuture<VolumeApiResult> future) {
    DataObject templateOnPrimaryStoreObj = dataStore.create(template);
    VMTemplateStoragePoolVO templatePoolRef = _tmpltPoolDao.findByPoolTemplate(dataStore.getId(), template.getId());
    if (templatePoolRef == null) {
        throw new CloudRuntimeException("Failed to find template " + template.getUniqueName() + " in storage pool " + dataStore.getId());
    } else {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Found template " + template.getUniqueName() + " in storage pool " + dataStore.getId() + " with VMTemplateStoragePool id: " + templatePoolRef.getId());
        }
    }
    long templatePoolRefId = templatePoolRef.getId();
    CreateBaseImageContext<CreateCmdResult> context = new CreateBaseImageContext<CreateCmdResult>(null, volume, dataStore, template, future, templateOnPrimaryStoreObj, templatePoolRefId);
    AsyncCallbackDispatcher<VolumeServiceImpl, CopyCommandResult> caller = AsyncCallbackDispatcher.create(this);
    caller.setCallback(caller.getTarget().copyBaseImageCallback(null, null)).setContext(context);
    int storagePoolMaxWaitSeconds = NumbersUtil.parseInt(configDao.getValue(Config.StoragePoolMaxWaitSeconds.key()), 3600);
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Acquire lock on VMTemplateStoragePool " + templatePoolRefId + " with timeout " + storagePoolMaxWaitSeconds + " seconds");
    }
    templatePoolRef = _tmpltPoolDao.acquireInLockTable(templatePoolRefId, storagePoolMaxWaitSeconds);
    if (templatePoolRef == null) {
        if (s_logger.isDebugEnabled()) {
            s_logger.info("Unable to acquire lock on VMTemplateStoragePool " + templatePoolRefId);
        }
        templatePoolRef = _tmpltPoolDao.findByPoolTemplate(dataStore.getId(), template.getId());
        if (templatePoolRef != null && templatePoolRef.getState() == ObjectInDataStoreStateMachine.State.Ready) {
            s_logger.info("Unable to acquire lock on VMTemplateStoragePool " + templatePoolRefId + ", But Template " + template.getUniqueName() + " is already copied to primary storage, skip copying");
            createVolumeFromBaseImageAsync(volume, templateOnPrimaryStoreObj, dataStore, future);
            return;
        }
        throw new CloudRuntimeException("Unable to acquire lock on VMTemplateStoragePool: " + templatePoolRefId);
    }
    if (s_logger.isDebugEnabled()) {
        s_logger.info("lock is acquired for VMTemplateStoragePool " + templatePoolRefId);
    }
    try {
        if (templatePoolRef.getState() == ObjectInDataStoreStateMachine.State.Ready) {
            s_logger.info("Template " + template.getUniqueName() + " is already copied to primary storage, skip copying");
            createVolumeFromBaseImageAsync(volume, templateOnPrimaryStoreObj, dataStore, future);
            return;
        }
        templateOnPrimaryStoreObj.processEvent(Event.CreateOnlyRequested);
        motionSrv.copyAsync(template, templateOnPrimaryStoreObj, caller);
    } catch (Throwable e) {
        s_logger.debug("failed to create template on storage", e);
        templateOnPrimaryStoreObj.processEvent(Event.OperationFailed);
        // make sure that template_spool_ref entry is still present so that the second thread can acquire the lock
        dataStore.create(template);
        VolumeApiResult result = new VolumeApiResult(volume);
        result.setResult(e.toString());
        future.complete(result);
    } finally {
        if (s_logger.isDebugEnabled()) {
            s_logger.info("releasing lock for VMTemplateStoragePool " + templatePoolRefId);
        }
        _tmpltPoolDao.releaseFromLockTable(templatePoolRefId);
    }
    return;
}
Also used : CreateCmdResult(org.apache.cloudstack.engine.subsystem.api.storage.CreateCmdResult) EndPoint(org.apache.cloudstack.engine.subsystem.api.storage.EndPoint) RemoteHostEndPoint(org.apache.cloudstack.storage.RemoteHostEndPoint) VMTemplateStoragePoolVO(com.cloud.storage.VMTemplateStoragePoolVO) DataObject(org.apache.cloudstack.engine.subsystem.api.storage.DataObject) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) CopyCommandResult(org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult) DB(com.cloud.utils.db.DB)

Example 14 with VMTemplateStoragePoolVO

use of com.cloud.storage.VMTemplateStoragePoolVO in project cloudstack by apache.

the class VolumeServiceImpl method createManagedVolumeCloneTemplateAsync.

/**
     * Clones the template volume on managed storage to the ROOT volume
     *
     * @param volumeInfo ROOT volume to create
     * @param templateOnPrimary Template from which to clone the ROOT volume
     * @param destPrimaryDataStore Primary storage of the volume
     * @param future For async
     */
private void createManagedVolumeCloneTemplateAsync(VolumeInfo volumeInfo, TemplateInfo templateOnPrimary, PrimaryDataStore destPrimaryDataStore, AsyncCallFuture<VolumeApiResult> future) {
    VMTemplateStoragePoolVO templatePoolRef = _tmpltPoolDao.findByPoolTemplate(destPrimaryDataStore.getId(), templateOnPrimary.getId());
    if (templatePoolRef == null) {
        throw new CloudRuntimeException("Failed to find template " + templateOnPrimary.getUniqueName() + " in storage pool " + destPrimaryDataStore.getId());
    }
    //XXX: not sure if this the right thing to do here. We can always fallback to the "copy from sec storage"
    if (templatePoolRef.getDownloadState() == Status.NOT_DOWNLOADED) {
        throw new CloudRuntimeException("Template " + templateOnPrimary.getUniqueName() + " has not been downloaded to primary storage.");
    }
    try {
        volumeInfo.processEvent(Event.CreateOnlyRequested);
        CreateVolumeFromBaseImageContext<VolumeApiResult> context = new CreateVolumeFromBaseImageContext<>(null, volumeInfo, destPrimaryDataStore, templateOnPrimary, future, null);
        AsyncCallbackDispatcher<VolumeServiceImpl, CopyCommandResult> caller = AsyncCallbackDispatcher.create(this);
        caller.setCallback(caller.getTarget().createVolumeFromBaseImageCallBack(null, null));
        caller.setContext(context);
        motionSrv.copyAsync(templateOnPrimary, volumeInfo, caller);
    } catch (Throwable e) {
        s_logger.debug("Failed to clone template on primary storage", e);
        volumeInfo.processEvent(Event.OperationFailed);
        throw new CloudRuntimeException(e.getMessage());
    }
}
Also used : VMTemplateStoragePoolVO(com.cloud.storage.VMTemplateStoragePoolVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) CopyCommandResult(org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult)

Example 15 with VMTemplateStoragePoolVO

use of com.cloud.storage.VMTemplateStoragePoolVO in project cloudstack by apache.

the class VolumeServiceImpl method createManagedStorageVolumeFromTemplateAsync.

@Override
public AsyncCallFuture<VolumeApiResult> createManagedStorageVolumeFromTemplateAsync(VolumeInfo volumeInfo, long destDataStoreId, TemplateInfo srcTemplateInfo, long destHostId) {
    PrimaryDataStore destPrimaryDataStore = dataStoreMgr.getPrimaryDataStore(destDataStoreId);
    Host destHost = _hostDao.findById(destHostId);
    if (destHost == null) {
        throw new CloudRuntimeException("Destination host should not be null.");
    }
    Boolean storageCanCloneVolume = new Boolean(destPrimaryDataStore.getDriver().getCapabilities().get(DataStoreCapabilities.CAN_CREATE_VOLUME_FROM_VOLUME.toString()));
    boolean computeZoneSupportsResign = computeZoneSupportsResign(destHost.getDataCenterId(), destHost.getHypervisorType());
    AsyncCallFuture<VolumeApiResult> future = new AsyncCallFuture<>();
    if (storageCanCloneVolume && computeZoneSupportsResign) {
        s_logger.debug("Storage " + destDataStoreId + " can support cloning using a cached template and host cluster can perform UUID resigning.");
        TemplateInfo templateOnPrimary = destPrimaryDataStore.getTemplate(srcTemplateInfo.getId());
        if (templateOnPrimary == null) {
            templateOnPrimary = createManagedTemplateVolume(srcTemplateInfo, destPrimaryDataStore);
            if (templateOnPrimary == null) {
                throw new CloudRuntimeException("Failed to create template " + srcTemplateInfo.getUniqueName() + " on primary storage: " + destDataStoreId);
            }
        }
        // Copy the template to the template volume.
        VMTemplateStoragePoolVO templatePoolRef = _tmpltPoolDao.findByPoolTemplate(destPrimaryDataStore.getId(), templateOnPrimary.getId());
        if (templatePoolRef == null) {
            throw new CloudRuntimeException("Failed to find template " + srcTemplateInfo.getUniqueName() + " in storage pool " + destPrimaryDataStore.getId());
        }
        if (templatePoolRef.getDownloadState() == Status.NOT_DOWNLOADED) {
            copyTemplateToManagedTemplateVolume(srcTemplateInfo, templateOnPrimary, templatePoolRef, destPrimaryDataStore, destHost);
        }
        // We have a template on primary storage. Clone it to new volume.
        s_logger.debug("Creating a clone from template on primary storage " + destDataStoreId);
        createManagedVolumeCloneTemplateAsync(volumeInfo, templateOnPrimary, destPrimaryDataStore, future);
    } else {
        s_logger.debug("Primary storage does not support cloning or no support for UUID resigning on the host side; copying the template normally");
        createManagedVolumeCopyTemplateAsync(volumeInfo, destPrimaryDataStore, srcTemplateInfo, destHost, future);
    }
    return future;
}
Also used : VMTemplateStoragePoolVO(com.cloud.storage.VMTemplateStoragePoolVO) AsyncCallFuture(org.apache.cloudstack.framework.async.AsyncCallFuture) TemplateInfo(org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Host(com.cloud.host.Host) PrimaryDataStore(org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore)

Aggregations

VMTemplateStoragePoolVO (com.cloud.storage.VMTemplateStoragePoolVO)30 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)11 VMTemplateVO (com.cloud.storage.VMTemplateVO)9 StoragePoolVO (org.apache.cloudstack.storage.datastore.db.StoragePoolVO)9 PrimaryDataStore (org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore)8 DB (com.cloud.utils.db.DB)5 TemplateInfo (org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo)5 TemplateDataStoreVO (org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO)5 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)4 SolidFireUtil (org.apache.cloudstack.storage.datastore.util.SolidFireUtil)4 Answer (com.cloud.agent.api.Answer)3 SnapshotVO (com.cloud.storage.SnapshotVO)3 SnapshotDetailsVO (com.cloud.storage.dao.SnapshotDetailsVO)3 NamedThreadFactory (com.cloud.utils.concurrency.NamedThreadFactory)3 ExecutorService (java.util.concurrent.ExecutorService)3 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)3 CopyCommandResult (org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult)3 SnapshotDataStoreVO (org.apache.cloudstack.storage.datastore.db.SnapshotDataStoreVO)3 VolumeDataStoreVO (org.apache.cloudstack.storage.datastore.db.VolumeDataStoreVO)3