Search in sources :

Example 1 with SyncVolumePathAnswer

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

the class StorageManagerImpl method handleRemoveChildStoragePoolFromDatastoreCluster.

private void handleRemoveChildStoragePoolFromDatastoreCluster(Set<String> childDatastoreUUIDs) {
    for (String childDatastoreUUID : childDatastoreUUIDs) {
        StoragePoolVO dataStoreVO = _storagePoolDao.findPoolByUUID(childDatastoreUUID);
        List<VolumeVO> allVolumes = _volumeDao.findByPoolId(dataStoreVO.getId());
        allVolumes.removeIf(volumeVO -> volumeVO.getInstanceId() == null);
        allVolumes.removeIf(volumeVO -> volumeVO.getState() != Volume.State.Ready);
        for (VolumeVO volume : allVolumes) {
            VMInstanceVO vmInstance = _vmInstanceDao.findById(volume.getInstanceId());
            if (vmInstance == null) {
                continue;
            }
            long volumeId = volume.getId();
            Long hostId = vmInstance.getHostId();
            if (hostId == null) {
                hostId = vmInstance.getLastHostId();
            }
            HostVO hostVO = _hostDao.findById(hostId);
            // Prepare for the syncvolumepath command
            DataTO volTO = volFactory.getVolume(volume.getId()).getTO();
            DiskTO disk = new DiskTO(volTO, volume.getDeviceId(), volume.getPath(), volume.getVolumeType());
            Map<String, String> details = new HashMap<String, String>();
            details.put(DiskTO.PROTOCOL_TYPE, Storage.StoragePoolType.DatastoreCluster.toString());
            disk.setDetails(details);
            s_logger.debug(String.format("Attempting to process SyncVolumePathCommand for the volume %d on the host %d with state %s", volumeId, hostId, hostVO.getResourceState()));
            SyncVolumePathCommand cmd = new SyncVolumePathCommand(disk);
            final Answer answer = _agentMgr.easySend(hostId, cmd);
            // validate answer
            if (answer == null) {
                throw new CloudRuntimeException("Unable to get an answer to the SyncVolumePath command for volume " + volumeId);
            }
            if (!answer.getResult()) {
                throw new CloudRuntimeException("Unable to process SyncVolumePathCommand for the volume" + volumeId + " to the host " + hostId + " due to " + answer.getDetails());
            }
            assert (answer instanceof SyncVolumePathAnswer) : "Well, now why won't you actually return the SyncVolumePathAnswer when it's SyncVolumePathCommand? volume=" + volume.getUuid() + "Host=" + hostId;
            // check for the changed details of volume and update database
            VolumeVO volumeVO = _volumeDao.findById(volumeId);
            String datastoreName = answer.getContextParam("datastoreName");
            if (datastoreName != null) {
                StoragePoolVO storagePoolVO = _storagePoolDao.findByUuid(datastoreName);
                if (storagePoolVO != null) {
                    volumeVO.setPoolId(storagePoolVO.getId());
                } else {
                    s_logger.warn(String.format("Unable to find datastore %s while updating the new datastore of the volume %d", datastoreName, volumeId));
                }
            }
            String volumePath = answer.getContextParam("volumePath");
            if (volumePath != null) {
                volumeVO.setPath(volumePath);
            }
            String chainInfo = answer.getContextParam("chainInfo");
            if (chainInfo != null) {
                volumeVO.setChainInfo(chainInfo);
            }
            _volumeDao.update(volumeVO.getId(), volumeVO);
        }
        dataStoreVO.setParent(0L);
        _storagePoolDao.update(dataStoreVO.getId(), dataStoreVO);
    }
}
Also used : HashMap(java.util.HashMap) VMInstanceVO(com.cloud.vm.VMInstanceVO) SyncVolumePathCommand(org.apache.cloudstack.storage.command.SyncVolumePathCommand) HostVO(com.cloud.host.HostVO) SyncVolumePathAnswer(org.apache.cloudstack.storage.command.SyncVolumePathAnswer) ModifyStoragePoolAnswer(com.cloud.agent.api.ModifyStoragePoolAnswer) GetVolumeStatsAnswer(com.cloud.agent.api.GetVolumeStatsAnswer) Answer(com.cloud.agent.api.Answer) SyncVolumePathAnswer(org.apache.cloudstack.storage.command.SyncVolumePathAnswer) GetStoragePoolCapabilitiesAnswer(com.cloud.agent.api.GetStoragePoolCapabilitiesAnswer) GetStorageStatsAnswer(com.cloud.agent.api.GetStorageStatsAnswer) DataTO(com.cloud.agent.api.to.DataTO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) StoragePoolVO(org.apache.cloudstack.storage.datastore.db.StoragePoolVO) DiskTO(com.cloud.agent.api.to.DiskTO)

Example 2 with SyncVolumePathAnswer

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

the class VmwareStorageProcessor method syncVolumePath.

