Search in sources :

Example 46 with ComputeState

use of com.vmware.photon.controller.model.resources.ComputeService.ComputeState in project photon-model by vmware.

the class MetricsClusterTest method testStatsCollectorCreation.

@Test
public void testStatsCollectorCreation() throws Throwable {
    // create a resource pool
    ResourcePoolState rpState = new ResourcePoolState();
    rpState.name = UUID.randomUUID().toString();
    ResourcePoolState rpReturnState = postServiceSynchronously(ResourcePoolService.FACTORY_LINK, rpState, ResourcePoolState.class);
    // 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;
    computeState.resourcePoolLink = rpReturnState.documentSelfLink;
    List<String> computeLinks = new ArrayList<>(this.numResources);
    for (int i = 0; i < this.numResources; i++) {
        ComputeState res = postServiceSynchronously(ComputeService.FACTORY_LINK, computeState, ComputeState.class);
        computeLinks.add(res.documentSelfLink);
    }
    // Run a stats collection on the resources
    StatsCollectionTaskState collectionTaskState = new StatsCollectionTaskState();
    collectionTaskState.resourcePoolLink = rpReturnState.documentSelfLink;
    collectionTaskState.options = EnumSet.of(TaskOption.SELF_DELETE_ON_COMPLETION);
    collectionTaskState.taskInfo = TaskState.createDirect();
    postServiceSynchronously(StatsCollectionTaskService.FACTORY_LINK, collectionTaskState, StatsCollectionTaskState.class);
    int numberOfRawMetrics = this.numResources * 4;
    // verify that the collection tasks have been deleted
    this.host.waitFor("Timeout waiting for task to expire", () -> {
        ServiceDocumentQueryResult collectRes = this.host.getFactoryState(UriUtils.buildUri(this.host, StatsCollectionTaskService.FACTORY_LINK));
        if (collectRes.documentLinks.size() == 0) {
            return true;
        }
        return false;
    });
    // Get a Count on this.host() for Resource metrics
    Long resourceHostResourceMetricCount = this.getDocumentCount(this.host, ResourceMetricsService.FACTORY_LINK);
    // Get a Count on this.metricHost() for ResourceMetrics
    Long metricHostResourceMetricCount = this.getDocumentCount(this.metricHost, ResourceMetricsService.FACTORY_LINK);
    // Count should be 0 on this.host()
    assertEquals(0, resourceHostResourceMetricCount.intValue());
    // Count should be something on this.metricHost()
    assertEquals(this.numResources * 4, metricHostResourceMetricCount.intValue());
    // Kick off an Aggregation Task - Verifies that the ResourceMetricQueries are going to the right cluster.
    StatsAggregationTaskState aggregationTaskState = new StatsAggregationTaskState();
    Query taskQuery = Query.Builder.create().addFieldClause(ComputeState.FIELD_NAME_RESOURCE_POOL_LINK, rpReturnState.documentSelfLink).build();
    aggregationTaskState.query = taskQuery;
    aggregationTaskState.metricNames = Collections.singleton(MockStatsAdapter.KEY_1);
    aggregationTaskState.taskInfo = TaskState.createDirect();
    postServiceSynchronously(StatsAggregationTaskService.FACTORY_LINK, aggregationTaskState, StatsAggregationTaskState.class);
    // 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, StatsAggregationTaskService.FACTORY_LINK));
        if (res.documentLinks.size() == 0) {
            return true;
        }
        return false;
    });
    this.host.waitFor("Error waiting for stats", () -> {
        ServiceDocumentQueryResult aggrRes = this.metricHost.getFactoryState(UriUtils.buildUri(this.metricHost, ResourceMetricsService.FACTORY_LINK));
        if (aggrRes.documentCount == (this.numResources + numberOfRawMetrics)) {
            return true;
        }
        return false;
    });
    // Get a Count on this.host() for Aggregate metrics
    Long resourceHostAggregateMetricCount = this.getDocumentCount(this.host, ResourceMetricsService.FACTORY_LINK);
    // Get a Count on this.metricHost() for Aggregate Metrics
    Long metricHostAggregateMetricCount = this.getDocumentCount(this.metricHost, ResourceMetricsService.FACTORY_LINK);
    // Count should be 0 on this.host()
    assertEquals(0, resourceHostAggregateMetricCount.intValue());
    // Count should be something on this.metricHost()
    assertEquals((this.numResources + numberOfRawMetrics), metricHostAggregateMetricCount.intValue());
}
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) StatsCollectionTaskState(com.vmware.photon.controller.model.tasks.monitoring.StatsCollectionTaskService.StatsCollectionTaskState) StatsAggregationTaskState(com.vmware.photon.controller.model.tasks.monitoring.StatsAggregationTaskService.StatsAggregationTaskState) BaseModelTest(com.vmware.photon.controller.model.helpers.BaseModelTest) Test(org.junit.Test)

Example 47 with ComputeState

use of com.vmware.photon.controller.model.resources.ComputeService.ComputeState 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 48 with ComputeState

use of com.vmware.photon.controller.model.resources.ComputeService.ComputeState 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 49 with ComputeState

