Update DataObjectInStore interface and unify implementation of

ImageDataFactory, VolumeDataFactory and SnapshotDataFactory
implementations.
This commit is contained in:
Min Chen 2013-04-17 11:40:57 -07:00
parent c74969fcb0
commit fe3b01ece1
17 changed files with 313 additions and 177 deletions

View File

@ -319,4 +319,15 @@ public class VMTemplateHostVO implements VMTemplateStorageResourceAssoc, DataObj
return updated;
}
@Override
public long getObjectId() {
return this.getTemplateId();
}
@Override
public long getDataStoreId() {
return this.getHostId();
}
}

View File

@ -271,5 +271,15 @@ public class VMTemplateStoragePoolVO implements VMTemplateStorageResourceAssoc,
return updated;
}
@Override
public long getObjectId() {
return this.getTemplateId();
}
@Override
public long getDataStoreId() {
return this.getPoolId();
}
}

View File

@ -344,4 +344,15 @@ public class VolumeHostVO implements InternalIdentity, DataObjectInStore {
return this.state;
}
@Override
public long getObjectId() {
return this.getVolumeId();
}
@Override
public long getDataStoreId() {
return this.getHostId();
}
}

View File

@ -24,4 +24,6 @@ import com.cloud.utils.fsm.StateObject;
public interface DataObjectInStore extends StateObject<ObjectInDataStoreStateMachine.State> {
public String getInstallPath();
public void setInstallPath(String path);
public long getObjectId();
public long getDataStoreId();
}

View File

@ -31,4 +31,12 @@ public interface SnapshotDataStoreDao extends GenericDao<SnapshotDataStoreVO, Lo
public List<SnapshotDataStoreVO> listByStoreId(long id);
public void deletePrimaryRecordsForStore(long id);
public SnapshotDataStoreVO findByStoreSnapshot(long storeId, long snapshotId);
public SnapshotDataStoreVO findByStoreSnapshot(long storeId, long snapshotId, boolean lock);
public SnapshotDataStoreVO findBySnapshot(long snapshotId);
public List<SnapshotDataStoreVO> listDestroyed(long storeId);
}

View File

@ -95,6 +95,7 @@ public class SnapshotDataStoreVO implements StateObject<ObjectInDataStoreStateMa
return installPath;
}
@Override
public long getDataStoreId() {
return dataStoreId;
}
@ -239,4 +240,10 @@ public class SnapshotDataStoreVO implements StateObject<ObjectInDataStoreStateMa
return this.state;
}
@Override
public long getObjectId() {
return this.getSnapshotId();
}
}

View File

@ -113,6 +113,7 @@ public class TemplateDataStoreVO implements StateObject<ObjectInDataStoreStateMa
}
@Override
public long getDataStoreId() {
return dataStoreId;
}
@ -313,4 +314,11 @@ public class TemplateDataStoreVO implements StateObject<ObjectInDataStoreStateMa
return updated;
}
@Override
public long getObjectId() {
return this.getTemplateId();
}
}

View File

@ -32,7 +32,11 @@ public interface VolumeDataStoreDao extends GenericDao<VolumeDataStoreVO, Long>,
public void deletePrimaryRecordsForStore(long id);
public VolumeDataStoreVO findByVolumeId(long volumeId);
public VolumeDataStoreVO findByVolume(long volumeId);
public VolumeDataStoreVO findByStoreVolume(long storeId, long volumeId);
public VolumeDataStoreVO findByStoreVolume(long storeId, long volumeId, boolean lock);
public List<VolumeDataStoreVO> listDestroyed(long storeId);
}

View File

@ -117,6 +117,7 @@ public class VolumeDataStoreVO implements StateObject<ObjectInDataStoreStateMach
return installPath;
}
@Override
public long getDataStoreId() {
return dataStoreId;
}
@ -345,4 +346,10 @@ public class VolumeDataStoreVO implements StateObject<ObjectInDataStoreStateMach
return this.state;
}
@Override
public long getObjectId() {
return this.getVolumeId();
}
}

View File

