From 8d29cdf804e0c4fe9fa1e9ed6d00bc2f6117b6e5 Mon Sep 17 00:00:00 2001 From: Min Chen Date: Wed, 24 Apr 2013 11:54:42 -0700 Subject: [PATCH] Remove SnapshotVO.getSecHostId and SnapshotVO.setSecHostId references from codebase to use SnapshotDataStoreVO. --- core/src/com/cloud/storage/SnapshotVO.java | 11 --- .../motion/AncientDataMotionStrategy.java | 30 ++++---- .../storage/snapshot/SnapshotServiceImpl.java | 2 - .../com/cloud/storage/StorageManagerImpl.java | 2 +- .../com/cloud/storage/dao/SnapshotDao.java | 2 - .../cloud/storage/dao/SnapshotDaoImpl.java | 74 +++++++------------ .../storage/snapshot/SnapshotManagerImpl.java | 27 ++++--- 7 files changed, 56 insertions(+), 92 deletions(-) diff --git a/core/src/com/cloud/storage/SnapshotVO.java b/core/src/com/cloud/storage/SnapshotVO.java index 78b96ec9779..dbfd7baa617 100644 --- a/core/src/com/cloud/storage/SnapshotVO.java +++ b/core/src/com/cloud/storage/SnapshotVO.java @@ -85,9 +85,6 @@ public class SnapshotVO implements Snapshot { @Column(name="s3_id") Long s3Id; - @Column(name="sechost_id") - Long secHostId; - @Column(name="prev_snap_id") long prevSnapshotId; @@ -190,14 +187,6 @@ public class SnapshotVO implements Snapshot { this.swiftId = swiftId; } - public Long getSecHostId() { - return secHostId; - } - - public void setSecHostId(Long secHostId) { - this.secHostId = secHostId; - } - @Override public HypervisorType getHypervisorType() { return hypervisorType; 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 affe0308dba..84d2b16e91c 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 @@ -38,6 +38,8 @@ import org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo; import org.apache.cloudstack.framework.async.AsyncCompletionCallback; import org.apache.cloudstack.storage.command.CopyCommand; import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao; +import org.apache.cloudstack.storage.datastore.db.SnapshotDataStoreDao; +import org.apache.cloudstack.storage.datastore.db.SnapshotDataStoreVO; import org.apache.cloudstack.storage.datastore.db.StoragePoolVO; import org.apache.cloudstack.storage.datastore.db.TemplateDataStoreDao; import org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO; @@ -113,6 +115,8 @@ public class AncientDataMotionStrategy implements DataMotionStrategy { @Inject SnapshotDao snapshotDao; @Inject + SnapshotDataStoreDao _snapshotStoreDao; + @Inject PrimaryDataStoreDao primaryDataStoreDao; @Inject DataStoreManager dataStoreMgr; @@ -622,12 +626,12 @@ public class AncientDataMotionStrategy implements DataMotionStrategy { return sendCommand(cmd, pool, template.getId(), zoneId, secStore); } - private HostVO getSecHost(long volumeId, long dcId) { + private DataStore getSecHost(long volumeId, long dcId) { Long id = snapshotDao.getSecHostId(volumeId); if ( id != null) { - return hostDao.findById(id); + return this.dataStoreMgr.getDataStore(id, DataStoreRole.Image); } - return this.templateMgr.getSecondaryStorageHost(dcId); + return this.dataStoreMgr.getImageStore(dcId); } protected Answer copySnapshot(DataObject srcObject, DataObject destObject) { @@ -636,9 +640,9 @@ public class AncientDataMotionStrategy implements DataMotionStrategy { Long dcId = baseVolume.getDataCenterId(); Long accountId = baseVolume.getAccountId(); - HostVO secHost = getSecHost(baseVolume.getId(), baseVolume.getDataCenterId()); - Long secHostId = secHost.getId(); - String secondaryStoragePoolUrl = secHost.getStorageUrl(); + DataStore secStore = getSecHost(baseVolume.getId(), baseVolume.getDataCenterId()); + Long secHostId = secStore.getId(); + String secondaryStoragePoolUrl = secStore.getUri(); String snapshotUuid = srcSnapshot.getPath(); // In order to verify that the snapshot is not empty, // we check if the parent of the snapshot is not the same as the parent of the previous snapshot. @@ -681,16 +685,10 @@ public class AncientDataMotionStrategy implements DataMotionStrategy { BackupSnapshotAnswer answer = (BackupSnapshotAnswer) this.snapshotMgr.sendToPool(baseVolume, backupSnapshotCommand); if (answer != null && answer.getResult()) { SnapshotVO snapshotVO = this.snapshotDao.findById(srcSnapshot.getId()); - if (backupSnapshotCommand.getSwift() != null ) { - snapshotVO.setSwiftId(swift.getId()); - snapshotVO.setBackupSnapshotId(answer.getBackupSnapshotName()); - } else if (backupSnapshotCommand.getS3() != null) { - snapshotVO.setS3Id(s3.getId()); - snapshotVO.setBackupSnapshotId(answer.getBackupSnapshotName()); - } else { - snapshotVO.setSecHostId(secHost.getId()); - snapshotVO.setBackupSnapshotId(answer.getBackupSnapshotName()); - } + snapshotVO.setBackupSnapshotId(answer.getBackupSnapshotName()); + // persist an entry in snapshot_store_ref + SnapshotDataStoreVO snapshotStore = new SnapshotDataStoreVO(secStore.getId(), snapshotVO.getId()); + this._snapshotStoreDao.persist(snapshotStore); if (answer.isFull()) { snapshotVO.setPrevSnapshotId(0L); } diff --git a/engine/storage/snapshot/src/org/apache/cloudstack/storage/snapshot/SnapshotServiceImpl.java b/engine/storage/snapshot/src/org/apache/cloudstack/storage/snapshot/SnapshotServiceImpl.java index 7eee7720330..633e193b939 100644 --- a/engine/storage/snapshot/src/org/apache/cloudstack/storage/snapshot/SnapshotServiceImpl.java +++ b/engine/storage/snapshot/src/org/apache/cloudstack/storage/snapshot/SnapshotServiceImpl.java @@ -191,9 +191,7 @@ public class SnapshotServiceImpl implements SnapshotService { snapshotVO.setPath(preSnapshotPath); snapshotVO.setBackupSnapshotId(preSnapshotVO.getBackupSnapshotId()); - snapshotVO.setSwiftId(preSnapshotVO.getSwiftId()); snapshotVO.setPrevSnapshotId(preSnapshotVO.getId()); - snapshotVO.setSecHostId(preSnapshotVO.getSecHostId()); snapshot.processEvent(Snapshot.Event.OperationNotPerformed); } else { long preSnapshotId = 0; diff --git a/server/src/com/cloud/storage/StorageManagerImpl.java b/server/src/com/cloud/storage/StorageManagerImpl.java index ac9c6f4d238..37fb3769156 100755 --- a/server/src/com/cloud/storage/StorageManagerImpl.java +++ b/server/src/com/cloud/storage/StorageManagerImpl.java @@ -1169,7 +1169,7 @@ public class StorageManagerImpl extends ManagerBase implements StorageManager, C @DB List findAllVolumeIdInSnapshotTable(Long storeId) { - String sql = "SELECT volume_id from snapshots, snapshot_store_ref WHERE store_id=? GROUP BY volume_id"; + String sql = "SELECT volume_id from snapshots, snapshot_store_ref WHERE snapshots.id = snapshot_store_ref.snapshot_id and store_id=? GROUP BY volume_id"; List list = new ArrayList(); try { Transaction txn = Transaction.currentTxn(); diff --git a/server/src/com/cloud/storage/dao/SnapshotDao.java b/server/src/com/cloud/storage/dao/SnapshotDao.java index 3ac9e77fb5d..e6aecc7654f 100644 --- a/server/src/com/cloud/storage/dao/SnapshotDao.java +++ b/server/src/com/cloud/storage/dao/SnapshotDao.java @@ -37,8 +37,6 @@ public interface SnapshotDao extends GenericDao, StateDao listByVolumeIdVersion(long volumeId, String version); Long getSecHostId(long volumeId); long updateSnapshotSecHost(long dcId, long secHostId); - List listByHostId(Filter filter, long hostId); - List listByHostId(long hostId); public Long countSnapshotsForAccount(long accountId); List listByInstanceId(long instanceId, Snapshot.State... status); List listByStatus(long volumeId, Snapshot.State... status); diff --git a/server/src/com/cloud/storage/dao/SnapshotDaoImpl.java b/server/src/com/cloud/storage/dao/SnapshotDaoImpl.java index f55663fe204..2dd5da14265 100644 --- a/server/src/com/cloud/storage/dao/SnapshotDaoImpl.java +++ b/server/src/com/cloud/storage/dao/SnapshotDaoImpl.java @@ -54,9 +54,10 @@ import com.cloud.vm.dao.VMInstanceDao; @Local (value={SnapshotDao.class}) public class SnapshotDaoImpl extends GenericDaoBase implements SnapshotDao { public static final Logger s_logger = Logger.getLogger(SnapshotDaoImpl.class.getName()); + //TODO: we should remove these direct sqls private static final String GET_LAST_SNAPSHOT = "SELECT id FROM snapshots where volume_id = ? AND id != ? AND path IS NOT NULL ORDER BY created DESC"; private static final String UPDATE_SNAPSHOT_VERSION = "UPDATE snapshots SET version = ? WHERE volume_id = ? AND version = ?"; - private static final String GET_SECHOST_ID = "SELECT sechost_id FROM snapshots where volume_id = ? AND backup_snap_id IS NOT NULL AND sechost_id IS NOT NULL LIMIT 1"; + private static final String GET_SECHOST_ID = "SELECT store_id FROM snapshots, snapshot_store_ref where snapshots.id = snapshot_store_ref.snapshot_id AND volume_id = ? AND backup_snap_id IS NOT NULL AND sechost_id IS NOT NULL LIMIT 1"; private static final String UPDATE_SECHOST_ID = "UPDATE snapshots SET sechost_id = ? WHERE data_center_id = ?"; private SearchBuilder VolumeIdSearch; @@ -64,7 +65,6 @@ public class SnapshotDaoImpl extends GenericDaoBase implements private SearchBuilder ParentIdSearch; private SearchBuilder backupUuidSearch; private SearchBuilder VolumeIdVersionSearch; - private SearchBuilder HostIdSearch; private SearchBuilder AccountIdSearch; private SearchBuilder InstanceIdSearch; private SearchBuilder StatusSearch; @@ -80,20 +80,20 @@ public class SnapshotDaoImpl extends GenericDaoBase implements sc.setParameters("prevSnapshotId", snapshotId); return findOneIncludingRemovedBy(sc); } - + @Override public List listByBackupUuid(long volumeId, String backupUuid) { SearchCriteria sc = backupUuidSearch.create(); sc.setParameters("backupUuid", backupUuid); return listBy(sc, null); } - + @Override public List listByVolumeIdType(long volumeId, Type type ) { return listByVolumeIdType(null, volumeId, type); } - - + + @Override public List listByVolumeIdVersion(long volumeId, String version ) { return listByVolumeIdVersion(null, volumeId, version); @@ -103,41 +103,28 @@ public class SnapshotDaoImpl extends GenericDaoBase implements public List listByVolumeId(long volumeId) { return listByVolumeId(null, volumeId); } - + @Override public List listByVolumeId(Filter filter, long volumeId ) { SearchCriteria sc = VolumeIdSearch.create(); sc.setParameters("volumeId", volumeId); return listBy(sc, filter); } - - @Override - public List listByHostId(long hostId) { - return listByHostId(null, hostId); - } - - @Override - public List listByHostId(Filter filter, long hostId ) { - SearchCriteria sc = HostIdSearch.create(); - sc.setParameters("hostId", hostId); - sc.setParameters("status", Snapshot.State.BackedUp); - return listBy(sc, filter); - } - - @Override + + @Override public List listByVolumeIdIncludingRemoved(long volumeId) { SearchCriteria sc = VolumeIdSearch.create(); sc.setParameters("volumeId", volumeId); return listIncludingRemovedBy(sc, null); } - + public List listByVolumeIdType(Filter filter, long volumeId, Type type ) { SearchCriteria sc = VolumeIdTypeSearch.create(); sc.setParameters("volumeId", volumeId); sc.setParameters("type", type.ordinal()); return listBy(sc, filter); } - + public List listByVolumeIdVersion(Filter filter, long volumeId, String version ) { SearchCriteria sc = VolumeIdVersionSearch.create(); sc.setParameters("volumeId", volumeId); @@ -147,32 +134,27 @@ public class SnapshotDaoImpl extends GenericDaoBase implements public SnapshotDaoImpl() { } - + @PostConstruct protected void init() { VolumeIdSearch = createSearchBuilder(); VolumeIdSearch.and("volumeId", VolumeIdSearch.entity().getVolumeId(), SearchCriteria.Op.EQ); VolumeIdSearch.done(); - - HostIdSearch = createSearchBuilder(); - HostIdSearch.and("hostId", HostIdSearch.entity().getSecHostId(), SearchCriteria.Op.EQ); - HostIdSearch.and("status", HostIdSearch.entity().getState(), SearchCriteria.Op.EQ); - HostIdSearch.done(); - + VolumeIdTypeSearch = createSearchBuilder(); VolumeIdTypeSearch.and("volumeId", VolumeIdTypeSearch.entity().getVolumeId(), SearchCriteria.Op.EQ); VolumeIdTypeSearch.and("type", VolumeIdTypeSearch.entity().getsnapshotType(), SearchCriteria.Op.EQ); VolumeIdTypeSearch.done(); - + VolumeIdVersionSearch = createSearchBuilder(); VolumeIdVersionSearch.and("volumeId", VolumeIdVersionSearch.entity().getVolumeId(), SearchCriteria.Op.EQ); VolumeIdVersionSearch.and("version", VolumeIdVersionSearch.entity().getVersion(), SearchCriteria.Op.EQ); VolumeIdVersionSearch.done(); - + ParentIdSearch = createSearchBuilder(); ParentIdSearch.and("prevSnapshotId", ParentIdSearch.entity().getPrevSnapshotId(), SearchCriteria.Op.EQ); ParentIdSearch.done(); - + backupUuidSearch = createSearchBuilder(); backupUuidSearch.and("backupUuid", backupUuidSearch.entity().getBackupSnapshotId(), SearchCriteria.Op.EQ); backupUuidSearch.done(); @@ -180,18 +162,18 @@ public class SnapshotDaoImpl extends GenericDaoBase implements AccountIdSearch = createSearchBuilder(); AccountIdSearch.and("accountId", AccountIdSearch.entity().getAccountId(), SearchCriteria.Op.EQ); AccountIdSearch.done(); - + StatusSearch = createSearchBuilder(); StatusSearch.and("volumeId", StatusSearch.entity().getVolumeId(), SearchCriteria.Op.EQ); StatusSearch.and("status", StatusSearch.entity().getState(), SearchCriteria.Op.IN); StatusSearch.done(); - + CountSnapshotsByAccount = createSearchBuilder(Long.class); - CountSnapshotsByAccount.select(null, Func.COUNT, null); + CountSnapshotsByAccount.select(null, Func.COUNT, null); CountSnapshotsByAccount.and("account", CountSnapshotsByAccount.entity().getAccountId(), SearchCriteria.Op.EQ); CountSnapshotsByAccount.and("removed", CountSnapshotsByAccount.entity().getRemoved(), SearchCriteria.Op.NULL); CountSnapshotsByAccount.done(); - + InstanceIdSearch = createSearchBuilder(); InstanceIdSearch.and("status", InstanceIdSearch.entity().getState(), SearchCriteria.Op.IN); @@ -227,7 +209,7 @@ public class SnapshotDaoImpl extends GenericDaoBase implements } } catch (Exception ex) { } - return null; + return null; } @Override public long getLastSnapshot(long volumeId, long snapId) { @@ -265,7 +247,7 @@ public class SnapshotDaoImpl extends GenericDaoBase implements } return 0; } - + @Override public long updateSnapshotSecHost(long dcId, long secHostId) { Transaction txn = Transaction.currentTxn(); @@ -289,20 +271,20 @@ public class SnapshotDaoImpl extends GenericDaoBase implements sc.setParameters("account", accountId); return customSearch(sc, null).get(0); } - + @Override public List listByInstanceId(long instanceId, Snapshot.State... status) { SearchCriteria sc = this.InstanceIdSearch.create(); - + if (status != null && status.length != 0) { sc.setParameters("status", (Object[])status); } - + sc.setJoinParameters("instanceSnapshots", "state", Volume.State.Ready); sc.setJoinParameters("instanceVolumes", "instanceId", instanceId); return listBy(sc, null); } - + @Override public List listByStatus(long volumeId, Snapshot.State... status) { SearchCriteria sc = this.StatusSearch.create(); @@ -310,7 +292,7 @@ public class SnapshotDaoImpl extends GenericDaoBase implements sc.setParameters("status", (Object[])status); return listBy(sc, null); } - + @Override @DB public boolean remove(Long id) { @@ -324,7 +306,7 @@ public class SnapshotDaoImpl extends GenericDaoBase implements txn.commit(); return result; } - + @Override public List listAllByStatus(Snapshot.State... status) { SearchCriteria sc = this.StatusSearch.create(); diff --git a/server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java b/server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java index 88bcc6ac19d..a7842fadbbf 100755 --- a/server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java +++ b/server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java @@ -31,6 +31,7 @@ import org.apache.cloudstack.api.command.user.snapshot.CreateSnapshotPolicyCmd; import org.apache.cloudstack.api.command.user.snapshot.DeleteSnapshotPoliciesCmd; import org.apache.cloudstack.api.command.user.snapshot.ListSnapshotPoliciesCmd; import org.apache.cloudstack.api.command.user.snapshot.ListSnapshotsCmd; +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; @@ -38,6 +39,8 @@ import org.apache.cloudstack.engine.subsystem.api.storage.SnapshotService; import org.apache.cloudstack.engine.subsystem.api.storage.VolumeDataFactory; import org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo; import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao; +import org.apache.cloudstack.storage.datastore.db.SnapshotDataStoreDao; +import org.apache.cloudstack.storage.datastore.db.SnapshotDataStoreVO; import org.apache.cloudstack.storage.datastore.db.StoragePoolVO; import org.apache.log4j.Logger; import org.springframework.stereotype.Component; @@ -79,6 +82,7 @@ import com.cloud.projects.Project.ListProjectResourcesCriteria; import com.cloud.server.ResourceTag.TaggedResourceType; import com.cloud.storage.Snapshot; import com.cloud.storage.Snapshot.Type; +import com.cloud.storage.DataStoreRole; import com.cloud.storage.SnapshotPolicyVO; import com.cloud.storage.SnapshotScheduleVO; import com.cloud.storage.SnapshotVO; @@ -146,6 +150,8 @@ public class SnapshotManagerImpl extends ManagerBase implements SnapshotManager, @Inject protected SnapshotDao _snapshotDao; @Inject + protected SnapshotDataStoreDao _snapshotStoreDao; + @Inject protected PrimaryDataStoreDao _storagePoolDao; @Inject protected EventDao _eventDao; @@ -523,24 +529,17 @@ public class SnapshotManagerImpl extends ManagerBase implements SnapshotManager, } } - private HostVO getSecondaryStorageHost(SnapshotVO snapshot) { - HostVO secHost = null; - if( snapshot.getSwiftId() == null || snapshot.getSwiftId() == 0) { - secHost = _hostDao.findById(snapshot.getSecHostId()); - } else { - Long dcId = snapshot.getDataCenterId(); - secHost = this.templateMgr.getSecondaryStorageHost(dcId); - } - return secHost; - } @Override public String getSecondaryStorageURL(SnapshotVO snapshot) { - HostVO secHost = getSecondaryStorageHost(snapshot); - if (secHost != null) { - return secHost.getStorageUrl(); + SnapshotDataStoreVO snapshotStore = this._snapshotStoreDao.findBySnapshot(snapshot.getId()); + if (snapshotStore != null){ + DataStore store = this.dataStoreMgr.getDataStore(snapshotStore.getDataStoreId(), DataStoreRole.Image); + if ( store != null ){ + return store.getUri(); + } } - throw new CloudRuntimeException("Can not find secondary storage"); + throw new CloudRuntimeException("Can not find secondary storage hosting the snapshot"); } @Override