Search in sources :

Example 86 with Operation

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

the class ResourceGroomerTaskService method deleteDisassociatePatchStaleDocuments.

/**
 * Deletes documents that have no endpointLinks associated with them.
 * Disassociate documents if they have invalid endpointLinks by sending a collection update
 * patch.
 */
private void deleteDisassociatePatchStaleDocuments(EndpointResourceDeletionRequest task, SubStage next) {
    if (task.documentsToBeDeletedLinks.isEmpty() && task.endpointLinksByDocumentsToBeDisassociated.isEmpty()) {
        task.subStage = next;
        sendSelfPatch(task);
        return;
    }
    List<Operation> deletePatchOperations = new ArrayList<>();
    task.documentsToBeDeletedLinks.stream().forEach(documentLink -> {
        deletePatchOperations.add(Operation.createDelete(this.getHost(), documentLink).setReferer(this.getHost().getUri()));
    });
    task.endpointLinksByDocumentsToBeDisassociated.entrySet().stream().forEach(entry -> {
        Map<String, Collection<Object>> itemsToRemove = Collections.singletonMap(ResourceState.FIELD_NAME_ENDPOINT_LINKS, new ArrayList<>(entry.getValue()));
        ServiceStateCollectionUpdateRequest request = ServiceStateCollectionUpdateRequest.create(null, itemsToRemove);
        deletePatchOperations.add(Operation.createPatch(this.getHost(), entry.getKey()).setBody(request).setReferer(this.getUri()));
        logInfo("Removing endpointLinks: %s from resource: %s", entry.getValue(), entry.getKey());
    });
    task.endpointLinkToBePatchedByDocumentLinks.entrySet().stream().forEach(entry -> {
        if (entry.getKey().startsWith(ResourceGroupService.FACTORY_LINK) || entry.getKey().startsWith(AuthCredentialsService.FACTORY_LINK)) {
            deletePatchOperations.add(createResourceGroupEndpointLinkPatchOp(entry.getKey(), entry.getValue()));
        } else {
            deletePatchOperations.add(createEndpointLinkPatchOp(entry.getKey(), entry.getValue()));
        }
        logInfo("Changing endpointLink to %s from resource: %s", entry.getValue(), entry.getKey());
    });
    logInfo("Deleting stale documents that have invalid endpointLinks. [documentCount=%s]", task.documentsToBeDeletedLinks.size());
    logInfo("Patching stale documents that have invalid endpointLinks list. [documentCount=%s]", task.endpointLinksByDocumentsToBeDisassociated.size());
    logInfo("Patching stale documents that have invalid endpointLink. [documentCount=%s]", task.endpointLinkToBePatchedByDocumentLinks.size());
    task.documentsDeletedCount += task.documentsToBeDeletedLinks.size();
    task.endpointLinksPatchedCount += task.endpointLinksByDocumentsToBeDisassociated.size();
    task.endpointLinkPatchedCount += task.endpointLinkToBePatchedByDocumentLinks.size();
    OperationJoin.create(deletePatchOperations).setCompletion((o, e) -> {
        if (e != null) {
            task.failureMessage = e.values().iterator().next().getMessage();
            task.subStage = SubStage.FAILED;
            sendSelfFailurePatch(task, task.failureMessage);
            return;
        }
        task.subStage = next;
        sendSelfPatch(task);
    }).sendWith(this, OPERATION_BATCH_SIZE);
}
Also used : Service(com.vmware.xenon.common.Service) AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) Arrays(java.util.Arrays) QueryTask(com.vmware.xenon.services.common.QueryTask) ServiceDocument(com.vmware.xenon.common.ServiceDocument) ResourceGroupState(com.vmware.photon.controller.model.resources.ResourceGroupService.ResourceGroupState) Utils(com.vmware.xenon.common.Utils) TaskFactoryService(com.vmware.xenon.services.common.TaskFactoryService) LoadBalancerState(com.vmware.photon.controller.model.resources.LoadBalancerService.LoadBalancerState) RouterState(com.vmware.photon.controller.model.resources.RouterService.RouterState) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) EndpointService(com.vmware.photon.controller.model.resources.EndpointService) Map(java.util.Map) StorageDescription(com.vmware.photon.controller.model.resources.StorageDescriptionService.StorageDescription) CUSTOM_PROP_ENDPOINT_LINK(com.vmware.photon.controller.model.constants.PhotonModelConstants.CUSTOM_PROP_ENDPOINT_LINK) EndpointState(com.vmware.photon.controller.model.resources.EndpointService.EndpointState) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) NetworkInterfaceState(com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState) EndpointResourceDeletionRequest(com.vmware.photon.controller.model.tasks.ResourceGroomerTaskService.EndpointResourceDeletionRequest) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) Occurance(com.vmware.xenon.services.common.QueryTask.Query.Occurance) ResourceUtils(com.vmware.photon.controller.model.resources.ResourceUtils) ResourceGroupService(com.vmware.photon.controller.model.resources.ResourceGroupService) List(java.util.List) SOURCE_TASK_LINK(com.vmware.photon.controller.model.constants.PhotonModelConstants.SOURCE_TASK_LINK) QueryOption(com.vmware.xenon.services.common.QueryTask.QuerySpecification.QueryOption) FactoryService(com.vmware.xenon.common.FactoryService) ServiceStateMapUpdateRequest(com.vmware.xenon.common.ServiceStateMapUpdateRequest) TaskService(com.vmware.xenon.services.common.TaskService) HashMap(java.util.HashMap) PhotonModelUtils(com.vmware.photon.controller.model.resources.util.PhotonModelUtils) ArrayList(java.util.ArrayList) SecurityGroupState(com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState) ServiceUriPaths(com.vmware.xenon.services.common.ServiceUriPaths) HashSet(java.util.HashSet) ServiceStateCollectionUpdateRequest(com.vmware.xenon.common.ServiceStateCollectionUpdateRequest) AuthCredentialsService(com.vmware.xenon.services.common.AuthCredentialsService) 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) ResourceState(com.vmware.photon.controller.model.resources.ResourceState) Operation(com.vmware.xenon.common.Operation) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) PhotonModelConstants(com.vmware.photon.controller.model.constants.PhotonModelConstants) ImageState(com.vmware.photon.controller.model.resources.ImageService.ImageState) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState) Collections(java.util.Collections) OperationJoin(com.vmware.xenon.common.OperationJoin) ArrayList(java.util.ArrayList) Collection(java.util.Collection) ServiceStateCollectionUpdateRequest(com.vmware.xenon.common.ServiceStateCollectionUpdateRequest) Operation(com.vmware.xenon.common.Operation)

