Search in sources :

Example 46 with Pair

use of com.cloud.legacymodel.utils.Pair in project cosmic by MissionCriticalCloud.

the class SecondaryStorageManagerImpl method isZoneReady.

public boolean isZoneReady(final Map<Long, ZoneHostInfo> zoneHostInfoMap, final long dataCenterId) {
    final ZoneHostInfo zoneHostInfo = zoneHostInfoMap.get(dataCenterId);
    if (zoneHostInfo != null && (zoneHostInfo.getFlags() & RunningHostInfoAgregator.ZoneHostInfo.ROUTING_HOST_MASK) != 0) {
        final VMTemplateVO template = this._templateDao.findSystemVMReadyTemplate(dataCenterId, HypervisorType.Any);
        if (template == null) {
            logger.debug("System vm template is not ready at data center " + dataCenterId + ", wait until it is ready to launch secondary storage vm");
            return false;
        }
        final List<DataStore> stores = this._dataStoreMgr.getImageStoresByScope(new ZoneScope(dataCenterId));
        if (stores.size() < 1) {
            logger.debug("No image store added  in zone " + dataCenterId + ", wait until it is ready to launch secondary storage vm");
            return false;
        }
        final DataStore store = this.templateMgr.getImageStore(dataCenterId, template.getId());
        if (store == null) {
            logger.debug("No secondary storage available in zone " + dataCenterId + ", wait until it is ready to launch secondary storage vm");
            return false;
        }
        boolean useLocalStorage = false;
        final Boolean useLocal = ConfigurationManagerImpl.SystemVMUseLocalStorage.valueIn(dataCenterId);
        if (useLocal != null) {
            useLocalStorage = useLocal.booleanValue();
        }
        final List<Pair<Long, Integer>> l = this._storagePoolHostDao.getDatacenterStoragePoolHostInfo(dataCenterId, !useLocalStorage);
        if (l != null && l.size() > 0 && l.get(0).second().intValue() > 0) {
            return true;
        } else {
            logger.debug("Primary storage is not ready, wait until it is ready to launch secondary storage vm. dcId: " + dataCenterId + ", " + ConfigurationManagerImpl.SystemVMUseLocalStorage.key() + ": " + useLocalStorage + ". " + "If you want to use local storage to start SSVM, need to set " + ConfigurationManagerImpl.SystemVMUseLocalStorage.key() + " to true");
        }
    }
    return false;
}
Also used : ZoneScope(com.cloud.engine.subsystem.api.storage.ZoneScope) ZoneHostInfo(com.cloud.consoleproxy.RunningHostInfoAgregator.ZoneHostInfo) DataStore(com.cloud.engine.subsystem.api.storage.DataStore) VMTemplateVO(com.cloud.storage.VMTemplateVO) Pair(com.cloud.legacymodel.utils.Pair)

Example 47 with Pair

use of com.cloud.legacymodel.utils.Pair in project cosmic by MissionCriticalCloud.

the class SecondaryStorageManagerImpl method scanPool.

