Search in sources :

Example 11 with CreateObjectAnswer

use of org.apache.cloudstack.storage.command.CreateObjectAnswer in project cloudstack by apache.

the class VolumeObject method processEventOnly.

@Override
public void processEventOnly(ObjectInDataStoreStateMachine.Event event, Answer answer) {
    try {
        if (dataStore.getRole() == DataStoreRole.Primary) {
            if (answer instanceof CopyCmdAnswer) {
                CopyCmdAnswer cpyAnswer = (CopyCmdAnswer) answer;
                VolumeVO vol = volumeDao.findById(getId());
                VolumeObjectTO newVol = (VolumeObjectTO) cpyAnswer.getNewData();
                vol.setPath(newVol.getPath());
                if (newVol.getSize() != null) {
                    vol.setSize(newVol.getSize());
                }
                vol.setPoolId(getDataStore().getId());
                volumeDao.update(vol.getId(), vol);
            } else if (answer instanceof CreateObjectAnswer) {
                CreateObjectAnswer createAnswer = (CreateObjectAnswer) answer;
                VolumeObjectTO newVol = (VolumeObjectTO) createAnswer.getData();
                VolumeVO vol = volumeDao.findById(getId());
                vol.setPath(newVol.getPath());
                if (newVol.getSize() != null) {
                    vol.setSize(newVol.getSize());
                }
                vol.setPoolId(getDataStore().getId());
                volumeDao.update(vol.getId(), vol);
            }
        } else {
            // image store or imageCache store
            if (answer instanceof DownloadAnswer) {
                DownloadAnswer dwdAnswer = (DownloadAnswer) answer;
                VolumeDataStoreVO volStore = volumeStoreDao.findByStoreVolume(dataStore.getId(), getId());
                volStore.setInstallPath(dwdAnswer.getInstallPath());
                volStore.setChecksum(dwdAnswer.getCheckSum());
                volumeStoreDao.update(volStore.getId(), volStore);
            } else if (answer instanceof CopyCmdAnswer) {
                CopyCmdAnswer cpyAnswer = (CopyCmdAnswer) answer;
                VolumeDataStoreVO volStore = volumeStoreDao.findByStoreVolume(dataStore.getId(), getId());
                VolumeObjectTO newVol = (VolumeObjectTO) cpyAnswer.getNewData();
                volStore.setInstallPath(newVol.getPath());
                if (newVol.getSize() != null) {
                    volStore.setSize(newVol.getSize());
                }
                volumeStoreDao.update(volStore.getId(), volStore);
            }
        }
    } catch (RuntimeException ex) {
        if (event == ObjectInDataStoreStateMachine.Event.OperationFailed) {
            objectInStoreMgr.deleteIfNotReady(this);
        }
        throw ex;
    }
    this.processEventOnly(event);
}
Also used : CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VolumeVO(com.cloud.storage.VolumeVO) CreateObjectAnswer(org.apache.cloudstack.storage.command.CreateObjectAnswer) VolumeDataStoreVO(org.apache.cloudstack.storage.datastore.db.VolumeDataStoreVO) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) DownloadAnswer(com.cloud.agent.api.storage.DownloadAnswer) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer)

Example 12 with CreateObjectAnswer

use of org.apache.cloudstack.storage.command.CreateObjectAnswer in project cloudstack by apache.

the class VolumeObject method processEvent.

