cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From edi...@apache.org
Subject [3/5] refactor snapshot
Date Tue, 30 Apr 2013 02:38:12 GMT
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/2f689171/plugins/storage/volume/default/src/org/apache/cloudstack/storage/datastore/driver/CloudStackPrimaryDataStoreDriverImpl.java
----------------------------------------------------------------------
diff --git a/plugins/storage/volume/default/src/org/apache/cloudstack/storage/datastore/driver/CloudStackPrimaryDataStoreDriverImpl.java
b/plugins/storage/volume/default/src/org/apache/cloudstack/storage/datastore/driver/CloudStackPrimaryDataStoreDriverImpl.java
index 5e82a7e..93d37de 100644
--- a/plugins/storage/volume/default/src/org/apache/cloudstack/storage/datastore/driver/CloudStackPrimaryDataStoreDriverImpl.java
+++ b/plugins/storage/volume/default/src/org/apache/cloudstack/storage/datastore/driver/CloudStackPrimaryDataStoreDriverImpl.java
@@ -36,6 +36,7 @@ import org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo;
 import org.apache.cloudstack.framework.async.AsyncCompletionCallback;
 import org.apache.cloudstack.storage.command.CreateObjectCommand;
 import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao;
+import org.apache.cloudstack.storage.to.SnapshotObjectTO;
 import org.apache.cloudstack.storage.volume.VolumeObject;
 import org.apache.log4j.Logger;
 
