cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From edi...@apache.org
Subject [10/17] clean up storage related coded
Date Thu, 30 May 2013 01:06:30 GMT
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<VolumeVO, Long> implements VolumeDao {
     private static final Logger s_logger = Logger.getLogger(VolumeDaoImpl.class);
     protected final SearchBuilder<VolumeVO> DetachedAccountIdSearch;
@@ -63,23 +63,23 @@ public class VolumeDaoImpl extends GenericDaoBase<VolumeVO, Long> implements Vol
     protected GenericSearchBuilder<VolumeVO, Long> CountByAccount;
     protected GenericSearchBuilder<VolumeVO, SumCount> primaryStorageSearch;
     protected GenericSearchBuilder<VolumeVO, SumCount> 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<VolumeVO> findDetachedByAccount(long accountId) {
-    	SearchCriteria<VolumeVO> sc = DetachedAccountIdSearch.create();
-    	sc.setParameters("accountId", accountId);
-    	sc.setParameters("destroyed", Volume.State.Destroy);
-    	return listBy(sc);
+        SearchCriteria<VolumeVO> sc = DetachedAccountIdSearch.create();
+        sc.setParameters("accountId", accountId);
+        sc.setParameters("destroyed", Volume.State.Destroy);
+        return listBy(sc);
     }
-    
+
     @Override
     public List<VolumeVO> findByAccount(long accountId) {
         SearchCriteria<VolumeVO> sc = AllFieldsSearch.create();
@@ -87,172 +87,172 @@ public class VolumeDaoImpl extends GenericDaoBase<VolumeVO, Long> implements Vol
         sc.setParameters("state", Volume.State.Ready);
         return listBy(sc);
     }
-    
+
     @Override
     public List<VolumeVO> findByInstance(long id) {
         SearchCriteria<VolumeVO> sc = AllFieldsSearch.create();
         sc.setParameters("instanceId", id);
-	    return listBy(sc);
-	}
-   
+        return listBy(sc);
+    }
+
     @Override
