Search in sources :

Example 26 with VMSnapshotVO

use of com.cloud.vm.snapshot.VMSnapshotVO in project cloudstack by apache.

the class VMSnapshotDaoImpl method updateState.

@Override
public boolean updateState(State currentState, Event event, State nextState, VMSnapshot vo, Object data) {
    Long oldUpdated = vo.getUpdatedCount();
    Date oldUpdatedTime = vo.getUpdated();
    SearchCriteria<VMSnapshotVO> 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((VMSnapshotVO) vo, sc);
    if (rows == 0 && s_logger.isDebugEnabled()) {
        VMSnapshotVO 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 VM snapshot: id=" + vo.getId() + ", as there is no such snapshot exists in the database anymore");
        }
    }
    return rows > 0;
}
Also used : VMSnapshotVO(com.cloud.vm.snapshot.VMSnapshotVO) UpdateBuilder(com.cloud.utils.db.UpdateBuilder) Date(java.util.Date)

Example 27 with VMSnapshotVO

use of com.cloud.vm.snapshot.VMSnapshotVO in project cloudstack by apache.

the class ScaleIOVMSnapshotStrategy method takeVMSnapshot.

@Override
public VMSnapshot takeVMSnapshot(VMSnapshot vmSnapshot) {
    UserVm userVm = userVmDao.findById(vmSnapshot.getVmId());
    VMSnapshotVO vmSnapshotVO = (VMSnapshotVO) vmSnapshot;
    try {
        vmSnapshotHelper.vmSnapshotStateTransitTo(vmSnapshotVO, VMSnapshot.Event.CreateRequested);
    } catch (NoTransitionException e) {
        throw new CloudRuntimeException(e.getMessage());
    }
    boolean result = false;
    try {
        Map<String, String> srcVolumeDestSnapshotMap = new HashMap<>();
        List<VolumeObjectTO> volumeTOs = vmSnapshotHelper.getVolumeTOList(userVm.getId());
        final Long storagePoolId = vmSnapshotHelper.getStoragePoolForVM(userVm.getId());
        StoragePoolVO storagePool = storagePoolDao.findById(storagePoolId);
        long prev_chain_size = 0;
        long virtual_size = 0;
        for (VolumeObjectTO volume : volumeTOs) {
            String volumeSnapshotName = String.format("%s-%s-%s-%s-%s", ScaleIOUtil.VMSNAPSHOT_PREFIX, vmSnapshotVO.getId(), volume.getId(), storagePool.getUuid().split("-")[0].substring(4), ManagementServerImpl.customCsIdentifier.value());
            srcVolumeDestSnapshotMap.put(ScaleIOUtil.getVolumePath(volume.getPath()), volumeSnapshotName);
            virtual_size += volume.getSize();
            VolumeVO volumeVO = volumeDao.findById(volume.getId());
            prev_chain_size += volumeVO.getVmSnapshotChainSize() == null ? 0 : volumeVO.getVmSnapshotChainSize();
        }
        VMSnapshotTO current = null;
        VMSnapshotVO currentSnapshot = vmSnapshotDao.findCurrentSnapshotByVmId(userVm.getId());
        if (currentSnapshot != null) {
            current = vmSnapshotHelper.getSnapshotWithParents(currentSnapshot);
        }
        if (current == null)
            vmSnapshotVO.setParent(null);
        else
            vmSnapshotVO.setParent(current.getId());
        try {
            final ScaleIOGatewayClient client = getScaleIOClient(storagePoolId);
            SnapshotGroup snapshotGroup = client.takeSnapshot(srcVolumeDestSnapshotMap);
            if (snapshotGroup == null) {
                throw new CloudRuntimeException("Failed to take VM snapshot on PowerFlex storage pool");
            }
            String snapshotGroupId = snapshotGroup.getSnapshotGroupId();
            List<String> volumeIds = snapshotGroup.getVolumeIds();
            if (volumeIds != null && !volumeIds.isEmpty()) {
                List<VMSnapshotDetailsVO> vmSnapshotDetails = new ArrayList<VMSnapshotDetailsVO>();
                vmSnapshotDetails.add(new VMSnapshotDetailsVO(vmSnapshot.getId(), "SnapshotGroupId", snapshotGroupId, false));
                for (int index = 0; index < volumeIds.size(); index++) {
                    String volumeSnapshotName = srcVolumeDestSnapshotMap.get(ScaleIOUtil.getVolumePath(volumeTOs.get(index).getPath()));
                    String pathWithScaleIOVolumeName = ScaleIOUtil.updatedPathWithVolumeName(volumeIds.get(index), volumeSnapshotName);
                    vmSnapshotDetails.add(new VMSnapshotDetailsVO(vmSnapshot.getId(), "Vol_" + volumeTOs.get(index).getId() + "_Snapshot", pathWithScaleIOVolumeName, false));
                }
                vmSnapshotDetailsDao.saveDetails(vmSnapshotDetails);
            }
            finalizeCreate(vmSnapshotVO, volumeTOs);
            result = true;
            LOGGER.debug("Create vm snapshot " + vmSnapshot.getName() + " succeeded for vm: " + userVm.getInstanceName());
            long new_chain_size = 0;
            for (VolumeObjectTO volumeTo : volumeTOs) {
                publishUsageEvent(EventTypes.EVENT_VM_SNAPSHOT_CREATE, vmSnapshot, userVm, volumeTo);
                new_chain_size += volumeTo.getSize();
            }
            publishUsageEvent(EventTypes.EVENT_VM_SNAPSHOT_ON_PRIMARY, vmSnapshot, userVm, new_chain_size - prev_chain_size, virtual_size);
            return vmSnapshot;
        } catch (Exception e) {
            String errMsg = "Unable to take vm snapshot due to: " + e.getMessage();
            LOGGER.warn(errMsg, e);
            throw new CloudRuntimeException(errMsg);
        }
    } finally {
        if (!result) {
            try {
                vmSnapshotHelper.vmSnapshotStateTransitTo(vmSnapshot, VMSnapshot.Event.OperationFailed);
                String subject = "Take snapshot failed for VM: " + userVm.getDisplayName();
                String message = "Snapshot operation failed for VM: " + userVm.getDisplayName() + ", Please check and delete if any stale volumes created with VM snapshot id: " + vmSnapshot.getVmId();
                alertManager.sendAlert(AlertManager.AlertType.ALERT_TYPE_VM_SNAPSHOT, userVm.getDataCenterId(), userVm.getPodIdToDeployIn(), subject, message);
            } catch (NoTransitionException e1) {
                LOGGER.error("Cannot set vm snapshot state due to: " + e1.getMessage());
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SnapshotGroup(org.apache.cloudstack.storage.datastore.api.SnapshotGroup) ConfigurationException(javax.naming.ConfigurationException) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VMSnapshotVO(com.cloud.vm.snapshot.VMSnapshotVO) UserVm(com.cloud.uservm.UserVm) VMSnapshotTO(com.cloud.agent.api.VMSnapshotTO) VolumeVO(com.cloud.storage.VolumeVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) ScaleIOGatewayClient(org.apache.cloudstack.storage.datastore.client.ScaleIOGatewayClient) StoragePoolVO(org.apache.cloudstack.storage.datastore.db.StoragePoolVO) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) VMSnapshotDetailsVO(com.cloud.vm.snapshot.VMSnapshotDetailsVO)

Example 28 with VMSnapshotVO

use of com.cloud.vm.snapshot.VMSnapshotVO in project cloudstack by apache.

the class VMSnapshotStrategyTest method testRevertSnapshot.

@Test
public void testRevertSnapshot() throws AgentUnavailableException, OperationTimedoutException {
    Long hostId = 1L;
    Long vmId = 1L;
    Long guestOsId = 1L;
    HypervisorType hypervisorType = HypervisorType.Any;
    String hypervisorVersion = "default";
    String guestOsName = "Other";
    List<VolumeObjectTO> volumeObjectTOs = new ArrayList<VolumeObjectTO>();
    VMSnapshotVO vmSnapshot = Mockito.mock(VMSnapshotVO.class);
    UserVmVO userVmVO = Mockito.mock(UserVmVO.class);
    Mockito.when(userVmVO.getGuestOSId()).thenReturn(guestOsId);
    Mockito.when(vmSnapshot.getVmId()).thenReturn(vmId);
    Mockito.when(vmSnapshotHelper.pickRunningHost(Matchers.anyLong())).thenReturn(hostId);
    Mockito.when(vmSnapshotHelper.getVolumeTOList(Matchers.anyLong())).thenReturn(volumeObjectTOs);
    Mockito.when(userVmDao.findById(Matchers.anyLong())).thenReturn(userVmVO);
    GuestOSVO guestOSVO = Mockito.mock(GuestOSVO.class);
    Mockito.when(guestOSDao.findById(Matchers.anyLong())).thenReturn(guestOSVO);
    GuestOSHypervisorVO guestOSHypervisorVO = Mockito.mock(GuestOSHypervisorVO.class);
    Mockito.when(guestOSHypervisorVO.getGuestOsName()).thenReturn(guestOsName);
    Mockito.when(guestOsHypervisorDao.findById(Matchers.anyLong())).thenReturn(guestOSHypervisorVO);
    Mockito.when(guestOsHypervisorDao.findByOsIdAndHypervisor(Matchers.anyLong(), Matchers.anyString(), Matchers.anyString())).thenReturn(guestOSHypervisorVO);
    VMSnapshotTO vmSnapshotTO = Mockito.mock(VMSnapshotTO.class);
    Mockito.when(vmSnapshotHelper.getSnapshotWithParents(Matchers.any(VMSnapshotVO.class))).thenReturn(vmSnapshotTO);
    Mockito.when(vmSnapshotDao.findById(Matchers.anyLong())).thenReturn(vmSnapshot);
    Mockito.when(vmSnapshot.getId()).thenReturn(1L);
    Mockito.when(vmSnapshot.getCreated()).thenReturn(new Date());
    Mockito.when(agentMgr.send(Matchers.anyLong(), Matchers.any(Command.class))).thenReturn(null);
    HostVO hostVO = Mockito.mock(HostVO.class);
    Mockito.when(hostDao.findById(Matchers.anyLong())).thenReturn(hostVO);
    Mockito.when(hostVO.getHypervisorType()).thenReturn(hypervisorType);
    Mockito.when(hostVO.getHypervisorVersion()).thenReturn(hypervisorVersion);
    Exception e = null;
    try {
        vmSnapshotStrategy.revertVMSnapshot(vmSnapshot);
    } catch (CloudRuntimeException e1) {
        e = e1;
    }
    assertNotNull(e);
    RevertToVMSnapshotAnswer answer = Mockito.mock(RevertToVMSnapshotAnswer.class);
    Mockito.when(answer.getResult()).thenReturn(Boolean.TRUE);
    Mockito.when(agentMgr.send(Matchers.anyLong(), Matchers.any(Command.class))).thenReturn(answer);
    boolean result = vmSnapshotStrategy.revertVMSnapshot(vmSnapshot);
    assertTrue(result);
}
Also used : UserVmVO(com.cloud.vm.UserVmVO) RevertToVMSnapshotAnswer(com.cloud.agent.api.RevertToVMSnapshotAnswer) ArrayList(java.util.ArrayList) GuestOSVO(com.cloud.storage.GuestOSVO) Date(java.util.Date) HostVO(com.cloud.host.HostVO) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) OperationTimedoutException(com.cloud.exception.OperationTimedoutException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) HypervisorType(com.cloud.hypervisor.Hypervisor.HypervisorType) GuestOSHypervisorVO(com.cloud.storage.GuestOSHypervisorVO) VMSnapshotVO(com.cloud.vm.snapshot.VMSnapshotVO) VMSnapshotTO(com.cloud.agent.api.VMSnapshotTO) Command(com.cloud.agent.api.Command) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) Test(org.junit.Test)

Example 29 with VMSnapshotVO

use of com.cloud.vm.snapshot.VMSnapshotVO in project cloudstack by apache.

the class VMSnapshotStrategyTest method testCreateVMSnapshot.

@Test
public void testCreateVMSnapshot() throws AgentUnavailableException, OperationTimedoutException {
    Long hostId = 1L;
    Long vmId = 1L;
    Long guestOsId = 1L;
    HypervisorType hypervisorType = HypervisorType.Any;
    String hypervisorVersion = "default";
    String guestOsName = "Other";
    List<VolumeObjectTO> volumeObjectTOs = new ArrayList<VolumeObjectTO>();
    VMSnapshotVO vmSnapshot = Mockito.mock(VMSnapshotVO.class);
    UserVmVO userVmVO = Mockito.mock(UserVmVO.class);
    Mockito.when(userVmVO.getGuestOSId()).thenReturn(guestOsId);
    Mockito.when(vmSnapshot.getVmId()).thenReturn(vmId);
    Mockito.when(vmSnapshotHelper.pickRunningHost(Matchers.anyLong())).thenReturn(hostId);
    Mockito.when(vmSnapshotHelper.getVolumeTOList(Matchers.anyLong())).thenReturn(volumeObjectTOs);
    Mockito.when(userVmDao.findById(Matchers.anyLong())).thenReturn(userVmVO);
    GuestOSVO guestOSVO = Mockito.mock(GuestOSVO.class);
    Mockito.when(guestOSDao.findById(Matchers.anyLong())).thenReturn(guestOSVO);
    GuestOSHypervisorVO guestOSHypervisorVO = Mockito.mock(GuestOSHypervisorVO.class);
    Mockito.when(guestOSHypervisorVO.getGuestOsName()).thenReturn(guestOsName);
    Mockito.when(guestOsHypervisorDao.findById(Matchers.anyLong())).thenReturn(guestOSHypervisorVO);
    Mockito.when(guestOsHypervisorDao.findByOsIdAndHypervisor(Matchers.anyLong(), Matchers.anyString(), Matchers.anyString())).thenReturn(guestOSHypervisorVO);
    Mockito.when(agentMgr.send(Matchers.anyLong(), Matchers.any(Command.class))).thenReturn(null);
    HostVO hostVO = Mockito.mock(HostVO.class);
    Mockito.when(hostDao.findById(Matchers.anyLong())).thenReturn(hostVO);
    Mockito.when(hostVO.getHypervisorType()).thenReturn(hypervisorType);
    Mockito.when(hostVO.getHypervisorVersion()).thenReturn(hypervisorVersion);
    Exception e = null;
    try {
        vmSnapshotStrategy.takeVMSnapshot(vmSnapshot);
    } catch (CloudRuntimeException e1) {
        e = e1;
    }
    assertNotNull(e);
    CreateVMSnapshotAnswer answer = Mockito.mock(CreateVMSnapshotAnswer.class);
    Mockito.when(answer.getResult()).thenReturn(true);
    Mockito.when(agentMgr.send(Matchers.anyLong(), Matchers.any(Command.class))).thenReturn(answer);
    Mockito.when(vmSnapshotDao.findById(Matchers.anyLong())).thenReturn(vmSnapshot);
    VMSnapshot snapshot = null;
    snapshot = vmSnapshotStrategy.takeVMSnapshot(vmSnapshot);
    assertNotNull(snapshot);
}
Also used : UserVmVO(com.cloud.vm.UserVmVO) ArrayList(java.util.ArrayList) GuestOSVO(com.cloud.storage.GuestOSVO) VMSnapshot(com.cloud.vm.snapshot.VMSnapshot) HostVO(com.cloud.host.HostVO) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) OperationTimedoutException(com.cloud.exception.OperationTimedoutException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) HypervisorType(com.cloud.hypervisor.Hypervisor.HypervisorType) GuestOSHypervisorVO(com.cloud.storage.GuestOSHypervisorVO) VMSnapshotVO(com.cloud.vm.snapshot.VMSnapshotVO) CreateVMSnapshotAnswer(com.cloud.agent.api.CreateVMSnapshotAnswer) Command(com.cloud.agent.api.Command) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) Test(org.junit.Test)

