Search in sources :

Example 16 with SingleResourceStatsCollectionTaskState

use of com.vmware.photon.controller.model.tasks.monitoring.SingleResourceStatsCollectionTaskService.SingleResourceStatsCollectionTaskState in project photon-model by vmware.

the class SingleResourceStatsCollectionTaskService method updateAndPersistStats.

private void updateAndPersistStats(SingleResourceStatsCollectionTaskState currentState) {
    if (currentState.statsAdapterReference == null) {
        throw new IllegalStateException("stats adapter reference should not be null");
    }
    if (currentState.statsList.size() == 0) {
        // If there are no stats reported and if it's a final batch, just finish the task.
        if (currentState.isFinalBatch) {
            SingleResourceStatsCollectionTaskState nextStatePatch = new SingleResourceStatsCollectionTaskState();
            nextStatePatch.taskInfo = TaskUtils.createTaskState(TaskStage.FINISHED);
            TaskUtils.sendPatch(this, nextStatePatch);
        }
        return;
    }
    long expirationTime = Utils.getNowMicrosUtc() + TimeUnit.DAYS.toMicros(EXPIRATION_INTERVAL);
    List<Operation> operations = new ArrayList<>();
    List<ResourceMetrics> metricsList = new ArrayList<>();
    List<InMemoryResourceMetric> inMemoryMetricsList = new ArrayList<>();
    // Push the last collection metric to the in memory stats available at the
    // compute-link/stats URI.
    ServiceStats.ServiceStat minuteStats = new ServiceStats.ServiceStat();
    String statsLink = getAdapterLinkFromURI(currentState.statsAdapterReference);
    minuteStats.name = getLastCollectionMetricKeyForAdapterLink(statsLink, true);
    minuteStats.latestValue = Utils.getNowMicrosUtc();
    minuteStats.sourceTimeMicrosUtc = Utils.getNowMicrosUtc();
    minuteStats.unit = PhotonModelConstants.UNIT_MICROSECONDS;
    URI inMemoryStatsUri = UriUtils.buildStatsUri(UriUtils.extendUri(UriUtils.buildUri(ClusterUtil.getClusterUri(getHost(), ServiceTypeCluster.INVENTORY_SERVICE)), currentState.computeLink));
    operations.add(Operation.createPost(inMemoryStatsUri).setBody(minuteStats));
    populateResourceMetrics(metricsList, getLastCollectionMetricKeyForAdapterLink(statsLink, false), minuteStats, currentState.computeLink, expirationTime, null);
    for (ComputeStats stats : currentState.statsList) {
        // TODO: https://jira-hzn.eng.vmware.com/browse/VSYM-330
        InMemoryResourceMetric hourlyMemoryState = new InMemoryResourceMetric();
        if (currentState.publishInMemory) {
            String computeId = UriUtils.getLastPathSegment(stats.computeLink);
            hourlyMemoryState.timeSeriesStats = new HashMap<>();
            hourlyMemoryState.documentSelfLink = computeId + StatsConstants.HOUR_SUFFIX;
            inMemoryMetricsList.add(hourlyMemoryState);
        }
        for (Entry<String, List<ServiceStat>> entries : stats.statValues.entrySet()) {
            // sort stats by source time
            entries.getValue().sort(Comparator.comparing(o -> o.sourceTimeMicrosUtc));
            // Persist every data point
            for (ServiceStat serviceStat : entries.getValue()) {
                String computeLink = stats.computeLink;
                if (computeLink == null) {
                    computeLink = currentState.computeLink;
                }
                if (currentState.publishInMemory) {
                    // update in-memory stats
                    updateInMemoryStats(hourlyMemoryState, entries.getKey(), serviceStat, StatsConstants.BUCKET_SIZE_HOURS_IN_MILLIS);
                }
                populateResourceMetrics(metricsList, entries.getKey(), serviceStat, computeLink, expirationTime, stats.getCustomProperties());
            }
        }
    }
    for (ResourceMetrics metrics : metricsList) {
        operations.add(Operation.createPost(UriUtils.buildUri(ClusterUtil.getClusterUri(getHost(), ServiceTypeCluster.METRIC_SERVICE), ResourceMetricsService.FACTORY_LINK)).setBodyNoCloning(metrics));
    }
    if (currentState.publishInMemory) {
        for (InMemoryResourceMetric metric : inMemoryMetricsList) {
            operations.add(Operation.createPost(getHost(), InMemoryResourceMetricService.FACTORY_LINK).setBodyNoCloning(metric));
        }
    }
    // Save each data point sequentially to create time based monotonically increasing sequence.
    batchPersistStats(operations, 0, currentState.isFinalBatch);
}
Also used : Service(com.vmware.xenon.common.Service) ServiceTypeCluster(com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster) QueryTask(com.vmware.xenon.services.common.QueryTask) ServiceDocument(com.vmware.xenon.common.ServiceDocument) AggregationType(com.vmware.xenon.common.ServiceStats.TimeSeriesStats.AggregationType) MatchType(com.vmware.xenon.services.common.QueryTask.QueryTerm.MatchType) SingleResourceStatsCollectionTaskState(com.vmware.photon.controller.model.tasks.monitoring.SingleResourceStatsCollectionTaskService.SingleResourceStatsCollectionTaskState) Utils(com.vmware.xenon.common.Utils) TaskFactoryService(com.vmware.xenon.services.common.TaskFactoryService) Map(java.util.Map) URI(java.net.URI) EnumSet(java.util.EnumSet) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) ComputeStatsRequest(com.vmware.photon.controller.model.adapterapi.ComputeStatsRequest) ServiceStats(com.vmware.xenon.common.ServiceStats) List(java.util.List) TimeSeriesStats(com.vmware.xenon.common.ServiceStats.TimeSeriesStats) UriUtils(com.vmware.xenon.common.UriUtils) Entry(java.util.Map.Entry) QueryOption(com.vmware.xenon.services.common.QueryTask.QuerySpecification.QueryOption) TaskState(com.vmware.xenon.common.TaskState) TaskOption(com.vmware.photon.controller.model.tasks.TaskOption) FactoryService(com.vmware.xenon.common.FactoryService) NumericRange(com.vmware.xenon.services.common.QueryTask.NumericRange) TaskService(com.vmware.xenon.services.common.TaskService) ResourceMetrics(com.vmware.photon.controller.model.monitoring.ResourceMetricsService.ResourceMetrics) TaskUtils(com.vmware.photon.controller.model.tasks.TaskUtils) InMemoryResourceMetric(com.vmware.photon.controller.model.monitoring.InMemoryResourceMetricService.InMemoryResourceMetric) HashMap(java.util.HashMap) PhotonModelUtils(com.vmware.photon.controller.model.resources.util.PhotonModelUtils) ResourceMetricsService(com.vmware.photon.controller.model.monitoring.ResourceMetricsService) ArrayList(java.util.ArrayList) ServiceUriPaths(com.vmware.xenon.services.common.ServiceUriPaths) Query(com.vmware.xenon.services.common.QueryTask.Query) UriPaths(com.vmware.photon.controller.model.UriPaths) PropertyUsageOption(com.vmware.xenon.common.ServiceDocumentDescription.PropertyUsageOption) ComputeStats(com.vmware.photon.controller.model.adapterapi.ComputeStatsResponse.ComputeStats) OperationSequence(com.vmware.xenon.common.OperationSequence) Operation(com.vmware.xenon.common.Operation) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) TypeName(com.vmware.xenon.common.ServiceDocumentDescription.TypeName) ServiceStat(com.vmware.xenon.common.ServiceStats.ServiceStat) TaskStage(com.vmware.xenon.common.TaskState.TaskStage) TimeUnit(java.util.concurrent.TimeUnit) InMemoryResourceMetricService(com.vmware.photon.controller.model.monitoring.InMemoryResourceMetricService) ClusterUtil(com.vmware.photon.controller.model.util.ClusterUtil) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) PhotonModelConstants(com.vmware.photon.controller.model.constants.PhotonModelConstants) Comparator(java.util.Comparator) QuerySpecification(com.vmware.xenon.services.common.QueryTask.QuerySpecification) InMemoryResourceMetric(com.vmware.photon.controller.model.monitoring.InMemoryResourceMetricService.InMemoryResourceMetric) ServiceStat(com.vmware.xenon.common.ServiceStats.ServiceStat) SingleResourceStatsCollectionTaskState(com.vmware.photon.controller.model.tasks.monitoring.SingleResourceStatsCollectionTaskService.SingleResourceStatsCollectionTaskState) ArrayList(java.util.ArrayList) Operation(com.vmware.xenon.common.Operation) URI(java.net.URI) ResourceMetrics(com.vmware.photon.controller.model.monitoring.ResourceMetricsService.ResourceMetrics) ServiceStat(com.vmware.xenon.common.ServiceStats.ServiceStat) ServiceStats(com.vmware.xenon.common.ServiceStats) ComputeStats(com.vmware.photon.controller.model.adapterapi.ComputeStatsResponse.ComputeStats) List(java.util.List) ArrayList(java.util.ArrayList)

