Search in sources :

Example 1 with QueryTask

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

the class LongRunEndToEndStatsAggregationTest method verifyTimeBinMetrics.

/**
 * Performs check to verify time bin metrics are available for every compute resource and also
 * checks the occurrence of estimated charges for one of the resource metrics.
 */
private void verifyTimeBinMetrics(ServiceDocumentQueryResult res) {
    List<Object> results = new ArrayList<>();
    for (String computeResourceLink : res.documentLinks) {
        for (String metricName : TestAWSSetupUtils.getMetricNames()) {
            QueryTask.QuerySpecification querySpec = new QueryTask.QuerySpecification();
            querySpec.query = QueryTask.Query.Builder.create().addKindFieldClause(ResourceMetrics.class).addFieldClause(ServiceDocument.FIELD_NAME_SELF_LINK, UriUtils.buildUriPath(ResourceMetricsService.FACTORY_LINK, computeResourceLink), QueryTask.QueryTerm.MatchType.PREFIX).addRangeClause(QuerySpecification.buildCompositeFieldName(ResourceMetrics.FIELD_NAME_ENTRIES, metricName), NumericRange.createDoubleRange(0.0, Double.MAX_VALUE, true, true)).build();
            QueryTask qt = QueryTask.Builder.createDirectTask().addOption(QueryTask.QuerySpecification.QueryOption.TOP_RESULTS).addOption(QueryTask.QuerySpecification.QueryOption.INCLUDE_ALL_VERSIONS).setResultLimit(1).addOption(QueryTask.QuerySpecification.QueryOption.EXPAND_CONTENT).addOption(QueryTask.QuerySpecification.QueryOption.SORT).orderDescending(ServiceDocument.FIELD_NAME_SELF_LINK, ServiceDocumentDescription.TypeName.STRING).setQuery(querySpec.query).build();
            this.host.createQueryTaskService(qt, false, true, qt, null);
            if (qt.results.documentLinks.size() > 0 && qt.results.documentLinks.get(0) != null) {
                results.add(qt.results.documents.get(qt.results.documentLinks.get(0)));
            }
        }
    }
    long expectedExpirationTime = Utils.getNowMicrosUtc() + TimeUnit.DAYS.toMicros(DEFAULT_RETENTION_LIMIT_DAYS);
    boolean estimatedChargeFound = false;
    for (Object aggrDocument : results) {
        ResourceMetrics aggrMetric = Utils.fromJson(aggrDocument, ResourceMetrics.class);
        // Make sure all the documents have expiration time set.
        assertTrue("Expiration time is not correctly set.", aggrMetric.documentExpirationTimeMicros < expectedExpirationTime);
        // The assertion here checks whether we are aggregating only on latest value. To
        // that effect, here is the breakdown for the check:
        // count = num of resources: one value for each resource
        // sum = null: not specified in the aggregate type set
        assertNotNull("Value is not set", aggrMetric.entries.get(0));
    }
    assertTrue(estimatedChargeFound);
}
Also used : QuerySpecification(com.vmware.xenon.services.common.QueryTask.QuerySpecification) ResourceMetrics(com.vmware.photon.controller.model.monitoring.ResourceMetricsService.ResourceMetrics) QueryTask(com.vmware.xenon.services.common.QueryTask) ArrayList(java.util.ArrayList) QuerySpecification(com.vmware.xenon.services.common.QueryTask.QuerySpecification)

Example 2 with QueryTask

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

the class LongRunEndToEndStatsCollectionTest method getResourceMetrics.

/**
 * Method returns ResourceMetrics collected during stats collection for a resource endpoint
 * for the given resource metric key.
 */
