Search in sources :

Example 6 with TaskManager

use of com.vmware.photon.controller.model.adapters.util.TaskManager in project photon-model by vmware.

the class VSphereIncrementalEnumerationService method garbageCollectUntouchedComputeResources.

private void garbageCollectUntouchedComputeResources(ComputeEnumerateResourceRequest request, Set<String> untouchedResources, TaskManager mgr) {
    if (untouchedResources.isEmpty()) {
        mgr.patchTask(TaskStage.FINISHED);
        return;
    }
    if (!request.preserveMissing) {
        // delete dependent resources without waiting for response
        for (String resourceLink : untouchedResources) {
            Operation.createDelete(PhotonModelUriUtils.createInventoryUri(getHost(), resourceLink)).sendWith(this);
        }
        mgr.patchTask(TaskStage.FINISHED);
        return;
    }
    List<Operation> deleteOps = new ArrayList<>();
    for (String resourceLink : untouchedResources) {
        if (resourceLink.startsWith(ComputeService.FACTORY_LINK)) {
            ResourceCleanRequest patch = new ResourceCleanRequest();
            patch.resourceLink = resourceLink;
            deleteOps.add(Operation.createPatch(this, VSphereUriPaths.RESOURCE_CLEANER).setBody(patch));
        } else {
            deleteOps.add(Operation.createDelete(PhotonModelUriUtils.createInventoryUri(getHost(), resourceLink)));
        }
    }
    OperationJoin.create(deleteOps).setCompletion((os, es) -> mgr.patchTask(TaskStage.FINISHED)).sendWith(this);
}
Also used : Arrays(java.util.Arrays) ComputeEnumerateResourceRequest(com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest) TagCache(com.vmware.photon.controller.model.adapters.vsphere.tagging.TagCache) QueryTask(com.vmware.xenon.services.common.QueryTask) ServiceDocument(com.vmware.xenon.common.ServiceDocument) ResourceGroupState(com.vmware.photon.controller.model.resources.ResourceGroupService.ResourceGroupState) UpdateSet(com.vmware.vim25.UpdateSet) PropertyFilterSpec(com.vmware.vim25.PropertyFilterSpec) Utils(com.vmware.xenon.common.Utils) VsphereEnumerationHelper.convertOnlyResultToDocument(com.vmware.photon.controller.model.adapters.vsphere.VsphereEnumerationHelper.convertOnlyResultToDocument) Map(java.util.Map) Connection(com.vmware.photon.controller.model.adapters.vsphere.util.connection.Connection) URI(java.net.URI) VapiConnection(com.vmware.photon.controller.model.adapters.vsphere.vapi.VapiConnection) SubnetService(com.vmware.photon.controller.model.resources.SubnetService) StatelessService(com.vmware.xenon.common.StatelessService) TaskManager(com.vmware.photon.controller.model.adapters.util.TaskManager) DatacenterLister(com.vmware.photon.controller.model.adapters.vsphere.util.finders.DatacenterLister) Set(java.util.Set) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) Occurance(com.vmware.xenon.services.common.QueryTask.Query.Occurance) Collectors(java.util.stream.Collectors) List(java.util.List) StorageDescriptionService(com.vmware.photon.controller.model.resources.StorageDescriptionService) DeferredResult(com.vmware.xenon.common.DeferredResult) ComputeService(com.vmware.photon.controller.model.resources.ComputeService) VsphereEnumerationHelper.withTaskResults(com.vmware.photon.controller.model.adapters.vsphere.VsphereEnumerationHelper.withTaskResults) AboutInfo(com.vmware.vim25.AboutInfo) DiskService(com.vmware.photon.controller.model.resources.DiskService) MoRefKeyedMap(com.vmware.photon.controller.model.adapters.vsphere.util.MoRefKeyedMap) ComputeProperties(com.vmware.photon.controller.model.ComputeProperties) ResourceCleanRequest(com.vmware.photon.controller.model.adapters.vsphere.VsphereResourceCleanerService.ResourceCleanRequest) PhotonModelUriUtils(com.vmware.photon.controller.model.util.PhotonModelUriUtils) ObjectUpdateKind(com.vmware.vim25.ObjectUpdateKind) HashMap(java.util.HashMap) RuntimeFaultFaultMsg(com.vmware.vim25.RuntimeFaultFaultMsg) RpcException(com.vmware.photon.controller.model.adapters.vsphere.vapi.RpcException) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) HashSet(java.util.HashSet) EnumerationAction(com.vmware.photon.controller.model.adapterapi.EnumerationAction) TYPE_SERVER_DISK(com.vmware.photon.controller.model.adapters.vsphere.util.VimNames.TYPE_SERVER_DISK) ResourceState(com.vmware.photon.controller.model.resources.ResourceState) Element(com.vmware.photon.controller.model.adapters.vsphere.util.finders.Element) Operation(com.vmware.xenon.common.Operation) ObjectUpdateIterator(com.vmware.photon.controller.model.adapters.vsphere.EnumerationClient.ObjectUpdateIterator) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) IOException(java.io.IOException) PropertyFilterUpdate(com.vmware.vim25.PropertyFilterUpdate) VimNames(com.vmware.photon.controller.model.adapters.vsphere.util.VimNames) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) TaskStage(com.vmware.xenon.common.TaskState.TaskStage) NetworkService(com.vmware.photon.controller.model.resources.NetworkService) ConcurrentSkipListSet(java.util.concurrent.ConcurrentSkipListSet) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) ObjectUpdate(com.vmware.vim25.ObjectUpdate) OperationJoin(com.vmware.xenon.common.OperationJoin) ArrayList(java.util.ArrayList) Operation(com.vmware.xenon.common.Operation) ResourceCleanRequest(com.vmware.photon.controller.model.adapters.vsphere.VsphereResourceCleanerService.ResourceCleanRequest)

