Skip to content

Commit 2f68917

Browse files
committed
refactor snapshot
1 parent 37cbe88 commit 2f68917

30 files changed

Lines changed: 889 additions & 500 deletions

File tree

api/src/com/cloud/storage/Snapshot.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ public enum State {
6060
CreatedOnPrimary,
6161
BackingUp,
6262
BackedUp,
63+
Copying,
64+
Destroying,
65+
Destroyed,//it's a state, user can't see the snapshot from ui, while the snapshot may still exist on the storage
6366
Error;
6467

6568
public String toString() {
@@ -76,6 +79,8 @@ enum Event {
7679
OperationNotPerformed,
7780
BackupToSecondary,
7881
BackedupToSecondary,
82+
DestroyRequested,
83+
CopyingRequested,
7984
OperationSucceeded,
8085
OperationFailed
8186
}

api/src/com/cloud/storage/VolumeApiService.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ public interface VolumeApiService {
8080

8181
Volume detachVolumeFromVM(DetachVolumeCmd cmmd);
8282

83-
Snapshot takeSnapshot(Long volumeId, Long policyId)
83+
Snapshot takeSnapshot(Long volumeId, Long policyId, Long snapshotId, Account account)
8484
throws ResourceAllocationException;
85+
86+
Snapshot allocSnapshot(Long volumeId, Long policyId)
87+
throws ResourceAllocationException;
8588
}

api/src/org/apache/cloudstack/api/command/user/snapshot/CreateSnapshotCmd.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public AsyncJob.Type getInstanceType() {
152152

153153
@Override
154154
public void create() throws ResourceAllocationException {
155-
Snapshot snapshot = _snapshotService.allocSnapshot(getVolumeId(), getPolicyId());
155+
Snapshot snapshot = this._volumeService.allocSnapshot(getVolumeId(), getPolicyId());
156156
if (snapshot != null) {
157157
this.setEntityId(snapshot.getId());
158158
this.setEntityUuid(snapshot.getUuid());
@@ -164,14 +164,20 @@ public void create() throws ResourceAllocationException {
164164
@Override
165165
public void execute() {
166166
UserContext.current().setEventDetails("Volume Id: "+getVolumeId());
167-
Snapshot snapshot = _snapshotService.createSnapshot(getVolumeId(), getPolicyId(), getEntityId(), _accountService.getAccount(getEntityOwnerId()));
168-
if (snapshot != null) {
169-
SnapshotResponse response = _responseGenerator.createSnapshotResponse(snapshot);
170-
response.setResponseName(getCommandName());
171-
this.setResponseObject(response);
172-
} else {
167+
Snapshot snapshot;
168+
try {
169+
snapshot = _volumeService.takeSnapshot(this.getVolumeId(), this.getPolicyId(), this.getEntityId(), _accountService.getAccount(getEntityOwnerId()));
170+
if (snapshot != null) {
171+
SnapshotResponse response = _responseGenerator.createSnapshotResponse(snapshot);
172+
response.setResponseName(getCommandName());
173+
this.setResponseObject(response);
174+
} else {
175+
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to create snapshot due to an internal error creating snapshot for volume " + volumeId);
176+
}
177+
} catch (Exception e) {
173178
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to create snapshot due to an internal error creating snapshot for volume " + volumeId);
174179
}
180+
175181
}
176182

177183

engine/api/src/org/apache/cloudstack/engine/subsystem/api/storage/SnapshotStrategy.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@
33
import com.cloud.storage.Snapshot;
44

55

6+
67
public interface SnapshotStrategy {
78
public SnapshotInfo takeSnapshot(SnapshotInfo snapshot);
89
public SnapshotInfo backupSnapshot(SnapshotInfo snapshot);
910
public boolean deleteSnapshot(Long snapshotId);
10-
public boolean canHandle(Snapshot snapshot);
11+
/**
12+
* @param snapshot
13+
* @return
14+
*/
15+
boolean canHandle(Snapshot snapshot);
1116
}

engine/api/src/org/apache/cloudstack/engine/subsystem/api/storage/StorageCacheManager.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,11 @@
2222
public interface StorageCacheManager {
2323
public DataStore getCacheStorage(Scope scope);
2424
public DataObject createCacheObject(DataObject data, Scope scope);
25+
/** only create cache object in db
26+
* @param data
27+
* @param scope
28+
* @return
29+
*/
30+
DataObject getCacheObject(DataObject data, Scope scope);
31+
DataObject deleteCacheObject(DataObject data);
2532
}

engine/api/src/org/apache/cloudstack/engine/subsystem/api/storage/VolumeInfo.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@ public interface VolumeInfo extends DataObject, Volume {
2727
public Object getpayload();
2828
public HypervisorType getHypervisorType();
2929
public Long getLastPoolId();
30+
public String getAttachedVmName();
3031
}

engine/api/src/org/apache/cloudstack/storage/command/CopyCommand.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@
2323
public class CopyCommand extends Command implements StorageSubSystemCommand {
2424
private DataTO srcTO;
2525
private DataTO destTO;
26+
private DataTO cacheTO;
2627

27-
28-
public CopyCommand(DataTO srcUri, DataTO destUri, int timeout) {
28+
public CopyCommand(DataTO srcData, DataTO destData, int timeout) {
2929
super();
30-
this.srcTO = srcUri;
31-
this.destTO = destUri;
30+
this.srcTO = srcData;
31+
this.destTO = destData;
3232
this.setWait(timeout);
3333
}
3434

@@ -45,4 +45,12 @@ public boolean executeInSequence() {
4545
return true;
4646
}
4747

48+
public DataTO getCacheTO() {
49+
return cacheTO;
50+
}
51+
52+
public void setCacheTO(DataTO cacheTO) {
53+
this.cacheTO = cacheTO;
54+
}
55+
4856
}

engine/api/src/org/apache/cloudstack/storage/to/SnapshotObjectTO.java

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,43 @@
22

33
import org.apache.cloudstack.engine.subsystem.api.storage.DataObjectType;
44
import org.apache.cloudstack.engine.subsystem.api.storage.DataTO;
5+
import org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo;
56

67
import com.cloud.agent.api.to.DataStoreTO;
78

89
public class SnapshotObjectTO implements DataTO {
910
private String path;
11+
private VolumeObjectTO volume;
12+
private String parentSnapshotPath;
13+
private DataStoreTO dataStore;
14+
private String vmName;
15+
private String name;
16+
private long id;
17+
18+
public SnapshotObjectTO() {
19+
20+
}
21+
22+
public SnapshotObjectTO(SnapshotInfo snapshot) {
23+
this.path = snapshot.getPath();
24+
this.setId(snapshot.getId());
25+
this.volume = (VolumeObjectTO)snapshot.getBaseVolume().getTO();
26+
this.setVmName(snapshot.getBaseVolume().getAttachedVmName());
27+
if (snapshot.getParent() != null) {
28+
this.parentSnapshotPath = snapshot.getParent().getPath();
29+
}
30+
this.dataStore = snapshot.getDataStore().getTO();
31+
this.setName(snapshot.getName());
32+
}
33+
1034
@Override
1135
public DataObjectType getObjectType() {
12-
// TODO Auto-generated method stub
13-
return null;
36+
return DataObjectType.SNAPSHOT;
1437
}
1538

1639
@Override
1740
public DataStoreTO getDataStore() {
18-
// TODO Auto-generated method stub
19-
return null;
41+
return this.dataStore;
2042
}
2143

2244
@Override
@@ -27,4 +49,44 @@ public String getPath() {
2749
public void setPath(String path) {
2850
this.path = path;
2951
}
52+
53+
public VolumeObjectTO getVolume() {
54+
return volume;
55+
}
56+
57+
public void setVolume(VolumeObjectTO volume) {
58+
this.volume = volume;
59+
}
60+
61+
public String getParentSnapshotPath() {
62+
return parentSnapshotPath;
63+
}
64+
65+
public void setParentSnapshotPath(String parentSnapshotPath) {
66+
this.parentSnapshotPath = parentSnapshotPath;
67+
}
68+
69+
public String getVmName() {
70+
return vmName;
71+
}
72+
73+
public void setVmName(String vmName) {
74+
this.vmName = vmName;
75+
}
76+
77+
public long getId() {
78+
return id;
79+
}
80+
81+
public void setId(long id) {
82+
this.id = id;
83+
}
84+
85+
public String getName() {
86+
return name;
87+
}
88+
89+
public void setName(String name) {
90+
this.name = name;
91+
}
3092
}

engine/api/src/org/apache/cloudstack/storage/to/VolumeObjectTO.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public class VolumeObjectTO implements DataTO {
3232
private String name;
3333
private long size;
3434
private String path;
35+
private Long volumeId;
3536

3637
public VolumeObjectTO() {
3738

@@ -49,6 +50,7 @@ public VolumeObjectTO(VolumeInfo volume) {
4950
}
5051
//this.name = volume.getName();
5152
this.size = volume.getSize();
53+
this.setVolumeId(volume.getId());
5254
}
5355

5456
public String getUuid() {
@@ -103,4 +105,12 @@ public void setPath(String path) {
103105
this.path = path;
104106
}
105107

108+
public Long getVolumeId() {
109+
return volumeId;
110+
}
111+
112+
public void setVolumeId(Long volumeId) {
113+
this.volumeId = volumeId;
114+
}
115+
106116
}

engine/storage/cache/src/org/apache/cloudstack/storage/cache/manager/StorageCacheManagerImpl.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,24 @@ public DataObject createCacheObject(DataObject data, Scope scope) {
164164
return null;
165165
}
166166

167+
@Override
168+
public DataObject getCacheObject(DataObject data, Scope scope) {
169+
DataStore cacheStore = this.getCacheStorage(scope);
170+
DataObject objOnCacheStore = cacheStore.create(data);
171+
172+
return objOnCacheStore;
173+
}
174+
167175
protected Void createCacheObjectCallBack(AsyncCallbackDispatcher<StorageCacheManagerImpl, CopyCommandResult> callback,
168176
CreateCacheObjectContext<CopyCommandResult> context) {
169177
AsyncCallFuture<CopyCommandResult> future = context.future;
170178
future.complete(callback.getResult());
171179
return null;
172180
}
181+
182+
@Override
183+
public DataObject deleteCacheObject(DataObject data) {
184+
// TODO Auto-generated method stub
185+
return null;
186+
}
173187
}

0 commit comments

Comments
 (0)