@Override
public void processEvent(ObjectInDataStoreStateMachine.Event event, Answer answer) {
    try {
        if (dataStore.getRole() == DataStoreRole.Primary) {
            if (answer instanceof CopyCmdAnswer) {
                CopyCmdAnswer cpyAnswer = (CopyCmdAnswer) answer;
                VolumeVO vol = volumeDao.findById(getId());
                VolumeObjectTO newVol = (VolumeObjectTO) cpyAnswer.getNewData();
                vol.setPath(newVol.getPath());
                if (newVol.getSize() != null) {
                    vol.setSize(newVol.getSize());
                }
                if (newVol.getFormat() != null) {
                    vol.setFormat(newVol.getFormat());
                }
                vol.setPoolId(getDataStore().getId());
                volumeDao.update(vol.getId(), vol);
            } else if (answer instanceof CreateObjectAnswer) {
                CreateObjectAnswer createAnswer = (CreateObjectAnswer) answer;
                VolumeObjectTO newVol = (VolumeObjectTO) createAnswer.getData();
                VolumeVO vol = volumeDao.findById(getId());
                vol.setPath(newVol.getPath());
                if (newVol.getSize() != null) {
                    vol.setSize(newVol.getSize());
                }
                vol.setPoolId(getDataStore().getId());
                if (newVol.getFormat() != null) {
                    vol.setFormat(newVol.getFormat());
                }
                volumeDao.update(vol.getId(), vol);
            }
        } else {
            // image store or imageCache store
            if (answer instanceof DownloadAnswer) {
                DownloadAnswer dwdAnswer = (DownloadAnswer) answer;
                VolumeDataStoreVO volStore = volumeStoreDao.findByStoreVolume(dataStore.getId(), getId());
                volStore.setInstallPath(dwdAnswer.getInstallPath());
                volStore.setChecksum(dwdAnswer.getCheckSum());
                volumeStoreDao.update(volStore.getId(), volStore);
            } else if (answer instanceof CopyCmdAnswer) {
                CopyCmdAnswer cpyAnswer = (CopyCmdAnswer) answer;
                VolumeDataStoreVO volStore = volumeStoreDao.findByStoreVolume(dataStore.getId(), getId());
                VolumeObjectTO newVol = (VolumeObjectTO) cpyAnswer.getNewData();
                volStore.setInstallPath(newVol.getPath());
                if (newVol.getSize() != null) {
                    volStore.setSize(newVol.getSize());
                }
                volumeStoreDao.update(volStore.getId(), volStore);
            }
        }
    } catch (RuntimeException ex) {
        if (event == ObjectInDataStoreStateMachine.Event.OperationFailed) {
            objectInStoreMgr.deleteIfNotReady(this);
        }
        throw ex;
    }
    this.processEvent(event);
}
Also used : CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VolumeVO(com.cloud.storage.VolumeVO) CreateObjectAnswer(org.apache.cloudstack.storage.command.CreateObjectAnswer) VolumeDataStoreVO(org.apache.cloudstack.storage.datastore.db.VolumeDataStoreVO) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) DownloadAnswer(com.cloud.agent.api.storage.DownloadAnswer) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer)

Example 13 with CreateObjectAnswer

use of org.apache.cloudstack.storage.command.CreateObjectAnswer in project cloudstack by apache.

the class XenserverSnapshotStrategy method backupSnapshot.

