Search in sources :

Example 26 with QueryByPages

use of com.vmware.photon.controller.model.query.QueryUtils.QueryByPages in project photon-model by vmware.

the class AzureComputeEnumerationAdapterService method queryForComputeStates.

/**
 * Query all compute states for the cluster filtered by the received set of instance Ids.
 */
private void queryForComputeStates(EnumerationContext ctx, ComputeEnumerationSubStages next) {
    if (ctx.virtualMachines.isEmpty() && ctx.regions.isEmpty()) {
        ctx.subStage = ComputeEnumerationSubStages.DISASSOCIATE_COMPUTE_STATES;
        handleSubStage(ctx);
        return;
    }
    Query.Builder qBuilder = Query.Builder.create().addKindFieldClause(ComputeState.class).addFieldClause(ComputeState.FIELD_NAME_PARENT_LINK, ctx.request.resourceLink());
    Query.Builder instanceIdFilterParentQuery = Query.Builder.create(Occurance.MUST_OCCUR);
    for (String instanceId : ctx.virtualMachines.keySet()) {
        Query instanceIdFilter = Query.Builder.create(Occurance.SHOULD_OCCUR).addFieldClause(ComputeState.FIELD_NAME_ID, instanceId).build();
        instanceIdFilterParentQuery.addClause(instanceIdFilter);
    }
    // also get compute states representing regions
    for (RegionInfo region : ctx.regions.values()) {
        Query instanceIdFilter = Query.Builder.create(Occurance.SHOULD_OCCUR).addFieldClause(ComputeState.FIELD_NAME_ID, region.regionId).build();
        instanceIdFilterParentQuery.addClause(instanceIdFilter);
    }
    qBuilder.addClause(instanceIdFilterParentQuery.build());
    QueryByPages<ComputeState> queryLocalStates = new QueryByPages<>(getHost(), qBuilder.build(), ComputeState.class, ctx.parentCompute.tenantLinks, // endpointLink
    null, ctx.parentCompute.documentSelfLink).setMaxPageSize(QueryUtils.MAX_RESULT_LIMIT).setClusterType(ServiceTypeCluster.INVENTORY_SERVICE);
    queryLocalStates.queryDocuments(c -> ctx.computeStates.put(c.id, c)).whenComplete(thenHandleSubStage(ctx, next));
}
Also used : PowerState(com.vmware.photon.controller.model.resources.ComputeService.PowerState) Arrays(java.util.Arrays) ComputeEnumerateResourceRequest(com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest) ServiceTypeCluster(com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster) LifecycleState(com.vmware.photon.controller.model.resources.ComputeService.LifecycleState) DISK_CONTROLLER_NUMBER(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.DISK_CONTROLLER_NUMBER) Action1(rx.functions.Action1) StringUtils(org.apache.commons.lang3.StringUtils) Azure(com.microsoft.azure.management.Azure) Utils(com.vmware.xenon.common.Utils) Pair(org.apache.commons.lang3.tuple.Pair) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) Map(java.util.Map) StorageDescription(com.vmware.photon.controller.model.resources.StorageDescriptionService.StorageDescription) OSDisk(com.microsoft.azure.management.compute.OSDisk) ResourceEnumerationTaskService(com.vmware.photon.controller.model.tasks.ResourceEnumerationTaskService) NetworkInterfaceState(com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState) StatelessService(com.vmware.xenon.common.StatelessService) Set(java.util.Set) NetworkInterfaceService(com.vmware.photon.controller.model.resources.NetworkInterfaceService) StorageAccountTypes(com.microsoft.azure.management.compute.StorageAccountTypes) TagService(com.vmware.photon.controller.model.resources.TagService) CompletionHandler(com.vmware.xenon.common.Operation.CompletionHandler) SOURCE_TASK_LINK(com.vmware.photon.controller.model.constants.PhotonModelConstants.SOURCE_TASK_LINK) InstanceViewStatus(com.microsoft.azure.management.compute.InstanceViewStatus) DeferredResult(com.vmware.xenon.common.DeferredResult) UriUtils(com.vmware.xenon.common.UriUtils) ComputeService(com.vmware.photon.controller.model.resources.ComputeService) NumericRange(com.vmware.xenon.services.common.QueryTask.NumericRange) AZURE_DATA_DISK_CACHING(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AZURE_DATA_DISK_CACHING) ImageReferenceInner(com.microsoft.azure.management.compute.implementation.ImageReferenceInner) VirtualMachinesInner(com.microsoft.azure.management.compute.implementation.VirtualMachinesInner) ComputeDescriptionService(com.vmware.photon.controller.model.resources.ComputeDescriptionService) PhotonModelUtils(com.vmware.photon.controller.model.resources.util.PhotonModelUtils) RegionInfo(com.vmware.photon.controller.model.adapterapi.RegionEnumerationResponse.RegionInfo) TagsUtil(com.vmware.photon.controller.model.adapters.util.TagsUtil) ArrayList(java.util.ArrayList) TagState(com.vmware.photon.controller.model.resources.TagService.TagState) ServiceStateCollectionUpdateRequest(com.vmware.xenon.common.ServiceStateCollectionUpdateRequest) Query(com.vmware.xenon.services.common.QueryTask.Query) EnumerationStages(com.vmware.photon.controller.model.adapters.util.enums.EnumerationStages) OperatingSystemTypes(com.microsoft.azure.management.compute.OperatingSystemTypes) BiConsumer(java.util.function.BiConsumer) AZURE_DIAGNOSTIC_STORAGE_ACCOUNT_LINK(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AZURE_DIAGNOSTIC_STORAGE_ACCOUNT_LINK) AZURE_STORAGE_ACCOUNT_URI(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AZURE_STORAGE_ACCOUNT_URI) VirtualMachineInner(com.microsoft.azure.management.compute.implementation.VirtualMachineInner) AdapterUtils(com.vmware.photon.controller.model.adapters.util.AdapterUtils) DataDisk(com.microsoft.azure.management.compute.DataDisk) ResourceState(com.vmware.photon.controller.model.resources.ResourceState) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) ENVIRONMENT_NAME_AZURE(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription.ENVIRONMENT_NAME_AZURE) QueryTop(com.vmware.photon.controller.model.query.QueryUtils.QueryTop) CUSTOM_OS_TYPE(com.vmware.photon.controller.model.ComputeProperties.CUSTOM_OS_TYPE) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) PhotonModelConstants(com.vmware.photon.controller.model.constants.PhotonModelConstants) ComputeEnumerateAdapterRequest(com.vmware.photon.controller.model.adapters.util.ComputeEnumerateAdapterRequest) RegionEnumerationResponse(com.vmware.photon.controller.model.adapterapi.RegionEnumerationResponse) QuerySpecification(com.vmware.xenon.services.common.QueryTask.QuerySpecification) PhotonModelUriUtils.createInventoryUri(com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri) AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) QueryTask(com.vmware.xenon.services.common.QueryTask) AZURE_RESOURCE_GROUP_NAME(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AZURE_RESOURCE_GROUP_NAME) OSType(com.vmware.photon.controller.model.ComputeProperties.OSType) AzureUriPaths(com.vmware.photon.controller.model.adapters.azure.AzureUriPaths) AZURE_MANAGED_DISK_TYPE(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AZURE_MANAGED_DISK_TYPE) ComputeType(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription.ComputeType) AzureSdkClients(com.vmware.photon.controller.model.adapters.azure.utils.AzureSdkClients) AzureUtils.injectOperationContext(com.vmware.photon.controller.model.adapters.azure.utils.AzureUtils.injectOperationContext) CUSTOM_PROP_ENDPOINT_LINK(com.vmware.photon.controller.model.constants.PhotonModelConstants.CUSTOM_PROP_ENDPOINT_LINK) URI(java.net.URI) TagsUtil.newTagState(com.vmware.photon.controller.model.adapters.util.TagsUtil.newTagState) EndpointState(com.vmware.photon.controller.model.resources.EndpointService.EndpointState) AzureConstants(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants) AzureConstants.getQueryResultLimit(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.getQueryResultLimit) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) Occurance(com.vmware.xenon.services.common.QueryTask.Query.Occurance) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) List(java.util.List) AzureUtils(com.vmware.photon.controller.model.adapters.azure.utils.AzureUtils) AzureUtils.getResourceGroupName(com.vmware.photon.controller.model.adapters.azure.utils.AzureUtils.getResourceGroupName) TAG_KEY_TYPE(com.vmware.photon.controller.model.constants.PhotonModelConstants.TAG_KEY_TYPE) Entry(java.util.Map.Entry) NetworkInterfacesInner(com.microsoft.azure.management.network.implementation.NetworkInterfacesInner) QueryOption(com.vmware.xenon.services.common.QueryTask.QuerySpecification.QueryOption) InstanceViewTypes(com.microsoft.azure.management.compute.InstanceViewTypes) TagsUtil.setTagLinksToResourceState(com.vmware.photon.controller.model.adapters.util.TagsUtil.setTagLinksToResourceState) Builder(com.vmware.xenon.services.common.QueryTask.Query.Builder) DiskService(com.vmware.photon.controller.model.resources.DiskService) AzureUtils.isDiskManaged(com.vmware.photon.controller.model.adapters.azure.utils.AzureUtils.isDiskManaged) Default(com.vmware.photon.controller.model.adapters.azure.utils.AzureDeferredResultServiceCallback.Default) QueryByPages(com.vmware.photon.controller.model.query.QueryUtils.QueryByPages) AZURE_OSDISK_CACHING(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AZURE_OSDISK_CACHING) PublicIPAddress(com.microsoft.azure.management.network.PublicIPAddress) HashMap(java.util.HashMap) HashSet(java.util.HashSet) AuthCredentialsService(com.vmware.xenon.services.common.AuthCredentialsService) TagsUtil.updateLocalTagStates(com.vmware.photon.controller.model.adapters.util.TagsUtil.updateLocalTagStates) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) EnumerationAction(com.vmware.photon.controller.model.adapterapi.EnumerationAction) AzureResourceType(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AzureResourceType) ExecutorService(java.util.concurrent.ExecutorService) Iterator(java.util.Iterator) NetworkInterfaceIPConfigurationInner(com.microsoft.azure.management.network.implementation.NetworkInterfaceIPConfigurationInner) Operation(com.vmware.xenon.common.Operation) Page(com.microsoft.azure.Page) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) NetworkInterfaceReferenceInner(com.microsoft.azure.management.compute.implementation.NetworkInterfaceReferenceInner) AzureDeferredResultServiceCallback(com.vmware.photon.controller.model.adapters.azure.utils.AzureDeferredResultServiceCallback) NetworkInterfaceInner(com.microsoft.azure.management.network.implementation.NetworkInterfaceInner) Collections(java.util.Collections) OperationJoin(com.vmware.xenon.common.OperationJoin) RESOURCE_GROUP_NAME(com.vmware.photon.controller.model.ComputeProperties.RESOURCE_GROUP_NAME) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) QueryByPages(com.vmware.photon.controller.model.query.QueryUtils.QueryByPages) Query(com.vmware.xenon.services.common.QueryTask.Query) Builder(com.vmware.xenon.services.common.QueryTask.Query.Builder) RegionInfo(com.vmware.photon.controller.model.adapterapi.RegionEnumerationResponse.RegionInfo)

