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);
}
Aggregations