@@ -193,24 +194,17 @@ public class CloudStackPrimaryDataStoreDriverImpl implements PrimaryDataStoreDri
 			AsyncCompletionCallback<CreateCmdResult> callback) {
 	    CreateCmdResult result = null;
 	    try {
-	        VolumeInfo volume = snapshot.getBaseVolume();
-	        String vmName = this.volumeMgr.getVmNameOnVolume(volume);
-	        SnapshotVO preSnapshotVO = this.snapshotMgr.getParentSnapshot(volume, snapshot);
-	        String parentSnapshotPath = null;
-	        if (preSnapshotVO != null) {
-	            parentSnapshotPath = preSnapshotVO.getPath();
+	        DataTO snapshotTO = snapshot.getTO();
+	        
+	        CreateObjectCommand cmd = new CreateObjectCommand(snapshotTO);
+	        Answer answer = storageMgr.sendToPool((StoragePool)snapshot.getDataStore(), null,
cmd);
+	
+	        result = new CreateCmdResult(null, answer);
+	        if (answer != null && !answer.getResult()) {
+	            result.setResult(answer.getDetails());
 	        }
-	        StoragePool srcPool = (StoragePool)volume.getDataStore();
 
-	        ManageSnapshotCommand cmd = new ManageSnapshotCommand(snapshot.getId(), volume.getPath(),
srcPool, parentSnapshotPath, snapshot.getName(), vmName);
-
-	        ManageSnapshotAnswer answer = (ManageSnapshotAnswer) this.snapshotMgr.sendToPool(volume,
cmd);
-
-	        if ((answer != null) && answer.getResult()) {
-	            result = new CreateCmdResult(answer.getSnapshotPath(), null);
-	        } else {
-	            result = new CreateCmdResult(null, null);
-	        }
+	        callback.complete(result);
 	    } catch (Exception e) {
 	        s_logger.debug("Failed to take snapshot: " + snapshot.getId(), e);
 	        result = new CreateCmdResult(null, null);

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/2f689171/server/src/com/cloud/storage/CreateSnapshotPayload.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/storage/CreateSnapshotPayload.java b/server/src/com/cloud/storage/CreateSnapshotPayload.java
index cafe46c..2eec00d 100644
--- a/server/src/com/cloud/storage/CreateSnapshotPayload.java
+++ b/server/src/com/cloud/storage/CreateSnapshotPayload.java
@@ -1,7 +1,11 @@
 package com.cloud.storage;
 
+import com.cloud.user.Account;
+
 public class CreateSnapshotPayload {
 	private Long snapshotPolicyId;
+	private Long snapshotId;
+	private Account account;
 
 	public Long getSnapshotPolicyId() {
 		return snapshotPolicyId;
@@ -11,4 +15,20 @@ public class CreateSnapshotPayload {
 		this.snapshotPolicyId = snapshotPolicyId;
 	}
 
+    public Long getSnapshotId() {
+        return snapshotId;
+    }
+
+    public void setSnapshotId(Long snapshotId) {
+        this.snapshotId = snapshotId;
+    }
+
+    public Account getAccount() {
+        return account;
+    }
+
+    public void setAccount(Account account) {
+        this.account = account;
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/2f689171/server/src/com/cloud/storage/VolumeManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/storage/VolumeManagerImpl.java b/server/src/com/cloud/storage/VolumeManagerImpl.java
index 3177a59..23e3295 100644
--- a/server/src/com/cloud/storage/VolumeManagerImpl.java
+++ b/server/src/com/cloud/storage/VolumeManagerImpl.java
@@ -133,6 +133,7 @@ import com.cloud.storage.dao.VolumeDao;
 import com.cloud.storage.download.DownloadMonitor;
 import com.cloud.storage.s3.S3Manager;
 import com.cloud.storage.secondary.SecondaryStorageVmManager;
+import com.cloud.storage.snapshot.SnapshotApiService;
 import com.cloud.storage.snapshot.SnapshotManager;
 import com.cloud.storage.snapshot.SnapshotScheduler;
 import com.cloud.tags.dao.ResourceTagDao;
@@ -307,6 +308,8 @@ public class VolumeManagerImpl extends ManagerBase implements VolumeManager
{
     TemplateDataFactory tmplFactory;
     @Inject
     SnapshotDataFactory snapshotFactory;
+    @Inject
+    SnapshotApiService snapshotMgr;
     private int _copyvolumewait;
     @Inject
     protected HypervisorCapabilitiesDao _hypervisorCapabilitiesDao;
@@ -526,7 +529,7 @@ public class VolumeManagerImpl extends ManagerBase implements VolumeManager
{
 
         VolumeInfo vol = this.volFactory.getVolume(volume.getId());
         DataStore store = this.dataStoreMgr.getDataStore(pool.getId(), DataStoreRole.Primary);
-        SnapshotInfo snapInfo = this.snapshotFactory.getSnapshot(snapshot.getId());
+        SnapshotInfo snapInfo = this.snapshotFactory.getSnapshot(snapshot.getId(), DataStoreRole.Image);
         AsyncCallFuture<VolumeApiResult> future = this.volService.createVolumeFromSnapshot(vol,
store, snapInfo);
         try {
             VolumeApiResult result = future.get();
@@ -2470,8 +2473,26 @@ public class VolumeManagerImpl extends ManagerBase implements VolumeManager
{
     
     
     @Override
-    public Snapshot takeSnapshot(Long volumeId, Long policyId) throws ResourceAllocationException
{
-    	Account caller = UserContext.current().getCaller();
+    public Snapshot takeSnapshot(Long volumeId, Long policyId, Long snapshotId, Account account)
throws ResourceAllocationException {
+        VolumeInfo volume = this.volFactory.getVolume(volumeId);
+        if (volume == null) {
+            throw new InvalidParameterValueException("Creating snapshot failed due to volume:"
+ volumeId + " doesn't exist");
+        }
+        
+        if (volume.getState() != Volume.State.Ready) {
+            throw new InvalidParameterValueException("VolumeId: " + volumeId + " is not in
" + Volume.State.Ready + " state but " + volume.getState() + ". Cannot take snapshot.");
+        }
+       
+        CreateSnapshotPayload payload = new CreateSnapshotPayload();
+        payload.setSnapshotId(snapshotId);
+        payload.setSnapshotPolicyId(policyId);
+        payload.setAccount(account);
+        return this.volService.takeSnapshot(volume);
+    }
+
+    @Override
+    public Snapshot allocSnapshot(Long volumeId, Long policyId) throws ResourceAllocationException
{
+        Account caller = UserContext.current().getCaller();
 
         VolumeInfo volume = this.volFactory.getVolume(volumeId);
         if (volume == null) {
@@ -2502,10 +2523,7 @@ public class VolumeManagerImpl extends ManagerBase implements VolumeManager
{
             throw new InvalidParameterValueException("VolumeId: " + volumeId + " please attach
this volume to a VM before create snapshot for it");
         }
         
-        CreateSnapshotPayload payload = new CreateSnapshotPayload();
-        payload.setSnapshotPolicyId(policyId);
-        return this.volService.takeSnapshot(volume);
-
+        return this.snapshotMgr.allocSnapshot(volumeId, policyId);
     }
 
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/2f689171/server/src/com/cloud/storage/dao/SnapshotDaoImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/storage/dao/SnapshotDaoImpl.java b/server/src/com/cloud/storage/dao/SnapshotDaoImpl.java
index 24d4c65..a349389 100644
--- a/server/src/com/cloud/storage/dao/SnapshotDaoImpl.java
+++ b/server/src/com/cloud/storage/dao/SnapshotDaoImpl.java
@@ -151,7 +151,7 @@ public class SnapshotDaoImpl extends GenericDaoBase<SnapshotVO, Long>
implements
         VolumeIdVersionSearch.and("volumeId", VolumeIdVersionSearch.entity().getVolumeId(),
SearchCriteria.Op.EQ);
         VolumeIdVersionSearch.and("version", VolumeIdVersionSearch.entity().getVersion(),
SearchCriteria.Op.EQ);
         VolumeIdVersionSearch.done();
-
+/*
         ParentIdSearch = createSearchBuilder();
         ParentIdSearch.and("prevSnapshotId", ParentIdSearch.entity().getPrevSnapshotId(),
SearchCriteria.Op.EQ);
         ParentIdSearch.done();
@@ -159,7 +159,7 @@ public class SnapshotDaoImpl extends GenericDaoBase<SnapshotVO, Long>
implements
         backupUuidSearch = createSearchBuilder();
         backupUuidSearch.and("backupUuid", backupUuidSearch.entity().getBackupSnapshotId(),
SearchCriteria.Op.EQ);
         backupUuidSearch.done();
-
+*/
         AccountIdSearch = createSearchBuilder();
         AccountIdSearch.and("accountId", AccountIdSearch.entity().getAccountId(), SearchCriteria.Op.EQ);
         AccountIdSearch.done();

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/2f689171/server/src/com/cloud/storage/snapshot/SnapshotManager.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/storage/snapshot/SnapshotManager.java b/server/src/com/cloud/storage/snapshot/SnapshotManager.java
index 983b50c..d6e0af6 100755
--- a/server/src/com/cloud/storage/snapshot/SnapshotManager.java
+++ b/server/src/com/cloud/storage/snapshot/SnapshotManager.java
@@ -56,10 +56,6 @@ public interface SnapshotManager {
      *            The account which is to be deleted.
      */
     boolean deleteSnapshotDirsForAccount(long accountId);
-  
-    void downloadSnapshotsFromSwift(SnapshotVO ss);
-
-    void downloadSnapshotsFromS3(SnapshotVO snapshot);
 
     String getSecondaryStorageURL(SnapshotVO snapshot);
 

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/2f689171/server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java b/server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java
index c18ffa8..572bcab 100755
--- a/server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java
+++ b/server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java
@@ -349,8 +349,10 @@ public class SnapshotManagerImpl extends ManagerBase implements SnapshotManager,
          return this.snapshotSrv.backupSnapshot(snapshot);
     }
 
+    /*
     @Override
     public void downloadSnapshotsFromSwift(SnapshotVO ss) {
+       
         long volumeId = ss.getVolumeId();
         VolumeVO volume = _volsDao.findById(volumeId);
         Long dcId = volume.getDataCenterId();
@@ -432,7 +434,7 @@ public class SnapshotManagerImpl extends ManagerBase implements SnapshotManager,
                     e);
         }
 
-    }
+    }*/
 
     @Override
     public SnapshotVO getParentSnapshot(VolumeInfo volume) {
@@ -974,72 +976,44 @@ public class SnapshotManagerImpl extends ManagerBase implements SnapshotManager,
 	}
     @Override
     public SnapshotInfo takeSnapshot(VolumeInfo volume) throws ResourceAllocationException
{
-        Account caller = UserContext.current().getCaller();
+        CreateSnapshotPayload payload = (CreateSnapshotPayload)volume.getpayload();
+        Long snapshotId = payload.getSnapshotId();
+        Account snapshotOwner = payload.getAccount();
+        SnapshotInfo snapshot = this.snapshotFactory.getSnapshot(snapshotId, volume.getDataStore());
+        boolean processed = false;
         
-        supportedByHypervisor(volume);
-        CreateSnapshotPayload snapInfo = (CreateSnapshotPayload)volume.getpayload();
-        Long policyId = snapInfo.getSnapshotPolicyId();
-        // Verify permissions
-        _accountMgr.checkAccess(caller, null, true, volume);
-        Type snapshotType = getSnapshotType(policyId);
-        Account owner = _accountMgr.getAccount(volume.getAccountId());
-
-        try{
-            _resourceLimitMgr.checkResourceLimit(owner, ResourceType.snapshot);
-            if (backup) {
-                _resourceLimitMgr.checkResourceLimit(owner, ResourceType.secondary_storage,
new Long(volume.getSize()));
-            } else {
-                _resourceLimitMgr.checkResourceLimit(owner, ResourceType.primary_storage,
new Long(volume.getSize()));
+        try {
+            for (SnapshotStrategy strategy : snapshotStrategies) {
+                if (strategy.canHandle(snapshot)) {
+                    processed = true;
+                    snapshot = strategy.takeSnapshot(snapshot);
+                    break;
+                }
             }
-        } catch (ResourceAllocationException e) {
-            if (snapshotType != Type.MANUAL){
-                String msg = "Snapshot resource limit exceeded for account id : " + owner.getId()
+ ". Failed to create recurring snapshots";
-                s_logger.warn(msg);
-                _alertMgr.sendAlert(AlertManager.ALERT_TYPE_UPDATE_RESOURCE_COUNT, 0L, 0L,
msg,
-                        "Snapshot resource limit exceeded for account id : " + owner.getId()
+ ". Failed to create recurring snapshots; please use updateResourceLimit to increase the
limit");
+            if (!processed) {
+                throw new CloudRuntimeException("Can't find snapshot strategy to deal with
snapshot:" + snapshotId);
             }
-            throw e;
-        }
+            postCreateSnapshot(volume.getId(), snapshotId, payload.getSnapshotPolicyId());
+        
+            UsageEventUtils.publishUsageEvent(EventTypes.EVENT_SNAPSHOT_CREATE, snapshot.getAccountId(),
+                    snapshot.getDataCenterId(), snapshotId, snapshot.getName(), null, null,
+                    volume.getSize(), snapshot.getClass().getName(), snapshot.getUuid());
 
-        // Determine the name for this snapshot
-        // Snapshot Name: VMInstancename + volumeName + timeString
-        String timeString = DateUtil.getDateDisplayString(DateUtil.GMT_TIMEZONE, new Date(),
DateUtil.YYYYMMDD_FORMAT);
 
-        VMInstanceVO vmInstance = _vmDao.findById(volume.getInstanceId());
-        String vmDisplayName = "detached";
-        if (vmInstance != null) {
-            vmDisplayName = vmInstance.getHostName();
-        }
-        String snapshotName = vmDisplayName + "_" + volume.getName() + "_" + timeString;
+            _resourceLimitMgr.incrementResourceCount(snapshotOwner.getId(), ResourceType.snapshot);
 
-        HypervisorType hypervisorType = volume.getHypervisorType();
-        SnapshotVO snapshotVO = new SnapshotVO(volume.getDataCenterId(), volume.getAccountId(),
volume.getDomainId(), volume.getId(), volume.getDiskOfferingId(), snapshotName,
-                (short) snapshotType.ordinal(), snapshotType.name(), volume.getSize(), hypervisorType);
-        
-        SnapshotVO snapshot = _snapshotDao.persist(snapshotVO);
-        if (snapshot == null) {
-            throw new CloudRuntimeException("Failed to create snapshot for volume: " + volume.getId());
-        }
-        if (backup) {
-            _resourceLimitMgr.incrementResourceCount(volume.getAccountId(), ResourceType.secondary_storage,
-                    new Long(volume.getSize()));
-        } else {
-            _resourceLimitMgr.incrementResourceCount(volume.getAccountId(), ResourceType.primary_storage,
-                    new Long(volume.getSize()));
-        }
-        SnapshotInfo snap = this.snapshotFactory.getSnapshot(snapshot.getId(), volume.getDataStore());
-        boolean processed = false;
-        for (SnapshotStrategy strategy : snapshotStrategies) {
-        	if (strategy.canHandle(snap)) {
-        		processed = true;
-        		snap = strategy.takeSnapshot(snap);
-        		break;
-        	}
-        }
-        if (!processed) {
-        	throw new CloudRuntimeException("Can't find snapshot strategy to deal with snapshot:"
+ snapshot.getId());
+        } catch(Exception e) {
+            s_logger.debug("Failed to create snapshot", e);
+            if (backup) {
+                _resourceLimitMgr.decrementResourceCount(snapshotOwner.getId(), ResourceType.secondary_storage,
+                        new Long(volume.getSize()));
+            } else {
+                _resourceLimitMgr.decrementResourceCount(snapshotOwner.getId(), ResourceType.primary_storage,
+                        new Long(volume.getSize()));
+            }
+            throw new CloudRuntimeException("Failed to create snapshot", e);
         }
-        return snap;
+        return snapshot;
     }
 
     @Override
@@ -1128,4 +1102,61 @@ public class SnapshotManagerImpl extends ManagerBase implements SnapshotManager,
     	}
     	return true;
     }
+
+    @Override
+    public Snapshot allocSnapshot(Long volumeId, Long policyId) throws ResourceAllocationException
{
+        Account caller = UserContext.current().getCaller();
+        VolumeInfo volume = this.volFactory.getVolume(volumeId);
+        supportedByHypervisor(volume);
+
+        // Verify permissions
+        _accountMgr.checkAccess(caller, null, true, volume);
+        Type snapshotType = getSnapshotType(policyId);
+        Account owner = _accountMgr.getAccount(volume.getAccountId());
+
+        try{
+            _resourceLimitMgr.checkResourceLimit(owner, ResourceType.snapshot);
+            if (backup) {
+                _resourceLimitMgr.checkResourceLimit(owner, ResourceType.secondary_storage,
new Long(volume.getSize()));
+            } else {
+                _resourceLimitMgr.checkResourceLimit(owner, ResourceType.primary_storage,
new Long(volume.getSize()));
+            }
+        } catch (ResourceAllocationException e) {
+            if (snapshotType != Type.MANUAL){
+                String msg = "Snapshot resource limit exceeded for account id : " + owner.getId()
+ ". Failed to create recurring snapshots";
+                s_logger.warn(msg);
+                _alertMgr.sendAlert(AlertManager.ALERT_TYPE_UPDATE_RESOURCE_COUNT, 0L, 0L,
msg,
+                        "Snapshot resource limit exceeded for account id : " + owner.getId()
+ ". Failed to create recurring snapshots; please use updateResourceLimit to increase the
limit");
+            }
+            throw e;
+        }
+
+        // Determine the name for this snapshot
+        // Snapshot Name: VMInstancename + volumeName + timeString
+        String timeString = DateUtil.getDateDisplayString(DateUtil.GMT_TIMEZONE, new Date(),
DateUtil.YYYYMMDD_FORMAT);
+
+        VMInstanceVO vmInstance = _vmDao.findById(volume.getInstanceId());
+        String vmDisplayName = "detached";
+        if (vmInstance != null) {
+            vmDisplayName = vmInstance.getHostName();
+        }
+        String snapshotName = vmDisplayName + "_" + volume.getName() + "_" + timeString;
+
+        HypervisorType hypervisorType = volume.getHypervisorType();
+        SnapshotVO snapshotVO = new SnapshotVO(volume.getDataCenterId(), volume.getAccountId(),
volume.getDomainId(), volume.getId(), volume.getDiskOfferingId(), snapshotName,
+                (short) snapshotType.ordinal(), snapshotType.name(), volume.getSize(), hypervisorType);
+        
+        SnapshotVO snapshot = _snapshotDao.persist(snapshotVO);
+        if (snapshot == null) {
+            throw new CloudRuntimeException("Failed to create snapshot for volume: " + volume.getId());
+        }
+        if (backup) {
+            _resourceLimitMgr.incrementResourceCount(volume.getAccountId(), ResourceType.secondary_storage,
+                    new Long(volume.getSize()));
+        } else {
+            _resourceLimitMgr.incrementResourceCount(volume.getAccountId(), ResourceType.primary_storage,
+                    new Long(volume.getSize()));
+        }
+        return snapshot;
+    }
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/2f689171/server/src/com/cloud/template/TemplateManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/template/TemplateManagerImpl.java b/server/src/com/cloud/template/TemplateManagerImpl.java
index de69c12..fbac9b4 100755
--- a/server/src/com/cloud/template/TemplateManagerImpl.java
+++ b/server/src/com/cloud/template/TemplateManagerImpl.java
@@ -1355,7 +1355,7 @@ public class TemplateManagerImpl extends ManagerBase implements TemplateManager,
             }
             AsyncCallFuture<TemplateApiResult> future = null;
             if (snapshotId != null) {
-                SnapshotInfo snapInfo = this._snapshotFactory.getSnapshot(snapshotId);
+                SnapshotInfo snapInfo = this._snapshotFactory.getSnapshot(snapshotId, DataStoreRole.Image);
                 future = this._tmpltSvr.createTemplateFromSnapshotAsync(snapInfo, tmplInfo,
store.get(0));
             } else if (volumeId != null) {
                volume = _volumeDao.findById(volumeId);


Mime
View raw message