Search in sources :

Example 6 with ServiceStats

use of com.vmware.xenon.common.ServiceStats in project photon-model by vmware.

the class LongRunEndToEndAzureStatsAggregation method checkInMemoryStatsPresent.

/**
 * Perform check to verify that every compute resource has stats generated for all the metric
 * keys using which stats aggregation was performed on resources.
 */
private void checkInMemoryStatsPresent(ServiceDocumentQueryResult res) {
    for (Map.Entry<String, Object> resourceMap : res.documents.entrySet()) {
        ServiceStats resStats = this.host.getServiceState(null, ServiceStats.class, UriUtils.buildStatsUri(createServiceURI(host, null, resourceMap.getKey())));
        int statCount = 0;
        for (ServiceStat stat : resStats.entries.values()) {
            for (String key : AzureTestUtil.getMetricNames()) {
                if (stat.name.startsWith(key)) {
                    statCount++;
                    break;
                }
            }
        }
        // after stats aggregation all resources should have all metrics.
        assertEquals("Did not find in-memory stats", AzureTestUtil.getMetricNames().size(), statCount);
    }
}
Also used : ServiceStat(com.vmware.xenon.common.ServiceStats.ServiceStat) ServiceStats(com.vmware.xenon.common.ServiceStats) Map(java.util.Map)

Example 7 with ServiceStats

use of com.vmware.xenon.common.ServiceStats in project photon-model by vmware.

the class LongRunEndToEndStatsCollectionTest method verifyMetricStats.

/**
 * Performs query to fetch Resource metric for list of compute resources.
 * Verifies the counts of resources to indicate number of ENDPOINT_HOST, VM_GUEST and other compute
 * types that includes compute type ZONE.
 */
