Search in sources :

Example 16 with SnapshotDataStoreVO

use of org.apache.cloudstack.storage.datastore.db.SnapshotDataStoreVO 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 17 with SnapshotDataStoreVO

use of org.apache.cloudstack.storage.datastore.db.SnapshotDataStoreVO 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 18 with SnapshotDataStoreVO

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

the class SnapshotManagerImpl method deleteSnapshotDirsForAccount.

@Override
public boolean deleteSnapshotDirsForAccount(long accountId) {
    List<VolumeVO> volumes = _volsDao.findByAccount(accountId);
    // The above call will list only non-destroyed volumes.
    // So call this method before marking the volumes as destroyed.
    // i.e Call them before the VMs for those volumes are destroyed.
    boolean success = true;
    for (VolumeVO volume : volumes) {
        if (volume.getPoolId() == null) {
            continue;
        }
        Long volumeId = volume.getId();
        Long dcId = volume.getDataCenterId();
        if (_snapshotDao.listByVolumeIdIncludingRemoved(volumeId).isEmpty()) {
            // This volume doesn't have any snapshots. Nothing do delete.
            continue;
        }
        List<DataStore> ssHosts = dataStoreMgr.getImageStoresByScope(new ZoneScope(dcId));
        for (DataStore ssHost : ssHosts) {
            String snapshotDir = TemplateConstants.DEFAULT_SNAPSHOT_ROOT_DIR + "/" + accountId + "/" + volumeId;
            DeleteSnapshotsDirCommand cmd = new DeleteSnapshotsDirCommand(ssHost.getTO(), snapshotDir);
            EndPoint ep = _epSelector.select(ssHost);
            Answer answer = null;
            if (ep == null) {
                String errMsg = "No remote endpoint to send command, check if host or ssvm is down?";
                s_logger.error(errMsg);
                answer = new Answer(cmd, false, errMsg);
            } else {
                answer = ep.sendMessage(cmd);
            }
            if ((answer != null) && answer.getResult()) {
                s_logger.debug("Deleted all snapshots for volume: " + volumeId + " under account: " + accountId);
            } else {
                success = false;
                if (answer != null) {
                    s_logger.warn("Failed to delete all snapshot for volume " + volumeId + " on secondary storage " + ssHost.getUri());
                    s_logger.error(answer.getDetails());
                }
            }
        }
        // Either way delete the snapshots for this volume.
        List<SnapshotVO> snapshots = listSnapsforVolume(volumeId);
        for (SnapshotVO snapshot : snapshots) {
            SnapshotStrategy snapshotStrategy = _storageStrategyFactory.getSnapshotStrategy(snapshot, SnapshotOperation.DELETE);
            if (snapshotStrategy == null) {
                s_logger.error("Unable to find snaphot strategy to handle snapshot with id '" + snapshot.getId() + "'");
                continue;
            }
            SnapshotDataStoreVO snapshotStoreRef = _snapshotStoreDao.findBySnapshot(snapshot.getId(), DataStoreRole.Image);
            if (snapshotStrategy.deleteSnapshot(snapshot.getId())) {
                if (Type.MANUAL == snapshot.getRecurringType()) {
                    _resourceLimitMgr.decrementResourceCount(accountId, ResourceType.snapshot);
                    if (snapshotStoreRef != null) {
                        _resourceLimitMgr.decrementResourceCount(accountId, ResourceType.secondary_storage, new Long(snapshotStoreRef.getPhysicalSize()));
                    }
                }
                // Log event after successful deletion
                UsageEventUtils.publishUsageEvent(EventTypes.EVENT_SNAPSHOT_DELETE, snapshot.getAccountId(), volume.getDataCenterId(), snapshot.getId(), snapshot.getName(), null, null, volume.getSize(), snapshot.getClass().getName(), snapshot.getUuid());
            }
        }
    }
    // Returns true if snapshotsDir has been deleted for all volumes.
    return success;
}
Also used : SnapshotDataStoreVO(org.apache.cloudstack.storage.datastore.db.SnapshotDataStoreVO) EndPoint(org.apache.cloudstack.engine.subsystem.api.storage.EndPoint) ZoneScope(org.apache.cloudstack.engine.subsystem.api.storage.ZoneScope) DeleteSnapshotsDirCommand(com.cloud.agent.api.DeleteSnapshotsDirCommand) Answer(com.cloud.agent.api.Answer) VMSnapshotVO(com.cloud.vm.snapshot.VMSnapshotVO) SnapshotVO(com.cloud.storage.SnapshotVO) VolumeVO(com.cloud.storage.VolumeVO) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) SnapshotStrategy(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotStrategy)

