use of com.vmware.xenon.services.common.QueryTask in project photon-model by vmware.
the class AWSEnumerationAndCreationAdapterService method loadLocalResources.
/**
* Query the local data store and retrieve all the the compute states that exist filtered by the
* instanceIds that are received in the enumeration data from AWS.
*/
private static void loadLocalResources(AWSEnumerationAndCreationAdapterService service, EnumerationCreationContext context, Collection<String> remoteIds, Consumer<Map<String, ComputeState>> successHandler, Consumer<Map<String, ComputeState>> failureHandler) {
// instance Ids
if (remoteIds == null || remoteIds.isEmpty()) {
failureHandler.accept(null);
return;
}
Query.Builder qBuilder = Query.Builder.create().addKindFieldClause(ComputeState.class).addInClause(ResourceState.FIELD_NAME_ID, remoteIds);
addScopeCriteria(qBuilder, context);
QueryTask queryTask = QueryTask.Builder.createDirectTask().setQuery(qBuilder.build()).addOption(QueryOption.EXPAND_CONTENT).addOption(QueryOption.TOP_RESULTS).setResultLimit(getQueryResultLimit()).build();
queryTask.tenantLinks = context.parentCompute.tenantLinks;
service.logFine(() -> String.format("Created query for resources: " + remoteIds));
QueryUtils.startInventoryQueryTask(service, queryTask).whenComplete((qrt, e) -> {
if (e != null) {
service.logSevere(() -> String.format("Failure retrieving query results: %s", e.toString()));
signalErrorToEnumerationAdapter(e, context, service);
return;
}
service.logFine(() -> String.format("%d compute states found", qrt.results.documentCount));
Map<String, ComputeState> localInstances = new HashMap<>();
for (Object s : qrt.results.documents.values()) {
ComputeState localInstance = Utils.fromJson(s, ComputeState.class);
localInstances.put(localInstance.id, localInstance);
}
successHandler.accept(localInstances);
});
}
use of com.vmware.xenon.services.common.QueryTask in project photon-model by vmware.
the class AWSNetworkStateEnumerationAdapterService method getLocalNetworkStates.
/**
* Gets the VPC information from the local database to perform updates to existing network
* states.
*/
private void getLocalNetworkStates(AWSNetworkStateCreationContext context, AWSNetworkStateCreationStage next) {
if (context.vpcs.isEmpty()) {
handleNetworkStateChanges(context, next);
return;
}
QueryTask queryTask = createQueryToGetExistingNetworkStatesFilteredByDiscoveredVPCs(context.vpcs.keySet(), context.request.parentComputeLink, context.request.request.endpointLink, context.request.regionId, context.request.tenantLinks);
// create the query to find resources
QueryUtils.startInventoryQueryTask(this, queryTask).whenComplete((qrt, e) -> {
if (e != null) {
logSevere(() -> String.format("Failure retrieving query results: %s", e.toString()));
finishWithFailure(context, e);
return;
}
if (qrt.results.documents != null) {
for (Object s : qrt.results.documents.values()) {
NetworkState networkState = Utils.fromJson(s, NetworkState.class);
context.localNetworkStateMap.put(networkState.id, networkState);
}
}
logFine(() -> String.format("%d network states found.", qrt.results.documentCount));
handleNetworkStateChanges(context, next);
});
}
use of com.vmware.xenon.services.common.QueryTask in project photon-model by vmware.
the class AWSS3StorageEnumerationAdapterService method getLocalResources.
/**
* Query and get list of S3 buckets present locally in disk states in current context.
*/
private void getLocalResources(S3StorageEnumerationContext aws, S3StorageEnumerationSubStage next) {
// instance Ids. the filtering is performed on the selected resource pool.
if (aws.localResourcesNextPageLink == null) {
Query.Builder qBuilder = Query.Builder.create().addKindFieldClause(DiskState.class).addFieldClause(DiskState.FIELD_NAME_STORAGE_TYPE, STORAGE_TYPE_S3).addInClause(DiskState.FIELD_NAME_ID, aws.remoteBucketsByBucketName.keySet());
addScopeCriteria(qBuilder, aws);
QueryTask queryTask = QueryTask.Builder.createDirectTask().setQuery(qBuilder.build()).addOption(QueryOption.EXPAND_CONTENT).setResultLimit(getQueryResultLimit()).build();
queryTask.tenantLinks = aws.parentCompute.tenantLinks;
QueryUtils.startInventoryQueryTask(this, queryTask).whenComplete((qrt, e) -> {
if (e != null) {
this.logSevere(() -> String.format("Failure retrieving query" + " results: %s", e.toString()));
signalErrorToEnumerationAdapter(aws, e);
return;
}
qrt.results.documents.values().forEach(documentJson -> {
DiskState localDisk = Utils.fromJson(documentJson, DiskState.class);
aws.localDiskStatesByBucketName.put(localDisk.name, localDisk);
});
this.logFine(() -> String.format("%d S3 disk states found.", qrt.results.documentCount));
if (qrt.results.nextPageLink != null) {
this.logFine("Processing next page for local disk states.");
aws.localResourcesNextPageLink = qrt.results.nextPageLink;
handleReceivedEnumerationData(aws);
} else {
aws.subStage = next;
handleReceivedEnumerationData(aws);
}
});
} else {
Operation.createGet(createInventoryUri(this.getHost(), aws.localResourcesNextPageLink)).setReferer(this.getUri()).setCompletion((o, e) -> {
if (e != null) {
this.logSevere(() -> String.format("Failure retrieving query" + " results: %s", e.toString()));
signalErrorToEnumerationAdapter(aws, e);
return;
}
QueryTask qrt = o.getBody(QueryTask.class);
qrt.results.documents.values().forEach(documentJson -> {
DiskState localDisk = Utils.fromJson(documentJson, DiskState.class);
aws.localDiskStatesByBucketName.put(localDisk.name, localDisk);
});
this.logFine(() -> String.format("%d S3 disk states found.", qrt.results.documentCount));
if (qrt.results.nextPageLink != null) {
this.logFine("Processing next page for local disk states.");
aws.localResourcesNextPageLink = qrt.results.nextPageLink;
handleReceivedEnumerationData(aws);
} else {
aws.subStage = next;
handleReceivedEnumerationData(aws);
}
}).sendWith(this);
}
}
use of com.vmware.xenon.services.common.QueryTask in project photon-model by vmware.
the class AWSS3StorageEnumerationAdapterService method deleteDiskStates.
/**
* Deletes undiscovered resources.
* <p>
* The logic works by recording a timestamp when enumeration starts. This timestamp is used to
* lookup resources which haven't been touched as part of current enumeration cycle.
* <p>
* Finally, delete on a resource is invoked only if it meets two criteria:
* - Timestamp older than current enumeration cycle.
* - S3 bucket is not present on AWS.
* <p>
* The method paginates through list of resources for deletion.
*/
private void deleteDiskStates(S3StorageEnumerationContext aws, S3StorageEnumerationSubStage next) {
Query.Builder qBuilder = Query.Builder.create().addKindFieldClause(DiskState.class).addFieldClause(DiskState.FIELD_NAME_STORAGE_TYPE, STORAGE_TYPE_S3).addRangeClause(DiskState.FIELD_NAME_UPDATE_TIME_MICROS, QueryTask.NumericRange.createLessThanRange(aws.enumerationStartTimeInMicros));
addScopeCriteria(qBuilder, aws);
QueryTask q = QueryTask.Builder.createDirectTask().addOption(QueryOption.EXPAND_CONTENT).setQuery(qBuilder.build()).setResultLimit(getQueryResultLimit()).build();
q.tenantLinks = aws.parentCompute.tenantLinks;
q.documentExpirationTimeMicros = Utils.getNowMicrosUtc() + QueryUtils.TEN_MINUTES_IN_MICROS;
this.logFine(() -> "Querying disks for deletion");
QueryUtils.startInventoryQueryTask(this, q).whenComplete((queryTask, e) -> {
if (e != null) {
this.logWarning("Failure querying S3 disks for deletion for [endpoint=%s]", aws.request.original.endpointLink);
aws.subStage = next;
handleReceivedEnumerationData(aws);
return;
}
if (queryTask.results.nextPageLink == null) {
this.logFine(() -> "No disk states match for deletion");
aws.subStage = next;
handleReceivedEnumerationData(aws);
return;
}
aws.deletionNextPageLink = queryTask.results.nextPageLink;
processDeletionRequest(aws, next);
});
}
use of com.vmware.xenon.services.common.QueryTask in project photon-model by vmware.
the class LongRunEndToEndStatsAggregationTest method getResourceMetrics.
/**
* Method returns ResourceMetrics collected during stats collection and aggregation for a
* resource endpoint for the given resource metric key.
*/
private ResourceMetrics getResourceMetrics(String resourceLink, String metricKey) {
QueryTask qt = QueryTask.Builder.createDirectTask().addOption(QueryOption.TOP_RESULTS).addOption(QueryOption.INCLUDE_ALL_VERSIONS).setResultLimit(1).addOption(QueryOption.EXPAND_CONTENT).addOption(QueryOption.SORT).orderDescending(ServiceDocument.FIELD_NAME_SELF_LINK, ServiceDocumentDescription.TypeName.STRING).setQuery(QueryTask.Query.Builder.create().addKindFieldClause(ResourceMetrics.class).addFieldClause(ServiceDocument.FIELD_NAME_SELF_LINK, UriUtils.buildUriPath(ResourceMetricsService.FACTORY_LINK, UriUtils.getLastPathSegment(resourceLink)), QueryTask.QueryTerm.MatchType.PREFIX).addRangeClause(QuerySpecification.buildCompositeFieldName(ResourceMetrics.FIELD_NAME_ENTRIES, metricKey), QueryTask.NumericRange.createDoubleRange(0.0, Double.MAX_VALUE, true, true)).build()).build();
this.host.createQueryTaskService(qt, false, true, qt, null);
ResourceMetrics resourceMetric = null;
if (qt.results.documentLinks.size() > 0) {
String documentLink = qt.results.documentLinks.get(0);
resourceMetric = Utils.fromJson(qt.results.documents.get(documentLink), ResourceMetrics.class);
}
return resourceMetric;
}
Aggregations