Search in sources :

Example 31 with TemplateDataStoreVO

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

the class BareMetalTemplateAdapter method delete.

@Override
@DB
public boolean delete(TemplateProfile profile) {
    VMTemplateVO template = profile.getTemplate();
    Long templateId = template.getId();
    boolean success = true;
    String zoneName;
    if (!template.isCrossZones() && profile.getZoneId() != null) {
        zoneName = profile.getZoneId().toString();
    } else {
        zoneName = "all zones";
    }
    s_logger.debug("Attempting to mark template host refs for template: " + template.getName() + " as destroyed in zone: " + zoneName);
    Account account = _accountDao.findByIdIncludingRemoved(template.getAccountId());
    String eventType = EventTypes.EVENT_TEMPLATE_DELETE;
    List<TemplateDataStoreVO> templateHostVOs = this._tmpltStoreDao.listByTemplate(templateId);
    for (TemplateDataStoreVO vo : templateHostVOs) {
        TemplateDataStoreVO lock = null;
        try {
            lock = _tmpltStoreDao.acquireInLockTable(vo.getId());
            if (lock == null) {
                s_logger.debug("Failed to acquire lock when deleting templateDataStoreVO with ID: " + vo.getId());
                success = false;
                break;
            }
            vo.setDestroyed(true);
            _tmpltStoreDao.update(vo.getId(), vo);
        } finally {
            if (lock != null) {
                _tmpltStoreDao.releaseFromLockTable(lock.getId());
            }
        }
    }
    if (profile.getZoneId() != null) {
        UsageEventVO usageEvent = new UsageEventVO(eventType, account.getId(), profile.getZoneId(), templateId, null);
        _usageEventDao.persist(usageEvent);
    } else {
        List<DataCenterVO> dcs = _dcDao.listAllIncludingRemoved();
        for (DataCenterVO dc : dcs) {
            UsageEventVO usageEvent = new UsageEventVO(eventType, account.getId(), dc.getId(), templateId, null);
            _usageEventDao.persist(usageEvent);
        }
    }
    VMTemplateZoneVO templateZone = _tmpltZoneDao.findByZoneTemplate(profile.getZoneId(), templateId);
    if (templateZone != null) {
        _tmpltZoneDao.remove(templateZone.getId());
    }
    s_logger.debug("Successfully marked template host refs for template: " + template.getName() + " as destroyed in zone: " + zoneName);
    // If there are no more non-destroyed template host entries for this template, delete it
    if (success && (_tmpltStoreDao.listByTemplate(templateId).size() == 0)) {
        long accountId = template.getAccountId();
        VMTemplateVO lock = _tmpltDao.acquireInLockTable(templateId);
        try {
            if (lock == null) {
                s_logger.debug("Failed to acquire lock when deleting template with ID: " + templateId);
                success = false;
            } else if (_tmpltDao.remove(templateId)) {
                // Decrement the number of templates and total secondary storage space used by the account.
                _resourceLimitMgr.decrementResourceCount(accountId, ResourceType.template);
                _resourceLimitMgr.recalculateResourceCount(accountId, template.getDomainId(), ResourceType.secondary_storage.getOrdinal());
            }
        } finally {
            if (lock != null) {
                _tmpltDao.releaseFromLockTable(lock.getId());
            }
        }
        s_logger.debug("Removed template: " + template.getName() + " because all of its template host refs were marked as destroyed.");
    }
    return success;
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) Account(com.cloud.user.Account) VMTemplateZoneVO(com.cloud.storage.VMTemplateZoneVO) VMTemplateVO(com.cloud.storage.VMTemplateVO) UsageEventVO(com.cloud.event.UsageEventVO) TemplateDataStoreVO(org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO) DB(com.cloud.utils.db.DB)

Example 32 with TemplateDataStoreVO

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

the class StorageCacheReplacementAlgorithmLRU method chooseOneToBeReplaced.