@Override
public SnapshotInfo backupSnapshot(SnapshotInfo snapshot) {
    SnapshotInfo parentSnapshot = snapshot.getParent();
    if (parentSnapshot != null && snapshot.getPath().equalsIgnoreCase(parentSnapshot.getPath())) {
        s_logger.debug("backup an empty snapshot");
        // don't need to backup this snapshot
        SnapshotDataStoreVO parentSnapshotOnBackupStore = snapshotStoreDao.findBySnapshot(parentSnapshot.getId(), DataStoreRole.Image);
        if (parentSnapshotOnBackupStore != null && parentSnapshotOnBackupStore.getState() == State.Ready) {
            DataStore store = dataStoreMgr.getDataStore(parentSnapshotOnBackupStore.getDataStoreId(), parentSnapshotOnBackupStore.getRole());
            SnapshotInfo snapshotOnImageStore = (SnapshotInfo) store.create(snapshot);
            snapshotOnImageStore.processEvent(Event.CreateOnlyRequested);
            SnapshotObjectTO snapTO = new SnapshotObjectTO();
            snapTO.setPath(parentSnapshotOnBackupStore.getInstallPath());
            CreateObjectAnswer createSnapshotAnswer = new CreateObjectAnswer(snapTO);
            snapshotOnImageStore.processEvent(Event.OperationSuccessed, createSnapshotAnswer);
            SnapshotObject snapObj = (SnapshotObject) snapshot;
            try {
                snapObj.processEvent(Snapshot.Event.OperationNotPerformed);
            } catch (NoTransitionException e) {
                s_logger.debug("Failed to change state: " + snapshot.getId() + ": " + e.toString());
                throw new CloudRuntimeException(e.toString());
            }
            return snapshotDataFactory.getSnapshot(snapObj.getId(), store);
        } else {
            s_logger.debug("parent snapshot hasn't been backed up yet");
        }
    }
    // determine full snapshot backup or not
    boolean fullBackup = true;
    SnapshotDataStoreVO parentSnapshotOnBackupStore = snapshotStoreDao.findLatestSnapshotForVolume(snapshot.getVolumeId(), DataStoreRole.Image);
    SnapshotDataStoreVO parentSnapshotOnPrimaryStore = snapshotStoreDao.findLatestSnapshotForVolume(snapshot.getVolumeId(), DataStoreRole.Primary);
    HypervisorType hypervisorType = snapshot.getBaseVolume().getHypervisorType();
    if (parentSnapshotOnPrimaryStore != null && parentSnapshotOnBackupStore != null && hypervisorType == Hypervisor.HypervisorType.XenServer) {
        // CS does incremental backup only for XenServer
        // In case of volume migration from one pool to other pool, CS should take full snapshot to avoid any issues with delta chain,
        // to check if this is a migrated volume, compare the current pool id of volume and store_id of oldest snapshot on primary for this volume.
        // Why oldest? Because at this point CS has two snapshot on primary entries for same volume, one with old pool_id and other one with
        // current pool id. So, verify and if volume found to be migrated, delete snapshot entry with previous pool store_id.
        SnapshotDataStoreVO oldestSnapshotOnPrimary = snapshotStoreDao.findOldestSnapshotForVolume(snapshot.getVolumeId(), DataStoreRole.Primary);
        VolumeVO volume = volumeDao.findById(snapshot.getVolumeId());
        if (oldestSnapshotOnPrimary != null) {
            if (oldestSnapshotOnPrimary.getDataStoreId() == volume.getPoolId() && oldestSnapshotOnPrimary.getId() != parentSnapshotOnPrimaryStore.getId()) {
                int _deltaSnapshotMax = NumbersUtil.parseInt(configDao.getValue("snapshot.delta.max"), SnapshotManager.DELTAMAX);
                int deltaSnap = _deltaSnapshotMax;
                int i;
                for (i = 1; i < deltaSnap; i++) {
                    Long prevBackupId = parentSnapshotOnBackupStore.getParentSnapshotId();
                    if (prevBackupId == 0) {
                        break;
                    }
                    parentSnapshotOnBackupStore = snapshotStoreDao.findBySnapshot(prevBackupId, DataStoreRole.Image);
                    if (parentSnapshotOnBackupStore == null) {
                        break;
                    }
                }
                if (i >= deltaSnap) {
                    fullBackup = true;
                } else {
                    fullBackup = false;
                }
            } else if (oldestSnapshotOnPrimary.getId() != parentSnapshotOnPrimaryStore.getId()) {
                // if there is an snapshot entry for previousPool(primary storage) of migrated volume, delete it becasue CS created one more snapshot entry for current pool
                snapshotStoreDao.remove(oldestSnapshotOnPrimary.getId());
            }
        }
    }
    snapshot.addPayload(fullBackup);
    return snapshotSvr.backupSnapshot(snapshot);
}
Also used : SnapshotObjectTO(org.apache.cloudstack.storage.to.SnapshotObjectTO) SnapshotDataStoreVO(org.apache.cloudstack.storage.datastore.db.SnapshotDataStoreVO) CreateObjectAnswer(org.apache.cloudstack.storage.command.CreateObjectAnswer) HypervisorType(com.cloud.hypervisor.Hypervisor.HypervisorType) SnapshotInfo(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo) VolumeVO(com.cloud.storage.VolumeVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) NoTransitionException(com.cloud.utils.fsm.NoTransitionException)

Example 14 with CreateObjectAnswer

use of org.apache.cloudstack.storage.command.CreateObjectAnswer in project cloudstack by apache.

