Search in sources :

Example 76 with Query

use of com.vmware.xenon.services.common.QueryTask.Query in project photon-model by vmware.

the class ResourceGroomerTaskService method buildQueryTask.

/**
 * Builds QueryTask for resource document query and endpoint query.
 * If endpointLink is specified, adds clauses for that and creates a
 * QueryTask to be executed in tenant's context.
 */
private static QueryTask buildQueryTask(EndpointResourceDeletionRequest task, boolean isEndpointQuery, Set<String> currentEndpointLinks) {
    Query query;
    if (isEndpointQuery) {
        query = Query.Builder.create().addInClause(ServiceDocument.FIELD_NAME_SELF_LINK, currentEndpointLinks).addFieldClause(ServiceDocument.FIELD_NAME_UPDATE_ACTION, Action.DELETE.name()).addInCollectionItemClause(ResourceState.FIELD_NAME_TENANT_LINKS, task.tenantLinks).build();
    } else {
        query = Query.Builder.create().addInClause(ServiceDocument.FIELD_NAME_KIND, DOCUMENT_KINDS).addInCollectionItemClause(ResourceState.FIELD_NAME_TENANT_LINKS, task.tenantLinks).build();
        if (isEndpointSpecified(task)) {
            Query endpointSpecificClauses = Query.Builder.create(Occurance.MUST_OCCUR).addFieldClause(ResourceState.FIELD_NAME_ENDPOINT_LINK, task.endpointLink, Occurance.SHOULD_OCCUR).addCollectionItemClause(ResourceState.FIELD_NAME_ENDPOINT_LINKS, task.endpointLink, Occurance.SHOULD_OCCUR).build();
            query.addBooleanClause(endpointSpecificClauses);
        }
    }
    QueryTask queryTask = QueryTask.Builder.createDirectTask().setQuery(query).addOption(QueryOption.EXPAND_CONTENT).setResultLimit(QUERY_RESULT_LIMIT).build();
    if (isEndpointQuery) {
        queryTask.querySpec.options.add(QueryOption.INCLUDE_DELETED);
        queryTask.querySpec.options.add(QueryOption.BROADCAST);
        queryTask.querySpec.options.add(QueryOption.OWNER_SELECTION);
    }
    queryTask.tenantLinks = new ArrayList<>(task.tenantLinks);
    return queryTask;
}
Also used : QueryTask(com.vmware.xenon.services.common.QueryTask) Query(com.vmware.xenon.services.common.QueryTask.Query)

Example 77 with Query

use of com.vmware.xenon.services.common.QueryTask.Query 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 78 with Query

use of com.vmware.xenon.services.common.QueryTask.Query 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)

Example 79 with Query

use of com.vmware.xenon.services.common.QueryTask.Query in project photon-model by vmware.

the class StatsCollectionTaskService method initializeQuery.

