Search in sources :

Example 36 with QueryTask

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

the class NicSecurityGroupsTaskService method validateSecurityGroupsEndpoint.

/**
 * Validate that all security groups belong to the same endpoint
 */
private DeferredResult<Void> validateSecurityGroupsEndpoint(NicSecurityGroupsTaskState state) {
    QueryTask.Query query = QueryTask.Query.Builder.create().addKindFieldClause(SecurityGroupState.class).addInClause(ServiceDocument.FIELD_NAME_SELF_LINK, state.securityGroupLinks).build();
    QueryTask queryTask = QueryTask.Builder.createDirectTask().setQuery(query).addSelectTerm(SecurityGroupState.FIELD_NAME_ENDPOINT_LINK).addOption(QueryTask.QuerySpecification.QueryOption.EXPAND_SELECTED_FIELDS).build();
    return QueryUtils.startInventoryQueryTask(this, queryTask).thenApply(qrt -> {
        AssertUtil.assertTrue(qrt != null && qrt.results.documentCount > 0, String.format("Could not find security groups with links %s", state.securityGroupLinks));
        Set<String> endpointLinks = qrt.results.documents.values().stream().map(o -> Utils.fromJson(o, SecurityGroupState.class).endpointLink).collect(Collectors.toSet());
        // we only support security groups from the same endpoint for the same request
        if (endpointLinks.size() != 1) {
            throw new IllegalArgumentException("All security groups must belong to the same endpoint.");
        }
        if (endpointLinks.iterator().next() == null) {
            throw new IllegalArgumentException("All security groups must have endpoint link set.");
        }
        return null;
    });
}
Also used : LINK(com.vmware.xenon.common.ServiceDocumentDescription.PropertyUsageOption.LINK) QueryTask(com.vmware.xenon.services.common.QueryTask) ServiceDocument(com.vmware.xenon.common.ServiceDocument) SecurityGroupState(com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState) Utils(com.vmware.xenon.common.Utils) UriPaths(com.vmware.photon.controller.model.UriPaths) Map(java.util.Map) PropertyUsageOption(com.vmware.xenon.common.ServiceDocumentDescription.PropertyUsageOption) URI(java.net.URI) AssertUtil(com.vmware.photon.controller.model.util.AssertUtil) FIXED_ITEM_NAME(com.vmware.xenon.common.ServiceDocumentDescription.PropertyIndexingOption.FIXED_ITEM_NAME) Operation(com.vmware.xenon.common.Operation) NicSecurityGroupsRequest(com.vmware.photon.controller.model.adapterapi.NicSecurityGroupsRequest) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) Set(java.util.Set) OperationRequestType(com.vmware.photon.controller.model.adapterapi.NicSecurityGroupsRequest.OperationRequestType) Collectors(java.util.stream.Collectors) OPTIONAL(com.vmware.xenon.common.ServiceDocumentDescription.PropertyUsageOption.OPTIONAL) List(java.util.List) NicSecurityGroupsTaskState(com.vmware.photon.controller.model.tasks.NicSecurityGroupsTaskService.NicSecurityGroupsTaskState) DeferredResult(com.vmware.xenon.common.DeferredResult) UriUtils(com.vmware.xenon.common.UriUtils) TaskState(com.vmware.xenon.common.TaskState) REQUIRED(com.vmware.xenon.common.ServiceDocumentDescription.PropertyUsageOption.REQUIRED) EXPAND(com.vmware.xenon.common.ServiceDocumentDescription.PropertyIndexingOption.EXPAND) TaskService(com.vmware.xenon.services.common.TaskService) PhotonModelUriUtils.createInventoryUri(com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri) QueryTask(com.vmware.xenon.services.common.QueryTask) SecurityGroupState(com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState)

Example 37 with QueryTask

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

the class ProvisioningUtils method queryComputeInstancesByType.