Example 17 with SingleResourceStatsCollectionTaskState

use of com.vmware.photon.controller.model.tasks.monitoring.SingleResourceStatsCollectionTaskService.SingleResourceStatsCollectionTaskState in project photon-model by vmware.

the class StatsCollectionTaskService method createSingleResourceComputeTask.

private void createSingleResourceComputeTask(String computeLink, String subtaskLink, StatsCollectionTaskState currentState) {
    SingleResourceStatsCollectionTaskState initState = new SingleResourceStatsCollectionTaskState();
    initState.parentTaskReference = UriUtils.buildPublicUri(getHost(), subtaskLink);
    initState.computeLink = computeLink;
    initState.statsAdapterReference = currentState.statsAdapterReference;
    SubTaskState<StatsCollectionStage> patchState = new SubTaskState<>();
    patchState.taskInfo = TaskUtils.createTaskState(TaskStage.FINISHED);
    initState.parentPatchBody = patchState;
    if (currentState.options != null && currentState.options.contains(TaskOption.IS_MOCK)) {
        initState.options = EnumSet.of(TaskOption.IS_MOCK);
    }
    sendRequest(Operation.createPost(this, SingleResourceStatsCollectionTaskService.FACTORY_LINK).setBody(initState).setCompletion((factoryPostOp, factoryPostEx) -> {
        if (factoryPostEx != null) {
            TaskUtils.sendFailurePatch(this, new StatsCollectionTaskState(), factoryPostEx);
        }
    }));
}
Also used : Service(com.vmware.xenon.common.Service) ServiceTypeCluster(com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster) QueryTask(com.vmware.xenon.services.common.QueryTask) ResourcePoolState(com.vmware.photon.controller.model.resources.ResourcePoolService.ResourcePoolState) PhotonModelUtils(com.vmware.photon.controller.model.resources.util.PhotonModelUtils) SingleResourceStatsCollectionTaskState(com.vmware.photon.controller.model.tasks.monitoring.SingleResourceStatsCollectionTaskService.SingleResourceStatsCollectionTaskState) ServiceUriPaths(com.vmware.xenon.services.common.ServiceUriPaths) SubTaskService(com.vmware.photon.controller.model.tasks.SubTaskService) Utils(com.vmware.xenon.common.Utils) Query(com.vmware.xenon.services.common.QueryTask.Query) UriPaths(com.vmware.photon.controller.model.UriPaths) TaskFactoryService(com.vmware.xenon.services.common.TaskFactoryService) ServiceNotFoundException(com.vmware.xenon.common.ServiceHost.ServiceNotFoundException) PropertyUsageOption(com.vmware.xenon.common.ServiceDocumentDescription.PropertyUsageOption) URI(java.net.URI) EnumSet(java.util.EnumSet) SubTaskState(com.vmware.photon.controller.model.tasks.SubTaskService.SubTaskState) ServiceTaskCallback(com.vmware.photon.controller.model.tasks.ServiceTaskCallback) Operation(com.vmware.xenon.common.Operation) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) TaskStage(com.vmware.xenon.common.TaskState.TaskStage) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) ClusterUtil(com.vmware.photon.controller.model.util.ClusterUtil) ServiceTaskCallbackResponse(com.vmware.photon.controller.model.tasks.ServiceTaskCallback.ServiceTaskCallbackResponse) UriUtils(com.vmware.xenon.common.UriUtils) TaskState(com.vmware.xenon.common.TaskState) TaskOption(com.vmware.photon.controller.model.tasks.TaskOption) FactoryService(com.vmware.xenon.common.FactoryService) TaskService(com.vmware.xenon.services.common.TaskService) PhotonModelUriUtils.createInventoryUri(com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri) TaskUtils(com.vmware.photon.controller.model.tasks.TaskUtils) SubTaskState(com.vmware.photon.controller.model.tasks.SubTaskService.SubTaskState) SingleResourceStatsCollectionTaskState(com.vmware.photon.controller.model.tasks.monitoring.SingleResourceStatsCollectionTaskService.SingleResourceStatsCollectionTaskState) SingleResourceStatsCollectionTaskState(com.vmware.photon.controller.model.tasks.monitoring.SingleResourceStatsCollectionTaskService.SingleResourceStatsCollectionTaskState)