@Override
public Pair<AfterScanAction, Object> scanPool(final Long pool) {
    logger.info("Scanning secondary storage pool {}", pool.toString());
    final long dataCenterId = pool.longValue();
    final List<SecondaryStorageVmVO> ssVms = this._secStorageVmDao.getSecStorageVmListInStates(SecondaryStorageVmRole.templateProcessor, dataCenterId, Running, Migrating, Starting, Stopped, Stopping);
    final int vmSize = (ssVms == null) ? 0 : ssVms.size();
    final List<DataStore> ssStores = this._dataStoreMgr.getImageStoresByScope(new ZoneScope(dataCenterId));
    final int storeSize = (ssStores == null) ? 0 : ssStores.size();
    if (storeSize > vmSize) {
        final int requiredVMs = storeSize - vmSize;
        logger.info("Found less ({}) secondary storage VMs than image stores ({}) in dcId={}, starting {} new VMs", vmSize, storeSize, dataCenterId, requiredVMs);
        return new Pair<>(AfterScanAction.expand(requiredVMs), SecondaryStorageVmRole.templateProcessor);
    } else {
        final String standByCapacity = this._configDao.getValue(Config.SecStorageCapacityStandby.toString());
        final String maxPerVm = this._configDao.getValue(Config.SecStorageSessionMax.toString());
        final int requiredCapacity = new SecondaryStorageCapacityCalculator().calculateRequiredCapacity(Integer.parseInt(standByCapacity), Integer.parseInt(maxPerVm));
        if (requiredCapacity > vmSize) {
            final int requiredVMs = requiredCapacity - vmSize;
            logger.info("Found less ({}) secondary storage VMs than required ({}) in dcId={}, starting {} new VMs", vmSize, requiredCapacity, dataCenterId, requiredVMs);
            return new Pair<>(AfterScanAction.expand(requiredVMs), SecondaryStorageVmRole.templateProcessor);
        }
    }
    return new Pair<>(AfterScanAction.nop(), SecondaryStorageVmRole.templateProcessor);
}
Also used : ZoneScope(com.cloud.engine.subsystem.api.storage.ZoneScope) SecondaryStorageVmVO(com.cloud.vm.SecondaryStorageVmVO) DataStore(com.cloud.engine.subsystem.api.storage.DataStore) Pair(com.cloud.legacymodel.utils.Pair)

Example 48 with Pair

use of com.cloud.legacymodel.utils.Pair in project cosmic by MissionCriticalCloud.

the class VolumeApiServiceImpl method uploadVolume.