public static ServiceDocumentQueryResult queryComputeInstancesByType(VerificationHost host, long desiredCount, String type, boolean exactCountFlag) throws Throwable {
    QueryTask.Query kindClause = new QueryTask.Query().setTermPropertyName(ServiceDocument.FIELD_NAME_KIND).setTermMatchValue(Utils.buildKind(ComputeState.class));
    kindClause.occurance = Occurance.MUST_OCCUR;
    QueryTask.Query typeClause = new QueryTask.Query().setTermPropertyName(ComputeState.FIELD_NAME_TYPE).setTermMatchValue(type);
    kindClause.occurance = Occurance.MUST_OCCUR;
    QueryTask.QuerySpecification querySpec = new QueryTask.QuerySpecification();
    querySpec.query.addBooleanClause(kindClause);
    querySpec.query.addBooleanClause(typeClause);
    QueryTask[] tasks = new QueryTask[1];
    host.waitFor("", () -> {
        QueryTask task = QueryTask.create(querySpec).setDirect(true);
        host.createQueryTaskService(UriUtils.buildUri(host, ServiceUriPaths.CORE_QUERY_TASKS), task, false, true, task, null);
        if (exactCountFlag) {
            if (task.results.documentLinks.size() == desiredCount) {
                tasks[0] = task;
                return true;
            }
        } else {
            if (task.results.documentLinks.size() >= desiredCount) {
                tasks[0] = task;
                return true;
            }
        }
        host.log("Expected %d, got %d, Query task: %s", desiredCount, task.results.documentLinks.size(), task);
        return false;
    });
    QueryTask resultTask = tasks[0];
    return resultTask.results;
}
Also used : ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) QueryTask(com.vmware.xenon.services.common.QueryTask)

Example 38 with QueryTask

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

the class TagGroomerTaskServiceTest method getTags.

/**
 * Query for tagLinks
 */
public List<TagState> getTags(QueryTask queryTask) {
    Operation postQuery = Operation.createPost(UriUtils.buildUri(this.host, ServiceUriPaths.CORE_LOCAL_QUERY_TASKS)).setBody(queryTask);
    Operation queryResponse = this.host.waitForResponse(postQuery);
    if (queryResponse.getStatusCode() != 200) {
        return null;
    }
    QueryTask response = queryResponse.getBody(QueryTask.class);
    List<TagState> tagList = new ArrayList<>();
    response.results.documents.values().forEach(tagState -> {
        TagState ts = Utils.fromJson(tagState, TagState.class);
        tagList.add(ts);
    });
    return tagList;
}
Also used : QueryTask(com.vmware.xenon.services.common.QueryTask) ArrayList(java.util.ArrayList) Operation(com.vmware.xenon.common.Operation) TagState(com.vmware.photon.controller.model.resources.TagService.TagState)

Example 39 with QueryTask

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

the class SingleResourceStatsAggregationTaskService method getLastRollupTimeFromQuery.