-    public List<VolumeVO> findByInstanceAndDeviceId(long instanceId, long deviceId){
-    	SearchCriteria<VolumeVO> sc = AllFieldsSearch.create();
-    	sc.setParameters("instanceId", instanceId);
-    	sc.setParameters("deviceId", deviceId);
-    	return listBy(sc);
+    public List<VolumeVO> findByInstanceAndDeviceId(long instanceId, long deviceId) {
+        SearchCriteria<VolumeVO> sc = AllFieldsSearch.create();
+        sc.setParameters("instanceId", instanceId);
+        sc.setParameters("deviceId", deviceId);
+        return listBy(sc);
     }
-    
+
     @Override
     public List<VolumeVO> findByPoolId(long poolId) {
         SearchCriteria<VolumeVO> 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<VolumeVO> findCreatedByInstance(long id) {
         SearchCriteria<VolumeVO> sc = AllFieldsSearch.create();
         sc.setParameters("instanceId", id);
         sc.setParameters("state", Volume.State.Ready);
         return listBy(sc);
     }
-    
+
     @Override
     public List<VolumeVO> findUsableVolumesForInstance(long instanceId) {
         SearchCriteria<VolumeVO> 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<VolumeVO> findByInstanceAndType(long id, Type vType) {
+
+    @Override
+    public List<VolumeVO> findByInstanceAndType(long id, Type vType) {
         SearchCriteria<VolumeVO> sc = AllFieldsSearch.create();
         sc.setParameters("instanceId", id);
         sc.setParameters("vType", vType.toString());
-	    return listBy(sc);
-	}
-	
-	@Override
-	public List<VolumeVO> findByInstanceIdDestroyed(long vmId) {
-		SearchCriteria<VolumeVO> sc = AllFieldsSearch.create();
-		sc.setParameters("instanceId", vmId);
-		sc.setParameters("destroyed", Volume.State.Destroy);
-		return listBy(sc);
-	}
-	
-	@Override
-	public List<VolumeVO> findReadyRootVolumesByInstance(long instanceId) {
-		SearchCriteria<VolumeVO> 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<VolumeVO> findByAccountAndPod(long accountId, long podId) {
-		SearchCriteria<VolumeVO> sc = AllFieldsSearch.create();
+        return listBy(sc);
+    }
+
+    @Override
+    public List<VolumeVO> findByInstanceIdDestroyed(long vmId) {
+        SearchCriteria<VolumeVO> sc = AllFieldsSearch.create();
+        sc.setParameters("instanceId", vmId);
+        sc.setParameters("destroyed", Volume.State.Destroy);
+        return listBy(sc);
+    }
+
+    @Override
+    public List<VolumeVO> findReadyRootVolumesByInstance(long instanceId) {
+        SearchCriteria<VolumeVO> 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<VolumeVO> findByAccountAndPod(long accountId, long podId) {
+        SearchCriteria<VolumeVO> sc = AllFieldsSearch.create();
         sc.setParameters("accountId", accountId);
         sc.setParameters("pod", podId);
         sc.setParameters("state", Volume.State.Ready);
-        
+
         return listIncludingRemovedBy(sc);
-	}
-	
-	@Override
-	public List<VolumeVO> findByTemplateAndZone(long templateId, long zoneId) {
-		SearchCriteria<VolumeVO> sc = TemplateZoneSearch.create();
-		sc.setParameters("template", templateId);
-		sc.setParameters("zone", zoneId);
-		
-		return listIncludingRemovedBy(sc);
-	}
-
-	@Override
-	public boolean isAnyVolumeActivelyUsingTemplateOnPool(long templateId, long poolId) {
-	    SearchCriteria<Long> sc = ActiveTemplateSearch.create();
-	    sc.setParameters("template", templateId);
-	    sc.setParameters("pool", poolId);
-	    
-	    List<Long> 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<VolumeVO> findByTemplateAndZone(long templateId, long zoneId) {
+        SearchCriteria<VolumeVO> sc = TemplateZoneSearch.create();
+        sc.setParameters("template", templateId);
+        sc.setParameters("zone", zoneId);
+
+        return listIncludingRemovedBy(sc);
+    }
+
+    @Override
+    public boolean isAnyVolumeActivelyUsingTemplateOnPool(long templateId, long poolId) {
+        SearchCriteria<Long> sc = ActiveTemplateSearch.create();
+        sc.setParameters("template", templateId);
+        sc.setParameters("pool", poolId);
+
+        List<Long> 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<VolumeVO> 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<VolumeVO, Long> 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<VolumeVO, Long> 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<Long, Long> getCountAndTotalByPool(long poolId) {
+    @Override
+    @DB(txn = false)
+    public Pair<Long, Long> getCountAndTotalByPool(long poolId) {
         SearchCriteria<SumCount> sc = TotalSizeByPoolSearch.create();
         sc.setParameters("poolId", poolId);
         List<SumCount> results = customSearch(sc, null);
         SumCount sumCount = results.get(0);
         return new Pair<Long, Long>(sumCount.count, sumCount.sum);
-	}
+    }
 
     @Override
-	public Long countAllocatedVolumesForAccount(long accountId) {
-	  	SearchCriteria<Long> sc = CountByAccount.create();
+    public Long countAllocatedVolumesForAccount(long accountId) {
+        SearchCriteria<Long> 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<VolumeVO, Long> 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<VolumeVO> listVolumesToBeDestroyed() {
         SearchCriteria<VolumeVO> 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<VolumeVO> 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<VolumeVO> 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<Long> listPoolIdsByVolumeCount(long dcId, Long podId, Long clusterId, long accountId) {
@@ -420,7 +427,7 @@ public class VolumeDaoImpl extends GenericDaoBase<VolumeVO, Long> 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<VolumeVO, Long> implements Vol
             throw new CloudRuntimeException("Caught: " + ORDER_POOLS_NUMBER_OF_VOLUMES_FOR_ACCOUNT, e);
         }
     }
-    
-    @Override @DB(txn=false)
+
+    @Override
+    @DB(txn = false)
     public Pair<Long, Long> getNonDestroyedCountAndTotalByPool(long poolId) {
         SearchCriteria<SumCount> sc = TotalSizeByPoolSearch.create();
         sc.setParameters("poolId", poolId);
@@ -442,7 +450,7 @@ public class VolumeDaoImpl extends GenericDaoBase<VolumeVO, Long> implements Vol
         SumCount sumCount = results.get(0);
         return new Pair<Long, Long>(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<VolumeHostVO, Long>, StateDao<ObjectInDataStoreStateMachine.State, ObjectInDataStoreStateMachine.Event, DataObjectInStore>{
+public interface VolumeHostDao extends GenericDao<VolumeHostVO, Long>,
+        StateDao<ObjectInDataStoreStateMachine.State, ObjectInDataStoreStateMachine.Event, DataObjectInStore> {
 
-	VolumeHostVO findByHostVolume(long hostId, long volumeId);
+    VolumeHostVO findByHostVolume(long hostId, long volumeId);
 
-	VolumeHostVO findByVolumeId(long volumeId);
+    VolumeHostVO findByVolumeId(long volumeId);
 
-	List<VolumeHostVO> listBySecStorage(long sserverId);
+    List<VolumeHostVO> listBySecStorage(long sserverId);
 
-	List<VolumeHostVO> listDestroyed(long hostId);
+    List<VolumeHostVO> 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<VolumeHostVO, Long> implements VolumeHostDao {
     private static final Logger s_logger = Logger.getLogger(VolumeHostDaoImpl.class);
     protected final SearchBuilder<VolumeHostVO> HostVolumeSearch;
@@ -44,7 +44,8 @@ public class VolumeHostDaoImpl extends GenericDaoBase<VolumeHostVO, Long> implem
     protected final SearchBuilder<VolumeHostVO> HostSearch;
     protected final SearchBuilder<VolumeHostVO> HostDestroyedSearch;
     protected final SearchBuilder<VolumeHostVO> 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<VolumeHostVO, Long> 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<VolumeHostVO, Long> 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<VolumeHostVO, Long> implem
         updateStateSearch.done();
     }
 
-
-
     @Override
     public VolumeHostVO findByHostVolume(long hostId, long volumeId) {
         SearchCriteria<VolumeHostVO> sc = HostVolumeSearch.create();
@@ -88,7 +87,7 @@ public class VolumeHostDaoImpl extends GenericDaoBase<VolumeHostVO, Long> 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<VolumeHostVO, Long> implem
         return findOneBy(sc);
     }
 
-
-
     @Override
     public List<VolumeHostVO> listBySecStorage(long ssHostId) {
         SearchCriteria<VolumeHostVO> sc = HostSearch.create();
@@ -118,7 +115,7 @@ public class VolumeHostDaoImpl extends GenericDaoBase<VolumeHostVO, Long> implem
     }
 
     @Override
-    public List<VolumeHostVO> listDestroyed(long hostId){
+    public List<VolumeHostVO> listDestroyed(long hostId) {
         SearchCriteria<VolumeHostVO> sc = HostDestroyedSearch.create();
         sc.setParameters("host_id", hostId);
         sc.setParameters("destroyed", true);
@@ -126,13 +123,11 @@ public class VolumeHostDaoImpl extends GenericDaoBase<VolumeHostVO, Long> 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<VolumeHostVO> sc = updateStateSearch.create();
         sc.setParameters("id", volHost.getId());
         sc.setParameters("state", currentState);
@@ -149,14 +144,18 @@ public class VolumeHostDaoImpl extends GenericDaoBase<VolumeHostVO, Long> 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<DataStore> 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> storageCacheAllocator;
     @Inject
@@ -120,10 +118,9 @@ public class StorageCacheManagerImpl implements StorageCacheManager, Manager {
         return true;
     }
 
-
-
     private class CreateCacheObjectContext<T> extends AsyncRpcConext<T> {
         final AsyncCallFuture<CopyCommandResult> 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<CopyCommandResult> future = new AsyncCallFuture<CopyCommandResult>();
-		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<CopyCommandResult> future = new AsyncCallFuture<CopyCommandResult>();
+        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<StorageCacheManagerImpl, CopyCommandResult> callback,
-	        CreateCacheObjectContext<CopyCommandResult> context) {
-	    AsyncCallFuture<CopyCommandResult> future = context.future;
-	    future.complete(callback.getResult());
-	    return null;
-	}
+    protected Void createCacheObjectCallBack(
+            AsyncCallbackDispatcher<StorageCacheManagerImpl, CopyCommandResult> callback,
+            CreateCacheObjectContext<CopyCommandResult> context) {
+        AsyncCallFuture<CopyCommandResult> 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<CopyCommandResult> callback) {
+    public Void copyAsync(DataObject srcData, DataObject destData, AsyncCompletionCallback<CopyCommandResult> 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<DataMotionStrategy> strategies;
 
     @Override
-    public void copyAsync(DataObject srcData, DataObject destData,
-            AsyncCompletionCallback<CopyCommandResult> callback) {
+    public void copyAsync(DataObject srcData, DataObject destData, AsyncCompletionCallback<CopyCommandResult> 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<VolumeInfo, DataStore> volumeMap, VirtualMachineTO vmTo,
-            Host srcHost, Host destHost, AsyncCompletionCallback<CopyCommandResult> callback) {
+    public void copyAsync(Map<VolumeInfo, DataStore> volumeMap, VirtualMachineTO vmTo, Host srcHost, Host destHost,
+            AsyncCompletionCallback<CopyCommandResult> 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);


Mime
View raw message