Search in sources :

Example 31 with QueryTask

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

the class ResourceRemovalTaskService method deallocateIpAddressesForResources.

private void deallocateIpAddressesForResources(ResourceRemovalTaskState currentState, QueryTask queryTask, String subTaskLink) {
    // handle empty pages
    if (queryTask.results.documentCount == 0) {
        sendSelfPatch(TaskState.TaskStage.FINISHED, SubStage.FINISHED, null);
        return;
    }
    if (subTaskLink == null) {
        createSubTaskForIpDeallocationCallbacks(currentState, queryTask, link -> deallocateIpAddressesForResources(currentState, queryTask, link));
        return;
    }
    logFine("Starting ip deallocation of %d compute resources using sub task", queryTask.results.documentCount);
    // for each compute resource link in the results, call ResourceIPDeallocationTaskService
    for (String resourceLink : queryTask.results.documentLinks) {
        ResourceIPDeallocationTaskState deallocateTask = new ResourceIPDeallocationTaskState();
        deallocateTask.resourceLink = resourceLink;
        deallocateTask.tenantLinks = currentState.tenantLinks;
        deallocateTask.serviceTaskCallback = ServiceTaskCallback.create(UriUtils.buildPublicUri(getHost(), subTaskLink));
        deallocateTask.serviceTaskCallback.onSuccessFinishTask();
        // Similar to instance deletes - a failure for one network interface deallocate will fail the task
        deallocateTask.serviceTaskCallback.onErrorFailTask();
        this.sendRequest(Operation.createPost(this, ResourceIPDeallocationTaskService.FACTORY_LINK).setBody(deallocateTask).setCompletion((o, ex) -> {
            if (ex != null) {
                logSevere("Failure deallocating IPs for resource: [%s]: %s", resourceLink, ex);
                ResourceOperationResponse subTaskPatchBody = ResourceOperationResponse.finish(resourceLink);
                sendPatch(subTaskLink, subTaskPatchBody);
            } else {
                logFine("Sent deallocation IPs request for resource [%s]", resourceLink);
            }
        }));
    }
}
Also used : Service(com.vmware.xenon.common.Service) QueryByPages(com.vmware.photon.controller.model.query.QueryUtils.QueryByPages) ServiceTypeCluster(com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster) QueryTask(com.vmware.xenon.services.common.QueryTask) ComputeInstanceRequest(com.vmware.photon.controller.model.adapterapi.ComputeInstanceRequest) ServiceUriPaths(com.vmware.xenon.services.common.ServiceUriPaths) Utils(com.vmware.xenon.common.Utils) Query(com.vmware.xenon.services.common.QueryTask.Query) UriPaths(com.vmware.photon.controller.model.UriPaths) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) PropertyUsageOption(com.vmware.xenon.common.ServiceDocumentDescription.PropertyUsageOption) URI(java.net.URI) EnumSet(java.util.EnumSet) ServiceErrorResponse(com.vmware.xenon.common.ServiceErrorResponse) Operation(com.vmware.xenon.common.Operation) ResourceOperationResponse(com.vmware.photon.controller.model.adapterapi.ResourceOperationResponse) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) SnapshotService(com.vmware.photon.controller.model.resources.SnapshotService) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) ResourceIPDeallocationTaskState(com.vmware.photon.controller.model.tasks.ResourceIPDeallocationTaskService.ResourceIPDeallocationTaskState) List(java.util.List) Stream(java.util.stream.Stream) ClusterUtil(com.vmware.photon.controller.model.util.ClusterUtil) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) ServiceTaskCallbackResponse(com.vmware.photon.controller.model.tasks.ServiceTaskCallback.ServiceTaskCallbackResponse) DeferredResult(com.vmware.xenon.common.DeferredResult) UriUtils(com.vmware.xenon.common.UriUtils) QueryOption(com.vmware.xenon.services.common.QueryTask.QuerySpecification.QueryOption) TaskState(com.vmware.xenon.common.TaskState) TaskService(com.vmware.xenon.services.common.TaskService) OperationJoin(com.vmware.xenon.common.OperationJoin) PhotonModelUriUtils.createInventoryUri(com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri) ResourceOperationResponse(com.vmware.photon.controller.model.adapterapi.ResourceOperationResponse) ResourceIPDeallocationTaskState(com.vmware.photon.controller.model.tasks.ResourceIPDeallocationTaskService.ResourceIPDeallocationTaskState)

Example 32 with QueryTask

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

the class ResourceRemovalTaskService method doInstanceDeletes.