Example 7 with TaskManager

use of com.vmware.photon.controller.model.adapters.util.TaskManager in project photon-model by vmware.

the class VSphereIncrementalEnumerationService method handlePatch.

@Override
public void handlePatch(Operation patch) {
    // complete the patch immediately.
    patch.complete();
    logInfo("Received PATCH for incremental enumeration!");
    VSphereIncrementalEnumerationRequest enumerationRequest = patch.getBody(VSphereIncrementalEnumerationRequest.class);
    ComputeEnumerateResourceRequest request = enumerationRequest.request;
    URI parentUri = ComputeService.ComputeStateWithDescription.buildUri(PhotonModelUriUtils.createInventoryUri(getHost(), request.resourceReference));
    logInfo("Creating task manager!");
    TaskManager mgr = new TaskManager(this, request.taskReference, request.resourceLink());
    logInfo(" Requesting GET on compute state with description!.");
    Operation.createGet(parentUri).setCompletion(o -> {
        logInfo("Submitting job to threadpool!");
        VsphereEnumerationHelper.submitWorkToVSpherePool(this, () -> {
            logInfo("Incremental enumeration job started for endpoint %s", enumerationRequest.request.endpointLink);
            ComputeStateWithDescription computeStateWithDesc = o.getBody(ComputeStateWithDescription.class);
            VapiConnection vapiConnection = VapiConnection.createFromVimConnection(this.connection);
            logInfo("Establishing VAPI connection for endpoint %s", enumerationRequest.request.endpointLink);
            try {
                vapiConnection.login();
            } catch (IOException | RpcException rpce) {
                logWarning(() -> String.format("Cannot login into vAPI endpoint: %s", Utils.toString(rpce)));
                mgr.patchTaskToFailure(rpce);
                // self delete service so that full enumeration kicks in next invocation.
                selfDeleteService();
                return;
            }
            try {
                // Get instanceUuid of the vCenter
                AboutInfo vCenter = this.connection.getServiceContent().getAbout();
                for (CollectorDetails collectorDetails : this.collectors) {
                    logInfo("Retrieving  resources incremental data for data center: %s", collectorDetails.datacenter);
                    EnumerationProgress enumerationProgress = new EnumerationProgress(new HashSet<>(), request, computeStateWithDesc, vapiConnection, collectorDetails.datacenter, vCenter.getInstanceUuid());
                    EnumerationClient client = new EnumerationClient(this.connection, computeStateWithDesc, VimUtils.convertStringToMoRef(collectorDetails.datacenter));
                    List<ObjectUpdate> resourcesUpdates = collectResourcesData(collectorDetails);
                    List<ObjectUpdate> vmUpdates = collectVMData(collectorDetails);
                    logInfo("Received resources updates for datacenter: %s : %s", collectorDetails.datacenter, resourcesUpdates.size());
                    logInfo("Received vm updates for datacenter: %s : %s", collectorDetails.datacenter, vmUpdates.size());
                    logInfo("Resources Updates: %s", Utils.toJson(resourcesUpdates));
                    logInfo("VM Updates: %s", Utils.toJson(vmUpdates));
                    if (!resourcesUpdates.isEmpty()) {
                        SegregatedOverlays segregatedOverlays = segregateObjectUpdates(enumerationProgress, resourcesUpdates);
                        this.logInfo("Processing incremental changes for folders for datacenter [%s]!", collectorDetails.datacenter);
                        VsphereFolderEnumerationHelper.handleFolderChanges(this, segregatedOverlays.folders, enumerationProgress, client);
                        logInfo("Processing incremental changes for networks for datacenter [%s]!", collectorDetails.datacenter);
                        VSphereNetworkEnumerationHelper.handleNetworkChanges(this, segregatedOverlays.networks, enumerationProgress, client);
                        logInfo("Processing incremental changes for Datastores for datacenter [%s]!", collectorDetails.datacenter);
                        VsphereDatastoreEnumerationHelper.handleDatastoreChanges(this, segregatedOverlays.datastores, enumerationProgress);
                        logInfo("Processing incremental changes for compute resource for datacenter [%s]!", collectorDetails.datacenter);
                        VsphereComputeResourceEnumerationHelper.handleComputeResourceChanges(this, segregatedOverlays.clusters, enumerationProgress, client);
                        logInfo("Processing incremental changes for host system for datacenter [%s]!", collectorDetails.datacenter);
                        VSphereHostSystemEnumerationHelper.handleHostSystemChanges(this, segregatedOverlays.hosts, enumerationProgress, client);
                        logInfo("Processing incremental changes for resource pool for datacenter [%s]!", collectorDetails.datacenter);
                        VSphereResourcePoolEnumerationHelper.handleResourcePoolChanges(this, segregatedOverlays.resourcePools, enumerationProgress, client);
                    }
                    if (!vmUpdates.isEmpty()) {
                        logInfo("Processing incremental changes for virtual machines for datacenter [%s]!", collectorDetails.datacenter);
                        VSphereVirtualMachineEnumerationHelper.handleVMChanges(this, vmUpdates, enumerationProgress, client);
                    }
                    // sync storage profiles
                    logInfo("Syncing storage profiles for datacenter [%s]!", collectorDetails.datacenter);
                    VsphereStoragePolicyEnumerationHelper.syncStorageProfiles(this, client, enumerationProgress);
                }
                mgr.patchTask(TaskStage.FINISHED);
            } catch (Exception exception) {
                String msg = "Error processing PropertyCollector results during incremental retrieval";
                logWarning(() -> msg + ": " + exception.toString());
                mgr.patchTaskToFailure(exception);
                // self delete service so that full enumeration kicks in next invocation.
                // TODO: This is not complete. We need to enable owner selection on this service.
                selfDeleteService();
                return;
            } finally {
                vapiConnection.close();
            }
        });
    }, mgr).sendWith(this);
}
Also used : Arrays(java.util.Arrays) ComputeEnumerateResourceRequest(com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest) TagCache(com.vmware.photon.controller.model.adapters.vsphere.tagging.TagCache) QueryTask(com.vmware.xenon.services.common.QueryTask) ServiceDocument(com.vmware.xenon.common.ServiceDocument) ResourceGroupState(com.vmware.photon.controller.model.resources.ResourceGroupService.ResourceGroupState) UpdateSet(com.vmware.vim25.UpdateSet) PropertyFilterSpec(com.vmware.vim25.PropertyFilterSpec) Utils(com.vmware.xenon.common.Utils) VsphereEnumerationHelper.convertOnlyResultToDocument(com.vmware.photon.controller.model.adapters.vsphere.VsphereEnumerationHelper.convertOnlyResultToDocument) Map(java.util.Map) Connection(com.vmware.photon.controller.model.adapters.vsphere.util.connection.Connection) URI(java.net.URI) VapiConnection(com.vmware.photon.controller.model.adapters.vsphere.vapi.VapiConnection) SubnetService(com.vmware.photon.controller.model.resources.SubnetService) StatelessService(com.vmware.xenon.common.StatelessService) TaskManager(com.vmware.photon.controller.model.adapters.util.TaskManager) DatacenterLister(com.vmware.photon.controller.model.adapters.vsphere.util.finders.DatacenterLister) Set(java.util.Set) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) Occurance(com.vmware.xenon.services.common.QueryTask.Query.Occurance) Collectors(java.util.stream.Collectors) List(java.util.List) StorageDescriptionService(com.vmware.photon.controller.model.resources.StorageDescriptionService) DeferredResult(com.vmware.xenon.common.DeferredResult) ComputeService(com.vmware.photon.controller.model.resources.ComputeService) VsphereEnumerationHelper.withTaskResults(com.vmware.photon.controller.model.adapters.vsphere.VsphereEnumerationHelper.withTaskResults) AboutInfo(com.vmware.vim25.AboutInfo) DiskService(com.vmware.photon.controller.model.resources.DiskService) MoRefKeyedMap(com.vmware.photon.controller.model.adapters.vsphere.util.MoRefKeyedMap) ComputeProperties(com.vmware.photon.controller.model.ComputeProperties) ResourceCleanRequest(com.vmware.photon.controller.model.adapters.vsphere.VsphereResourceCleanerService.ResourceCleanRequest) PhotonModelUriUtils(com.vmware.photon.controller.model.util.PhotonModelUriUtils) ObjectUpdateKind(com.vmware.vim25.ObjectUpdateKind) HashMap(java.util.HashMap) RuntimeFaultFaultMsg(com.vmware.vim25.RuntimeFaultFaultMsg) RpcException(com.vmware.photon.controller.model.adapters.vsphere.vapi.RpcException) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) HashSet(java.util.HashSet) EnumerationAction(com.vmware.photon.controller.model.adapterapi.EnumerationAction) TYPE_SERVER_DISK(com.vmware.photon.controller.model.adapters.vsphere.util.VimNames.TYPE_SERVER_DISK) ResourceState(com.vmware.photon.controller.model.resources.ResourceState) Element(com.vmware.photon.controller.model.adapters.vsphere.util.finders.Element) Operation(com.vmware.xenon.common.Operation) ObjectUpdateIterator(com.vmware.photon.controller.model.adapters.vsphere.EnumerationClient.ObjectUpdateIterator) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) IOException(java.io.IOException) PropertyFilterUpdate(com.vmware.vim25.PropertyFilterUpdate) VimNames(com.vmware.photon.controller.model.adapters.vsphere.util.VimNames) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) TaskStage(com.vmware.xenon.common.TaskState.TaskStage) NetworkService(com.vmware.photon.controller.model.resources.NetworkService) ConcurrentSkipListSet(java.util.concurrent.ConcurrentSkipListSet) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) ObjectUpdate(com.vmware.vim25.ObjectUpdate) OperationJoin(com.vmware.xenon.common.OperationJoin) VapiConnection(com.vmware.photon.controller.model.adapters.vsphere.vapi.VapiConnection) ComputeEnumerateResourceRequest(com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) AboutInfo(com.vmware.vim25.AboutInfo) URI(java.net.URI) RpcException(com.vmware.photon.controller.model.adapters.vsphere.vapi.RpcException) IOException(java.io.IOException) TaskManager(com.vmware.photon.controller.model.adapters.util.TaskManager) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 8 with TaskManager