@Override
@ActionEvent(eventType = EventTypes.EVENT_VOLUME_UPLOAD, eventDescription = "uploading volume for post upload", async = true)
public GetUploadParamsResponse uploadVolume(final GetUploadParamsForVolumeCmd cmd) throws ResourceAllocationException, MalformedURLException {
    final Account caller = CallContext.current().getCallingAccount();
    final long ownerId = cmd.getEntityOwnerId();
    final Account owner = this._entityMgr.findById(Account.class, ownerId);
    final Long zoneId = cmd.getZoneId();
    final String volumeName = cmd.getName();
    final String format = cmd.getFormat();
    final Long diskOfferingId = cmd.getDiskOfferingId();
    final String imageStoreUuid = cmd.getImageStoreUuid();
    final DataStore store = this._tmpltMgr.getImageStore(imageStoreUuid, zoneId);
    validateVolume(caller, ownerId, zoneId, volumeName, null, format, diskOfferingId);
    return Transaction.execute(new TransactionCallbackWithException<GetUploadParamsResponse, MalformedURLException>() {

        @Override
        public GetUploadParamsResponse doInTransaction(final TransactionStatus status) throws MalformedURLException {
            final VolumeVO volume = persistVolume(owner, zoneId, volumeName, null, cmd.getFormat(), diskOfferingId, Volume.State.NotUploaded);
            final VolumeInfo vol = VolumeApiServiceImpl.this.volFactory.getVolume(volume.getId());
            final RegisterVolumePayload payload = new RegisterVolumePayload(null, cmd.getChecksum(), cmd.getFormat());
            vol.addPayload(payload);
            final Pair<EndPoint, DataObject> pair = VolumeApiServiceImpl.this.volService.registerVolumeForPostUpload(vol, store);
            final EndPoint ep = pair.first();
            final DataObject dataObject = pair.second();
            final GetUploadParamsResponse response = new GetUploadParamsResponse();
            final String ssvmUrlDomain = VolumeApiServiceImpl.this._configDao.getValue(Config.SecStorageSecureCopyCert.key());
            final String url = ImageStoreUtil.generatePostUploadUrl(ssvmUrlDomain, ep.getPublicAddr(), vol.getUuid());
            response.setPostURL(new URL(url));
            // set the post url, this is used in the monitoring thread to determine the SSVM
            final VolumeDataStoreVO volumeStore = VolumeApiServiceImpl.this._volumeStoreDao.findByVolume(vol.getId());
            assert volumeStore != null : "sincle volume is registered, volumestore cannot be null at this stage";
            volumeStore.setExtractUrl(url);
            VolumeApiServiceImpl.this._volumeStoreDao.persist(volumeStore);
            response.setId(UUID.fromString(vol.getUuid()));
            final int timeout = ImageStoreUploadMonitorImpl.getUploadOperationTimeout();
            final DateTime currentDateTime = new DateTime(DateTimeZone.UTC);
            final String expires = currentDateTime.plusMinutes(timeout).toString();
            response.setTimeout(expires);
            final String key = VolumeApiServiceImpl.this._configDao.getValue(Config.SSVMPSK.key());
            /*
                 * encoded metadata using the post upload config key
                 */
            final TemplateOrVolumePostUploadCommand command = new TemplateOrVolumePostUploadCommand(vol.getId(), vol.getUuid(), volumeStore.getInstallPath(), cmd.getChecksum(), vol.getType().toString(), vol.getName(), vol.getFormat().toString(), dataObject.getDataStore().getUri(), dataObject.getDataStore().getRole().toString());
            command.setLocalPath(volumeStore.getLocalDownloadPath());
            // using the existing max upload size configuration
            command.setMaxUploadSize(VolumeApiServiceImpl.this._configDao.getValue(Config.MaxUploadVolumeSize.key()));
            command.setDefaultMaxAccountSecondaryStorage(VolumeApiServiceImpl.this._configDao.getValue(Config.DefaultMaxAccountSecondaryStorage.key()));
            command.setAccountId(vol.getAccountId());
            final Gson gson = new GsonBuilder().create();
            final String metadata = EncryptionUtil.encodeData(gson.toJson(command), key);
            response.setMetadata(metadata);
            /*
                 * signature calculated on the url, expiry, metadata.
                 */
            response.setSignature(EncryptionUtil.generateSignature(metadata + url + expires, key));
            return response;
        }
    });
}
Also used : Account(com.cloud.legacymodel.user.Account) MalformedURLException(java.net.MalformedURLException) GsonBuilder(com.google.gson.GsonBuilder) TransactionStatus(com.cloud.utils.db.TransactionStatus) Gson(com.google.gson.Gson) VolumeInfo(com.cloud.engine.subsystem.api.storage.VolumeInfo) EndPoint(com.cloud.engine.subsystem.api.storage.EndPoint) GetUploadParamsResponse(com.cloud.api.response.GetUploadParamsResponse) URL(java.net.URL) DateTime(org.joda.time.DateTime) DataObject(com.cloud.engine.subsystem.api.storage.DataObject) TemplateOrVolumePostUploadCommand(com.cloud.legacymodel.communication.command.TemplateOrVolumePostUploadCommand) DataStore(com.cloud.engine.subsystem.api.storage.DataStore) VolumeDataStoreVO(com.cloud.storage.datastore.db.VolumeDataStoreVO) Pair(com.cloud.legacymodel.utils.Pair) ActionEvent(com.cloud.event.ActionEvent)

Example 49 with Pair

use of com.cloud.legacymodel.utils.Pair in project cosmic by MissionCriticalCloud.

the class HypervisorTemplateAdapter method delete.