the class FakePrimaryDataStoreDriver method takeSnapshot.

@Override
public void takeSnapshot(SnapshotInfo snapshot, AsyncCompletionCallback<CreateCmdResult> callback) {
    CreateCmdResult result = new CreateCmdResult(null, null);
    if (snapshotResult) {
        SnapshotObjectTO newSnap = new SnapshotObjectTO();
        newSnap.setPath(UUID.randomUUID().toString());
        CreateObjectAnswer answer = new CreateObjectAnswer(newSnap);
        result.setAnswer(answer);
    } else {
        result.setResult("Failed to create snapshot");
    }
    callback.complete(result);
    return;
}
Also used : SnapshotObjectTO(org.apache.cloudstack.storage.to.SnapshotObjectTO) CreateObjectAnswer(org.apache.cloudstack.storage.command.CreateObjectAnswer) CreateCmdResult(org.apache.cloudstack.engine.subsystem.api.storage.CreateCmdResult)

Example 15 with CreateObjectAnswer

use of org.apache.cloudstack.storage.command.CreateObjectAnswer in project cloudstack by apache.

the class KVMStorageProcessor method createSnapshot.

@Override
public Answer createSnapshot(final CreateObjectCommand cmd) {
    final SnapshotObjectTO snapshotTO = (SnapshotObjectTO) cmd.getData();
    final PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) snapshotTO.getDataStore();
    final VolumeObjectTO volume = snapshotTO.getVolume();
    final String snapshotName = UUID.randomUUID().toString();
    final String vmName = volume.getVmName();
    try {
        final Connect conn = LibvirtConnection.getConnectionByVmName(vmName);
        DomainInfo.DomainState state = null;
        Domain vm = null;
        if (vmName != null) {
            try {
                vm = resource.getDomain(conn, vmName);
                state = vm.getInfo().state;
            } catch (final LibvirtException e) {
                s_logger.trace("Ignoring libvirt error.", e);
            }
        }
        final KVMStoragePool primaryPool = storagePoolMgr.getStoragePool(primaryStore.getPoolType(), primaryStore.getUuid());
        final KVMPhysicalDisk disk = storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), volume.getPath());
        if (state == DomainInfo.DomainState.VIR_DOMAIN_RUNNING && !primaryPool.isExternalSnapshot()) {
            final String vmUuid = vm.getUUIDString();
            final Object[] args = new Object[] { snapshotName, vmUuid };
            final String snapshot = SnapshotXML.format(args);
            final long start = System.currentTimeMillis();
            vm.snapshotCreateXML(snapshot);
            final long total = (System.currentTimeMillis() - start) / 1000;
            s_logger.debug("snapshot takes " + total + " seconds to finish");
            /*
                 * libvirt on RHEL6 doesn't handle resume event emitted from
                 * qemu
                 */
            vm = resource.getDomain(conn, vmName);
            state = vm.getInfo().state;
            if (state == DomainInfo.DomainState.VIR_DOMAIN_PAUSED) {
                vm.resume();
            }
        } else {
            /**
                 * For RBD we can't use libvirt to do our snapshotting or any Bash scripts.
                 * libvirt also wants to store the memory contents of the Virtual Machine,
                 * but that's not possible with RBD since there is no way to store the memory
                 * contents in RBD.
                 *
                 * So we rely on the Java bindings for RBD to create our snapshot
                 *
                 * This snapshot might not be 100% consistent due to writes still being in the
                 * memory of the Virtual Machine, but if the VM runs a kernel which supports
                 * barriers properly (>2.6.32) this won't be any different then pulling the power
                 * cord out of a running machine.
                 */
            if (primaryPool.getType() == StoragePoolType.RBD) {
                try {
                    final Rados r = new Rados(primaryPool.getAuthUserName());
                    r.confSet("mon_host", primaryPool.getSourceHost() + ":" + primaryPool.getSourcePort());
                    r.confSet("key", primaryPool.getAuthSecret());
                    r.confSet("client_mount_timeout", "30");
                    r.connect();
                    s_logger.debug("Succesfully connected to Ceph cluster at " + r.confGet("mon_host"));
                    final IoCTX io = r.ioCtxCreate(primaryPool.getSourceDir());
                    final Rbd rbd = new Rbd(io);
                    final RbdImage image = rbd.open(disk.getName());
                    s_logger.debug("Attempting to create RBD snapshot " + disk.getName() + "@" + snapshotName);
                    image.snapCreate(snapshotName);
                    rbd.close(image);
                    r.ioCtxDestroy(io);
                } catch (final Exception e) {
                    s_logger.error("A RBD snapshot operation on " + disk.getName() + " failed. The error was: " + e.getMessage());
                }
            } else {
                /* VM is not running, create a snapshot by ourself */
                final Script command = new Script(_manageSnapshotPath, _cmdsTimeout, s_logger);
                command.add("-c", disk.getPath());
                command.add("-n", snapshotName);
                final String result = command.execute();
                if (result != null) {
                    s_logger.debug("Failed to manage snapshot: " + result);
                    return new CreateObjectAnswer("Failed to manage snapshot: " + result);
                }
            }
        }
        final SnapshotObjectTO newSnapshot = new SnapshotObjectTO();
        // NOTE: sort of hack, we'd better just put snapshtoName
        newSnapshot.setPath(disk.getPath() + File.separator + snapshotName);
        return new CreateObjectAnswer(newSnapshot);
    } catch (final LibvirtException e) {
        s_logger.debug("Failed to manage snapshot: ", e);
        return new CreateObjectAnswer("Failed to manage snapshot: " + e.toString());
    }
}
Also used : SnapshotObjectTO(org.apache.cloudstack.storage.to.SnapshotObjectTO) Script(com.cloud.utils.script.Script) LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) Rados(com.ceph.rados.Rados) CreateObjectAnswer(org.apache.cloudstack.storage.command.CreateObjectAnswer) URISyntaxException(java.net.URISyntaxException) LibvirtException(org.libvirt.LibvirtException) RbdException(com.ceph.rbd.RbdException) QemuImgException(org.apache.cloudstack.utils.qemu.QemuImgException) FileNotFoundException(java.io.FileNotFoundException) InternalErrorException(com.cloud.exception.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) RadosException(com.ceph.rados.exceptions.RadosException) IOException(java.io.IOException) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) Rbd(com.ceph.rbd.Rbd) RbdImage(com.ceph.rbd.RbdImage) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) IoCTX(com.ceph.rados.IoCTX) DomainInfo(org.libvirt.DomainInfo) Domain(org.libvirt.Domain)