private void verifyMetricStats(ServiceDocumentQueryResult res, StatsToCheck statsToCheck) {
    // count for resources of type ENDPOINT_HOST
    int vmHosts = 0;
    // count for resources of type VM_GUEST
    int vmGuests = 0;
    // count for resources of type ZONE
    int zones = 0;
    // count for resources of type others
    int misc = 0;
    // count of resources having Estimated Charges resource metric.
    int resourcesWithEstChargesMetricCount = 0;
    // count of resources not having Estimated Charges resource metric.
    int resourcesWithNoEstChargesMetricCount = 0;
    ResourceMetrics resourceMetric;
    // Fetch stats for each compute resource and verify its Compute Type and stat values.
    for (Map.Entry<String, Object> resourceMap : res.documents.entrySet()) {
        // Fetch stats for a compute resource
        ServiceStats stats = this.host.getServiceState(null, ServiceStats.class, UriUtils.buildStatsUri(createServiceURI(host, null, resourceMap.getKey())));
        ComputeState state = Utils.fromJson(resourceMap.getValue(), ComputeState.class);
        if (state.type == ComputeType.ENDPOINT_HOST) {
            // regionId should be null
            assertNull(state.regionId);
            resourceMetric = getResourceMetrics(resourceMap.getKey(), PhotonModelConstants.ESTIMATED_CHARGES);
            // EstimatedCharges metric will be available for ENDPOINT_HOST resource
            assertNotNull(resourceMetric);
            statsToCheck.estimatedCharges = resourceMetric.entries.get(PhotonModelConstants.ESTIMATED_CHARGES);
            // CPUUtilizationPercent metric will not be available for ENDPOINT_HOST resource
            resourceMetric = getResourceMetrics(resourceMap.getKey(), PhotonModelConstants.CPU_UTILIZATION_PERCENT);
            assertNull(resourceMetric);
            vmHosts++;
            resourcesWithEstChargesMetricCount++;
        } else if (state.type == ComputeType.VM_GUEST) {
            // regionId should not be null
            assertNotNull(state.regionId);
            long micros = this.currentTimeMicros - state.creationTimeMicros;
            resourceMetric = getResourceMetrics(resourceMap.getKey(), PhotonModelConstants.ESTIMATED_CHARGES);
            // EstimatedCharges metric will not be available for VM_GUEST resource
            assertNull(resourceMetric);
            // CPUUtilizationPercent metric will be available for VM_GUEST resource
            resourceMetric = getResourceMetrics(resourceMap.getKey(), PhotonModelConstants.CPU_UTILIZATION_PERCENT);
            // time VM was created.
            if (state.powerState == ComputeService.PowerState.ON && micros > TimeUnit.MINUTES.toMicros(initTimeForStatsAvailInMin)) {
                assertNotNull(resourceMetric);
            }
            vmGuests++;
            resourcesWithNoEstChargesMetricCount++;
        } else if (state.type == ComputeType.ZONE) {
            // regionId should not be null
            assertNotNull(state.regionId);
            resourceMetric = getResourceMetrics(resourceMap.getKey(), PhotonModelConstants.ESTIMATED_CHARGES);
            // EstimatedCharges metric will not be available for ZONE
            assertNull(resourceMetric);
            // CPUUtilizationPercent metric will not be available for ZONE
            resourceMetric = getResourceMetrics(resourceMap.getKey(), PhotonModelConstants.CPU_UTILIZATION_PERCENT);
            assertNull(resourceMetric);
            zones++;
            resourcesWithNoEstChargesMetricCount++;
        } else {
            misc++;
        }
        // store stats entries values for last successful collection time.
        statsToCheck.serviceStat.version = stats.entries.get(this.lastSuccessfulCollectionTimeKey).version;
        statsToCheck.serviceStat.lastUpdateMicrosUtc = stats.entries.get(this.lastSuccessfulCollectionTimeKey).lastUpdateMicrosUtc;
        statsToCheck.serviceStat.latestValue = stats.entries.get(this.lastSuccessfulCollectionTimeKey).latestValue;
        // verify last successful collection time values is set and not null for each resource.
        assertNotNull(statsToCheck.serviceStat.version);
        assertNotNull(statsToCheck.serviceStat.lastUpdateMicrosUtc);
        assertNotNull(statsToCheck.serviceStat.latestValue);
    }
    // count of computes of misc type should be 0.
    assertEquals(0, misc);
    // There should be single host for a given Endpoint.
    assertEquals(1, vmHosts);
    // sum of VM_GUEST's + ZONE's should be equal to total compute resources minus ENDPOINT_HOST's
    // this will throw exception if there are other types of compute resources discovered.
    assertEquals(this.totalComputeResources - vmHosts, vmGuests + zones);
    // Number of ENDPOINT_HOST's equals number of resources having metric for EstimatedCharges
    assertEquals(vmHosts, resourcesWithEstChargesMetricCount);
    // count of other resources will equal to number of resources not having metric for EstimatedCharges
    assertEquals(vmGuests + zones, resourcesWithNoEstChargesMetricCount);
    // count of zones supposed to be greater than 0
    assertTrue(zones > 0);
}
Also used : ResourceMetrics(com.vmware.photon.controller.model.monitoring.ResourceMetricsService.ResourceMetrics) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) ServiceStats(com.vmware.xenon.common.ServiceStats) JsonObject(com.google.gson.JsonObject) Map(java.util.Map)

Example 8 with ServiceStats

use of com.vmware.xenon.common.ServiceStats in project photon-model by vmware.

the class ResourceGroomerTaskServiceTest method assertStats.

/**
 * Assert deletedDocumentCount and patchedDocumentCount stats of the groomer task that ran.
 */
