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