Search in sources :

Example 6 with ResourcePoolState

use of com.vmware.photon.controller.model.resources.ResourcePoolService.ResourcePoolState in project photon-model by vmware.

the class EndpointAllocationTaskService method createEndpoint.

private void createEndpoint(EndpointAllocationTaskState currentState) {
    List<String> createdDocumentLinks = new ArrayList<>();
    EndpointState es = currentState.endpointState;
    Map<String, String> endpointProperties = currentState.endpointState.endpointProperties;
    es.endpointProperties = null;
    if (es.documentSelfLink == null) {
        es.documentSelfLink = UriUtils.buildUriPath(EndpointService.FACTORY_LINK, this.getHost().nextUUID());
    }
    // merge endpoint and task tenant links
    if (es.tenantLinks == null || es.tenantLinks.isEmpty()) {
        es.tenantLinks = currentState.tenantLinks;
    } else if (currentState.tenantLinks != null) {
        currentState.tenantLinks.forEach(tl -> {
            if (!es.tenantLinks.contains(tl)) {
                es.tenantLinks.add(tl);
            }
        });
    }
    Operation endpointOp = Operation.createPost(this, EndpointService.FACTORY_LINK);
    ComputeDescription computeDescription = configureDescription(currentState, es);
    ComputeState computeState = configureCompute(currentState, es, endpointProperties);
    Operation cdOp = createComputeDescriptionOp(currentState, computeDescription.documentSelfLink);
    Operation compOp = createComputeStateOp(currentState, computeState.documentSelfLink);
    // pool link
    if (currentState.enumerationRequest != null && currentState.enumerationRequest.resourcePoolLink != null) {
        es.resourcePoolLink = currentState.enumerationRequest.resourcePoolLink;
        computeState.resourcePoolLink = es.resourcePoolLink;
    }
    OperationSequence sequence;
    if (es.authCredentialsLink == null) {
        AuthCredentialsServiceState auth = configureAuth(es);
        Operation authOp = Operation.createPost(createInventoryUri(this.getHost(), AuthCredentialsService.FACTORY_LINK)).setBody(auth);
        sequence = OperationSequence.create(authOp).setCompletion((ops, exs) -> {
            if (exs != null) {
                long firstKey = exs.keySet().iterator().next();
                exs.values().forEach(ex -> logWarning(() -> String.format("Error in " + "sequence to create auth credentials: %s", ex.getMessage())));
                sendFailurePatch(this, currentState, exs.get(firstKey));
                return;
            }
            Operation o = ops.get(authOp.getId());
            AuthCredentialsServiceState authState = o.getBody(AuthCredentialsServiceState.class);
            computeDescription.authCredentialsLink = authState.documentSelfLink;
            es.authCredentialsLink = authState.documentSelfLink;
            cdOp.setBody(computeDescription);
        }).next(cdOp);
    } else {
        cdOp.setBody(computeDescription);
        sequence = OperationSequence.create(cdOp);
    }
    sequence = sequence.setCompletion((ops, exs) -> {
        if (exs != null) {
            long firstKey = exs.keySet().iterator().next();
            exs.values().forEach(ex -> logWarning(() -> String.format("Error in " + "sequence to create compute description: %s", ex.getMessage())));
            sendFailurePatch(this, currentState, exs.get(firstKey));
            return;
        }
        Operation o = ops.get(cdOp.getId());
        ComputeDescription desc = o.getBody(ComputeDescription.class);
        if (!currentState.accountAlreadyExists) {
            createdDocumentLinks.add(desc.documentSelfLink);
        }
        computeState.descriptionLink = desc.documentSelfLink;
        es.computeDescriptionLink = desc.documentSelfLink;
    });
    // Don't create resource pool, if a resource pool link was passed.
    if (es.resourcePoolLink == null) {
        Operation poolOp = createResourcePoolOp(es);
        sequence = sequence.next(poolOp).setCompletion((ops, exs) -> {
            if (exs != null) {
                long firstKey = exs.keySet().iterator().next();
                exs.values().forEach(ex -> logWarning(() -> String.format("Error creating resource" + " pool: %s", ex.getMessage())));
                sendFailurePatch(this, currentState, exs.get(firstKey));
                return;
            }
            Operation o = ops.get(poolOp.getId());
            ResourcePoolState poolState = o.getBody(ResourcePoolState.class);
            createdDocumentLinks.add(poolState.documentSelfLink);
            es.resourcePoolLink = poolState.documentSelfLink;
            computeState.resourcePoolLink = es.resourcePoolLink;
            compOp.setBody(computeState);
        });
    } else {
        Operation getPoolOp = Operation.createGet(this, es.resourcePoolLink);
        sequence = sequence.next(getPoolOp).setCompletion((ops, exs) -> {
            if (exs != null) {
                long firstKey = exs.keySet().iterator().next();
                exs.values().forEach(ex -> logWarning(() -> String.format("Error retrieving resource" + " pool: %s", ex.getMessage())));
                sendFailurePatch(this, currentState, exs.get(firstKey));
                return;
            }
            Operation o = ops.get(getPoolOp.getId());
            ResourcePoolState poolState = o.getBody(ResourcePoolState.class);
            if (poolState.customProperties != null) {
                String endpointLink = poolState.customProperties.get(ENDPOINT_LINK_PROP_NAME);
                if (endpointLink != null && endpointLink.equals(es.documentSelfLink)) {
                    sendFailurePatch(this, currentState, new IllegalStateException("Passed resource pool is associated with a different endpoint."));
                    return;
                }
            }
            es.resourcePoolLink = poolState.documentSelfLink;
            computeState.resourcePoolLink = es.resourcePoolLink;
            compOp.setBody(computeState);
        });
    }
    sequence.next(compOp).setCompletion((ops, exs) -> {
        if (exs != null) {
            long firstKey = exs.keySet().iterator().next();
            exs.values().forEach(ex -> logWarning(() -> String.format("Error in " + "sequence to create compute state: %s", ex.getMessage())));
            sendFailurePatch(this, currentState, exs.get(firstKey));
            return;
        }
        Operation csOp = ops.get(compOp.getId());
        ComputeState c = csOp.getBody(ComputeState.class);
        if (!currentState.accountAlreadyExists) {
            createdDocumentLinks.add(c.documentSelfLink);
        }
        es.computeLink = c.documentSelfLink;
        endpointOp.setBody(es);
    }).next(endpointOp).setCompletion((ops, exs) -> {
        if (exs != null) {
            long firstKey = exs.keySet().iterator().next();
            exs.values().forEach(ex -> logWarning(() -> String.format("Error in " + "sequence to create endpoint state: %s", ex.getMessage())));
            sendFailurePatch(this, currentState, exs.get(firstKey));
            return;
        }
        Operation esOp = ops.get(endpointOp.getId());
        EndpointState endpoint = esOp.getBody(EndpointState.class);
        createdDocumentLinks.add(endpoint.documentSelfLink);
        // propagate the endpoint properties to the next stage
        endpoint.endpointProperties = endpointProperties;
        EndpointAllocationTaskState state = createUpdateSubStageTask(SubStage.INVOKE_ADAPTER);
        state.endpointState = endpoint;
        state.createdDocumentLinks = createdDocumentLinks;
        sendSelfPatch(state);
    }).sendWith(this);
}
Also used : AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) CertificateInfoServiceErrorResponse(com.vmware.photon.controller.model.support.CertificateInfoServiceErrorResponse) ServiceTypeCluster(com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster) ServiceDocument(com.vmware.xenon.common.ServiceDocument) Utils(com.vmware.xenon.common.Utils) EndpointService(com.vmware.photon.controller.model.resources.EndpointService) Map(java.util.Map) CUSTOM_PROP_ENDPOINT_LINK(com.vmware.photon.controller.model.constants.PhotonModelConstants.CUSTOM_PROP_ENDPOINT_LINK) ResourcePoolService(com.vmware.photon.controller.model.resources.ResourcePoolService) URI(java.net.URI) SINGLE_ASSIGNMENT(com.vmware.xenon.common.ServiceDocumentDescription.PropertyUsageOption.SINGLE_ASSIGNMENT) EnumSet(java.util.EnumSet) EndpointState(com.vmware.photon.controller.model.resources.EndpointService.EndpointState) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) ENDPOINT_LINK_PROP_NAME(com.vmware.photon.controller.model.ComputeProperties.ENDPOINT_LINK_PROP_NAME) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) ServiceHost(com.vmware.xenon.common.ServiceHost) PhotonModelAdaptersConfigAccessService(com.vmware.photon.controller.model.adapters.registry.PhotonModelAdaptersConfigAccessService) OPTIONAL(com.vmware.xenon.common.ServiceDocumentDescription.PropertyUsageOption.OPTIONAL) List(java.util.List) RequestType(com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.RequestType) TaskUtils.sendFailurePatch(com.vmware.photon.controller.model.tasks.TaskUtils.sendFailurePatch) CompletionHandler(com.vmware.xenon.common.Operation.CompletionHandler) SOURCE_TASK_LINK(com.vmware.photon.controller.model.constants.PhotonModelConstants.SOURCE_TASK_LINK) DeferredResult(com.vmware.xenon.common.DeferredResult) UriUtils(com.vmware.xenon.common.UriUtils) ComputeService(com.vmware.photon.controller.model.resources.ComputeService) TaskState(com.vmware.xenon.common.TaskState) TaskService(com.vmware.xenon.services.common.TaskService) AdapterTypePath(com.vmware.photon.controller.model.UriPaths.AdapterTypePath) STORE_ONLY(com.vmware.xenon.common.ServiceDocumentDescription.PropertyIndexingOption.STORE_ONLY) ResourcePoolState(com.vmware.photon.controller.model.resources.ResourcePoolService.ResourcePoolState) HashMap(java.util.HashMap) ComputeDescriptionService(com.vmware.photon.controller.model.resources.ComputeDescriptionService) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) EndpointConfigRequest(com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest) AuthCredentialsService(com.vmware.xenon.services.common.AuthCredentialsService) UriPaths(com.vmware.photon.controller.model.UriPaths) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) OperationSequence(com.vmware.xenon.common.OperationSequence) SERVICE_USE(com.vmware.xenon.common.ServiceDocumentDescription.PropertyUsageOption.SERVICE_USE) ResourceEnumerationTaskState(com.vmware.photon.controller.model.tasks.ResourceEnumerationTaskService.ResourceEnumerationTaskState) Operation(com.vmware.xenon.common.Operation) CertificateInfo(com.vmware.photon.controller.model.support.CertificateInfo) ScheduledTaskState(com.vmware.photon.controller.model.tasks.ScheduledTaskService.ScheduledTaskState) TaskStage(com.vmware.xenon.common.TaskState.TaskStage) TimeUnit(java.util.concurrent.TimeUnit) PhotonModelAdapterConfig(com.vmware.photon.controller.model.adapters.registry.PhotonModelAdaptersRegistryService.PhotonModelAdapterConfig) ClusterUtil(com.vmware.photon.controller.model.util.ClusterUtil) LocalizableValidationException(com.vmware.xenon.common.LocalizableValidationException) PropertyIndexingOption(com.vmware.xenon.common.ServiceDocumentDescription.PropertyIndexingOption) OperationJoin(com.vmware.xenon.common.OperationJoin) PhotonModelUriUtils.createInventoryUri(com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri) 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) OperationSequence(com.vmware.xenon.common.OperationSequence) ArrayList(java.util.ArrayList) Operation(com.vmware.xenon.common.Operation) EndpointState(com.vmware.photon.controller.model.resources.EndpointService.EndpointState) AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState)

