Search in sources :

Example 1 with OperationSequence

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");
}
Also used : AdapterUtils(com.vmware.photon.controller.model.adapters.util.AdapterUtils) Arrays(java.util.Arrays) ComputeEnumerateResourceRequest(com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest) StatelessService(com.vmware.xenon.common.StatelessService) Operation(com.vmware.xenon.common.Operation) BaseAdapterStage(com.vmware.photon.controller.model.adapters.util.BaseAdapterContext.BaseAdapterStage) BaseAdapterContext(com.vmware.photon.controller.model.adapters.util.BaseAdapterContext) AWSUtils(com.vmware.photon.controller.model.adapters.awsadapter.AWSUtils) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Regions(com.amazonaws.regions.Regions) List(java.util.List) Utils(com.vmware.xenon.common.Utils) GET_REGIONS(com.vmware.photon.controller.model.adapters.awsadapter.enumeration.AWSEnumerationAdapterService.AWSEnumerationStages.GET_REGIONS) ComputeEnumerateAdapterRequest(com.vmware.photon.controller.model.adapters.util.ComputeEnumerateAdapterRequest) KICKOFF_ENUMERATION(com.vmware.photon.controller.model.adapters.awsadapter.enumeration.AWSEnumerationAdapterService.AWSEnumerationStages.KICKOFF_ENUMERATION) OperationSequence(com.vmware.xenon.common.OperationSequence) Collections(java.util.Collections) AWSUriPaths(com.vmware.photon.controller.model.adapters.awsadapter.AWSUriPaths) OperationJoin(com.vmware.xenon.common.OperationJoin) PATCH_COMPLETION(com.vmware.photon.controller.model.adapters.awsadapter.enumeration.AWSEnumerationAdapterService.AWSEnumerationStages.PATCH_COMPLETION) OperationSequence(com.vmware.xenon.common.OperationSequence) ComputeEnumerateAdapterRequest(com.vmware.photon.controller.model.adapters.util.ComputeEnumerateAdapterRequest) OperationJoin(com.vmware.xenon.common.OperationJoin) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Operation(com.vmware.xenon.common.Operation)

Example 2 with OperationSequence

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);
}
Also used : EndpointState(com.vmware.photon.controller.model.resources.EndpointService.EndpointState) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) OperationSequence(com.vmware.xenon.common.OperationSequence) Operation(com.vmware.xenon.common.Operation)

Example 3 with OperationSequence

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);
}
Also used : AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) CertificateInfoServiceErrorResponse(com.vmware.photon.controller.model.support.CertificateInfoServiceErrorResponse) ServiceTypeCluster(com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster) ServiceDocument(com.vmware.xenon.common.ServiceDocument) Utils(com.vmware.xenon.common.Utils) EndpointService(com.vmware.photon.controller.model.resources.EndpointService) Map(java.util.Map) CUSTOM_PROP_ENDPOINT_LINK(com.vmware.photon.controller.model.constants.PhotonModelConstants.CUSTOM_PROP_ENDPOINT_LINK) ResourcePoolService(com.vmware.photon.controller.model.resources.ResourcePoolService) URI(java.net.URI) SINGLE_ASSIGNMENT(com.vmware.xenon.common.ServiceDocumentDescription.PropertyUsageOption.SINGLE_ASSIGNMENT) EnumSet(java.util.EnumSet) EndpointState(com.vmware.photon.controller.model.resources.EndpointService.EndpointState) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) ENDPOINT_LINK_PROP_NAME(com.vmware.photon.controller.model.ComputeProperties.ENDPOINT_LINK_PROP_NAME) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) ServiceHost(com.vmware.xenon.common.ServiceHost) PhotonModelAdaptersConfigAccessService(com.vmware.photon.controller.model.adapters.registry.PhotonModelAdaptersConfigAccessService) OPTIONAL(com.vmware.xenon.common.ServiceDocumentDescription.PropertyUsageOption.OPTIONAL) List(java.util.List) RequestType(com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.RequestType) TaskUtils.sendFailurePatch(com.vmware.photon.controller.model.tasks.TaskUtils.sendFailurePatch) CompletionHandler(com.vmware.xenon.common.Operation.CompletionHandler) SOURCE_TASK_LINK(com.vmware.photon.controller.model.constants.PhotonModelConstants.SOURCE_TASK_LINK) DeferredResult(com.vmware.xenon.common.DeferredResult) UriUtils(com.vmware.xenon.common.UriUtils) ComputeService(com.vmware.photon.controller.model.resources.ComputeService) TaskState(com.vmware.xenon.common.TaskState) TaskService(com.vmware.xenon.services.common.TaskService) AdapterTypePath(com.vmware.photon.controller.model.UriPaths.AdapterTypePath) STORE_ONLY(com.vmware.xenon.common.ServiceDocumentDescription.PropertyIndexingOption.STORE_ONLY) ResourcePoolState(com.vmware.photon.controller.model.resources.ResourcePoolService.ResourcePoolState) HashMap(java.util.HashMap) ComputeDescriptionService(com.vmware.photon.controller.model.resources.ComputeDescriptionService) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) EndpointConfigRequest(com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest) AuthCredentialsService(com.vmware.xenon.services.common.AuthCredentialsService) UriPaths(com.vmware.photon.controller.model.UriPaths) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) OperationSequence(com.vmware.xenon.common.OperationSequence) SERVICE_USE(com.vmware.xenon.common.ServiceDocumentDescription.PropertyUsageOption.SERVICE_USE) ResourceEnumerationTaskState(com.vmware.photon.controller.model.tasks.ResourceEnumerationTaskService.ResourceEnumerationTaskState) Operation(com.vmware.xenon.common.Operation) CertificateInfo(com.vmware.photon.controller.model.support.CertificateInfo) ScheduledTaskState(com.vmware.photon.controller.model.tasks.ScheduledTaskService.ScheduledTaskState) TaskStage(com.vmware.xenon.common.TaskState.TaskStage) TimeUnit(java.util.concurrent.TimeUnit) PhotonModelAdapterConfig(com.vmware.photon.controller.model.adapters.registry.PhotonModelAdaptersRegistryService.PhotonModelAdapterConfig) ClusterUtil(com.vmware.photon.controller.model.util.ClusterUtil) LocalizableValidationException(com.vmware.xenon.common.LocalizableValidationException) PropertyIndexingOption(com.vmware.xenon.common.ServiceDocumentDescription.PropertyIndexingOption) OperationJoin(com.vmware.xenon.common.OperationJoin) PhotonModelUriUtils.createInventoryUri(com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) ResourcePoolState(com.vmware.photon.controller.model.resources.ResourcePoolService.ResourcePoolState) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) OperationSequence(com.vmware.xenon.common.OperationSequence) ArrayList(java.util.ArrayList) Operation(com.vmware.xenon.common.Operation) EndpointState(com.vmware.photon.controller.model.resources.EndpointService.EndpointState) AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState)

