Search in sources :

Example 16 with TaskManager

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

the class VSphereAdapterResizeComputeService method handlePatch.

@Override
public void handlePatch(Operation op) {
    if (!op.hasBody()) {
        op.fail(new IllegalArgumentException("body is required"));
        return;
    }
    ResourceOperationRequest request = op.getBody(ResourceOperationRequest.class);
    op.complete();
    TaskManager mgr = new TaskManager(this, request.taskReference, request.resourceLink());
    if (request.payload != null && !request.payload.isEmpty()) {
        logInfo("Handle operation %s for compute %s.", request.operation, request.resourceLink());
        final String newCpu = request.payload.get(COMPUTE_CPU_COUNT);
        final String newMemory = request.payload.get(COMPUTE_MEMORY_IN_MBYTES);
        final String rebootVMFlag = request.payload.get(REBOOT_VM_FLAG);
        if ((newCpu == null || newCpu.isEmpty()) && (newMemory == null || newMemory.isEmpty())) {
            // payload doesn't contain anything so patch task to FINISHED and return
            mgr.patchTask(TaskState.TaskStage.FINISHED);
            return;
        }
        VSphereVMContext vmContext = new VSphereVMContext(this, request, op);
        vmContext.pool = VSphereIOThreadPoolAllocator.getPool(this);
        VSphereVMContext.populateVMContextThen(this, vmContext, ctx -> {
            int newCpuCount;
            long newMemoryInMBytes;
            boolean rebootVM;
            try {
                if (newCpu != null && !newCpu.isEmpty()) {
                    newCpuCount = Integer.parseInt(newCpu);
                } else {
                    newCpuCount = toIntExact(ctx.child.description.cpuCount);
                }
                if (newMemory != null && !newMemory.isEmpty()) {
                    newMemoryInMBytes = Long.parseLong(newMemory);
                } else {
                    newMemoryInMBytes = ctx.child.description.totalMemoryBytes / MEGA_BYTES_TO_BYTES_CONSTANT;
                }
            } catch (NumberFormatException numberEx) {
                // NumberFormatException for all the parseXXX
                logWarning(String.format("Request payload has values for CPU: %s or memory: %s in incorrect types " + numberEx.getMessage(), newCpu, newMemory));
                ctx.failWithMessage(String.format("Request payload has values for CPU: %s or memory: %s in incorrect types ", newCpu, newMemory), numberEx);
                return;
            }
            if (rebootVMFlag != null && !rebootVMFlag.isEmpty()) {
                rebootVM = Boolean.valueOf(rebootVMFlag);
            } else {
                rebootVM = true;
            }
            if (request.isMockRequest) {
                patchComputeAndCompleteRequest(ctx, newCpuCount, newMemoryInMBytes);
                return;
            }
            handleResourceOperationRequest(ctx, newCpuCount, newMemoryInMBytes, rebootVM);
        });
    } else {
        mgr.patchTaskToFailure(new IllegalArgumentException("Request payload is required for resize compute operation"));
    }
}
Also used : TaskManager(com.vmware.photon.controller.model.adapters.util.TaskManager) ResourceOperationRequest(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest)

Example 17 with TaskManager

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

the class VSphereComputeDiskManagementService method handlePatch.

@Override
public void handlePatch(Operation op) {
    if (!op.hasBody()) {
        op.fail(new IllegalArgumentException("body is required"));
        return;
    }
    ResourceOperationRequest request = op.getBody(ResourceOperationRequest.class);
    try {
        validateInputRequest(request);
    } catch (Exception e) {
        op.fail(e);
        return;
    }
    op.setStatusCode(Operation.STATUS_CODE_CREATED);
    op.complete();
    TaskManager taskManager = new TaskManager(this, request.taskReference, request.resourceLink());
    logInfo("Handle operation %s for compute %s.", request.operation, request.resourceLink());
    VSphereVMDiskContext.populateVMDiskContextThen(this, createInitialContext(taskManager, request, op), ctx -> {
        if (request.operation.equals(ResourceOperation.ATTACH_DISK.operation)) {
            handleAttachDisk(ctx);
        } else if (request.operation.equals(ResourceOperation.DETACH_DISK.operation)) {
            handleDetachDisk(ctx);
        } else {
            IllegalArgumentException exception = new IllegalArgumentException(String.format("Unknown Operation %s for a disk", request.operation));
            ctx.fail(exception);
        }
    });
}
Also used : TaskManager(com.vmware.photon.controller.model.adapters.util.TaskManager) ResourceOperationRequest(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest)