@Override
@DB
public boolean delete(final TemplateProfile profile) {
    boolean success = true;
    final VMTemplateVO template = profile.getTemplate();
    // find all eligible image stores for this template
    final List<DataStore> imageStores = this.templateMgr.getImageStoreByTemplate(template.getId(), profile.getZoneId());
    if (imageStores == null || imageStores.size() == 0) {
        // already destroyed on image stores
        s_logger.info("Unable to find image store still having template: " + template.getName() + ", so just mark the template removed");
    } else {
        // Make sure the template is downloaded to all found image stores
        for (final DataStore store : imageStores) {
            final long storeId = store.getId();
            final List<TemplateDataStoreVO> templateStores = this._tmpltStoreDao.listByTemplateStore(template.getId(), storeId);
            for (final TemplateDataStoreVO templateStore : templateStores) {
                if (templateStore.getDownloadState() == VMTemplateStatus.DOWNLOAD_IN_PROGRESS) {
                    final String errorMsg = "Please specify a template that is not currently being downloaded.";
                    s_logger.debug("Template: " + template.getName() + " is currently being downloaded to secondary storage host: " + store.getName() + "; cant' delete it.");
                    throw new CloudRuntimeException(errorMsg);
                }
            }
        }
        for (final DataStore imageStore : imageStores) {
            // publish zone-wide usage event
            final Long sZoneId = ((ImageStoreEntity) imageStore).getDataCenterId();
            s_logger.info("Delete template from image store: " + imageStore.getName());
            final AsyncCallFuture<TemplateApiResult> future = this.imageService.deleteTemplateAsync(this.imageFactory.getTemplate(template.getId(), imageStore));
            try {
                final TemplateApiResult result = future.get();
                success = result.isSuccess();
                if (!success) {
                    s_logger.warn("Failed to delete the template " + template + " from the image store: " + imageStore.getName() + " due to: " + result.getResult());
                    break;
                }
                // remove from template_zone_ref
                final List<VMTemplateZoneVO> templateZones = this.templateZoneDao.listByZoneTemplate(sZoneId, template.getId());
                if (templateZones != null) {
                    for (final VMTemplateZoneVO templateZone : templateZones) {
                        this.templateZoneDao.remove(templateZone.getId());
                    }
                }
                // mark all the occurrences of this template in the given store as destroyed.
                this.templateDataStoreDao.removeByTemplateStore(template.getId(), imageStore.getId());
            } catch (final InterruptedException | ExecutionException e) {
                s_logger.debug("delete template Failed", e);
                throw new CloudRuntimeException("delete template Failed", e);
            }
        }
    }
    if (success) {
        if ((imageStores.size() > 1) && (profile.getZoneId() != null)) {
            // if template is stored in more than one image stores, and the zone id is not null, then don't delete other templates.
            return success;
        }
        // delete all cache entries for this template
        final List<TemplateInfo> cacheTmpls = this.imageFactory.listTemplateOnCache(template.getId());
        for (final TemplateInfo tmplOnCache : cacheTmpls) {
            s_logger.info("Delete template from image cache store: " + tmplOnCache.getDataStore().getName());
            tmplOnCache.delete();
        }
        // find all eligible image stores for this template
        final List<DataStore> iStores = this.templateMgr.getImageStoreByTemplate(template.getId(), null);
        if (iStores == null || iStores.size() == 0) {
            // Mark template as Inactive.
            template.setState(VirtualMachineTemplate.State.Inactive);
            this._tmpltDao.update(template.getId(), template);
            // Decrement the number of templates and total secondary storage
            // space used by the account
            final Account account = this._accountDao.findByIdIncludingRemoved(template.getAccountId());
            this._resourceLimitMgr.decrementResourceCount(template.getAccountId(), ResourceType.template);
            this._resourceLimitMgr.recalculateResourceCount(template.getAccountId(), account.getDomainId(), ResourceType.secondary_storage.getOrdinal());
        }
        // remove its related ACL permission
        final Pair<Class<?>, Long> tmplt = new Pair<>(VirtualMachineTemplate.class, template.getId());
        this._messageBus.publish(this._name, EntityManager.MESSAGE_REMOVE_ENTITY_EVENT, PublishScope.LOCAL, tmplt);
    }
    return success;
}
Also used : Account(com.cloud.legacymodel.user.Account) VMTemplateZoneVO(com.cloud.storage.VMTemplateZoneVO) VMTemplateVO(com.cloud.storage.VMTemplateVO) TemplateDataStoreVO(com.cloud.storage.datastore.db.TemplateDataStoreVO) TemplateApiResult(com.cloud.engine.subsystem.api.storage.TemplateService.TemplateApiResult) TemplateInfo(com.cloud.engine.subsystem.api.storage.TemplateInfo) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) DataStore(com.cloud.engine.subsystem.api.storage.DataStore) ImageStoreEntity(com.cloud.storage.image.datastore.ImageStoreEntity) ExecutionException(java.util.concurrent.ExecutionException) Pair(com.cloud.legacymodel.utils.Pair) DB(com.cloud.utils.db.DB)

