Search in sources :

Example 1 with ServiceStats

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

the class LongRunEndToEndStatsAggregationTest 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 : TestAWSSetupUtils.getMetricNames()) {
                if (stat.name.startsWith(key)) {
                    statCount++;
                    break;
                }
            }
        }
        // after stats aggregation all resources should have all metrics.
        assertEquals("Did not find in-memory stats", TestAWSSetupUtils.getMetricNames().size(), statCount);
    }
}
Also used : ServiceStat(com.vmware.xenon.common.ServiceStats.ServiceStat) ServiceStats(com.vmware.xenon.common.ServiceStats) Map(java.util.Map)

Example 2 with ServiceStats

use of com.vmware.xenon.common.ServiceStats 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);
}
Also used : Service(com.vmware.xenon.common.Service) ServiceTypeCluster(com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster) QueryTask(com.vmware.xenon.services.common.QueryTask) ServiceDocument(com.vmware.xenon.common.ServiceDocument) AggregationType(com.vmware.xenon.common.ServiceStats.TimeSeriesStats.AggregationType) MatchType(com.vmware.xenon.services.common.QueryTask.QueryTerm.MatchType) SingleResourceStatsCollectionTaskState(com.vmware.photon.controller.model.tasks.monitoring.SingleResourceStatsCollectionTaskService.SingleResourceStatsCollectionTaskState) Utils(com.vmware.xenon.common.Utils) TaskFactoryService(com.vmware.xenon.services.common.TaskFactoryService) Map(java.util.Map) URI(java.net.URI) EnumSet(java.util.EnumSet) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) ComputeStatsRequest(com.vmware.photon.controller.model.adapterapi.ComputeStatsRequest) ServiceStats(com.vmware.xenon.common.ServiceStats) List(java.util.List) TimeSeriesStats(com.vmware.xenon.common.ServiceStats.TimeSeriesStats) UriUtils(com.vmware.xenon.common.UriUtils) Entry(java.util.Map.Entry) QueryOption(com.vmware.xenon.services.common.QueryTask.QuerySpecification.QueryOption) TaskState(com.vmware.xenon.common.TaskState) TaskOption(com.vmware.photon.controller.model.tasks.TaskOption) FactoryService(com.vmware.xenon.common.FactoryService) NumericRange(com.vmware.xenon.services.common.QueryTask.NumericRange) TaskService(com.vmware.xenon.services.common.TaskService) ResourceMetrics(com.vmware.photon.controller.model.monitoring.ResourceMetricsService.ResourceMetrics) TaskUtils(com.vmware.photon.controller.model.tasks.TaskUtils) InMemoryResourceMetric(com.vmware.photon.controller.model.monitoring.InMemoryResourceMetricService.InMemoryResourceMetric) HashMap(java.util.HashMap) PhotonModelUtils(com.vmware.photon.controller.model.resources.util.PhotonModelUtils) ResourceMetricsService(com.vmware.photon.controller.model.monitoring.ResourceMetricsService) ArrayList(java.util.ArrayList) ServiceUriPaths(com.vmware.xenon.services.common.ServiceUriPaths) Query(com.vmware.xenon.services.common.QueryTask.Query) UriPaths(com.vmware.photon.controller.model.UriPaths) PropertyUsageOption(com.vmware.xenon.common.ServiceDocumentDescription.PropertyUsageOption) ComputeStats(com.vmware.photon.controller.model.adapterapi.ComputeStatsResponse.ComputeStats) OperationSequence(com.vmware.xenon.common.OperationSequence) Operation(com.vmware.xenon.common.Operation) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) TypeName(com.vmware.xenon.common.ServiceDocumentDescription.TypeName) ServiceStat(com.vmware.xenon.common.ServiceStats.ServiceStat) TaskStage(com.vmware.xenon.common.TaskState.TaskStage) TimeUnit(java.util.concurrent.TimeUnit) InMemoryResourceMetricService(com.vmware.photon.controller.model.monitoring.InMemoryResourceMetricService) ClusterUtil(com.vmware.photon.controller.model.util.ClusterUtil) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) PhotonModelConstants(com.vmware.photon.controller.model.constants.PhotonModelConstants) Comparator(java.util.Comparator) QuerySpecification(com.vmware.xenon.services.common.QueryTask.QuerySpecification) ServiceStat(com.vmware.xenon.common.ServiceStats.ServiceStat) ServiceStats(com.vmware.xenon.common.ServiceStats) URI(java.net.URI)

Example 3 with ServiceStats

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

the class StatsCollectionTaskServiceTest method testStatsQueryCustomization.