Example 27 with QueryByPages

use of com.vmware.photon.controller.model.query.QueryUtils.QueryByPages in project photon-model by vmware.

the class AzureSubscriptionsEnumerationService method updateExistingResources.

private void updateExistingResources(AzureSubscriptionsEnumerationContext enumerationContext, AzureCostComputeEnumerationStages nextStage) {
    // Query the subscriptions which we want to create to check if they already exist
    Query azureSubscriptionEndpointQuery = createQueryForAzureSubscriptionEndpoints(enumerationContext);
    // Use max page size since we are collectDocuments EndpointStates
    QueryByPages<EndpointState> querySubscriptionEndpoints = new QueryByPages<>(getHost(), azureSubscriptionEndpointQuery, EndpointState.class, enumerationContext.parent.tenantLinks).setMaxPageSize(QueryUtils.MAX_RESULT_LIMIT).setClusterType(ServiceTypeCluster.INVENTORY_SERVICE);
    querySubscriptionEndpoints.collectDocuments(Collectors.toList()).whenComplete((subscriptionEndpoints, t) -> {
        if (t != null) {
            getFailureConsumer(enumerationContext).accept(t);
            return;
        }
        if (subscriptionEndpoints.isEmpty()) {
            enumerationContext.stage = nextStage;
            handleAzureSubscriptionsEnumerationRequest(enumerationContext);
            return;
        }
        queryExistingComputeStatesOfEndpoints(enumerationContext, nextStage, subscriptionEndpoints);
    });
}
Also used : EndpointState(com.vmware.photon.controller.model.resources.EndpointService.EndpointState) QueryByPages(com.vmware.photon.controller.model.query.QueryUtils.QueryByPages) Query(com.vmware.xenon.services.common.QueryTask.Query)

