use of com.vmware.xenon.common.OperationSequence in project photon-model by vmware.
the class AWSEnumerationAdapterService method kickOffEnumerationWorkFlows.
/**
* Kicks off the enumeration flows for creation and deletion.
*/
public void kickOffEnumerationWorkFlows(EnumerationContext context, AWSEnumerationStages next) {
List<List<Operation>> enumOperations = new ArrayList<>();
if (!AWSUtils.isAwsClientMock()) {
ComputeEnumerateAdapterRequest awsS3EnumerationRequest = new ComputeEnumerateAdapterRequest(context.request, context.endpointAuth, context.parent, Regions.DEFAULT_REGION.getName());
Operation patchAWSS3StorageAdapterService = Operation.createPatch(this, AWSS3StorageEnumerationAdapterService.SELF_LINK).setBody(awsS3EnumerationRequest).setReferer(getHost().getUri());
enumOperations.add(Collections.singletonList(patchAWSS3StorageAdapterService));
}
for (String regionId : context.regions) {
List<Operation> enumOperationsForRegion = new ArrayList<>();
ComputeEnumerateAdapterRequest awsEnumerationRequest = new ComputeEnumerateAdapterRequest(context.request, context.endpointAuth, context.parent, regionId);
Operation patchAWSCreationAdapterService = Operation.createPatch(this, AWSEnumerationAndCreationAdapterService.SELF_LINK).setBody(awsEnumerationRequest).setReferer(this.getHost().getUri());
Operation patchAWSDeletionAdapterService = Operation.createPatch(this, AWSEnumerationAndDeletionAdapterService.SELF_LINK).setBody(awsEnumerationRequest).setReferer(getHost().getUri());
Operation patchAWSEBSStorageAdapterService = Operation.createPatch(this, AWSEBSStorageEnumerationAdapterService.SELF_LINK).setBody(awsEnumerationRequest).setReferer(getHost().getUri());
enumOperationsForRegion.add(patchAWSCreationAdapterService);
enumOperationsForRegion.add(patchAWSDeletionAdapterService);
enumOperationsForRegion.add(patchAWSEBSStorageAdapterService);
enumOperations.add(enumOperationsForRegion);
}
if (enumOperations.size() == 0) {
logFine(() -> "No enumeration tasks to run");
context.stage = next;
handleEnumerationRequest(context);
return;
}
OperationJoin.JoinedCompletionHandler joinCompletion = (ox, exc) -> {
if (exc != null) {
logSevere(() -> String.format("Error starting the enumeration workflows for AWS: %s", Utils.toString(exc)));
context.taskManager.patchTaskToFailure(exc.values().iterator().next());
return;
}
logInfo(() -> "Completed creation and deletion enumeration for compute and storage" + " states");
context.stage = next;
handleEnumerationRequest(context);
};
OperationSequence enumOp = OperationSequence.create(OperationJoin.create(enumOperations.get(0)));
for (int i = 1; i < enumOperations.size(); i++) {
enumOp = enumOp.next(OperationJoin.create(enumOperations.get(i)));
}
enumOp.setCompletion(joinCompletion);
enumOp.sendWith(getHost());
logFine(() -> "Started creation and deletion enumeration for AWS computes and storage");
}
use of com.vmware.xenon.common.OperationSequence in project photon-model by vmware.
the class AzureSubscriptionEndpointCreationService method createSubscriptionEndpoint.
private void createSubscriptionEndpoint(EndpointState azureEaEndpoint, AzureSubscriptionEndpointCreationRequest request, Operation parentOp) {
Operation authOp = Operation.createPost(UriUtils.extendUri(getInventoryServiceUri(), AuthCredentialsService.FACTORY_LINK));
Operation cdOp = Operation.createPost(UriUtils.extendUri(getInventoryServiceUri(), ComputeDescriptionService.FACTORY_LINK));
Operation csOp = Operation.createPost(UriUtils.extendUri(getInventoryServiceUri(), ComputeService.FACTORY_LINK));
Operation endPointOp = Operation.createPost(UriUtils.extendUri(getInventoryServiceUri(), EndpointService.FACTORY_LINK));
AuthCredentialsServiceState authCredentialsState = createAuthCredentialsState(azureEaEndpoint, request);
EndpointState endpointState = createEndpointState(azureEaEndpoint, request);
ComputeDescription computeDescState = AzureUtils.constructAzureSubscriptionComputeDescription(endpointState.documentSelfLink, azureEaEndpoint.tenantLinks, request.subscriptionId, null, null, endpointState.computeLink);
authOp.setBody(authCredentialsState);
OperationSequence sequence = OperationSequence.create(authOp).setCompletion((ops, exs) -> {
if (exs != null) {
handleFailure(exs, parentOp);
return;
}
Operation o = ops.get(authOp.getId());
AuthCredentialsServiceState authState = o.getBody(AuthCredentialsServiceState.class);
computeDescState.authCredentialsLink = authState.documentSelfLink;
endpointState.authCredentialsLink = authState.documentSelfLink;
cdOp.setBody(computeDescState);
}).next(cdOp).setCompletion((ops, exs) -> {
if (exs != null) {
handleFailure(exs, parentOp);
return;
}
Operation o = ops.get(cdOp.getId());
ComputeDescription cd = o.getBody(ComputeDescription.class);
ComputeState cs = AzureUtils.constructAzureSubscriptionComputeState(endpointState.documentSelfLink, cd.documentSelfLink, azureEaEndpoint.tenantLinks, request.subscriptionId, azureEaEndpoint.resourcePoolLink, getCustomPropertiesMap(endpointState, request), null, endpointState.computeLink);
csOp.setBody(cs);
endpointState.computeDescriptionLink = cd.documentSelfLink;
}).next(csOp).setCompletion((ops, exs) -> {
if (exs != null) {
handleFailure(exs, parentOp);
return;
}
Operation o = ops.get(csOp.getId());
ComputeState cs = o.getBody(ComputeState.class);
endpointState.computeLink = cs.documentSelfLink;
endPointOp.setBody(endpointState);
}).next(endPointOp).setCompletion((ops, exs) -> {
if (exs != null) {
handleFailure(exs, parentOp);
return;
}
Operation o = ops.get(endPointOp.getId());
EndpointState es = o.getBody(EndpointState.class);
parentOp.setBody(es);
parentOp.complete();
});
sequence.sendWith(this);
}
use of com.vmware.xenon.common.OperationSequence in project photon-model by vmware.
the class EndpointAllocationTaskService method createEndpoint.
private void createEndpoint(EndpointAllocationTaskState currentState) {
List<String> createdDocumentLinks = new ArrayList<>();
EndpointState es = currentState.endpointState;
Map<String, String> endpointProperties = currentState.endpointState.endpointProperties;
es.endpointProperties = null;
if (es.documentSelfLink == null) {
es.documentSelfLink = UriUtils.buildUriPath(EndpointService.FACTORY_LINK, this.getHost().nextUUID());
}
// merge endpoint and task tenant links
if (es.tenantLinks == null || es.tenantLinks.isEmpty()) {
es.tenantLinks = currentState.tenantLinks;
} else if (currentState.tenantLinks != null) {
currentState.tenantLinks.forEach(tl -> {
if (!es.tenantLinks.contains(tl)) {
es.tenantLinks.add(tl);
}
});
}
Operation endpointOp = Operation.createPost(this, EndpointService.FACTORY_LINK);
ComputeDescription computeDescription = configureDescription(currentState, es);
ComputeState computeState = configureCompute(currentState, es, endpointProperties);
Operation cdOp = createComputeDescriptionOp(currentState, computeDescription.documentSelfLink);
Operation compOp = createComputeStateOp(currentState, computeState.documentSelfLink);
// pool link
if (currentState.enumerationRequest != null && currentState.enumerationRequest.resourcePoolLink != null) {
es.resourcePoolLink = currentState.enumerationRequest.resourcePoolLink;
computeState.resourcePoolLink = es.resourcePoolLink;
}
OperationSequence sequence;
if (es.authCredentialsLink == null) {
AuthCredentialsServiceState auth = configureAuth(es);
Operation authOp = Operation.createPost(createInventoryUri(this.getHost(), AuthCredentialsService.FACTORY_LINK)).setBody(auth);
sequence = OperationSequence.create(authOp).setCompletion((ops, exs) -> {
if (exs != null) {
long firstKey = exs.keySet().iterator().next();
exs.values().forEach(ex -> logWarning(() -> String.format("Error in " + "sequence to create auth credentials: %s", ex.getMessage())));
sendFailurePatch(this, currentState, exs.get(firstKey));
return;
}
Operation o = ops.get(authOp.getId());
AuthCredentialsServiceState authState = o.getBody(AuthCredentialsServiceState.class);
computeDescription.authCredentialsLink = authState.documentSelfLink;
es.authCredentialsLink = authState.documentSelfLink;
cdOp.setBody(computeDescription);
}).next(cdOp);
} else {
cdOp.setBody(computeDescription);
sequence = OperationSequence.create(cdOp);
}
sequence = sequence.setCompletion((ops, exs) -> {
if (exs != null) {
long firstKey = exs.keySet().iterator().next();
exs.values().forEach(ex -> logWarning(() -> String.format("Error in " + "sequence to create compute description: %s", ex.getMessage())));
sendFailurePatch(this, currentState, exs.get(firstKey));
return;
}
Operation o = ops.get(cdOp.getId());
ComputeDescription desc = o.getBody(ComputeDescription.class);
if (!currentState.accountAlreadyExists) {
createdDocumentLinks.add(desc.documentSelfLink);
}
computeState.descriptionLink = desc.documentSelfLink;
es.computeDescriptionLink = desc.documentSelfLink;
});
// Don't create resource pool, if a resource pool link was passed.
if (es.resourcePoolLink == null) {
Operation poolOp = createResourcePoolOp(es);
sequence = sequence.next(poolOp).setCompletion((ops, exs) -> {
if (exs != null) {
long firstKey = exs.keySet().iterator().next();
exs.values().forEach(ex -> logWarning(() -> String.format("Error creating resource" + " pool: %s", ex.getMessage())));
sendFailurePatch(this, currentState, exs.get(firstKey));
return;
}
Operation o = ops.get(poolOp.getId());
ResourcePoolState poolState = o.getBody(ResourcePoolState.class);
createdDocumentLinks.add(poolState.documentSelfLink);
es.resourcePoolLink = poolState.documentSelfLink;
computeState.resourcePoolLink = es.resourcePoolLink;
compOp.setBody(computeState);
});
} else {
Operation getPoolOp = Operation.createGet(this, es.resourcePoolLink);
sequence = sequence.next(getPoolOp).setCompletion((ops, exs) -> {
if (exs != null) {
long firstKey = exs.keySet().iterator().next();
exs.values().forEach(ex -> logWarning(() -> String.format("Error retrieving resource" + " pool: %s", ex.getMessage())));
sendFailurePatch(this, currentState, exs.get(firstKey));
return;
}
Operation o = ops.get(getPoolOp.getId());
ResourcePoolState poolState = o.getBody(ResourcePoolState.class);
if (poolState.customProperties != null) {
String endpointLink = poolState.customProperties.get(ENDPOINT_LINK_PROP_NAME);
if (endpointLink != null && endpointLink.equals(es.documentSelfLink)) {
sendFailurePatch(this, currentState, new IllegalStateException("Passed resource pool is associated with a different endpoint."));
return;
}
}
es.resourcePoolLink = poolState.documentSelfLink;
computeState.resourcePoolLink = es.resourcePoolLink;
compOp.setBody(computeState);
});
}
sequence.next(compOp).setCompletion((ops, exs) -> {
if (exs != null) {
long firstKey = exs.keySet().iterator().next();
exs.values().forEach(ex -> logWarning(() -> String.format("Error in " + "sequence to create compute state: %s", ex.getMessage())));
sendFailurePatch(this, currentState, exs.get(firstKey));
return;
}
Operation csOp = ops.get(compOp.getId());
ComputeState c = csOp.getBody(ComputeState.class);
if (!currentState.accountAlreadyExists) {
createdDocumentLinks.add(c.documentSelfLink);
}
es.computeLink = c.documentSelfLink;
endpointOp.setBody(es);
}).next(endpointOp).setCompletion((ops, exs) -> {
if (exs != null) {
long firstKey = exs.keySet().iterator().next();
exs.values().forEach(ex -> logWarning(() -> String.format("Error in " + "sequence to create endpoint state: %s", ex.getMessage())));
sendFailurePatch(this, currentState, exs.get(firstKey));
return;
}
Operation esOp = ops.get(endpointOp.getId());
EndpointState endpoint = esOp.getBody(EndpointState.class);
createdDocumentLinks.add(endpoint.documentSelfLink);
// propagate the endpoint properties to the next stage
endpoint.endpointProperties = endpointProperties;
EndpointAllocationTaskState state = createUpdateSubStageTask(SubStage.INVOKE_ADAPTER);
state.endpointState = endpoint;
state.createdDocumentLinks = createdDocumentLinks;
sendSelfPatch(state);
}).sendWith(this);
}
use of com.vmware.xenon.common.OperationSequence in project photon-model by vmware.
the class SingleResourceStatsCollectionTaskService method batchPersistStats.
private void batchPersistStats(List<Operation> operations, int batchIndex, boolean isFinalBatch) {
OperationSequence opSequence = null;
Integer nextBatchIndex = null;
for (int i = batchIndex; i < operations.size(); i++) {
final Operation operation = operations.get(i);
if (opSequence == null) {
opSequence = OperationSequence.create(operation);
continue;
}
opSequence = opSequence.next(operation);
// Batch size of 100
int batchSize = 100;
int opSequenceSize = i + 1;
if ((opSequenceSize % batchSize) == 0) {
nextBatchIndex = opSequenceSize;
break;
}
}
Integer finalNextBatchIndex = nextBatchIndex;
opSequence.setCompletion((ops, exc) -> {
if (exc != null) {
logWarning(() -> String.format("Failed stats collection: %s", exc.values().iterator().next().getMessage()));
TaskUtils.sendFailurePatch(this, new SingleResourceStatsCollectionTaskState(), exc.values());
return;
}
if (finalNextBatchIndex == null || finalNextBatchIndex == operations.size()) {
if (isFinalBatch) {
SingleResourceStatsCollectionTaskState nextStatePatch = new SingleResourceStatsCollectionTaskState();
nextStatePatch.taskInfo = TaskUtils.createTaskState(TaskStage.FINISHED);
TaskUtils.sendPatch(this, nextStatePatch);
}
return;
}
batchPersistStats(operations, finalNextBatchIndex, isFinalBatch);
});
opSequence.sendWith(this);
}
use of com.vmware.xenon.common.OperationSequence 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);
}
Aggregations