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;
});
}
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;
}
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;
}
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);
}
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);
});
}
Aggregations