public void assertStats(double deletedDocumentCount, double endpointLinksPatchedCount, double endpointLinkPatchedCount, String taskLink) {
    URI taskStatsUri = UriUtils.buildStatsUri(this.host, taskLink);
    Operation getStatsOp = Operation.createGet(taskStatsUri).setReferer(this.host.getUri());
    Operation response = this.host.waitForResponse(getStatsOp);
    ServiceStats stats = response.getBody(ServiceStats.class);
    assertEquals(deletedDocumentCount, stats.entries.get(ResourceGroomerTaskService.STAT_NAME_DOCUMENTS_DELETED).latestValue, 0);
    assertEquals(endpointLinksPatchedCount, stats.entries.get(ResourceGroomerTaskService.STAT_NAME_ENDPOINT_LINKS_PATCHED).latestValue, 0);
    assertEquals(endpointLinkPatchedCount, stats.entries.get(ResourceGroomerTaskService.STAT_NAME_ENDPOINT_LINK_PATCHED).latestValue, 0);
}
Also used : ServiceStats(com.vmware.xenon.common.ServiceStats) Operation(com.vmware.xenon.common.Operation) URI(java.net.URI)

Example 9 with ServiceStats

use of com.vmware.xenon.common.ServiceStats 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;
    });
}
Also used : ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) SingleResourceStatsAggregationTaskState(com.vmware.photon.controller.model.tasks.monitoring.SingleResourceStatsAggregationTaskService.SingleResourceStatsAggregationTaskState) ResourcePoolState(com.vmware.photon.controller.model.resources.ResourcePoolService.ResourcePoolState) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) ServiceDocumentQueryResult(com.vmware.xenon.common.ServiceDocumentQueryResult) ServiceStat(com.vmware.xenon.common.ServiceStats.ServiceStat) ResourceMetrics(com.vmware.photon.controller.model.monitoring.ResourceMetricsService.ResourceMetrics) ServiceStats(com.vmware.xenon.common.ServiceStats) StatsCollectionTaskState(com.vmware.photon.controller.model.tasks.monitoring.StatsCollectionTaskService.StatsCollectionTaskState) BaseModelTest(com.vmware.photon.controller.model.helpers.BaseModelTest) Test(org.junit.Test)

Example 10 with ServiceStats

use of com.vmware.xenon.common.ServiceStats in project photon-model by vmware.

the class StatsCollectionTaskServiceTest method testCustomStatsAdapter.

