Search in sources :

Example 11 with TemplateDataStoreVO

use of org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO in project cloudstack by apache.

the class TemplateDataStoreDaoImpl method deletePrimaryRecordsForTemplate.

@Override
public void deletePrimaryRecordsForTemplate(long templateId) {
    SearchCriteria<TemplateDataStoreVO> sc = templateSearch.create();
    sc.setParameters("template_id", templateId);
    TransactionLegacy txn = TransactionLegacy.currentTxn();
    txn.start();
    expunge(sc);
    txn.commit();
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) TemplateDataStoreVO(org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO)

Example 12 with TemplateDataStoreVO

use of org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO in project cloudstack by apache.

the class TemplateDataStoreDaoImpl method updateState.

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

Example 13 with TemplateDataStoreVO

use of org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO in project cloudstack by apache.

the class StorageManagerImpl method cleanupSecondaryStorage.

@Override
@DB
public void cleanupSecondaryStorage(boolean recurring) {
    // so here we don't need to issue DeleteCommand to resource anymore, only need to remove db entry.
    try {
        // Cleanup templates in template_store_ref
        List<DataStore> imageStores = _dataStoreMgr.getImageStoresByScope(new ZoneScope(null));
        for (DataStore store : imageStores) {
            try {
                long storeId = store.getId();
                List<TemplateDataStoreVO> destroyedTemplateStoreVOs = _templateStoreDao.listDestroyed(storeId);
                s_logger.debug("Secondary storage garbage collector found " + destroyedTemplateStoreVOs.size() + " templates to cleanup on template_store_ref for store: " + store.getName());
                for (TemplateDataStoreVO destroyedTemplateStoreVO : destroyedTemplateStoreVOs) {
                    if (s_logger.isDebugEnabled()) {
                        s_logger.debug("Deleting template store DB entry: " + destroyedTemplateStoreVO);
                    }
                    _templateStoreDao.remove(destroyedTemplateStoreVO.getId());
                }
            } catch (Exception e) {
                s_logger.warn("problem cleaning up templates in template_store_ref for store: " + store.getName(), e);
            }
        }
        // CleanUp snapshots on snapshot_store_ref
        for (DataStore store : imageStores) {
            try {
                List<SnapshotDataStoreVO> destroyedSnapshotStoreVOs = _snapshotStoreDao.listDestroyed(store.getId());
                s_logger.debug("Secondary storage garbage collector found " + destroyedSnapshotStoreVOs.size() + " snapshots to cleanup on snapshot_store_ref for store: " + store.getName());
                for (SnapshotDataStoreVO destroyedSnapshotStoreVO : destroyedSnapshotStoreVOs) {
                    // check if this snapshot has child
                    SnapshotInfo snap = snapshotFactory.getSnapshot(destroyedSnapshotStoreVO.getSnapshotId(), store);
                    if (snap.getChild() != null) {
                        s_logger.debug("Skip snapshot on store: " + destroyedSnapshotStoreVO + " , because it has child");
                        continue;
                    }
                    if (s_logger.isDebugEnabled()) {
                        s_logger.debug("Deleting snapshot store DB entry: " + destroyedSnapshotStoreVO);
                    }
                    _snapshotDao.remove(destroyedSnapshotStoreVO.getSnapshotId());
                    SnapshotDataStoreVO snapshotOnPrimary = _snapshotStoreDao.findBySnapshot(destroyedSnapshotStoreVO.getSnapshotId(), DataStoreRole.Primary);
                    if (snapshotOnPrimary != null) {
                        _snapshotStoreDao.remove(snapshotOnPrimary.getId());
                    }
                    _snapshotStoreDao.remove(destroyedSnapshotStoreVO.getId());
                }
            } catch (Exception e2) {
                s_logger.warn("problem cleaning up snapshots in snapshot_store_ref for store: " + store.getName(), e2);
            }
        }
        // CleanUp volumes on volume_store_ref
        for (DataStore store : imageStores) {
            try {
                List<VolumeDataStoreVO> destroyedStoreVOs = _volumeStoreDao.listDestroyed(store.getId());
                s_logger.debug("Secondary storage garbage collector found " + destroyedStoreVOs.size() + " volumes to cleanup on volume_store_ref for store: " + store.getName());
                for (VolumeDataStoreVO destroyedStoreVO : destroyedStoreVOs) {
                    if (s_logger.isDebugEnabled()) {
                        s_logger.debug("Deleting volume store DB entry: " + destroyedStoreVO);
                    }
                    _volumeStoreDao.remove(destroyedStoreVO.getId());
                }
            } catch (Exception e2) {
                s_logger.warn("problem cleaning up volumes in volume_store_ref for store: " + store.getName(), e2);
            }
        }
    } catch (Exception e3) {
        s_logger.warn("problem cleaning up secondary storage DB entries. ", e3);
    }
}
Also used : ZoneScope(org.apache.cloudstack.engine.subsystem.api.storage.ZoneScope) SnapshotInfo(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) SnapshotDataStoreVO(org.apache.cloudstack.storage.datastore.db.SnapshotDataStoreVO) VolumeDataStoreVO(org.apache.cloudstack.storage.datastore.db.VolumeDataStoreVO) TemplateDataStoreVO(org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO) ConnectionException(com.cloud.exception.ConnectionException) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) OperationTimedoutException(com.cloud.exception.OperationTimedoutException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) StorageConflictException(com.cloud.exception.StorageConflictException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) StorageUnavailableException(com.cloud.exception.StorageUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) UnknownHostException(java.net.UnknownHostException) ExecutionException(java.util.concurrent.ExecutionException) ResourceInUseException(com.cloud.exception.ResourceInUseException) URISyntaxException(java.net.URISyntaxException) DiscoveryException(com.cloud.exception.DiscoveryException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ConfigurationException(javax.naming.ConfigurationException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) DB(com.cloud.utils.db.DB)

Example 14 with TemplateDataStoreVO

use of org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO in project cloudstack by apache.

the class StorageManagerImpl method deleteSecondaryStagingStore.

@Override
public boolean deleteSecondaryStagingStore(DeleteSecondaryStagingStoreCmd cmd) {
    final long storeId = cmd.getId();
    // Verify that cache store exists
    ImageStoreVO store = _imageStoreDao.findById(storeId);
    if (store == null) {
        throw new InvalidParameterValueException("Cache store with id " + storeId + " doesn't exist");
    }
    _accountMgr.checkAccessAndSpecifyAuthority(CallContext.current().getCallingAccount(), store.getDataCenterId());
    // Verify that there are no live snapshot, template, volume on the cache
    // store that is currently referenced
    List<SnapshotDataStoreVO> snapshots = _snapshotStoreDao.listActiveOnCache(storeId);
    if (snapshots != null && snapshots.size() > 0) {
        throw new InvalidParameterValueException("Cannot delete cache store with staging snapshots currently in use!");
    }
    List<VolumeDataStoreVO> volumes = _volumeStoreDao.listActiveOnCache(storeId);
    if (volumes != null && volumes.size() > 0) {
        throw new InvalidParameterValueException("Cannot delete cache store with staging volumes currently in use!");
    }
    List<TemplateDataStoreVO> templates = _templateStoreDao.listActiveOnCache(storeId);
    if (templates != null && templates.size() > 0) {
        throw new InvalidParameterValueException("Cannot delete cache store with staging templates currently in use!");
    }
    // ready to delete
    Transaction.execute(new TransactionCallbackNoReturn() {

        @Override
        public void doInTransactionWithoutResult(TransactionStatus status) {
            // first delete from image_store_details table, we need to do that since
            // we are not actually deleting record from main
            // image_data_store table, so delete cascade will not work
            _imageStoreDetailsDao.deleteDetails(storeId);
            _snapshotStoreDao.deletePrimaryRecordsForStore(storeId, DataStoreRole.ImageCache);
            _volumeStoreDao.deletePrimaryRecordsForStore(storeId);
            _templateStoreDao.deletePrimaryRecordsForStore(storeId);
            _imageStoreDao.remove(storeId);
        }
    });
    return true;
}
Also used : InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) SnapshotDataStoreVO(org.apache.cloudstack.storage.datastore.db.SnapshotDataStoreVO) VolumeDataStoreVO(org.apache.cloudstack.storage.datastore.db.VolumeDataStoreVO) TransactionStatus(com.cloud.utils.db.TransactionStatus) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) ImageStoreVO(org.apache.cloudstack.storage.datastore.db.ImageStoreVO) TemplateDataStoreVO(org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO)

