From 52799f46a485c1a9fa8ed8e76b1cabd777583e40 Mon Sep 17 00:00:00 2001 From: Edison Su Date: Wed, 1 May 2013 14:00:53 -0700 Subject: [PATCH] fix data motion --- .../subsystem/api/storage/SnapshotInfo.java | 1 - .../motion/AncientDataMotionStrategy.java | 22 ++++++-- .../storage/snapshot/SnapshotObject.java | 50 +++++++++++++------ 3 files changed, 53 insertions(+), 20 deletions(-) diff --git a/engine/api/src/org/apache/cloudstack/engine/subsystem/api/storage/SnapshotInfo.java b/engine/api/src/org/apache/cloudstack/engine/subsystem/api/storage/SnapshotInfo.java index 2973a545c94..41ea5f3dd3c 100644 --- a/engine/api/src/org/apache/cloudstack/engine/subsystem/api/storage/SnapshotInfo.java +++ b/engine/api/src/org/apache/cloudstack/engine/subsystem/api/storage/SnapshotInfo.java @@ -26,6 +26,5 @@ public interface SnapshotInfo extends DataObject, Snapshot { public VolumeInfo getBaseVolume(); public void addPayload(Object data); Long getDataCenterId(); - public Long getPrevSnapshotId(); ObjectInDataStoreStateMachine.State getStatus(); } diff --git a/engine/storage/datamotion/src/org/apache/cloudstack/storage/motion/AncientDataMotionStrategy.java b/engine/storage/datamotion/src/org/apache/cloudstack/storage/motion/AncientDataMotionStrategy.java index 2075ef60b9c..5b8cbf68e1b 100644 --- a/engine/storage/datamotion/src/org/apache/cloudstack/storage/motion/AncientDataMotionStrategy.java +++ b/engine/storage/datamotion/src/org/apache/cloudstack/storage/motion/AncientDataMotionStrategy.java @@ -29,6 +29,7 @@ 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.DataTO; import org.apache.cloudstack.engine.subsystem.api.storage.EndPoint; import org.apache.cloudstack.engine.subsystem.api.storage.EndPointSelector; @@ -136,6 +137,21 @@ public class AncientDataMotionStrategy implements DataMotionStrategy { // TODO Auto-generated method stub return true; } + + protected boolean needCacheStorage(DataObject srcData, DataObject destData) { + DataTO srcTO = srcData.getTO(); + DataTO destTO = destData.getTO(); + DataStoreTO srcStoreTO = srcTO.getDataStore(); + DataStoreTO destStoreTO = destTO.getDataStore(); + if (srcStoreTO instanceof NfsTO || srcStoreTO.getRole() == DataStoreRole.ImageCache) { + return false; + } + + if (destStoreTO instanceof NfsTO || destStoreTO.getRole() == DataStoreRole.ImageCache) { + return false; + } + return true; + } @DB protected Answer copyVolumeFromImage(DataObject srcData, DataObject destData) { @@ -143,7 +159,7 @@ public class AncientDataMotionStrategy implements DataMotionStrategy { int _copyvolumewait = NumbersUtil.parseInt(value, Integer.parseInt(Config.CopyVolumeWait.getDefaultValue())); - if (srcData.getDataStore().getRole() != DataStoreRole.ImageCache && destData.getDataStore().getRole() != DataStoreRole.ImageCache) { + if (needCacheStorage(srcData, destData)) { //need to copy it to image cache store DataObject cacheData = cacheMgr.createCacheObject(srcData, destData.getDataStore().getScope()); CopyCommand cmd = new CopyCommand(cacheData.getTO(), destData.getTO(), _copyvolumewait); @@ -162,7 +178,7 @@ public class AncientDataMotionStrategy implements DataMotionStrategy { private Answer copyTemplate(DataObject srcData, DataObject destData) { String value = configDao.getValue(Config.PrimaryStorageDownloadWait.toString()); int _primaryStorageDownloadWait = NumbersUtil.parseInt(value, Integer.parseInt(Config.PrimaryStorageDownloadWait.getDefaultValue())); - if (srcData.getDataStore().getRole() != DataStoreRole.ImageCache && destData.getDataStore().getRole() != DataStoreRole.ImageCache) { + if (needCacheStorage(srcData, destData)) { //need to copy it to image cache store DataObject cacheData = cacheMgr.createCacheObject(srcData, destData.getDataStore().getScope()); CopyCommand cmd = new CopyCommand(cacheData.getTO(), destData.getTO(), _primaryStorageDownloadWait); @@ -330,7 +346,7 @@ public class AncientDataMotionStrategy implements DataMotionStrategy { Integer.parseInt(Config.CreatePrivateTemplateFromVolumeWait .getDefaultValue())); - if (srcData.getDataStore().getRole() != DataStoreRole.ImageCache && destData.getDataStore().getRole() != DataStoreRole.ImageCache) { + if (needCacheStorage(srcData, destData)) { //need to copy it to image cache store DataObject cacheData = cacheMgr.createCacheObject(srcData, destData.getDataStore().getScope()); CopyCommand cmd = new CopyCommand(cacheData.getTO(), destData.getTO(), _createprivatetemplatefromvolumewait); diff --git a/engine/storage/snapshot/src/org/apache/cloudstack/storage/snapshot/SnapshotObject.java b/engine/storage/snapshot/src/org/apache/cloudstack/storage/snapshot/SnapshotObject.java index 6fddcdacbbe..c19efb0d47f 100644 --- a/engine/storage/snapshot/src/org/apache/cloudstack/storage/snapshot/SnapshotObject.java +++ b/engine/storage/snapshot/src/org/apache/cloudstack/storage/snapshot/SnapshotObject.java @@ -26,6 +26,7 @@ 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.DataTO; import org.apache.cloudstack.engine.subsystem.api.storage.ObjectInDataStoreStateMachine; +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; @@ -45,6 +46,9 @@ import com.cloud.storage.SnapshotVO; import com.cloud.storage.dao.SnapshotDao; import com.cloud.storage.dao.VolumeDao; import com.cloud.utils.component.ComponentContext; +import com.cloud.utils.db.SearchCriteria.Op; +import com.cloud.utils.db.SearchCriteria2; +import com.cloud.utils.db.SearchCriteriaService; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.utils.fsm.NoTransitionException; @@ -59,9 +63,11 @@ public class SnapshotObject implements SnapshotInfo { @Inject protected VolumeDataFactory volFactory; @Inject protected SnapshotStateMachineManager stateMachineMgr; @Inject + SnapshotDataFactory snapshotFactory; + @Inject ObjectInDataStoreManager ojbectInStoreMgr; @Inject - SnapshotDataStoreDao snapshotStore; + SnapshotDataStoreDao snapshotStoreDao; public SnapshotObject() { } @@ -83,15 +89,30 @@ public class SnapshotObject implements SnapshotInfo { @Override public SnapshotInfo getParent() { + SnapshotDataStoreVO snapStoreVO = this.snapshotStoreDao.findByStoreSnapshot(this.store.getRole(), this.store.getId(), this.snapshot.getId()); + if (snapStoreVO == null) { + return null; + } - // TODO Auto-generated method stub - return null; + long parentId = snapStoreVO.getParentSnapshotId(); + if (parentId == 0) { + return null; + } + + return this.snapshotFactory.getSnapshot(parentId, store); } @Override public SnapshotInfo getChild() { - // TODO Auto-generated method stub - return null; + SearchCriteriaService sc = SearchCriteria2.create(SnapshotDataStoreVO.class); + sc.addAnd(sc.getEntity().getDataStoreId(), Op.EQ, this.store.getId()); + sc.addAnd(sc.getEntity().getRole(), Op.EQ, this.store.getRole()); + sc.addAnd(sc.getEntity().getParentSnapshotId(), Op.EQ, this.getId()); + SnapshotDataStoreVO vo = sc.find(); + if (vo == null) { + return null; + } + return this.snapshotFactory.getSnapshot(vo.getId(), store); } @Override @@ -211,37 +232,34 @@ public class SnapshotObject implements SnapshotInfo { stateMachineMgr.processEvent(this.snapshot, event); } - @Override - public Long getPrevSnapshotId() { - SnapshotDataStoreVO snapshotStoreVO = this.snapshotStore.findBySnapshot(this.getId(), this.getDataStore().getRole()); - return snapshotStoreVO.getParentSnapshotId(); - } - public SnapshotVO getSnapshotVO(){ return this.snapshot; } @Override public DataTO getTO() { - // TODO Auto-generated method stub - return null; + DataTO to = this.store.getDriver().getTO(this); + if (to == null) { + return new SnapshotObjectTO(this); + } + return to; } @Override public void processEvent(ObjectInDataStoreStateMachine.Event event, Answer answer) { - SnapshotDataStoreVO snapshotStore = this.snapshotStore.findByStoreSnapshot(this.getDataStore().getRole(), + SnapshotDataStoreVO snapshotStore = this.snapshotStoreDao.findByStoreSnapshot(this.getDataStore().getRole(), this.getDataStore().getId(), this.getId()); if (answer instanceof CreateObjectAnswer) { SnapshotObjectTO snapshotTO = (SnapshotObjectTO)((CreateObjectAnswer) answer).getData(); snapshotStore.setInstallPath(snapshotTO.getPath()); - this.snapshotStore.update(snapshotStore.getId(), snapshotStore); + this.snapshotStoreDao.update(snapshotStore.getId(), snapshotStore); } else if (answer instanceof CopyCmdAnswer) { SnapshotObjectTO snapshotTO = (SnapshotObjectTO)((CopyCmdAnswer) answer).getNewData(); snapshotStore.setInstallPath(snapshotTO.getPath()); if (snapshotTO.getParentSnapshotPath() == null) { snapshotStore.setParentSnapshotId(0L); } - this.snapshotStore.update(snapshotStore.getId(), snapshotStore); + this.snapshotStoreDao.update(snapshotStore.getId(), snapshotStore); } else { throw new CloudRuntimeException("Unknown answer: " + answer.getClass()); }