private ResourceMetrics getResourceMetrics(String resourceLink, String metricKey) {
    QueryTask qt = QueryTask.Builder.createDirectTask().addOption(QueryOption.TOP_RESULTS).addOption(QueryOption.INCLUDE_ALL_VERSIONS).setResultLimit(1).addOption(QueryOption.EXPAND_CONTENT).addOption(QueryOption.SORT).orderDescending(ServiceDocument.FIELD_NAME_SELF_LINK, ServiceDocumentDescription.TypeName.STRING).setQuery(QueryTask.Query.Builder.create().addKindFieldClause(ResourceMetrics.class).addFieldClause(ServiceDocument.FIELD_NAME_SELF_LINK, UriUtils.buildUriPath(ResourceMetricsService.FACTORY_LINK, UriUtils.getLastPathSegment(resourceLink)), QueryTask.QueryTerm.MatchType.PREFIX).addRangeClause(QueryTask.QuerySpecification.buildCompositeFieldName(ResourceMetrics.FIELD_NAME_ENTRIES, metricKey), QueryTask.NumericRange.createDoubleRange(0.0, Double.MAX_VALUE, true, true)).build()).build();
    this.host.createQueryTaskService(qt, false, true, qt, null);
    ResourceMetrics resourceMetric = null;
    if (qt.results.documentLinks.size() > 0) {
        String documentLink = qt.results.documentLinks.get(0);
        resourceMetric = Utils.fromJson(qt.results.documents.get(documentLink), ResourceMetrics.class);
    }
    return resourceMetric;
}
Also used : ResourceMetrics(com.vmware.photon.controller.model.monitoring.ResourceMetricsService.ResourceMetrics) QueryTask(com.vmware.xenon.services.common.QueryTask)

Example 3 with QueryTask

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

the class TestAWSEnumerationDocumentCountInLongRun method storeDocumentLinksFromComputeStates.

/**
 * Gets and stores resource pool links and network interface links by querying
 * given instance IDs.
 * @param instanceIdList List of instance IDs provisioned by the test
 */
private void storeDocumentLinksFromComputeStates(List<String> instanceIdList) {
    // Query to get all compute state documents associated with list of instance IDs.
    QueryTask.Query computeStateQuery = QueryTask.Query.Builder.create().addKindFieldClause(ComputeState.class).addInClause(ComputeState.FIELD_NAME_ID, instanceIdList).build();
    QueryTask q = QueryTask.Builder.createDirectTask().setQuery(computeStateQuery).addOption(QueryTask.QuerySpecification.QueryOption.EXPAND_CONTENT).build();
    Operation queryComputeState = QueryUtils.createQueryTaskOperation(this.host, q, ServiceTypeCluster.INVENTORY_SERVICE).setReferer(this.host.getUri());
    Operation queryResponse = this.host.waitForResponse(queryComputeState);
    Assert.assertTrue("Error retrieving compute states", queryResponse.getStatusCode() == 200);
    QueryTask qt = queryResponse.getBody(QueryTask.class);
    // Store all compute links
    this.computeStateLinks.addAll(qt.results.documentLinks);
    // Store resource pool links and network links from all compute states.
    for (String documentLink : this.computeStateLinks) {
        ComputeState cs = Utils.fromJson(qt.results.documents.get(documentLink), ComputeState.class);
        this.resourcePoolLinks.add(cs.resourcePoolLink);
        this.networkInterfaceLinks.addAll(cs.networkInterfaceLinks);
    }
}
Also used : ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) QueryTask(com.vmware.xenon.services.common.QueryTask) Operation(com.vmware.xenon.common.Operation)

Example 4 with QueryTask

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

the class AWSComputeDescriptionEnumerationAdapterService method getLocalComputeDescriptions.

/**
 * Get all the compute descriptions already in the system and filtered by
 * - Supported Children (Docker Container)
 * - Environment name(AWS),
 * - Name (instance type),
 * - ZoneId(placement).
 * - Endpoint link
 */
private void getLocalComputeDescriptions(AWSComputeDescriptionCreationServiceContext context, AWSComputeDescCreationStage next) {
    QueryTask queryTask = getCDsRepresentingVMsInLocalSystemCreatedByEnumerationQuery(context.representativeComputeDescriptionSet, context.cdState.tenantLinks, context.cdState.regionId, context.cdState.parentComputeLink, context.cdState.endpointLink);
    // create the query to find an existing compute description
    QueryUtils.startInventoryQueryTask(this, queryTask).whenComplete((qrt, e) -> {
        if (e != null) {
            logWarning(() -> String.format("Failure retrieving query results: %s", e.toString()));
            finishWithFailure(context, e);
            return;
        }
        if (qrt != null && qrt.results.documentCount > 0) {
            for (Object s : qrt.results.documents.values()) {
                ComputeDescription localComputeDescription = Utils.fromJson(s, ComputeDescription.class);
                context.localComputeDescriptionMap.put(getKeyForComputeDescriptionFromCD(localComputeDescription), localComputeDescription);
            }
            logFine(() -> String.format("%d compute descriptions found", context.localComputeDescriptionMap.size()));
        } else {
            logFine(() -> "No compute descriptions found");
        }
        context.creationStage = next;
        handleComputeDescriptionCreation(context);
    });
}
Also used : QueryTask(com.vmware.xenon.services.common.QueryTask) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription)

