Search in sources :

Example 16 with ResourceMetrics

use of com.vmware.photon.controller.model.monitoring.ResourceMetricsService.ResourceMetrics in project photon-model by vmware.

the class VsphereDatastoreEnumerationHelper method updateStorageStats.

private static void updateStorageStats(VSphereIncrementalEnumerationService service, DatastoreOverlay ds, String selfLink) {
    // update stats only if capacity is greater than zero
    if (ds.getCapacityBytesOrZero() > 0) {
        ResourceMetrics metrics = new ResourceMetrics();
        metrics.timestampMicrosUtc = Utils.getNowMicrosUtc();
        metrics.documentSelfLink = StatsUtil.getMetricKey(selfLink, metrics.timestampMicrosUtc);
        metrics.entries = new HashMap<>();
        metrics.entries.put(STORAGE_USED_BYTES, (double) ds.getCapacityBytes() - ds.getFreeSpaceBytes());
        metrics.entries.put(STORAGE_AVAILABLE_BYTES, (double) ds.getFreeSpaceBytes());
        metrics.documentExpirationTimeMicros = Utils.getNowMicrosUtc() + TimeUnit.DAYS.toMicros(SingleResourceStatsCollectionTaskService.EXPIRATION_INTERVAL);
        metrics.customProperties = new HashMap<>();
        metrics.customProperties.put(ResourceMetrics.PROPERTY_RESOURCE_LINK, selfLink);
        Operation.createPost(UriUtils.buildUri(ClusterUtil.getClusterUri(service.getHost(), ServiceTypeCluster.METRIC_SERVICE), ResourceMetricsService.FACTORY_LINK)).setBodyNoCloning(metrics).sendWith(service);
    }
}
Also used : ResourceMetrics(com.vmware.photon.controller.model.monitoring.ResourceMetricsService.ResourceMetrics)

Example 17 with ResourceMetrics

use of com.vmware.photon.controller.model.monitoring.ResourceMetricsService.ResourceMetrics in project photon-model by vmware.

the class LongRunEndToEndStatsAggregationTest method getResourceMetrics.

/**
 * Method returns ResourceMetrics collected during stats collection and aggregation for a
 * resource endpoint for the given resource metric key.
 */
private ResourceMetrics getResourceMetrics(String resourceLink, String metricKey) {
    QueryTask qt = QueryTask.Builder.createDirectTask().addOption(QueryOption.TOP_RESULTS).addOption(QueryOption.INCLUDE_ALL_VERSIONS).setResultLimit(1).addOption(QueryOption.EXPAND_CONTENT).addOption(QueryOption.SORT).orderDescending(ServiceDocument.FIELD_NAME_SELF_LINK, ServiceDocumentDescription.TypeName.STRING).setQuery(QueryTask.Query.Builder.create().addKindFieldClause(ResourceMetrics.class).addFieldClause(ServiceDocument.FIELD_NAME_SELF_LINK, UriUtils.buildUriPath(ResourceMetricsService.FACTORY_LINK, UriUtils.getLastPathSegment(resourceLink)), QueryTask.QueryTerm.MatchType.PREFIX).addRangeClause(QuerySpecification.buildCompositeFieldName(ResourceMetrics.FIELD_NAME_ENTRIES, metricKey), QueryTask.NumericRange.createDoubleRange(0.0, Double.MAX_VALUE, true, true)).build()).build();
    this.host.createQueryTaskService(qt, false, true, qt, null);
    ResourceMetrics resourceMetric = null;
    if (qt.results.documentLinks.size() > 0) {
        String documentLink = qt.results.documentLinks.get(0);
        resourceMetric = Utils.fromJson(qt.results.documents.get(documentLink), ResourceMetrics.class);
    }
    return resourceMetric;
}
Also used : ResourceMetrics(com.vmware.photon.controller.model.monitoring.ResourceMetricsService.ResourceMetrics) QueryTask(com.vmware.xenon.services.common.QueryTask)

Example 18 with ResourceMetrics

use of com.vmware.photon.controller.model.monitoring.ResourceMetricsService.ResourceMetrics in project photon-model by vmware.

the class LongRunEndToEndStatsAggregationTest method checkExpirationTime.

/**
 * Performs check to verify that expiration time for aggregate metric documents for every
 * compute resource has been set to be expired after 56 days.
 */
private void checkExpirationTime(ServiceDocumentQueryResult aggrResult) {
    long expectedExpirationTime = Utils.getNowMicrosUtc() + TimeUnit.DAYS.toMicros(DEFAULT_RETENTION_LIMIT_DAYS);
    for (Object aggrDocument : aggrResult.documents.values()) {
        ResourceMetrics aggrMetric = Utils.fromJson(aggrDocument, ResourceMetrics.class);
        // Make sure all the documents have expiration time set.
        assertTrue("Expiration time is not correctly set.", aggrMetric.documentExpirationTimeMicros < expectedExpirationTime);
    }
}
Also used : ResourceMetrics(com.vmware.photon.controller.model.monitoring.ResourceMetricsService.ResourceMetrics)

Example 19 with ResourceMetrics

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

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

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