@Override
public DataObject chooseOneToBeReplaced(DataStore store) {
    if (unusedTimeInterval == null) {
        unusedTimeInterval = NumbersUtil.parseInt(configDao.getValue(Config.StorageCacheReplacementLRUTimeInterval.key()), 30);
    }
    Calendar cal = Calendar.getInstance();
    cal.setTime(DateUtil.now());
    cal.add(Calendar.DAY_OF_MONTH, -unusedTimeInterval.intValue());
    Date bef = cal.getTime();
    QueryBuilder<TemplateDataStoreVO> sc = QueryBuilder.create(TemplateDataStoreVO.class);
    sc.and(sc.entity().getLastUpdated(), SearchCriteria.Op.LT, bef);
    sc.and(sc.entity().getState(), SearchCriteria.Op.EQ, ObjectInDataStoreStateMachine.State.Ready);
    sc.and(sc.entity().getDataStoreId(), SearchCriteria.Op.EQ, store.getId());
    sc.and(sc.entity().getDataStoreRole(), SearchCriteria.Op.EQ, store.getRole());
    sc.and(sc.entity().getRefCnt(), SearchCriteria.Op.EQ, 0);
    TemplateDataStoreVO template = sc.find();
    if (template != null) {
        return templateFactory.getTemplate(template.getTemplateId(), store);
    }
    QueryBuilder<VolumeDataStoreVO> volSc = QueryBuilder.create(VolumeDataStoreVO.class);
    volSc.and(volSc.entity().getLastUpdated(), SearchCriteria.Op.LT, bef);
    volSc.and(volSc.entity().getState(), SearchCriteria.Op.EQ, ObjectInDataStoreStateMachine.State.Ready);
    volSc.and(volSc.entity().getDataStoreId(), SearchCriteria.Op.EQ, store.getId());
    volSc.and(volSc.entity().getRefCnt(), SearchCriteria.Op.EQ, 0);
    VolumeDataStoreVO volume = volSc.find();
    if (volume != null) {
        return volumeFactory.getVolume(volume.getVolumeId(), store);
    }
    QueryBuilder<SnapshotDataStoreVO> snapshotSc = QueryBuilder.create(SnapshotDataStoreVO.class);
    snapshotSc.and(snapshotSc.entity().getLastUpdated(), SearchCriteria.Op.LT, bef);
    snapshotSc.and(snapshotSc.entity().getState(), SearchCriteria.Op.EQ, ObjectInDataStoreStateMachine.State.Ready);
    snapshotSc.and(snapshotSc.entity().getDataStoreId(), SearchCriteria.Op.EQ, store.getId());
    snapshotSc.and(snapshotSc.entity().getRole(), SearchCriteria.Op.EQ, store.getRole());
    snapshotSc.and(snapshotSc.entity().getRefCnt(), SearchCriteria.Op.EQ, 0);
    SnapshotDataStoreVO snapshot = snapshotSc.find();
    if (snapshot != null) {
        return snapshotFactory.getSnapshot(snapshot.getSnapshotId(), store);
    }
    return null;
}
Also used : Calendar(java.util.Calendar) VolumeDataStoreVO(org.apache.cloudstack.storage.datastore.db.VolumeDataStoreVO) SnapshotDataStoreVO(org.apache.cloudstack.storage.datastore.db.SnapshotDataStoreVO) TemplateDataStoreVO(org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO) Date(java.util.Date)

Example 33 with TemplateDataStoreVO

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

the class TemplateDataStoreDaoImpl method duplicateCacheRecordsOnRegionStore.

/**
     * Duplicate all image cache store entries
     */
