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