Example 50 with Pair

use of com.cloud.legacymodel.utils.Pair in project cosmic by MissionCriticalCloud.

the class StorageManagerImpl method sendToPool.

@Override
public Pair<Long, Answer[]> sendToPool(final StoragePool pool, final long[] hostIdsToTryFirst, final List<Long> hostIdsToAvoid, final Commands cmds) throws StorageUnavailableException {
    final List<Long> hostIds = getUpHostsInPool(pool.getId());
    Collections.shuffle(hostIds);
    if (hostIdsToTryFirst != null) {
        for (int i = hostIdsToTryFirst.length - 1; i >= 0; i--) {
            if (hostIds.remove(hostIdsToTryFirst[i])) {
                hostIds.add(0, hostIdsToTryFirst[i]);
            }
        }
    }
    if (hostIdsToAvoid != null) {
        hostIds.removeAll(hostIdsToAvoid);
    }
    if (hostIds == null || hostIds.isEmpty()) {
        throw new StorageUnavailableException("Unable to send command to the pool " + pool.getId() + " due to there is no enabled hosts up in this cluster", pool.getId());
    }
    for (final Long hostId : hostIds) {
        try {
            final List<Answer> answers = new ArrayList<>();
            final Command[] cmdArray = cmds.toCommands();
            for (final Command cmd : cmdArray) {
                final long targetHostId = this._hvGuruMgr.getGuruProcessedCommandTargetHost(hostId, cmd);
                answers.add(this._agentMgr.send(targetHostId, cmd));
            }
            return new Pair<>(hostId, answers.toArray(new Answer[answers.size()]));
        } catch (final AgentUnavailableException e) {
            s_logger.debug("Unable to send storage pool command to " + pool + " via " + hostId, e);
        } catch (final OperationTimedoutException e) {
            s_logger.debug("Unable to send storage pool command to " + pool + " via " + hostId, e);
        }
    }
    throw new StorageUnavailableException("Unable to send command to the pool ", pool.getId());
}
Also used : OperationTimedoutException(com.cloud.legacymodel.exceptions.OperationTimedoutException) ArrayList(java.util.ArrayList) EndPoint(com.cloud.engine.subsystem.api.storage.EndPoint) Answer(com.cloud.legacymodel.communication.answer.Answer) StorageUnavailableException(com.cloud.legacymodel.exceptions.StorageUnavailableException) Command(com.cloud.legacymodel.communication.command.Command) AgentUnavailableException(com.cloud.legacymodel.exceptions.AgentUnavailableException) Pair(com.cloud.legacymodel.utils.Pair)

Aggregations

Pair (com.cloud.legacymodel.utils.Pair)139 ArrayList (java.util.ArrayList)87 List (java.util.List)64 Account (com.cloud.legacymodel.user.Account)49 Filter (com.cloud.utils.db.Filter)48 InvalidParameterValueException (com.cloud.legacymodel.exceptions.InvalidParameterValueException)38 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)27 HashMap (java.util.HashMap)27 Ternary (com.cloud.legacymodel.utils.Ternary)23 ListProjectResourcesCriteria (com.cloud.projects.Project.ListProjectResourcesCriteria)22 ExcludeList (com.cloud.deploy.DeploymentPlanner.ExcludeList)20 SSHKeyPair (com.cloud.legacymodel.user.SSHKeyPair)16 TemplateFilter (com.cloud.legacymodel.storage.VirtualMachineTemplate.TemplateFilter)13 Map (java.util.Map)13 DB (com.cloud.utils.db.DB)11 DomainVO (com.cloud.domain.DomainVO)10 VolumeVO (com.cloud.storage.VolumeVO)10 PermissionDeniedException (com.cloud.legacymodel.exceptions.PermissionDeniedException)9 Network (com.cloud.legacymodel.network.Network)9 ResourceTagVO (com.cloud.tags.ResourceTagVO)9