private void initializeQuery(Operation op, StatsCollectionTaskState currentState, ResourcePoolState resourcePoolState) {
    // load the RP state, if not already
    if (resourcePoolState == null) {
        sendRequest(Operation.createGet(UriUtils.extendUri(ClusterUtil.getClusterUri(getHost(), ServiceTypeCluster.INVENTORY_SERVICE), currentState.resourcePoolLink)).setCompletion((o, e) -> {
            if (e != null) {
                if (e instanceof ServiceNotFoundException || o.getStatusCode() == Operation.STATUS_CODE_NOT_FOUND) {
                    logInfo(() -> String.format("Resource pool %s seems to have been deleted", currentState.resourcePoolLink));
                } else {
                    logWarning(() -> String.format("Error retrieving resource pool %s: %s", currentState.resourcePoolLink, Utils.toString(e)));
                }
                sendSelfPatch(new StatsCollectionTaskState(), TaskStage.FAILED, patchBody -> {
                    patchBody.taskInfo.failure = Utils.toServiceErrorResponse(e);
                });
                return;
            }
            ResourcePoolState loadedRpState = o.getBody(ResourcePoolState.class);
            initializeQuery(op, currentState, loadedRpState);
        }));
        return;
    }
    int resultLimit = DEFAULT_QUERY_RESULT_LIMIT;
    try {
        resultLimit = (QUERY_RESULT_LIMIT != null) ? Integer.valueOf(QUERY_RESULT_LIMIT) : DEFAULT_QUERY_RESULT_LIMIT;
    } catch (NumberFormatException e) {
        // use the default;
        logWarning(STATS_QUERY_RESULT_LIMIT + " is not a number; Using a default value of " + DEFAULT_QUERY_RESULT_LIMIT);
    }
    Query resourcePoolStateQuery = resourcePoolState.query;
    // Customize default resource pool Query.
    if (currentState.customizationClauses != null && !currentState.customizationClauses.isEmpty()) {
        currentState.customizationClauses.stream().forEach(q -> {
            resourcePoolStateQuery.addBooleanClause(q);
        });
    }
    QueryTask.Builder queryTaskBuilder = QueryTask.Builder.createDirectTask().setQuery(resourcePoolStateQuery).setResultLimit(resultLimit);
    QueryTask qTask = queryTaskBuilder.build();
    QueryUtils.startInventoryQueryTask(this, qTask).whenComplete((queryRsp, queryEx) -> {
        if (queryEx != null) {
            TaskUtils.sendFailurePatch(this, new StatsCollectionTaskState(), queryEx);
            return;
        }
        StatsCollectionTaskState patchBody = new StatsCollectionTaskState();
        if (queryRsp.results.nextPageLink == null) {
            patchBody.taskInfo = TaskUtils.createTaskState(TaskStage.FINISHED);
        } else {
            patchBody.taskInfo = TaskUtils.createTaskState(TaskStage.STARTED);
            patchBody.taskSubStage = StatsCollectionStage.GET_RESOURCES;
            patchBody.nextPageLink = queryRsp.results.nextPageLink;
        }
        TaskUtils.sendPatch(this, patchBody);
    });
}
Also used : Service(com.vmware.xenon.common.Service) ServiceTypeCluster(com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster) QueryTask(com.vmware.xenon.services.common.QueryTask) ResourcePoolState(com.vmware.photon.controller.model.resources.ResourcePoolService.ResourcePoolState) PhotonModelUtils(com.vmware.photon.controller.model.resources.util.PhotonModelUtils) SingleResourceStatsCollectionTaskState(com.vmware.photon.controller.model.tasks.monitoring.SingleResourceStatsCollectionTaskService.SingleResourceStatsCollectionTaskState) ServiceUriPaths(com.vmware.xenon.services.common.ServiceUriPaths) SubTaskService(com.vmware.photon.controller.model.tasks.SubTaskService) Utils(com.vmware.xenon.common.Utils) Query(com.vmware.xenon.services.common.QueryTask.Query) UriPaths(com.vmware.photon.controller.model.UriPaths) TaskFactoryService(com.vmware.xenon.services.common.TaskFactoryService) ServiceNotFoundException(com.vmware.xenon.common.ServiceHost.ServiceNotFoundException) PropertyUsageOption(com.vmware.xenon.common.ServiceDocumentDescription.PropertyUsageOption) URI(java.net.URI) EnumSet(java.util.EnumSet) SubTaskState(com.vmware.photon.controller.model.tasks.SubTaskService.SubTaskState) ServiceTaskCallback(com.vmware.photon.controller.model.tasks.ServiceTaskCallback) Operation(com.vmware.xenon.common.Operation) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) TaskStage(com.vmware.xenon.common.TaskState.TaskStage) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) ClusterUtil(com.vmware.photon.controller.model.util.ClusterUtil) ServiceTaskCallbackResponse(com.vmware.photon.controller.model.tasks.ServiceTaskCallback.ServiceTaskCallbackResponse) UriUtils(com.vmware.xenon.common.UriUtils) TaskState(com.vmware.xenon.common.TaskState) TaskOption(com.vmware.photon.controller.model.tasks.TaskOption) FactoryService(com.vmware.xenon.common.FactoryService) TaskService(com.vmware.xenon.services.common.TaskService) PhotonModelUriUtils.createInventoryUri(com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri) TaskUtils(com.vmware.photon.controller.model.tasks.TaskUtils) ResourcePoolState(com.vmware.photon.controller.model.resources.ResourcePoolService.ResourcePoolState) QueryTask(com.vmware.xenon.services.common.QueryTask) Query(com.vmware.xenon.services.common.QueryTask.Query) ServiceNotFoundException(com.vmware.xenon.common.ServiceHost.ServiceNotFoundException) SingleResourceStatsCollectionTaskState(com.vmware.photon.controller.model.tasks.monitoring.SingleResourceStatsCollectionTaskService.SingleResourceStatsCollectionTaskState)

Example 80 with Query

use of com.vmware.xenon.services.common.QueryTask.Query in project photon-model by vmware.

the class EndpointRemovalTaskService method disassociateAssociatedDocuments.

/**
 * Disassociate associated resource, e.g. enumeration task if started.
 */
private void disassociateAssociatedDocuments(EndpointRemovalTaskState state, Collection<String> documentKinds, SubStage next) {
    Query resourceQuery = getAssociatedDocumentsQuery(state, documentKinds);
    QueryTask resourceQueryTask = QueryTask.Builder.createDirectTask().setQuery(resourceQuery).setResultLimit(QueryUtils.DEFAULT_RESULT_LIMIT).addOption(QueryOption.EXPAND_CONTENT).build();
    resourceQueryTask.tenantLinks = state.tenantLinks;
    QueryUtils.startInventoryQueryTask(this, resourceQueryTask).whenComplete((queryTask, throwable) -> {
        if (throwable != null) {
            logWarning(throwable.getMessage());
            sendFailureSelfPatch(throwable);
            return;
        }
        if (queryTask.results.nextPageLink == null) {
            sendSelfPatch(TaskStage.STARTED, next);
            return;
        }
        disassociateResourceHelper(queryTask.results.nextPageLink, next, state.endpointLink);
    });
}
Also used : QueryTask(com.vmware.xenon.services.common.QueryTask) Query(com.vmware.xenon.services.common.QueryTask.Query)

Aggregations

Query (com.vmware.xenon.services.common.QueryTask.Query)81 QueryTask (com.vmware.xenon.services.common.QueryTask)50 Operation (com.vmware.xenon.common.Operation)39 ArrayList (java.util.ArrayList)29 HashMap (java.util.HashMap)26 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)25 Utils (com.vmware.xenon.common.Utils)22 List (java.util.List)21 Map (java.util.Map)21 QueryUtils (com.vmware.photon.controller.model.query.QueryUtils)20 UriUtils (com.vmware.xenon.common.UriUtils)20 HashSet (java.util.HashSet)20 QueryByPages (com.vmware.photon.controller.model.query.QueryUtils.QueryByPages)19 ServiceTypeCluster (com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster)18 Set (java.util.Set)18 URI (java.net.URI)17 OperationJoin (com.vmware.xenon.common.OperationJoin)16 StatelessService (com.vmware.xenon.common.StatelessService)16 Collections (java.util.Collections)15 Collectors (java.util.stream.Collectors)15