use of com.vmware.photon.controller.model.tasks.ScheduledTaskService.ScheduledTaskState in project photon-model by vmware.
the class EndpointAllocationTaskService method doTriggerEnumeration.
private void doTriggerEnumeration(EndpointAllocationTaskState currentState, SubStage next, URI adapterManagementReference) {
EndpointState endpoint = currentState.endpointState;
long intervalMicros = currentState.enumerationRequest.refreshIntervalMicros != null ? currentState.enumerationRequest.refreshIntervalMicros : DEFAULT_SCHEDULED_TASK_INTERVAL_MICROS;
// Use endpoint documentSelfLink's last part as convention, so that we are able to stop
// enumeration during endpoint update.
String id = UriUtils.getLastPathSegment(endpoint.documentSelfLink);
ResourceEnumerationTaskState enumTaskState = new ResourceEnumerationTaskState();
enumTaskState.parentComputeLink = endpoint.computeLink;
enumTaskState.endpointLink = endpoint.documentSelfLink;
enumTaskState.resourcePoolLink = currentState.enumerationRequest.resourcePoolLink;
enumTaskState.adapterManagementReference = adapterManagementReference;
enumTaskState.tenantLinks = endpoint.tenantLinks;
enumTaskState.options = EnumSet.of(TaskOption.SELF_DELETE_ON_COMPLETION);
if (currentState.options.contains(TaskOption.IS_MOCK)) {
enumTaskState.options.add(TaskOption.IS_MOCK);
}
if (currentState.options.contains(TaskOption.PRESERVE_MISSING_RESOUCES)) {
enumTaskState.options.add(TaskOption.PRESERVE_MISSING_RESOUCES);
}
ScheduledTaskState scheduledTaskState = new ScheduledTaskState();
scheduledTaskState.documentSelfLink = id;
scheduledTaskState.factoryLink = ResourceEnumerationTaskService.FACTORY_LINK;
scheduledTaskState.initialStateJson = Utils.toJson(enumTaskState);
scheduledTaskState.intervalMicros = intervalMicros;
scheduledTaskState.delayMicros = currentState.enumerationRequest.delayMicros;
scheduledTaskState.noDelayOnInitialExecution = Boolean.TRUE;
scheduledTaskState.tenantLinks = endpoint.tenantLinks;
scheduledTaskState.customProperties = new HashMap<>();
scheduledTaskState.customProperties.put(ENDPOINT_LINK_PROP_NAME, endpoint.documentSelfLink);
Operation.createPost(this, ScheduledTaskService.FACTORY_LINK).addPragmaDirective(Operation.PRAGMA_DIRECTIVE_FORCE_INDEX_UPDATE).setBody(scheduledTaskState).setCompletion((o, e) -> {
if (e != null) {
logWarning(() -> String.format("Error triggering Enumeration task, reason:" + " %s", e.getMessage()));
}
sendSelfPatch(createUpdateSubStageTask(next));
}).sendWith(this);
}
use of com.vmware.photon.controller.model.tasks.ScheduledTaskService.ScheduledTaskState 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);
}
use of com.vmware.photon.controller.model.tasks.ScheduledTaskService.ScheduledTaskState in project photon-model by vmware.
the class TestVSphereStatsTask method testCollectStats.
@Test
public void testCollectStats() throws Throwable {
// Create a resource pool where the VM will be housed
this.resourcePool = createResourcePool();
this.auth = createAuth();
this.computeHostDescription = createComputeDescription();
this.computeHost = createComputeHost();
// collect data
doRefresh();
// collect stats for a few instances
StatsCollectionTaskService.StatsCollectionTaskState statCollectionState = new StatsCollectionTaskService.StatsCollectionTaskState();
statCollectionState.resourcePoolLink = this.resourcePool.documentSelfLink;
ScheduledTaskState statsCollectionTaskState = new ScheduledTaskState();
statsCollectionTaskState.factoryLink = StatsCollectionTaskService.FACTORY_LINK;
statsCollectionTaskState.initialStateJson = Utils.toJson(statCollectionState);
statsCollectionTaskState.intervalMicros = TimeUnit.MINUTES.toMicros(1);
TestUtils.doPost(this.host, statsCollectionTaskState, ScheduledTaskState.class, UriUtils.buildUri(this.host, ScheduledTaskService.FACTORY_LINK));
if (isMock()) {
return;
}
// wait up to 5 minutes as first call to perfManager can take a while.
host.setTimeoutSeconds(5 * 60);
host.waitFor("No stats inserted", () -> {
ServiceDocumentQueryResult state = host.getFactoryState(UriUtils.buildFactoryUri(TestVSphereStatsTask.this.host, ResourceMetricsService.class));
return !state.documentLinks.isEmpty();
});
}
use of com.vmware.photon.controller.model.tasks.ScheduledTaskService.ScheduledTaskState in project photon-model by vmware.
the class StatsCollectionTaskServiceTest method testCustomStatsAdapter.
@Test
public void testCustomStatsAdapter() throws Throwable {
ResourcePoolState rpState = new ResourcePoolState();
rpState.name = UUID.randomUUID().toString();
ResourcePoolState rpReturnState = postServiceSynchronously(ResourcePoolService.FACTORY_LINK, rpState, ResourcePoolState.class);
ComputeDescription desc = new ComputeDescription();
desc.name = rpState.name;
desc.statsAdapterReferences = Collections.singleton(UriUtils.buildUri(this.host, CustomStatsAdapter.SELF_LINK));
ComputeDescription descReturnState = postServiceSynchronously(ComputeDescriptionService.FACTORY_LINK, desc, ComputeDescription.class);
ComputeState computeState = new ComputeState();
computeState.name = rpState.name;
computeState.descriptionLink = descReturnState.documentSelfLink;
computeState.resourcePoolLink = rpReturnState.documentSelfLink;
List<String> computeLinks = new ArrayList<>();
for (int i = 0; i < this.numResources; i++) {
ComputeState res = postServiceSynchronously(ComputeService.FACTORY_LINK, computeState, ComputeState.class);
computeLinks.add(res.documentSelfLink);
}
// create a stats collection scheduler task
StatsCollectionTaskState statCollectionState = new StatsCollectionTaskState();
statCollectionState.resourcePoolLink = rpReturnState.documentSelfLink;
statCollectionState.statsAdapterReference = UriUtils.buildUri(this.host, CustomStatsAdapter.SELF_LINK);
statCollectionState.options = EnumSet.of(TaskOption.SELF_DELETE_ON_COMPLETION);
ScheduledTaskState statsCollectionTaskState = new ScheduledTaskState();
statsCollectionTaskState.factoryLink = StatsCollectionTaskService.FACTORY_LINK;
statsCollectionTaskState.initialStateJson = Utils.toJson(statCollectionState);
statsCollectionTaskState.intervalMicros = TimeUnit.MILLISECONDS.toMicros(500);
statsCollectionTaskState = postServiceSynchronously(ScheduledTaskService.FACTORY_LINK, statsCollectionTaskState, ScheduledTaskState.class);
ServiceDocumentQueryResult res = this.host.getFactoryState(UriUtils.buildExpandLinksQueryUri(UriUtils.buildUri(this.host, ScheduledTaskService.FACTORY_LINK)));
assertTrue(res.documents.size() > 0);
// get stats from resources
for (int i = 0; i < computeLinks.size(); i++) {
String statsUriPath = UriUtils.buildUriPath(computeLinks.get(i), ServiceHost.SERVICE_URI_SUFFIX_STATS);
this.host.waitFor("Error waiting for stats", () -> {
ServiceStats resStats = getServiceSynchronously(statsUriPath, ServiceStats.class);
boolean returnStatus = false;
// was populated in the in memory stats
for (ServiceStat stat : resStats.entries.values()) {
if (stat.name.startsWith(UriUtils.getLastPathSegment(CustomStatsAdapter.SELF_LINK))) {
returnStatus = true;
break;
}
}
return returnStatus;
});
}
// clean up
deleteServiceSynchronously(statsCollectionTaskState.documentSelfLink);
}
use of com.vmware.photon.controller.model.tasks.ScheduledTaskService.ScheduledTaskState in project photon-model by vmware.
the class StatsCollectionTaskServiceTest method testStatsCollection.
private void testStatsCollection(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 metric host
// 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<>(this.numResources);
for (int i = 0; i < this.numResources; i++) {
ComputeState res = postServiceSynchronously(ComputeService.FACTORY_LINK, computeState, ComputeState.class);
computeLinks.add(res.documentSelfLink);
}
// create a resource pool including all the created computes
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 a stats collection scheduler task
StatsCollectionTaskState statCollectionState = new StatsCollectionTaskState();
statCollectionState.resourcePoolLink = rpReturnState.documentSelfLink;
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.SECONDS.toMicros(2);
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);
// the last successful collection time should be populated as an in memory stat.
for (int i = 0; i < this.numResources; i++) {
String statsUriPath = UriUtils.buildUriPath(computeLinks.get(i), ServiceHost.SERVICE_URI_SUFFIX_STATS);
this.host.waitFor("Error waiting for in memory stats", () -> {
ServiceStats resStats = getServiceSynchronously(statsUriPath, ServiceStats.class);
boolean returnStatus = false;
for (ServiceStat stat : resStats.entries.values()) {
if (stat.latestValue > 0) {
returnStatus = true;
break;
}
}
return returnStatus;
});
}
host.log(Level.INFO, "Successfully verified that all the last collection time is available in memory.");
// persisted at a per metric level along with the last collection run time
for (String computeLink : computeLinks) {
ResourceMetrics metric = getResourceMetrics(verificationHost, computeLink, MockStatsAdapter.KEY_1);
assertNotNull("The resource metric for" + MockStatsAdapter.KEY_1 + " should not be null ", metric);
assertEquals(metric.entries.size(), 1);
assertEquals(metric.customProperties.get("prop1"), "val1");
ResourceMetrics metric2 = getResourceMetrics(verificationHost, computeLink, MockStatsAdapter.KEY_2);
assertNotNull("The resource metric for" + MockStatsAdapter.KEY_2 + "should not be null ", metric2);
assertEquals(metric2.entries.size(), 1);
String lastSuccessfulRunMetricKey = UriUtils.getLastPathSegment(MockStatsAdapter.SELF_LINK) + StatsUtil.SEPARATOR + PhotonModelConstants.LAST_SUCCESSFUL_STATS_COLLECTION_TIME;
ResourceMetrics metricLastRun = getResourceMetrics(verificationHost, computeLink, lastSuccessfulRunMetricKey);
assertNotNull("The resource metric for" + lastSuccessfulRunMetricKey + " should not be null ", metricLastRun);
}
host.log(Level.INFO, "Successfully verified that the required resource metrics are persisted in the resource metrics table");
// Verify sorted order of the metrics versions by timestamp
for (String computeLink : computeLinks) {
// get all versions
QueryTask qt = QueryTask.Builder.createDirectTask().addOption(QueryOption.EXPAND_CONTENT).addOption(QueryOption.SORT).orderAscending(ServiceDocument.FIELD_NAME_SELF_LINK, TypeName.STRING).setQuery(Query.Builder.create().addKindFieldClause(ResourceMetrics.class).addFieldClause(ServiceDocument.FIELD_NAME_SELF_LINK, UriUtils.buildUriPath(ResourceMetricsService.FACTORY_LINK, UriUtils.getLastPathSegment(computeLink)), MatchType.PREFIX).addRangeClause(QuerySpecification.buildCompositeFieldName(ResourceMetrics.FIELD_NAME_ENTRIES, MockStatsAdapter.KEY_1), NumericRange.createDoubleRange(Double.MIN_VALUE, Double.MAX_VALUE, true, true)).build()).build();
verificationHost.createQueryTaskService(qt, false, true, qt, null);
ResourceMetrics prevMetric = null;
for (String documentLink : qt.results.documentLinks) {
ResourceMetrics metric = Utils.fromJson(qt.results.documents.get(documentLink), ResourceMetrics.class);
if (prevMetric == null) {
prevMetric = metric;
continue;
}
assertTrue(prevMetric.timestampMicrosUtc < metric.timestampMicrosUtc);
}
}
// verify that the aggregation 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;
});
if (testOnCluster) {
this.cleanUpMetricHost(metricHost);
}
}
Aggregations