Example 7 with ResourcePoolState

use of com.vmware.photon.controller.model.resources.ResourcePoolService.ResourcePoolState in project photon-model by vmware.

the class StatsAggregationTaskServiceTest method testStatsAggregation.

private void testStatsAggregation(boolean testOnCluster) throws Throwable {
    VerificationHost metricHost = null;
    if (testOnCluster) {
        metricHost = this.setupMetricHost();
    }
    // Use this.host if metricHost is null.
    VerificationHost verificationHost = (metricHost == null ? this.host : metricHost);
    // create a resource pool
    ResourcePoolState rpState = new ResourcePoolState();
    rpState.name = "testName";
    ResourcePoolState rpReturnState = postServiceSynchronously(ResourcePoolService.FACTORY_LINK, rpState, ResourcePoolState.class);
    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;
    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);
    }
    // kick off an aggregation task when stats are not populated
    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);
    this.host.waitFor("Error waiting for stats", () -> {
        ServiceDocumentQueryResult aggrRes = verificationHost.getFactoryState(UriUtils.buildUri(verificationHost, ResourceMetricsService.FACTORY_LINK));
        // Expect 0 stats because they're not collected yet
        if (aggrRes.documentCount == 0) {
            return true;
        }
        return false;
    });
    StatsCollectionTaskState collectionTaskState = new StatsCollectionTaskState();
    collectionTaskState.resourcePoolLink = rpReturnState.documentSelfLink;
    collectionTaskState.taskInfo = TaskState.createDirect();
    postServiceSynchronously(StatsCollectionTaskService.FACTORY_LINK, collectionTaskState, StatsCollectionTaskState.class);
    int numberOfRawMetrics = this.numResources * 4;
    // kick off an aggregation task
    aggregationTaskState = new StatsAggregationTaskState();
    aggregationTaskState.query = taskQuery;
    aggregationTaskState.metricNames = Collections.singleton(MockStatsAdapter.KEY_1);
    aggregationTaskState.taskInfo = TaskState.createDirect();
    postServiceSynchronously(StatsAggregationTaskService.FACTORY_LINK, aggregationTaskState, StatsAggregationTaskState.class);
    this.host.waitFor("Error waiting for stats", () -> {
        ServiceDocumentQueryResult aggrRes = verificationHost.getFactoryState(UriUtils.buildUri(verificationHost, ResourceMetricsService.FACTORY_LINK));
        if (aggrRes.documentCount == this.numResources + numberOfRawMetrics) {
            return true;
        }
        return false;
    });
    // 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;
    });
    if (testOnCluster) {
        this.cleanUpMetricHost(metricHost);
    }
}
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) VerificationHost(com.vmware.xenon.common.test.VerificationHost) ServiceDocumentQueryResult(com.vmware.xenon.common.ServiceDocumentQueryResult) StatsAggregationTaskState(com.vmware.photon.controller.model.tasks.monitoring.StatsAggregationTaskService.StatsAggregationTaskState) StatsCollectionTaskState(com.vmware.photon.controller.model.tasks.monitoring.StatsCollectionTaskService.StatsCollectionTaskState)