use of com.vmware.photon.controller.model.adapters.util.TaskManager in project photon-model by vmware.

the class VSphereAdapterImageEnumerationService method thenWithEndpointState.

private void thenWithEndpointState(ImageEnumerateRequest request, EndpointState endpoint, TaskManager mgr) {
    URI parentUri = ComputeStateWithDescription.buildUri(UriUtils.buildUri(getHost(), endpoint.computeLink));
    Operation.createGet(PhotonModelUriUtils.createInventoryUri(getHost(), parentUri)).setCompletion(o -> thenWithParentState(request, o.getBody(ComputeStateWithDescription.class), mgr), mgr).sendWith(this);
}
Also used : PhotonModelUriUtils(com.vmware.photon.controller.model.util.PhotonModelUriUtils) QueryTask(com.vmware.xenon.services.common.QueryTask) RuntimeFaultFaultMsg(com.vmware.vim25.RuntimeFaultFaultMsg) VapiClient(com.vmware.photon.controller.model.adapters.vsphere.vapi.VapiClient) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) RpcException(com.vmware.photon.controller.model.adapters.vsphere.vapi.RpcException) HashSet(java.util.HashSet) PropertyFilterSpec(com.vmware.vim25.PropertyFilterSpec) Query(com.vmware.xenon.services.common.QueryTask.Query) EnumerationAction(com.vmware.photon.controller.model.adapterapi.EnumerationAction) Connection(com.vmware.photon.controller.model.adapters.vsphere.util.connection.Connection) ImageService(com.vmware.photon.controller.model.resources.ImageService) URI(java.net.URI) EndpointState(com.vmware.photon.controller.model.resources.EndpointService.EndpointState) VapiConnection(com.vmware.photon.controller.model.adapters.vsphere.vapi.VapiConnection) Element(com.vmware.photon.controller.model.adapters.vsphere.util.finders.Element) ObjectContent(com.vmware.vim25.ObjectContent) StatelessService(com.vmware.xenon.common.StatelessService) Operation(com.vmware.xenon.common.Operation) TaskManager(com.vmware.photon.controller.model.adapters.util.TaskManager) DatacenterLister(com.vmware.photon.controller.model.adapters.vsphere.util.finders.DatacenterLister) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) Set(java.util.Set) IOException(java.io.IOException) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) TaskStage(com.vmware.xenon.common.TaskState.TaskStage) List(java.util.List) InvalidPropertyFaultMsg(com.vmware.vim25.InvalidPropertyFaultMsg) ConcurrentSkipListSet(java.util.concurrent.ConcurrentSkipListSet) LibraryClient(com.vmware.photon.controller.model.adapters.vsphere.vapi.LibraryClient) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) CompletionHandler(com.vmware.xenon.common.Operation.CompletionHandler) Phaser(java.util.concurrent.Phaser) DeferredResult(com.vmware.xenon.common.DeferredResult) UriUtils(com.vmware.xenon.common.UriUtils) ImageState(com.vmware.photon.controller.model.resources.ImageService.ImageState) ImageEnumerateRequest(com.vmware.photon.controller.model.adapterapi.ImageEnumerateRequest) Builder(com.vmware.xenon.services.common.QueryTask.Query.Builder) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) URI(java.net.URI)