@Override
public Answer syncVolumePath(SyncVolumePathCommand cmd) {
    DiskTO disk = cmd.getDisk();
    VolumeObjectTO volumeTO = (VolumeObjectTO) disk.getData();
    DataStoreTO primaryStore = volumeTO.getDataStore();
    String volumePath = volumeTO.getPath();
    String vmName = volumeTO.getVmName();
    boolean datastoreChangeObserved = false;
    boolean volumePathChangeObserved = false;
    String chainInfo = null;
    try {
        VmwareContext context = hostService.getServiceContext(null);
        VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, null);
        VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(vmName);
        if (vmMo == null) {
            vmMo = hyperHost.findVmOnPeerHyperHost(vmName);
            if (vmMo == null) {
                String msg = "Unable to find the VM to execute SyncVolumePathCommand, vmName: " + vmName;
                s_logger.error(msg);
                throw new Exception(msg);
            }
        }
        String datastoreUUID = primaryStore.getUuid();
        if (disk.getDetails().get(DiskTO.PROTOCOL_TYPE) != null && disk.getDetails().get(DiskTO.PROTOCOL_TYPE).equalsIgnoreCase("DatastoreCluster")) {
            VirtualMachineDiskInfo matchingExistingDisk = getMatchingExistingDisk(hyperHost, context, vmMo, disk);
            VirtualMachineDiskInfoBuilder diskInfoBuilder = vmMo.getDiskInfoBuilder();
            if (diskInfoBuilder != null && matchingExistingDisk != null) {
                String[] diskChain = matchingExistingDisk.getDiskChain();
                assert (diskChain.length > 0);
                DatastoreFile file = new DatastoreFile(diskChain[0]);
                if (!file.getFileBaseName().equalsIgnoreCase(volumePath)) {
                    if (s_logger.isInfoEnabled())
                        s_logger.info("Detected disk-chain top file change on volume: " + volumeTO.getId() + " " + volumePath + " -> " + file.getFileBaseName());
                    volumePathChangeObserved = true;
                    volumePath = file.getFileBaseName();
                    volumeTO.setPath(volumePath);
                    chainInfo = _gson.toJson(matchingExistingDisk);
                }
                DatastoreMO diskDatastoreMofromVM = getDiskDatastoreMofromVM(hyperHost, context, vmMo, disk, diskInfoBuilder);
                if (diskDatastoreMofromVM != null) {
                    String actualPoolUuid = diskDatastoreMofromVM.getCustomFieldValue(CustomFieldConstants.CLOUD_UUID);
                    if (!actualPoolUuid.equalsIgnoreCase(primaryStore.getUuid())) {
                        s_logger.warn(String.format("Volume %s found to be in a different storage pool %s", volumePath, actualPoolUuid));
                        datastoreChangeObserved = true;
                        datastoreUUID = actualPoolUuid;
                        chainInfo = _gson.toJson(matchingExistingDisk);
                    }
                }
            }
        }
        SyncVolumePathAnswer answer = new SyncVolumePathAnswer(disk);
        if (datastoreChangeObserved) {
            answer.setContextParam("datastoreName", datastoreUUID);
        }
        if (volumePathChangeObserved) {
            answer.setContextParam("volumePath", volumePath);
        }
        if (chainInfo != null && !chainInfo.isEmpty()) {
            answer.setContextParam("chainInfo", chainInfo);
        }
        return answer;
    } catch (Throwable e) {
        return new SyncVolumePathAnswer(hostService.createLogMessageException(e, cmd));
    }
}
Also used : PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) VirtualMachineDiskInfoBuilder(com.cloud.hypervisor.vmware.mo.VirtualMachineDiskInfoBuilder) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) RemoteException(java.rmi.RemoteException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) DatastoreMO(com.cloud.hypervisor.vmware.mo.DatastoreMO) SyncVolumePathAnswer(org.apache.cloudstack.storage.command.SyncVolumePathAnswer) VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) DatastoreFile(com.cloud.hypervisor.vmware.mo.DatastoreFile) VirtualMachineDiskInfo(org.apache.cloudstack.utils.volume.VirtualMachineDiskInfo) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) DiskTO(com.cloud.agent.api.to.DiskTO)

Aggregations

DiskTO (com.cloud.agent.api.to.DiskTO)2 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)2 SyncVolumePathAnswer (org.apache.cloudstack.storage.command.SyncVolumePathAnswer)2 Answer (com.cloud.agent.api.Answer)1 GetStoragePoolCapabilitiesAnswer (com.cloud.agent.api.GetStoragePoolCapabilitiesAnswer)1 GetStorageStatsAnswer (com.cloud.agent.api.GetStorageStatsAnswer)1 GetVolumeStatsAnswer (com.cloud.agent.api.GetVolumeStatsAnswer)1 ModifyStoragePoolAnswer (com.cloud.agent.api.ModifyStoragePoolAnswer)1 DataStoreTO (com.cloud.agent.api.to.DataStoreTO)1 DataTO (com.cloud.agent.api.to.DataTO)1 HostVO (com.cloud.host.HostVO)1 DatastoreFile (com.cloud.hypervisor.vmware.mo.DatastoreFile)1 DatastoreMO (com.cloud.hypervisor.vmware.mo.DatastoreMO)1 VirtualMachineDiskInfoBuilder (com.cloud.hypervisor.vmware.mo.VirtualMachineDiskInfoBuilder)1 VirtualMachineMO (com.cloud.hypervisor.vmware.mo.VirtualMachineMO)1 VmwareHypervisorHost (com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost)1 VmwareContext (com.cloud.hypervisor.vmware.util.VmwareContext)1 VMInstanceVO (com.cloud.vm.VMInstanceVO)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 RemoteException (java.rmi.RemoteException)1