Example 15 with TemplateDataStoreVO

use of org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO in project cloudstack by apache.

the class TemplateManagerImpl method createPrivateTemplate.

@Override
@DB
@ActionEvent(eventType = EventTypes.EVENT_TEMPLATE_CREATE, eventDescription = "creating template", async = true)
public VirtualMachineTemplate createPrivateTemplate(CreateTemplateCmd command) throws CloudRuntimeException {
    final long templateId = command.getEntityId();
    Long volumeId = command.getVolumeId();
    Long snapshotId = command.getSnapshotId();
    VMTemplateVO privateTemplate = null;
    final Long accountId = CallContext.current().getCallingAccountId();
    SnapshotVO snapshot = null;
    VolumeVO volume = null;
    try {
        TemplateInfo tmplInfo = _tmplFactory.getTemplate(templateId, DataStoreRole.Image);
        long zoneId = 0;
        if (snapshotId != null) {
            snapshot = _snapshotDao.findById(snapshotId);
            zoneId = snapshot.getDataCenterId();
        } else if (volumeId != null) {
            volume = _volumeDao.findById(volumeId);
            zoneId = volume.getDataCenterId();
        }
        DataStore store = _dataStoreMgr.getImageStore(zoneId);
        if (store == null) {
            throw new CloudRuntimeException("cannot find an image store for zone " + zoneId);
        }
        AsyncCallFuture<TemplateApiResult> future = null;
        if (snapshotId != null) {
            DataStoreRole dataStoreRole = ApiResponseHelper.getDataStoreRole(snapshot, _snapshotStoreDao, _dataStoreMgr);
            SnapshotInfo snapInfo = _snapshotFactory.getSnapshot(snapshotId, dataStoreRole);
            if (dataStoreRole == DataStoreRole.Image) {
                if (snapInfo == null) {
                    snapInfo = _snapshotFactory.getSnapshot(snapshotId, DataStoreRole.Primary);
                    if (snapInfo == null) {
                        throw new CloudRuntimeException("Cannot find snapshot " + snapshotId);
                    }
                    // We need to copy the snapshot onto secondary.
                    SnapshotStrategy snapshotStrategy = _storageStrategyFactory.getSnapshotStrategy(snapshot, SnapshotOperation.BACKUP);
                    snapshotStrategy.backupSnapshot(snapInfo);
                    // Attempt to grab it again.
                    snapInfo = _snapshotFactory.getSnapshot(snapshotId, dataStoreRole);
                    if (snapInfo == null) {
                        throw new CloudRuntimeException("Cannot find snapshot " + snapshotId + " on secondary and could not create backup");
                    }
                }
                DataStore snapStore = snapInfo.getDataStore();
                if (snapStore != null) {
                    // pick snapshot image store to create template
                    store = snapStore;
                }
            }
            future = _tmpltSvr.createTemplateFromSnapshotAsync(snapInfo, tmplInfo, store);
        } else if (volumeId != null) {
            VolumeInfo volInfo = _volFactory.getVolume(volumeId);
            future = _tmpltSvr.createTemplateFromVolumeAsync(volInfo, tmplInfo, store);
        } else {
            throw new CloudRuntimeException("Creating private Template need to specify snapshotId or volumeId");
        }
        CommandResult result = null;
        try {
            result = future.get();
            if (result.isFailed()) {
                privateTemplate = null;
                s_logger.debug("Failed to create template" + result.getResult());
                throw new CloudRuntimeException("Failed to create template" + result.getResult());
            }
            // create entries in template_zone_ref table
            if (_dataStoreMgr.isRegionStore(store)) {
                // template created on region store
                _tmpltSvr.associateTemplateToZone(templateId, null);
            } else {
                VMTemplateZoneVO templateZone = new VMTemplateZoneVO(zoneId, templateId, new Date());
                _tmpltZoneDao.persist(templateZone);
            }
            privateTemplate = _tmpltDao.findById(templateId);
            TemplateDataStoreVO srcTmpltStore = _tmplStoreDao.findByStoreTemplate(store.getId(), templateId);
            UsageEventVO usageEvent = new UsageEventVO(EventTypes.EVENT_TEMPLATE_CREATE, privateTemplate.getAccountId(), zoneId, privateTemplate.getId(), privateTemplate.getName(), null, privateTemplate.getSourceTemplateId(), srcTmpltStore.getPhysicalSize(), privateTemplate.getSize());
            _usageEventDao.persist(usageEvent);
        } catch (InterruptedException e) {
            s_logger.debug("Failed to create template", e);
            throw new CloudRuntimeException("Failed to create template", e);
        } catch (ExecutionException e) {
            s_logger.debug("Failed to create template", e);
            throw new CloudRuntimeException("Failed to create template", e);
        }
    } finally {
        if (privateTemplate == null) {
            final VolumeVO volumeFinal = volume;
            final SnapshotVO snapshotFinal = snapshot;
            Transaction.execute(new TransactionCallbackNoReturn() {

                @Override
                public void doInTransactionWithoutResult(TransactionStatus status) {
                    // template_store_ref entries should have been removed using our
                    // DataObject.processEvent command in case of failure, but clean
                    // it up here to avoid
                    // some leftovers which will cause removing template from
                    // vm_template table fail.
                    _tmplStoreDao.deletePrimaryRecordsForTemplate(templateId);
                    // Remove the template_zone_ref record
                    _tmpltZoneDao.deletePrimaryRecordsForTemplate(templateId);
                    // Remove the template record
                    _tmpltDao.expunge(templateId);
                    // decrement resource count
                    if (accountId != null) {
                        _resourceLimitMgr.decrementResourceCount(accountId, ResourceType.template);
                        _resourceLimitMgr.decrementResourceCount(accountId, ResourceType.secondary_storage, new Long(volumeFinal != null ? volumeFinal.getSize() : snapshotFinal.getSize()));
                    }
                }
            });
        }
    }
    if (privateTemplate != null) {
        return privateTemplate;
    } else {
        throw new CloudRuntimeException("Failed to create a template");
    }
}
Also used : VMTemplateZoneVO(com.cloud.storage.VMTemplateZoneVO) VMTemplateVO(com.cloud.storage.VMTemplateVO) TransactionStatus(com.cloud.utils.db.TransactionStatus) VolumeInfo(org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo) UsageEventVO(com.cloud.event.UsageEventVO) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) TemplateDataStoreVO(org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO) TemplateApiResult(org.apache.cloudstack.engine.subsystem.api.storage.TemplateService.TemplateApiResult) Date(java.util.Date) CommandResult(org.apache.cloudstack.storage.command.CommandResult) DataStoreRole(com.cloud.storage.DataStoreRole) TemplateInfo(org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo) SnapshotInfo(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo) SnapshotVO(com.cloud.storage.SnapshotVO) VolumeVO(com.cloud.storage.VolumeVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) PrimaryDataStore(org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) ExecutionException(java.util.concurrent.ExecutionException) SnapshotStrategy(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotStrategy) ActionEvent(com.cloud.event.ActionEvent) DB(com.cloud.utils.db.DB)