Example 19 with SnapshotDataStoreVO

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

the class SnapshotDataStoreDaoImpl method updateVolumeIds.

@Override
public void updateVolumeIds(long oldVolId, long newVolId) {
    SearchCriteria<SnapshotDataStoreVO> sc = volumeIdSearch.create();
    sc.setParameters("volume_id", oldVolId);
    SnapshotDataStoreVO snapshot = createForUpdate();
    snapshot.setVolumeId(newVolId);
    UpdateBuilder ub = getUpdateBuilder(snapshot);
    update(ub, sc, null);
}
Also used : SnapshotDataStoreVO(org.apache.cloudstack.storage.datastore.db.SnapshotDataStoreVO) UpdateBuilder(com.cloud.utils.db.UpdateBuilder)

Example 20 with SnapshotDataStoreVO

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

the class SnapshotDataStoreDaoImpl method duplicateCacheRecordsOnRegionStore.

@Override
public void duplicateCacheRecordsOnRegionStore(long storeId) {
    // find all records on image cache
    SearchCriteria<SnapshotDataStoreVO> sc = storeSnapshotSearch.create();
    sc.setParameters("store_role", DataStoreRole.ImageCache);
    sc.setParameters("destroyed", false);
    List<SnapshotDataStoreVO> snapshots = listBy(sc);
    // create an entry for each record, but with empty install path since the content is not yet on region-wide store yet
    if (snapshots != null) {
        s_logger.info("Duplicate " + snapshots.size() + " snapshot cache store records to region store");
        for (SnapshotDataStoreVO snap : snapshots) {
            SnapshotDataStoreVO snapStore = findByStoreSnapshot(DataStoreRole.Image, storeId, snap.getSnapshotId());
            if (snapStore != null) {
                s_logger.info("There is already entry for snapshot " + snap.getSnapshotId() + " on region store " + storeId);
                continue;
            }
            s_logger.info("Persisting an entry for snapshot " + snap.getSnapshotId() + " on region store " + storeId);
            SnapshotDataStoreVO ss = new SnapshotDataStoreVO();
            ss.setSnapshotId(snap.getSnapshotId());
            ss.setDataStoreId(storeId);
            ss.setRole(DataStoreRole.Image);
            ss.setVolumeId(snap.getVolumeId());
            ss.setParentSnapshotId(snap.getParentSnapshotId());
            ss.setState(snap.getState());
            ss.setSize(snap.getSize());
            ss.setPhysicalSize(snap.getPhysicalSize());
            ss.setRefCnt(snap.getRefCnt());
            persist(ss);
            // increase ref_cnt so that this will not be recycled before the content is pushed to region-wide store
            snap.incrRefCnt();
            update(snap.getId(), snap);
        }
    }
}
Also used : SnapshotDataStoreVO(org.apache.cloudstack.storage.datastore.db.SnapshotDataStoreVO)

Aggregations

SnapshotDataStoreVO (org.apache.cloudstack.storage.datastore.db.SnapshotDataStoreVO)38 DataStore (org.apache.cloudstack.engine.subsystem.api.storage.DataStore)15 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)11 SnapshotVO (com.cloud.storage.SnapshotVO)10 SnapshotInfo (org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo)10 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)9 VolumeDataStoreVO (org.apache.cloudstack.storage.datastore.db.VolumeDataStoreVO)8 TemplateDataStoreVO (org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO)6 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)5 StorageUnavailableException (com.cloud.exception.StorageUnavailableException)5 VolumeVO (com.cloud.storage.VolumeVO)5 Date (java.util.Date)5 ConfigurationException (javax.naming.ConfigurationException)5 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)4 DB (com.cloud.utils.db.DB)4 VMTemplateStoragePoolVO (com.cloud.storage.VMTemplateStoragePoolVO)3 Account (com.cloud.user.Account)3 NoTransitionException (com.cloud.utils.fsm.NoTransitionException)3 VMSnapshotVO (com.cloud.vm.snapshot.VMSnapshotVO)3 SnapshotResult (org.apache.cloudstack.engine.subsystem.api.storage.SnapshotResult)3