private void getLastRollupTimeFromQuery(SingleResourceStatsAggregationTaskState currentState, Map<String, Long> lastUpdateMap) {
    List<Operation> operations = new ArrayList<>();
    for (String metricName : currentState.metricNames) {
        List<String> rollupKeys = buildRollupKeys(metricName);
        for (String rollupKey : rollupKeys) {
            // if the last update time was computed based on the memory stat, move on
            if (lastUpdateMap.get(rollupKey) != null) {
                continue;
            }
            String resourceId = UriUtils.getLastPathSegment(currentState.resourceLink);
            String metricSelfLink = UriUtils.buildUriPath(ResourceMetricsService.FACTORY_LINK, resourceId);
            Query.Builder builder = Query.Builder.create();
            builder.addKindFieldClause(ResourceMetrics.class);
            builder.addFieldClause(ResourceMetrics.FIELD_NAME_SELF_LINK, metricSelfLink, MatchType.PREFIX);
            builder.addRangeClause(QuerySpecification.buildCompositeFieldName(ResourceMetrics.FIELD_NAME_ENTRIES, rollupKey), NumericRange.createDoubleRange(0.0, Double.MAX_VALUE, true, true));
            QueryTask queryTask = Builder.createDirectTask().addOption(QueryOption.SORT).addOption(QueryOption.TOP_RESULTS).addOption(QueryOption.INCLUDE_ALL_VERSIONS).addOption(QueryOption.EXPAND_CONTENT).orderDescending(ServiceDocument.FIELD_NAME_SELF_LINK, TypeName.STRING).setResultLimit(1).setQuery(builder.build()).build();
            Operation op = QueryUtils.createQueryTaskOperation(this, queryTask, ServiceTypeCluster.METRIC_SERVICE);
            logInfo(() -> String.format("Invoking a query to obtain last rollup time for %s ", currentState.resourceLink));
            operations.add(op);
        }
    }
    // flood the system with lot of queries.
    if (operations.size() == 0) {
        SingleResourceStatsAggregationTaskState patchBody = new SingleResourceStatsAggregationTaskState();
        patchBody.taskInfo = TaskUtils.createTaskState(TaskStage.STARTED);
        // setting hasResources for purpose of test cases
        patchBody.hasResources = currentState.hasResources;
        patchBody.taskStage = StatsAggregationStage.INIT_RESOURCE_QUERY;
        patchBody.lastRollupTimeForMetric = lastUpdateMap;
        sendSelfPatch(patchBody);
        return;
    }
    OperationSequence opSequence = null;
    for (Operation operation : operations) {
        if (opSequence == null) {
            opSequence = OperationSequence.create(operation);
            continue;
        }
        opSequence = opSequence.next(operation);
    }
    opSequence.setCompletion((ops, failures) -> {
        if (failures != null) {
            sendSelfFailurePatch(currentState, failures.values().iterator().next().getMessage());
            return;
        }
        for (Operation operation : ops.values()) {
            QueryTask response = operation.getBody(QueryTask.class);
            for (Object obj : response.results.documents.values()) {
                ResourceMetrics resourceMetrics = Utils.fromJson(obj, ResourceMetrics.class);
                for (String metricName : resourceMetrics.entries.keySet()) {
                    lastUpdateMap.replace(metricName, resourceMetrics.timestampMicrosUtc);
                }
            }
        }
        SingleResourceStatsAggregationTaskState patchBody = new SingleResourceStatsAggregationTaskState();
        patchBody.taskInfo = TaskUtils.createTaskState(TaskStage.STARTED);
        patchBody.hasResources = currentState.hasResources;
        patchBody.taskStage = StatsAggregationStage.INIT_RESOURCE_QUERY;
        patchBody.lastRollupTimeForMetric = lastUpdateMap;
        sendSelfPatch(patchBody);
    });
    opSequence.sendWith(this);
}
Also used : ResourceMetrics(com.vmware.photon.controller.model.monitoring.ResourceMetricsService.ResourceMetrics) QueryTask(com.vmware.xenon.services.common.QueryTask) Query(com.vmware.xenon.services.common.QueryTask.Query) OperationSequence(com.vmware.xenon.common.OperationSequence) ArrayList(java.util.ArrayList) Operation(com.vmware.xenon.common.Operation)

Example 40 with QueryTask

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

the class SingleResourceStatsAggregationTaskService method getRawMetrics.