use of com.vmware.photon.controller.model.resources.ComputeService.ComputeState in project photon-model by vmware.

the class AWSPowerService method updateComputeState.

private void updateComputeState(ComputePowerRequest pr, DefaultAdapterContext c) {
    ComputeState state = new ComputeState();
    state.powerState = pr.powerState;
    if (OFF.equals(pr.powerState)) {
        // clear IP address in case of power-off
        state.address = "";
    }
    Operation.createPatch(pr.resourceReference).setBody(state).setCompletion((o, e) -> {
        if (e != null) {
            c.taskManager.patchTaskToFailure(e);
            return;
        }
        c.taskManager.finishTask();
    }).sendWith(this);
}
Also used : ComputePowerRequest(com.vmware.photon.controller.model.adapterapi.ComputePowerRequest) OperationContext(com.vmware.xenon.common.OperationContext) StopInstancesResult(com.amazonaws.services.ec2.model.StopInstancesResult) StatelessService(com.vmware.xenon.common.StatelessService) Operation(com.vmware.xenon.common.Operation) StartInstancesResult(com.amazonaws.services.ec2.model.StartInstancesResult) BaseAdapterStage(com.vmware.photon.controller.model.adapters.util.BaseAdapterContext.BaseAdapterStage) StopInstancesRequest(com.amazonaws.services.ec2.model.StopInstancesRequest) StartInstancesRequest(com.amazonaws.services.ec2.model.StartInstancesRequest) DefaultAdapterContext(com.vmware.photon.controller.model.adapters.util.BaseAdapterContext.DefaultAdapterContext) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) OFF(com.vmware.photon.controller.model.resources.ComputeService.PowerState.OFF) AsyncHandler(com.amazonaws.handlers.AsyncHandler) AWSClientManagerFactory(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManagerFactory) AWSClientManager(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManager) AmazonEC2AsyncClient(com.amazonaws.services.ec2.AmazonEC2AsyncClient) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState)

Example 50 with ComputeState

use of com.vmware.photon.controller.model.resources.ComputeService.ComputeState in project photon-model by vmware.

the class AWSRebootService method updateComputeState.

private void updateComputeState(ResourceOperationRequest ror, DefaultAdapterContext c) {
    ComputeState state = new ComputeState();
    state.powerState = ComputeService.PowerState.ON;
    Operation.createPatch(ror.resourceReference).setBody(state).setCompletion((o, e) -> {
        if (e != null) {
            c.taskManager.patchTaskToFailure(e);
            return;
        }
        c.taskManager.finishTask();
    }).sendWith(this);
}
Also used : Service(com.vmware.xenon.common.Service) ResourceOperationSpec(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationSpecService.ResourceOperationSpec) RebootInstancesRequest(com.amazonaws.services.ec2.model.RebootInstancesRequest) RebootInstancesResult(com.amazonaws.services.ec2.model.RebootInstancesResult) ResourceOperationUtils.handleAdapterResourceOperationRegistration(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationUtils.handleAdapterResourceOperationRegistration) StatelessService(com.vmware.xenon.common.StatelessService) Operation(com.vmware.xenon.common.Operation) ServiceDocument(com.vmware.xenon.common.ServiceDocument) BaseAdapterStage(com.vmware.photon.controller.model.adapters.util.BaseAdapterContext.BaseAdapterStage) ResourceOperationRequest(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest) TargetCriteria(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationUtils.TargetCriteria) ResourceOperation(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperation) DefaultAdapterContext(com.vmware.photon.controller.model.adapters.util.BaseAdapterContext.DefaultAdapterContext) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) AsyncHandler(com.amazonaws.handlers.AsyncHandler) ComputeService(com.vmware.photon.controller.model.resources.ComputeService) EndpointType(com.vmware.photon.controller.model.constants.PhotonModelConstants.EndpointType) FactoryService(com.vmware.xenon.common.FactoryService) AWSClientManagerFactory(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManagerFactory) ResourceType(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationSpecService.ResourceType) AWSClientManager(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManager) AmazonEC2AsyncClient(com.amazonaws.services.ec2.AmazonEC2AsyncClient) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState)

Aggregations

ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)214 Operation (com.vmware.xenon.common.Operation)93 ArrayList (java.util.ArrayList)63 QueryTask (com.vmware.xenon.services.common.QueryTask)58 ComputeDescription (com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription)57 HashMap (java.util.HashMap)54 List (java.util.List)51 Map (java.util.Map)50 Utils (com.vmware.xenon.common.Utils)45 Test (org.junit.Test)45 UriUtils (com.vmware.xenon.common.UriUtils)44 URI (java.net.URI)42 Collectors (java.util.stream.Collectors)42 ComputeService (com.vmware.photon.controller.model.resources.ComputeService)40 DiskState (com.vmware.photon.controller.model.resources.DiskService.DiskState)40 EndpointState (com.vmware.photon.controller.model.resources.EndpointService.EndpointState)40 StatelessService (com.vmware.xenon.common.StatelessService)40 Query (com.vmware.xenon.services.common.QueryTask.Query)40 AuthCredentialsServiceState (com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState)38 ComputeType (com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription.ComputeType)36