use of com.vmware.xenon.services.common.QueryTask.Query in project photon-model by vmware.
the class TagGroomerTaskService method queryDocumentsTaggedWithTag.
/**
* Issue query for documents that contain the specific tagLink.
*/
private void queryDocumentsTaggedWithTag(TagDeletionRequest task, SubStage next) {
if (task.tagsMap.isEmpty()) {
task.subStage = SubStage.FINISHED;
sendSelfPatch(task);
return;
}
Set<String> tagLinks = new HashSet<>();
task.tagsMap.forEach((k, v) -> tagLinks.add(k));
Query query = Query.Builder.create().addInCollectionItemClause(ResourceState.FIELD_NAME_TAG_LINKS, tagLinks).build();
// get all documents that contain the links.
QueryTask queryTask = QueryTask.Builder.createDirectTask().addOption(QueryOption.SELECT_LINKS).addLinkTerm(ResourceState.FIELD_NAME_TAG_LINKS).setResultLimit(MAX_QUERY_RESULT_LIMIT).setQuery(query).build();
QueryUtils.startInventoryQueryTask(this, queryTask).whenComplete((response, e) -> {
if (e != null) {
task.failureMessage = e.getMessage();
task.subStage = SubStage.FAILED;
sendSelfPatch(task);
return;
}
if (response.results != null) {
task.docsNextPageLink = response.results.nextPageLink;
task.subStage = next;
} else {
task.subStage = SubStage.GET_TAGS_NEXT_PAGE;
}
sendSelfPatch(task);
});
}
use of com.vmware.xenon.services.common.QueryTask.Query in project photon-model by vmware.
the class EndpointRemovalTaskService method disassociateResources.
/**
* Disassociate computes discovered with this endpoint.
*/
private void disassociateResources(EndpointRemovalTaskState state, SubStage next) {
Query.Builder qBuilder = Query.Builder.create().addKindFieldClause(ComputeState.class).addFieldClause(ComputeState.FIELD_NAME_PARENT_LINK, state.endpoint.computeLink);
QueryTask queryTask = QueryTask.Builder.createDirectTask().setQuery(qBuilder.build()).addOption(QueryOption.EXPAND_CONTENT).setResultLimit(QueryUtils.DEFAULT_RESULT_LIMIT).build();
queryTask.documentExpirationTimeMicros = state.documentExpirationTimeMicros;
queryTask.documentSelfLink = UUID.randomUUID().toString();
queryTask.tenantLinks = state.tenantLinks;
QueryUtils.startInventoryQueryTask(this, queryTask).whenComplete((responseTask, throwable) -> {
if (throwable != null) {
logWarning(() -> String.format("Failure retrieving query results: " + "%s", throwable.toString()));
sendFailureSelfPatch(throwable);
return;
}
if (responseTask.results.nextPageLink == null) {
logFine("Query returned no resources to update");
sendSelfPatch(TaskStage.STARTED, next);
return;
}
// process one page of resource links at a time
disassociateResourceHelper(responseTask.results.nextPageLink, next, state.endpointLink);
});
}
use of com.vmware.xenon.services.common.QueryTask.Query 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.Query in project photon-model by vmware.
the class StatsAggregationTaskServiceTest method testStatsAggregation.
private void testStatsAggregation(boolean testOnCluster) throws Throwable {
VerificationHost metricHost = null;
if (testOnCluster) {
metricHost = this.setupMetricHost();
}
// Use this.host if metricHost is null.
VerificationHost verificationHost = (metricHost == null ? this.host : metricHost);
// create a resource pool
ResourcePoolState rpState = new ResourcePoolState();
rpState.name = "testName";
ResourcePoolState rpReturnState = postServiceSynchronously(ResourcePoolService.FACTORY_LINK, rpState, ResourcePoolState.class);
ComputeDescription cDesc = new ComputeDescription();
cDesc.name = rpState.name;
cDesc.statsAdapterReference = UriUtils.buildUri(this.host, MockStatsAdapter.SELF_LINK);
ComputeDescription descReturnState = postServiceSynchronously(ComputeDescriptionService.FACTORY_LINK, cDesc, ComputeDescription.class);
ComputeState computeState = new ComputeState();
computeState.name = rpState.name;
computeState.descriptionLink = descReturnState.documentSelfLink;
computeState.resourcePoolLink = rpReturnState.documentSelfLink;
List<String> computeLinks = new ArrayList<>();
for (int i = 0; i < this.numResources; i++) {
ComputeState res = postServiceSynchronously(ComputeService.FACTORY_LINK, computeState, ComputeState.class);
computeLinks.add(res.documentSelfLink);
}
// kick off an aggregation task when stats are not populated
StatsAggregationTaskState aggregationTaskState = new StatsAggregationTaskState();
Query taskQuery = Query.Builder.create().addFieldClause(ComputeState.FIELD_NAME_RESOURCE_POOL_LINK, rpReturnState.documentSelfLink).build();
aggregationTaskState.query = taskQuery;
aggregationTaskState.metricNames = Collections.singleton(MockStatsAdapter.KEY_1);
aggregationTaskState.taskInfo = TaskState.createDirect();
postServiceSynchronously(StatsAggregationTaskService.FACTORY_LINK, aggregationTaskState, StatsAggregationTaskState.class);
this.host.waitFor("Error waiting for stats", () -> {
ServiceDocumentQueryResult aggrRes = verificationHost.getFactoryState(UriUtils.buildUri(verificationHost, ResourceMetricsService.FACTORY_LINK));
// Expect 0 stats because they're not collected yet
if (aggrRes.documentCount == 0) {
return true;
}
return false;
});
StatsCollectionTaskState collectionTaskState = new StatsCollectionTaskState();
collectionTaskState.resourcePoolLink = rpReturnState.documentSelfLink;
collectionTaskState.taskInfo = TaskState.createDirect();
postServiceSynchronously(StatsCollectionTaskService.FACTORY_LINK, collectionTaskState, StatsCollectionTaskState.class);
int numberOfRawMetrics = this.numResources * 4;
// kick off an aggregation task
aggregationTaskState = new StatsAggregationTaskState();
aggregationTaskState.query = taskQuery;
aggregationTaskState.metricNames = Collections.singleton(MockStatsAdapter.KEY_1);
aggregationTaskState.taskInfo = TaskState.createDirect();
postServiceSynchronously(StatsAggregationTaskService.FACTORY_LINK, aggregationTaskState, StatsAggregationTaskState.class);
this.host.waitFor("Error waiting for stats", () -> {
ServiceDocumentQueryResult aggrRes = verificationHost.getFactoryState(UriUtils.buildUri(verificationHost, ResourceMetricsService.FACTORY_LINK));
if (aggrRes.documentCount == this.numResources + numberOfRawMetrics) {
return true;
}
return false;
});
// verify that the aggregation tasks have been deleted
this.host.waitFor("Timeout waiting for task to expire", () -> {
ServiceDocumentQueryResult res = this.host.getFactoryState(UriUtils.buildUri(this.host, StatsAggregationTaskService.FACTORY_LINK));
if (res.documentLinks.size() == 0) {
return true;
}
return false;
});
if (testOnCluster) {
this.cleanUpMetricHost(metricHost);
}
}
use of com.vmware.xenon.services.common.QueryTask.Query in project photon-model by vmware.
the class MetricsClusterTest method testStatsCollectorCreation.
@Test
public void testStatsCollectorCreation() throws Throwable {
// create a resource pool
ResourcePoolState rpState = new ResourcePoolState();
rpState.name = UUID.randomUUID().toString();
ResourcePoolState rpReturnState = postServiceSynchronously(ResourcePoolService.FACTORY_LINK, rpState, ResourcePoolState.class);
// create a compute description for all the computes
ComputeDescription cDesc = new ComputeDescription();
cDesc.name = UUID.randomUUID().toString();
cDesc.statsAdapterReference = UriUtils.buildUri(this.host, MockStatsAdapter.SELF_LINK);
ComputeDescription descReturnState = postServiceSynchronously(ComputeDescriptionService.FACTORY_LINK, cDesc, ComputeDescription.class);
// create multiple computes
ComputeState computeState = new ComputeState();
computeState.name = UUID.randomUUID().toString();
computeState.descriptionLink = descReturnState.documentSelfLink;
computeState.resourcePoolLink = rpReturnState.documentSelfLink;
List<String> computeLinks = new ArrayList<>(this.numResources);
for (int i = 0; i < this.numResources; i++) {
ComputeState res = postServiceSynchronously(ComputeService.FACTORY_LINK, computeState, ComputeState.class);
computeLinks.add(res.documentSelfLink);
}
// Run a stats collection on the resources
StatsCollectionTaskState collectionTaskState = new StatsCollectionTaskState();
collectionTaskState.resourcePoolLink = rpReturnState.documentSelfLink;
collectionTaskState.options = EnumSet.of(TaskOption.SELF_DELETE_ON_COMPLETION);
collectionTaskState.taskInfo = TaskState.createDirect();
postServiceSynchronously(StatsCollectionTaskService.FACTORY_LINK, collectionTaskState, StatsCollectionTaskState.class);
int numberOfRawMetrics = this.numResources * 4;
// verify that the collection tasks have been deleted
this.host.waitFor("Timeout waiting for task to expire", () -> {
ServiceDocumentQueryResult collectRes = this.host.getFactoryState(UriUtils.buildUri(this.host, StatsCollectionTaskService.FACTORY_LINK));
if (collectRes.documentLinks.size() == 0) {
return true;
}
return false;
});
// Get a Count on this.host() for Resource metrics
Long resourceHostResourceMetricCount = this.getDocumentCount(this.host, ResourceMetricsService.FACTORY_LINK);
// Get a Count on this.metricHost() for ResourceMetrics
Long metricHostResourceMetricCount = this.getDocumentCount(this.metricHost, ResourceMetricsService.FACTORY_LINK);
// Count should be 0 on this.host()
assertEquals(0, resourceHostResourceMetricCount.intValue());
// Count should be something on this.metricHost()
assertEquals(this.numResources * 4, metricHostResourceMetricCount.intValue());
// Kick off an Aggregation Task - Verifies that the ResourceMetricQueries are going to the right cluster.
StatsAggregationTaskState aggregationTaskState = new StatsAggregationTaskState();
Query taskQuery = Query.Builder.create().addFieldClause(ComputeState.FIELD_NAME_RESOURCE_POOL_LINK, rpReturnState.documentSelfLink).build();
aggregationTaskState.query = taskQuery;
aggregationTaskState.metricNames = Collections.singleton(MockStatsAdapter.KEY_1);
aggregationTaskState.taskInfo = TaskState.createDirect();
postServiceSynchronously(StatsAggregationTaskService.FACTORY_LINK, aggregationTaskState, StatsAggregationTaskState.class);
// verify that the aggregation tasks have been deleted
this.host.waitFor("Timeout waiting for task to expire", () -> {
ServiceDocumentQueryResult res = this.host.getFactoryState(UriUtils.buildUri(this.host, StatsAggregationTaskService.FACTORY_LINK));
if (res.documentLinks.size() == 0) {
return true;
}
return false;
});
this.host.waitFor("Error waiting for stats", () -> {
ServiceDocumentQueryResult aggrRes = this.metricHost.getFactoryState(UriUtils.buildUri(this.metricHost, ResourceMetricsService.FACTORY_LINK));
if (aggrRes.documentCount == (this.numResources + numberOfRawMetrics)) {
return true;
}
return false;
});
// Get a Count on this.host() for Aggregate metrics
Long resourceHostAggregateMetricCount = this.getDocumentCount(this.host, ResourceMetricsService.FACTORY_LINK);
// Get a Count on this.metricHost() for Aggregate Metrics
Long metricHostAggregateMetricCount = this.getDocumentCount(this.metricHost, ResourceMetricsService.FACTORY_LINK);
// Count should be 0 on this.host()
assertEquals(0, resourceHostAggregateMetricCount.intValue());
// Count should be something on this.metricHost()
assertEquals((this.numResources + numberOfRawMetrics), metricHostAggregateMetricCount.intValue());
}
Aggregations