@Test
public void testStatsQueryCustomization() throws Throwable {
    // Before start clear Computes (if any)
    ServiceDocumentQueryResult computes = this.host.getFactoryState(UriUtils.buildExpandLinksQueryUri(UriUtils.buildUri(this.host, ComputeService.FACTORY_LINK)));
    for (Map.Entry<String, Object> t : computes.documents.entrySet()) {
        deleteServiceSynchronously(t.getKey());
    }
    // create a compute description for all the computes
    ComputeDescription cDesc = new ComputeDescription();
    cDesc.name = UUID.randomUUID().toString();
    cDesc.statsAdapterReference = UriUtils.buildUri(this.host, MockStatsAdapter.SELF_LINK);
    ComputeDescription descReturnState = postServiceSynchronously(ComputeDescriptionService.FACTORY_LINK, cDesc, ComputeDescription.class);
    // create multiple computes
    ComputeState computeState = new ComputeState();
    computeState.name = UUID.randomUUID().toString();
    computeState.descriptionLink = descReturnState.documentSelfLink;
    List<String> computeLinks = new ArrayList<>();
    // Create 20 Computes of different type.
    for (int i = 0; i < 20; i++) {
        // Set even computes to be ENDPOINT_HOST, the odd one -> VM_GUEST
        if (i % 2 == 0) {
            computeState.type = ComputeType.ENDPOINT_HOST;
        } else {
            computeState.type = ComputeType.VM_GUEST;
        }
        ComputeState res = postServiceSynchronously(ComputeService.FACTORY_LINK, computeState, ComputeState.class);
        computeLinks.add(res.documentSelfLink);
    }
    // create a resource pool including all the created computes. It will be customized during
    // StatsCollection task
    ResourcePoolState rpState = new ResourcePoolState();
    rpState.name = UUID.randomUUID().toString();
    rpState.properties = EnumSet.of(ResourcePoolProperty.ELASTIC);
    rpState.query = Query.Builder.create().addKindFieldClause(ComputeState.class).addInClause(ServiceDocument.FIELD_NAME_SELF_LINK, computeLinks).build();
    ResourcePoolState rpReturnState = postServiceSynchronously(ResourcePoolService.FACTORY_LINK, rpState, ResourcePoolState.class);
    // Create additional Query clause
    List<Query> queries = new ArrayList<>();
    Query typeQuery = new Query();
    typeQuery.setOccurance(Occurance.MUST_OCCUR);
    typeQuery.setTermPropertyName(ComputeState.FIELD_NAME_TYPE);
    typeQuery.setTermMatchValue(ComputeType.ENDPOINT_HOST.name());
    queries.add(typeQuery);
    // create a stats collection task
    StatsCollectionTaskState statCollectionState = new StatsCollectionTaskState();
    statCollectionState.resourcePoolLink = rpReturnState.documentSelfLink;
    statCollectionState.customizationClauses = queries;
    // statCollectionState.options = EnumSet.of(TaskOption.SELF_DELETE_ON_COMPLETION);
    StatsCollectionTaskState finalStatCollectionState = postServiceSynchronously(StatsCollectionTaskService.FACTORY_LINK, statCollectionState, StatsCollectionTaskState.class);
    // give 1 minute max time for StatsCollection task to finish.
    host.setTimeoutSeconds(60);
    host.waitFor(String.format("Timeout waiting for StatsCollectionTask: [%s] to complete.", finalStatCollectionState.documentSelfLink), () -> {
        StatsCollectionTaskState stats = getServiceSynchronously(finalStatCollectionState.documentSelfLink, StatsCollectionTaskState.class);
        return stats.taskInfo != null && stats.taskInfo.stage == TaskStage.FINISHED;
    });
    ServiceDocumentQueryResult res = this.host.getFactoryState(UriUtils.buildExpandLinksQueryUri(UriUtils.buildUri(this.host, ComputeService.FACTORY_LINK)));
    assertEquals(20, res.documents.size());
    int vmHosts = 0;
    int vmGuests = 0;
    // ENDPOINT_HOST should provide statistics.
    for (Map.Entry<String, Object> map : res.documents.entrySet()) {
        String uri = String.format("%s/stats", map.getKey());
        ServiceStats stats = getServiceSynchronously(uri, ServiceStats.class);
        ComputeState state = Utils.fromJson(map.getValue(), ComputeState.class);
        if (state.type == ComputeType.ENDPOINT_HOST) {
            assertTrue(!stats.entries.isEmpty());
            vmHosts++;
        } else {
            assertTrue(stats.entries.isEmpty());
            vmGuests++;
        }
    }
    assertEquals(10, vmHosts);
    assertEquals(10, vmGuests);
    // clean up
    deleteServiceSynchronously(finalStatCollectionState.documentSelfLink);
}
Also used : ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) ResourcePoolState(com.vmware.photon.controller.model.resources.ResourcePoolService.ResourcePoolState) Query(com.vmware.xenon.services.common.QueryTask.Query) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) ArrayList(java.util.ArrayList) ServiceDocumentQueryResult(com.vmware.xenon.common.ServiceDocumentQueryResult) ServiceStats(com.vmware.xenon.common.ServiceStats) Map(java.util.Map) HashMap(java.util.HashMap) 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)

