Search in sources :

Example 61 with QueryTask

use of com.vmware.xenon.services.common.QueryTask in project photon-model by vmware.

the class VSphereIncrementalEnumerationService method collectAllEndpointResources.

private DeferredResult<Set<String>> collectAllEndpointResources(ComputeEnumerateResourceRequest req, String parentLink) {
    QueryTask.Query combinedClause = new QueryTask.Query().setOccurance(Occurance.MUST_OCCUR);
    // The next clauses are the boolean clauses that will be added to the
    // combinedClause. At the top, all further queries should have SHOULD_OCCUR
    QueryTask.Query resourceClause = QueryTask.Query.Builder.create().addFieldClause(ResourceState.FIELD_NAME_ENDPOINT_LINK, req.endpointLink).addFieldClause(ComputeService.ComputeState.FIELD_NAME_LIFECYCLE_STATE, ComputeService.LifecycleState.PROVISIONING.toString(), QueryTask.QueryTerm.MatchType.TERM, QueryTask.Query.Occurance.MUST_NOT_OCCUR).addFieldClause(ServiceDocument.FIELD_NAME_SELF_LINK, parentLink, QueryTask.Query.Occurance.MUST_NOT_OCCUR).addInClause(ServiceDocument.FIELD_NAME_KIND, Arrays.asList(Utils.buildKind(ComputeService.ComputeState.class), Utils.buildKind(NetworkService.NetworkState.class), Utils.buildKind(StorageDescriptionService.StorageDescription.class), Utils.buildKind(SubnetService.SubnetState.class))).build().setOccurance(Occurance.SHOULD_OCCUR);
    // The below two queries are added to get the Folders and Datacenters that are enumerated
    // They are persisted as ResourceGroupState documents, and there are other documents of the same
    // kind (which have a different lifecycle), we're filtering on the "__computeType" property
    // Adding these documents here will enable automatic deletion of "untouched" resources in
    // the further logic
    QueryTask.Query folderClause = QueryTask.Query.Builder.create().addCompositeFieldClause(ResourceState.FIELD_NAME_CUSTOM_PROPERTIES, CustomProperties.TYPE, VimNames.TYPE_FOLDER).addKindFieldClause(ResourceGroupState.class).build().setOccurance(Occurance.SHOULD_OCCUR);
    QueryTask.Query datacenterClause = QueryTask.Query.Builder.create().addCompositeFieldClause(ResourceState.FIELD_NAME_CUSTOM_PROPERTIES, CustomProperties.TYPE, VimNames.TYPE_DATACENTER).addKindFieldClause(ResourceGroupState.class).build().setOccurance(Occurance.SHOULD_OCCUR);
    // Add all the clauses to the combined clause
    // The query structure is now --> MUST(SHOULD A + SHOULD B + SHOULD C)
    // where A, B, C are independent queries
    combinedClause.addBooleanClause(resourceClause).addBooleanClause(folderClause).addBooleanClause(datacenterClause);
    QueryTask task = QueryTask.Builder.createDirectTask().setQuery(combinedClause).setResultLimit(QueryUtils.DEFAULT_RESULT_LIMIT).build();
    DeferredResult<Set<String>> res = new DeferredResult<>();
    Set<String> links = new ConcurrentSkipListSet<>();
    QueryUtils.startInventoryQueryTask(this, task).whenComplete((result, e) -> {
        if (e != null) {
            res.complete(new HashSet<>());
            return;
        }
        if (result.results.nextPageLink == null) {
            res.complete(links);
            return;
        }
        Operation.createGet(PhotonModelUriUtils.createInventoryUri(getHost(), result.results.nextPageLink)).setCompletion(makeCompletion(links, res)).sendWith(this);
    });
    return res;
}
Also used : UpdateSet(com.vmware.vim25.UpdateSet) Set(java.util.Set) HashSet(java.util.HashSet) ConcurrentSkipListSet(java.util.concurrent.ConcurrentSkipListSet) ConcurrentSkipListSet(java.util.concurrent.ConcurrentSkipListSet) ComputeService(com.vmware.photon.controller.model.resources.ComputeService) QueryTask(com.vmware.xenon.services.common.QueryTask) SubnetService(com.vmware.photon.controller.model.resources.SubnetService) StorageDescriptionService(com.vmware.photon.controller.model.resources.StorageDescriptionService) NetworkService(com.vmware.photon.controller.model.resources.NetworkService) DeferredResult(com.vmware.xenon.common.DeferredResult)

Example 62 with QueryTask

use of com.vmware.xenon.services.common.QueryTask in project photon-model by vmware.

the class VSphereIncrementalEnumerationService method updateServerDisks.