private void getRawMetrics(SingleResourceStatsAggregationTaskState currentState, QueryTask resourceQueryTask, Map<String, Set<String>> metricsToBeQueried, Map<String, SortedMap<Long, List<TimeBin>>> inMemoryStats) {
    if (metricsToBeQueried == null || metricsToBeQueried.isEmpty()) {
        aggregateMetrics(currentState, resourceQueryTask, null, inMemoryStats);
        return;
    }
    Query.Builder overallQueryBuilder = Query.Builder.create();
    for (Entry<String, Set<String>> entry : metricsToBeQueried.entrySet()) {
        String resourceId = entry.getKey();
        for (String metricKey : entry.getValue()) {
            logFine(() -> String.format("Querying raw metrics from disk for %s", metricKey));
            Long range = null;
            int binSize = 0;
            for (Entry<String, Long> metricEntry : currentState.lastRollupTimeForMetric.entrySet()) {
                if (metricEntry.getKey().startsWith(metricKey)) {
                    if (range == null || range > metricEntry.getValue()) {
                        binSize = lookupBinSize(metricEntry.getKey());
                        range = metricEntry.getValue();
                    }
                }
            }
            Query.Builder builder = Query.Builder.create(Occurance.SHOULD_OCCUR);
            builder.addKindFieldClause(ResourceMetrics.class);
            builder.addFieldClause(ServiceDocument.FIELD_NAME_SELF_LINK, UriUtils.buildUriPath(ResourceMetricsService.FACTORY_LINK, resourceId), MatchType.PREFIX);
            builder.addRangeClause(QuerySpecification.buildCompositeFieldName(ResourceMetrics.FIELD_NAME_ENTRIES, metricKey), NumericRange.createDoubleRange(0.0, Double.MAX_VALUE, true, true));
            if (range != null && range != 0) {
                builder.addRangeClause(ResourceMetrics.FIELD_NAME_TIMESTAMP, NumericRange.createGreaterThanOrEqualRange(StatsUtil.computeIntervalBeginMicros(range - 1, binSize)));
            }
            overallQueryBuilder.addClause(builder.build());
        }
    }
    // create a set of rollup metric keys we are interested in and the timestamp
    // to rollup from for each
    Set<RollupMetricHolder> rollupMetricHolder = new HashSet<>();
    for (Entry<String, Long> metricEntry : currentState.lastRollupTimeForMetric.entrySet()) {
        RollupMetricHolder metric = new RollupMetricHolder();
        metric.rollupKey = metricEntry.getKey();
        if (metricEntry.getValue() != null && metricEntry.getValue() != 0) {
            metric.beginTimestampMicros = StatsUtil.computeIntervalBeginMicros(metricEntry.getValue() - 1, lookupBinSize(metricEntry.getKey()));
        }
        rollupMetricHolder.add(metric);
    }
    QueryTask task = QueryTask.Builder.createDirectTask().addOption(QueryOption.EXPAND_CONTENT).addOption(QueryOption.TOP_RESULTS).setResultLimit(RAW_METRICS_LIMIT).setQuery(overallQueryBuilder.build()).build();
    task.documentExpirationTimeMicros = Utils.getNowMicrosUtc() + QueryUtils.MINUTE_IN_MICROS;
    QueryUtils.startQueryTask(this, task, ServiceTypeCluster.METRIC_SERVICE).whenComplete((response, queryEx) -> {
        if (queryEx != null) {
            sendSelfFailurePatch(currentState, queryEx.getMessage());
            return;
        }
        Map<String, List<ResourceMetrics>> rawMetricsForKey = new HashMap<>();
        for (Object obj : response.results.documents.values()) {
            ResourceMetrics rawMetric = Utils.fromJson(obj, ResourceMetrics.class);
            for (RollupMetricHolder metric : rollupMetricHolder) {
                for (String rawMetricKey : rawMetric.entries.keySet()) {
                    if (!rawMetricKey.contains(stripRollupKey(metric.rollupKey))) {
                        continue;
                    }
                    // we want to consider raw metrics with the specified key and the appropriate timestamp
                    if ((metric.beginTimestampMicros == null || rawMetric.timestampMicrosUtc >= metric.beginTimestampMicros)) {
                        List<ResourceMetrics> rawMetricResultSet = rawMetricsForKey.get(metric.rollupKey);
                        if (rawMetricResultSet == null) {
                            rawMetricResultSet = new ArrayList<>();
                            rawMetricsForKey.put(metric.rollupKey, rawMetricResultSet);
                        }
                        rawMetricResultSet.add(rawMetric);
                    }
                }
            }
        }
        aggregateMetrics(currentState, resourceQueryTask, rawMetricsForKey, inMemoryStats);
    });
}
Also used : EnumSet(java.util.EnumSet) Set(java.util.Set) HashSet(java.util.HashSet) Query(com.vmware.xenon.services.common.QueryTask.Query) HashMap(java.util.HashMap) ResourceMetrics(com.vmware.photon.controller.model.monitoring.ResourceMetricsService.ResourceMetrics) QueryTask(com.vmware.xenon.services.common.QueryTask) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

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