Example 18 with TaskManager

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

the class VSphereIncrementalEnumerationService method handleStart.

@Override
public void handleStart(Operation op) {
    if (!op.hasBody()) {
        op.fail(new IllegalArgumentException("body is required"));
        return;
    }
    VSphereIncrementalEnumerationRequest enumerationRequest = op.getBody(VSphereIncrementalEnumerationRequest.class);
    ComputeEnumerateResourceRequest request = enumerationRequest.request;
    validate(request);
    op.setStatusCode(Operation.STATUS_CODE_CREATED);
    op.complete();
    TaskManager mgr = new TaskManager(this, request.taskReference, request.resourceLink());
    URI parentUri = ComputeService.ComputeStateWithDescription.buildUri(PhotonModelUriUtils.createInventoryUri(getHost(), request.resourceReference));
    Operation.createGet(parentUri).setCompletion(o -> {
        thenWithParentState(request, o.getBody(ComputeService.ComputeStateWithDescription.class), mgr);
    }, 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) TaskManager(com.vmware.photon.controller.model.adapters.util.TaskManager) ComputeEnumerateResourceRequest(com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest) URI(java.net.URI) ComputeService(com.vmware.photon.controller.model.resources.ComputeService)

Example 19 with TaskManager

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

the class VSphereAdapterImageEnumerationService method handlePatch.

@Override
public void handlePatch(Operation op) {
    if (!op.hasBody()) {
        op.fail(new IllegalArgumentException("body is required"));
        return;
    }
    op.setStatusCode(Operation.STATUS_CODE_CREATED);
    op.complete();
    ImageEnumerateRequest request = op.getBody(ImageEnumerateRequest.class);
    validate(request);
    TaskManager mgr = new TaskManager(this, request.taskReference, request.resourceLink());
    if (request.isMockRequest) {
        // just finish the mock request
        mgr.patchTask(TaskStage.FINISHED);
        return;
    }
    Operation.createGet(PhotonModelUriUtils.createInventoryUri(getHost(), request.resourceReference)).setCompletion(o -> thenWithEndpointState(request, o.getBody(EndpointState.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) EndpointState(com.vmware.photon.controller.model.resources.EndpointService.EndpointState) TaskManager(com.vmware.photon.controller.model.adapters.util.TaskManager) ImageEnumerateRequest(com.vmware.photon.controller.model.adapterapi.ImageEnumerateRequest)

Example 20 with TaskManager

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

the class VSphereAdapterResourceEnumerationService method handlePatch.

/**
 * On every enumeration request, we check if there's an existing stateless service and delete it.
 * This is needed to ensure no two enumeration processes are happening for same endpoint.
 * After deleting the service, we create a new stateless service by issuing POST on the factory of
 * stateless service and passing the last path segment of endpoint document selflink.
 *
 * @param op the enumeration operation request
 */
@Override
public void handlePatch(Operation op) {
    if (!op.hasBody()) {
        op.fail(new IllegalArgumentException("body is required"));
        return;
    }
    ComputeEnumerateResourceRequest request = op.getBody(ComputeEnumerateResourceRequest.class);
    validate(request);
    op.setStatusCode(Operation.STATUS_CODE_CREATED);
    op.complete();
    TaskManager mgr = new TaskManager(this, request.taskReference, request.resourceLink());
    if (request.isMockRequest) {
        // just finish the mock request
        mgr.patchTask(TaskState.TaskStage.FINISHED);
        return;
    }
    String eventBasedEnumerationServiceURI = UriUtils.buildUriPath(VSphereIncrementalEnumerationService.FACTORY_LINK, UriUtils.getLastPathSegment(request.endpointLink));
    URI uri = buildUri(this.getHost(), eventBasedEnumerationServiceURI);
    VSphereIncrementalEnumerationService.VSphereIncrementalEnumerationRequest enumerationRequest = new VSphereIncrementalEnumerationService.VSphereIncrementalEnumerationRequest();
    enumerationRequest.documentSelfLink = UriUtils.getLastPathSegment(eventBasedEnumerationServiceURI);
    enumerationRequest.request = request;
    // if yes, patch it. If no, create new one via post to factory.
    if (EnumerationAction.START.equals(request.enumerationAction)) {
        Operation.createGet(uri).setCompletion((o, e) -> {
            if (null != e || o.getStatusCode() == Operation.STATUS_CODE_NOT_FOUND) {
                // service not found, create a new one
                createNewEnumerationService(enumerationRequest, mgr);
            } else {
                logInfo("Patching incremental service for incremental enumeration for %s", enumerationRequest.documentSelfLink);
                Operation patchRequest = Operation.createPatch(uri).setBody(enumerationRequest).setCompletion((operation, err) -> {
                    if (err != null) {
                        logSevere("Unable to send enumeration request to enumeration" + " service for endpoint %s", enumerationRequest.documentSelfLink);
                        mgr.patchTaskToFailure(err);
                    }
                });
                patchRequest.sendWith(this);
            }
        }).sendWith(this);
    } else {
        // Stop any existing enumeration process when REFRESH is received.
        logFine("Deleting the incremental enumeration service.");
        Operation deleteRequest = Operation.createDelete(uri).setCompletion((o, e) -> {
            if (null != e) {
                logWarning("Delete of enumeration service failed for endpoint " + enumerationRequest.documentSelfLink + " Message: %s", e.getMessage());
            }
            // The delete service call will take care of stopping any running enumeration.
            if (request.enumerationAction == EnumerationAction.STOP) {
                logInfo("Successfully stopped enumeration for endpoint " + request.endpointLink);
                mgr.patchTask(TaskState.TaskStage.FINISHED);
            } else {
                logInfo("Creating the enumeration service for endpoint %s", request.endpointLink);
                createNewEnumerationService(enumerationRequest, mgr);
            }
        });
        deleteRequest.sendWith(this);
    }
}
Also used : VSphereIncrementalEnumerationRequest(com.vmware.photon.controller.model.adapters.vsphere.VSphereIncrementalEnumerationService.VSphereIncrementalEnumerationRequest) Utils(com.vmware.xenon.common.Utils) ComputeEnumerateResourceRequest(com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest) StatelessService(com.vmware.xenon.common.StatelessService) Operation(com.vmware.xenon.common.Operation) UriUtils(com.vmware.xenon.common.UriUtils) UriUtils.buildFactoryUri(com.vmware.xenon.common.UriUtils.buildFactoryUri) UriUtils.buildUri(com.vmware.xenon.common.UriUtils.buildUri) EnumerationAction(com.vmware.photon.controller.model.adapterapi.EnumerationAction) TaskManager(com.vmware.photon.controller.model.adapters.util.TaskManager) TaskState(com.vmware.xenon.common.TaskState) URI(java.net.URI) TaskManager(com.vmware.photon.controller.model.adapters.util.TaskManager) ComputeEnumerateResourceRequest(com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest) Operation(com.vmware.xenon.common.Operation) URI(java.net.URI) VSphereIncrementalEnumerationRequest(com.vmware.photon.controller.model.adapters.vsphere.VSphereIncrementalEnumerationService.VSphereIncrementalEnumerationRequest) VSphereIncrementalEnumerationRequest(com.vmware.photon.controller.model.adapters.vsphere.VSphereIncrementalEnumerationService.VSphereIncrementalEnumerationRequest)

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