Search in sources :

Example 31 with SnapshotInfo

use of org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo in project cloudstack by apache.

the class XenserverSnapshotStrategy method deleteSnapshotChain.

protected boolean deleteSnapshotChain(SnapshotInfo snapshot) {
    s_logger.debug("delete snapshot chain for snapshot: " + snapshot.getId());
    boolean result = false;
    //need to track, the snapshot itself is deleted or not.
    boolean resultIsSet = false;
    try {
        while (snapshot != null && (snapshot.getState() == Snapshot.State.Destroying || snapshot.getState() == Snapshot.State.Destroyed || snapshot.getState() == Snapshot.State.Error)) {
            SnapshotInfo child = snapshot.getChild();
            if (child != null) {
                s_logger.debug("the snapshot has child, can't delete it on the storage");
                break;
            }
            s_logger.debug("Snapshot: " + snapshot.getId() + " doesn't have children, so it's ok to delete it and its parents");
            SnapshotInfo parent = snapshot.getParent();
            boolean deleted = false;
            if (parent != null) {
                if (parent.getPath() != null && parent.getPath().equalsIgnoreCase(snapshot.getPath())) {
                    //NOTE: if both snapshots share the same path, it's for xenserver's empty delta snapshot. We can't delete the snapshot on the backend, as parent snapshot still reference to it
                    //Instead, mark it as destroyed in the db.
                    s_logger.debug("for empty delta snapshot, only mark it as destroyed in db");
                    snapshot.processEvent(Event.DestroyRequested);
                    snapshot.processEvent(Event.OperationSuccessed);
                    deleted = true;
                    if (!resultIsSet) {
                        result = true;
                        resultIsSet = true;
                    }
                }
            }
            if (!deleted) {
                boolean r = snapshotSvr.deleteSnapshot(snapshot);
                if (r) {
                    // delete snapshot in cache if there is
                    List<SnapshotInfo> cacheSnaps = snapshotDataFactory.listSnapshotOnCache(snapshot.getId());
                    for (SnapshotInfo cacheSnap : cacheSnaps) {
                        s_logger.debug("Delete snapshot " + snapshot.getId() + " from image cache store: " + cacheSnap.getDataStore().getName());
                        cacheSnap.delete();
                    }
                }
                if (!resultIsSet) {
                    result = r;
                    resultIsSet = true;
                }
            }
            snapshot = parent;
        }
    } catch (Exception e) {
        s_logger.debug("delete snapshot failed: ", e);
    }
    return result;
}
Also used : SnapshotInfo(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Example 32 with SnapshotInfo

use of org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo in project cloudstack by apache.

the class DefaultEndPointSelector method select.

@Override
public EndPoint select(DataObject object, StorageAction action) {
    if (action == StorageAction.TAKESNAPSHOT) {
        SnapshotInfo snapshotInfo = (SnapshotInfo) object;
        if (snapshotInfo.getHypervisorType() == Hypervisor.HypervisorType.KVM) {
            VolumeInfo volumeInfo = snapshotInfo.getBaseVolume();
            VirtualMachine vm = volumeInfo.getAttachedVM();
            if ((vm != null) && (vm.getState() == VirtualMachine.State.Running)) {
                Long hostId = vm.getHostId();
                return getEndPointFromHostId(hostId);
            }
        }
    } else if (action == StorageAction.MIGRATEVOLUME) {
        VolumeInfo volume = (VolumeInfo) object;
        if (volume.getHypervisorType() == Hypervisor.HypervisorType.Hyperv || volume.getHypervisorType() == Hypervisor.HypervisorType.VMware) {
            VirtualMachine vm = volume.getAttachedVM();
            if ((vm != null) && (vm.getState() == VirtualMachine.State.Running)) {
                Long hostId = vm.getHostId();
                return getEndPointFromHostId(hostId);
            }
        }
    } else if (action == StorageAction.DELETEVOLUME) {
        VolumeInfo volume = (VolumeInfo) object;
        if (volume.getHypervisorType() == Hypervisor.HypervisorType.VMware) {
            VirtualMachine vm = volume.getAttachedVM();
            if (vm != null) {
                Long hostId = vm.getHostId() != null ? vm.getHostId() : vm.getLastHostId();
                if (hostId != null) {
                    return getEndPointFromHostId(hostId);
                }
            }
        }
    }
    return select(object);
}
Also used : SnapshotInfo(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo) VolumeInfo(org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo) VirtualMachine(com.cloud.vm.VirtualMachine)

Example 33 with SnapshotInfo

use of org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo in project cloudstack by apache.

the class SnapshotTestWithFakeData method testTakeSnapshotFromVolume.

//@Test
public void testTakeSnapshotFromVolume() throws URISyntaxException {
    DataStore store = createDataStore();
    FakePrimaryDataStoreDriver dataStoreDriver = (FakePrimaryDataStoreDriver) store.getDriver();
    dataStoreDriver.makeTakeSnapshotSucceed(false);
    VolumeInfo volumeInfo = createVolume(1L, store);
    Assert.assertTrue(volumeInfo.getState() == Volume.State.Ready);
    SnapshotInfo result = volumeService.takeSnapshot(volumeInfo);
    Assert.assertTrue(volumeInfo.getState() == Volume.State.Ready);
    Assert.assertTrue(result == null);
}
Also used : SnapshotInfo(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) VolumeInfo(org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo)

Example 34 with SnapshotInfo

use of org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo in project cloudstack by apache.

the class SnapshotTest method deleteSnapshot.

@Test
public void deleteSnapshot() throws InterruptedException, ExecutionException {
    VolumeInfo vol = createCopyBaseImage();
    SnapshotVO snapshotVO = createSnapshotInDb(vol);
    SnapshotInfo snapshot = this.snapshotFactory.getSnapshot(snapshotVO.getId(), vol.getDataStore());
    SnapshotInfo newSnapshot = null;
    SnapshotStrategy snapshotStrategy = storageStrategyFactory.getSnapshotStrategy(snapshot, SnapshotOperation.TAKE);
    if (snapshotStrategy != null) {
        newSnapshot = snapshotStrategy.takeSnapshot(snapshot);
    }
    AssertJUnit.assertNotNull(newSnapshot);
    // create another snapshot
    for (SnapshotStrategy strategy : this.snapshotStrategies) {
        if (strategy.canHandle(snapshot, SnapshotOperation.DELETE) != StrategyPriority.CANT_HANDLE) {
            strategy.deleteSnapshot(newSnapshot.getId());
        }
    }
}
Also used : SnapshotInfo(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo) SnapshotVO(com.cloud.storage.SnapshotVO) VolumeInfo(org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo) SnapshotStrategy(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotStrategy) Test(org.testng.annotations.Test)

Example 35 with SnapshotInfo

use of org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo in project cloudstack by apache.

the class SnapshotTest method createTemplateFromSnapshot.

@Test
public void createTemplateFromSnapshot() throws InterruptedException, ExecutionException {
    VolumeInfo vol = createCopyBaseImage();
    SnapshotVO snapshotVO = createSnapshotInDb(vol);
    SnapshotInfo snapshot = this.snapshotFactory.getSnapshot(snapshotVO.getId(), vol.getDataStore());
    boolean result = false;
    SnapshotStrategy snapshotStrategy = storageStrategyFactory.getSnapshotStrategy(snapshot, SnapshotOperation.TAKE);
    if (snapshotStrategy != null) {
        snapshot = snapshotStrategy.takeSnapshot(snapshot);
        result = true;
    }
    AssertJUnit.assertTrue(result);
    LocalHostEndpoint ep = new LocalHostEndpoint();
    ep.setResource(new MockLocalNfsSecondaryStorageResource());
    Mockito.when(epSelector.select(Matchers.any(DataObject.class), Matchers.any(DataObject.class))).thenReturn(ep);
    try {
        VMTemplateVO templateVO = createTemplateInDb();
        TemplateInfo tmpl = this.templateFactory.getTemplate(templateVO.getId(), DataStoreRole.Image);
        DataStore imageStore = this.dataStoreMgr.getImageStore(this.dcId);
        AsyncCallFuture<TemplateApiResult> templateFuture = this.imageService.createTemplateFromSnapshotAsync(snapshot, tmpl, imageStore);
        TemplateApiResult apiResult = templateFuture.get();
        Assert.assertTrue(apiResult.isSuccess());
    } finally {
        Mockito.when(epSelector.select(Matchers.any(DataObject.class), Matchers.any(DataObject.class))).thenReturn(remoteEp);
    }
}
Also used : MockLocalNfsSecondaryStorageResource(org.apache.cloudstack.storage.MockLocalNfsSecondaryStorageResource) VMTemplateVO(com.cloud.storage.VMTemplateVO) VolumeInfo(org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo) TemplateApiResult(org.apache.cloudstack.engine.subsystem.api.storage.TemplateService.TemplateApiResult) LocalHostEndpoint(org.apache.cloudstack.storage.LocalHostEndpoint) SnapshotInfo(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo) TemplateInfo(org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo) SnapshotVO(com.cloud.storage.SnapshotVO) DataObject(org.apache.cloudstack.engine.subsystem.api.storage.DataObject) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) SnapshotStrategy(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotStrategy) Test(org.testng.annotations.Test)

Aggregations

SnapshotInfo (org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo)40 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)23 VolumeInfo (org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo)17 DataStore (org.apache.cloudstack.engine.subsystem.api.storage.DataStore)16 SnapshotVO (com.cloud.storage.SnapshotVO)15 NoTransitionException (com.cloud.utils.fsm.NoTransitionException)11 SnapshotResult (org.apache.cloudstack.engine.subsystem.api.storage.SnapshotResult)11 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)10 ExecutionException (java.util.concurrent.ExecutionException)10 SnapshotDataStoreVO (org.apache.cloudstack.storage.datastore.db.SnapshotDataStoreVO)10 SnapshotStrategy (org.apache.cloudstack.engine.subsystem.api.storage.SnapshotStrategy)9 DB (com.cloud.utils.db.DB)8 CopyCommandResult (org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult)8 VolumeVO (com.cloud.storage.VolumeVO)7 TemplateInfo (org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo)6 CommandResult (org.apache.cloudstack.storage.command.CommandResult)6 StorageUnavailableException (com.cloud.exception.StorageUnavailableException)5 Answer (com.cloud.agent.api.Answer)4 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)4 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)4