@Test
public void testCustomStatsAdapter() throws Throwable {
    ResourcePoolState rpState = new ResourcePoolState();
    rpState.name = UUID.randomUUID().toString();
    ResourcePoolState rpReturnState = postServiceSynchronously(ResourcePoolService.FACTORY_LINK, rpState, ResourcePoolState.class);
    ComputeDescription desc = new ComputeDescription();
    desc.name = rpState.name;
    desc.statsAdapterReferences = Collections.singleton(UriUtils.buildUri(this.host, CustomStatsAdapter.SELF_LINK));
    ComputeDescription descReturnState = postServiceSynchronously(ComputeDescriptionService.FACTORY_LINK, desc, ComputeDescription.class);
    ComputeState computeState = new ComputeState();
    computeState.name = rpState.name;
    computeState.descriptionLink = descReturnState.documentSelfLink;
    computeState.resourcePoolLink = rpReturnState.documentSelfLink;
    List<String> computeLinks = new ArrayList<>();
    for (int i = 0; i < this.numResources; i++) {
        ComputeState res = postServiceSynchronously(ComputeService.FACTORY_LINK, computeState, ComputeState.class);
        computeLinks.add(res.documentSelfLink);
    }
    // create a stats collection scheduler task
    StatsCollectionTaskState statCollectionState = new StatsCollectionTaskState();
    statCollectionState.resourcePoolLink = rpReturnState.documentSelfLink;
    statCollectionState.statsAdapterReference = UriUtils.buildUri(this.host, CustomStatsAdapter.SELF_LINK);
    statCollectionState.options = EnumSet.of(TaskOption.SELF_DELETE_ON_COMPLETION);
    ScheduledTaskState statsCollectionTaskState = new ScheduledTaskState();
    statsCollectionTaskState.factoryLink = StatsCollectionTaskService.FACTORY_LINK;
    statsCollectionTaskState.initialStateJson = Utils.toJson(statCollectionState);
    statsCollectionTaskState.intervalMicros = TimeUnit.MILLISECONDS.toMicros(500);
    statsCollectionTaskState = postServiceSynchronously(ScheduledTaskService.FACTORY_LINK, statsCollectionTaskState, ScheduledTaskState.class);
    ServiceDocumentQueryResult res = this.host.getFactoryState(UriUtils.buildExpandLinksQueryUri(UriUtils.buildUri(this.host, ScheduledTaskService.FACTORY_LINK)));
    assertTrue(res.documents.size() > 0);
    // get stats from resources
    for (int i = 0; i < computeLinks.size(); i++) {
        String statsUriPath = UriUtils.buildUriPath(computeLinks.get(i), ServiceHost.SERVICE_URI_SUFFIX_STATS);
        this.host.waitFor("Error waiting for stats", () -> {
            ServiceStats resStats = getServiceSynchronously(statsUriPath, ServiceStats.class);
            boolean returnStatus = false;
            // was populated in the in memory stats
            for (ServiceStat stat : resStats.entries.values()) {
                if (stat.name.startsWith(UriUtils.getLastPathSegment(CustomStatsAdapter.SELF_LINK))) {
                    returnStatus = true;
                    break;
                }
            }
            return returnStatus;
        });
    }
    // clean up
    deleteServiceSynchronously(statsCollectionTaskState.documentSelfLink);
}
Also used : ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) ResourcePoolState(com.vmware.photon.controller.model.resources.ResourcePoolService.ResourcePoolState) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) ArrayList(java.util.ArrayList) ServiceDocumentQueryResult(com.vmware.xenon.common.ServiceDocumentQueryResult) ServiceStat(com.vmware.xenon.common.ServiceStats.ServiceStat) ServiceStats(com.vmware.xenon.common.ServiceStats) ScheduledTaskState(com.vmware.photon.controller.model.tasks.ScheduledTaskService.ScheduledTaskState) StatsCollectionTaskState(com.vmware.photon.controller.model.tasks.monitoring.StatsCollectionTaskService.StatsCollectionTaskState) SingleResourceStatsCollectionTaskState(com.vmware.photon.controller.model.tasks.monitoring.SingleResourceStatsCollectionTaskService.SingleResourceStatsCollectionTaskState) BaseModelTest(com.vmware.photon.controller.model.helpers.BaseModelTest) Test(org.junit.Test)

Aggregations

ServiceStats (com.vmware.xenon.common.ServiceStats)13 ServiceStat (com.vmware.xenon.common.ServiceStats.ServiceStat)7 ComputeDescription (com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription)6 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)6 ArrayList (java.util.ArrayList)6 Map (java.util.Map)6 ResourceMetrics (com.vmware.photon.controller.model.monitoring.ResourceMetricsService.ResourceMetrics)5 ResourcePoolState (com.vmware.photon.controller.model.resources.ResourcePoolService.ResourcePoolState)5 SingleResourceStatsCollectionTaskState (com.vmware.photon.controller.model.tasks.monitoring.SingleResourceStatsCollectionTaskService.SingleResourceStatsCollectionTaskState)5 StatsCollectionTaskState (com.vmware.photon.controller.model.tasks.monitoring.StatsCollectionTaskService.StatsCollectionTaskState)5 ServiceDocumentQueryResult (com.vmware.xenon.common.ServiceDocumentQueryResult)5 BaseModelTest (com.vmware.photon.controller.model.helpers.BaseModelTest)4 Test (org.junit.Test)4 ScheduledTaskState (com.vmware.photon.controller.model.tasks.ScheduledTaskService.ScheduledTaskState)3 Operation (com.vmware.xenon.common.Operation)3 UriPaths (com.vmware.photon.controller.model.UriPaths)2 InMemoryResourceMetricService (com.vmware.photon.controller.model.monitoring.InMemoryResourceMetricService)2 InMemoryResourceMetric (com.vmware.photon.controller.model.monitoring.InMemoryResourceMetricService.InMemoryResourceMetric)2 ResourceMetricsService (com.vmware.photon.controller.model.monitoring.ResourceMetricsService)2 QueryUtils (com.vmware.photon.controller.model.query.QueryUtils)2