private void updateServerDisks(EnumerationProgress ctx, HostSystemOverlay hostSystemOverlay) {
    QueryTask queryTask = queryForServerDisks(ctx, hostSystemOverlay);
    withTaskResults(this, queryTask, serviceDocumentQueryResult -> {
        if (!serviceDocumentQueryResult.documentLinks.isEmpty()) {
            DiskService.DiskState oldDocument = convertOnlyResultToDocument(serviceDocumentQueryResult, DiskService.DiskState.class);
            updateDiskState(ctx, oldDocument, hostSystemOverlay);
        }
    });
}
Also used : QueryTask(com.vmware.xenon.services.common.QueryTask) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) DiskService(com.vmware.photon.controller.model.resources.DiskService)

Example 63 with QueryTask

use of com.vmware.xenon.services.common.QueryTask in project photon-model by vmware.

the class VSphereResourcePoolEnumerationHelper method handleResourcePoolChanges.

public static void handleResourcePoolChanges(VSphereIncrementalEnumerationService service, List<ResourcePoolOverlay> resourcePools, EnumerationProgress enumerationProgress, EnumerationClient client) {
    ComputeEnumerateResourceRequest request = enumerationProgress.getRequest();
    enumerationProgress.expectResourcePoolCount(resourcePools.size());
    for (ResourcePoolOverlay resourcePool : resourcePools) {
        // no need to collect the root resource pool
        if (ObjectUpdateKind.ENTER.equals(resourcePool.getObjectUpdateKind()) && VimNames.TYPE_RESOURCE_POOL.equals(resourcePool.getParent().getType())) {
            String ownerMoRefId = resourcePool.getOwner().getValue();
            QueryTask task = queryForRPOwner(ownerMoRefId, enumerationProgress);
            String selfLink = buildStableResourcePoolLink(resourcePool.getId(), request.endpointLink);
            withTaskResults(service, task, result -> {
                try {
                    if (!result.documentLinks.isEmpty()) {
                        ComputeState ownerDocument = convertOnlyResultToDocument(result, ComputeState.class);
                        createNewResourcePool(service, enumerationProgress, ownerDocument.name, selfLink, resourcePool, client);
                    } else {
                        // This happens for the resource pools within Host. The owner is a ComputeResource and
                        // is not currently enumerated in photon
                        createNewResourcePool(service, enumerationProgress, null, selfLink, resourcePool, client);
                    }
                } catch (Exception e) {
                    enumerationProgress.getResourcePoolTracker().track();
                }
            });
        } else {
            String rpSelfLink = buildStableResourcePoolLink(resourcePool.getId(), request.endpointLink);
            Operation.createGet(PhotonModelUriUtils.createInventoryUri(service.getHost(), rpSelfLink)).setCompletion((o, e) -> {
                try {
                    if (e == null) {
                        ComputeState oldState = o.getBody(ComputeState.class);
                        String existingOwnerName = getOwnerNameFromResourcePoolName(oldState.name);
                        if (ObjectUpdateKind.MODIFY.equals(resourcePool.getObjectUpdateKind())) {
                            updateResourcePool(service, enumerationProgress, existingOwnerName, oldState.documentSelfLink, resourcePool, false, client);
                        } else {
                            Operation.createDelete(PhotonModelUriUtils.createInventoryUri(service.getHost(), rpSelfLink)).setCompletion(trackResourcePool(enumerationProgress, resourcePool)).sendWith(service);
                        }
                    } else {
                        enumerationProgress.getResourcePoolTracker().track();
                    }
                } catch (Exception ex) {
                    enumerationProgress.getResourcePoolTracker().track();
                }
            }).sendWith(service);
        }
    }
    try {
        enumerationProgress.getResourcePoolTracker().await();
    } catch (InterruptedException e) {
        service.logSevere("Interrupted during incremental enumeration for resource pools!", e);
    }
}
Also used : PowerState(com.vmware.photon.controller.model.resources.ComputeService.PowerState) ComputeEnumerateResourceRequest(com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest) PhotonModelUriUtils(com.vmware.photon.controller.model.util.PhotonModelUriUtils) QueryTask(com.vmware.xenon.services.common.QueryTask) ObjectUpdateKind(com.vmware.vim25.ObjectUpdateKind) ComputeDescriptionService(com.vmware.photon.controller.model.resources.ComputeDescriptionService) RuntimeFaultFaultMsg(com.vmware.vim25.RuntimeFaultFaultMsg) ComputeType(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription.ComputeType) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) VsphereEnumerationHelper.convertOnlyResultToDocument(com.vmware.photon.controller.model.adapters.vsphere.VsphereEnumerationHelper.convertOnlyResultToDocument) ServiceNotFoundException(com.vmware.xenon.common.ServiceHost.ServiceNotFoundException) AdapterUtils(com.vmware.photon.controller.model.adapters.util.AdapterUtils) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) Operation(com.vmware.xenon.common.Operation) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) VimNames(com.vmware.photon.controller.model.adapters.vsphere.util.VimNames) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) List(java.util.List) UriUtils.buildUriPath(com.vmware.xenon.common.UriUtils.buildUriPath) InvalidPropertyFaultMsg(com.vmware.vim25.InvalidPropertyFaultMsg) CompletionHandler(com.vmware.xenon.common.Operation.CompletionHandler) UriUtils(com.vmware.xenon.common.UriUtils) ComputeService(com.vmware.photon.controller.model.resources.ComputeService) VsphereEnumerationHelper.withTaskResults(com.vmware.photon.controller.model.adapters.vsphere.VsphereEnumerationHelper.withTaskResults) Collections(java.util.Collections) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) QueryTask(com.vmware.xenon.services.common.QueryTask) ComputeEnumerateResourceRequest(com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest) ServiceNotFoundException(com.vmware.xenon.common.ServiceHost.ServiceNotFoundException)

