Return-Path: X-Original-To: apmail-cloudstack-commits-archive@www.apache.org Delivered-To: apmail-cloudstack-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id EF570F101 for ; Thu, 30 May 2013 01:06:25 +0000 (UTC) Received: (qmail 5937 invoked by uid 500); 30 May 2013 01:06:22 -0000 Delivered-To: apmail-cloudstack-commits-archive@cloudstack.apache.org Received: (qmail 5885 invoked by uid 500); 30 May 2013 01:06:22 -0000 Mailing-List: contact commits-help@cloudstack.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cloudstack.apache.org Delivered-To: mailing list commits@cloudstack.apache.org Received: (qmail 5504 invoked by uid 99); 30 May 2013 01:06:22 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 30 May 2013 01:06:22 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 154D789D60C; Thu, 30 May 2013 01:06:22 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: edison@apache.org To: commits@cloudstack.apache.org Date: Thu, 30 May 2013 01:06:30 -0000 Message-Id: In-Reply-To: <68b2aa323fa94535a845f148bb8b6dc2@git.apache.org> References: <68b2aa323fa94535a845f148bb8b6dc2@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [10/17] clean up storage related coded http://git-wip-us.apache.org/repos/asf/cloudstack/blob/0a522892/engine/schema/src/com/cloud/storage/dao/VolumeDaoImpl.java ---------------------------------------------------------------------- diff --git a/engine/schema/src/com/cloud/storage/dao/VolumeDaoImpl.java b/engine/schema/src/com/cloud/storage/dao/VolumeDaoImpl.java index 12ca3c7..f82b511 100755 --- a/engine/schema/src/com/cloud/storage/dao/VolumeDaoImpl.java +++ b/engine/schema/src/com/cloud/storage/dao/VolumeDaoImpl.java @@ -51,7 +51,7 @@ import com.cloud.utils.db.UpdateBuilder; import com.cloud.utils.exception.CloudRuntimeException; @Component -@Local(value=VolumeDao.class) +@Local(value = VolumeDao.class) public class VolumeDaoImpl extends GenericDaoBase implements VolumeDao { private static final Logger s_logger = Logger.getLogger(VolumeDaoImpl.class); protected final SearchBuilder DetachedAccountIdSearch; @@ -63,23 +63,23 @@ public class VolumeDaoImpl extends GenericDaoBase implements Vol protected GenericSearchBuilder CountByAccount; protected GenericSearchBuilder primaryStorageSearch; protected GenericSearchBuilder secondaryStorageSearch; - @Inject ResourceTagDao _tagsDao; + @Inject + ResourceTagDao _tagsDao; protected static final String SELECT_VM_SQL = "SELECT DISTINCT instance_id from volumes v where v.host_id = ? and v.mirror_state = ?"; protected static final String SELECT_HYPERTYPE_FROM_VOLUME = "SELECT c.hypervisor_type from volumes v, storage_pool s, cluster c where v.pool_id = s.id and s.cluster_id = c.id and v.id = ?"; - private static final String ORDER_POOLS_NUMBER_OF_VOLUMES_FOR_ACCOUNT = "SELECT pool.id, SUM(IF(vol.state='Ready' AND vol.account_id = ?, 1, 0)) FROM `cloud`.`storage_pool` pool LEFT JOIN `cloud`.`volumes` vol ON pool.id = vol.pool_id WHERE pool.data_center_id = ? " + - " AND pool.pod_id = ? AND pool.cluster_id = ? " + - " GROUP BY pool.id ORDER BY 2 ASC "; - + private static final String ORDER_POOLS_NUMBER_OF_VOLUMES_FOR_ACCOUNT = "SELECT pool.id, SUM(IF(vol.state='Ready' AND vol.account_id = ?, 1, 0)) FROM `cloud`.`storage_pool` pool LEFT JOIN `cloud`.`volumes` vol ON pool.id = vol.pool_id WHERE pool.data_center_id = ? " + + " AND pool.pod_id = ? AND pool.cluster_id = ? " + " GROUP BY pool.id ORDER BY 2 ASC "; + @Override public List findDetachedByAccount(long accountId) { - SearchCriteria sc = DetachedAccountIdSearch.create(); - sc.setParameters("accountId", accountId); - sc.setParameters("destroyed", Volume.State.Destroy); - return listBy(sc); + SearchCriteria sc = DetachedAccountIdSearch.create(); + sc.setParameters("accountId", accountId); + sc.setParameters("destroyed", Volume.State.Destroy); + return listBy(sc); } - + @Override public List findByAccount(long accountId) { SearchCriteria sc = AllFieldsSearch.create(); @@ -87,172 +87,172 @@ public class VolumeDaoImpl extends GenericDaoBase implements Vol sc.setParameters("state", Volume.State.Ready); return listBy(sc); } - + @Override public List findByInstance(long id) { SearchCriteria sc = AllFieldsSearch.create(); sc.setParameters("instanceId", id); - return listBy(sc); - } - + return listBy(sc); + } + @Override - public List findByInstanceAndDeviceId(long instanceId, long deviceId){ - SearchCriteria sc = AllFieldsSearch.create(); - sc.setParameters("instanceId", instanceId); - sc.setParameters("deviceId", deviceId); - return listBy(sc); + public List findByInstanceAndDeviceId(long instanceId, long deviceId) { + SearchCriteria sc = AllFieldsSearch.create(); + sc.setParameters("instanceId", instanceId); + sc.setParameters("deviceId", deviceId); + return listBy(sc); } - + @Override public List findByPoolId(long poolId) { SearchCriteria sc = AllFieldsSearch.create(); sc.setParameters("poolId", poolId); sc.setParameters("notDestroyed", Volume.State.Destroy); sc.setParameters("vType", Volume.Type.ROOT.toString()); - return listBy(sc); - } - - @Override + return listBy(sc); + } + + @Override public List findCreatedByInstance(long id) { SearchCriteria sc = AllFieldsSearch.create(); sc.setParameters("instanceId", id); sc.setParameters("state", Volume.State.Ready); return listBy(sc); } - + @Override public List findUsableVolumesForInstance(long instanceId) { SearchCriteria sc = InstanceStatesSearch.create(); sc.setParameters("instance", instanceId); sc.setParameters("states", Volume.State.Creating, Volume.State.Ready, Volume.State.Allocated); - + return listBy(sc); } - - @Override - public List findByInstanceAndType(long id, Type vType) { + + @Override + public List findByInstanceAndType(long id, Type vType) { SearchCriteria sc = AllFieldsSearch.create(); sc.setParameters("instanceId", id); sc.setParameters("vType", vType.toString()); - return listBy(sc); - } - - @Override - public List findByInstanceIdDestroyed(long vmId) { - SearchCriteria sc = AllFieldsSearch.create(); - sc.setParameters("instanceId", vmId); - sc.setParameters("destroyed", Volume.State.Destroy); - return listBy(sc); - } - - @Override - public List findReadyRootVolumesByInstance(long instanceId) { - SearchCriteria sc = AllFieldsSearch.create(); - sc.setParameters("instanceId", instanceId); - sc.setParameters("state", Volume.State.Ready); - sc.setParameters("vType", Volume.Type.ROOT); - return listBy(sc); - } - - @Override - public List findByAccountAndPod(long accountId, long podId) { - SearchCriteria sc = AllFieldsSearch.create(); + return listBy(sc); + } + + @Override + public List findByInstanceIdDestroyed(long vmId) { + SearchCriteria sc = AllFieldsSearch.create(); + sc.setParameters("instanceId", vmId); + sc.setParameters("destroyed", Volume.State.Destroy); + return listBy(sc); + } + + @Override + public List findReadyRootVolumesByInstance(long instanceId) { + SearchCriteria sc = AllFieldsSearch.create(); + sc.setParameters("instanceId", instanceId); + sc.setParameters("state", Volume.State.Ready); + sc.setParameters("vType", Volume.Type.ROOT); + return listBy(sc); + } + + @Override + public List findByAccountAndPod(long accountId, long podId) { + SearchCriteria sc = AllFieldsSearch.create(); sc.setParameters("accountId", accountId); sc.setParameters("pod", podId); sc.setParameters("state", Volume.State.Ready); - + return listIncludingRemovedBy(sc); - } - - @Override - public List findByTemplateAndZone(long templateId, long zoneId) { - SearchCriteria sc = TemplateZoneSearch.create(); - sc.setParameters("template", templateId); - sc.setParameters("zone", zoneId); - - return listIncludingRemovedBy(sc); - } - - @Override - public boolean isAnyVolumeActivelyUsingTemplateOnPool(long templateId, long poolId) { - SearchCriteria sc = ActiveTemplateSearch.create(); - sc.setParameters("template", templateId); - sc.setParameters("pool", poolId); - - List results = customSearchIncludingRemoved(sc, null); - assert results.size() > 0 : "How can this return a size of " + results.size(); - - return results.get(0) > 0; - } - + } + + @Override + public List findByTemplateAndZone(long templateId, long zoneId) { + SearchCriteria sc = TemplateZoneSearch.create(); + sc.setParameters("template", templateId); + sc.setParameters("zone", zoneId); + + return listIncludingRemovedBy(sc); + } + + @Override + public boolean isAnyVolumeActivelyUsingTemplateOnPool(long templateId, long poolId) { + SearchCriteria sc = ActiveTemplateSearch.create(); + sc.setParameters("template", templateId); + sc.setParameters("pool", poolId); + + List results = customSearchIncludingRemoved(sc, null); + assert results.size() > 0 : "How can this return a size of " + results.size(); + + return results.get(0) > 0; + } + @Override public void deleteVolumesByInstance(long instanceId) { SearchCriteria sc = AllFieldsSearch.create(); sc.setParameters("instanceId", instanceId); expunge(sc); } - + @Override public void attachVolume(long volumeId, long vmId, long deviceId) { - VolumeVO volume = createForUpdate(volumeId); - volume.setInstanceId(vmId); - volume.setDeviceId(deviceId); - volume.setUpdated(new Date()); - volume.setAttached(new Date()); - update(volumeId, volume); + VolumeVO volume = createForUpdate(volumeId); + volume.setInstanceId(vmId); + volume.setDeviceId(deviceId); + volume.setUpdated(new Date()); + volume.setAttached(new Date()); + update(volumeId, volume); } - + @Override public void detachVolume(long volumeId) { - VolumeVO volume = createForUpdate(volumeId); - volume.setInstanceId(null); + VolumeVO volume = createForUpdate(volumeId); + volume.setInstanceId(null); volume.setDeviceId(null); - volume.setUpdated(new Date()); - volume.setAttached(null); - update(volumeId, volume); + volume.setUpdated(new Date()); + volume.setAttached(null); + update(volumeId, volume); } - + @Override @DB - public HypervisorType getHypervisorType(long volumeId) { - /*lookup from cluster of pool*/ - Transaction txn = Transaction.currentTxn(); - PreparedStatement pstmt = null; - - try { - String sql = SELECT_HYPERTYPE_FROM_VOLUME; - pstmt = txn.prepareAutoCloseStatement(sql); - pstmt.setLong(1, volumeId); - ResultSet rs = pstmt.executeQuery(); - if (rs.next()) { + public HypervisorType getHypervisorType(long volumeId) { + /* lookup from cluster of pool */ + Transaction txn = Transaction.currentTxn(); + PreparedStatement pstmt = null; + + try { + String sql = SELECT_HYPERTYPE_FROM_VOLUME; + pstmt = txn.prepareAutoCloseStatement(sql); + pstmt.setLong(1, volumeId); + ResultSet rs = pstmt.executeQuery(); + if (rs.next()) { return HypervisorType.getType(rs.getString(1)); } - return HypervisorType.None; - } catch (SQLException e) { - throw new CloudRuntimeException("DB Exception on: " + SELECT_HYPERTYPE_FROM_VOLUME, e); - } catch (Throwable e) { - throw new CloudRuntimeException("Caught: " + SELECT_HYPERTYPE_FROM_VOLUME, e); - } - } - + return HypervisorType.None; + } catch (SQLException e) { + throw new CloudRuntimeException("DB Exception on: " + SELECT_HYPERTYPE_FROM_VOLUME, e); + } catch (Throwable e) { + throw new CloudRuntimeException("Caught: " + SELECT_HYPERTYPE_FROM_VOLUME, e); + } + } + @Override public ImageFormat getImageFormat(Long volumeId) { HypervisorType type = getHypervisorType(volumeId); - if ( type.equals(HypervisorType.KVM)) { + if (type.equals(HypervisorType.KVM)) { return ImageFormat.QCOW2; - } else if ( type.equals(HypervisorType.XenServer)) { + } else if (type.equals(HypervisorType.XenServer)) { return ImageFormat.VHD; - } else if ( type.equals(HypervisorType.VMware)) { + } else if (type.equals(HypervisorType.VMware)) { return ImageFormat.OVA; } else { s_logger.warn("Do not support hypervisor " + type.toString()); return null; } } - - public VolumeDaoImpl() { - AllFieldsSearch = createSearchBuilder(); - AllFieldsSearch.and("state", AllFieldsSearch.entity().getState(), Op.EQ); + + public VolumeDaoImpl() { + AllFieldsSearch = createSearchBuilder(); + AllFieldsSearch.and("state", AllFieldsSearch.entity().getState(), Op.EQ); AllFieldsSearch.and("accountId", AllFieldsSearch.entity().getAccountId(), Op.EQ); AllFieldsSearch.and("pod", AllFieldsSearch.entity().getPodId(), Op.EQ); AllFieldsSearch.and("instanceId", AllFieldsSearch.entity().getInstanceId(), Op.EQ); @@ -264,33 +264,33 @@ public class VolumeDaoImpl extends GenericDaoBase implements Vol AllFieldsSearch.and("notDestroyed", AllFieldsSearch.entity().getState(), Op.NEQ); AllFieldsSearch.and("updatedCount", AllFieldsSearch.entity().getUpdatedCount(), Op.EQ); AllFieldsSearch.done(); - + DetachedAccountIdSearch = createSearchBuilder(); DetachedAccountIdSearch.and("accountId", DetachedAccountIdSearch.entity().getAccountId(), Op.EQ); DetachedAccountIdSearch.and("destroyed", DetachedAccountIdSearch.entity().getState(), Op.NEQ); DetachedAccountIdSearch.and("instanceId", DetachedAccountIdSearch.entity().getInstanceId(), Op.NULL); DetachedAccountIdSearch.done(); - + TemplateZoneSearch = createSearchBuilder(); TemplateZoneSearch.and("template", TemplateZoneSearch.entity().getTemplateId(), Op.EQ); TemplateZoneSearch.and("zone", TemplateZoneSearch.entity().getDataCenterId(), Op.EQ); TemplateZoneSearch.done(); - + TotalSizeByPoolSearch = createSearchBuilder(SumCount.class); TotalSizeByPoolSearch.select("sum", Func.SUM, TotalSizeByPoolSearch.entity().getSize()); - TotalSizeByPoolSearch.select("count", Func.COUNT, (Object[])null); + TotalSizeByPoolSearch.select("count", Func.COUNT, (Object[]) null); TotalSizeByPoolSearch.and("poolId", TotalSizeByPoolSearch.entity().getPoolId(), Op.EQ); TotalSizeByPoolSearch.and("removed", TotalSizeByPoolSearch.entity().getRemoved(), Op.NULL); TotalSizeByPoolSearch.and("state", TotalSizeByPoolSearch.entity().getState(), Op.NEQ); TotalSizeByPoolSearch.done(); - + ActiveTemplateSearch = createSearchBuilder(Long.class); ActiveTemplateSearch.and("pool", ActiveTemplateSearch.entity().getPoolId(), Op.EQ); ActiveTemplateSearch.and("template", ActiveTemplateSearch.entity().getTemplateId(), Op.EQ); ActiveTemplateSearch.and("removed", ActiveTemplateSearch.entity().getRemoved(), Op.NULL); ActiveTemplateSearch.select(null, Func.COUNT, null); ActiveTemplateSearch.done(); - + InstanceStatesSearch = createSearchBuilder(); InstanceStatesSearch.and("instance", InstanceStatesSearch.entity().getInstanceId(), Op.EQ); InstanceStatesSearch.and("states", InstanceStatesSearch.entity().getState(), Op.IN); @@ -315,24 +315,25 @@ public class VolumeDaoImpl extends GenericDaoBase implements Vol secondaryStorageSearch.and("path", secondaryStorageSearch.entity().getPath(), Op.NULL); secondaryStorageSearch.and("isRemoved", secondaryStorageSearch.entity().getRemoved(), Op.NULL); secondaryStorageSearch.done(); - } + } - @Override @DB(txn=false) - public Pair getCountAndTotalByPool(long poolId) { + @Override + @DB(txn = false) + public Pair getCountAndTotalByPool(long poolId) { SearchCriteria sc = TotalSizeByPoolSearch.create(); sc.setParameters("poolId", poolId); List results = customSearch(sc, null); SumCount sumCount = results.get(0); return new Pair(sumCount.count, sumCount.sum); - } + } @Override - public Long countAllocatedVolumesForAccount(long accountId) { - SearchCriteria sc = CountByAccount.create(); + public Long countAllocatedVolumesForAccount(long accountId) { + SearchCriteria sc = CountByAccount.create(); sc.setParameters("account", accountId); - sc.setParameters("state", Volume.State.Destroy); + sc.setParameters("state", Volume.State.Destroy); return customSearch(sc, null).get(0); - } + } @Override public long primaryStorageUsedForAccount(long accountId) { @@ -358,55 +359,61 @@ public class VolumeDaoImpl extends GenericDaoBase implements Vol } } + public static class SumCount { + public long sum; + public long count; - public static class SumCount { - public long sum; - public long count; - public SumCount() { - } - } + public SumCount() { + } + } @Override public List listVolumesToBeDestroyed() { SearchCriteria sc = AllFieldsSearch.create(); sc.setParameters("state", Volume.State.Destroy); - + return listBy(sc); } - @Override - public boolean updateState(com.cloud.storage.Volume.State currentState, - Event event, com.cloud.storage.Volume.State nextState, Volume vo, - Object data) { - - Long oldUpdated = vo.getUpdatedCount(); - Date oldUpdatedTime = vo.getUpdated(); - - SearchCriteria sc = AllFieldsSearch.create(); - sc.setParameters("id", vo.getId()); - sc.setParameters("state", currentState); - sc.setParameters("updatedCount", vo.getUpdatedCount()); - - vo.incrUpdatedCount(); - - UpdateBuilder builder = getUpdateBuilder(vo); - builder.set(vo, "state", nextState); - builder.set(vo, "updated", new Date()); - - int rows = update((VolumeVO)vo, sc); - if (rows == 0 && s_logger.isDebugEnabled()) { - VolumeVO dbVol = findByIdIncludingRemoved(vo.getId()); - if (dbVol != null) { - StringBuilder str = new StringBuilder("Unable to update ").append(vo.toString()); - str.append(": DB Data={id=").append(dbVol.getId()).append("; state=").append(dbVol.getState()).append("; updatecount=").append(dbVol.getUpdatedCount()).append(";updatedTime=").append(dbVol.getUpdated()); - str.append(": New Data={id=").append(vo.getId()).append("; state=").append(nextState).append("; event=").append(event).append("; updatecount=").append(vo.getUpdatedCount()).append("; updatedTime=").append(vo.getUpdated()); - str.append(": stale Data={id=").append(vo.getId()).append("; state=").append(currentState).append("; event=").append(event).append("; updatecount=").append(oldUpdated).append("; updatedTime=").append(oldUpdatedTime); - } else { - s_logger.debug("Unable to update volume: id=" + vo.getId() + ", as there is no such volume exists in the database anymore"); - } - } - return rows > 0; - } + @Override + public boolean updateState(com.cloud.storage.Volume.State currentState, Event event, + com.cloud.storage.Volume.State nextState, Volume vo, Object data) { + + Long oldUpdated = vo.getUpdatedCount(); + Date oldUpdatedTime = vo.getUpdated(); + + SearchCriteria sc = AllFieldsSearch.create(); + sc.setParameters("id", vo.getId()); + sc.setParameters("state", currentState); + sc.setParameters("updatedCount", vo.getUpdatedCount()); + + vo.incrUpdatedCount(); + + UpdateBuilder builder = getUpdateBuilder(vo); + builder.set(vo, "state", nextState); + builder.set(vo, "updated", new Date()); + + int rows = update((VolumeVO) vo, sc); + if (rows == 0 && s_logger.isDebugEnabled()) { + VolumeVO dbVol = findByIdIncludingRemoved(vo.getId()); + if (dbVol != null) { + StringBuilder str = new StringBuilder("Unable to update ").append(vo.toString()); + str.append(": DB Data={id=").append(dbVol.getId()).append("; state=").append(dbVol.getState()) + .append("; updatecount=").append(dbVol.getUpdatedCount()).append(";updatedTime=") + .append(dbVol.getUpdated()); + str.append(": New Data={id=").append(vo.getId()).append("; state=").append(nextState) + .append("; event=").append(event).append("; updatecount=").append(vo.getUpdatedCount()) + .append("; updatedTime=").append(vo.getUpdated()); + str.append(": stale Data={id=").append(vo.getId()).append("; state=").append(currentState) + .append("; event=").append(event).append("; updatecount=").append(oldUpdated) + .append("; updatedTime=").append(oldUpdatedTime); + } else { + s_logger.debug("Unable to update volume: id=" + vo.getId() + + ", as there is no such volume exists in the database anymore"); + } + } + return rows > 0; + } @Override public List listPoolIdsByVolumeCount(long dcId, Long podId, Long clusterId, long accountId) { @@ -420,7 +427,7 @@ public class VolumeDaoImpl extends GenericDaoBase implements Vol pstmt.setLong(2, dcId); pstmt.setLong(3, podId); pstmt.setLong(4, clusterId); - + ResultSet rs = pstmt.executeQuery(); while (rs.next()) { result.add(rs.getLong(1)); @@ -432,8 +439,9 @@ public class VolumeDaoImpl extends GenericDaoBase implements Vol throw new CloudRuntimeException("Caught: " + ORDER_POOLS_NUMBER_OF_VOLUMES_FOR_ACCOUNT, e); } } - - @Override @DB(txn=false) + + @Override + @DB(txn = false) public Pair getNonDestroyedCountAndTotalByPool(long poolId) { SearchCriteria sc = TotalSizeByPoolSearch.create(); sc.setParameters("poolId", poolId); @@ -442,7 +450,7 @@ public class VolumeDaoImpl extends GenericDaoBase implements Vol SumCount sumCount = results.get(0); return new Pair(sumCount.count, sumCount.sum); } - + @Override @DB public boolean remove(Long id) { http://git-wip-us.apache.org/repos/asf/cloudstack/blob/0a522892/engine/schema/src/com/cloud/storage/dao/VolumeHostDao.java ---------------------------------------------------------------------- diff --git a/engine/schema/src/com/cloud/storage/dao/VolumeHostDao.java b/engine/schema/src/com/cloud/storage/dao/VolumeHostDao.java index 39dda12..ccb276b 100755 --- a/engine/schema/src/com/cloud/storage/dao/VolumeHostDao.java +++ b/engine/schema/src/com/cloud/storage/dao/VolumeHostDao.java @@ -25,16 +25,17 @@ import com.cloud.storage.VolumeHostVO; import com.cloud.utils.db.GenericDao; import com.cloud.utils.fsm.StateDao; -public interface VolumeHostDao extends GenericDao, StateDao{ +public interface VolumeHostDao extends GenericDao, + StateDao { - VolumeHostVO findByHostVolume(long hostId, long volumeId); + VolumeHostVO findByHostVolume(long hostId, long volumeId); - VolumeHostVO findByVolumeId(long volumeId); + VolumeHostVO findByVolumeId(long volumeId); - List listBySecStorage(long sserverId); + List listBySecStorage(long sserverId); - List listDestroyed(long hostId); + List listDestroyed(long hostId); - VolumeHostVO findVolumeByZone(long zoneId, long volumeId); + VolumeHostVO findVolumeByZone(long zoneId, long volumeId); } http://git-wip-us.apache.org/repos/asf/cloudstack/blob/0a522892/engine/schema/src/com/cloud/storage/dao/VolumeHostDaoImpl.java ---------------------------------------------------------------------- diff --git a/engine/schema/src/com/cloud/storage/dao/VolumeHostDaoImpl.java b/engine/schema/src/com/cloud/storage/dao/VolumeHostDaoImpl.java index 2fd39e6..b731ebb 100755 --- a/engine/schema/src/com/cloud/storage/dao/VolumeHostDaoImpl.java +++ b/engine/schema/src/com/cloud/storage/dao/VolumeHostDaoImpl.java @@ -35,7 +35,7 @@ import com.cloud.utils.db.SearchCriteria.Op; import com.cloud.utils.db.UpdateBuilder; @Component -@Local(value={VolumeHostDao.class}) +@Local(value = { VolumeHostDao.class }) public class VolumeHostDaoImpl extends GenericDaoBase implements VolumeHostDao { private static final Logger s_logger = Logger.getLogger(VolumeHostDaoImpl.class); protected final SearchBuilder HostVolumeSearch; @@ -44,7 +44,8 @@ public class VolumeHostDaoImpl extends GenericDaoBase implem protected final SearchBuilder HostSearch; protected final SearchBuilder HostDestroyedSearch; protected final SearchBuilder updateStateSearch; - public VolumeHostDaoImpl(){ + + public VolumeHostDaoImpl() { HostVolumeSearch = createSearchBuilder(); HostVolumeSearch.and("host_id", HostVolumeSearch.entity().getHostId(), SearchCriteria.Op.EQ); HostVolumeSearch.and("volume_id", HostVolumeSearch.entity().getVolumeId(), SearchCriteria.Op.EQ); @@ -58,7 +59,7 @@ public class VolumeHostDaoImpl extends GenericDaoBase implem ZoneVolumeSearch.done(); HostSearch = createSearchBuilder(); - HostSearch.and("host_id", HostSearch.entity().getHostId(), SearchCriteria.Op.EQ); + HostSearch.and("host_id", HostSearch.entity().getHostId(), SearchCriteria.Op.EQ); HostSearch.and("destroyed", HostSearch.entity().getDestroyed(), SearchCriteria.Op.EQ); HostSearch.done(); @@ -70,8 +71,8 @@ public class VolumeHostDaoImpl extends GenericDaoBase implem HostDestroyedSearch = createSearchBuilder(); HostDestroyedSearch.and("host_id", HostDestroyedSearch.entity().getHostId(), SearchCriteria.Op.EQ); HostDestroyedSearch.and("destroyed", HostDestroyedSearch.entity().getDestroyed(), SearchCriteria.Op.EQ); - HostDestroyedSearch.done(); - + HostDestroyedSearch.done(); + updateStateSearch = this.createSearchBuilder(); updateStateSearch.and("id", updateStateSearch.entity().getId(), Op.EQ); updateStateSearch.and("state", updateStateSearch.entity().getState(), Op.EQ); @@ -79,8 +80,6 @@ public class VolumeHostDaoImpl extends GenericDaoBase implem updateStateSearch.done(); } - - @Override public VolumeHostVO findByHostVolume(long hostId, long volumeId) { SearchCriteria sc = HostVolumeSearch.create(); @@ -88,7 +87,7 @@ public class VolumeHostDaoImpl extends GenericDaoBase implem sc.setParameters("volume_id", volumeId); sc.setParameters("destroyed", false); return findOneIncludingRemovedBy(sc); - } + } @Override public VolumeHostVO findVolumeByZone(long volumeId, long zoneId) { @@ -107,8 +106,6 @@ public class VolumeHostDaoImpl extends GenericDaoBase implem return findOneBy(sc); } - - @Override public List listBySecStorage(long ssHostId) { SearchCriteria sc = HostSearch.create(); @@ -118,7 +115,7 @@ public class VolumeHostDaoImpl extends GenericDaoBase implem } @Override - public List listDestroyed(long hostId){ + public List listDestroyed(long hostId) { SearchCriteria sc = HostDestroyedSearch.create(); sc.setParameters("host_id", hostId); sc.setParameters("destroyed", true); @@ -126,13 +123,11 @@ public class VolumeHostDaoImpl extends GenericDaoBase implem } @Override - public boolean updateState(State currentState, Event event, - State nextState, DataObjectInStore vo, Object data) { + public boolean updateState(State currentState, Event event, State nextState, DataObjectInStore vo, Object data) { VolumeHostVO volHost = (VolumeHostVO) vo; Long oldUpdated = volHost.getUpdatedCount(); Date oldUpdatedTime = volHost.getUpdated(); - - + SearchCriteria sc = updateStateSearch.create(); sc.setParameters("id", volHost.getId()); sc.setParameters("state", currentState); @@ -149,14 +144,18 @@ public class VolumeHostDaoImpl extends GenericDaoBase implem VolumeHostVO dbVol = findByIdIncludingRemoved(volHost.getId()); if (dbVol != null) { StringBuilder str = new StringBuilder("Unable to update ").append(vo.toString()); - str.append(": DB Data={id=").append(dbVol.getId()).append("; state=").append(dbVol.getState()).append("; updatecount=").append(dbVol.getUpdatedCount()).append(";updatedTime=") + str.append(": DB Data={id=").append(dbVol.getId()).append("; state=").append(dbVol.getState()) + .append("; updatecount=").append(dbVol.getUpdatedCount()).append(";updatedTime=") .append(dbVol.getUpdated()); - str.append(": New Data={id=").append(volHost.getId()).append("; state=").append(nextState).append("; event=").append(event).append("; updatecount=").append(volHost.getUpdatedCount()) + str.append(": New Data={id=").append(volHost.getId()).append("; state=").append(nextState) + .append("; event=").append(event).append("; updatecount=").append(volHost.getUpdatedCount()) .append("; updatedTime=").append(volHost.getUpdated()); - str.append(": stale Data={id=").append(volHost.getId()).append("; state=").append(currentState).append("; event=").append(event).append("; updatecount=").append(oldUpdated) + str.append(": stale Data={id=").append(volHost.getId()).append("; state=").append(currentState) + .append("; event=").append(event).append("; updatecount=").append(oldUpdated) .append("; updatedTime=").append(oldUpdatedTime); } else { - s_logger.debug("Unable to update objectIndatastore: id=" + volHost.getId() + ", as there is no such object exists in the database anymore"); + s_logger.debug("Unable to update objectIndatastore: id=" + volHost.getId() + + ", as there is no such object exists in the database anymore"); } } return rows > 0; http://git-wip-us.apache.org/repos/asf/cloudstack/blob/0a522892/engine/storage/cache/src/org/apache/cloudstack/storage/cache/allocator/StorageCacheRandomAllocator.java ---------------------------------------------------------------------- diff --git a/engine/storage/cache/src/org/apache/cloudstack/storage/cache/allocator/StorageCacheRandomAllocator.java b/engine/storage/cache/src/org/apache/cloudstack/storage/cache/allocator/StorageCacheRandomAllocator.java index f8232b8..f244a03 100644 --- a/engine/storage/cache/src/org/apache/cloudstack/storage/cache/allocator/StorageCacheRandomAllocator.java +++ b/engine/storage/cache/src/org/apache/cloudstack/storage/cache/allocator/StorageCacheRandomAllocator.java @@ -18,6 +18,7 @@ */ package org.apache.cloudstack.storage.cache.allocator; +import java.util.Collections; import java.util.List; import javax.inject.Inject; @@ -30,27 +31,25 @@ import org.springframework.stereotype.Component; import com.cloud.storage.ScopeType; -import edu.emory.mathcs.backport.java.util.Collections; - @Component public class StorageCacheRandomAllocator implements StorageCacheAllocator { - private static final Logger s_logger = Logger - .getLogger(StorageCacheRandomAllocator.class); + private static final Logger s_logger = Logger.getLogger(StorageCacheRandomAllocator.class); @Inject DataStoreManager dataStoreMgr; + @Override public DataStore getCacheStore(Scope scope) { if (scope.getScopeType() != ScopeType.ZONE) { s_logger.debug("Can only support zone wide cache storage"); return null; } - + List cacheStores = dataStoreMgr.getImageCacheStores(scope); if (cacheStores.size() <= 0) { s_logger.debug("Can't find cache storage in zone: " + scope.getScopeId()); return null; } - + Collections.shuffle(cacheStores); return cacheStores.get(0); } http://git-wip-us.apache.org/repos/asf/cloudstack/blob/0a522892/engine/storage/cache/src/org/apache/cloudstack/storage/cache/manager/StorageCacheManagerImpl.java ---------------------------------------------------------------------- diff --git a/engine/storage/cache/src/org/apache/cloudstack/storage/cache/manager/StorageCacheManagerImpl.java b/engine/storage/cache/src/org/apache/cloudstack/storage/cache/manager/StorageCacheManagerImpl.java index d2895fd..cb5ea10 100644 --- a/engine/storage/cache/src/org/apache/cloudstack/storage/cache/manager/StorageCacheManagerImpl.java +++ b/engine/storage/cache/src/org/apache/cloudstack/storage/cache/manager/StorageCacheManagerImpl.java @@ -39,16 +39,14 @@ import org.apache.cloudstack.framework.async.AsyncCallbackDispatcher; import org.apache.cloudstack.framework.async.AsyncCompletionCallback; import org.apache.cloudstack.framework.async.AsyncRpcConext; import org.apache.cloudstack.storage.cache.allocator.StorageCacheAllocator; -import org.apache.cloudstack.storage.command.CommandResult; -import org.apache.cloudstack.storage.command.CopyCmdAnswer; import org.apache.cloudstack.storage.datastore.ObjectInDataStoreManager; import org.apache.log4j.Logger; import com.cloud.utils.component.Manager; +import com.cloud.utils.exception.CloudRuntimeException; public class StorageCacheManagerImpl implements StorageCacheManager, Manager { - private static final Logger s_logger = Logger - .getLogger(StorageCacheManagerImpl.class); + private static final Logger s_logger = Logger.getLogger(StorageCacheManagerImpl.class); @Inject List storageCacheAllocator; @Inject @@ -120,10 +118,9 @@ public class StorageCacheManagerImpl implements StorageCacheManager, Manager { return true; } - - private class CreateCacheObjectContext extends AsyncRpcConext { final AsyncCallFuture future; + /** * @param callback */ @@ -134,48 +131,47 @@ public class StorageCacheManagerImpl implements StorageCacheManager, Manager { } - @Override - public DataObject createCacheObject(DataObject data, Scope scope) { - DataStore cacheStore = this.getCacheStorage(scope); - DataObjectInStore obj = objectInStoreMgr.findObject(data, cacheStore); - if (obj != null && obj.getState() == ObjectInDataStoreStateMachine.State.Ready) { - s_logger.debug("there is already one in the cache store"); - return objectInStoreMgr.get(data, cacheStore); - } - - //TODO: consider multiple thread to create - DataObject objOnCacheStore = cacheStore.create(data); - - AsyncCallFuture future = new AsyncCallFuture(); - CopyCommandResult result = null; - try { - objOnCacheStore.processEvent(Event.CreateOnlyRequested); - - dataMotionSvr.copyAsync(data, objOnCacheStore, future); - result = future.get(); - - if (result.isFailed()) { - objOnCacheStore.processEvent(Event.OperationFailed); - } else { - objOnCacheStore.processEvent(Event.OperationSuccessed, result.getAnswer()); - return objOnCacheStore; - } + @Override + public DataObject createCacheObject(DataObject data, Scope scope) { + DataStore cacheStore = this.getCacheStorage(scope); + DataObjectInStore obj = objectInStoreMgr.findObject(data, cacheStore); + if (obj != null && obj.getState() == ObjectInDataStoreStateMachine.State.Ready) { + s_logger.debug("there is already one in the cache store"); + return objectInStoreMgr.get(data, cacheStore); + } + + DataObject objOnCacheStore = cacheStore.create(data); + + AsyncCallFuture future = new AsyncCallFuture(); + CopyCommandResult result = null; + try { + objOnCacheStore.processEvent(Event.CreateOnlyRequested); + + dataMotionSvr.copyAsync(data, objOnCacheStore, future); + result = future.get(); + + if (result.isFailed()) { + objOnCacheStore.processEvent(Event.OperationFailed); + } else { + objOnCacheStore.processEvent(Event.OperationSuccessed, result.getAnswer()); + return objOnCacheStore; + } } catch (InterruptedException e) { s_logger.debug("create cache storage failed: " + e.toString()); + throw new CloudRuntimeException(e); } catch (ExecutionException e) { s_logger.debug("create cache storage failed: " + e.toString()); - } catch (Exception e) { - s_logger.debug("create cache storage failed: " + e.toString()); + throw new CloudRuntimeException(e); } finally { if (result == null) { objOnCacheStore.processEvent(Event.OperationFailed); } } - return null; - } + return null; + } - @Override + @Override public DataObject getCacheObject(DataObject data, Scope scope) { DataStore cacheStore = this.getCacheStorage(scope); DataObject objOnCacheStore = cacheStore.create(data); @@ -183,12 +179,13 @@ public class StorageCacheManagerImpl implements StorageCacheManager, Manager { return objOnCacheStore; } - protected Void createCacheObjectCallBack(AsyncCallbackDispatcher callback, - CreateCacheObjectContext context) { - AsyncCallFuture future = context.future; - future.complete(callback.getResult()); - return null; - } + protected Void createCacheObjectCallBack( + AsyncCallbackDispatcher callback, + CreateCacheObjectContext context) { + AsyncCallFuture future = context.future; + future.complete(callback.getResult()); + return null; + } @Override public boolean deleteCacheObject(DataObject data) { http://git-wip-us.apache.org/repos/asf/cloudstack/blob/0a522892/engine/storage/datamotion/src/org/apache/cloudstack/storage/motion/AncientDataMotionStrategy.java ---------------------------------------------------------------------- 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 65beb40..a01d2d3 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 @@ -23,30 +23,29 @@ import java.util.Map; import javax.inject.Inject; import org.apache.cloudstack.engine.subsystem.api.storage.ClusterScope; -import org.apache.log4j.Logger; -import org.springframework.stereotype.Component; import org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult; import org.apache.cloudstack.engine.subsystem.api.storage.DataMotionStrategy; import org.apache.cloudstack.engine.subsystem.api.storage.DataObject; 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.EndPoint; +import org.apache.cloudstack.engine.subsystem.api.storage.EndPointSelector; import org.apache.cloudstack.engine.subsystem.api.storage.HostScope; import org.apache.cloudstack.engine.subsystem.api.storage.ObjectInDataStoreStateMachine.Event; import org.apache.cloudstack.engine.subsystem.api.storage.Scope; -import org.apache.cloudstack.engine.subsystem.api.storage.ZoneScope; -import org.apache.cloudstack.engine.subsystem.api.storage.EndPoint; -import org.apache.cloudstack.engine.subsystem.api.storage.EndPointSelector; import org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo; import org.apache.cloudstack.engine.subsystem.api.storage.StorageCacheManager; import org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo; +import org.apache.cloudstack.engine.subsystem.api.storage.ZoneScope; import org.apache.cloudstack.framework.async.AsyncCompletionCallback; -import org.apache.cloudstack.storage.command.CopyCmdAnswer; 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.TemplateDataStoreDao; import org.apache.cloudstack.storage.datastore.db.VolumeDataStoreDao; import org.apache.cloudstack.storage.image.datastore.ImageStoreEntity; +import org.apache.log4j.Logger; +import org.springframework.stereotype.Component; import com.cloud.agent.api.Answer; import com.cloud.agent.api.to.DataObjectType; @@ -56,11 +55,9 @@ import com.cloud.agent.api.to.NfsTO; import com.cloud.agent.api.to.VirtualMachineTO; import com.cloud.configuration.Config; import com.cloud.configuration.dao.ConfigurationDao; -import com.cloud.exception.StorageUnavailableException; import com.cloud.host.Host; import com.cloud.host.dao.HostDao; import com.cloud.storage.DataStoreRole; -import com.cloud.storage.ImageStore; import com.cloud.storage.StorageManager; import com.cloud.storage.StoragePool; import com.cloud.storage.VolumeManager; @@ -77,8 +74,7 @@ import com.cloud.utils.exception.CloudRuntimeException; @Component public class AncientDataMotionStrategy implements DataMotionStrategy { - private static final Logger s_logger = Logger - .getLogger(AncientDataMotionStrategy.class); + private static final Logger s_logger = Logger.getLogger(AncientDataMotionStrategy.class); @Inject EndPointSelector selector; @Inject @@ -107,8 +103,10 @@ public class AncientDataMotionStrategy implements DataMotionStrategy { DataStoreManager dataStoreMgr; @Inject TemplateDataStoreDao templateStoreDao; - @Inject DiskOfferingDao diskOfferingDao; - @Inject VMTemplatePoolDao templatePoolDao; + @Inject + DiskOfferingDao diskOfferingDao; + @Inject + VMTemplatePoolDao templatePoolDao; @Inject VolumeManager volumeMgr; @Inject @@ -125,7 +123,6 @@ public class AncientDataMotionStrategy implements DataMotionStrategy { return false; } - protected boolean needCacheStorage(DataObject srcData, DataObject destData) { DataTO srcTO = srcData.getTO(); DataTO destTO = destData.getTO(); @@ -140,9 +137,9 @@ public class AncientDataMotionStrategy implements DataMotionStrategy { } return true; } - + private Scope getZoneScope(Scope destScope) { - ZoneScope zoneScope = null; + ZoneScope zoneScope = null; if (destScope instanceof ClusterScope) { ClusterScope clusterScope = (ClusterScope) destScope; zoneScope = new ZoneScope(clusterScope.getZoneId()); @@ -150,14 +147,15 @@ public class AncientDataMotionStrategy implements DataMotionStrategy { HostScope hostScope = (HostScope) destScope; zoneScope = new ZoneScope(hostScope.getZoneId()); } else { - zoneScope = (ZoneScope)destScope; + zoneScope = (ZoneScope) destScope; } return zoneScope; } protected Answer copyObject(DataObject srcData, DataObject destData) { String value = configDao.getValue(Config.PrimaryStorageDownloadWait.toString()); - int _primaryStorageDownloadWait = NumbersUtil.parseInt(value, Integer.parseInt(Config.PrimaryStorageDownloadWait.getDefaultValue())); + int _primaryStorageDownloadWait = NumbersUtil.parseInt(value, + Integer.parseInt(Config.PrimaryStorageDownloadWait.getDefaultValue())); Answer answer = null; DataObject cacheData = null; try { @@ -193,7 +191,7 @@ public class AncientDataMotionStrategy implements DataMotionStrategy { protected DataObject cacheSnapshotChain(SnapshotInfo snapshot) { DataObject leafData = null; - while(snapshot != null) { + while (snapshot != null) { DataObject cacheData = cacheMgr.createCacheObject(snapshot, snapshot.getDataStore().getScope()); if (leafData == null) { leafData = cacheData; @@ -208,11 +206,10 @@ public class AncientDataMotionStrategy implements DataMotionStrategy { } protected Answer copyVolumeFromSnapshot(DataObject snapObj, DataObject volObj) { - SnapshotInfo snapshot = (SnapshotInfo)snapObj; + SnapshotInfo snapshot = (SnapshotInfo) snapObj; StoragePool pool = (StoragePool) volObj.getDataStore(); - String basicErrMsg = "Failed to create volume from " - + snapshot.getName() + " on pool " + pool; + String basicErrMsg = "Failed to create volume from " + snapshot.getName() + " on pool " + pool; DataStore store = snapObj.getDataStore(); DataStoreTO storTO = store.getTO(); DataObject srcData = snapObj; @@ -221,23 +218,21 @@ public class AncientDataMotionStrategy implements DataMotionStrategy { srcData = cacheSnapshotChain(snapshot); } - String value = configDao - .getValue(Config.CreateVolumeFromSnapshotWait.toString()); + String value = configDao.getValue(Config.CreateVolumeFromSnapshotWait.toString()); int _createVolumeFromSnapshotWait = NumbersUtil.parseInt(value, - Integer.parseInt(Config.CreateVolumeFromSnapshotWait - .getDefaultValue())); + Integer.parseInt(Config.CreateVolumeFromSnapshotWait.getDefaultValue())); CopyCommand cmd = new CopyCommand(srcData.getTO(), volObj.getTO(), _createVolumeFromSnapshotWait); EndPoint ep = selector.select(snapObj, volObj); Answer answer = ep.sendMessage(cmd); - return answer; + return answer; } catch (Exception e) { s_logger.error(basicErrMsg, e); throw new CloudRuntimeException(basicErrMsg); } finally { if (!(storTO instanceof NfsTO)) { - deleteSnapshotCacheChain((SnapshotInfo)srcData); + deleteSnapshotCacheChain((SnapshotInfo) srcData); } } } @@ -256,86 +251,82 @@ public class AncientDataMotionStrategy implements DataMotionStrategy { protected Answer copyVolumeBetweenPools(DataObject srcData, DataObject destData) { String value = configDao.getValue(Config.CopyVolumeWait.key()); - int _copyvolumewait = NumbersUtil.parseInt(value, - Integer.parseInt(Config.CopyVolumeWait.getDefaultValue())); + int _copyvolumewait = NumbersUtil.parseInt(value, Integer.parseInt(Config.CopyVolumeWait.getDefaultValue())); Scope destScope = getZoneScope(destData.getDataStore().getScope()); DataStore cacheStore = cacheMgr.getCacheStorage(destScope); if (cacheStore == null) { - //need to find a nfs image store, assuming that can't copy volume directly to s3 - ImageStoreEntity imageStore = (ImageStoreEntity)this.dataStoreMgr.getImageStore(destScope.getScopeId()); - if (!imageStore.getProtocol().equalsIgnoreCase("nfs")) { - s_logger.debug("can't find a nfs image store"); - return null; - } - - DataObject objOnImageStore = imageStore.create(srcData); - objOnImageStore.processEvent(Event.CreateOnlyRequested); - - Answer answer = this.copyObject(srcData, objOnImageStore); - if (answer == null || !answer.getResult()) { - if (answer != null) { - s_logger.debug("copy to image store failed: " + answer.getDetails()); - } - objOnImageStore.processEvent(Event.OperationFailed); - imageStore.delete(objOnImageStore); - return answer; - } - - objOnImageStore.processEvent(Event.OperationSuccessed, answer); - - objOnImageStore.processEvent(Event.CopyingRequested); - - CopyCommand cmd = new CopyCommand(objOnImageStore.getTO(), destData.getTO(), _copyvolumewait); - EndPoint ep = selector.select(objOnImageStore, destData); - answer = ep.sendMessage(cmd); - - if (answer == null || !answer.getResult()) { - if (answer != null) { - s_logger.debug("copy to primary store failed: " + answer.getDetails()); - } - objOnImageStore.processEvent(Event.OperationFailed); - imageStore.delete(objOnImageStore); - return answer; - } - - objOnImageStore.processEvent(Event.OperationSuccessed); - imageStore.delete(objOnImageStore); - return answer; + // need to find a nfs image store, assuming that can't copy volume + // directly to s3 + ImageStoreEntity imageStore = (ImageStoreEntity) this.dataStoreMgr.getImageStore(destScope.getScopeId()); + if (!imageStore.getProtocol().equalsIgnoreCase("nfs")) { + s_logger.debug("can't find a nfs image store"); + return null; + } + + DataObject objOnImageStore = imageStore.create(srcData); + objOnImageStore.processEvent(Event.CreateOnlyRequested); + + Answer answer = this.copyObject(srcData, objOnImageStore); + if (answer == null || !answer.getResult()) { + if (answer != null) { + s_logger.debug("copy to image store failed: " + answer.getDetails()); + } + objOnImageStore.processEvent(Event.OperationFailed); + imageStore.delete(objOnImageStore); + return answer; + } + + objOnImageStore.processEvent(Event.OperationSuccessed, answer); + + objOnImageStore.processEvent(Event.CopyingRequested); + + CopyCommand cmd = new CopyCommand(objOnImageStore.getTO(), destData.getTO(), _copyvolumewait); + EndPoint ep = selector.select(objOnImageStore, destData); + answer = ep.sendMessage(cmd); + + if (answer == null || !answer.getResult()) { + if (answer != null) { + s_logger.debug("copy to primary store failed: " + answer.getDetails()); + } + objOnImageStore.processEvent(Event.OperationFailed); + imageStore.delete(objOnImageStore); + return answer; + } + + objOnImageStore.processEvent(Event.OperationSuccessed); + imageStore.delete(objOnImageStore); + return answer; } else { - DataObject cacheData = cacheMgr.createCacheObject(srcData, destScope); - CopyCommand cmd = new CopyCommand(cacheData.getTO(), destData.getTO(), _copyvolumewait); - EndPoint ep = selector.select(cacheData, destData); - Answer answer = ep.sendMessage(cmd); - return answer; + DataObject cacheData = cacheMgr.createCacheObject(srcData, destScope); + CopyCommand cmd = new CopyCommand(cacheData.getTO(), destData.getTO(), _copyvolumewait); + EndPoint ep = selector.select(cacheData, destData); + Answer answer = ep.sendMessage(cmd); + return answer; } } @Override - public Void copyAsync(DataObject srcData, DataObject destData, - AsyncCompletionCallback callback) { + public Void copyAsync(DataObject srcData, DataObject destData, AsyncCompletionCallback callback) { Answer answer = null; String errMsg = null; try { - if (srcData.getType() == DataObjectType.SNAPSHOT - && destData.getType() == DataObjectType.VOLUME) { - answer = copyVolumeFromSnapshot(srcData, destData); - } else if (srcData.getType() == DataObjectType.SNAPSHOT - && destData.getType() == DataObjectType.TEMPLATE) { - answer = createTemplateFromSnapshot(srcData, destData); - } else if (srcData.getType() == DataObjectType.TEMPLATE - && destData.getType() == DataObjectType.VOLUME) { - answer = cloneVolume(srcData, destData); - } else if (destData.getType() == DataObjectType.VOLUME - && srcData.getType() == DataObjectType.VOLUME && srcData.getDataStore().getRole() == DataStoreRole.Primary && destData.getDataStore().getRole() == DataStoreRole.Primary) { - answer = copyVolumeBetweenPools(srcData, destData); - } else if (srcData.getType() == DataObjectType.SNAPSHOT && - destData.getType() == DataObjectType.SNAPSHOT) { - answer = copySnapshot(srcData, destData); + if (srcData.getType() == DataObjectType.SNAPSHOT && destData.getType() == DataObjectType.VOLUME) { + answer = copyVolumeFromSnapshot(srcData, destData); + } else if (srcData.getType() == DataObjectType.SNAPSHOT && destData.getType() == DataObjectType.TEMPLATE) { + answer = createTemplateFromSnapshot(srcData, destData); + } else if (srcData.getType() == DataObjectType.TEMPLATE && destData.getType() == DataObjectType.VOLUME) { + answer = cloneVolume(srcData, destData); + } else if (destData.getType() == DataObjectType.VOLUME && srcData.getType() == DataObjectType.VOLUME + && srcData.getDataStore().getRole() == DataStoreRole.Primary + && destData.getDataStore().getRole() == DataStoreRole.Primary) { + answer = copyVolumeBetweenPools(srcData, destData); + } else if (srcData.getType() == DataObjectType.SNAPSHOT && destData.getType() == DataObjectType.SNAPSHOT) { + answer = copySnapshot(srcData, destData); } else { - answer = copyObject(srcData, destData); + answer = copyObject(srcData, destData); } if (answer != null && !answer.getResult()) { @@ -352,19 +343,14 @@ public class AncientDataMotionStrategy implements DataMotionStrategy { } @DB - protected Answer createTemplateFromSnapshot(DataObject srcData, - DataObject destData) { + protected Answer createTemplateFromSnapshot(DataObject srcData, DataObject destData) { - String value = configDao - .getValue(Config.CreatePrivateTemplateFromSnapshotWait - .toString()); - int _createprivatetemplatefromsnapshotwait = NumbersUtil.parseInt( - value, Integer - .parseInt(Config.CreatePrivateTemplateFromSnapshotWait - .getDefaultValue())); + String value = configDao.getValue(Config.CreatePrivateTemplateFromSnapshotWait.toString()); + int _createprivatetemplatefromsnapshotwait = NumbersUtil.parseInt(value, + Integer.parseInt(Config.CreatePrivateTemplateFromSnapshotWait.getDefaultValue())); if (needCacheStorage(srcData, destData)) { - SnapshotInfo snapshot = (SnapshotInfo)srcData; + SnapshotInfo snapshot = (SnapshotInfo) srcData; srcData = cacheSnapshotChain(snapshot); } @@ -376,7 +362,8 @@ public class AncientDataMotionStrategy implements DataMotionStrategy { protected Answer copySnapshot(DataObject srcData, DataObject destData) { String value = configDao.getValue(Config.BackupSnapshotWait.toString()); - int _backupsnapshotwait = NumbersUtil.parseInt(value, Integer.parseInt(Config.BackupSnapshotWait.getDefaultValue())); + int _backupsnapshotwait = NumbersUtil.parseInt(value, + Integer.parseInt(Config.BackupSnapshotWait.getDefaultValue())); DataObject cacheData = null; Answer answer = null; http://git-wip-us.apache.org/repos/asf/cloudstack/blob/0a522892/engine/storage/datamotion/src/org/apache/cloudstack/storage/motion/DataMotionServiceImpl.java ---------------------------------------------------------------------- diff --git a/engine/storage/datamotion/src/org/apache/cloudstack/storage/motion/DataMotionServiceImpl.java b/engine/storage/datamotion/src/org/apache/cloudstack/storage/motion/DataMotionServiceImpl.java index 8db878a..22de0b2 100644 --- a/engine/storage/datamotion/src/org/apache/cloudstack/storage/motion/DataMotionServiceImpl.java +++ b/engine/storage/datamotion/src/org/apache/cloudstack/storage/motion/DataMotionServiceImpl.java @@ -42,17 +42,13 @@ public class DataMotionServiceImpl implements DataMotionService { List strategies; @Override - public void copyAsync(DataObject srcData, DataObject destData, - AsyncCompletionCallback callback) { + public void copyAsync(DataObject srcData, DataObject destData, AsyncCompletionCallback callback) { if (srcData.getDataStore().getDriver().canCopy(srcData, destData)) { - srcData.getDataStore().getDriver() - .copyAsync(srcData, destData, callback); + srcData.getDataStore().getDriver().copyAsync(srcData, destData, callback); return; - } else if (destData.getDataStore().getDriver() - .canCopy(srcData, destData)) { - destData.getDataStore().getDriver() - .copyAsync(srcData, destData, callback); + } else if (destData.getDataStore().getDriver().canCopy(srcData, destData)) { + destData.getDataStore().getDriver().copyAsync(srcData, destData, callback); return; } @@ -66,8 +62,8 @@ public class DataMotionServiceImpl implements DataMotionService { } @Override - public void copyAsync(Map volumeMap, VirtualMachineTO vmTo, - Host srcHost, Host destHost, AsyncCompletionCallback callback) { + public void copyAsync(Map volumeMap, VirtualMachineTO vmTo, Host srcHost, Host destHost, + AsyncCompletionCallback callback) { for (DataMotionStrategy strategy : strategies) { if (strategy.canHandle(volumeMap, srcHost, destHost)) { strategy.copyAsync(volumeMap, vmTo, srcHost, destHost, callback); http://git-wip-us.apache.org/repos/asf/cloudstack/blob/0a522892/engine/storage/image/src/org/apache/cloudstack/storage/image/TemplateDataFactoryImpl.java ---------------------------------------------------------------------- diff --git a/engine/storage/image/src/org/apache/cloudstack/storage/image/TemplateDataFactoryImpl.java b/engine/storage/image/src/org/apache/cloudstack/storage/image/TemplateDataFactoryImpl.java index 52c79e6..e369c1c 100644 --- a/engine/storage/image/src/org/apache/cloudstack/storage/image/TemplateDataFactoryImpl.java +++ b/engine/storage/image/src/org/apache/cloudstack/storage/image/TemplateDataFactoryImpl.java @@ -33,15 +33,13 @@ import org.springframework.stereotype.Component; import com.cloud.storage.DataStoreRole; import com.cloud.storage.VMTemplateStoragePoolVO; -import com.cloud.storage.VMTemplateStorageResourceAssoc; import com.cloud.storage.VMTemplateVO; import com.cloud.storage.dao.VMTemplateDao; import com.cloud.storage.dao.VMTemplatePoolDao; @Component public class TemplateDataFactoryImpl implements TemplateDataFactory { - private static final Logger s_logger = Logger - .getLogger(TemplateDataFactoryImpl.class); + private static final Logger s_logger = Logger.getLogger(TemplateDataFactoryImpl.class); @Inject VMTemplateDao imageDataDao; @Inject @@ -50,11 +48,12 @@ public class TemplateDataFactoryImpl implements TemplateDataFactory { VMTemplatePoolDao templatePoolDao; @Inject TemplateDataStoreDao templateStoreDao; + @Override public TemplateInfo getTemplate(long templateId, DataStore store) { VMTemplateVO templ = imageDataDao.findById(templateId); if (store == null) { - TemplateObject tmpl = TemplateObject.getTemplate(templ, null); + TemplateObject tmpl = TemplateObject.getTemplate(templ, null); return tmpl; } // verify if the given input parameters are consistent with our db data. @@ -75,17 +74,15 @@ public class TemplateDataFactoryImpl implements TemplateDataFactory { s_logger.debug("template " + templateId + " is not in store:" + store.getId() + ", type:" + store.getRole()); } - TemplateObject tmpl = TemplateObject.getTemplate(templ, store); + TemplateObject tmpl = TemplateObject.getTemplate(templ, store); return tmpl; } - @Override public TemplateInfo getTemplate(long templateId, DataStoreRole storeRole) { - VMTemplateVO templ = imageDataDao.findById(templateId); TemplateDataStoreVO tmplStore = templateStoreDao.findByTemplate(templateId, storeRole); DataStore store = null; - if ( tmplStore != null ){ + if (tmplStore != null) { store = this.storeMgr.getDataStore(tmplStore.getDataStoreId(), storeRole); } return this.getTemplate(templateId, store); @@ -93,16 +90,14 @@ public class TemplateDataFactoryImpl implements TemplateDataFactory { @Override public TemplateInfo getTemplate(long templateId, DataStoreRole storeRole, Long zoneId) { - VMTemplateVO templ = imageDataDao.findById(templateId); TemplateDataStoreVO tmplStore = templateStoreDao.findByTemplateZone(templateId, zoneId, storeRole); DataStore store = null; - if ( tmplStore != null ){ + if (tmplStore != null) { store = this.storeMgr.getDataStore(tmplStore.getDataStoreId(), storeRole); } return this.getTemplate(templateId, store); } - @Override public TemplateInfo getTemplate(DataObject obj, DataStore store) { return this.getTemplate(obj.getId(), store);