Aggregations

SingleResourceStatsCollectionTaskState (com.vmware.photon.controller.model.tasks.monitoring.SingleResourceStatsCollectionTaskService.SingleResourceStatsCollectionTaskState)17 ServiceStat (com.vmware.xenon.common.ServiceStats.ServiceStat)11 Operation (com.vmware.xenon.common.Operation)10 ArrayList (java.util.ArrayList)10 List (java.util.List)9 ComputeStatsRequest (com.vmware.photon.controller.model.adapterapi.ComputeStatsRequest)8 ComputeStats (com.vmware.photon.controller.model.adapterapi.ComputeStatsResponse.ComputeStats)8 UriUtils (com.vmware.xenon.common.UriUtils)8 URI (java.net.URI)8 QueryUtils (com.vmware.photon.controller.model.query.QueryUtils)7 ComputeStateWithDescription (com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription)7 ClusterUtil (com.vmware.photon.controller.model.util.ClusterUtil)7 ServiceTypeCluster (com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster)7 Utils (com.vmware.xenon.common.Utils)7 QueryTask (com.vmware.xenon.services.common.QueryTask)7 Query (com.vmware.xenon.services.common.QueryTask.Query)7 Map (java.util.Map)7 TimeUnit (java.util.concurrent.TimeUnit)7 UriPaths (com.vmware.photon.controller.model.UriPaths)6 PhotonModelConstants (com.vmware.photon.controller.model.constants.PhotonModelConstants)6