use of com.vmware.photon.controller.model.tasks.monitoring.SingleResourceStatsCollectionTaskService.SingleResourceStatsCollectionTaskState in project photon-model by vmware.
the class AWSCostStatsService method postAccumulatedCostStats.
protected void postAccumulatedCostStats(AWSCostStatsCreationContext statsData, boolean isFinalBatch, boolean shouldForcePost) {
int batchSize = Integer.getInteger(BATCH_SIZE_KEY, DEFAULT_BATCH_SIZE);
if (!shouldForcePost && !isFinalBatch && statsData.statsResponse.statsList.size() < batchSize) {
return;
}
SingleResourceStatsCollectionTaskState respBody = new SingleResourceStatsCollectionTaskState();
respBody.taskStage = SingleResourceTaskCollectionStage.valueOf(statsData.statsRequest.nextStage);
respBody.statsAdapterReference = UriUtils.buildUri(getHost(), SELF_LINK);
respBody.statsList = statsData.statsResponse.statsList;
respBody.computeLink = statsData.computeDesc.documentSelfLink;
respBody.isFinalBatch = isFinalBatch;
sendRequest(Operation.createPatch(statsData.statsRequest.taskReference).setBody(respBody).setCompletion((o, e) -> {
if (e != null) {
getFailureConsumer(statsData).accept(e);
return;
}
}));
statsData.statsResponse.statsList = new ArrayList<>();
}
use of com.vmware.photon.controller.model.tasks.monitoring.SingleResourceStatsCollectionTaskService.SingleResourceStatsCollectionTaskState in project photon-model by vmware.
the class AzureCostStatsService method postStats.
/**
* Send stats for persistence.
* @param context Holds data to be posted for persistence in
* {@code context.statsResponse.statsList}
*/
private void postStats(Context context) {
if (!context.isFinalBatch && context.statsResponse.statsList.size() == 0) {
return;
}
SingleResourceStatsCollectionTaskState respBody = new SingleResourceStatsCollectionTaskState();
respBody.taskStage = SingleResourceTaskCollectionStage.valueOf(context.statsRequest.nextStage);
respBody.statsAdapterReference = UriUtils.buildUri(getHost(), SELF_LINK);
respBody.statsList = context.statsResponse.statsList;
respBody.computeLink = context.computeHostDesc.documentSelfLink;
respBody.isFinalBatch = context.isFinalBatch;
sendRequest(Operation.createPatch(context.statsRequest.taskReference).setBody(respBody).setCompletion((operation, exception) -> {
if (exception != null) {
handleError(context, null, exception, false);
}
}));
context.statsResponse.statsList = new ArrayList<>();
}
use of com.vmware.photon.controller.model.tasks.monitoring.SingleResourceStatsCollectionTaskService.SingleResourceStatsCollectionTaskState in project photon-model by vmware.
the class SingleResourceStatsCollectionTaskService method populateLastCollectionTimeForMetricsInStatsRequest.
/**
* Gets the last collection for a compute for a given adapter URI.
* As a first step, the in memory stats for the compute are queried and if the metric
* for the last collection time is found, then the timestamp for that is returned.
*
* Else, the ResoureMetric table is queried and the latest version of the metric is used
* to determine the last collection time for the stats.
*/
private void populateLastCollectionTimeForMetricsInStatsRequest(SingleResourceStatsCollectionTaskState currentState, ComputeStatsRequest computeStatsRequest, URI patchUri, List<String> tenantLinks) {
URI computeStatsUri = UriUtils.buildStatsUri(UriUtils.extendUri(ClusterUtil.getClusterUri(getHost(), ServiceTypeCluster.INVENTORY_SERVICE), currentState.computeLink));
Operation.createGet(computeStatsUri).setCompletion((o, e) -> {
if (e != null) {
logSevere(() -> String.format("Could not get the last collection time from" + " in memory stats: %s", Utils.toString(e)));
// get the value from the persisted store.
populateLastCollectionTimeFromPersistenceStore(currentState, computeStatsRequest, patchUri, tenantLinks);
return;
}
ServiceStats serviceStats = o.getBody(ServiceStats.class);
String statsAdapterLink = getAdapterLinkFromURI(patchUri);
String lastSuccessfulRunMetricKey = getLastCollectionMetricKeyForAdapterLink(statsAdapterLink, true);
if (serviceStats.entries.containsKey(lastSuccessfulRunMetricKey)) {
ServiceStat lastRunStat = serviceStats.entries.get(lastSuccessfulRunMetricKey);
computeStatsRequest.lastCollectionTimeMicrosUtc = lastRunStat.sourceTimeMicrosUtc;
sendStatsRequestToAdapter(currentState, patchUri, computeStatsRequest);
} else {
populateLastCollectionTimeFromPersistenceStore(currentState, computeStatsRequest, patchUri, tenantLinks);
}
}).sendWith(this);
}
use of com.vmware.photon.controller.model.tasks.monitoring.SingleResourceStatsCollectionTaskService.SingleResourceStatsCollectionTaskState 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);
}
use of com.vmware.photon.controller.model.tasks.monitoring.SingleResourceStatsCollectionTaskService.SingleResourceStatsCollectionTaskState in project photon-model by vmware.
the class SingleResourceStatsCollectionTaskService method handleStart.
@Override
public void handleStart(Operation taskOperation) {
SingleResourceStatsCollectionTaskState initialState = validateStartPost(taskOperation);
if (initialState == null) {
return;
}
initializeState(initialState, taskOperation);
initialState.taskInfo.stage = TaskStage.CREATED;
taskOperation.setBody(initialState).setStatusCode(Operation.STATUS_CODE_ACCEPTED).complete();
// self patch to start state machine
sendSelfPatch(initialState, TaskStage.STARTED, null);
}
Aggregations