Search in sources :

Example 31 with ResourceMetrics

use of com.vmware.photon.controller.model.monitoring.ResourceMetricsService.ResourceMetrics 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

ResourceMetrics (com.vmware.photon.controller.model.monitoring.ResourceMetricsService.ResourceMetrics)31 QueryTask (com.vmware.xenon.services.common.QueryTask)19 ArrayList (java.util.ArrayList)12 Operation (com.vmware.xenon.common.Operation)10 Query (com.vmware.xenon.services.common.QueryTask.Query)9 HashMap (java.util.HashMap)9 Map (java.util.Map)9 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)7 ServiceStat (com.vmware.xenon.common.ServiceStats.ServiceStat)7 URI (java.net.URI)7 ServiceStats (com.vmware.xenon.common.ServiceStats)6 ResourceMetricsService (com.vmware.photon.controller.model.monitoring.ResourceMetricsService)5 SingleResourceStatsCollectionTaskState (com.vmware.photon.controller.model.tasks.monitoring.SingleResourceStatsCollectionTaskService.SingleResourceStatsCollectionTaskState)5 ServiceDocument (com.vmware.xenon.common.ServiceDocument)5 UriUtils (com.vmware.xenon.common.UriUtils)5 UriPaths (com.vmware.photon.controller.model.UriPaths)4 ComputeStats (com.vmware.photon.controller.model.adapterapi.ComputeStatsResponse.ComputeStats)4 List (java.util.List)4 ComputeStatsRequest (com.vmware.photon.controller.model.adapterapi.ComputeStatsRequest)3 AwsServices (com.vmware.photon.controller.model.adapters.awsadapter.util.AWSCsvBillParser.AwsServices)3