Search in sources :

Example 1 with InMemoryResourceMetric

use of com.vmware.photon.controller.model.monitoring.InMemoryResourceMetricService.InMemoryResourceMetric 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)

Aggregations

UriPaths (com.vmware.photon.controller.model.UriPaths)1 ComputeStatsRequest (com.vmware.photon.controller.model.adapterapi.ComputeStatsRequest)1 ComputeStats (com.vmware.photon.controller.model.adapterapi.ComputeStatsResponse.ComputeStats)1 PhotonModelConstants (com.vmware.photon.controller.model.constants.PhotonModelConstants)1 InMemoryResourceMetricService (com.vmware.photon.controller.model.monitoring.InMemoryResourceMetricService)1 InMemoryResourceMetric (com.vmware.photon.controller.model.monitoring.InMemoryResourceMetricService.InMemoryResourceMetric)1 ResourceMetricsService (com.vmware.photon.controller.model.monitoring.ResourceMetricsService)1 ResourceMetrics (com.vmware.photon.controller.model.monitoring.ResourceMetricsService.ResourceMetrics)1 QueryUtils (com.vmware.photon.controller.model.query.QueryUtils)1 ComputeDescription (com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription)1 ComputeStateWithDescription (com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription)1 PhotonModelUtils (com.vmware.photon.controller.model.resources.util.PhotonModelUtils)1 TaskOption (com.vmware.photon.controller.model.tasks.TaskOption)1 TaskUtils (com.vmware.photon.controller.model.tasks.TaskUtils)1 SingleResourceStatsCollectionTaskState (com.vmware.photon.controller.model.tasks.monitoring.SingleResourceStatsCollectionTaskService.SingleResourceStatsCollectionTaskState)1 ClusterUtil (com.vmware.photon.controller.model.util.ClusterUtil)1 ServiceTypeCluster (com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster)1 FactoryService (com.vmware.xenon.common.FactoryService)1 Operation (com.vmware.xenon.common.Operation)1 OperationSequence (com.vmware.xenon.common.OperationSequence)1