@Override
public void duplicateCacheRecordsOnRegionStore(long storeId) {
    // find all records on image cache
    SearchCriteria<TemplateDataStoreVO> sc = templateRoleSearch.create();
    sc.setParameters("store_role", DataStoreRole.ImageCache);
    sc.setParameters("destroyed", false);
    List<TemplateDataStoreVO> tmpls = listBy(sc);
    // create an entry for each template record, but with empty install path since the content is not yet on region-wide store yet
    if (tmpls != null) {
        s_logger.info("Duplicate " + tmpls.size() + " template cache store records to region store");
        for (TemplateDataStoreVO tmpl : tmpls) {
            long templateId = tmpl.getTemplateId();
            VMTemplateVO template = _tmpltDao.findById(templateId);
            if (template == null) {
                throw new CloudRuntimeException("No template is found for template id: " + templateId);
            }
            if (template.getTemplateType() == TemplateType.SYSTEM) {
                s_logger.info("No need to duplicate system template since it will be automatically downloaded while adding region store");
                continue;
            }
            TemplateDataStoreVO tmpStore = findByStoreTemplate(storeId, tmpl.getTemplateId());
            if (tmpStore != null) {
                s_logger.info("There is already entry for template " + tmpl.getTemplateId() + " on region store " + storeId);
                continue;
            }
            s_logger.info("Persisting an entry for template " + tmpl.getTemplateId() + " on region store " + storeId);
            TemplateDataStoreVO ts = new TemplateDataStoreVO();
            ts.setTemplateId(tmpl.getTemplateId());
            ts.setDataStoreId(storeId);
            ts.setDataStoreRole(DataStoreRole.Image);
            ts.setState(tmpl.getState());
            ts.setDownloadPercent(tmpl.getDownloadPercent());
            ts.setDownloadState(tmpl.getDownloadState());
            ts.setSize(tmpl.getSize());
            ts.setPhysicalSize(tmpl.getPhysicalSize());
            ts.setErrorString(tmpl.getErrorString());
            ts.setDownloadUrl(tmpl.getDownloadUrl());
            ts.setRefCnt(tmpl.getRefCnt());
            persist(ts);
            // increase ref_cnt of cache store entry so that this will not be recycled before the content is pushed to region-wide store
            tmpl.incrRefCnt();
            this.update(tmpl.getId(), tmpl);
            // mark the template as cross-zones
            template.setCrossZones(true);
            _tmpltDao.update(templateId, template);
            // add template_zone_ref association for these cross-zone templates
            _tmplSrv.associateTemplateToZone(templateId, null);
        }
    }
}
Also used : CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VMTemplateVO(com.cloud.storage.VMTemplateVO) TemplateDataStoreVO(org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO)

Example 34 with TemplateDataStoreVO

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

the class TemplateDataStoreDaoImpl method updateStoreRoleToCachce.

@Override
public void updateStoreRoleToCachce(long storeId) {
    SearchCriteria<TemplateDataStoreVO> sc = storeSearch.create();
    sc.setParameters("store_id", storeId);
    sc.setParameters("destroyed", false);
    List<TemplateDataStoreVO> tmpls = listBy(sc);
    if (tmpls != null) {
        s_logger.info("Update to cache store role for " + tmpls.size() + " entries in template_store_ref");
        for (TemplateDataStoreVO tmpl : tmpls) {
            tmpl.setDataStoreRole(DataStoreRole.ImageCache);
            update(tmpl.getId(), tmpl);
        }
    }
}
Also used : TemplateDataStoreVO(org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO)

Example 35 with TemplateDataStoreVO

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

the class TemplateDataStoreDaoImpl method findByTemplateZoneReady.

@Override
public TemplateDataStoreVO findByTemplateZoneReady(long templateId, Long zoneId) {
    List<DataStore> imgStores = null;
    imgStores = _storeMgr.getImageStoresByScope(new ZoneScope(zoneId));
    if (imgStores != null) {
        Collections.shuffle(imgStores);
        for (DataStore store : imgStores) {
            List<TemplateDataStoreVO> sRes = listByTemplateStoreStatus(templateId, store.getId(), State.Ready);
            if (sRes != null && sRes.size() > 0) {
                return sRes.get(0);
            }
        }
    }
    return null;
}
Also used : ZoneScope(org.apache.cloudstack.engine.subsystem.api.storage.ZoneScope) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) TemplateDataStoreVO(org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO)

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