Skip to content

Commit ae90259

Browse files
committed
fix snapshot
1 parent fb4036e commit ae90259

5 files changed

Lines changed: 72 additions & 30 deletions

File tree

engine/storage/snapshot/src/org/apache/cloudstack/storage/snapshot/SnapshotDataFactoryImpl.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ public SnapshotInfo getSnapshot(long snapshotId) {
7474

7575
@Override
7676
public SnapshotInfo getSnapshot(DataObject obj, DataStore store) {
77-
throw new CloudRuntimeException("not implemented yet");
77+
SnapshotVO snapshot = snapshotDao.findByIdIncludingRemoved(obj.getId());
78+
if (snapshot == null) {
79+
throw new CloudRuntimeException("Can't find snapshot: " + obj.getId());
80+
}
81+
SnapshotObject so = SnapshotObject.getSnapshotObject(snapshot, store);
82+
return so;
7883
}
7984
}

engine/storage/snapshot/src/org/apache/cloudstack/storage/snapshot/SnapshotStateMachineManagerImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public SnapshotStateMachineManagerImpl() {
2626
stateMachine.addTransition(Snapshot.State.Creating, Event.OperationFailed, Snapshot.State.Error);
2727
stateMachine.addTransition(Snapshot.State.CreatedOnPrimary, Event.BackupToSecondary, Snapshot.State.BackingUp);
2828
stateMachine.addTransition(Snapshot.State.BackingUp, Event.OperationSucceeded, Snapshot.State.BackedUp);
29-
stateMachine.addTransition(Snapshot.State.BackingUp, Event.OperationFailed, Snapshot.State.Error);
29+
stateMachine.addTransition(Snapshot.State.BackingUp, Event.OperationFailed, Snapshot.State.CreatedOnPrimary);
3030

3131
stateMachine.registerListener(new SnapshotStateListener());
3232
}