Example 5 with QueryTask

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

the class AWSComputeStateCreationAdapterService method getRelatedComputeDescriptions.

/**
 * Looks up the compute descriptions associated with the compute states to be created in the
 * system.
 */
private void getRelatedComputeDescriptions(AWSComputeStateCreationContext context, AWSComputeStateCreationStage next) {
    // Get the related compute descriptions for all the compute states are to be updated and
    // created.
    Set<InstanceDescKey> representativeCDSet = getRepresentativeListOfCDsFromInstanceList(context.request.instancesToBeCreated, context.request.zones);
    representativeCDSet.addAll(getRepresentativeListOfCDsFromInstanceList(context.request.instancesToBeUpdated.values(), context.request.zones));
    if (representativeCDSet.isEmpty()) {
        context.creationStage = next;
        handleComputeStateCreateOrUpdate(context);
        return;
    }
    QueryTask queryTask = getCDsRepresentingVMsInLocalSystemCreatedByEnumerationQuery(representativeCDSet, context.request.tenantLinks, context.request.regionId, context.request.parentComputeLink, context.request.endpointLink);
    queryTask.documentExpirationTimeMicros = Utils.getNowMicrosUtc() + QUERY_TASK_EXPIRY_MICROS;
    // create the query to find an existing compute description
    QueryUtils.startInventoryQueryTask(this, queryTask).whenComplete((qrt, e) -> {
        if (e != null) {
            logWarning(() -> String.format("Failure retrieving query results: %s", e.toString()));
            finishWithFailure(context, e);
            return;
        }
        if (qrt != null && qrt.results.documentCount > 0) {
            for (Object s : qrt.results.documents.values()) {
                ComputeDescription localComputeDescription = Utils.fromJson(s, ComputeDescription.class);
                context.computeDescriptionMap.put(getKeyForComputeDescriptionFromCD(localComputeDescription), localComputeDescription.documentSelfLink);
            }
            logFine(() -> String.format("%d compute descriptions found", context.computeDescriptionMap.size()));
        } else {
            logFine(() -> "No compute descriptions found");
        }
        context.creationStage = next;
        handleComputeStateCreateOrUpdate(context);
    });
}
Also used : QueryTask(com.vmware.xenon.services.common.QueryTask) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) InstanceDescKey(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils.InstanceDescKey)

Aggregations

QueryTask (com.vmware.xenon.services.common.QueryTask)147 Query (com.vmware.xenon.services.common.QueryTask.Query)62 Operation (com.vmware.xenon.common.Operation)61 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)39 ArrayList (java.util.ArrayList)26 QueryUtils (com.vmware.photon.controller.model.query.QueryUtils)20 List (java.util.List)20 ResourceMetrics (com.vmware.photon.controller.model.monitoring.ResourceMetricsService.ResourceMetrics)19 UriUtils (com.vmware.xenon.common.UriUtils)18 Utils (com.vmware.xenon.common.Utils)17 HashSet (java.util.HashSet)16 HashMap (java.util.HashMap)14 QueryOption (com.vmware.xenon.services.common.QueryTask.QuerySpecification.QueryOption)12 TimeUnit (java.util.concurrent.TimeUnit)12 ComputeEnumerateResourceRequest (com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest)11 AdapterUtils (com.vmware.photon.controller.model.adapters.util.AdapterUtils)11 ComputeDescription (com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription)11 ServiceTypeCluster (com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster)11 URI (java.net.URI)11 Set (java.util.Set)11