@ -21,13 +21,10 @@ package org.apache.cloudstack.storage.image;
import javax.inject.Inject;
import org.apache.cloudstack.engine.subsystem.api.storage.DataObject;
import org.apache.cloudstack.engine.subsystem.api.storage.DataObjectInStore;
import org.apache.cloudstack.engine.subsystem.api.storage.DataObjectType;
import org.apache.cloudstack.engine.subsystem.api.storage.DataStore;
import org.apache.cloudstack.engine.subsystem.api.storage.DataStoreManager;
import org.apache.cloudstack.engine.subsystem.api.storage.ImageDataFactory;
import org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo;
import org.apache.cloudstack.storage.datastore.ObjectInDataStoreManager;
import org.apache.cloudstack.storage.datastore.db.TemplateDataStoreDao;
import org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO;
import org.apache.cloudstack.storage.image.store.TemplateObject;
@ -47,8 +44,6 @@ public class ImageDataFactoryImpl implements ImageDataFactory {
@Inject
VMTemplateDao imageDataDao;
@Inject
ObjectInDataStoreManager objMap;
@Inject
DataStoreManager storeMgr;
@Inject
VMTemplatePoolDao templatePoolDao;
@ -61,6 +56,7 @@ public class ImageDataFactoryImpl implements ImageDataFactory {
TemplateObject tmpl = TemplateObject.getTemplate(templ, null);
return tmpl;
}
// verify if the given input parameters are consistent with our db data.
boolean found = false;
if (store.getRole() == DataStoreRole.Primary) {
VMTemplateStoragePoolVO templatePoolVO = templatePoolDao.findByPoolTemplate(store.getId(), templateId);
@ -68,8 +64,8 @@ public class ImageDataFactoryImpl implements ImageDataFactory {
found = true;
}
} else {
DataObjectInStore obj = objMap.findObject(templ.getId(), DataObjectType.TEMPLATE, store.getId(), store.getRole());
if (obj != null) {
TemplateDataStoreVO templateStoreVO = templateStoreDao.findByStoreTemplate(store.getId(), templateId);
if (templateStoreVO != null) {
found = true;
}
}
@ -82,8 +78,9 @@ public class ImageDataFactoryImpl implements ImageDataFactory {
return tmpl;
}
// NOTE that this method can only be used for get template information stored in secondary storage
//TODO: this method is problematic, since one template can be stored in multiple image stores.
// need to see if we can get rid of this method or change to plural format.
// need to see if we can get rid of this method or change to plural format, or restrict to 1:1 mapping
@Override
public TemplateInfo getTemplate(long templateId) {
VMTemplateVO templ = imageDataDao.findById(templateId);
@ -94,6 +91,7 @@ public class ImageDataFactoryImpl implements ImageDataFactory {
}
return this.getTemplate(templateId, store);
}
@Override
public TemplateInfo getTemplate(DataObject obj, DataStore store) {
return this.getTemplate(obj.getId(), store);

View File

@ -21,15 +21,14 @@ package org.apache.cloudstack.storage.snapshot;
import javax.inject.Inject;
import org.apache.cloudstack.engine.subsystem.api.storage.DataObject;
import org.apache.cloudstack.engine.subsystem.api.storage.DataObjectInStore;
import org.apache.cloudstack.engine.subsystem.api.storage.DataObjectType;
import org.apache.cloudstack.engine.subsystem.api.storage.DataStore;
import org.apache.cloudstack.engine.subsystem.api.storage.DataStoreManager;
import org.apache.cloudstack.engine.subsystem.api.storage.SnapshotDataFactory;
import org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo;
import org.apache.cloudstack.engine.subsystem.api.storage.VolumeDataFactory;
import org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo;
import org.apache.cloudstack.storage.datastore.ObjectInDataStoreManager;
import org.apache.cloudstack.storage.datastore.db.SnapshotDataStoreDao;
import org.apache.cloudstack.storage.datastore.db.SnapshotDataStoreVO;
import org.springframework.stereotype.Component;
import com.cloud.storage.DataStoreRole;
@ -43,7 +42,7 @@ public class SnapshotDataFactoryImpl implements SnapshotDataFactory {
@Inject
SnapshotDao snapshotDao;
@Inject
ObjectInDataStoreManager objMap;
SnapshotDataStoreDao snapshotStoreDao;
@Inject
DataStoreManager storeMgr;
@Inject
@ -51,19 +50,20 @@ public class SnapshotDataFactoryImpl implements SnapshotDataFactory {
@Override
public SnapshotInfo getSnapshot(long snapshotId, DataStore store) {
SnapshotVO snapshot = snapshotDao.findByIdIncludingRemoved(snapshotId);
DataObjectInStore obj = objMap.findObject(snapshot.getId(), DataObjectType.SNAPSHOT, store.getId(), store.getRole());
if (obj == null) {
return null;
}
SnapshotObject so = SnapshotObject.getSnapshotObject(snapshot, store);
return so;
}
@Override
public SnapshotInfo getSnapshot(long snapshotId) {
SnapshotVO snapshot = snapshotDao.findByIdIncludingRemoved(snapshotId);
SnapshotObject so = null;
if (snapshot.getState() == Snapshot.State.BackedUp) {
DataStore store = objMap.findStore(snapshot.getId(), DataObjectType.SNAPSHOT, DataStoreRole.Image);
DataStore store = null;
SnapshotDataStoreVO snapshotStore = snapshotStoreDao.findBySnapshot(snapshotId);
if ( snapshotStore != null ){
store = this.storeMgr.getDataStore(snapshotStore.getDataStoreId(), DataStoreRole.Image);
}
so = SnapshotObject.getSnapshotObject(snapshot, store);
} else {
VolumeInfo volume = this.volumeFactory.getVolume(snapshot.getVolumeId());

View File

@ -218,20 +218,11 @@ public class ObjectInDataStoreManagerImpl implements ObjectInDataStoreManager {
if (role == DataStoreRole.Image) {
switch (type){
case TEMPLATE:
SearchCriteria<TemplateDataStoreVO> ts = templateDataStoreDao.createSearchCriteria();
ts.addAnd("templateId", SearchCriteria.Op.EQ, objId);
ts.addAnd("dataStoreId", SearchCriteria.Op.EQ, dataStoreId);
vo = templateDataStoreDao.findOneBy(ts);
vo = templateDataStoreDao.findByStoreTemplate(dataStoreId, objId);
case SNAPSHOT:
SearchCriteria<SnapshotDataStoreVO> ss = snapshotDataStoreDao.createSearchCriteria();
ss.addAnd("snapshotId", SearchCriteria.Op.EQ, objId);
ss.addAnd("dataStoreId", SearchCriteria.Op.EQ, objId);
vo = snapshotDataStoreDao.findOneBy(ss);
vo = snapshotDataStoreDao.findByStoreSnapshot(dataStoreId, objId);
case VOLUME:
SearchCriteria<VolumeDataStoreVO> vs = volumeDataStoreDao.createSearchCriteria();
vs.addAnd("volumeId", SearchCriteria.Op.EQ, objId);
vs.addAnd("dataStoreId", SearchCriteria.Op.EQ, objId);
vo = volumeDataStoreDao.findOneBy(vs);
vo = volumeDataStoreDao.findByStoreVolume(dataStoreId, objId);
}
} else if (type == DataObjectType.TEMPLATE && role == DataStoreRole.Primary) {
vo = templatePoolDao.findByPoolTemplate(dataStoreId, objId);
@ -255,13 +246,17 @@ public class ObjectInDataStoreManagerImpl implements ObjectInDataStoreManager {
public DataStore findStore(long objId, DataObjectType type, DataStoreRole role) {
DataStore store = null;
if (role == DataStoreRole.Image) {
SearchCriteriaService<ObjectInDataStoreVO, ObjectInDataStoreVO> sc = SearchCriteria2.create(ObjectInDataStoreVO.class);
sc.addAnd(sc.getEntity().getDataStoreRole(), Op.EQ, role);
sc.addAnd(sc.getEntity().getObjectId(), Op.EQ, objId);
sc.addAnd(sc.getEntity().getObjectType(), Op.EQ, type);
ObjectInDataStoreVO vo = sc.find();
DataObjectInStore vo = null;
switch (type){
case TEMPLATE:
vo = templateDataStoreDao.findByTemplate(objId);
case SNAPSHOT:
vo = snapshotDataStoreDao.findBySnapshot(objId);
case VOLUME:
vo = volumeDataStoreDao.findByVolume(objId);
}
if (vo != null) {
store = this.storeMgr.getDataStore(vo.getDataStoreId(), vo.getDataStoreRole());
store = this.storeMgr.getDataStore(vo.getDataStoreId(), DataStoreRole.Image);
}
}
return store;

View File

@ -125,6 +125,7 @@ public class ObjectInDataStoreVO implements StateObject<ObjectInDataStoreStateMa
this.dataStoreRole = role;
}
@Override
public long getObjectId() {
return this.objectId;
}
@ -182,6 +183,7 @@ public class ObjectInDataStoreVO implements StateObject<ObjectInDataStoreStateMa
this.updated = updated;
}
@Override
public long getDataStoreId() {
return dataStoreId;
}

View File

@ -26,6 +26,7 @@ import org.apache.cloudstack.engine.subsystem.api.storage.ObjectInDataStoreState
import org.apache.cloudstack.engine.subsystem.api.storage.ObjectInDataStoreStateMachine.State;
import org.apache.cloudstack.storage.datastore.db.SnapshotDataStoreDao;
import org.apache.cloudstack.storage.datastore.db.SnapshotDataStoreVO;
import org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
@ -42,6 +43,8 @@ public class SnapshotDataStoreDaoImpl extends GenericDaoBase<SnapshotDataStoreVO
private static final Logger s_logger = Logger.getLogger(SnapshotDataStoreDaoImpl.class);
private SearchBuilder<SnapshotDataStoreVO> updateStateSearch;
private SearchBuilder<SnapshotDataStoreVO> storeSearch;
private SearchBuilder<SnapshotDataStoreVO> snapshotSearch;
private SearchBuilder<SnapshotDataStoreVO> storeSnapshotSearch;
@Override
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
@ -57,6 +60,18 @@ public class SnapshotDataStoreDaoImpl extends GenericDaoBase<SnapshotDataStoreVO
updateStateSearch.and("state", updateStateSearch.entity().getState(), Op.EQ);
updateStateSearch.and("updatedCount", updateStateSearch.entity().getUpdatedCount(), Op.EQ);
updateStateSearch.done();
snapshotSearch = createSearchBuilder();
snapshotSearch.and("snapshot_id", snapshotSearch.entity().getSnapshotId(), SearchCriteria.Op.EQ);
snapshotSearch.and("destroyed", snapshotSearch.entity().getDestroyed(), SearchCriteria.Op.EQ);
snapshotSearch.done();
storeSnapshotSearch = createSearchBuilder();
storeSnapshotSearch.and("snapshot_id", storeSnapshotSearch.entity().getSnapshotId(), SearchCriteria.Op.EQ);
storeSnapshotSearch.and("store_id", storeSnapshotSearch.entity().getDataStoreId(), SearchCriteria.Op.EQ);
storeSnapshotSearch.and("destroyed", storeSnapshotSearch.entity().getDestroyed(), SearchCriteria.Op.EQ);
storeSnapshotSearch.done();
return true;
}
@ -116,6 +131,41 @@ public class SnapshotDataStoreDaoImpl extends GenericDaoBase<SnapshotDataStoreVO
txn.commit();
}
@Override
public SnapshotDataStoreVO findByStoreSnapshot(long storeId, long snapshotId) {
SearchCriteria<SnapshotDataStoreVO> sc = storeSnapshotSearch.create();
sc.setParameters("store_id", storeId);
sc.setParameters("snapshot_id", snapshotId);
sc.setParameters("destroyed", false);
return findOneIncludingRemovedBy(sc);
}
@Override
public SnapshotDataStoreVO findByStoreSnapshot(long storeId, long snapshotId, boolean lock) {
SearchCriteria<SnapshotDataStoreVO> sc = storeSnapshotSearch.create();
sc.setParameters("store_id", storeId);
sc.setParameters("snapshot_id", snapshotId);
sc.setParameters("destroyed", false);
if (!lock)
return findOneIncludingRemovedBy(sc);
else
return lockOneRandomRow(sc, true);
}
@Override
public SnapshotDataStoreVO findBySnapshot(long snapshotId) {
SearchCriteria<SnapshotDataStoreVO> sc = snapshotSearch.create();
sc.setParameters("snapshot_id", snapshotId);
sc.setParameters("destroyed", false);
return findOneIncludingRemovedBy(sc);
}
@Override
public List<SnapshotDataStoreVO> listDestroyed(long id) {
SearchCriteria<SnapshotDataStoreVO> sc = storeSearch.create();
sc.setParameters("store_id", id);
sc.setParameters("destroyed", true);
return listIncludingRemovedBy(sc);
}
}

View File

@ -133,12 +133,13 @@ public class VolumeDataStoreDaoImpl extends GenericDaoBase<VolumeDataStoreVO, Lo
@Override
public VolumeDataStoreVO findByVolumeId(long volumeId) {
public VolumeDataStoreVO findByVolume(long volumeId) {
SearchCriteria<VolumeDataStoreVO> sc = volumeSearch.create();
sc.setParameters("volume_id", volumeId);
sc.setParameters("destroyed", false);
return findOneBy(sc);
}
@Override
public VolumeDataStoreVO findByStoreVolume(long storeId, long volumeId) {
SearchCriteria<VolumeDataStoreVO> sc = storeVolumeSearch.create();
@ -148,5 +149,23 @@ public class VolumeDataStoreDaoImpl extends GenericDaoBase<VolumeDataStoreVO, Lo
return findOneBy(sc);
}
@Override
public VolumeDataStoreVO findByStoreVolume(long storeId, long volumeId, boolean lock) {
SearchCriteria<VolumeDataStoreVO> sc = storeVolumeSearch.create();
sc.setParameters("store_id", storeId);
sc.setParameters("volume_id", volumeId);
sc.setParameters("destroyed", false);
if (!lock)
return findOneIncludingRemovedBy(sc);
else
return lockOneRandomRow(sc, true);
}
@Override
public List<VolumeDataStoreVO> listDestroyed(long id) {
SearchCriteria<VolumeDataStoreVO> sc = storeSearch.create();
sc.setParameters("store_id", id);
sc.setParameters("destroyed", true);
return listIncludingRemovedBy(sc);
}
}

View File

@ -21,12 +21,12 @@ package org.apache.cloudstack.storage.volume;
import javax.inject.Inject;
import org.apache.cloudstack.engine.subsystem.api.storage.DataObject;
import org.apache.cloudstack.engine.subsystem.api.storage.DataObjectType;
import org.apache.cloudstack.engine.subsystem.api.storage.DataStore;
import org.apache.cloudstack.engine.subsystem.api.storage.DataStoreManager;
import org.apache.cloudstack.engine.subsystem.api.storage.VolumeDataFactory;
import org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo;
import org.apache.cloudstack.storage.datastore.ObjectInDataStoreManager;
import org.apache.cloudstack.storage.datastore.db.VolumeDataStoreDao;
import org.apache.cloudstack.storage.datastore.db.VolumeDataStoreVO;
import org.springframework.stereotype.Component;
import com.cloud.storage.DataStoreRole;
@ -38,7 +38,7 @@ public class VolumeDataFactoryImpl implements VolumeDataFactory {
@Inject
VolumeDao volumeDao;
@Inject
ObjectInDataStoreManager objMap;
VolumeDataStoreDao volumeStoreDao;
@Inject
DataStoreManager storeMgr;
@Override
@ -55,7 +55,11 @@ public class VolumeDataFactoryImpl implements VolumeDataFactory {
VolumeVO volumeVO = volumeDao.findById(volumeId);
VolumeObject vol = null;
if (volumeVO.getPoolId() == null) {
DataStore store = objMap.findStore(volumeVO.getId(), DataObjectType.VOLUME, DataStoreRole.Image);
DataStore store = null;
VolumeDataStoreVO volumeStore = volumeStoreDao.findByVolume(volumeId);
if ( volumeStore != null ){
store = this.storeMgr.getDataStore(volumeStore.getDataStoreId(), DataStoreRole.Image);
}
vol = VolumeObject.getVolumeObject(store, volumeVO);
} else {
DataStore store = this.storeMgr.getDataStore(volumeVO.getPoolId(), DataStoreRole.Primary);

View File

@ -774,7 +774,7 @@ public class VolumeServiceImpl implements VolumeService {
if (volInfo.getSize() > 0) {
try {
String url = _volumeStoreDao.findByVolumeId(volume.getId()).getDownloadUrl();
String url = _volumeStoreDao.findByVolume(volume.getId()).getDownloadUrl();
_resourceLimitMgr.checkResourceLimit(_accountMgr.getAccount(volume.getAccountId()),
com.cloud.configuration.Resource.ResourceType.secondary_storage,
volInfo.getSize() - UriUtils.getRemoteSize(url));