Example 9 with TaskManager

use of com.vmware.photon.controller.model.adapters.util.TaskManager in project photon-model by vmware.

the class DvsNetworkService method handlePatch.

@Override
public void handlePatch(Operation op) {
    if (!op.hasBody()) {
        op.fail(new IllegalArgumentException("body is required"));
        return;
    }
    SubnetInstanceRequest req = op.getBody(SubnetInstanceRequest.class);
    if (req.isMockRequest) {
        new TaskManager(this, req.taskReference, req.resourceLink()).patchTask(TaskStage.FINISHED);
        op.complete();
    }
    if (req.requestType == InstanceRequestType.CREATE) {
        op.complete();
        new CreatePortgroupFlow(this, op, req).provisionAsync();
        return;
    }
    if (req.requestType == InstanceRequestType.DELETE) {
        op.complete();
        new DeletePortgroupFlow(this, op, req).provisionAsync();
        return;
    }
    op.fail(new IllegalArgumentException("bad request type"));
}
Also used : TaskManager(com.vmware.photon.controller.model.adapters.util.TaskManager) SubnetInstanceRequest(com.vmware.photon.controller.model.adapterapi.SubnetInstanceRequest)

Example 10 with TaskManager

use of com.vmware.photon.controller.model.adapters.util.TaskManager in project photon-model by vmware.