private void doInstanceDeletes(ResourceRemovalTaskState currentState, QueryTask queryTask, String subTaskLink) {
    // handle empty pages
    if (queryTask.results.documentCount == 0) {
        sendSelfPatch(currentState.taskInfo.stage, SubStage.DEALLOCATE_IP_ADDRESSES, s -> {
            s.nextPageLink = null;
        });
        return;
    }
    if (subTaskLink == null) {
        createSubTaskForDeleteCallbacks(currentState, queryTask, link -> doInstanceDeletes(currentState, queryTask, link));
        return;
    }
    logFine(() -> String.format("Starting delete of %d compute resources using sub task %s", queryTask.results.documentLinks.size(), subTaskLink));
    // a DELETE request to its associated instance service.
    for (String resourceLink : queryTask.results.documentLinks) {
        URI u = ComputeStateWithDescription.buildUri(UriUtils.buildUri(getHost(), resourceLink));
        sendRequest(Operation.createGet(u).setCompletion((o, e) -> {
            if (e != null) {
                // we do not fail task if one delete failed ...
                // send a FINISHED patch which is what a sub task
                // would do. Since the
                // current state
                // is still at REMOVING_RESOURCES, we will just
                // increment a counter
                ResourceOperationResponse subTaskPatchBody = ResourceOperationResponse.fail(resourceLink, e);
                sendPatch(subTaskLink, subTaskPatchBody);
                return;
            }
            sendInstanceDelete(resourceLink, subTaskLink, o, currentState);
        }));
    }
}
Also used : Service(com.vmware.xenon.common.Service) QueryByPages(com.vmware.photon.controller.model.query.QueryUtils.QueryByPages) ServiceTypeCluster(com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster) QueryTask(com.vmware.xenon.services.common.QueryTask) ComputeInstanceRequest(com.vmware.photon.controller.model.adapterapi.ComputeInstanceRequest) ServiceUriPaths(com.vmware.xenon.services.common.ServiceUriPaths) Utils(com.vmware.xenon.common.Utils) Query(com.vmware.xenon.services.common.QueryTask.Query) UriPaths(com.vmware.photon.controller.model.UriPaths) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) PropertyUsageOption(com.vmware.xenon.common.ServiceDocumentDescription.PropertyUsageOption) URI(java.net.URI) EnumSet(java.util.EnumSet) ServiceErrorResponse(com.vmware.xenon.common.ServiceErrorResponse) Operation(com.vmware.xenon.common.Operation) ResourceOperationResponse(com.vmware.photon.controller.model.adapterapi.ResourceOperationResponse) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) SnapshotService(com.vmware.photon.controller.model.resources.SnapshotService) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) ResourceIPDeallocationTaskState(com.vmware.photon.controller.model.tasks.ResourceIPDeallocationTaskService.ResourceIPDeallocationTaskState) List(java.util.List) Stream(java.util.stream.Stream) ClusterUtil(com.vmware.photon.controller.model.util.ClusterUtil) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) ServiceTaskCallbackResponse(com.vmware.photon.controller.model.tasks.ServiceTaskCallback.ServiceTaskCallbackResponse) DeferredResult(com.vmware.xenon.common.DeferredResult) UriUtils(com.vmware.xenon.common.UriUtils) QueryOption(com.vmware.xenon.services.common.QueryTask.QuerySpecification.QueryOption) TaskState(com.vmware.xenon.common.TaskState) TaskService(com.vmware.xenon.services.common.TaskService) OperationJoin(com.vmware.xenon.common.OperationJoin) PhotonModelUriUtils.createInventoryUri(com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri) ResourceOperationResponse(com.vmware.photon.controller.model.adapterapi.ResourceOperationResponse) URI(java.net.URI)

Example 33 with QueryTask

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

the class TagGroomerTaskService method getDiscoveredNotDeletedTagStates.

/**
 * Collect all tag states with origins ["DISCOVERED"] that have not been soft deleted, and
 * perform soft deletion of stale ones by page.
 *
 * If the tag has multiple origins, do not delete the tag.
 */
private void getDiscoveredNotDeletedTagStates(TagDeletionRequest task, SubStage next) {
    Query query = Query.Builder.create().addKindFieldClause(TagState.class).build();
    Map<String, Query.Occurance> origin = new HashMap<>();
    origin.put(DISCOVERED.toString(), Query.Occurance.MUST_OCCUR);
    origin.put(SYSTEM.toString(), Query.Occurance.MUST_NOT_OCCUR);
    origin.put(USER_DEFINED.toString(), Query.Occurance.MUST_NOT_OCCUR);
    Query externalQuery = createOriginTagQuery(Boolean.TRUE, origin);
    query.addBooleanClause(externalQuery);
    query.addBooleanClause(new Query().setTermPropertyName(TagState.FIELD_NAME_DELETED).setTermMatchValue(Boolean.FALSE.toString()));
    QueryTask queryTask = QueryTask.Builder.createDirectTask().setQuery(query).addOption(QueryOption.EXPAND_CONTENT).setResultLimit(QUERY_RESULT_LIMIT).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 && response.results.nextPageLink != null) {
            task.tagsNextPageLink = response.results.nextPageLink;
            task.subStage = next;
        } else {
            task.subStage = SubStage.FINISHED;
        }
        sendSelfPatch(task);
    });
}
Also used : QueryTask(com.vmware.xenon.services.common.QueryTask) PhotonModelUtils.createOriginTagQuery(com.vmware.photon.controller.model.resources.util.PhotonModelUtils.createOriginTagQuery) Query(com.vmware.xenon.services.common.QueryTask.Query) HashMap(java.util.HashMap) TagState(com.vmware.photon.controller.model.resources.TagService.TagState)

Example 34 with QueryTask

use of com.vmware.xenon.services.common.QueryTask 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);
    });
}
Also used : QueryTask(com.vmware.xenon.services.common.QueryTask) PhotonModelUtils.createOriginTagQuery(com.vmware.photon.controller.model.resources.util.PhotonModelUtils.createOriginTagQuery) Query(com.vmware.xenon.services.common.QueryTask.Query) HashSet(java.util.HashSet)

Example 35 with QueryTask

use of com.vmware.xenon.services.common.QueryTask 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);
    });
}
Also used : ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) QueryTask(com.vmware.xenon.services.common.QueryTask) Query(com.vmware.xenon.services.common.QueryTask.Query)

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