Example 30 with VMSnapshotVO

use of com.cloud.vm.snapshot.VMSnapshotVO in project cloudstack by apache.

the class DefaultVMSnapshotStrategy method finalizeRevert.

protected void finalizeRevert(VMSnapshotVO vmSnapshot, List<VolumeObjectTO> volumeToList) {
    // update volumes path
    updateVolumePath(volumeToList);
    // update current snapshot, current snapshot is the one reverted to
    VMSnapshotVO previousCurrent = vmSnapshotDao.findCurrentSnapshotByVmId(vmSnapshot.getVmId());
    if (previousCurrent != null) {
        previousCurrent.setCurrent(false);
        vmSnapshotDao.persist(previousCurrent);
    }
    vmSnapshot.setCurrent(true);
    vmSnapshotDao.persist(vmSnapshot);
}
Also used : VMSnapshotVO(com.cloud.vm.snapshot.VMSnapshotVO)

Aggregations

VMSnapshotVO (com.cloud.vm.snapshot.VMSnapshotVO)58 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)35 UserVmVO (com.cloud.vm.UserVmVO)27 AgentUnavailableException (com.cloud.exception.AgentUnavailableException)20 OperationTimedoutException (com.cloud.exception.OperationTimedoutException)20 Account (com.cloud.user.Account)17 HostVO (com.cloud.host.HostVO)16 VMSnapshotTO (com.cloud.agent.api.VMSnapshotTO)15 GuestOSVO (com.cloud.storage.GuestOSVO)15 Test (org.junit.Test)14 HypervisorType (com.cloud.hypervisor.Hypervisor.HypervisorType)13 GuestOSHypervisorVO (com.cloud.storage.GuestOSHypervisorVO)13 NoTransitionException (com.cloud.utils.fsm.NoTransitionException)13 ArrayList (java.util.ArrayList)13 VolumeObjectTO (org.apache.cloudstack.storage.to.VolumeObjectTO)12 VolumeVO (com.cloud.storage.VolumeVO)11 ConfigurationException (javax.naming.ConfigurationException)11 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)10 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)10 IOException (java.io.IOException)10