Example 8 with ResourcePoolState

use of com.vmware.photon.controller.model.resources.ResourcePoolService.ResourcePoolState 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 9 with ResourcePoolState

use of com.vmware.photon.controller.model.resources.ResourcePoolService.ResourcePoolState 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 10 with ResourcePoolState

use of com.vmware.photon.controller.model.resources.ResourcePoolService.ResourcePoolState 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)

Aggregations

ResourcePoolState (com.vmware.photon.controller.model.resources.ResourcePoolService.ResourcePoolState)38 AuthCredentialsServiceState (com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState)22 Test (org.junit.Test)12 BaseModelTest (com.vmware.photon.controller.model.helpers.BaseModelTest)10 ComputeDescription (com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription)10 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)10 Operation (com.vmware.xenon.common.Operation)10 ArrayList (java.util.ArrayList)9 StatsCollectionTaskState (com.vmware.photon.controller.model.tasks.monitoring.StatsCollectionTaskService.StatsCollectionTaskState)7 ServiceDocumentQueryResult (com.vmware.xenon.common.ServiceDocumentQueryResult)7 Before (org.junit.Before)7 ServiceStats (com.vmware.xenon.common.ServiceStats)6 ApplicationTokenCredentials (com.microsoft.azure.credentials.ApplicationTokenCredentials)5 SingleResourceStatsCollectionTaskState (com.vmware.photon.controller.model.tasks.monitoring.SingleResourceStatsCollectionTaskService.SingleResourceStatsCollectionTaskState)5 ComputeManagementClientImpl (com.microsoft.azure.management.compute.implementation.ComputeManagementClientImpl)4 SecurityGroupState (com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState)4 ProvisionSecurityGroupTaskState (com.vmware.photon.controller.model.tasks.ProvisionSecurityGroupTaskService.ProvisionSecurityGroupTaskState)4 ScheduledTaskState (com.vmware.photon.controller.model.tasks.ScheduledTaskService.ScheduledTaskState)4 Query (com.vmware.xenon.services.common.QueryTask.Query)4 NetworkManagementClientImpl (com.microsoft.azure.management.network.implementation.NetworkManagementClientImpl)3