use of com.vmware.photon.controller.model.tasks.monitoring.SingleResourceStatsAggregationTaskService.SingleResourceStatsAggregationTaskState in project photon-model by vmware.
the class SingleResourceStatsAggregationTaskServiceTest method testResourceStatsAggregation.
@Test
public void testResourceStatsAggregation() throws Throwable {
// create a resource pool
ResourcePoolState rpState = new ResourcePoolState();
rpState.name = "testName";
ResourcePoolState rpReturnState = postServiceSynchronously(ResourcePoolService.FACTORY_LINK, rpState, ResourcePoolState.class);
ComputeState[] computeStateArray = new ComputeState[NUM_COMPUTE_RESOURCES];
for (int i = 0; i < NUM_COMPUTE_RESOURCES; i++) {
ComputeDescription cDesc = new ComputeDescription();
cDesc.name = rpState.name;
cDesc.statsAdapterReference = UriUtils.buildUri(this.host, MockStatsAdapter.SELF_LINK);
ComputeDescription descReturnState = postServiceSynchronously(ComputeDescriptionService.FACTORY_LINK, cDesc, ComputeDescription.class);
ComputeState computeState = new ComputeState();
computeState.name = rpState.name;
computeState.descriptionLink = descReturnState.documentSelfLink;
computeState.resourcePoolLink = rpReturnState.documentSelfLink;
computeStateArray[i] = postServiceSynchronously(ComputeService.FACTORY_LINK, computeState, ComputeState.class);
}
StatsCollectionTaskState collectionTaskState = new StatsCollectionTaskState();
collectionTaskState.resourcePoolLink = rpReturnState.documentSelfLink;
collectionTaskState.taskInfo = TaskState.createDirect();
int counter = 0;
// executing stats collection multiple times
while (counter < NUM_COLLECTIONS) {
this.postServiceSynchronously(StatsCollectionTaskService.FACTORY_LINK, collectionTaskState, StatsCollectionTaskState.class);
counter++;
}
// wait for stats to be populated
this.host.waitFor("Error waiting for stats", () -> {
boolean returnStatus = false;
for (int i = 0; i < NUM_COMPUTE_RESOURCES; i++) {
String statsUriPath = UriUtils.buildUriPath(computeStateArray[i].documentSelfLink, ServiceHost.SERVICE_URI_SUFFIX_STATS);
ServiceStats resStats = getServiceSynchronously(statsUriPath, ServiceStats.class);
for (ServiceStat stat : resStats.entries.values()) {
if (stat.name.startsWith(UriUtils.getLastPathSegment(MockStatsAdapter.SELF_LINK))) {
returnStatus = true;
break;
}
}
}
return returnStatus;
});
// number of keys = 2, number of lastCollectionTimeMetrics = 2
int numberOfRawMetrics = NUM_COLLECTIONS * NUM_COMPUTE_RESOURCES * 2 * 2;
// kick off an aggregation task
SingleResourceStatsAggregationTaskState aggregationTaskState = new SingleResourceStatsAggregationTaskState();
aggregationTaskState.resourceLink = computeStateArray[0].documentSelfLink;
aggregationTaskState.metricNames = new HashSet<>(Arrays.asList(MockStatsAdapter.KEY_1, MockStatsAdapter.KEY_2, MockStatsAdapter.KEY_3));
postServiceSynchronously(SingleResourceStatsAggregationTaskService.FACTORY_LINK, aggregationTaskState, SingleResourceStatsAggregationTaskState.class);
long expectedExpirationTime = Utils.getNowMicrosUtc() + TimeUnit.DAYS.toMicros(DEFAULT_RETENTION_LIMIT_DAYS);
this.host.waitFor("Error waiting for rolled up stats", () -> {
ServiceDocumentQueryResult result = this.host.getExpandedFactoryState(UriUtils.buildUri(this.host, ResourceMetricsService.FACTORY_LINK));
String randomDocumentLink = result.documentLinks.get(0);
ResourceMetrics metric = Utils.fromJson(result.documents.get(randomDocumentLink), ResourceMetrics.class);
// Make sure all the documents have expiration time set.
Assert.assertTrue("Expiration time is not correctly set.", metric.documentExpirationTimeMicros < expectedExpirationTime);
return (result.documentCount == 2 + numberOfRawMetrics);
});
String statsUriPath = UriUtils.buildUriPath(computeStateArray[0].documentSelfLink, ServiceHost.SERVICE_URI_SUFFIX_STATS);
ServiceStats resStats = getServiceSynchronously(statsUriPath, ServiceStats.class);
int statCount = 0;
for (ServiceStat stat : resStats.entries.values()) {
if (stat.name.startsWith(MockStatsAdapter.KEY_1) || stat.name.startsWith(MockStatsAdapter.KEY_2)) {
statCount++;
}
}
Assert.assertEquals("Did not find in-memory stats", 2, statCount);
// verify that the aggregation tasks have been deleted
this.host.waitFor("Timeout waiting for task to expire", () -> {
ServiceDocumentQueryResult res = this.host.getFactoryState(UriUtils.buildUri(this.host, SingleResourceStatsAggregationTaskService.FACTORY_LINK));
return res.documentLinks.size() == 0;
});
}
use of com.vmware.photon.controller.model.tasks.monitoring.SingleResourceStatsAggregationTaskService.SingleResourceStatsAggregationTaskState in project photon-model by vmware.
the class StatsAggregationTaskService method createSingleResourceComputeTask.
private void createSingleResourceComputeTask(String resourceLink, String subtaskLink, StatsAggregationTaskState currentState) {
SingleResourceStatsAggregationTaskState initState = new SingleResourceStatsAggregationTaskState();
initState.resourceLink = resourceLink;
initState.metricNames = currentState.metricNames;
initState.parentTaskReference = UriUtils.buildPublicUri(getHost(), subtaskLink);
sendRequest(Operation.createPost(this, SingleResourceStatsAggregationTaskService.FACTORY_LINK).setBody(initState).setCompletion((factoryPostOp, factoryPostEx) -> {
if (factoryPostEx != null) {
TaskUtils.sendFailurePatch(this, new StatsAggregationTaskState(), factoryPostEx);
}
}));
}
Aggregations