engine/storage/snapshot/src/org/apache/cloudstack/storage/snapshot/strategy/AncientSnasphotStrategy.java

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,10 @@ protected Void createSnapshotAsyncCallback(AsyncCallbackDispatcher<AncientSnasph
180180

181181
try {
182182
SnapshotVO preSnapshotVO = this.snapshotMgr.getParentSnapshot(volume, snapshot);
183-
String preSnapshotPath = preSnapshotVO.getPath();
183+
String preSnapshotPath = null;
184+
if (preSnapshotVO != null) {
185+
preSnapshotPath = preSnapshotVO.getPath();
186+
}
184187
SnapshotVO snapshotVO = this.snapshotDao.findById(snapshot.getId());
185188
// The snapshot was successfully created
186189
if (preSnapshotPath != null && preSnapshotPath.equals(result.getPath())) {
@@ -238,6 +241,11 @@ protected Void createSnapshotAsyncCallback(AsyncCallbackDispatcher<AncientSnasph
238241
} catch (Exception e) {
239242
s_logger.debug("Failed to create snapshot: ", e);
240243
snapResult.setResult(e.toString());
244+
try {
245+
snapshot.processEvent(Snapshot.Event.OperationFailed);
246+
} catch (NoTransitionException e1) {
247+
s_logger.debug("Failed to change snapshot state: " + e1.toString());
248+
}
241249
}
242250

243251
future.complete(snapResult);
@@ -263,19 +271,30 @@ protected SnapshotInfo createSnapshotOnPrimary(VolumeInfo volume, Long snapshotI
263271
s_logger.debug("Failed to update snapshot state due to " + nte.getMessage());
264272
throw new CloudRuntimeException("Failed to update snapshot state due to " + nte.getMessage());
265273
}
266-
AsyncCallFuture<SnapshotResult> future = new AsyncCallFuture<SnapshotResult>();
267274

268-
CreateSnapshotContext<CommandResult> context = new CreateSnapshotContext<CommandResult>(
269-
null, volume, snapshot, future);
270-
AsyncCallbackDispatcher<AncientSnasphotStrategy, CreateCmdResult> caller = AsyncCallbackDispatcher
271-
.create(this);
272-
caller.setCallback(
273-
caller.getTarget().createSnapshotAsyncCallback(null, null))
274-
.setContext(context);
275-
PrimaryDataStoreDriver primaryStore = (PrimaryDataStoreDriver)volume.getDataStore().getDriver();
276-
277-
primaryStore.takeSnapshot(snapshot, caller);
275+
AsyncCallFuture<SnapshotResult> future = new AsyncCallFuture<SnapshotResult>();
276+
try {
277+
CreateSnapshotContext<CommandResult> context = new CreateSnapshotContext<CommandResult>(
278+
null, volume, snapshot, future);
279+
AsyncCallbackDispatcher<AncientSnasphotStrategy, CreateCmdResult> caller = AsyncCallbackDispatcher
280+
.create(this);
281+
caller.setCallback(
282+
caller.getTarget().createSnapshotAsyncCallback(null, null))
283+
.setContext(context);
284+
PrimaryDataStoreDriver primaryStore = (PrimaryDataStoreDriver)volume.getDataStore().getDriver();
285+
primaryStore.takeSnapshot(snapshot, caller);
286+
} catch (Exception e) {
287+
s_logger.debug("Failed to take snapshot: " + snapshot.getId(), e);
288+
try {
289+
snapshot.processEvent(Snapshot.Event.OperationFailed);
290+
} catch (NoTransitionException e1) {
291+
s_logger.debug("Failed to change state for event: OperationFailed" , e);
292+
}
293+
throw new CloudRuntimeException("Failed to take snapshot" + snapshot.getId());
294+
}
295+
278296
SnapshotResult result;
297+
279298
try {
280299
result = future.get();
281300
if (result.isFailed()) {
@@ -390,6 +409,11 @@ public SnapshotInfo backupSnapshot(SnapshotInfo snapshot) {
390409
} catch (Exception e) {
391410
s_logger.debug("Failed to copy snapshot", e);
392411
result.setResult("Failed to copy snapshot:" +e.toString());
412+
try {
413+
snapObj.processEvent(Snapshot.Event.OperationFailed);
414+
} catch (NoTransitionException e1) {
415+
s_logger.debug("Failed to change state: " + e1.toString());
416+
}
393417
future.complete(result);
394418
}
395419

engine/storage/volume/src/org/apache/cloudstack/storage/datastore/driver/AncientPrimaryDataStoreDriverImpl.java

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -288,22 +288,31 @@ public boolean canCopy(DataObject srcData, DataObject destData) {
288288
@Override
289289
public void takeSnapshot(SnapshotInfo snapshot,
290290
AsyncCompletionCallback<CreateCmdResult> callback) {
291-
VolumeInfo volume = snapshot.getBaseVolume();
292-
String vmName = this.volumeMgr.getVmNameOnVolume(volume);
293-
SnapshotVO preSnapshotVO = this.snapshotMgr.getParentSnapshot(volume, snapshot);
294-
StoragePool srcPool = (StoragePool)volume.getDataStore();
295-
296-
ManageSnapshotCommand cmd = new ManageSnapshotCommand(snapshot.getId(), volume.getPath(), srcPool, preSnapshotVO.getPath(), snapshot.getName(), vmName);
297-
298-
ManageSnapshotAnswer answer = (ManageSnapshotAnswer) this.snapshotMgr.sendToPool(volume, cmd);
299-
300-
CreateCmdResult result = null;
301-
if ((answer != null) && answer.getResult()) {
302-
result = new CreateCmdResult(answer.getSnapshotPath(), null);
303-
} else {
304-
result = new CreateCmdResult(null, null);
305-
}
306-
291+
CreateCmdResult result = null;
292+
try {
293+
VolumeInfo volume = snapshot.getBaseVolume();
294+
String vmName = this.volumeMgr.getVmNameOnVolume(volume);
295+
SnapshotVO preSnapshotVO = this.snapshotMgr.getParentSnapshot(volume, snapshot);
296+
String parentSnapshotPath = null;
297+
if (preSnapshotVO != null) {
298+
parentSnapshotPath = preSnapshotVO.getPath();
299+
}
300+
StoragePool srcPool = (StoragePool)volume.getDataStore();
301+
302+
ManageSnapshotCommand cmd = new ManageSnapshotCommand(snapshot.getId(), volume.getPath(), srcPool, parentSnapshotPath, snapshot.getName(), vmName);
303+
304+
ManageSnapshotAnswer answer = (ManageSnapshotAnswer) this.snapshotMgr.sendToPool(volume, cmd);
305+
306+
if ((answer != null) && answer.getResult()) {
307+
result = new CreateCmdResult(answer.getSnapshotPath(), null);
308+
} else {
309+
result = new CreateCmdResult(null, null);
310+
}
311+
} catch (Exception e) {
312+
s_logger.debug("Failed to take snapshot: " + snapshot.getId(), e);
313+
result = new CreateCmdResult(null, null);
314+
result.setResult(e.toString());
315+
}
307316
callback.complete(result);
308317
}
309318

server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,10 @@ public Snapshot backupSnapshot(Long snapshotId) {
351351
throw new CloudRuntimeException("Can't find snapshot:" + snapshotId);
352352
}
353353

354+
if (snapshot.getState() == Snapshot.State.BackedUp) {
355+
return snapshot;
356+
}
357+
354358
SnapshotStrategy strategy = null;
355359
for (SnapshotStrategy st : snapshotStrategies) {
356360
if (st.canHandle(snapshot)) {

0 commit comments

Comments
 (0)