Aggregations

TemplateDataStoreVO (org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO)46 VMTemplateVO (com.cloud.storage.VMTemplateVO)22 DataStore (org.apache.cloudstack.engine.subsystem.api.storage.DataStore)22 TemplateInfo (org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo)15 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)14 Date (java.util.Date)13 ZoneScope (org.apache.cloudstack.engine.subsystem.api.storage.ZoneScope)8 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)7 PrimaryDataStore (org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore)7 TemplateApiResult (org.apache.cloudstack.engine.subsystem.api.storage.TemplateService.TemplateApiResult)7 VolumeDataStoreVO (org.apache.cloudstack.storage.datastore.db.VolumeDataStoreVO)7 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)6 VMTemplateStoragePoolVO (com.cloud.storage.VMTemplateStoragePoolVO)6 DB (com.cloud.utils.db.DB)6 ExecutionException (java.util.concurrent.ExecutionException)6 SnapshotDataStoreVO (org.apache.cloudstack.storage.datastore.db.SnapshotDataStoreVO)6 StorageUnavailableException (com.cloud.exception.StorageUnavailableException)5 ConfigurationException (javax.naming.ConfigurationException)5 ImageStoreEntity (org.apache.cloudstack.storage.image.datastore.ImageStoreEntity)5 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)4