Example 87 with Operation

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

the class ResourceRemovalTaskService method createSubTaskForIpDeallocationCallbacks.

/**
 * Before we proceed with issuing IP deallocation requests to the instance services we must create a sub
 * task that will track the IP dealloaction completions. The instance service will issue a PATCH with
 * TaskStage.FINISHED, for every PATCH we send it
 */
private void createSubTaskForIpDeallocationCallbacks(ResourceRemovalTaskState currentState, QueryTask queryTask, Consumer<String> subTaskLinkConsumer) {
    ServiceTaskCallback<SubStage> callback = ServiceTaskCallback.create(UriUtils.buildPublicUri(getHost(), getSelfLink()));
    if (queryTask.results.nextPageLink != null) {
        callback.onSuccessTo(SubStage.DEALLOCATE_IP_ADDRESSES).addProperty(ResourceRemovalTaskState.FIELD_NAME_NEXT_PAGE_LINK, queryTask.results.nextPageLink);
    } else {
        callback.onSuccessTo(SubStage.DELETE_DOCUMENTS);
    }
    SubTaskService.SubTaskState<SubStage> subTaskInitState = new SubTaskService.SubTaskState<SubStage>();
    // tell the sub task with what to patch us, on completion
    subTaskInitState.serviceTaskCallback = callback;
    subTaskInitState.completionsRemaining = queryTask.results.documentLinks.size();
    subTaskInitState.errorThreshold = currentState.errorThreshold;
    subTaskInitState.tenantLinks = currentState.tenantLinks;
    subTaskInitState.documentExpirationTimeMicros = currentState.documentExpirationTimeMicros;
    Operation startPost = Operation.createPost(this, SubTaskService.FACTORY_LINK).setBody(subTaskInitState).setCompletion((o, e) -> {
        if (e != null) {
            logWarning("Failure creating sub task: %s", Utils.toString(e));
            sendFailureSelfPatch(e);
            return;
        }
        SubTaskService.SubTaskState<?> body = o.getBody(SubTaskService.SubTaskState.class);
        subTaskLinkConsumer.accept(body.documentSelfLink);
    });
    sendRequest(startPost);
}
Also used : Operation(com.vmware.xenon.common.Operation)