Example 28 with QueryByPages

use of com.vmware.photon.controller.model.query.QueryUtils.QueryByPages in project photon-model by vmware.

the class ResourcePoolQueryHelper method executeRpQueries.

/**
 * Executes the resource pool queries in parallel and then collects the result.
 */
private DeferredResult<Void> executeRpQueries() {
    List<DeferredResult<Void>> rpQueryDRs = new ArrayList<>(this.result.resourcesPools.size());
    Map<String, Map<String, ComputeState>> computeMapByRpLink = new ConcurrentHashMap<>();
    for (ResourcePoolData rpData : this.result.resourcesPools.values()) {
        String rpLink = rpData.resourcePoolState.documentSelfLink;
        Query rpQuery = rpData.resourcePoolState.query;
        Query.Builder queryBuilder = Query.Builder.create().addClause(rpQuery);
        if (this.computeLinks != null && !this.computeLinks.isEmpty()) {
            queryBuilder.addInClause(ServiceDocument.FIELD_NAME_SELF_LINK, this.computeLinks);
        } else if (this.additionalQueryClausesProvider != null) {
            this.additionalQueryClausesProvider.accept(queryBuilder);
        }
        QueryByPages<ComputeState> computeQuery = new QueryByPages<>(this.host, queryBuilder.build(), ComputeState.class, this.tenantLinks).setMaxPageSize(PAGE_SIZE);
        computeQuery.setClusterType(ServiceTypeCluster.INVENTORY_SERVICE);
        DeferredResult<Map<String, ComputeState>> rpQueryDR;
        if (this.expandComputes) {
            rpQueryDR = computeQuery.collectDocuments(Collectors.toMap(cs -> cs.documentSelfLink, cs -> cs));
        } else {
            // manually collect links since Collectors.toMap() does not allow null values
            Map<String, ComputeState> computesMap = new HashMap<>();
            rpQueryDR = computeQuery.queryLinks(csLink -> computesMap.put(csLink, null)).thenApply(ignore -> computesMap);
        }
        rpQueryDRs.add(rpQueryDR.thenAccept(computesMap -> computeMapByRpLink.put(rpLink, computesMap)));
    }
    return DeferredResult.allOf(rpQueryDRs).thenAccept(ignore -> computeMapByRpLink.forEach(this::storeComputes)).thenApply(ignore -> (Void) null);
}
Also used : QueryByPages(com.vmware.photon.controller.model.query.QueryUtils.QueryByPages) Collection(java.util.Collection) ServiceTypeCluster(com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ResourcePoolData(com.vmware.photon.controller.model.tasks.helpers.ResourcePoolQueryHelper.QueryResult.ResourcePoolData) Set(java.util.Set) ResourcePoolState(com.vmware.photon.controller.model.resources.ResourcePoolService.ResourcePoolState) ServiceDocument(com.vmware.xenon.common.ServiceDocument) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) ServiceHost(com.vmware.xenon.common.ServiceHost) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Objects(java.util.Objects) Consumer(java.util.function.Consumer) List(java.util.List) Stream(java.util.stream.Stream) Query(com.vmware.xenon.services.common.QueryTask.Query) UriPaths(com.vmware.photon.controller.model.UriPaths) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) Map(java.util.Map) DeferredResult(com.vmware.xenon.common.DeferredResult) Collections(java.util.Collections) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) Query(com.vmware.xenon.services.common.QueryTask.Query) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ResourcePoolData(com.vmware.photon.controller.model.tasks.helpers.ResourcePoolQueryHelper.QueryResult.ResourcePoolData) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Map(java.util.Map) DeferredResult(com.vmware.xenon.common.DeferredResult)