Example 64 with QueryTask

use of com.vmware.xenon.services.common.QueryTask in project photon-model by vmware.

the class VSphereVMSnapshotEnumerationHelper method enumerateSnapshots.

static void enumerateSnapshots(VSphereIncrementalEnumerationService service, EnumerationProgress enumerationProgress, List<VmOverlay> vms) {
    if (CollectionUtils.isEmpty(vms)) {
        return;
    }
    vms.forEach(vm -> {
        // process only those VMs that have snapshots attached.
        if (vm.getRootSnapshotList() != null) {
            ComputeEnumerateResourceRequest request = enumerationProgress.getRequest();
            QueryTask task = VSphereVirtualMachineEnumerationHelper.queryForVm(enumerationProgress, request.resourceLink(), vm.getInstanceUuid(), null);
            VsphereEnumerationHelper.withTaskResults(service, task, result -> {
                ComputeState computeState = VsphereEnumerationHelper.convertOnlyResultToDocument(result, ComputeState.class);
                processSnapshots(service, enumerationProgress, vm, computeState.documentSelfLink);
            });
        }
    });
    enumerationProgress.getSnapshotTracker().arriveAndDeregister();
}
Also used : ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) QueryTask(com.vmware.xenon.services.common.QueryTask) ComputeEnumerateResourceRequest(com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest)

Example 65 with QueryTask

use of com.vmware.xenon.services.common.QueryTask in project photon-model by vmware.

the class VSphereVirtualMachineEnumerationHelper method handleVMChanges.

public static void handleVMChanges(VSphereIncrementalEnumerationService service, List<ObjectUpdate> resourcesUpdates, EnumerationProgress enumerationProgress, EnumerationClient client) {
    List<VmOverlay> vmOverlays = new ArrayList<>();
    for (ObjectUpdate objectUpdate : resourcesUpdates) {
        if (VimUtils.isVirtualMachine(objectUpdate.getObj())) {
            VmOverlay vm = new VmOverlay(objectUpdate);
            if (vm.getInstanceUuid() != null || !objectUpdate.getKind().equals(ObjectUpdateKind.ENTER)) {
                vmOverlays.add(vm);
            }
        }
    }
    for (VmOverlay vmOverlay : vmOverlays) {
        if (ObjectUpdateKind.ENTER == vmOverlay.getObjectUpdateKind()) {
            createNewVm(service, enumerationProgress, vmOverlay);
        } else {
            ComputeEnumerateResourceRequest request = enumerationProgress.getRequest();
            QueryTask task = queryForVm(enumerationProgress, request.resourceLink(), null, vmOverlay.getId());
            VsphereEnumerationHelper.withTaskResults(service, task, result -> {
                if (!result.documentLinks.isEmpty()) {
                    ComputeState oldDocument = VsphereEnumerationHelper.convertOnlyResultToDocument(result, ComputeState.class);
                    if (ObjectUpdateKind.MODIFY == vmOverlay.getObjectUpdateKind()) {
                        updateVm(service, oldDocument, enumerationProgress, vmOverlay, false);
                    } else {
                        deleteVM(enumerationProgress, vmOverlay, service, oldDocument);
                    }
                } else {
                    enumerationProgress.getVmTracker().arrive();
                }
            });
        }
    }
}
Also used : ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) QueryTask(com.vmware.xenon.services.common.QueryTask) ComputeEnumerateResourceRequest(com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest) ArrayList(java.util.ArrayList) ObjectUpdate(com.vmware.vim25.ObjectUpdate)

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