Aggregations

CreateObjectAnswer (org.apache.cloudstack.storage.command.CreateObjectAnswer)20 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)11 SnapshotObjectTO (org.apache.cloudstack.storage.to.SnapshotObjectTO)11 VolumeObjectTO (org.apache.cloudstack.storage.to.VolumeObjectTO)10 DataTO (com.cloud.agent.api.to.DataTO)6 VolumeVO (com.cloud.storage.VolumeVO)5 InternalErrorException (com.cloud.exception.InternalErrorException)4 Ovm3ResourceException (com.cloud.hypervisor.ovm3.objects.Ovm3ResourceException)4 CopyCmdAnswer (org.apache.cloudstack.storage.command.CopyCmdAnswer)4 Answer (com.cloud.agent.api.Answer)3 StoragePlugin (com.cloud.hypervisor.ovm3.objects.StoragePlugin)3 URISyntaxException (java.net.URISyntaxException)3 PrimaryDataStoreTO (org.apache.cloudstack.storage.to.PrimaryDataStoreTO)3 RadosException (com.ceph.rados.exceptions.RadosException)2 RbdException (com.ceph.rbd.RbdException)2 CopyVolumeAnswer (com.cloud.agent.api.storage.CopyVolumeAnswer)2 CreateAnswer (com.cloud.agent.api.storage.CreateAnswer)2 CreatePrivateTemplateAnswer (com.cloud.agent.api.storage.CreatePrivateTemplateAnswer)2 DownloadAnswer (com.cloud.agent.api.storage.DownloadAnswer)2 Connection (com.xensource.xenapi.Connection)2