Example 29 with QueryByPages

use of com.vmware.photon.controller.model.query.QueryUtils.QueryByPages in project photon-model by vmware.

the class ResourcePoolQueryHelper method handleMissingComputes.

/**
 * With the given compute links, finds which ones are not already retrieved as part of a
 * resource pool, and loads the corresponding ComputeState documents into the result.
 */
private DeferredResult<Void> handleMissingComputes(Collection<String> allComputeLinks) {
    Collection<String> missingComputeLinks = new HashSet<>(allComputeLinks);
    missingComputeLinks.removeAll(this.result.computesByLink.keySet());
    if (missingComputeLinks.isEmpty()) {
        return DeferredResult.completed(null);
    }
    Query query = Query.Builder.create().addKindFieldClause(ComputeState.class).addInClause(ServiceDocument.FIELD_NAME_SELF_LINK, missingComputeLinks).build();
    QueryByPages<ComputeState> queryByPages = new QueryByPages<>(this.host, query, ComputeState.class, this.tenantLinks);
    queryByPages.setClusterType(ServiceTypeCluster.INVENTORY_SERVICE);
    return queryByPages.setMaxPageSize(PAGE_SIZE).collectDocuments(Collectors.toMap(cs -> cs.documentSelfLink, cs -> cs)).thenAccept(computesMap -> storeComputes(null, computesMap));
}
Also used : ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) QueryByPages(com.vmware.photon.controller.model.query.QueryUtils.QueryByPages) Query(com.vmware.xenon.services.common.QueryTask.Query) HashSet(java.util.HashSet)

Aggregations

QueryByPages (com.vmware.photon.controller.model.query.QueryUtils.QueryByPages)29 Query (com.vmware.xenon.services.common.QueryTask.Query)22 List (java.util.List)18 ArrayList (java.util.ArrayList)17 HashSet (java.util.HashSet)17 Collectors (java.util.stream.Collectors)16 Utils (com.vmware.xenon.common.Utils)15 HashMap (java.util.HashMap)15 Map (java.util.Map)15 QueryUtils (com.vmware.photon.controller.model.query.QueryUtils)14 ServiceTypeCluster (com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster)14 DeferredResult (com.vmware.xenon.common.DeferredResult)14 Set (java.util.Set)14 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)14 PhotonModelUtils (com.vmware.photon.controller.model.resources.util.PhotonModelUtils)13 Operation (com.vmware.xenon.common.Operation)13 AzureConstants (com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants)12 ResourceState (com.vmware.photon.controller.model.resources.ResourceState)12 TagState (com.vmware.photon.controller.model.resources.TagService.TagState)12 StringUtils (org.apache.commons.lang3.StringUtils)12