the class AWSStatsService method handlePatch.

@Override
public void handlePatch(Operation op) {
    if (!op.hasBody()) {
        op.fail(new IllegalArgumentException("body is required"));
        return;
    }
    ComputeStatsRequest statsRequest = op.getBody(ComputeStatsRequest.class);
    op.complete();
    TaskManager taskManager = new TaskManager(this, statsRequest.taskReference, statsRequest.resourceLink());
    if (statsRequest.isMockRequest) {
        // patch status to parent task
        taskManager.finishTask();
        return;
    }
    AWSStatsDataHolder statsData = new AWSStatsDataHolder();
    statsData.statsRequest = statsRequest;
    statsData.taskManager = taskManager;
    getVMDescription(statsData);
}
Also used : ComputeStatsRequest(com.vmware.photon.controller.model.adapterapi.ComputeStatsRequest) TaskManager(com.vmware.photon.controller.model.adapters.util.TaskManager)

Aggregations

TaskManager (com.vmware.photon.controller.model.adapters.util.TaskManager)21 StatelessService (com.vmware.xenon.common.StatelessService)9 DeferredResult (com.vmware.xenon.common.DeferredResult)8 List (java.util.List)8 ComputeStatsRequest (com.vmware.photon.controller.model.adapterapi.ComputeStatsRequest)7 Operation (com.vmware.xenon.common.Operation)7 URI (java.net.URI)7 EnumerationAction (com.vmware.photon.controller.model.adapterapi.EnumerationAction)6 ArrayList (java.util.ArrayList)6 Connection (com.vmware.photon.controller.model.adapters.vsphere.util.connection.Connection)4 QueryUtils (com.vmware.photon.controller.model.query.QueryUtils)4 ComputeStateWithDescription (com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription)4 PhotonModelUriUtils (com.vmware.photon.controller.model.util.PhotonModelUriUtils)4 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)4 TaskStage (com.vmware.xenon.common.TaskState.TaskStage)4 QueryTask (com.vmware.xenon.services.common.QueryTask)4 ComputeProperties (com.vmware.photon.controller.model.ComputeProperties)3 DatacenterLister (com.vmware.photon.controller.model.adapters.vsphere.util.finders.DatacenterLister)3 Element (com.vmware.photon.controller.model.adapters.vsphere.util.finders.Element)3 RpcException (com.vmware.photon.controller.model.adapters.vsphere.vapi.RpcException)3