Search in sources :

Example 76 with QueryTask

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);
    });
}
Also used : ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) QueryTask(com.vmware.xenon.services.common.QueryTask) Query(com.vmware.xenon.services.common.QueryTask.Query) HashMap(java.util.HashMap)

Example 77 with QueryTask

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);
    });
}
Also used : QueryTask(com.vmware.xenon.services.common.QueryTask) AWSNetworkUtils.mapVPCToNetworkState(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSNetworkUtils.mapVPCToNetworkState) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState)

Example 78 with QueryTask

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);
    }
}
Also used : BUCKET_OWNER_NAME(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.BUCKET_OWNER_NAME) BucketTaggingConfiguration(com.amazonaws.services.s3.model.BucketTaggingConfiguration) AWSResourceType(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWSResourceType) QueryTask(com.vmware.xenon.services.common.QueryTask) Utils(com.vmware.xenon.common.Utils) STORAGE_TYPE_S3(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.STORAGE_TYPE_S3) Map(java.util.Map) AWSClientManager(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManager) StatelessService(com.vmware.xenon.common.StatelessService) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) AmazonS3Client(com.amazonaws.services.s3.AmazonS3Client) AdapterUtils.getDeletionState(com.vmware.photon.controller.model.adapters.util.AdapterUtils.getDeletionState) List(java.util.List) TagService(com.vmware.photon.controller.model.resources.TagService) DeferredResult(com.vmware.xenon.common.DeferredResult) TAG_KEY_TYPE(com.vmware.photon.controller.model.constants.PhotonModelConstants.TAG_KEY_TYPE) QueryOption(com.vmware.xenon.services.common.QueryTask.QuerySpecification.QueryOption) DiskService(com.vmware.photon.controller.model.resources.DiskService) OperationContext(com.vmware.xenon.common.OperationContext) HashMap(java.util.HashMap) PhotonModelUtils(com.vmware.photon.controller.model.resources.util.PhotonModelUtils) TagsUtil(com.vmware.photon.controller.model.adapters.util.TagsUtil) ArrayList(java.util.ArrayList) Regions(com.amazonaws.regions.Regions) HashSet(java.util.HashSet) AWSConstants(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants) TagState(com.vmware.photon.controller.model.resources.TagService.TagState) ServiceStateCollectionUpdateRequest(com.vmware.xenon.common.ServiceStateCollectionUpdateRequest) AuthCredentialsService(com.vmware.xenon.services.common.AuthCredentialsService) Query(com.vmware.xenon.services.common.QueryTask.Query) AdapterUtils.createPostOperation(com.vmware.photon.controller.model.adapters.util.AdapterUtils.createPostOperation) BiConsumer(java.util.function.BiConsumer) EnumerationAction(com.vmware.photon.controller.model.adapterapi.EnumerationAction) AwsClientType(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AwsClientType) ExecutorService(java.util.concurrent.ExecutorService) AWSConstants.getQueryResultLimit(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.getQueryResultLimit) AdapterUtils(com.vmware.photon.controller.model.adapters.util.AdapterUtils) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception) ResourceState(com.vmware.photon.controller.model.resources.ResourceState) Operation(com.vmware.xenon.common.Operation) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) Bucket(com.amazonaws.services.s3.model.Bucket) TimeUnit(java.util.concurrent.TimeUnit) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) ComputeEnumerateAdapterRequest(com.vmware.photon.controller.model.adapters.util.ComputeEnumerateAdapterRequest) AWSClientManagerFactory(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManagerFactory) Collections(java.util.Collections) AWSUriPaths(com.vmware.photon.controller.model.adapters.awsadapter.AWSUriPaths) OperationJoin(com.vmware.xenon.common.OperationJoin) PhotonModelUriUtils.createInventoryUri(com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri) QueryTask(com.vmware.xenon.services.common.QueryTask) Query(com.vmware.xenon.services.common.QueryTask.Query) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState)

Example 79 with QueryTask

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);
    });
}
Also used : QueryTask(com.vmware.xenon.services.common.QueryTask) Query(com.vmware.xenon.services.common.QueryTask.Query)

Example 80 with QueryTask

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;
}
Also used : ResourceMetrics(com.vmware.photon.controller.model.monitoring.ResourceMetricsService.ResourceMetrics) QueryTask(com.vmware.xenon.services.common.QueryTask)

Aggregations

QueryTask (com.vmware.xenon.services.common.QueryTask)147 Query (com.vmware.xenon.services.common.QueryTask.Query)62 Operation (com.vmware.xenon.common.Operation)61 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)39 ArrayList (java.util.ArrayList)26 QueryUtils (com.vmware.photon.controller.model.query.QueryUtils)20 List (java.util.List)20 ResourceMetrics (com.vmware.photon.controller.model.monitoring.ResourceMetricsService.ResourceMetrics)19 UriUtils (com.vmware.xenon.common.UriUtils)18 Utils (com.vmware.xenon.common.Utils)17 HashSet (java.util.HashSet)16 HashMap (java.util.HashMap)14 QueryOption (com.vmware.xenon.services.common.QueryTask.QuerySpecification.QueryOption)12 TimeUnit (java.util.concurrent.TimeUnit)12 ComputeEnumerateResourceRequest (com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest)11 AdapterUtils (com.vmware.photon.controller.model.adapters.util.AdapterUtils)11 ComputeDescription (com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription)11 ServiceTypeCluster (com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster)11 URI (java.net.URI)11 Set (java.util.Set)11