Example 4 with OperationSequence

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);
}
Also used : OperationSequence(com.vmware.xenon.common.OperationSequence) SingleResourceStatsCollectionTaskState(com.vmware.photon.controller.model.tasks.monitoring.SingleResourceStatsCollectionTaskService.SingleResourceStatsCollectionTaskState) Operation(com.vmware.xenon.common.Operation)

Example 5 with OperationSequence

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);
}
Also used : ResourceMetrics(com.vmware.photon.controller.model.monitoring.ResourceMetricsService.ResourceMetrics) QueryTask(com.vmware.xenon.services.common.QueryTask) Query(com.vmware.xenon.services.common.QueryTask.Query) OperationSequence(com.vmware.xenon.common.OperationSequence) ArrayList(java.util.ArrayList) Operation(com.vmware.xenon.common.Operation)

Aggregations

Operation (com.vmware.xenon.common.Operation)8 OperationSequence (com.vmware.xenon.common.OperationSequence)8 ArrayList (java.util.ArrayList)5 ComputeDescription (com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription)3 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)3 EndpointState (com.vmware.photon.controller.model.resources.EndpointService.EndpointState)3 OperationJoin (com.vmware.xenon.common.OperationJoin)3 ENDPOINT_LINK_PROP_NAME (com.vmware.photon.controller.model.ComputeProperties.ENDPOINT_LINK_PROP_NAME)2 UriPaths (com.vmware.photon.controller.model.UriPaths)2 AdapterTypePath (com.vmware.photon.controller.model.UriPaths.AdapterTypePath)2 EndpointConfigRequest (com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest)2 RequestType (com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.RequestType)2 PhotonModelAdaptersConfigAccessService (com.vmware.photon.controller.model.adapters.registry.PhotonModelAdaptersConfigAccessService)2 PhotonModelAdapterConfig (com.vmware.photon.controller.model.adapters.registry.PhotonModelAdaptersRegistryService.PhotonModelAdapterConfig)2 CUSTOM_PROP_ENDPOINT_LINK (com.vmware.photon.controller.model.constants.PhotonModelConstants.CUSTOM_PROP_ENDPOINT_LINK)2 SOURCE_TASK_LINK (com.vmware.photon.controller.model.constants.PhotonModelConstants.SOURCE_TASK_LINK)2 ComputeDescriptionService (com.vmware.photon.controller.model.resources.ComputeDescriptionService)2 ComputeService (com.vmware.photon.controller.model.resources.ComputeService)2 EndpointService (com.vmware.photon.controller.model.resources.EndpointService)2 ResourcePoolService (com.vmware.photon.controller.model.resources.ResourcePoolService)2