Example 4 with ServiceStats

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

the class StatsCollectionTaskServiceTest method testCustomStatsAdapterPrecedence.

@Test
public void testCustomStatsAdapterPrecedence() 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.statsAdapterReference = UriUtils.buildUri(this.host, MockStatsAdapter.SELF_LINK);
    desc.statsAdapterReferences = new HashSet<>(Arrays.asList(UriUtils.buildUri(this.host, "/foo"), 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;
            // populated correctly.
            for (ServiceStat stat : resStats.entries.values()) {
                // host.log(Level.INFO, "*****%s", stat.name);
                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)

Example 5 with ServiceStats

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

the class TestAWSSetupUtils method enumerateResources.

/**
 * Enumerates resources on the AWS endpoint. Expects a peerURI and the tenantLinks to be set.
 */
public static void enumerateResources(VerificationHost host, ComputeState computeHost, EndpointState endpointState, URI peerURI, EnumSet<TaskOption> options, String testCase) throws Throwable {
    // Perform resource enumeration on the AWS end point. Pass the references to the AWS compute
    // host.
    host.log("Performing resource enumeration");
    ResourceEnumerationTaskService.ResourceEnumerationTaskState enumTask = performResourceEnumeration(host, computeHost, endpointState, peerURI, options);
    // Wait for the enumeration task to be completed.
    host.waitForFinishedTask(ResourceEnumerationTaskState.class, createServiceURI(host, peerURI, enumTask.documentSelfLink));
    host.log("\n==%s==Total Time Spent in Enumeration==\n", testCase + getVMCount(host, peerURI));
    ServiceStats enumerationStats = host.getServiceState(null, ServiceStats.class, UriUtils.buildStatsUri(createServiceURI(host, peerURI, AWSEnumerationAdapterService.SELF_LINK)));
    host.log(Utils.toJsonHtml(enumerationStats));
    host.log("\n==Total Time Spent in Creation Workflow==\n");
    ServiceStats enumerationCreationStats = host.getServiceState(null, ServiceStats.class, UriUtils.buildStatsUri(createServiceURI(host, peerURI, AWSEnumerationAndCreationAdapterService.SELF_LINK)));
    host.log(Utils.toJsonHtml(enumerationCreationStats));
    host.log("\n==Time spent in individual creation services==\n");
    ServiceStats computeDescriptionCreationStats = host.getServiceState(null, ServiceStats.class, UriUtils.buildStatsUri(createServiceURI(host, peerURI, AWSComputeDescriptionEnumerationAdapterService.SELF_LINK)));
    host.log(Utils.toJsonHtml(computeDescriptionCreationStats));
    ServiceStats computeStateCreationStats = host.getServiceState(null, ServiceStats.class, UriUtils.buildStatsUri(createServiceURI(host, peerURI, AWSComputeStateCreationAdapterService.SELF_LINK)));
    host.log(Utils.toJsonHtml(computeStateCreationStats));
    host.log("\n==Total Time Spent in Deletion Workflow==\n");
    ServiceStats deletionEnumerationStats = host.getServiceState(null, ServiceStats.class, UriUtils.buildStatsUri(createServiceURI(host, peerURI, AWSEnumerationAndDeletionAdapterService.SELF_LINK)));
    host.log(Utils.toJsonHtml(deletionEnumerationStats));
    host.log("\n==Total Time Spent in EBS Storage Enumeration Workflow==\n");
    ServiceStats ebsStorageEnumerationStats = host.getServiceState(null, ServiceStats.class, UriUtils.buildStatsUri(createServiceURI(host, peerURI, AWSEBSStorageEnumerationAdapterService.SELF_LINK)));
    host.log(Utils.toJsonHtml(ebsStorageEnumerationStats));
    host.log("\n==Total Time Spent in S3 Storage Enumeration Workflow==\n");
    ServiceStats s3StorageEnumerationStats = host.getServiceState(null, ServiceStats.class, UriUtils.buildStatsUri(createServiceURI(host, peerURI, AWSS3StorageEnumerationAdapterService.SELF_LINK)));
    host.log(Utils.toJsonHtml(s3StorageEnumerationStats));
}
Also used : ServiceStats(com.vmware.xenon.common.ServiceStats) ResourceEnumerationTaskState(com.vmware.photon.controller.model.tasks.ResourceEnumerationTaskService.ResourceEnumerationTaskState) ResourceEnumerationTaskService(com.vmware.photon.controller.model.tasks.ResourceEnumerationTaskService)

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