Example 88 with Operation

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

the class ResourceRemovalTaskService method sendPatch.

private void sendPatch(String link, Object body) {
    Operation patch = Operation.createPatch(this, link).setBody(body).setCompletion((o, ex) -> {
        if (ex != null) {
            logWarning(() -> String.format("Self patch failed: %s", Utils.toString(ex)));
        }
    });
    sendRequest(patch);
}
Also used : Operation(com.vmware.xenon.common.Operation)

Example 89 with Operation

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

the class TaskUtils method subscribeToNotifications.

public static void subscribeToNotifications(StatefulService service, Consumer<Operation> notificationConsumer, String taskLink) {
    ServiceSubscriber subscribeBody = new ServiceSubscriber();
    subscribeBody.replayState = true;
    subscribeBody.usePublicUri = true;
    Operation subscribeOp = Operation.createPost(service, taskLink).setReferer(service.getUri()).setCompletion((regOp, regEx) -> {
        if (regEx != null) {
            sendFailureSelfPatch(service, regEx);
            return;
        }
    });
    ReliableSubscriptionService notificationTarget = ReliableSubscriptionService.create(subscribeOp, subscribeBody, notificationConsumer);
    service.getHost().startSubscriptionService(subscribeOp, notificationTarget, subscribeBody);
}
Also used : ReliableSubscriptionService(com.vmware.xenon.services.common.ReliableSubscriptionService) ServiceSubscriber(com.vmware.xenon.common.ServiceSubscriptionState.ServiceSubscriber) Operation(com.vmware.xenon.common.Operation)

Example 90 with Operation

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

the class TaskUtils method sendPatch.

/**
 * Issue a patch request to the specified service
 *
 * @param service
 *         Service to issue the patch to
 * @param body
 *         Patch body
 */
public static void sendPatch(StatefulService service, Object body) {
    Operation patch = Operation.createPatch(service.getUri()).setBody(body);
    service.sendRequest(patch);
}
Also used : Operation(com.vmware.xenon.common.Operation)

Aggregations

Operation (com.vmware.xenon.common.Operation)391 URI (java.net.URI)142 ArrayList (java.util.ArrayList)132 QueryTask (com.vmware.xenon.services.common.QueryTask)118 List (java.util.List)118 Utils (com.vmware.xenon.common.Utils)111 StatelessService (com.vmware.xenon.common.StatelessService)108 UriUtils (com.vmware.xenon.common.UriUtils)106 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)98 Map (java.util.Map)92 HashMap (java.util.HashMap)90 OperationJoin (com.vmware.xenon.common.OperationJoin)86 Query (com.vmware.xenon.services.common.QueryTask.Query)86 QueryUtils (com.vmware.photon.controller.model.query.QueryUtils)82 Collectors (java.util.stream.Collectors)79 HashSet (java.util.HashSet)78 AuthCredentialsServiceState (com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState)73 AdapterUtils (com.vmware.photon.controller.model.adapters.util.AdapterUtils)70 DeferredResult (com.vmware.xenon.common.DeferredResult)69 Consumer (java.util.function.Consumer)69