From 3907a4a42b05724506288d64b561725e7bd6687b Mon Sep 17 00:00:00 2001 From: Daniel Augusto Veronezi Salvador Date: Tue, 13 Apr 2021 17:12:18 -0300 Subject: [PATCH 01/25] Improve logs --- .../main/java/com/cloud/vm/NicProfile.java | 12 +- .../cloud/vm/VirtualMachineManagerImpl.java | 355 +++++++----------- 2 files changed, 136 insertions(+), 231 deletions(-) diff --git a/api/src/main/java/com/cloud/vm/NicProfile.java b/api/src/main/java/com/cloud/vm/NicProfile.java index 7ac5342ee167..2b664c5f9d9d 100644 --- a/api/src/main/java/com/cloud/vm/NicProfile.java +++ b/api/src/main/java/com/cloud/vm/NicProfile.java @@ -430,16 +430,6 @@ public void deallocate() { @Override public String toString() { - return new StringBuilder("NicProfile[").append(id) - .append("-") - .append(vmId) - .append("-") - .append(reservationId) - .append("-") - .append(iPv4Address) - .append("-") - .append(broadcastUri) - .append("]") - .toString(); + return String.format("NicProfile {\"id\": %s, \"vmId\": %s, \"reservationId\": \"%s\", \"iPv4Address\": \"%s\", \"broadcastUri\": \"%s\"}", id, vmId, reservationId, iPv4Address, broadcastUri); } } diff --git a/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java b/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java index eca0852060d8..d777608f47c3 100755 --- a/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java +++ b/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java @@ -249,6 +249,7 @@ import com.cloud.vm.snapshot.VMSnapshotVO; import com.cloud.vm.snapshot.dao.VMSnapshotDao; import com.google.common.base.Strings; +import java.util.stream.Collectors; public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMachineManager, VmWorkJobHandler, Listener, Configurable { private static final Logger s_logger = Logger.getLogger(VirtualMachineManagerImpl.class); @@ -572,8 +573,9 @@ protected void advanceExpunge(VMInstanceVO vm) throws ResourceUnavailableExcepti final HypervisorGuru hvGuru = _hvGuruMgr.getGuru(vm.getHypervisorType()); - s_logger.debug("Cleaning up NICS"); - final List nicExpungeCommands = hvGuru.finalizeExpungeNics(vm, profile.getNics()); + List vmNics = profile.getNics(); + s_logger.debug(String.format("Cleaning up NICS [%s] of %s.", vmNics.stream().map(nic -> nic.toString()).collect(Collectors.joining(", ")),vm.toString())); + final List nicExpungeCommands = hvGuru.finalizeExpungeNics(vm, vmNics); _networkMgr.cleanupNics(profile); s_logger.debug("Cleaning up hypervisor data structures (ex. SRs in XenServer) for managed storage"); @@ -597,9 +599,9 @@ protected void advanceExpunge(VMInstanceVO vm) throws ResourceUnavailableExcepti if (!cmds.isSuccessful()) { for (final Answer answer : cmds.getAnswers()) { if (!answer.getResult()) { - s_logger.warn("Failed to expunge vm due to: " + answer.getDetails()); - - throw new CloudRuntimeException("Unable to expunge " + vm + " due to " + answer.getDetails()); + String message = String.format("Unable to expunge %s due to [%s].", vm.toString(), answer.getDetails()); + s_logger.warn(message); + throw new CloudRuntimeException(message); } } } @@ -643,8 +645,9 @@ protected void advanceExpunge(VMInstanceVO vm) throws ResourceUnavailableExcepti if (!cmds.isSuccessful()) { for (final Answer answer : cmds.getAnswers()) { if (!answer.getResult()) { - s_logger.warn("Failed to expunge vm due to: " + answer.getDetails()); - throw new CloudRuntimeException("Unable to expunge " + vm + " due to " + answer.getDetails()); + String message = String.format("Unable to expunge %s due to [%s].", vm.toString(), answer.getDetails()); + s_logger.warn(message); + throw new CloudRuntimeException(message); } } } @@ -704,14 +707,12 @@ private void sendModifyTargetsCommand(ModifyTargetsCommand cmd, long hostId) { Answer answer = _agentMgr.easySend(hostId, cmd); if (answer == null) { - String msg = "Unable to get an answer to the modify targets command"; - - s_logger.warn(msg); + s_logger.warn(String.format("Unable to get an answer to the modify targets command. Targets [%s].", cmd.getTargets().stream().map(target -> target.toString()).collect(Collectors.joining(", ")))); + return; } - else if (!answer.getResult()) { - String msg = "Unable to modify target on the following host: " + hostId; - s_logger.warn(msg); + if (!answer.getResult()) { + s_logger.warn(String.format("Unable to modify targets [%s] on the host [%s].", cmd.getTargets().stream().map(target -> target.toString()).collect(Collectors.joining(", ")), hostId)); } } @@ -762,15 +763,13 @@ public void start(final String vmUuid, final Map params, final DeploymentPlan planToDeploy, final DeploymentPlanner planner) { try { advanceStart(vmUuid, params, planToDeploy, planner); - } catch (final ConcurrentOperationException e) { - throw new CloudRuntimeException("Unable to start a VM due to concurrent operation", e).add(VirtualMachine.class, vmUuid); - } catch (final InsufficientCapacityException e) { - throw new CloudRuntimeException("Unable to start a VM due to insufficient capacity", e).add(VirtualMachine.class, vmUuid); + } catch (ConcurrentOperationException | InsufficientCapacityException e) { + throw new CloudRuntimeException(String.format("Unable to start a VM [%s] due to [%s].", vmUuid, e.getMessage()), e).add(VirtualMachine.class, vmUuid); } catch (final ResourceUnavailableException e) { if (e.getScope() != null && e.getScope().equals(VirtualRouter.class)){ throw new CloudRuntimeException("Network is unavailable. Please contact administrator", e).add(VirtualMachine.class, vmUuid); } - throw new CloudRuntimeException("Unable to start a VM due to unavailable resources", e).add(VirtualMachine.class, vmUuid); + throw new CloudRuntimeException(String.format("Unable to start a VM [%s] due to [%s].", vmUuid, e.getMessage()), e).add(VirtualMachine.class, vmUuid); } } @@ -954,13 +953,7 @@ public void advanceStart(final String vmUuid, final Map outcome = startVmThroughJobQueue(vmUuid, params, planToDeploy, planner); - try { - final VirtualMachine vm = outcome.get(); - } catch (final InterruptedException e) { - throw new RuntimeException("Operation is interrupted", e); - } catch (final java.util.concurrent.ExecutionException e) { - throw new RuntimeException("Execution exception", e); - } + retrieveVmFromJobOutcome(outcome, vmUuid, "startVm"); final Object jobResult = _jobMgr.unmarshallResultObject(outcome.getJob()); if (jobResult != null) { @@ -1258,7 +1251,7 @@ public void orchestrateStart(final String vmUuid, final Map outcome = stopVmThroughJobQueue(vmUuid, cleanUpEvenIfUnableToStop); - try { - final VirtualMachine vm = outcome.get(); - } catch (final InterruptedException e) { - throw new RuntimeException("Operation is interrupted", e); - } catch (final java.util.concurrent.ExecutionException e) { - throw new RuntimeException("Execution excetion", e); - } + retrieveVmFromJobOutcome(outcome, vmUuid, "stopVm"); final Object jobResult = _jobMgr.unmarshallResultObject(outcome.getJob()); if (jobResult != null) { @@ -1995,7 +1976,7 @@ private void advanceStop(final VMInstanceVO vm, final boolean cleanUpEvenIfUnabl try { if (!stateTransitTo(vm, Event.StopRequested, vm.getHostId())) { - throw new ConcurrentOperationException("VM is being operated on."); + throw new ConcurrentOperationException(String.format("%s is being operated on.", vm.toString())); } } catch (final NoTransitionException e1) { if (!cleanUpEvenIfUnableToStop) { @@ -2003,7 +1984,7 @@ private void advanceStop(final VMInstanceVO vm, final boolean cleanUpEvenIfUnabl } final boolean doCleanup = true; if (s_logger.isDebugEnabled()) { - s_logger.debug("Unable to transition the state but we're moving on because it's forced stop"); + s_logger.warn("Unable to transition the state but we're moving on because it's forced stop", e1); } if (doCleanup) { @@ -2072,10 +2053,8 @@ private void advanceStop(final VMInstanceVO vm, final boolean cleanUpEvenIfUnabl throw new CloudRuntimeException("Invalid answer received in response to a StopCommand on " + vm.instanceName); } - } catch (final AgentUnavailableException e) { - s_logger.warn("Unable to stop vm, agent unavailable: " + e.toString()); - } catch (final OperationTimedoutException e) { - s_logger.warn("Unable to stop vm, operation timed out: " + e.toString()); + } catch (AgentUnavailableException | OperationTimedoutException e) { + s_logger.warn(String.format("Unable to stop % due to [%s].", profile.toString(), e.toString()), e); } finally { if (!stopped) { if (!cleanUpEvenIfUnableToStop) { @@ -2083,7 +2062,7 @@ private void advanceStop(final VMInstanceVO vm, final boolean cleanUpEvenIfUnabl try { stateTransitTo(vm, Event.OperationFailed, vm.getHostId()); } catch (final NoTransitionException e) { - s_logger.warn("Unable to transition the state " + vm); + s_logger.warn("Unable to transition the state " + vm, e); } throw new CloudRuntimeException("Unable to stop " + vm); } else { @@ -2127,8 +2106,9 @@ private void advanceStop(final VMInstanceVO vm, final boolean cleanUpEvenIfUnabl throw new CloudRuntimeException("unable to stop " + vm); } } catch (final NoTransitionException e) { - s_logger.warn(e.getMessage()); - throw new CloudRuntimeException("Unable to stop " + vm); + String message = String.format("Unable to stop %s due to [%s].", vm.toString(), e.getMessage()); + s_logger.warn(message, e); + throw new CloudRuntimeException(message, e); } } @@ -2195,8 +2175,9 @@ public void doInTransactionWithoutResult(final TransactionStatus status) throws } } } catch (final NoTransitionException e) { - s_logger.debug(e.getMessage()); - throw new CloudRuntimeException("Unable to destroy " + vm, e); + String message = String.format("Unable to destroy %s due to [%s].", vm.toString(), e.getMessage()); + s_logger.debug(message, e); + throw new CloudRuntimeException(message, e); } } }); @@ -2268,13 +2249,7 @@ public void storageMigration(final String vmUuid, final Map volumeTo } else { final Outcome outcome = migrateVmStorageThroughJobQueue(vmUuid, volumeToPool); - try { - final VirtualMachine vm = outcome.get(); - } catch (final InterruptedException e) { - throw new RuntimeException("Operation is interrupted", e); - } catch (final java.util.concurrent.ExecutionException e) { - throw new RuntimeException("Execution excetion", e); - } + retrieveVmFromJobOutcome(outcome, vmUuid, "migrateVmStorage"); final Object jobResult = _jobMgr.unmarshallResultObject(outcome.getJob()); if (jobResult != null) { @@ -2305,14 +2280,14 @@ private void orchestrateStorageMigration(final String vmUuid, final Map prepareVmStorageMigration(VMInstanceVO vm, Map volumeToPool) { Map volumeToPoolMap = new HashMap<>(); if (MapUtils.isEmpty(volumeToPool)) { - throw new CloudRuntimeException("Unable to migrate vm: missing volume to pool mapping"); + throw new CloudRuntimeException(String.format("Unable to migrate %s: missing volume to pool mapping.", vm.toString())); } Cluster cluster = null; Long dataCenterId = null; @@ -2472,7 +2447,7 @@ private Map prepareVmStorageMigration(VMInstanceVO vm, Map< stateTransitTo(vm, Event.StorageMigrationRequested, null); } catch (final NoTransitionException e) { String msg = String.format("Unable to migrate vm: %s", vm.getUuid()); - s_logger.debug(msg); + s_logger.warn(msg, e); throw new CloudRuntimeException(msg, e); } return volumeToPoolMap; @@ -2574,9 +2549,9 @@ private void removeStaleVmFromSource(VMInstanceVO vm, HostVO srcHost) { uvc.setCleanupVmFiles(true); try { _agentMgr.send(srcHost.getId(), uvc); - } catch (final Exception e) { + } catch (AgentUnavailableException | OperationTimedoutException e) { throw new CloudRuntimeException("Failed to unregister VM: " + vm.getInstanceName() + " from source host: " + srcHost.getId() + - " after successfully migrating VM's storage across VMware Datacenters"); + " after successfully migrating VM's storage across VMware Datacenters", e); } } @@ -2600,13 +2575,7 @@ public void migrate(final String vmUuid, final long srcHostId, final DeployDesti } else { final Outcome outcome = migrateVmThroughJobQueue(vmUuid, srcHostId, dest); - try { - final VirtualMachine vm = outcome.get(); - } catch (final InterruptedException e) { - throw new RuntimeException("Operation is interrupted", e); - } catch (final java.util.concurrent.ExecutionException e) { - throw new RuntimeException("Execution excetion", e); - } + retrieveVmFromJobOutcome(outcome, vmUuid, "migrateVm"); final Object jobResult = _jobMgr.unmarshallResultObject(outcome.getJob()); if (jobResult != null) { @@ -2773,7 +2742,7 @@ protected void migrate(final VMInstanceVO vm, final long srcHostId, final Deploy } } catch (final OperationTimedoutException e) { if (e.isActive()) { - s_logger.warn("Active migration command so scheduling a restart for " + vm); + s_logger.warn("Active migration command so scheduling a restart for " + vm, e); _haMgr.scheduleRestart(vm, true); } throw new AgentUnavailableException("Operation timed out on migrating " + vm, dstHostId); @@ -2793,13 +2762,13 @@ protected void migrate(final VMInstanceVO vm, final long srcHostId, final Deploy try { _agentMgr.send(srcHostId, new Commands(cleanup(vm, dpdkInterfaceMapping)), null); } catch (final AgentUnavailableException e) { - s_logger.error("AgentUnavailableException while cleanup on source host: " + srcHostId); + s_logger.error("AgentUnavailableException while cleanup on source host: " + srcHostId, e); } cleanup(vmGuru, new VirtualMachineProfileImpl(vm), work, Event.AgentReportStopped, true); throw new CloudRuntimeException("Unable to complete migration for " + vm); } } catch (final OperationTimedoutException e) { - s_logger.debug("Error while checking the vm " + vm + " on host " + dstHostId, e); + s_logger.warn("Error while checking the vm " + vm + " on host " + dstHostId, e); } migrated = true; } finally { @@ -2814,7 +2783,7 @@ protected void migrate(final VMInstanceVO vm, final long srcHostId, final Deploy try { _agentMgr.send(dstHostId, new Commands(cleanup(vm, dpdkInterfaceMapping)), null); } catch (final AgentUnavailableException ae) { - s_logger.info("Looks like the destination Host is unavailable for cleanup"); + s_logger.warn("Looks like the destination Host is unavailable for cleanup", ae); } _networkMgr.setHypervisorHostname(profile, dest, false); try { @@ -3033,11 +3002,11 @@ private void moveVmToMigratingState(final T vm, final L // Put the vm in migrating state. try { if (!changeState(vm, Event.MigrationRequested, hostId, work, Step.Migrating)) { - s_logger.info("Migration cancelled because state has changed: " + vm); + s_logger.error("Migration cancelled because state has changed: " + vm); throw new ConcurrentOperationException("Migration cancelled because state has changed: " + vm); } } catch (final NoTransitionException e) { - s_logger.info("Migration cancelled because " + e.getMessage()); + s_logger.error("Migration cancelled because " + e.getMessage(), e); throw new ConcurrentOperationException("Migration cancelled because " + e.getMessage()); } } @@ -3050,7 +3019,7 @@ private void moveVmOutofMigratingStateOnSuccess(final T throw new ConcurrentOperationException("Unable to change the state for " + vm); } } catch (final NoTransitionException e) { - s_logger.error("Unable to change state due to " + e.getMessage()); + s_logger.error("Unable to change state due to " + e.getMessage(), e); throw new ConcurrentOperationException("Unable to change state due to " + e.getMessage()); } } @@ -3077,13 +3046,7 @@ public void migrateWithStorage(final String vmUuid, final long srcHostId, final } else { final Outcome outcome = migrateVmWithStorageThroughJobQueue(vmUuid, srcHostId, destHostId, volumeToPool); - try { - final VirtualMachine vm = outcome.get(); - } catch (final InterruptedException e) { - throw new RuntimeException("Operation is interrupted", e); - } catch (final java.util.concurrent.ExecutionException e) { - throw new RuntimeException("Execution excetion", e); - } + retrieveVmFromJobOutcome(outcome, vmUuid, "migrateVmWithStorage"); final Object jobException = _jobMgr.unmarshallResultObject(outcome.getJob()); if (jobException != null) { @@ -3192,7 +3155,7 @@ private void orchestrateMigrateWithStorage(final String vmUuid, final long srcHo _agentMgr.send(srcHost.getId(), dettachCommand); s_logger.debug("Deleted config drive ISO for vm " + vm.getInstanceName() + " In host " + srcHost); } catch (OperationTimedoutException e) { - s_logger.debug("TIme out occured while exeuting command AttachOrDettachConfigDrive " + e.getMessage()); + s_logger.error("TIme out occured while exeuting command AttachOrDettachConfigDrive " + e.getMessage(), e); } } @@ -3210,13 +3173,13 @@ private void orchestrateMigrateWithStorage(final String vmUuid, final long srcHo try { _agentMgr.send(srcHostId, new Commands(cleanup(vm.getInstanceName())), null); } catch (final AgentUnavailableException e) { - s_logger.error("AgentUnavailableException while cleanup on source host: " + srcHostId); + s_logger.error("AgentUnavailableException while cleanup on source host: " + srcHostId, e); } cleanup(vmGuru, new VirtualMachineProfileImpl(vm), work, Event.AgentReportStopped, true); throw new CloudRuntimeException("VM not found on desintation host. Unable to complete migration for " + vm); } } catch (final OperationTimedoutException e) { - s_logger.warn("Error while checking the vm " + vm + " is on host " + destHost, e); + s_logger.error("Error while checking the vm " + vm + " is on host " + destHost, e); } migrated = true; } finally { @@ -3319,13 +3282,7 @@ public void migrateAway(final String vmUuid, final long srcHostId) throws Insuff } else { final Outcome outcome = migrateVmAwayThroughJobQueue(vmUuid, srcHostId); - try { - final VirtualMachine vm = outcome.get(); - } catch (final InterruptedException e) { - throw new RuntimeException("Operation is interrupted", e); - } catch (final java.util.concurrent.ExecutionException e) { - throw new RuntimeException("Execution excetion", e); - } + retrieveVmFromJobOutcome(outcome, vmUuid, "migrateVmAway"); final Object jobException = _jobMgr.unmarshallResultObject(outcome.getJob()); if (jobException != null) { @@ -3345,8 +3302,9 @@ public void migrateAway(final String vmUuid, final long srcHostId) throws Insuff private void orchestrateMigrateAway(final String vmUuid, final long srcHostId, final DeploymentPlanner planner) throws InsufficientServerCapacityException { final VMInstanceVO vm = _vmDao.findByUuid(vmUuid); if (vm == null) { - s_logger.debug("Unable to find a VM for " + vmUuid); - throw new CloudRuntimeException("Unable to find " + vmUuid); + String message = String.format("Unable to find VM with uuid [%s].", vmUuid); + s_logger.warn(message); + throw new CloudRuntimeException(message); } ServiceOfferingVO offeringVO = _offeringDao.findById(vm.getId(), vm.getServiceOfferingId()); @@ -3354,8 +3312,9 @@ private void orchestrateMigrateAway(final String vmUuid, final long srcHostId, f final Long hostId = vm.getHostId(); if (hostId == null) { - s_logger.debug("Unable to migrate because the VM doesn't have a host id: " + vm); - throw new CloudRuntimeException("Unable to migrate " + vmUuid); + String message = String.format("Unable to migrate %s due to it does not have a host id.", vm.toString()); + s_logger.warn(message); + throw new CloudRuntimeException(message); } final Host host = _hostDao.findById(hostId); @@ -3379,8 +3338,9 @@ private void orchestrateMigrateAway(final String vmUuid, final long srcHostId, f plan.setMigrationPlan(true); dest = _dpMgr.planDeployment(profile, plan, excludes, planner); } catch (final AffinityConflictException e2) { - s_logger.warn("Unable to create deployment, affinity rules associted to the VM conflict", e2); - throw new CloudRuntimeException("Unable to create deployment, affinity rules associted to the VM conflict"); + String message = String.format("Unable to create deployment, affinity rules associted to the %s conflict.", vm.toString()); + s_logger.warn(message, e2); + throw new CloudRuntimeException(message, e2); } if (dest != null) { @@ -3398,23 +3358,15 @@ private void orchestrateMigrateAway(final String vmUuid, final long srcHostId, f try { migrate(vm, srcHostId, dest); return; - } catch (final ResourceUnavailableException e) { - s_logger.debug("Unable to migrate to unavailable " + dest); - } catch (final ConcurrentOperationException e) { - s_logger.debug("Unable to migrate VM due to: " + e.getMessage()); + } catch (ResourceUnavailableException | ConcurrentOperationException e) { + s_logger.warn(String.format("Unable to migrate %s to %s due to [%s]", vm.toString(), dest.getHost().toString(), e.getMessage()), e); } try { advanceStop(vmUuid, true); throw new CloudRuntimeException("Unable to migrate " + vm); - } catch (final ResourceUnavailableException e) { - s_logger.debug("Unable to stop VM due to " + e.getMessage()); - throw new CloudRuntimeException("Unable to migrate " + vm); - } catch (final ConcurrentOperationException e) { - s_logger.debug("Unable to stop VM due to " + e.getMessage()); - throw new CloudRuntimeException("Unable to migrate " + vm); - } catch (final OperationTimedoutException e) { - s_logger.debug("Unable to stop VM due to " + e.getMessage()); + } catch (final ResourceUnavailableException | ConcurrentOperationException | OperationTimedoutException e) { + s_logger.error(String.format("Unable to stop %s due to [%s].", vm.toString(), e.getMessage()), e); throw new CloudRuntimeException("Unable to migrate " + vm); } } @@ -3486,13 +3438,7 @@ public void advanceReboot(final String vmUuid, final Map outcome = rebootVmThroughJobQueue(vmUuid, params); - try { - final VirtualMachine vm = outcome.get(); - } catch (final InterruptedException e) { - throw new RuntimeException("Operation is interrupted", e); - } catch (final java.util.concurrent.ExecutionException e) { - throw new RuntimeException("Execution excetion", e); - } + retrieveVmFromJobOutcome(outcome, vmUuid, "rebootVm"); final Object jobResult = _jobMgr.unmarshallResultObject(outcome.getJob()); if (jobResult != null) { @@ -3554,7 +3500,7 @@ private void orchestrateReboot(final String vmUuid, final Map outcome = addVmToNetworkThroughJobQueue(vm, network, requested); - try { - outcome.get(); - } catch (final InterruptedException e) { - throw new RuntimeException("Operation is interrupted", e); - } catch (final java.util.concurrent.ExecutionException e) { - throw new RuntimeException("Execution exception", e); - } + retrieveVmFromJobOutcome(outcome, vm.getUuid(), "addVmToNetwork"); final Object jobException = _jobMgr.unmarshallResultObject(outcome.getJob()); if (jobException != null) { @@ -4075,13 +4015,7 @@ public boolean removeNicFromVm(final VirtualMachine vm, final Nic nic) } else { final Outcome outcome = removeNicFromVmThroughJobQueue(vm, nic); - try { - outcome.get(); - } catch (final InterruptedException e) { - throw new RuntimeException("Operation is interrupted", e); - } catch (final java.util.concurrent.ExecutionException e) { - throw new RuntimeException("Execution excetion", e); - } + retrieveVmFromJobOutcome(outcome, vm.getUuid(), "removeNicFromVm"); final Object jobResult = _jobMgr.unmarshallResultObject(outcome.getJob()); if (jobResult != null) { @@ -4274,8 +4208,9 @@ public void findHostAndMigrate(final String vmUuid, final Long newSvcOfferingId, try { dest = _dpMgr.planDeployment(profile, plan, excludes, null); } catch (final AffinityConflictException e2) { - s_logger.warn("Unable to create deployment, affinity rules associted to the VM conflict", e2); - throw new CloudRuntimeException("Unable to create deployment, affinity rules associted to the VM conflict"); + String message = String.format("Unable to create deployment, affinity rules associted to the %s conflict.", vm.toString()); + s_logger.warn(message, e2); + throw new CloudRuntimeException(message); } if (dest != null) { @@ -4291,11 +4226,8 @@ public void findHostAndMigrate(final String vmUuid, final Long newSvcOfferingId, excludes.addHost(dest.getHost().getId()); try { migrateForScale(vm.getUuid(), srcHostId, dest, oldSvcOfferingId); - } catch (final ResourceUnavailableException e) { - s_logger.debug("Unable to migrate to unavailable " + dest); - throw e; - } catch (final ConcurrentOperationException e) { - s_logger.debug("Unable to migrate VM due to: " + e.getMessage()); + } catch (ResourceUnavailableException | ConcurrentOperationException e) { + s_logger.warn(String.format("Unable to migrate %s to %s due to [%s]", vm.toString(), dest.getHost().toString(), e.getMessage()), e); throw e; } } @@ -4319,13 +4251,7 @@ public void migrateForScale(final String vmUuid, final long srcHostId, final Dep } else { final Outcome outcome = migrateVmForScaleThroughJobQueue(vmUuid, srcHostId, dest, oldSvcOfferingId); - try { - final VirtualMachine vm = outcome.get(); - } catch (final InterruptedException e) { - throw new RuntimeException("Operation is interrupted", e); - } catch (final java.util.concurrent.ExecutionException e) { - throw new RuntimeException("Execution excetion", e); - } + retrieveVmFromJobOutcome(outcome, vmUuid, "migrateVmForScale"); final Object jobResult = _jobMgr.unmarshallResultObject(outcome.getJob()); if (jobResult != null) { @@ -4372,17 +4298,15 @@ private void orchestrateMigrateForScale(final String vmUuid, final long srcHostI final long vmId = vm.getId(); vm = _vmDao.findByUuid(vmUuid); if (vm == null) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("Unable to find the vm " + vm); - } - throw new CloudRuntimeException("Unable to find a virtual machine with id " + vmId); + String message = String.format("Unable to find VM {\"uuid\": \"%s\"}.", vmUuid); + s_logger.warn(message); + throw new CloudRuntimeException(message); } if (vm.getState() != State.Running) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("VM is not Running, unable to migrate the vm " + vm); - } - throw new CloudRuntimeException("VM is not Running, unable to migrate the vm currently " + vm + " , current state: " + vm.getState().toString()); + String message = String.format("%s is not in \"Running\" state, unable to migrate it. Current state [%s].", vm.toString(), vm.getState()); + s_logger.warn(message); + throw new CloudRuntimeException(message); } AlertManager.AlertType alertType = AlertManager.AlertType.ALERT_TYPE_USERVM_MIGRATE; @@ -4411,9 +4335,8 @@ private void orchestrateMigrateForScale(final String vmUuid, final long srcHostI pfma = _agentMgr.send(dstHostId, pfmc); if (pfma == null || !pfma.getResult()) { final String details = pfma != null ? pfma.getDetails() : "null answer returned"; - final String msg = "Unable to prepare for migration due to " + details; pfma = null; - throw new AgentUnavailableException(msg, dstHostId); + throw new AgentUnavailableException(String.format("Unable to prepare for migration to destination host [%s] due to [%s].", dstHostId, details), dstHostId); } } catch (final OperationTimedoutException e1) { throw new AgentUnavailableException("Operation timed out", dstHostId); @@ -4426,13 +4349,15 @@ private void orchestrateMigrateForScale(final String vmUuid, final long srcHostI vm.setLastHostId(srcHostId); try { - if (vm == null || vm.getHostId() == null || vm.getHostId() != srcHostId || !changeState(vm, Event.MigrationRequested, dstHostId, work, Step.Migrating)) { - s_logger.info("Migration cancelled because state has changed: " + vm); - throw new ConcurrentOperationException("Migration cancelled because state has changed: " + vm); + if (vm.getHostId() == null || vm.getHostId() != srcHostId || !changeState(vm, Event.MigrationRequested, dstHostId, work, Step.Migrating)) { + String message = String.format("Migration of %s cancelled because state has changed.", vm.toString()); + s_logger.warn(message); + throw new ConcurrentOperationException(message); } } catch (final NoTransitionException e1) { - s_logger.info("Migration cancelled because " + e1.getMessage()); - throw new ConcurrentOperationException("Migration cancelled because " + e1.getMessage()); + String message = String.format("Migration of %s cancelled due to [%s].", vm.toString(), e1.getMessage()); + s_logger.error(message, e1); + throw new ConcurrentOperationException(message); } boolean migrated = false; @@ -4451,17 +4376,16 @@ private void orchestrateMigrateForScale(final String vmUuid, final long srcHostI try { final Answer ma = _agentMgr.send(vm.getLastHostId(), mc); if (ma == null || !ma.getResult()) { - final String details = ma != null ? ma.getDetails() : "null answer returned"; - final String msg = "Unable to migrate due to " + details; + String msg = String.format("Unable to migrate %s due to [%s].", vm.toString(), ma != null ? ma.getDetails() : "null answer returned"); s_logger.error(msg); throw new CloudRuntimeException(msg); } } catch (final OperationTimedoutException e) { if (e.isActive()) { - s_logger.warn("Active migration command so scheduling a restart for " + vm); + s_logger.warn("Active migration command so scheduling a restart for " + vm, e); _haMgr.scheduleRestart(vm, true); } - throw new AgentUnavailableException("Operation timed out on migrating " + vm, dstHostId); + throw new AgentUnavailableException("Operation timed out on migrating " + vm, dstHostId, e); } try { @@ -4481,7 +4405,7 @@ private void orchestrateMigrateForScale(final String vmUuid, final long srcHostI try { _agentMgr.send(srcHostId, new Commands(cleanup(vm.getInstanceName())), null); } catch (final AgentUnavailableException e) { - s_logger.error(String.format("Unable to cleanup source %s. ", srcHost), e); + s_logger.error(String.format("Unable to cleanup source host [%s] due to [%s].", srcHostId, e.getMessage()), e); } cleanup(vmGuru, new VirtualMachineProfileImpl(vm), work, Event.AgentReportStopped, true); throw new CloudRuntimeException("Unable to complete migration for " + vm); @@ -4508,7 +4432,7 @@ private void orchestrateMigrateForScale(final String vmUuid, final long srcHostI try { stateTransitTo(vm, Event.OperationFailed, srcHostId); } catch (final NoTransitionException e) { - s_logger.warn(e.getMessage()); + s_logger.warn(e.getMessage(), e); } } else { _networkMgr.setHypervisorHostname(profile, dest, true); @@ -4540,10 +4464,10 @@ public boolean replugNic(final Network network, final NicTO nic, final VirtualMa throw new AgentUnavailableException("Unable to plug nic for router " + vm.getName() + " in network " + network, dest.getHost().getId(), e); } } else { - s_logger.warn("Unable to apply ReplugNic, vm " + router + " is not in the right state " + router.getState()); + String message = String.format("Unable to apply ReplugNic, VM [%s] is not in the right state (\"Running\"). VM state [%s].", router.toString(), router.getState()); + s_logger.warn(message); - throw new ResourceUnavailableException("Unable to apply ReplugNic on the backend," + " vm " + vm + " is not in the right state", DataCenter.class, - router.getDataCenterId()); + throw new ResourceUnavailableException(message, DataCenter.class, router.getDataCenterId()); } return result; @@ -4576,9 +4500,10 @@ public boolean plugNic(final Network network, final NicTO nic, final VirtualMach throw new AgentUnavailableException("Unable to plug nic for router " + vm.getName() + " in network " + network, dest.getHost().getId(), e); } } else { - s_logger.warn("Unable to apply PlugNic, vm " + router + " is not in the right state " + router.getState()); + String message = String.format("Unable to apply PlugNic, VM [%s] is not in the right state (\"Running\"). VM state [%s].", router.toString(), router.getState()); + s_logger.warn(message); - throw new ResourceUnavailableException("Unable to apply PlugNic on the backend," + " vm " + vm + " is not in the right state", DataCenter.class, + throw new ResourceUnavailableException(message, DataCenter.class, router.getDataCenterId()); } @@ -4618,10 +4543,10 @@ public boolean unplugNic(final Network network, final NicTO nic, final VirtualMa } else if (router.getState() == State.Stopped || router.getState() == State.Stopping) { s_logger.debug("Vm " + router.getInstanceName() + " is in " + router.getState() + ", so not sending unplug nic command to the backend"); } else { - s_logger.warn("Unable to apply unplug nic, Vm " + router + " is not in the right state " + router.getState()); + String message = String.format("Unable to apply unplug nic, VM [%s] is not in the right state (\"Running\"). VM state [%s].", router.toString(), router.getState()); + s_logger.warn(message); - throw new ResourceUnavailableException("Unable to apply unplug nic on the backend," + " vm " + router + " is not in the right state", DataCenter.class, - router.getDataCenterId()); + throw new ResourceUnavailableException(message, DataCenter.class, router.getDataCenterId()); } return result; @@ -4648,14 +4573,7 @@ public VMInstanceVO reConfigureVm(final String vmUuid, final ServiceOffering old } else { final Outcome outcome = reconfigureVmThroughJobQueue(vmUuid, oldServiceOffering, newServiceOffering, customParameters, reconfiguringOnExistingHost); - VirtualMachine vm = null; - try { - vm = outcome.get(); - } catch (final InterruptedException e) { - throw new RuntimeException("Operation is interrupted", e); - } catch (final java.util.concurrent.ExecutionException e) { - throw new RuntimeException("Execution excetion", e); - } + VirtualMachine vm = retrieveVmFromJobOutcome(outcome, vmUuid, "reconfigureVm"); final Object jobResult = _jobMgr.unmarshallResultObject(outcome.getJob()); if (jobResult != null) { @@ -5742,10 +5660,10 @@ private Pair orchestrateStart(final VmWorkStart work) th try { orchestrateStart(vm.getUuid(), work.getParams(), work.getPlan(), _dpMgr.getDeploymentPlannerByName(work.getDeploymentPlanner())); - } catch (CloudRuntimeException e) { - e.printStackTrace(); - s_logger.info("Caught CloudRuntimeException, returning job failed " + e); - CloudRuntimeException ex = new CloudRuntimeException("Unable to start VM instance"); + } catch (CloudRuntimeException e){ + String message = String.format("Unable to orchestrate start %s due to [%s].", vm.toString(), e.getMessage()); + s_logger.warn(message, e); + CloudRuntimeException ex = new CloudRuntimeException(message); return new Pair(JobInfo.Status.FAILED, JobSerializerHelper.toObjectSerializedString(ex)); } return new Pair(JobInfo.Status.SUCCEEDED, null); @@ -5755,8 +5673,9 @@ private Pair orchestrateStart(final VmWorkStart work) th private Pair orchestrateStop(final VmWorkStop work) throws Exception { final VMInstanceVO vm = _entityMgr.findById(VMInstanceVO.class, work.getVmId()); if (vm == null) { - s_logger.info("Unable to find vm " + work.getVmId()); - throw new CloudRuntimeException("Unable to find VM id=" + work.getVmId()); + String message = String.format("Unable to find %s.", vm.toString()); + s_logger.warn(message); + throw new CloudRuntimeException(message); } orchestrateStop(vm.getUuid(), work.isCleanup()); @@ -5786,7 +5705,7 @@ private Pair orchestrateMigrateAway(final VmWorkMigrateA try { orchestrateMigrateAway(vm.getUuid(), work.getSrcHostId(), null); } catch (final InsufficientServerCapacityException e) { - s_logger.warn("Failed to deploy vm " + vm.getId() + " with original planner, sending HAPlanner"); + s_logger.warn("Failed to deploy vm " + vm.getId() + " with original planner, sending HAPlanner", e); orchestrateMigrateAway(vm.getUuid(), work.getSrcHostId(), _haMgr.getHAPlanner()); } @@ -5956,13 +5875,7 @@ public UserVm restoreVirtualMachine(final long vmId, final Long newTemplateId) t } else { final Outcome outcome = restoreVirtualMachineThroughJobQueue(vmId, newTemplateId); - try { - outcome.get(); - } catch (final InterruptedException e) { - throw new RuntimeException("Operation is interrupted", e); - } catch (final java.util.concurrent.ExecutionException e) { - throw new RuntimeException("Execution exception", e); - } + retrieveVmFromJobOutcome(outcome, String.valueOf(vmId), "restoreVirtualMachine"); final Object jobResult = _jobMgr.unmarshallResultObject(outcome.getJob()); if (jobResult != null) { @@ -6060,13 +5973,7 @@ public Boolean updateDefaultNicForVM(final VirtualMachine vm, final Nic nic, fin } else { final Outcome outcome = updateDefaultNicForVMThroughJobQueue(vm, nic, defaultNic); - try { - outcome.get(); - } catch (final InterruptedException e) { - throw new RuntimeException("Operation is interrupted", e); - } catch (final java.util.concurrent.ExecutionException e) { - throw new RuntimeException("Execution exception", e); - } + retrieveVmFromJobOutcome(outcome, vm.getUuid(), "updateDefaultNicForVM"); final Object jobResult = _jobMgr.unmarshallResultObject(outcome.getJob()); if (jobResult != null) { @@ -6204,4 +6111,12 @@ public Pair findClusterAndHostIdForVm(long vmId) { } return findClusterAndHostIdForVm(vm); } + + protected VirtualMachine retrieveVmFromJobOutcome(Outcome jobOutcome, String vmUuid, String jobName) { + try { + return jobOutcome.get(); + } catch (InterruptedException | java.util.concurrent.ExecutionException e) { + throw new RuntimeException(String.format("Unable to retrieve result from job \"%s\" due to [%s]. VM {\"uuid\": \"%s\"}.", jobName, e.getMessage(), vmUuid), e); + } + } } From d32a185f582a29a33457113ef50425ba31234187 Mon Sep 17 00:00:00 2001 From: Daniel Augusto Veronezi Salvador Date: Tue, 13 Apr 2021 19:04:25 -0300 Subject: [PATCH 02/25] Remove unnecessary comments --- .../cloud/vm/VirtualMachineManagerImpl.java | 221 +++--------------- 1 file changed, 28 insertions(+), 193 deletions(-) diff --git a/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java b/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java index d777608f47c3..e523d78c8ff6 100755 --- a/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java +++ b/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java @@ -477,7 +477,7 @@ public void doInTransactionWithoutResult(final TransactionStatus status) throws volumeMgr.allocateRawVolume(Type.ROOT, "ROOT-" + vmFinal.getId(), rootDiskOfferingInfo.getDiskOffering(), rootDiskOfferingInfo.getSize(), rootDiskOfferingInfo.getMinIops(), rootDiskOfferingInfo.getMaxIops(), vmFinal, template, owner, null); } else if (template.getFormat() == ImageFormat.BAREMETAL) { - // Do nothing + } else { volumeMgr.allocateTemplatedVolumes(Type.ROOT, "ROOT-" + vmFinal.getId(), rootDiskOfferingInfo.getDiskOffering(), rootDiskOfferingInfo.getSize(), rootDiskOfferingInfo.getMinIops(), rootDiskOfferingInfo.getMaxIops(), template, vmFinal, owner); @@ -611,7 +611,6 @@ protected void advanceExpunge(VMInstanceVO vm) throws ResourceUnavailableExcepti volumeMgr.revokeAccess(vm.getId(), hostId); } - // Clean up volumes based on the vm's instance id volumeMgr.cleanupVolumes(vm.getId()); if (hostId != null && CollectionUtils.isNotEmpty(targets)) { @@ -620,13 +619,11 @@ protected void advanceExpunge(VMInstanceVO vm) throws ResourceUnavailableExcepti final VirtualMachineGuru guru = getVmGuru(vm); guru.finalizeExpunge(vm); - //remove the overcommit details from the uservm details + userVmDetailsDao.removeDetails(vm.getId()); - // Remove deploy as-is (if any) userVmDeployAsIsDetailsDao.removeDetails(vm.getId()); - // send hypervisor-dependent commands before removing final List finalizeExpungeCommands = hvGuru.finalizeExpunge(vm); if (finalizeExpungeCommands != null && finalizeExpungeCommands.size() > 0) { if (hostId != null) { @@ -718,13 +715,11 @@ private void sendModifyTargetsCommand(ModifyTargetsCommand cmd, long hostId) { @Override public boolean start() { - // TODO, initial delay is hardcoded _executor.scheduleAtFixedRate(new CleanupTask(), 5, VmJobStateReportInterval.value(), TimeUnit.SECONDS); _executor.scheduleAtFixedRate(new TransitionTask(), VmOpCleanupInterval.value(), VmOpCleanupInterval.value(), TimeUnit.SECONDS); cancelWorkItems(_nodeId); volumeMgr.cleanupStorageJobs(); - // cleanup left over place holder works _workJobDao.expungeLeftoverWorkJobs(ManagementServerNode.getManagementServerId()); return true; } @@ -790,7 +785,6 @@ protected boolean checkWorkItems(final VMInstanceVO vm, final State state) throw return true; } - // also check DB to get latest VM state to detect vm update from concurrent process before idle waiting to get an early exit final VMInstanceVO instance = _vmDao.findById(vm.getId()); if (instance != null && instance.getState() == State.Running) { if (s_logger.isDebugEnabled()) { @@ -890,7 +884,6 @@ public Ternary doInTransaction(final } protected boolean changeState(final T vm, final Event event, final Long hostId, final ItWorkVO work, final Step step) throws NoTransitionException { - // FIXME: We should do this better. Step previousStep = null; if (work != null) { previousStep = work.getStep(); @@ -934,7 +927,7 @@ public void advanceStart(final String vmUuid, final Map":params.get(VirtualMachineProfile.Param.BootIntoSetup)))); } - // avoid re-entrance + VmWorkJobVO placeHolder = null; final VirtualMachine vm = _vmDao.findByUuid(vmUuid); placeHolder = createPlaceHolderWork(vm.getId()); @@ -1026,7 +1019,6 @@ public void orchestrateStart(final String vmUuid, final Map vols = _volsDao.findReadyRootVolumesByInstance(vm.getId()); for (final VolumeVO vol : vols) { - // make sure if the templateId is unchanged. If it is changed, - // let planner - // reassign pool for the volume even if it ready. final Long volTemplateId = vol.getTemplateId(); if (volTemplateId != null && volTemplateId.longValue() != template.getId()) { if (s_logger.isDebugEnabled()) { @@ -1082,8 +1070,6 @@ public void orchestrateStart(final String vmUuid, final Map 1f || Float.parseFloat(cluster_detail_ram.getValue()) > 1f)) { userVmDetailsDao.addDetail(vm.getId(), VmDetailConstants.CPU_OVER_COMMIT_RATIO, cluster_detail_cpu.getValue(), true); @@ -1171,7 +1156,6 @@ public void orchestrateStart(final String vmUuid, final Map details = vmTO.getDetails(); for (String key : details.keySet()) { @@ -1421,7 +1398,6 @@ private void addExtraConfig(VirtualMachineTO vmTO) { } } - // for managed storage on KVM, need to make sure the path field of the volume in question is populated with the IQN private void handlePath(final DiskTO[] disks, final HypervisorType hypervisorType) { if (hypervisorType != HypervisorType.KVM) { return; @@ -1452,7 +1428,6 @@ private void handlePath(final DiskTO[] disks, final HypervisorType hypervisorTyp } } - // for managed storage on XenServer and VMware, need to update the DB with a path if the VDI/VMDK file was newly created private void handlePath(final DiskTO[] disks, final Map> iqnToData) { if (disks != null && iqnToData != null) { for (final DiskTO disk : disks) { @@ -1506,9 +1481,6 @@ private void syncDiskChainChange(final StartAnswer answer) { _volsDao.update(volume.getId(), volume); } - // Use getPath() from VolumeVO to get a fresh copy of what's in the DB. - // Before doing this, in a certain situation, getPath() from VolumeObjectTO - // returned null instead of an actual path (because it was out of date with the DB). if(vol.getPath() != null) { volumeMgr.updateVolumeDiskChain(vol.getId(), vol.getPath(), vol.getChainInfo(), vol.getUpdatedDataStoreUUID()); } else { @@ -1819,7 +1791,6 @@ public void advanceStop(final String vmUuid, final boolean cleanUpEvenIfUnableTo final AsyncJobExecutionContext jobContext = AsyncJobExecutionContext.getCurrentExecutionContext(); if (jobContext.isJobDispatchedBy(VmWorkConstants.VM_WORK_JOB_DISPATCHER)) { - // avoid re-entrance VmWorkJobVO placeHolder = null; final VirtualMachine vm = _vmDao.findByUuid(vmUuid); @@ -1934,7 +1905,7 @@ private void advanceStop(final VMInstanceVO vm, final boolean cleanUpEvenIfUnabl } return; } - // grab outstanding work item if any + final ItWorkVO work = _workDao.findByOutstandingWork(vm.getId(), vm.getState()); if (work != null) { if (s_logger.isDebugEnabled()) { @@ -1954,7 +1925,7 @@ private void advanceStop(final VMInstanceVO vm, final boolean cleanUpEvenIfUnabl } catch (final NoTransitionException e) { s_logger.warn(e.getMessage()); } - // mark outstanding work item if any as done + if (work != null) { if (s_logger.isDebugEnabled()) { s_logger.debug("Updating work item to Done, id:" + work.getId()); @@ -2098,7 +2069,6 @@ private void advanceStop(final VMInstanceVO vm, final boolean cleanUpEvenIfUnabl boolean result = stateTransitTo(vm, Event.OperationSucceeded, null); if (result) { if (VirtualMachine.Type.User.equals(vm.type) && ResoureCountRunningVMsonly.value()) { - //update resource count if stop successfully ServiceOfferingVO offering = _offeringDao.findById(vm.getId(), vm.getServiceOfferingId()); resourceCountDecrement(vm.getAccountId(),new Long(offering.getCpu()), new Long(offering.getRamSize())); } @@ -2117,8 +2087,6 @@ private void setStateMachine() { } protected boolean stateTransitTo(final VMInstanceVO vm, final VirtualMachine.Event e, final Long hostId, final String reservationId) throws NoTransitionException { - // if there are active vm snapshots task, state change is not allowed - vm.setReservationId(reservationId); return _stateMachine.transitTo(vm, e, new Pair(vm.getHostId(), hostId), _vmDao); } @@ -2235,7 +2203,7 @@ protected boolean checkVmOnHost(final VirtualMachine vm, final long hostId) thro public void storageMigration(final String vmUuid, final Map volumeToPool) { final AsyncJobExecutionContext jobContext = AsyncJobExecutionContext.getCurrentExecutionContext(); if (jobContext.isJobDispatchedBy(VmWorkConstants.VM_WORK_JOB_DISPATCHER)) { - // avoid re-entrance + VmWorkJobVO placeHolder = null; final VirtualMachine vm = _vmDao.findByUuid(vmUuid); placeHolder = createPlaceHolderWork(vm.getId()); @@ -2277,7 +2245,7 @@ private void orchestrateStorageMigration(final String vmUuid, final Map commandsToSend = hvGuru.finalizeMigrate(vm, volumeToPool); if (CollectionUtils.isNotEmpty(commandsToSend)) { Commands commandsContainer = new Commands(Command.OnError.Stop); commandsContainer.addCommands(commandsToSend); + try { - // OfflineVmwareMigration: change to the call back variety? - // OfflineVmwareMigration: getting a Long seq to be filled with _agentMgr.send(hostId, commandsContainer, this) return _agentMgr.send(hostId, commandsContainer); } catch (AgentUnavailableException | OperationTimedoutException e) { throw new CloudRuntimeException(String.format("Failed to migrate VM: %s", vm.getUuid()),e); @@ -2324,6 +2288,7 @@ private void afterHypervisorMigrationCleanup(VMInstanceVO vm, Map entry : volumeToPool.entrySet()) { @@ -2342,11 +2307,9 @@ private void afterHypervisorMigrationCleanup(VMInstanceVO vm, Map 0) - // OfflineVmwareMigration: iterate over the volumes for data updates } private void markVolumesInPool(VMInstanceVO vm, Answer[] hypervisorMigrationResults) { @@ -2400,7 +2363,6 @@ private void migrateThroughHypervisorOrStorage(VMInstanceVO vm, Map prepareVmStorageMigration(VMInstanceVO vm, Map< private void checkDestinationForTags(StoragePool destPool, VMInstanceVO vm) { List vols = _volsDao.findUsableVolumesForInstance(vm.getId()); - // OfflineVmwareMigration: iterate over volumes - // OfflineVmwareMigration: get disk offering + List storageTags = storageMgr.getStoragePoolTagList(destPool.getId()); for(Volume vol : vols) { DiskOfferingVO diskOffering = _diskOfferingDao.findById(vol.getDiskOfferingId()); @@ -2474,11 +2435,9 @@ private void checkDestinationForTags(StoragePool destPool, VMInstanceVO vm) { } static boolean matches(List volumeTags, List storagePoolTags) { - // OfflineVmwareMigration: commons collections 4 allows for Collections.containsAll(volumeTags,storagePoolTags); boolean result = true; if (volumeTags != null) { for (String tag : volumeTags) { - // there is a volume tags so if (storagePoolTags == null || !storagePoolTags.contains(tag)) { result = false; break; @@ -2500,22 +2459,17 @@ private void postStorageMigrationCleanup(VMInstanceVO vm, Map getCandidateStoragePoolsToMigrateLocalVolume(Virtual } private void moveVmToMigratingState(final T vm, final Long hostId, final ItWorkVO work) throws ConcurrentOperationException { - // Put the vm in migrating state. try { if (!changeState(vm, Event.MigrationRequested, hostId, work, Step.Migrating)) { s_logger.error("Migration cancelled because state has changed: " + vm); @@ -3012,7 +2960,6 @@ private void moveVmToMigratingState(final T vm, final L } private void moveVmOutofMigratingStateOnSuccess(final T vm, final Long hostId, final ItWorkVO work) throws ConcurrentOperationException { - // Put the vm in running state. try { if (!changeState(vm, Event.OperationSucceeded, hostId, work, Step.Started)) { s_logger.error("Unable to change the state for " + vm); @@ -3030,7 +2977,6 @@ public void migrateWithStorage(final String vmUuid, final long srcHostId, final final AsyncJobExecutionContext jobContext = AsyncJobExecutionContext.getCurrentExecutionContext(); if (jobContext.isJobDispatchedBy(VmWorkConstants.VM_WORK_JOB_DISPATCHER)) { - // avoid re-entrance VmWorkJobVO placeHolder = null; final VirtualMachine vm = _vmDao.findByUuid(vmUuid); @@ -3086,11 +3032,8 @@ private void orchestrateMigrateWithStorage(final String vmUuid, final long srcHo final VirtualMachineProfile profile = new VirtualMachineProfileImpl(vm, null, _offeringDao.findById(vm.getId(), vm.getServiceOfferingId()), null, null); profile.setHost(destHost); - // Create a map of which volume should go in which storage pool. final Map volumeToPoolMap = createMappingVolumeAndStoragePool(profile, destHost, volumeToPool); - // If none of the volumes have to be migrated, fail the call. Administrator needs to make a call for migrating - // a vm and not migrating a vm with storage. if (volumeToPoolMap == null || volumeToPoolMap.isEmpty()) { throw new InvalidParameterValueException("Migration of the vm " + vm + "from host " + srcHost + " to destination host " + destHost + " doesn't involve migrating the volumes."); @@ -3114,7 +3057,6 @@ private void orchestrateMigrateWithStorage(final String vmUuid, final long srcHo work.setResourceId(destHostId); work = _workDao.persist(work); - // Put the vm in migrating state. vm.setLastHostId(srcHostId); vm.setPodIdToDeployIn(destHost.getPodId()); moveVmToMigratingState(vm, destHostId, work); @@ -3122,11 +3064,6 @@ private void orchestrateMigrateWithStorage(final String vmUuid, final long srcHo boolean migrated = false; try { - - // config drive: Detach the config drive at source host - // After migration successful attach the config drive in destination host - // On migration failure VM will be stopped, So configIso will be deleted - Nic defaultNic = _networkModel.getDefaultNic(vm.getId()); if (defaultNic != null && VirtualMachine.Type.User.equals(vm.getType())) { @@ -3149,7 +3086,6 @@ private void orchestrateMigrateWithStorage(final String vmUuid, final long srcHo profile.setConfigDriveIsoRootFolder(configDriveIsoRootFolder); profile.setConfigDriveIsoFile(isoFile); - // At source host detach the config drive iso. AttachOrDettachConfigDriveCommand dettachCommand = new AttachOrDettachConfigDriveCommand(vm.getInstanceName(), vmData, VmConfigDriveLabel.value(), false); try { _agentMgr.send(srcHost.getId(), dettachCommand); @@ -3161,10 +3097,8 @@ private void orchestrateMigrateWithStorage(final String vmUuid, final long srcHo } } - // Migrate the vm and its volume. volumeMgr.migrateVolumes(vm, to, srcHost, destHost, volumeToPoolMap); - // Put the vm back to running state. moveVmOutofMigratingStateOnSuccess(vm, destHost.getId(), work); try { @@ -3264,8 +3198,6 @@ protected void cancelWorkItems(final long nodeId) { public void migrateAway(final String vmUuid, final long srcHostId) throws InsufficientServerCapacityException { final AsyncJobExecutionContext jobContext = AsyncJobExecutionContext.getCurrentExecutionContext(); if (jobContext.isJobDispatchedBy(VmWorkConstants.VM_WORK_JOB_DISPATCHER)) { - // avoid re-entrance - VmWorkJobVO placeHolder = null; final VirtualMachine vm = _vmDao.findByUuid(vmUuid); placeHolder = createPlaceHolderWork(vm.getId()); @@ -3416,7 +3348,6 @@ public void advanceReboot(final String vmUuid, final Map params) throws InsufficientCapacityException, ConcurrentOperationException, ResourceUnavailableException { final VMInstanceVO vm = _vmDao.findByUuid(vmUuid); - // if there are active vm snapshots task, state change is not allowed if (_vmSnapshotMgr.hasActiveVMSnapshotTasks(vm.getId())) { s_logger.error("Unable to reboot VM " + vm + " due to: " + vm.getInstanceName() + " has active VM snapshots tasks"); throw new CloudRuntimeException("Unable to reboot VM " + vm + " due to: " + vm.getInstanceName() + " has active VM snapshots tasks"); @@ -3468,7 +3398,6 @@ private void orchestrateReboot(final String vmUuid, final Map vmMetadatum) { if (vmMetadatum == null || vmMetadatum.isEmpty()) { return; @@ -3614,7 +3541,6 @@ public void syncVMMetaData(final Map vmMetadatum) { } } - // this is XenServer specific private void updateVmMetaData(Long vmId, String platform) { UserVmVO userVm = _userVmDao.findById(vmId); _userVmDao.loadDetails(userVm); @@ -3671,8 +3597,6 @@ public boolean processCommands(final long agentId, final long seq, final Command _syncMgr.processHostVmStatePingReport(agentId, ping.getHostVmStateReport()); } - // take the chance to scan VMs that are stuck in transitional states - // and are missing from the report scanStalledVMInTransitionStateOnUpHost(agentId); processed = true; } @@ -3721,8 +3645,7 @@ public void processConnect(final Host agent, final StartupCommand cmd, final boo final Long clusterId = agent.getClusterId(); final long agentId = agent.getId(); - if (agent.getHypervisorType() == HypervisorType.XenServer) { // only for Xen - // initiate the cron job + if (agent.getHypervisorType() == HypervisorType.XenServer) { final ClusterVMMetaDataSyncCommand syncVMMetaDataCmd = new ClusterVMMetaDataSyncCommand(ClusterVMMetaDataSyncInterval.value(), clusterId); try { final long seq_no = _agentMgr.send(agentId, new Commands(syncVMMetaDataCmd), this); @@ -3777,14 +3700,12 @@ public void checkIfCanUpgrade(final VirtualMachine vmInstance, final ServiceOffe throw new InvalidParameterValueException("Invalid parameter, newServiceOffering can't be null"); } - // Check that the VM is stopped / running if (!(vmInstance.getState().equals(State.Stopped) || vmInstance.getState().equals(State.Running))) { s_logger.warn("Unable to upgrade virtual machine " + vmInstance.toString() + " in state " + vmInstance.getState()); throw new InvalidParameterValueException("Unable to upgrade virtual machine " + vmInstance.toString() + " " + " in state " + vmInstance.getState() + "; make sure the virtual machine is stopped/running"); } - // Check if the service offering being upgraded to is what the VM is already running with if (!newServiceOffering.isDynamic() && vmInstance.getServiceOfferingId() == newServiceOffering.getId()) { if (s_logger.isInfoEnabled()) { s_logger.info("Not upgrading vm " + vmInstance.toString() + " since it already has the requested " + "service offering (" + newServiceOffering.getName() + @@ -3799,18 +3720,15 @@ public void checkIfCanUpgrade(final VirtualMachine vmInstance, final ServiceOffe checkIfNewOfferingStorageScopeMatchesStoragePool(vmInstance, newServiceOffering); - // if vm is a system vm, check if it is a system service offering, if yes return with error as it cannot be used for user vms if (currentServiceOffering.isSystemUse() != newServiceOffering.isSystemUse()) { throw new InvalidParameterValueException("isSystem property is different for current service offering and new service offering"); } - // Check that there are enough resources to upgrade the service offering if (!isVirtualMachineUpgradable(vmInstance, newServiceOffering)) { throw new InvalidParameterValueException("Unable to upgrade virtual machine, not enough resources available " + "for an offering of " + newServiceOffering.getCpu() + " cpu(s) at " + newServiceOffering.getSpeed() + " Mhz, and " + newServiceOffering.getRamSize() + " MB of memory"); } - // Check that the service offering being upgraded to has all the tags of the current service offering. final List currentTags = StringUtils.csvTagsToList(currentServiceOffering.getTags()); final List newTags = StringUtils.csvTagsToList(newServiceOffering.getTags()); if (!newTags.containsAll(currentTags)) { @@ -3879,7 +3797,6 @@ public NicProfile addVmToNetwork(final VirtualMachine vm, final Network network, final AsyncJobExecutionContext jobContext = AsyncJobExecutionContext.getCurrentExecutionContext(); if (jobContext.isJobDispatchedBy(VmWorkConstants.VM_WORK_JOB_DISPATCHER)) { - // avoid re-entrance VmWorkJobVO placeHolder = null; placeHolder = createPlaceHolderWork(vm.getId()); try { @@ -3929,16 +3846,12 @@ private NicProfile orchestrateAddVmToNetwork(final VirtualMachine vm, final Netw final Host host = _hostDao.findById(vm.getHostId()); final DeployDestination dest = new DeployDestination(dc, null, null, host); - //check vm state if (vm.getState() == State.Running) { - //1) allocate and prepare nic final NicProfile nic = _networkMgr.createNicForVm(network, requested, context, vmProfile, true); - //2) Convert vmProfile to vmTO final HypervisorGuru hvGuru = _hvGuruMgr.getGuru(vmProfile.getVirtualMachine().getHypervisorType()); final VirtualMachineTO vmTO = hvGuru.implement(vmProfile); - //3) Convert nicProfile to NicTO final NicTO nicTO = toNicTO(nic, vmProfile.getVirtualMachine().getHypervisorType()); if (network != null) { @@ -3951,7 +3864,6 @@ private NicProfile orchestrateAddVmToNetwork(final VirtualMachine vm, final Netw nicTO.setDetails(details); } - //4) plug the nic to the vm s_logger.debug("Plugging nic for vm " + vm + " in network " + network); boolean result = false; @@ -3961,9 +3873,8 @@ private NicProfile orchestrateAddVmToNetwork(final VirtualMachine vm, final Netw _userVmMgr.setupVmForPvlan(true, vm.getHostId(), nic); s_logger.debug("Nic is plugged successfully for vm " + vm + " in network " + network + ". Vm is a part of network now"); final long isDefault = nic.isDefaultNic() ? 1 : 0; - // insert nic's Id into DB as resource_name + if(VirtualMachine.Type.User.equals(vmVO.getType())) { - //Log usage event for user Vms only UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NETWORK_OFFERING_ASSIGN, vmVO.getAccountId(), vmVO.getDataCenterId(), vmVO.getId(), Long.toString(nic.getId()), network.getNetworkOfferingId(), null, isDefault, VirtualMachine.class.getName(), vmVO.getUuid(), vm.isDisplay()); } @@ -3979,7 +3890,6 @@ private NicProfile orchestrateAddVmToNetwork(final VirtualMachine vm, final Netw } } } else if (vm.getState() == State.Stopped) { - //1) allocate nic return _networkMgr.createNicForVm(network, requested, context, vmProfile, false); } else { s_logger.warn("Unable to add vm " + vm + " to network " + network); @@ -4001,7 +3911,6 @@ public boolean removeNicFromVm(final VirtualMachine vm, final Nic nic) final AsyncJobExecutionContext jobContext = AsyncJobExecutionContext.getCurrentExecutionContext(); if (jobContext.isJobDispatchedBy(VmWorkConstants.VM_WORK_JOB_DISPATCHER)) { - // avoid re-entrance VmWorkJobVO placeHolder = null; placeHolder = createPlaceHolderWork(vm.getId()); try { @@ -4054,7 +3963,6 @@ private boolean orchestrateRemoveNicFromVm(final VirtualMachine vm, final Nic ni new NicProfile(nic, network, nic.getBroadcastUri(), nic.getIsolationUri(), _networkModel.getNetworkRate(network.getId(), vm.getId()), _networkModel.isSecurityGroupSupportedInNetwork(network), _networkModel.getNetworkTag(vmProfile.getVirtualMachine().getHypervisorType(), network)); - //1) Unplug the nic if (vm.getState() == State.Running) { final NicTO nicTO = toNicTO(nicProfile, vmProfile.getVirtualMachine().getHypervisorType()); s_logger.debug("Un-plugging nic " + nic + " for vm " + vm + " from network " + network); @@ -4074,11 +3982,9 @@ private boolean orchestrateRemoveNicFromVm(final VirtualMachine vm, final Nic ni throw new ResourceUnavailableException("Unable to remove vm " + vm + " from network, is not in the right state", DataCenter.class, vm.getDataCenterId()); } - //2) Release the nic _networkMgr.releaseNic(vmProfile, nic); s_logger.debug("Successfully released nic " + nic + "for vm " + vm); - //3) Remove the nic _networkMgr.removeNic(vmProfile, nic); _nicsDao.remove(nic.getId()); return true; @@ -4087,7 +3993,6 @@ private boolean orchestrateRemoveNicFromVm(final VirtualMachine vm, final Nic ni @Override @DB public boolean removeVmFromNetwork(final VirtualMachine vm, final Network network, final URI broadcastUri) throws ConcurrentOperationException, ResourceUnavailableException { - // TODO will serialize on the VM object later to resolve operation conflicts return orchestrateRemoveVmFromNetwork(vm, network, broadcastUri); } @@ -4117,16 +4022,13 @@ private boolean orchestrateRemoveVmFromNetwork(final VirtualMachine vm, final Ne return false; } - // don't delete default NIC on a user VM if (nic.isDefaultNic() && vm.getType() == VirtualMachine.Type.User) { s_logger.warn("Failed to remove nic from " + vm + " in " + network + ", nic is default."); throw new CloudRuntimeException("Failed to remove nic from " + vm + " in " + network + ", nic is default."); } - //Lock on nic is needed here final Nic lock = _nicsDao.acquireInLockTable(nic.getId()); if (lock == null) { - //check if nic is still there. Return if it was released already if (_nicsDao.findById(nic.getId()) == null) { if (s_logger.isDebugEnabled()) { s_logger.debug("Not need to remove the vm " + vm + " from network " + network + " as the vm doesn't have nic in this network"); @@ -4145,7 +4047,6 @@ private boolean orchestrateRemoveVmFromNetwork(final VirtualMachine vm, final Ne new NicProfile(nic, network, nic.getBroadcastUri(), nic.getIsolationUri(), _networkModel.getNetworkRate(network.getId(), vm.getId()), _networkModel.isSecurityGroupSupportedInNetwork(network), _networkModel.getNetworkTag(vmProfile.getVirtualMachine().getHypervisorType(), network)); - //1) Unplug the nic if (vm.getState() == State.Running) { final NicTO nicTO = toNicTO(nicProfile, vmProfile.getVirtualMachine().getHypervisorType()); s_logger.debug("Un-plugging nic for vm " + vm + " from network " + network); @@ -4161,11 +4062,9 @@ private boolean orchestrateRemoveVmFromNetwork(final VirtualMachine vm, final Ne throw new ResourceUnavailableException("Unable to remove vm " + vm + " from network, is not in the right state", DataCenter.class, vm.getDataCenterId()); } - //2) Release the nic _networkMgr.releaseNic(vmProfile, nic); s_logger.debug("Successfully released nic " + nic + "for vm " + vm); - //3) Remove the nic _networkMgr.removeNic(vmProfile, nic); return true; } finally { @@ -4201,7 +4100,7 @@ public void findHostAndMigrate(final String vmUuid, final Long newSvcOfferingId, final Host host = _hostDao.findById(srcHostId); final DataCenterDeployment plan = new DataCenterDeployment(host.getDataCenterId(), host.getPodId(), host.getClusterId(), null, null, null); excludes.addHost(vm.getHostId()); - vm.setServiceOfferingId(newSvcOfferingId); // Need to find the destination host based on new svc offering + vm.setServiceOfferingId(newSvcOfferingId); DeployDestination dest = null; @@ -4237,7 +4136,6 @@ public void migrateForScale(final String vmUuid, final long srcHostId, final Dep throws ResourceUnavailableException, ConcurrentOperationException { final AsyncJobExecutionContext jobContext = AsyncJobExecutionContext.getCurrentExecutionContext(); if (jobContext.isJobDispatchedBy(VmWorkConstants.VM_WORK_JOB_DISPATCHER)) { - // avoid re-entrance VmWorkJobVO placeHolder = null; final VirtualMachine vm = _vmDao.findByUuid(vmUuid); placeHolder = createPlaceHolderWork(vm.getId()); @@ -4390,7 +4288,7 @@ private void orchestrateMigrateForScale(final String vmUuid, final long srcHostI try { final long newServiceOfferingId = vm.getServiceOfferingId(); - vm.setServiceOfferingId(oldSvcOfferingId); // release capacity for the old service offering only + vm.setServiceOfferingId(oldSvcOfferingId); if (!changeState(vm, VirtualMachine.Event.OperationSucceeded, dstHostId, work, Step.Started)) { throw new ConcurrentOperationException("Unable to change the state for " + vm); } @@ -4517,7 +4415,6 @@ public boolean unplugNic(final Network network, final NicTO nic, final VirtualMa final VMInstanceVO router = _vmDao.findById(vm.getId()); if (router.getState() == State.Running) { - // collect vm network statistics before unplug a nic UserVmVO userVm = _userVmDao.findById(vm.getId()); if (userVm != null && userVm.getType() == VirtualMachine.Type.User) { _userVmService.collectVmNetworkStatistics(userVm); @@ -4559,7 +4456,6 @@ public VMInstanceVO reConfigureVm(final String vmUuid, final ServiceOffering old final AsyncJobExecutionContext jobContext = AsyncJobExecutionContext.getCurrentExecutionContext(); if (jobContext.isJobDispatchedBy(VmWorkConstants.VM_WORK_JOB_DISPATCHER)) { - // avoid re-entrance VmWorkJobVO placeHolder = null; final VirtualMachine vm = _vmDao.findByUuid(vmUuid); placeHolder = createPlaceHolderWork(vm.getId()); @@ -4633,9 +4529,9 @@ private VMInstanceVO orchestrateReConfigureVm(String vmUuid, ServiceOffering old try { if (reconfiguringOnExistingHost) { vm.setServiceOfferingId(oldServiceOffering.getId()); - _capacityMgr.releaseVmCapacity(vm, false, false, vm.getHostId()); //release the old capacity + _capacityMgr.releaseVmCapacity(vm, false, false, vm.getHostId()); vm.setServiceOfferingId(newServiceOffering.getId()); - _capacityMgr.allocateVmCapacity(vm, false); // lock the new capacity + _capacityMgr.allocateVmCapacity(vm, false); } Answer scaleVmAnswer = _agentMgr.send(vm.getHostId(), scaleVmCommand); @@ -4678,7 +4574,6 @@ private void removeCustomOfferingDetails(long vmId) { } private void saveCustomOfferingDetails(long vmId, ServiceOffering serviceOffering) { - //save the custom values to the database. Map details = userVmDetailsDao.listDetailsKeyPairs(vmId); details.put(UsageEventVO.DynamicParameters.cpuNumber.name(), serviceOffering.getCpu().toString()); details.put(UsageEventVO.DynamicParameters.cpuSpeed.name(), serviceOffering.getSpeed().toString()); @@ -4714,9 +4609,9 @@ public void setStoragePoolAllocators(final List storagePoo _storagePoolAllocators = storagePoolAllocators; } - // - // PowerState report handling for out-of-band changes and handling of left-over transitional VM states - // + /** + * PowerState report handling for out-of-band changes and handling of left-over transitional VM states + */ @MessageHandler(topic = Topics.VM_POWER_STATE) protected void HandlePowerStateReport(final String subject, final String senderAddress, final Object args) { @@ -4726,7 +4621,6 @@ protected void HandlePowerStateReport(final String subject, final String senderA final List pendingWorkJobs = _workJobDao.listPendingWorkJobs( VirtualMachine.Type.Instance, vmId); if (pendingWorkJobs.size() == 0 && !_haMgr.hasPendingHaWork(vmId)) { - // there is no pending operation job final VMInstanceVO vm = _vmDao.findById(vmId); if (vm != null) { switch (vm.getPowerState()) { @@ -4735,13 +4629,9 @@ protected void HandlePowerStateReport(final String subject, final String senderA break; case PowerOff: - case PowerReportMissing: // rigorously set to Migrating? or just do nothing until...? or create a missing state? - // for now handle in line with legacy as power off + case PowerReportMissing: handlePowerOffReportWithNoPendingJobsOnVM(vm); break; - - // PowerUnknown shouldn't be reported, it is a derived - // VM power state from host state (host un-reachable) case PowerUnknown: default: assert false; @@ -4752,22 +4642,14 @@ protected void HandlePowerStateReport(final String subject, final String senderA } } else { s_logger.info("There is pending job or HA tasks working on the VM. vm id: " + vmId + ", postpone power-change report by resetting power-change counters"); - - // reset VM power state tracking so that we won't lost signal when VM has - // been translated to _vmDao.resetVmPowerStateTracking(vmId); } } private void handlePowerOnReportWithNoPendingJobsOnVM(final VMInstanceVO vm) { - // - // 1) handle left-over transitional VM states - // 2) handle out of band VM live migration - // 3) handle out of sync stationary states, marking VM from Stopped to Running with - // alert messages - // Host host = _hostDao.findById(vm.getHostId()); Host poweredHost = _hostDao.findById(vm.getPowerHostId()); + switch (vm.getState()) { case Starting: s_logger.info("VM " + vm.getInstanceName() + " is at " + vm.getState() + " and we received a power-on report while there is no pending jobs on it"); @@ -4780,7 +4662,6 @@ private void handlePowerOnReportWithNoPendingJobsOnVM(final VMInstanceVO vm) { s_logger.info("VM " + vm.getInstanceName() + " is sync-ed to at Running state according to power-on report from hypervisor"); - // we need to alert admin or user about this risky state transition _alertMgr.sendAlert(AlertManager.AlertType.ALERT_TYPE_SYNC, vm.getDataCenterId(), vm.getPodIdToDeployIn(), VM_SYNC_ALERT_SUBJECT, "VM " + vm.getHostName() + "(" + vm.getInstanceName() + ") state is sync-ed (Starting -> Running) from out-of-context transition. VM network environment may need to be reset"); @@ -4839,10 +4720,6 @@ private void handlePowerOnReportWithNoPendingJobsOnVM(final VMInstanceVO vm) { } private void handlePowerOffReportWithNoPendingJobsOnVM(final VMInstanceVO vm) { - - // 1) handle left-over transitional VM states - // 2) handle out of sync stationary states, schedule force-stop to release resources - // switch (vm.getState()) { case Starting: case Stopping: @@ -4867,12 +4744,10 @@ private void handlePowerOffReportWithNoPendingJobsOnVM(final VMInstanceVO vm) { return; } - // not when report is missing if (PowerState.PowerOff.equals(vm.getPowerState())) { final VirtualMachineGuru vmGuru = getVmGuru(vm); final VirtualMachineProfile profile = new VirtualMachineProfileImpl(vm); if (!sendStop(vmGuru, profile, true, true)) { - // In case StopCommand fails, don't proceed further return; } else { // Release resources on StopCommand success @@ -4909,22 +4784,6 @@ private void handlePowerOffReportWithNoPendingJobsOnVM(final VMInstanceVO vm) { } private void scanStalledVMInTransitionStateOnUpHost(final long hostId) { - // - // Check VM that is stuck in Starting, Stopping, Migrating states, we won't check - // VMs in expunging state (this need to be handled specially) - // - // checking condition - // 1) no pending VmWork job - // 2) on hostId host and host is UP - // - // When host is UP, soon or later we will get a report from the host about the VM, - // however, if VM is missing from the host report (it may happen in out of band changes - // or from designed behave of XS/KVM), the VM may not get a chance to run the state-sync logic - // - // Therefore, we will scan thoses VMs on UP host based on last update timestamp, if the host is UP - // and a VM stalls for status update, we will consider them to be powered off - // (which is relatively safe to do so) - final long stallThresholdInMs = VmJobStateReportInterval.value() + (VmJobStateReportInterval.value() >> 1); final Date cutTime = new Date(DateUtil.currentGMTTime().getTime() - stallThresholdInMs); final List mostlikelyStoppedVMs = listStalledVMInTransitionStateOnUpHost(hostId, cutTime); @@ -4952,14 +4811,12 @@ private void scanStalledVMInTransitionStateOnDisconnectedHosts() { for (final Long vmId : stuckAndUncontrollableVMs) { final VMInstanceVO vm = _vmDao.findById(vmId); - // We now only alert administrator about this situation _alertMgr.sendAlert(AlertManager.AlertType.ALERT_TYPE_SYNC, vm.getDataCenterId(), vm.getPodIdToDeployIn(), VM_SYNC_ALERT_SUBJECT, "VM " + vm.getHostName() + "(" + vm.getInstanceName() + ") is stuck in " + vm.getState() + " state and its host is unreachable for too long"); } } - // VMs that in transitional state without recent power state report private List listStalledVMInTransitionStateOnUpHost(final long hostId, final Date cutTime) { final String sql = "SELECT i.* FROM vm_instance as i, host as h WHERE h.status = 'UP' " + "AND h.id = ? AND i.power_state_update_time < ? AND i.host_id = h.id " + @@ -4995,7 +4852,6 @@ private List listStalledVMInTransitionStateOnUpHost(final long hostId, fin return l; } - // VMs that in transitional state and recently have power state update private List listVMInTransitionStateWithRecentReportOnUpHost(final long hostId, final Date cutTime) { final String sql = "SELECT i.* FROM vm_instance as i, host as h WHERE h.status = 'UP' " + "AND h.id = ? AND i.power_state_update_time > ? AND i.host_id = h.id " + @@ -5061,10 +4917,6 @@ private List listStalledVMInTransitionStateOnDisconnectedHosts(final Date } } - // - // VM operation based on new sync model - // - public class VmStateSyncOutcome extends OutcomeImpl { private long _vmId; @@ -5114,10 +4966,6 @@ protected VirtualMachine retrieve() { } } - // - // TODO build a common pattern to reduce code duplication in following methods - // no time for this at current iteration - // public Outcome startVmThroughJobQueue(final String vmUuid, final Map params, final DeploymentPlan planToDeploy, final DeploymentPlanner planner) { @@ -5148,7 +4996,6 @@ public Outcome startVmThroughJobQueue(final String vmUuid, workJob.setVmInstanceId(vm.getId()); workJob.setRelated(AsyncJobExecutionContext.getOriginJobId()); - // save work context info (there are some duplications) final VmWorkStart workInfo = new VmWorkStart(callingUser.getId(), callingAccount.getId(), vm.getId(), VirtualMachineManagerImpl.VM_WORK_JOB_HANDLER); workInfo.setPlan(planToDeploy); workInfo.setParams(params); @@ -5194,7 +5041,6 @@ public Outcome stopVmThroughJobQueue(final String vmUuid, final workJob.setVmInstanceId(vm.getId()); workJob.setRelated(AsyncJobExecutionContext.getOriginJobId()); - // save work context info (there are some duplications) final VmWorkStop workInfo = new VmWorkStop(user.getId(), account.getId(), vm.getId(), VirtualMachineManagerImpl.VM_WORK_JOB_HANDLER, cleanup); workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo)); @@ -5237,7 +5083,6 @@ public Outcome rebootVmThroughJobQueue(final String vmUuid, workJob.setVmInstanceId(vm.getId()); workJob.setRelated(AsyncJobExecutionContext.getOriginJobId()); - // save work context info (there are some duplications) final VmWorkReboot workInfo = new VmWorkReboot(user.getId(), account.getId(), vm.getId(), VirtualMachineManagerImpl.VM_WORK_JOB_HANDLER, params); workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo)); @@ -5285,7 +5130,6 @@ public Outcome migrateVmThroughJobQueue(final String vmUuid, fin workJob.setVmInstanceId(vm.getId()); workJob.setRelated(AsyncJobExecutionContext.getOriginJobId()); - // save work context info (there are some duplications) final VmWorkMigrate workInfo = new VmWorkMigrate(user.getId(), account.getId(), vm.getId(), VirtualMachineManagerImpl.VM_WORK_JOB_HANDLER, srcHostId, dest); workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo)); @@ -5325,7 +5169,6 @@ public Outcome migrateVmAwayThroughJobQueue(final String vmUuid, workJob.setVmInstanceId(vm.getId()); workJob.setRelated(AsyncJobExecutionContext.getOriginJobId()); - // save work context info (there are some duplications) final VmWorkMigrateAway workInfo = new VmWorkMigrateAway(user.getId(), account.getId(), vm.getId(), VirtualMachineManagerImpl.VM_WORK_JOB_HANDLER, srcHostId); workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo)); } @@ -5368,7 +5211,6 @@ public Outcome migrateVmWithStorageThroughJobQueue( workJob.setVmInstanceId(vm.getId()); workJob.setRelated(AsyncJobExecutionContext.getOriginJobId()); - // save work context info (there are some duplications) final VmWorkMigrateWithStorage workInfo = new VmWorkMigrateWithStorage(user.getId(), account.getId(), vm.getId(), VirtualMachineManagerImpl.VM_WORK_JOB_HANDLER, srcHostId, destHostId, volumeToPool); workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo)); @@ -5411,7 +5253,6 @@ public Outcome migrateVmForScaleThroughJobQueue( workJob.setVmInstanceId(vm.getId()); workJob.setRelated(AsyncJobExecutionContext.getOriginJobId()); - // save work context info (there are some duplications) final VmWorkMigrateForScale workInfo = new VmWorkMigrateForScale(user.getId(), account.getId(), vm.getId(), VirtualMachineManagerImpl.VM_WORK_JOB_HANDLER, srcHostId, dest, newSvcOfferingId); workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo)); @@ -5470,7 +5311,6 @@ public Outcome migrateVmStorageThroughJobQueue( workJob.setVmInstanceId(vm.getId()); workJob.setRelated(AsyncJobExecutionContext.getOriginJobId()); - // save work context info (there are some duplications) final VmWorkStorageMigration workInfo = new VmWorkStorageMigration(user.getId(), account.getId(), vm.getId(), VirtualMachineManagerImpl.VM_WORK_JOB_HANDLER, volumeToPool); workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo)); @@ -5510,7 +5350,6 @@ public Outcome addVmToNetworkThroughJobQueue( workJob.setVmInstanceId(vm.getId()); workJob.setRelated(AsyncJobExecutionContext.getOriginJobId()); - // save work context info (there are some duplications) final VmWorkAddVmToNetwork workInfo = new VmWorkAddVmToNetwork(user.getId(), account.getId(), vm.getId(), VirtualMachineManagerImpl.VM_WORK_JOB_HANDLER, network.getId(), requested); workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo)); @@ -5550,7 +5389,6 @@ public Outcome removeNicFromVmThroughJobQueue( workJob.setVmInstanceId(vm.getId()); workJob.setRelated(AsyncJobExecutionContext.getOriginJobId()); - // save work context info (there are some duplications) final VmWorkRemoveNicFromVm workInfo = new VmWorkRemoveNicFromVm(user.getId(), account.getId(), vm.getId(), VirtualMachineManagerImpl.VM_WORK_JOB_HANDLER, nic.getId()); workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo)); @@ -5590,7 +5428,6 @@ public Outcome removeVmFromNetworkThroughJobQueue( workJob.setVmInstanceId(vm.getId()); workJob.setRelated(AsyncJobExecutionContext.getOriginJobId()); - // save work context info (there are some duplications) final VmWorkRemoveVmFromNetwork workInfo = new VmWorkRemoveVmFromNetwork(user.getId(), account.getId(), vm.getId(), VirtualMachineManagerImpl.VM_WORK_JOB_HANDLER, network, broadcastUri); workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo)); @@ -5633,7 +5470,6 @@ public Outcome reconfigureVmThroughJobQueue( workJob.setVmInstanceId(vm.getId()); workJob.setRelated(AsyncJobExecutionContext.getOriginJobId()); - // save work context info (there are some duplications) final VmWorkReconfigure workInfo = new VmWorkReconfigure(user.getId(), account.getId(), vm.getId(), VirtualMachineManagerImpl.VM_WORK_JOB_HANDLER, oldServiceOffering.getId(), newServiceOffering.getId(), customParameters, reconfiguringOnExistingHost); workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo)); @@ -5802,7 +5638,6 @@ private Pair orchestrateReconfigure(final VmWorkReconfig ServiceOfferingVO oldServiceOffering = _offeringDao.findById(work.getOldServiceOfferingId()); ServiceOfferingVO newServiceOffering = _offeringDao.findById(work.getNewServiceOfferingId()); if (newServiceOffering.isDynamic()) { - // update the service offering object with the custom parameters like cpu, memory newServiceOffering = _offeringDao.getComputeOffering(newServiceOffering, work.getCustomParameters()); } From 54bb0772629683118cfa41f88dd3b07686e82729 Mon Sep 17 00:00:00 2001 From: Daniel Augusto Veronezi Salvador Date: Tue, 13 Apr 2021 19:34:19 -0300 Subject: [PATCH 03/25] Use diamond inference --- .../cloud/vm/VirtualMachineManagerImpl.java | 58 +++++++++---------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java b/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java index e523d78c8ff6..4411f567e767 100755 --- a/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java +++ b/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java @@ -369,7 +369,7 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac VmWorkJobHandlerProxy _jobHandlerProxy = new VmWorkJobHandlerProxy(this); - Map _vmGurus = new HashMap(); + Map _vmGurus = new HashMap<>(); protected StateMachine2 _stateMachine; static final ConfigKey StartRetry = new ConfigKey("Advanced", Integer.class, "start.retry", "10", @@ -511,7 +511,7 @@ public void doInTransactionWithoutResult(final TransactionStatus status) throws @Override public void allocate(final String vmInstanceName, final VirtualMachineTemplate template, final ServiceOffering serviceOffering, final LinkedHashMap> networks, final DeploymentPlan plan, final HypervisorType hyperType) throws InsufficientCapacityException { - allocate(vmInstanceName, template, serviceOffering, new DiskOfferingInfo(serviceOffering), new ArrayList(), networks, plan, hyperType, null, null); + allocate(vmInstanceName, template, serviceOffering, new DiskOfferingInfo(serviceOffering), new ArrayList<>(), networks, plan, hyperType, null, null); } private VirtualMachineGuru getVmGuru(final VirtualMachine vm) { @@ -831,10 +831,10 @@ public Ternary doInTransaction(final if (s_logger.isDebugEnabled()) { s_logger.debug("Successfully transitioned to start state for " + vm + " reservation id = " + work.getId()); } - return new Ternary(vm, context, work); + return new Ternary<>(vm, context, work); } - return new Ternary(null, null, work); + return new Ternary<>(null, null, work); } }); @@ -1254,7 +1254,7 @@ public void orchestrateStart(final String vmUuid, final Map vmmetadata = new HashMap(); + final Map vmmetadata = new HashMap<>(); vmmetadata.put(vm.getInstanceName(), platform); syncVMMetaData(vmmetadata); } @@ -2088,7 +2088,7 @@ private void setStateMachine() { protected boolean stateTransitTo(final VMInstanceVO vm, final VirtualMachine.Event e, final Long hostId, final String reservationId) throws NoTransitionException { vm.setReservationId(reservationId); - return _stateMachine.transitTo(vm, e, new Pair(vm.getHostId(), hostId), _vmDao); + return _stateMachine.transitTo(vm, e, new Pair<>(vm.getHostId(), hostId), _vmDao); } @Override @@ -2105,7 +2105,7 @@ public boolean stateTransitTo(final VirtualMachine vm1, final VirtualMachine.Eve vm.setLastHostId(vm.getHostId()); } } - return _stateMachine.transitTo(vm, e, new Pair(vm.getHostId(), hostId), _vmDao); + return _stateMachine.transitTo(vm, e, new Pair<>(vm.getHostId(), hostId), _vmDao); } @Override @@ -3417,7 +3417,7 @@ private void orchestrateReboot(final String vmUuid, final Map affectedVms = new ArrayList(); + List affectedVms = new ArrayList<>(); affectedVms.add(vm.getId()); _securityGroupManager.scheduleRulesetUpdateToHosts(affectedVms, true, null); } @@ -4565,7 +4565,7 @@ private void removeCustomOfferingDetails(long vmId) { details.remove(UsageEventVO.DynamicParameters.cpuNumber.name()); details.remove(UsageEventVO.DynamicParameters.cpuSpeed.name()); details.remove(UsageEventVO.DynamicParameters.memory.name()); - List detailList = new ArrayList(); + List detailList = new ArrayList<>(); for(Map.Entry entry: details.entrySet()) { UserVmDetailVO detailVO = new UserVmDetailVO(vmId, entry.getKey(), entry.getValue(), true); detailList.add(detailVO); @@ -4578,7 +4578,7 @@ private void saveCustomOfferingDetails(long vmId, ServiceOffering serviceOfferin details.put(UsageEventVO.DynamicParameters.cpuNumber.name(), serviceOffering.getCpu().toString()); details.put(UsageEventVO.DynamicParameters.cpuSpeed.name(), serviceOffering.getSpeed().toString()); details.put(UsageEventVO.DynamicParameters.memory.name(), serviceOffering.getRamSize().toString()); - List detailList = new ArrayList(); + List detailList = new ArrayList<>(); for (Map.Entry entry: details.entrySet()) { UserVmDetailVO detailVO = new UserVmDetailVO(vmId, entry.getKey(), entry.getValue(), true); detailList.add(detailVO); @@ -4824,7 +4824,7 @@ private List listStalledVMInTransitionStateOnUpHost(final long hostId, fin "AND i.id NOT IN (SELECT w.vm_instance_id FROM vm_work_job AS w JOIN async_job AS j ON w.id = j.id WHERE j.job_status = ?)" + "AND i.removed IS NULL"; - final List l = new ArrayList(); + final List l = new ArrayList<>(); TransactionLegacy txn = null; try { txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); @@ -4859,7 +4859,7 @@ private List listVMInTransitionStateWithRecentReportOnUpHost(final long ho "AND i.id NOT IN (SELECT w.vm_instance_id FROM vm_work_job AS w JOIN async_job AS j ON w.id = j.id WHERE j.job_status = ?)" + "AND i.removed IS NULL"; - final List l = new ArrayList(); + final List l = new ArrayList<>(); TransactionLegacy txn = null; try { txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); @@ -4892,7 +4892,7 @@ private List listStalledVMInTransitionStateOnDisconnectedHosts(final Date "AND i.id NOT IN (SELECT w.vm_instance_id FROM vm_work_job AS w JOIN async_job AS j ON w.id = j.id WHERE j.job_status = ?)" + "AND i.removed IS NULL"; - final List l = new ArrayList(); + final List l = new ArrayList<>(); TransactionLegacy txn = null; try { txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); @@ -5500,9 +5500,9 @@ private Pair orchestrateStart(final VmWorkStart work) th String message = String.format("Unable to orchestrate start %s due to [%s].", vm.toString(), e.getMessage()); s_logger.warn(message, e); CloudRuntimeException ex = new CloudRuntimeException(message); - return new Pair(JobInfo.Status.FAILED, JobSerializerHelper.toObjectSerializedString(ex)); + return new Pair<>(JobInfo.Status.FAILED, JobSerializerHelper.toObjectSerializedString(ex)); } - return new Pair(JobInfo.Status.SUCCEEDED, null); + return new Pair<>(JobInfo.Status.SUCCEEDED, null); } @ReflectionUse @@ -5515,7 +5515,7 @@ private Pair orchestrateStop(final VmWorkStop work) thro } orchestrateStop(vm.getUuid(), work.isCleanup()); - return new Pair(JobInfo.Status.SUCCEEDED, null); + return new Pair<>(JobInfo.Status.SUCCEEDED, null); } @ReflectionUse @@ -5527,7 +5527,7 @@ private Pair orchestrateMigrate(final VmWorkMigrate work assert vm != null; orchestrateMigrate(vm.getUuid(), work.getSrcHostId(), work.getDeployDestination()); - return new Pair(JobInfo.Status.SUCCEEDED, null); + return new Pair<>(JobInfo.Status.SUCCEEDED, null); } @ReflectionUse @@ -5545,7 +5545,7 @@ private Pair orchestrateMigrateAway(final VmWorkMigrateA orchestrateMigrateAway(vm.getUuid(), work.getSrcHostId(), _haMgr.getHAPlanner()); } - return new Pair(JobInfo.Status.SUCCEEDED, null); + return new Pair<>(JobInfo.Status.SUCCEEDED, null); } @ReflectionUse @@ -5559,7 +5559,7 @@ private Pair orchestrateMigrateWithStorage(final VmWorkM work.getSrcHostId(), work.getDestHostId(), work.getVolumeToPool()); - return new Pair(JobInfo.Status.SUCCEEDED, null); + return new Pair<>(JobInfo.Status.SUCCEEDED, null); } @ReflectionUse @@ -5573,7 +5573,7 @@ private Pair orchestrateMigrateForScale(final VmWorkMigr work.getSrcHostId(), work.getDeployDestination(), work.getNewServiceOfferringId()); - return new Pair(JobInfo.Status.SUCCEEDED, null); + return new Pair<>(JobInfo.Status.SUCCEEDED, null); } @ReflectionUse @@ -5584,7 +5584,7 @@ private Pair orchestrateReboot(final VmWorkReboot work) } assert vm != null; orchestrateReboot(vm.getUuid(), work.getParams()); - return new Pair(JobInfo.Status.SUCCEEDED, null); + return new Pair<>(JobInfo.Status.SUCCEEDED, null); } @ReflectionUse @@ -5599,7 +5599,7 @@ private Pair orchestrateAddVmToNetwork(final VmWorkAddVm final NicProfile nic = orchestrateAddVmToNetwork(vm, network, work.getRequestedNicProfile()); - return new Pair(JobInfo.Status.SUCCEEDED, _jobMgr.marshallResultObject(nic)); + return new Pair<>(JobInfo.Status.SUCCEEDED, _jobMgr.marshallResultObject(nic)); } @ReflectionUse @@ -5611,7 +5611,7 @@ private Pair orchestrateRemoveNicFromVm(final VmWorkRemo assert vm != null; final NicVO nic = _entityMgr.findById(NicVO.class, work.getNicId()); final boolean result = orchestrateRemoveNicFromVm(vm, nic); - return new Pair(JobInfo.Status.SUCCEEDED, + return new Pair<>(JobInfo.Status.SUCCEEDED, _jobMgr.marshallResultObject(result)); } @@ -5624,7 +5624,7 @@ private Pair orchestrateRemoveVmFromNetwork(final VmWork assert vm != null; final boolean result = orchestrateRemoveVmFromNetwork(vm, work.getNetwork(), work.getBroadcastUri()); - return new Pair(JobInfo.Status.SUCCEEDED, + return new Pair<>(JobInfo.Status.SUCCEEDED, _jobMgr.marshallResultObject(result)); } @@ -5643,7 +5643,7 @@ private Pair orchestrateReconfigure(final VmWorkReconfig reConfigureVm(vm.getUuid(), oldServiceOffering, newServiceOffering, work.getCustomParameters(), work.isSameHost()); - return new Pair(JobInfo.Status.SUCCEEDED, null); + return new Pair<>(JobInfo.Status.SUCCEEDED, null); } @ReflectionUse @@ -5655,7 +5655,7 @@ private Pair orchestrateStorageMigration(final VmWorkSto assert vm != null; orchestrateStorageMigration(vm.getUuid(), work.getVolumeToPool()); - return new Pair(JobInfo.Status.SUCCEEDED, null); + return new Pair<>(JobInfo.Status.SUCCEEDED, null); } @Override @@ -5786,9 +5786,9 @@ private Pair orchestrateRestoreVirtualMachine(final VmWo } assert vm != null; UserVm uservm = orchestrateRestoreVirtualMachine(vm.getId(), work.getTemplateId()); - HashMap passwordMap = new HashMap(); + HashMap passwordMap = new HashMap<>(); passwordMap.put(uservm.getId(), uservm.getPassword()); - return new Pair(JobInfo.Status.SUCCEEDED, _jobMgr.marshallResultObject(passwordMap)); + return new Pair<>(JobInfo.Status.SUCCEEDED, _jobMgr.marshallResultObject(passwordMap)); } @Override @@ -5893,7 +5893,7 @@ private Pair orchestrateUpdateDefaultNic(final VmWorkUpd throw new CloudRuntimeException("Unable to find default nic " + work.getDefaultNicId()); } final boolean result = orchestrateUpdateDefaultNicForVM(vm, nic, defaultNic); - return new Pair(JobInfo.Status.SUCCEEDED, + return new Pair<>(JobInfo.Status.SUCCEEDED, _jobMgr.marshallResultObject(result)); } From a17df556d49cef2636ad9759dea4d8ca6029b094 Mon Sep 17 00:00:00 2001 From: Daniel Augusto Veronezi Salvador Date: Tue, 13 Apr 2021 19:45:19 -0300 Subject: [PATCH 04/25] Fix some logs --- .../main/java/com/cloud/vm/VirtualMachineManagerImpl.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java b/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java index 4411f567e767..b70ef0c21da5 100755 --- a/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java +++ b/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java @@ -2025,7 +2025,7 @@ private void advanceStop(final VMInstanceVO vm, final boolean cleanUpEvenIfUnabl } } catch (AgentUnavailableException | OperationTimedoutException e) { - s_logger.warn(String.format("Unable to stop % due to [%s].", profile.toString(), e.toString()), e); + s_logger.warn(String.format("Unable to stop %s due to [%s].", profile.toString(), e.toString()), e); } finally { if (!stopped) { if (!cleanUpEvenIfUnableToStop) { @@ -2191,7 +2191,7 @@ protected boolean checkVmOnHost(final VirtualMachine vm, final long hostId) thro if (command != null) { RestoreVMSnapshotAnswer restoreVMSnapshotAnswer = (RestoreVMSnapshotAnswer) _agentMgr.send(hostId, command); if (restoreVMSnapshotAnswer == null || !restoreVMSnapshotAnswer.getResult()) { - s_logger.warn("Unable to restore the vm snapshot from image file after live migration of vm with vmsnapshots: " + restoreVMSnapshotAnswer.getDetails()); + s_logger.warn("Unable to restore the vm snapshot from image file after live migration of vm with vmsnapshots: " + restoreVMSnapshotAnswer == null ? "null answer" : restoreVMSnapshotAnswer.getDetails()); } } } @@ -5509,7 +5509,7 @@ private Pair orchestrateStart(final VmWorkStart work) th private Pair orchestrateStop(final VmWorkStop work) throws Exception { final VMInstanceVO vm = _entityMgr.findById(VMInstanceVO.class, work.getVmId()); if (vm == null) { - String message = String.format("Unable to find %s.", vm.toString()); + String message = String.format("Unable to find VM [%s].", work.getVmId()); s_logger.warn(message); throw new CloudRuntimeException(message); } From 7a70e886910c8a4f1ab6494dc0d89a0891a45db6 Mon Sep 17 00:00:00 2001 From: Daniel Augusto Veronezi Salvador Date: Tue, 13 Apr 2021 19:49:49 -0300 Subject: [PATCH 05/25] Remove unnecessary unboxing --- .../java/com/cloud/vm/VirtualMachineManagerImpl.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java b/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java index b70ef0c21da5..499aab694913 100755 --- a/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java +++ b/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java @@ -1051,7 +1051,7 @@ public void orchestrateStart(final String vmUuid, final Map vols = _volsDao.findReadyRootVolumesByInstance(vm.getId()); for (final VolumeVO vol : vols) { final Long volTemplateId = vol.getTemplateId(); - if (volTemplateId != null && volTemplateId.longValue() != template.getId()) { + if (volTemplateId != null && volTemplateId != template.getId()) { if (s_logger.isDebugEnabled()) { s_logger.debug(vol + " of " + vm + " is READY, but template ids don't match, let the planner reassign a new pool"); } @@ -1069,7 +1069,7 @@ public void orchestrateStart(final String vmUuid, final Map volumes = _volsDao.findCreatedByInstance(vm.getId()); for (final VolumeVO volume : volumes) { if (!_storagePoolDao.findById(volume.getPoolId()).getScope().equals(ScopeType.ZONE)) { @@ -4669,8 +4669,8 @@ private void handlePowerOnReportWithNoPendingJobsOnVM(final VMInstanceVO vm) { case Running: try { - if (vm.getHostId() != null && vm.getHostId().longValue() != vm.getPowerHostId().longValue()) { - s_logger.info(String.format("Detected out of band VM migration from %s to %s", host, poweredHost)); + if (vm.getHostId() != null && !vm.getHostId().equals(vm.getPowerHostId())) { + s_logger.info("Detected out of band VM migration from host " + vm.getHostId() + " to host " + vm.getPowerHostId()); } stateTransitTo(vm, VirtualMachine.Event.FollowAgentPowerOnReport, vm.getPowerHostId()); } catch (final NoTransitionException e) { From 5af8f72303990bc6957aba67650bde7ae3a43d80 Mon Sep 17 00:00:00 2001 From: Daniel Augusto Veronezi Salvador Date: Tue, 13 Apr 2021 20:52:17 -0300 Subject: [PATCH 06/25] Create method to handle job result --- .../cloud/vm/VirtualMachineManagerImpl.java | 243 +++++++----------- 1 file changed, 92 insertions(+), 151 deletions(-) diff --git a/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java b/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java index 499aab694913..7ea2a43e55f7 100755 --- a/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java +++ b/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java @@ -948,20 +948,7 @@ public void advanceStart(final String vmUuid, final Map volumeTo retrieveVmFromJobOutcome(outcome, vmUuid, "migrateVmStorage"); - final Object jobResult = _jobMgr.unmarshallResultObject(outcome.getJob()); - if (jobResult != null) { - if (jobResult instanceof RuntimeException) { - throw (RuntimeException)jobResult; - } else if (jobResult instanceof Throwable) { - throw new RuntimeException("Unexpected exception", (Throwable)jobResult); - } + try { + retrieveResultFromJobOutcomeAndThrowExceptionIfNeeded(outcome); + } catch (ResourceUnavailableException | InsufficientCapacityException ex) { + throw new RuntimeException("Unexpected exception", ex); } } } @@ -2526,18 +2501,10 @@ public void migrate(final String vmUuid, final long srcHostId, final DeployDesti retrieveVmFromJobOutcome(outcome, vmUuid, "migrateVm"); - final Object jobResult = _jobMgr.unmarshallResultObject(outcome.getJob()); - if (jobResult != null) { - if (jobResult instanceof ResourceUnavailableException) { - throw (ResourceUnavailableException)jobResult; - } else if (jobResult instanceof ConcurrentOperationException) { - throw (ConcurrentOperationException)jobResult; - } else if (jobResult instanceof RuntimeException) { - throw (RuntimeException)jobResult; - } else if (jobResult instanceof Throwable) { - throw new RuntimeException("Unexpected exception", (Throwable)jobResult); - } - + try { + retrieveResultFromJobOutcomeAndThrowExceptionIfNeeded(outcome); + } catch (InsufficientCapacityException ex) { + throw new RuntimeException("Unexpected exception", ex); } } } @@ -2994,17 +2961,10 @@ public void migrateWithStorage(final String vmUuid, final long srcHostId, final retrieveVmFromJobOutcome(outcome, vmUuid, "migrateVmWithStorage"); - final Object jobException = _jobMgr.unmarshallResultObject(outcome.getJob()); - if (jobException != null) { - if (jobException instanceof ResourceUnavailableException) { - throw (ResourceUnavailableException)jobException; - } else if (jobException instanceof ConcurrentOperationException) { - throw (ConcurrentOperationException)jobException; - } else if (jobException instanceof RuntimeException) { - throw (RuntimeException)jobException; - } else if (jobException instanceof Throwable) { - throw new RuntimeException("Unexpected exception", (Throwable)jobException); - } + try { + retrieveResultFromJobOutcomeAndThrowExceptionIfNeeded(outcome); + } catch (InsufficientCapacityException ex) { + throw new RuntimeException("Unexpected exception", ex); } } } @@ -3216,17 +3176,10 @@ public void migrateAway(final String vmUuid, final long srcHostId) throws Insuff retrieveVmFromJobOutcome(outcome, vmUuid, "migrateVmAway"); - final Object jobException = _jobMgr.unmarshallResultObject(outcome.getJob()); - if (jobException != null) { - if (jobException instanceof InsufficientServerCapacityException) { - throw (InsufficientServerCapacityException)jobException; - } else if (jobException instanceof ConcurrentOperationException) { - throw (ConcurrentOperationException)jobException; - } else if (jobException instanceof RuntimeException) { - throw (RuntimeException)jobException; - } else if (jobException instanceof Throwable) { - throw new RuntimeException("Unexpected exception", (Throwable)jobException); - } + try { + retrieveResultFromJobOutcomeAndThrowExceptionIfNeeded(outcome); + } catch (ResourceUnavailableException | InsufficientCapacityException ex) { + throw new RuntimeException("Unexpected exception", ex); } } } @@ -3371,20 +3324,7 @@ public void advanceReboot(final String vmUuid, final Map passwordMap = (HashMap)jobResult; - UserVmVO userVm = _userVmDao.findById(vmId); - userVm.setPassword(passwordMap.get(vmId)); - return userVm; - } + Object jobResult = retrieveResultFromJobOutcomeAndThrowExceptionIfNeeded(outcome); + + if (jobResult != null && jobResult instanceof HashMap) { + HashMap passwordMap = (HashMap)jobResult; + UserVmVO userVm = _userVmDao.findById(vmId); + userVm.setPassword(passwordMap.get(vmId)); + return userVm; } + throw new RuntimeException("Unexpected job execution result"); } } @@ -5810,11 +5710,14 @@ public Boolean updateDefaultNicForVM(final VirtualMachine vm, final Nic nic, fin retrieveVmFromJobOutcome(outcome, vm.getUuid(), "updateDefaultNicForVM"); - final Object jobResult = _jobMgr.unmarshallResultObject(outcome.getJob()); - if (jobResult != null) { - if (jobResult instanceof Boolean) { + try { + Object jobResult = retrieveResultFromJobOutcomeAndThrowExceptionIfNeeded(outcome); + + if (jobResult != null && jobResult instanceof Boolean) { return (Boolean)jobResult; } + } catch (ResourceUnavailableException | InsufficientCapacityException ex) { + throw new RuntimeException("Unhandled exception", ex); } throw new RuntimeException("Unexpected job execution result"); @@ -5954,4 +5857,42 @@ protected VirtualMachine retrieveVmFromJobOutcome(Outcome jobOut throw new RuntimeException(String.format("Unable to retrieve result from job \"%s\" due to [%s]. VM {\"uuid\": \"%s\"}.", jobName, e.getMessage(), vmUuid), e); } } + + protected Object retrieveResultFromJobOutcomeAndThrowExceptionIfNeeded(Outcome outcome) throws ResourceUnavailableException, InsufficientCapacityException{ + Object jobResult = _jobMgr.unmarshallResultObject(outcome.getJob()); + + if (jobResult == null) { + return null; + } + + if (jobResult instanceof AgentUnavailableException) { + throw (AgentUnavailableException) jobResult; + } + + if (jobResult instanceof InsufficientServerCapacityException) { + throw (InsufficientServerCapacityException) jobResult; + } + + if (jobResult instanceof ResourceUnavailableException) { + throw (ResourceUnavailableException) jobResult; + } + + if (jobResult instanceof InsufficientCapacityException) { + throw (InsufficientCapacityException) jobResult; + } + + if (jobResult instanceof ConcurrentOperationException) { + throw (ConcurrentOperationException) jobResult; + } + + if (jobResult instanceof RuntimeException) { + throw (RuntimeException) jobResult; + } + + if (jobResult instanceof Throwable) { + throw new RuntimeException("Unexpected exception", (Throwable)jobResult); + } + + return jobResult; + } } From ad7347e561cde87fa0e6531850c1a2b94e2a3c24 Mon Sep 17 00:00:00 2001 From: Daniel Augusto Veronezi Salvador Date: Tue, 13 Apr 2021 21:34:12 -0300 Subject: [PATCH 07/25] Remove unused vars and fix some logics --- .../cloud/vm/VirtualMachineManagerImpl.java | 209 +++++++----------- 1 file changed, 83 insertions(+), 126 deletions(-) diff --git a/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java b/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java index 7ea2a43e55f7..3920c5b8e2fc 100755 --- a/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java +++ b/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java @@ -58,7 +58,6 @@ import org.apache.cloudstack.framework.ca.Certificate; import org.apache.cloudstack.framework.config.ConfigKey; import org.apache.cloudstack.framework.config.Configurable; -import org.apache.cloudstack.framework.config.dao.ConfigurationDao; import org.apache.cloudstack.framework.jobs.AsyncJob; import org.apache.cloudstack.framework.jobs.AsyncJobExecutionContext; import org.apache.cloudstack.framework.jobs.AsyncJobManager; @@ -337,8 +336,6 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac @Inject private UserVmDetailsDao userVmDetailsDao; @Inject - private ConfigurationDao _configDao; - @Inject private VolumeOrchestrationService volumeMgr; @Inject private DeploymentPlanningManager _dpMgr; @@ -476,9 +473,7 @@ public void doInTransactionWithoutResult(final TransactionStatus status) throws if (template.getFormat() == ImageFormat.ISO) { volumeMgr.allocateRawVolume(Type.ROOT, "ROOT-" + vmFinal.getId(), rootDiskOfferingInfo.getDiskOffering(), rootDiskOfferingInfo.getSize(), rootDiskOfferingInfo.getMinIops(), rootDiskOfferingInfo.getMaxIops(), vmFinal, template, owner, null); - } else if (template.getFormat() == ImageFormat.BAREMETAL) { - - } else { + } else if (template.getFormat() != ImageFormat.BAREMETAL) { volumeMgr.allocateTemplatedVolumes(Type.ROOT, "ROOT-" + vmFinal.getId(), rootDiskOfferingInfo.getDiskOffering(), rootDiskOfferingInfo.getSize(), rootDiskOfferingInfo.getMinIops(), rootDiskOfferingInfo.getMaxIops(), template, vmFinal, owner); } @@ -904,10 +899,7 @@ protected boolean areAffinityGroupsAssociated(final VirtualMachineProfile vmProf final VirtualMachine vm = vmProfile.getVirtualMachine(); final long vmGroupCount = _affinityGroupVMMapDao.countAffinityGroupsForVm(vm.getId()); - if (vmGroupCount > 0) { - return true; - } - return false; + return vmGroupCount > 0; } @Override @@ -928,9 +920,8 @@ public void advanceStart(final String vmUuid, final Map":params.get(VirtualMachineProfile.Param.BootIntoSetup)))); } - VmWorkJobVO placeHolder = null; final VirtualMachine vm = _vmDao.findByUuid(vmUuid); - placeHolder = createPlaceHolderWork(vm.getId()); + VmWorkJobVO placeHolder = createPlaceHolderWork(vm.getId()); try { orchestrateStart(vmUuid, params, planToDeploy, planner); } finally { @@ -1102,11 +1093,9 @@ public void orchestrateStart(final String vmUuid, final Map para if (params == null) { return; } - StringBuffer msgBuf = new StringBuffer("Uefi params "); + + StringBuilder msgBuf = new StringBuilder("Uefi params "); boolean log = false; if (params.get(VirtualMachineProfile.Param.UefiFlag) != null) { msgBuf.append(String.format("UefiFlag: %s ", params.get(VirtualMachineProfile.Param.UefiFlag))); @@ -1502,14 +1491,22 @@ public void stopForced(String vmUuid) throws ResourceUnavailableException { @Override public boolean getExecuteInSequence(final HypervisorType hypervisorType) { - if (HypervisorType.KVM == hypervisorType || HypervisorType.XenServer == hypervisorType || HypervisorType.Hyperv == hypervisorType || HypervisorType.LXC == hypervisorType) { - return false; - } else if (HypervisorType.VMware == hypervisorType) { - final Boolean fullClone = HypervisorGuru.VmwareFullClone.value(); - return fullClone; - } else { + if (null == hypervisorType) { return ExecuteInSequence.value(); } + + switch (hypervisorType) { + case KVM: + case XenServer: + case Hyperv: + case LXC: + return false; + case VMware: + final Boolean fullClone = HypervisorGuru.VmwareFullClone.value(); + return fullClone; + default: + return ExecuteInSequence.value(); + } } @Override @@ -1535,7 +1532,6 @@ public Boolean doInTransaction(TransactionStatus status) { } final VirtualMachineProfile profile = new VirtualMachineProfileImpl(vm); - final HypervisorGuru hvGuru = _hvGuruMgr.getGuru(vm.getHypervisorType()); final VirtualMachineGuru guru = getVmGuru(vm); try { @@ -2182,9 +2178,8 @@ public void storageMigration(final String vmUuid, final Map volumeTo final AsyncJobExecutionContext jobContext = AsyncJobExecutionContext.getCurrentExecutionContext(); if (jobContext.isJobDispatchedBy(VmWorkConstants.VM_WORK_JOB_DISPATCHER)) { - VmWorkJobVO placeHolder = null; final VirtualMachine vm = _vmDao.findByUuid(vmUuid); - placeHolder = createPlaceHolderWork(vm.getId()); + VmWorkJobVO placeHolder = createPlaceHolderWork(vm.getId()); try { orchestrateStorageMigration(vmUuid, volumeToPool); } finally { @@ -2486,9 +2481,8 @@ public void migrate(final String vmUuid, final long srcHostId, final DeployDesti final AsyncJobExecutionContext jobContext = AsyncJobExecutionContext.getCurrentExecutionContext(); if (jobContext.isJobDispatchedBy(VmWorkConstants.VM_WORK_JOB_DISPATCHER)) { - VmWorkJobVO placeHolder = null; final VirtualMachine vm = _vmDao.findByUuid(vmUuid); - placeHolder = createPlaceHolderWork(vm.getId()); + VmWorkJobVO placeHolder = createPlaceHolderWork(vm.getId()); try { orchestrateMigrate(vmUuid, srcHostId, dest); } finally { @@ -2522,7 +2516,6 @@ private void orchestrateMigrate(final String vmUuid, final long srcHostId, final protected void migrate(final VMInstanceVO vm, final long srcHostId, final DeployDestination dest) throws ResourceUnavailableException, ConcurrentOperationException { s_logger.info("Migrating " + vm + " to " + dest); - final UserVmVO userVm = _userVmDao.findById(vm.getId()); final long dstHostId = dest.getHost().getId(); final Host fromHost = _hostDao.findById(srcHostId); if (fromHost == null) { @@ -2603,7 +2596,7 @@ protected void migrate(final VMInstanceVO vm, final long srcHostId, final Deploy vm.setLastHostId(srcHostId); try { - if (vm == null || vm.getHostId() == null || vm.getHostId() != srcHostId || !changeState(vm, Event.MigrationRequested, dstHostId, work, Step.Migrating)) { + if (vm.getHostId() == null || vm.getHostId() != srcHostId || !changeState(vm, Event.MigrationRequested, dstHostId, work, Step.Migrating)) { _networkMgr.rollbackNicForMigration(vmSrc, profile); if (vm != null) { volumeMgr.release(vm.getId(), dstHostId); @@ -2944,10 +2937,8 @@ public void migrateWithStorage(final String vmUuid, final long srcHostId, final final AsyncJobExecutionContext jobContext = AsyncJobExecutionContext.getCurrentExecutionContext(); if (jobContext.isJobDispatchedBy(VmWorkConstants.VM_WORK_JOB_DISPATCHER)) { - - VmWorkJobVO placeHolder = null; final VirtualMachine vm = _vmDao.findByUuid(vmUuid); - placeHolder = createPlaceHolderWork(vm.getId()); + VmWorkJobVO placeHolder = createPlaceHolderWork(vm.getId()); try { orchestrateMigrateWithStorage(vmUuid, srcHostId, destHostId, volumeToPool); } finally { @@ -2955,7 +2946,6 @@ public void migrateWithStorage(final String vmUuid, final long srcHostId, final _workJobDao.expunge(placeHolder.getId()); } } - } else { final Outcome outcome = migrateVmWithStorageThroughJobQueue(vmUuid, srcHostId, destHostId, volumeToPool); @@ -3020,7 +3010,6 @@ private void orchestrateMigrateWithStorage(final String vmUuid, final long srcHo vm.setLastHostId(srcHostId); vm.setPodIdToDeployIn(destHost.getPodId()); moveVmToMigratingState(vm, destHostId, work); - List vmData = null; boolean migrated = false; try { @@ -3035,7 +3024,7 @@ private void orchestrateMigrateWithStorage(final String vmUuid, final long srcHo if (_networkModel.isSharedNetworkWithoutServices(network.getId())) { final String serviceOffering = _serviceOfferingDao.findByIdIncludingRemoved(vm.getId(), vm.getServiceOfferingId()).getDisplayText(); boolean isWindows = _guestOSCategoryDao.findById(_guestOSDao.findById(vm.getGuestOSId()).getCategoryId()).getName().equalsIgnoreCase("Windows"); - vmData = _networkModel.generateVmData(userVm.getUserData(), serviceOffering, vm.getDataCenterId(), vm.getInstanceName(), vm.getHostName(), vm.getId(), + List vmData = _networkModel.generateVmData(userVm.getUserData(), serviceOffering, vm.getDataCenterId(), vm.getInstanceName(), vm.getHostName(), vm.getId(), vm.getUuid(), defaultNic.getMacAddress(), userVm.getDetail("SSH.PublicKey"), (String) profile.getParameter(VirtualMachineProfile.Param.VmPassword), isWindows, VirtualMachineManager.getHypervisorHostname(destination.getHost() != null ? destination.getHost().getName() : "")); String vmName = vm.getInstanceName(); @@ -3158,9 +3147,8 @@ protected void cancelWorkItems(final long nodeId) { public void migrateAway(final String vmUuid, final long srcHostId) throws InsufficientServerCapacityException { final AsyncJobExecutionContext jobContext = AsyncJobExecutionContext.getCurrentExecutionContext(); if (jobContext.isJobDispatchedBy(VmWorkConstants.VM_WORK_JOB_DISPATCHER)) { - VmWorkJobVO placeHolder = null; final VirtualMachine vm = _vmDao.findByUuid(vmUuid); - placeHolder = createPlaceHolderWork(vm.getId()); + VmWorkJobVO placeHolder = createPlaceHolderWork(vm.getId()); try { try { orchestrateMigrateAway(vmUuid, srcHostId, null); @@ -3276,9 +3264,7 @@ public boolean isVirtualMachineUpgradable(final VirtualMachine vm, final Service boolean isMachineUpgradable = true; for (final HostAllocator allocator : hostAllocators) { isMachineUpgradable = allocator.isVirtualMachineUpgradable(vm, offering); - if (isMachineUpgradable) { - continue; - } else { + if (!isMachineUpgradable) { break; } } @@ -3301,9 +3287,8 @@ public void advanceReboot(final String vmUuid, final Map dpdkInterfac } private String getControlNicIpForVM(VirtualMachine vm) { - if (vm.getType() == VirtualMachine.Type.ConsoleProxy || vm.getType() == VirtualMachine.Type.SecondaryStorageVm) { - NicVO nic = _nicsDao.getControlNicForVM(vm.getId()); - return nic.getIPv4Address(); - } else if (vm.getType() == VirtualMachine.Type.DomainRouter) { - return vm.getPrivateIpAddress(); - } else { + if (null == vm.getType()) { return null; } + + switch (vm.getType()) { + case ConsoleProxy: + case SecondaryStorageVm: + NicVO nic = _nicsDao.getControlNicForVM(vm.getId()); + return nic.getIPv4Address(); + case DomainRouter: + return vm.getPrivateIpAddress(); + default: + return null; + } } public Command cleanup(final String vmName) { VirtualMachine vm = _vmDao.findVMByInstanceName(vmName); @@ -3737,8 +3728,7 @@ public NicProfile addVmToNetwork(final VirtualMachine vm, final Network network, final AsyncJobExecutionContext jobContext = AsyncJobExecutionContext.getCurrentExecutionContext(); if (jobContext.isJobDispatchedBy(VmWorkConstants.VM_WORK_JOB_DISPATCHER)) { - VmWorkJobVO placeHolder = null; - placeHolder = createPlaceHolderWork(vm.getId()); + VmWorkJobVO placeHolder = createPlaceHolderWork(vm.getId()); try { return orchestrateAddVmToNetwork(vm, network, requested); } finally { @@ -3783,15 +3773,13 @@ private NicProfile orchestrateAddVmToNetwork(final VirtualMachine vm, final Netw final NicTO nicTO = toNicTO(nic, vmProfile.getVirtualMachine().getHypervisorType()); - if (network != null) { - final Map details = networkOfferingDetailsDao.getNtwkOffDetails(network.getNetworkOfferingId()); - if (details != null) { - details.putIfAbsent(NetworkOffering.Detail.PromiscuousMode, NetworkOrchestrationService.PromiscuousMode.value().toString()); - details.putIfAbsent(NetworkOffering.Detail.MacAddressChanges, NetworkOrchestrationService.MacAddressChanges.value().toString()); - details.putIfAbsent(NetworkOffering.Detail.ForgedTransmits, NetworkOrchestrationService.ForgedTransmits.value().toString()); - } - nicTO.setDetails(details); + final Map details = networkOfferingDetailsDao.getNtwkOffDetails(network.getNetworkOfferingId()); + if (details != null) { + details.putIfAbsent(NetworkOffering.Detail.PromiscuousMode, NetworkOrchestrationService.PromiscuousMode.value().toString()); + details.putIfAbsent(NetworkOffering.Detail.MacAddressChanges, NetworkOrchestrationService.MacAddressChanges.value().toString()); + details.putIfAbsent(NetworkOffering.Detail.ForgedTransmits, NetworkOrchestrationService.ForgedTransmits.value().toString()); } + nicTO.setDetails(details); s_logger.debug("Plugging nic for vm " + vm + " in network " + network); @@ -3829,9 +3817,7 @@ private NicProfile orchestrateAddVmToNetwork(final VirtualMachine vm, final Netw @Override public NicTO toNicTO(final NicProfile nic, final HypervisorType hypervisorType) { final HypervisorGuru hvGuru = _hvGuruMgr.getGuru(hypervisorType); - - final NicTO nicTO = hvGuru.toNicTO(nic); - return nicTO; + return hvGuru.toNicTO(nic); } @Override @@ -3840,8 +3826,7 @@ public boolean removeNicFromVm(final VirtualMachine vm, final Nic nic) final AsyncJobExecutionContext jobContext = AsyncJobExecutionContext.getCurrentExecutionContext(); if (jobContext.isJobDispatchedBy(VmWorkConstants.VM_WORK_JOB_DISPATCHER)) { - VmWorkJobVO placeHolder = null; - placeHolder = createPlaceHolderWork(vm.getId()); + VmWorkJobVO placeHolder = createPlaceHolderWork(vm.getId()); try { return orchestrateRemoveNicFromVm(vm, nic); } finally { @@ -3991,11 +3976,9 @@ private boolean orchestrateRemoveVmFromNetwork(final VirtualMachine vm, final Ne _networkMgr.removeNic(vmProfile, nic); return true; } finally { - if (lock != null) { - _nicsDao.releaseFromLockTable(lock.getId()); - if (s_logger.isDebugEnabled()) { - s_logger.debug("Lock is released for nic id " + lock.getId() + " as a part of remove vm " + vm + " from network " + network); - } + _nicsDao.releaseFromLockTable(lock.getId()); + if (s_logger.isDebugEnabled()) { + s_logger.debug("Lock is released for nic id " + lock.getId() + " as a part of remove vm " + vm + " from network " + network); } } } @@ -4059,9 +4042,8 @@ public void migrateForScale(final String vmUuid, final long srcHostId, final Dep throws ResourceUnavailableException, ConcurrentOperationException { final AsyncJobExecutionContext jobContext = AsyncJobExecutionContext.getCurrentExecutionContext(); if (jobContext.isJobDispatchedBy(VmWorkConstants.VM_WORK_JOB_DISPATCHER)) { - VmWorkJobVO placeHolder = null; final VirtualMachine vm = _vmDao.findByUuid(vmUuid); - placeHolder = createPlaceHolderWork(vm.getId()); + VmWorkJobVO placeHolder = createPlaceHolderWork(vm.getId()); try { orchestrateMigrateForScale(vmUuid, srcHostId, dest, oldSvcOfferingId); } finally { @@ -4109,7 +4091,6 @@ private void orchestrateMigrateForScale(final String vmUuid, final long srcHostI final VirtualMachineGuru vmGuru = getVmGuru(vm); - final long vmId = vm.getId(); vm = _vmDao.findByUuid(vmUuid); if (vm == null) { String message = String.format("Unable to find VM {\"uuid\": \"%s\"}.", vmUuid); @@ -4372,9 +4353,8 @@ public VMInstanceVO reConfigureVm(final String vmUuid, final ServiceOffering old final AsyncJobExecutionContext jobContext = AsyncJobExecutionContext.getCurrentExecutionContext(); if (jobContext.isJobDispatchedBy(VmWorkConstants.VM_WORK_JOB_DISPATCHER)) { - VmWorkJobVO placeHolder = null; final VirtualMachine vm = _vmDao.findByUuid(vmUuid); - placeHolder = createPlaceHolderWork(vm.getId()); + VmWorkJobVO placeHolder = createPlaceHolderWork(vm.getId()); try { return orchestrateReConfigureVm(vmUuid, oldServiceOffering, newServiceOffering, reconfiguringOnExistingHost); } finally { @@ -4528,7 +4508,7 @@ protected void HandlePowerStateReport(final String subject, final String senderA final List pendingWorkJobs = _workJobDao.listPendingWorkJobs( VirtualMachine.Type.Instance, vmId); - if (pendingWorkJobs.size() == 0 && !_haMgr.hasPendingHaWork(vmId)) { + if (CollectionUtils.isEmpty(pendingWorkJobs) && !_haMgr.hasPendingHaWork(vmId)) { final VMInstanceVO vm = _vmDao.findById(vmId); if (vm != null) { switch (vm.getPowerState()) { @@ -4733,28 +4713,21 @@ private List listStalledVMInTransitionStateOnUpHost(final long hostId, fin "AND i.removed IS NULL"; final List l = new ArrayList<>(); - TransactionLegacy txn = null; - try { - txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); + try (TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB)) { + String cutTimeStr = DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), cutTime); - PreparedStatement pstmt = null; try { - pstmt = txn.prepareAutoCloseStatement(sql); + PreparedStatement pstmt = txn.prepareAutoCloseStatement(sql); pstmt.setLong(1, hostId); - pstmt.setString(2, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), cutTime)); + pstmt.setString(2, cutTimeStr); pstmt.setInt(3, JobInfo.Status.IN_PROGRESS.ordinal()); final ResultSet rs = pstmt.executeQuery(); while (rs.next()) { l.add(rs.getLong(1)); } - } catch (final SQLException e) { - } catch (final Throwable e) { - } - - } finally { - if (txn != null) { - txn.close(); + } catch (SQLException e) { + s_logger.error(String.format("Unable to execute SQL [%s] with params {\"h.id\": %s, \"i.power_state_update_time\": \"%s\"} due to [%s].", sql, hostId, cutTimeStr, e.getMessage()), e); } } return l; @@ -4768,28 +4741,24 @@ private List listVMInTransitionStateWithRecentReportOnUpHost(final long ho "AND i.removed IS NULL"; final List l = new ArrayList<>(); - TransactionLegacy txn = null; - try { - txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); - PreparedStatement pstmt = null; + try (TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB)) { + String cutTimeStr = DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), cutTime); + int jobStatusInProgress = JobInfo.Status.IN_PROGRESS.ordinal(); + try { - pstmt = txn.prepareAutoCloseStatement(sql); + PreparedStatement pstmt = txn.prepareAutoCloseStatement(sql); pstmt.setLong(1, hostId); - pstmt.setString(2, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), cutTime)); - pstmt.setInt(3, JobInfo.Status.IN_PROGRESS.ordinal()); + pstmt.setString(2, cutTimeStr); + pstmt.setInt(3, jobStatusInProgress); final ResultSet rs = pstmt.executeQuery(); while (rs.next()) { l.add(rs.getLong(1)); } } catch (final SQLException e) { - } catch (final Throwable e) { + s_logger.error(String.format("Unable to execute SQL [%s] with params {\"h.id\": %s, \"i.power_state_update_time\": \"%s\", \"j.job_status\": %s} due to [%s].", sql, hostId, cutTimeStr, jobStatusInProgress, e.getMessage()), e); } return l; - } finally { - if (txn != null) { - txn.close(); - } } } @@ -4801,27 +4770,23 @@ private List listStalledVMInTransitionStateOnDisconnectedHosts(final Date "AND i.removed IS NULL"; final List l = new ArrayList<>(); - TransactionLegacy txn = null; - try { - txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); - PreparedStatement pstmt = null; + try (TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB)) { + String cutTimeStr = DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), cutTime); + int jobStatusInProgress = JobInfo.Status.IN_PROGRESS.ordinal(); + try { - pstmt = txn.prepareAutoCloseStatement(sql); + PreparedStatement pstmt = txn.prepareAutoCloseStatement(sql); - pstmt.setString(1, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), cutTime)); - pstmt.setInt(2, JobInfo.Status.IN_PROGRESS.ordinal()); + pstmt.setString(1, cutTimeStr); + pstmt.setInt(2, jobStatusInProgress); final ResultSet rs = pstmt.executeQuery(); while (rs.next()) { l.add(rs.getLong(1)); } } catch (final SQLException e) { - } catch (final Throwable e) { + s_logger.error(String.format("Unable to execute SQL [%s] with params {\"i.power_state_update_time\": \"%s\", \"j.job_status\": %s} due to [%s].", sql, cutTimeStr, jobStatusInProgress, e.getMessage()), e); } return l; - } finally { - if (txn != null) { - txn.close(); - } } } @@ -4834,10 +4799,7 @@ public VmStateSyncOutcome(final AsyncJob job, final PowerState desiredPowerState public boolean checkCondition() { final AsyncJobVO jobVo = _entityMgr.findById(AsyncJobVO.class, job.getId()); assert jobVo != null; - if (jobVo == null || jobVo.getStatus() != JobInfo.Status.IN_PROGRESS) { - return true; - } - return false; + return jobVo.getStatus() != JobInfo.Status.IN_PROGRESS; } }, Topics.VM_POWER_STATE, AsyncJob.Topics.JOB_STATE); _vmId = vmId; @@ -4858,11 +4820,7 @@ public VmJobVirtualMachineOutcome(final AsyncJob job, final long vmId) { public boolean checkCondition() { final AsyncJobVO jobVo = _entityMgr.findById(AsyncJobVO.class, job.getId()); assert jobVo != null; - if (jobVo == null || jobVo.getStatus() != JobInfo.Status.IN_PROGRESS) { - return true; - } - - return false; + return jobVo.getStatus() != JobInfo.Status.IN_PROGRESS; } }, AsyncJob.Topics.JOB_STATE); _vmId = vmId; @@ -5696,8 +5654,7 @@ public Boolean updateDefaultNicForVM(final VirtualMachine vm, final Nic nic, fin final AsyncJobExecutionContext jobContext = AsyncJobExecutionContext.getCurrentExecutionContext(); if (jobContext.isJobDispatchedBy(VmWorkConstants.VM_WORK_JOB_DISPATCHER)) { - VmWorkJobVO placeHolder = null; - placeHolder = createPlaceHolderWork(vm.getId()); + VmWorkJobVO placeHolder = createPlaceHolderWork(vm.getId()); try { return orchestrateUpdateDefaultNicForVM(vm, nic, defaultNic); } finally { From 89ca0349d097c087fc124e70313ed99df29ad564 Mon Sep 17 00:00:00 2001 From: Daniel Augusto Veronezi Salvador Date: Wed, 14 Apr 2021 10:17:05 -0300 Subject: [PATCH 08/25] Extract code to method and few adjusts --- .../cloud/vm/VirtualMachineManagerImpl.java | 43 +++++++++---------- .../java/com/cloud/storage/VMTemplateVO.java | 8 +--- 2 files changed, 21 insertions(+), 30 deletions(-) diff --git a/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java b/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java index 3920c5b8e2fc..42c781bb1519 100755 --- a/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java +++ b/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java @@ -470,11 +470,14 @@ public void doInTransactionWithoutResult(final TransactionStatus status) throws s_logger.debug("Allocating disks for " + vmFinal); } + String rootVolumeName = String.format("ROOT-%s", vmFinal.getId()); if (template.getFormat() == ImageFormat.ISO) { - volumeMgr.allocateRawVolume(Type.ROOT, "ROOT-" + vmFinal.getId(), rootDiskOfferingInfo.getDiskOffering(), rootDiskOfferingInfo.getSize(), + volumeMgr.allocateRawVolume(Type.ROOT, rootVolumeName, rootDiskOfferingInfo.getDiskOffering(), rootDiskOfferingInfo.getSize(), rootDiskOfferingInfo.getMinIops(), rootDiskOfferingInfo.getMaxIops(), vmFinal, template, owner, null); - } else if (template.getFormat() != ImageFormat.BAREMETAL) { - volumeMgr.allocateTemplatedVolumes(Type.ROOT, "ROOT-" + vmFinal.getId(), rootDiskOfferingInfo.getDiskOffering(), rootDiskOfferingInfo.getSize(), + } else if (template.getFormat() == ImageFormat.BAREMETAL) { + s_logger.debug(String.format("Template [%s] has format [%s]. Skipping ROOT volume [%s] allocation.", template.toString(), ImageFormat.BAREMETAL, rootVolumeName)); + } else { + volumeMgr.allocateTemplatedVolumes(Type.ROOT, rootVolumeName, rootDiskOfferingInfo.getDiskOffering(), rootDiskOfferingInfo.getSize(), rootDiskOfferingInfo.getMinIops(), rootDiskOfferingInfo.getMaxIops(), template, vmFinal, owner); } @@ -573,7 +576,7 @@ protected void advanceExpunge(VMInstanceVO vm) throws ResourceUnavailableExcepti final List nicExpungeCommands = hvGuru.finalizeExpungeNics(vm, vmNics); _networkMgr.cleanupNics(profile); - s_logger.debug("Cleaning up hypervisor data structures (ex. SRs in XenServer) for managed storage"); + s_logger.debug(String.format("Cleaning up hypervisor data structures (ex. SRs in XenServer) for managed storage. Data from %s.", vm.toString())); final List volumeExpungeCommands = hvGuru.finalizeExpungeVolumes(vm); @@ -590,16 +593,7 @@ protected void advanceExpunge(VMInstanceVO vm) throws ResourceUnavailableExcepti } _agentMgr.send(hostId, cmds); - - if (!cmds.isSuccessful()) { - for (final Answer answer : cmds.getAnswers()) { - if (!answer.getResult()) { - String message = String.format("Unable to expunge %s due to [%s].", vm.toString(), answer.getDetails()); - s_logger.warn(message); - throw new CloudRuntimeException(message); - } - } - } + handleUnsuccessfulCommands(cmds, vm); } if (hostId != null) { @@ -634,22 +628,25 @@ protected void advanceExpunge(VMInstanceVO vm) throws ResourceUnavailableExcepti } } _agentMgr.send(hostId, cmds); - if (!cmds.isSuccessful()) { - for (final Answer answer : cmds.getAnswers()) { - if (!answer.getResult()) { - String message = String.format("Unable to expunge %s due to [%s].", vm.toString(), answer.getDetails()); - s_logger.warn(message); - throw new CloudRuntimeException(message); - } - } - } + handleUnsuccessfulCommands(cmds, vm); } } if (s_logger.isDebugEnabled()) { s_logger.debug("Expunged " + vm); } + } + protected void handleUnsuccessfulCommands(Commands cmds, VMInstanceVO vm) throws CloudRuntimeException { + if (!cmds.isSuccessful()) { + for (Answer answer : cmds.getAnswers()) { + if (!answer.getResult()) { + String message = String.format("Unable to expunge %s due to [%s].", vm.toString(), answer.getDetails()); + s_logger.warn(message); + throw new CloudRuntimeException(message); + } + } + } } private List> getTargets(Long hostId, long vmId) { diff --git a/engine/schema/src/main/java/com/cloud/storage/VMTemplateVO.java b/engine/schema/src/main/java/com/cloud/storage/VMTemplateVO.java index 61df40e50d88..49843fbfaebd 100644 --- a/engine/schema/src/main/java/com/cloud/storage/VMTemplateVO.java +++ b/engine/schema/src/main/java/com/cloud/storage/VMTemplateVO.java @@ -551,15 +551,9 @@ public int hashCode() { return uniqueName.hashCode(); } - @Transient - String toString; - @Override public String toString() { - if (toString == null) { - toString = new StringBuilder("Tmpl[").append(id).append("-").append(format).append("-").append(uniqueName).toString(); - } - return toString; + return String.format("Template {\"id\": %s, \"uniqueName\": \"%s\", \"format\": \"%s\"}", id, uniqueName, format); } public void setRemoved(Date removed) { From 3e0be4d057afeef9f0b59c46f6982eb514e55b36 Mon Sep 17 00:00:00 2001 From: Daniel Augusto Veronezi Salvador Date: Wed, 14 Apr 2021 10:33:49 -0300 Subject: [PATCH 09/25] Use CollectionUtils --- .../cloud/vm/VirtualMachineManagerImpl.java | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java b/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java index 42c781bb1519..c6e87e0c67b5 100755 --- a/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java +++ b/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java @@ -584,7 +584,7 @@ protected void advanceExpunge(VMInstanceVO vm) throws ResourceUnavailableExcepti List> targets = getTargets(hostId, vm.getId()); - if (volumeExpungeCommands != null && volumeExpungeCommands.size() > 0 && hostId != null) { + if (CollectionUtils.isNotEmpty(volumeExpungeCommands) && hostId != null) { final Commands cmds = new Commands(Command.OnError.Stop); for (final Command volumeExpungeCommand : volumeExpungeCommands) { @@ -614,7 +614,7 @@ protected void advanceExpunge(VMInstanceVO vm) throws ResourceUnavailableExcepti userVmDeployAsIsDetailsDao.removeDetails(vm.getId()); final List finalizeExpungeCommands = hvGuru.finalizeExpunge(vm); - if (finalizeExpungeCommands != null && finalizeExpungeCommands.size() > 0) { + if (CollectionUtils.isNotEmpty(finalizeExpungeCommands)) { if (hostId != null) { final Commands cmds = new Commands(Command.OnError.Stop); for (final Command command : finalizeExpungeCommands) { @@ -4843,7 +4843,7 @@ public Outcome startVmThroughJobQueue(final String vmUuid, final List pendingWorkJobs = _workJobDao.listPendingWorkJobs(VirtualMachine.Type.Instance, vm.getId(), VmWorkStart.class.getName()); - if (pendingWorkJobs.size() > 0) { + if (CollectionUtils.isNotEmpty(pendingWorkJobs)) { assert pendingWorkJobs.size() == 1; workJob = pendingWorkJobs.get(0); } else { @@ -4888,7 +4888,7 @@ public Outcome stopVmThroughJobQueue(final String vmUuid, final VmWorkStop.class.getName()); VmWorkJobVO workJob = null; - if (pendingWorkJobs != null && pendingWorkJobs.size() > 0) { + if (CollectionUtils.isNotEmpty(pendingWorkJobs)) { assert pendingWorkJobs.size() == 1; workJob = pendingWorkJobs.get(0); } else { @@ -4930,7 +4930,7 @@ public Outcome rebootVmThroughJobQueue(final String vmUuid, VmWorkReboot.class.getName()); VmWorkJobVO workJob = null; - if (pendingWorkJobs != null && pendingWorkJobs.size() > 0) { + if (CollectionUtils.isNotEmpty(pendingWorkJobs)) { assert pendingWorkJobs.size() == 1; workJob = pendingWorkJobs.get(0); } else { @@ -4977,7 +4977,7 @@ public Outcome migrateVmThroughJobQueue(final String vmUuid, fin VmWorkMigrate.class.getName()); VmWorkJobVO workJob = null; - if (pendingWorkJobs != null && pendingWorkJobs.size() > 0) { + if (CollectionUtils.isNotEmpty(pendingWorkJobs)) { assert pendingWorkJobs.size() == 1; workJob = pendingWorkJobs.get(0); } else { @@ -5017,7 +5017,7 @@ public Outcome migrateVmAwayThroughJobQueue(final String vmUuid, VmWorkMigrateAway.class.getName()); VmWorkJobVO workJob = null; - if (pendingWorkJobs != null && pendingWorkJobs.size() > 0) { + if (CollectionUtils.isNotEmpty(pendingWorkJobs)) { assert pendingWorkJobs.size() == 1; workJob = pendingWorkJobs.get(0); } else { @@ -5058,7 +5058,7 @@ public Outcome migrateVmWithStorageThroughJobQueue( VmWorkMigrateWithStorage.class.getName()); VmWorkJobVO workJob = null; - if (pendingWorkJobs != null && pendingWorkJobs.size() > 0) { + if (CollectionUtils.isNotEmpty(pendingWorkJobs)) { assert pendingWorkJobs.size() == 1; workJob = pendingWorkJobs.get(0); } else { @@ -5100,7 +5100,7 @@ public Outcome migrateVmForScaleThroughJobQueue( VmWorkMigrateForScale.class.getName()); VmWorkJobVO workJob = null; - if (pendingWorkJobs != null && pendingWorkJobs.size() > 0) { + if (CollectionUtils.isNotEmpty(pendingWorkJobs)) { assert pendingWorkJobs.size() == 1; workJob = pendingWorkJobs.get(0); } else { @@ -5158,7 +5158,7 @@ public Outcome migrateVmStorageThroughJobQueue( VmWorkStorageMigration.class.getName()); VmWorkJobVO workJob = null; - if (pendingWorkJobs != null && pendingWorkJobs.size() > 0) { + if (CollectionUtils.isNotEmpty(pendingWorkJobs)) { assert pendingWorkJobs.size() == 1; workJob = pendingWorkJobs.get(0); } else { @@ -5197,7 +5197,7 @@ public Outcome addVmToNetworkThroughJobQueue( VmWorkAddVmToNetwork.class.getName()); VmWorkJobVO workJob = null; - if (pendingWorkJobs != null && pendingWorkJobs.size() > 0) { + if (CollectionUtils.isNotEmpty(pendingWorkJobs)) { assert pendingWorkJobs.size() == 1; workJob = pendingWorkJobs.get(0); } else { @@ -5236,7 +5236,7 @@ public Outcome removeNicFromVmThroughJobQueue( VmWorkRemoveNicFromVm.class.getName()); VmWorkJobVO workJob = null; - if (pendingWorkJobs != null && pendingWorkJobs.size() > 0) { + if (CollectionUtils.isNotEmpty(pendingWorkJobs)) { assert pendingWorkJobs.size() == 1; workJob = pendingWorkJobs.get(0); } else { @@ -5275,7 +5275,7 @@ public Outcome removeVmFromNetworkThroughJobQueue( VmWorkRemoveVmFromNetwork.class.getName()); VmWorkJobVO workJob = null; - if (pendingWorkJobs != null && pendingWorkJobs.size() > 0) { + if (CollectionUtils.isNotEmpty(pendingWorkJobs)) { assert pendingWorkJobs.size() == 1; workJob = pendingWorkJobs.get(0); } else { @@ -5317,7 +5317,7 @@ public Outcome reconfigureVmThroughJobQueue( VmWorkReconfigure.class.getName()); VmWorkJobVO workJob = null; - if (pendingWorkJobs != null && pendingWorkJobs.size() > 0) { + if (CollectionUtils.isNotEmpty(pendingWorkJobs)) { assert pendingWorkJobs.size() == 1; workJob = pendingWorkJobs.get(0); } else { @@ -5606,7 +5606,7 @@ public Outcome restoreVirtualMachineThroughJobQueue(final long v VmWorkRestore.class.getName()); VmWorkJobVO workJob = null; - if (pendingWorkJobs != null && pendingWorkJobs.size() > 0) { + if (CollectionUtils.isNotEmpty(pendingWorkJobs)) { assert pendingWorkJobs.size() == 1; workJob = pendingWorkJobs.get(0); } else { @@ -5707,7 +5707,7 @@ public Outcome updateDefaultNicForVMThroughJobQueue(final Virtua VmWorkUpdateDefaultNic.class.getName()); VmWorkJobVO workJob = null; - if (pendingWorkJobs != null && pendingWorkJobs.size() > 0) { + if (CollectionUtils.isNotEmpty(pendingWorkJobs)) { assert pendingWorkJobs.size() == 1; workJob = pendingWorkJobs.get(0); } else { From da5406e8e7b4462e4a57f3c2adbea2612bf0e574 Mon Sep 17 00:00:00 2001 From: Daniel Augusto Veronezi Salvador Date: Wed, 14 Apr 2021 14:14:40 -0300 Subject: [PATCH 10/25] Extract pending work job validation to method --- .../cloud/vm/VirtualMachineManagerImpl.java | 400 +++++++++--------- 1 file changed, 196 insertions(+), 204 deletions(-) diff --git a/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java b/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java index c6e87e0c67b5..69b019a7d9c1 100755 --- a/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java +++ b/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java @@ -4837,29 +4837,27 @@ public Outcome startVmThroughJobQueue(final String vmUuid, final User callingUser = context.getCallingUser(); final Account callingAccount = context.getCallingAccount(); - final VMInstanceVO vm = _vmDao.findByUuid(vmUuid); + VirtualMachine.Type vmType = VirtualMachine.Type.Instance; + String commandName = VmWorkStart.class.getName(); + Pair pendingWorkJob = retrievePendingWorkJob(vmUuid, vmType, commandName); - VmWorkJobVO workJob = null; - final List pendingWorkJobs = _workJobDao.listPendingWorkJobs(VirtualMachine.Type.Instance, - vm.getId(), VmWorkStart.class.getName()); + VmWorkJobVO workJob = pendingWorkJob.first(); + Long vmId = pendingWorkJob.second(); - if (CollectionUtils.isNotEmpty(pendingWorkJobs)) { - assert pendingWorkJobs.size() == 1; - workJob = pendingWorkJobs.get(0); - } else { + if (workJob == null) { workJob = new VmWorkJobVO(context.getContextId()); workJob.setDispatcher(VmWorkConstants.VM_WORK_JOB_DISPATCHER); - workJob.setCmd(VmWorkStart.class.getName()); + workJob.setCmd(commandName); workJob.setAccountId(callingAccount.getId()); workJob.setUserId(callingUser.getId()); workJob.setStep(VmWorkJobVO.Step.Starting); - workJob.setVmType(VirtualMachine.Type.Instance); - workJob.setVmInstanceId(vm.getId()); + workJob.setVmType(vmType); + workJob.setVmInstanceId(vmId); workJob.setRelated(AsyncJobExecutionContext.getOriginJobId()); - final VmWorkStart workInfo = new VmWorkStart(callingUser.getId(), callingAccount.getId(), vm.getId(), VirtualMachineManagerImpl.VM_WORK_JOB_HANDLER); + final VmWorkStart workInfo = new VmWorkStart(callingUser.getId(), callingAccount.getId(), vmId, VirtualMachineManagerImpl.VM_WORK_JOB_HANDLER); workInfo.setPlan(planToDeploy); workInfo.setParams(params); if (planner != null) { @@ -4867,13 +4865,13 @@ public Outcome startVmThroughJobQueue(final String vmUuid, } workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo)); - _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vm.getId()); + _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vmId); } AsyncJobExecutionContext.getCurrentExecutionContext().joinJob(workJob.getId()); return new VmStateSyncOutcome(workJob, - VirtualMachine.PowerState.PowerOn, vm.getId(), null); + VirtualMachine.PowerState.PowerOn, vmId, null); } public Outcome stopVmThroughJobQueue(final String vmUuid, final boolean cleanup) { @@ -4881,39 +4879,35 @@ public Outcome stopVmThroughJobQueue(final String vmUuid, final final Account account = context.getCallingAccount(); final User user = context.getCallingUser(); - final VMInstanceVO vm = _vmDao.findByUuid(vmUuid); + String commandName = VmWorkStop.class.getName(); + Pair pendingWorkJob = retrievePendingWorkJob(vmUuid, null, commandName); - final List pendingWorkJobs = _workJobDao.listPendingWorkJobs( - vm.getType(), vm.getId(), - VmWorkStop.class.getName()); + VmWorkJobVO workJob = pendingWorkJob.first(); + Long vmId = pendingWorkJob.second(); - VmWorkJobVO workJob = null; - if (CollectionUtils.isNotEmpty(pendingWorkJobs)) { - assert pendingWorkJobs.size() == 1; - workJob = pendingWorkJobs.get(0); - } else { + if (workJob == null) { workJob = new VmWorkJobVO(context.getContextId()); workJob.setDispatcher(VmWorkConstants.VM_WORK_JOB_DISPATCHER); - workJob.setCmd(VmWorkStop.class.getName()); + workJob.setCmd(commandName); workJob.setAccountId(account.getId()); workJob.setUserId(user.getId()); workJob.setStep(VmWorkJobVO.Step.Prepare); workJob.setVmType(VirtualMachine.Type.Instance); - workJob.setVmInstanceId(vm.getId()); + workJob.setVmInstanceId(vmId); workJob.setRelated(AsyncJobExecutionContext.getOriginJobId()); - final VmWorkStop workInfo = new VmWorkStop(user.getId(), account.getId(), vm.getId(), VirtualMachineManagerImpl.VM_WORK_JOB_HANDLER, cleanup); + final VmWorkStop workInfo = new VmWorkStop(user.getId(), account.getId(), vmId, VirtualMachineManagerImpl.VM_WORK_JOB_HANDLER, cleanup); workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo)); - _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vm.getId()); + _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vmId); } AsyncJobExecutionContext.getCurrentExecutionContext().joinJob(workJob.getId()); return new VmStateSyncOutcome(workJob, - VirtualMachine.PowerState.PowerOff, vm.getId(), null); + VirtualMachine.PowerState.PowerOff, vmId, null); } public Outcome rebootVmThroughJobQueue(final String vmUuid, @@ -4923,39 +4917,36 @@ public Outcome rebootVmThroughJobQueue(final String vmUuid, final Account account = context.getCallingAccount(); final User user = context.getCallingUser(); - final VMInstanceVO vm = _vmDao.findByUuid(vmUuid); + VirtualMachine.Type vmType = VirtualMachine.Type.Instance; + String commandName = VmWorkReboot.class.getName(); + Pair pendingWorkJob = retrievePendingWorkJob(vmUuid, vmType, commandName); - final List pendingWorkJobs = _workJobDao.listPendingWorkJobs( - VirtualMachine.Type.Instance, vm.getId(), - VmWorkReboot.class.getName()); + VmWorkJobVO workJob = pendingWorkJob.first(); + Long vmId = pendingWorkJob.second(); - VmWorkJobVO workJob = null; - if (CollectionUtils.isNotEmpty(pendingWorkJobs)) { - assert pendingWorkJobs.size() == 1; - workJob = pendingWorkJobs.get(0); - } else { + if (workJob == null) { workJob = new VmWorkJobVO(context.getContextId()); workJob.setDispatcher(VmWorkConstants.VM_WORK_JOB_DISPATCHER); - workJob.setCmd(VmWorkReboot.class.getName()); + workJob.setCmd(commandName); workJob.setAccountId(account.getId()); workJob.setUserId(user.getId()); workJob.setStep(VmWorkJobVO.Step.Prepare); - workJob.setVmType(VirtualMachine.Type.Instance); - workJob.setVmInstanceId(vm.getId()); + workJob.setVmType(vmType); + workJob.setVmInstanceId(vmId); workJob.setRelated(AsyncJobExecutionContext.getOriginJobId()); - final VmWorkReboot workInfo = new VmWorkReboot(user.getId(), account.getId(), vm.getId(), VirtualMachineManagerImpl.VM_WORK_JOB_HANDLER, params); + final VmWorkReboot workInfo = new VmWorkReboot(user.getId(), account.getId(), vmId, VirtualMachineManagerImpl.VM_WORK_JOB_HANDLER, params); workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo)); - _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vm.getId()); + _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vmId); } AsyncJobExecutionContext.getCurrentExecutionContext().joinJob(workJob.getId()); return new VmJobVirtualMachineOutcome(workJob, - vm.getId()); + vmId); } public Outcome migrateVmThroughJobQueue(final String vmUuid, final long srcHostId, final DeployDestination dest) { @@ -4970,39 +4961,37 @@ public Outcome migrateVmThroughJobQueue(final String vmUuid, fin } } - final VMInstanceVO vm = _vmDao.findByUuid(vmUuid); + VMInstanceVO vm = _vmDao.findByUuid(vmUuid); + Long vmId = vm.getId(); - final List pendingWorkJobs = _workJobDao.listPendingWorkJobs( - VirtualMachine.Type.Instance, vm.getId(), - VmWorkMigrate.class.getName()); + VirtualMachine.Type vmType = VirtualMachine.Type.Instance; + String commandName = VmWorkMigrate.class.getName(); + Pair pendingWorkJob = retrievePendingWorkJob(vmId, vmUuid, vmType, commandName); - VmWorkJobVO workJob = null; - if (CollectionUtils.isNotEmpty(pendingWorkJobs)) { - assert pendingWorkJobs.size() == 1; - workJob = pendingWorkJobs.get(0); - } else { + VmWorkJobVO workJob = pendingWorkJob.first(); + if (workJob == null) { workJob = new VmWorkJobVO(context.getContextId()); workJob.setDispatcher(VmWorkConstants.VM_WORK_JOB_DISPATCHER); - workJob.setCmd(VmWorkMigrate.class.getName()); + workJob.setCmd(commandName); workJob.setAccountId(account.getId()); workJob.setUserId(user.getId()); - workJob.setVmType(VirtualMachine.Type.Instance); - workJob.setVmInstanceId(vm.getId()); + workJob.setVmType(vmType); + workJob.setVmInstanceId(vmId); workJob.setRelated(AsyncJobExecutionContext.getOriginJobId()); - final VmWorkMigrate workInfo = new VmWorkMigrate(user.getId(), account.getId(), vm.getId(), VirtualMachineManagerImpl.VM_WORK_JOB_HANDLER, srcHostId, dest); + final VmWorkMigrate workInfo = new VmWorkMigrate(user.getId(), account.getId(), vmId, VirtualMachineManagerImpl.VM_WORK_JOB_HANDLER, srcHostId, dest); workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo)); - _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vm.getId()); + _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vmId); } AsyncJobExecutionContext.getCurrentExecutionContext().joinJob(workJob.getId()); return new VmStateSyncOutcome(workJob, - VirtualMachine.PowerState.PowerOn, vm.getId(), vm.getPowerHostId()); + VirtualMachine.PowerState.PowerOn, vmId, vm.getPowerHostId()); } public Outcome migrateVmAwayThroughJobQueue(final String vmUuid, final long srcHostId) { @@ -5010,37 +4999,36 @@ public Outcome migrateVmAwayThroughJobQueue(final String vmUuid, final User user = context.getCallingUser(); final Account account = context.getCallingAccount(); - final VMInstanceVO vm = _vmDao.findByUuid(vmUuid); + VMInstanceVO vm = _vmDao.findByUuid(vmUuid); + Long vmId = vm.getId(); - final List pendingWorkJobs = _workJobDao.listPendingWorkJobs( - VirtualMachine.Type.Instance, vm.getId(), - VmWorkMigrateAway.class.getName()); + VirtualMachine.Type vmType = VirtualMachine.Type.Instance; + String commandName = VmWorkMigrateAway.class.getName(); + Pair pendingWorkJob = retrievePendingWorkJob(vmId, vmUuid, vmType, commandName); - VmWorkJobVO workJob = null; - if (CollectionUtils.isNotEmpty(pendingWorkJobs)) { - assert pendingWorkJobs.size() == 1; - workJob = pendingWorkJobs.get(0); - } else { + VmWorkJobVO workJob = pendingWorkJob.first(); + + if (workJob == null) { workJob = new VmWorkJobVO(context.getContextId()); workJob.setDispatcher(VmWorkConstants.VM_WORK_JOB_DISPATCHER); - workJob.setCmd(VmWorkMigrateAway.class.getName()); + workJob.setCmd(commandName); workJob.setAccountId(account.getId()); workJob.setUserId(user.getId()); - workJob.setVmType(VirtualMachine.Type.Instance); - workJob.setVmInstanceId(vm.getId()); + workJob.setVmType(vmType); + workJob.setVmInstanceId(vmId); workJob.setRelated(AsyncJobExecutionContext.getOriginJobId()); - final VmWorkMigrateAway workInfo = new VmWorkMigrateAway(user.getId(), account.getId(), vm.getId(), VirtualMachineManagerImpl.VM_WORK_JOB_HANDLER, srcHostId); + final VmWorkMigrateAway workInfo = new VmWorkMigrateAway(user.getId(), account.getId(), vmId, VirtualMachineManagerImpl.VM_WORK_JOB_HANDLER, srcHostId); workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo)); } - _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vm.getId()); + _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vmId); AsyncJobExecutionContext.getCurrentExecutionContext().joinJob(workJob.getId()); - return new VmStateSyncOutcome(workJob, VirtualMachine.PowerState.PowerOn, vm.getId(), vm.getPowerHostId()); + return new VmStateSyncOutcome(workJob, VirtualMachine.PowerState.PowerOn, vmId, vm.getPowerHostId()); } public Outcome migrateVmWithStorageThroughJobQueue( @@ -5051,39 +5039,35 @@ public Outcome migrateVmWithStorageThroughJobQueue( final User user = context.getCallingUser(); final Account account = context.getCallingAccount(); - final VMInstanceVO vm = _vmDao.findByUuid(vmUuid); + VirtualMachine.Type vmType = VirtualMachine.Type.Instance; + String commandName = VmWorkMigrateWithStorage.class.getName(); + Pair pendingWorkJob = retrievePendingWorkJob(vmUuid, vmType, commandName); - final List pendingWorkJobs = _workJobDao.listPendingWorkJobs( - VirtualMachine.Type.Instance, vm.getId(), - VmWorkMigrateWithStorage.class.getName()); - - VmWorkJobVO workJob = null; - if (CollectionUtils.isNotEmpty(pendingWorkJobs)) { - assert pendingWorkJobs.size() == 1; - workJob = pendingWorkJobs.get(0); - } else { + VmWorkJobVO workJob = pendingWorkJob.first(); + Long vmId = pendingWorkJob.second(); + if (workJob == null) { workJob = new VmWorkJobVO(context.getContextId()); workJob.setDispatcher(VmWorkConstants.VM_WORK_JOB_DISPATCHER); - workJob.setCmd(VmWorkMigrateWithStorage.class.getName()); + workJob.setCmd(commandName); workJob.setAccountId(account.getId()); workJob.setUserId(user.getId()); - workJob.setVmType(VirtualMachine.Type.Instance); - workJob.setVmInstanceId(vm.getId()); + workJob.setVmType(vmType); + workJob.setVmInstanceId(vmId); workJob.setRelated(AsyncJobExecutionContext.getOriginJobId()); - final VmWorkMigrateWithStorage workInfo = new VmWorkMigrateWithStorage(user.getId(), account.getId(), vm.getId(), + final VmWorkMigrateWithStorage workInfo = new VmWorkMigrateWithStorage(user.getId(), account.getId(), vmId, VirtualMachineManagerImpl.VM_WORK_JOB_HANDLER, srcHostId, destHostId, volumeToPool); workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo)); - _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vm.getId()); + _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vmId); } AsyncJobExecutionContext.getCurrentExecutionContext().joinJob(workJob.getId()); return new VmStateSyncOutcome(workJob, - VirtualMachine.PowerState.PowerOn, vm.getId(), destHostId); + VirtualMachine.PowerState.PowerOn, vmId, destHostId); } public Outcome migrateVmForScaleThroughJobQueue( @@ -5093,38 +5077,34 @@ public Outcome migrateVmForScaleThroughJobQueue( final User user = context.getCallingUser(); final Account account = context.getCallingAccount(); - final VMInstanceVO vm = _vmDao.findByUuid(vmUuid); + VirtualMachine.Type vmType = VirtualMachine.Type.Instance; + String commandName = VmWorkMigrateForScale.class.getName(); + Pair pendingWorkJob = retrievePendingWorkJob(vmUuid, vmType, commandName); - final List pendingWorkJobs = _workJobDao.listPendingWorkJobs( - VirtualMachine.Type.Instance, vm.getId(), - VmWorkMigrateForScale.class.getName()); - - VmWorkJobVO workJob = null; - if (CollectionUtils.isNotEmpty(pendingWorkJobs)) { - assert pendingWorkJobs.size() == 1; - workJob = pendingWorkJobs.get(0); - } else { + VmWorkJobVO workJob = pendingWorkJob.first(); + Long vmId = pendingWorkJob.second(); + if (workJob == null) { workJob = new VmWorkJobVO(context.getContextId()); workJob.setDispatcher(VmWorkConstants.VM_WORK_JOB_DISPATCHER); - workJob.setCmd(VmWorkMigrateForScale.class.getName()); + workJob.setCmd(commandName); workJob.setAccountId(account.getId()); workJob.setUserId(user.getId()); - workJob.setVmType(VirtualMachine.Type.Instance); - workJob.setVmInstanceId(vm.getId()); + workJob.setVmType(vmType); + workJob.setVmInstanceId(vmId); workJob.setRelated(AsyncJobExecutionContext.getOriginJobId()); - final VmWorkMigrateForScale workInfo = new VmWorkMigrateForScale(user.getId(), account.getId(), vm.getId(), + final VmWorkMigrateForScale workInfo = new VmWorkMigrateForScale(user.getId(), account.getId(), vmId, VirtualMachineManagerImpl.VM_WORK_JOB_HANDLER, srcHostId, dest, newSvcOfferingId); workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo)); - _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vm.getId()); + _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vmId); } AsyncJobExecutionContext.getCurrentExecutionContext().joinJob(workJob.getId()); - return new VmJobVirtualMachineOutcome(workJob, vm.getId()); + return new VmJobVirtualMachineOutcome(workJob, vmId); } private void checkConcurrentJobsPerDatastoreThreshhold(final StoragePool destPool) { @@ -5151,38 +5131,34 @@ public Outcome migrateVmStorageThroughJobQueue( checkConcurrentJobsPerDatastoreThreshhold(pool); } - final VMInstanceVO vm = _vmDao.findByUuid(vmUuid); - - final List pendingWorkJobs = _workJobDao.listPendingWorkJobs( - VirtualMachine.Type.Instance, vm.getId(), - VmWorkStorageMigration.class.getName()); + VirtualMachine.Type vmType = VirtualMachine.Type.Instance; + String commandName = VmWorkStorageMigration.class.getName(); + Pair pendingWorkJob = retrievePendingWorkJob(vmUuid, vmType, commandName); - VmWorkJobVO workJob = null; - if (CollectionUtils.isNotEmpty(pendingWorkJobs)) { - assert pendingWorkJobs.size() == 1; - workJob = pendingWorkJobs.get(0); - } else { + VmWorkJobVO workJob = pendingWorkJob.first(); + Long vmId = pendingWorkJob.second(); + if (workJob == null) { workJob = new VmWorkJobVO(context.getContextId()); workJob.setDispatcher(VmWorkConstants.VM_WORK_JOB_DISPATCHER); - workJob.setCmd(VmWorkStorageMigration.class.getName()); + workJob.setCmd(commandName); workJob.setAccountId(account.getId()); workJob.setUserId(user.getId()); - workJob.setVmType(VirtualMachine.Type.Instance); - workJob.setVmInstanceId(vm.getId()); + workJob.setVmType(vmType); + workJob.setVmInstanceId(vmId); workJob.setRelated(AsyncJobExecutionContext.getOriginJobId()); - final VmWorkStorageMigration workInfo = new VmWorkStorageMigration(user.getId(), account.getId(), vm.getId(), - VirtualMachineManagerImpl.VM_WORK_JOB_HANDLER, volumeToPool); + final VmWorkStorageMigration workInfo = new VmWorkStorageMigration(user.getId(), account.getId(), vmId, VirtualMachineManagerImpl.VM_WORK_JOB_HANDLER, volumeToPool); + workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo)); - _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vm.getId()); + _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vmId); } AsyncJobExecutionContext.getCurrentExecutionContext().joinJob(workJob.getId()); - return new VmJobVirtualMachineOutcome(workJob, vm.getId()); + return new VmJobVirtualMachineOutcome(workJob, vmId); } public Outcome addVmToNetworkThroughJobQueue( @@ -5192,36 +5168,34 @@ public Outcome addVmToNetworkThroughJobQueue( final User user = context.getCallingUser(); final Account account = context.getCallingAccount(); - final List pendingWorkJobs = _workJobDao.listPendingWorkJobs( - VirtualMachine.Type.Instance, vm.getId(), - VmWorkAddVmToNetwork.class.getName()); + Long vmId = vm.getId(); + VirtualMachine.Type vmType = VirtualMachine.Type.Instance; + String commandName = VmWorkAddVmToNetwork.class.getName(); + Pair pendingWorkJob = retrievePendingWorkJob(vmId, null, vmType, commandName); - VmWorkJobVO workJob = null; - if (CollectionUtils.isNotEmpty(pendingWorkJobs)) { - assert pendingWorkJobs.size() == 1; - workJob = pendingWorkJobs.get(0); - } else { + VmWorkJobVO workJob = pendingWorkJob.first(); + if (workJob == null) { workJob = new VmWorkJobVO(context.getContextId()); workJob.setDispatcher(VmWorkConstants.VM_WORK_JOB_DISPATCHER); - workJob.setCmd(VmWorkAddVmToNetwork.class.getName()); + workJob.setCmd(commandName); workJob.setAccountId(account.getId()); workJob.setUserId(user.getId()); - workJob.setVmType(VirtualMachine.Type.Instance); - workJob.setVmInstanceId(vm.getId()); + workJob.setVmType(vmType); + workJob.setVmInstanceId(vmId); workJob.setRelated(AsyncJobExecutionContext.getOriginJobId()); - final VmWorkAddVmToNetwork workInfo = new VmWorkAddVmToNetwork(user.getId(), account.getId(), vm.getId(), + final VmWorkAddVmToNetwork workInfo = new VmWorkAddVmToNetwork(user.getId(), account.getId(), vmId, VirtualMachineManagerImpl.VM_WORK_JOB_HANDLER, network.getId(), requested); workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo)); - _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vm.getId()); + _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vmId); } AsyncJobExecutionContext.getCurrentExecutionContext().joinJob(workJob.getId()); - return new VmJobVirtualMachineOutcome(workJob, vm.getId()); + return new VmJobVirtualMachineOutcome(workJob, vmId); } public Outcome removeNicFromVmThroughJobQueue( @@ -5231,36 +5205,34 @@ public Outcome removeNicFromVmThroughJobQueue( final User user = context.getCallingUser(); final Account account = context.getCallingAccount(); - final List pendingWorkJobs = _workJobDao.listPendingWorkJobs( - VirtualMachine.Type.Instance, vm.getId(), - VmWorkRemoveNicFromVm.class.getName()); + Long vmId = vm.getId(); + VirtualMachine.Type vmType = VirtualMachine.Type.Instance; + String commandName = VmWorkRemoveNicFromVm.class.getName(); + Pair pendingWorkJob = retrievePendingWorkJob(vmId, null, vmType, commandName); - VmWorkJobVO workJob = null; - if (CollectionUtils.isNotEmpty(pendingWorkJobs)) { - assert pendingWorkJobs.size() == 1; - workJob = pendingWorkJobs.get(0); - } else { + VmWorkJobVO workJob = pendingWorkJob.first(); + if (workJob == null) { workJob = new VmWorkJobVO(context.getContextId()); workJob.setDispatcher(VmWorkConstants.VM_WORK_JOB_DISPATCHER); - workJob.setCmd(VmWorkRemoveNicFromVm.class.getName()); + workJob.setCmd(commandName); workJob.setAccountId(account.getId()); workJob.setUserId(user.getId()); - workJob.setVmType(VirtualMachine.Type.Instance); - workJob.setVmInstanceId(vm.getId()); + workJob.setVmType(vmType); + workJob.setVmInstanceId(vmId); workJob.setRelated(AsyncJobExecutionContext.getOriginJobId()); - final VmWorkRemoveNicFromVm workInfo = new VmWorkRemoveNicFromVm(user.getId(), account.getId(), vm.getId(), + final VmWorkRemoveNicFromVm workInfo = new VmWorkRemoveNicFromVm(user.getId(), account.getId(), vmId, VirtualMachineManagerImpl.VM_WORK_JOB_HANDLER, nic.getId()); workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo)); - _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vm.getId()); + _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vmId); } AsyncJobExecutionContext.getCurrentExecutionContext().joinJob(workJob.getId()); - return new VmJobVirtualMachineOutcome(workJob, vm.getId()); + return new VmJobVirtualMachineOutcome(workJob, vmId); } public Outcome removeVmFromNetworkThroughJobQueue( @@ -5270,37 +5242,35 @@ public Outcome removeVmFromNetworkThroughJobQueue( final User user = context.getCallingUser(); final Account account = context.getCallingAccount(); - final List pendingWorkJobs = _workJobDao.listPendingWorkJobs( - VirtualMachine.Type.Instance, vm.getId(), - VmWorkRemoveVmFromNetwork.class.getName()); + Long vmId = vm.getId(); + VirtualMachine.Type vmType = VirtualMachine.Type.Instance; + String commandName = VmWorkRemoveVmFromNetwork.class.getName(); + Pair pendingWorkJob = retrievePendingWorkJob(vmId, null, vmType, commandName); - VmWorkJobVO workJob = null; - if (CollectionUtils.isNotEmpty(pendingWorkJobs)) { - assert pendingWorkJobs.size() == 1; - workJob = pendingWorkJobs.get(0); - } else { + VmWorkJobVO workJob = pendingWorkJob.first(); + if (workJob == null) { workJob = new VmWorkJobVO(context.getContextId()); workJob.setDispatcher(VmWorkConstants.VM_WORK_JOB_DISPATCHER); - workJob.setCmd(VmWorkRemoveVmFromNetwork.class.getName()); + workJob.setCmd(commandName); workJob.setAccountId(account.getId()); workJob.setUserId(user.getId()); - workJob.setVmType(VirtualMachine.Type.Instance); - workJob.setVmInstanceId(vm.getId()); + workJob.setVmType(vmType); + workJob.setVmInstanceId(vmId); workJob.setRelated(AsyncJobExecutionContext.getOriginJobId()); - final VmWorkRemoveVmFromNetwork workInfo = new VmWorkRemoveVmFromNetwork(user.getId(), account.getId(), vm.getId(), + final VmWorkRemoveVmFromNetwork workInfo = new VmWorkRemoveVmFromNetwork(user.getId(), account.getId(), vmId, VirtualMachineManagerImpl.VM_WORK_JOB_HANDLER, network, broadcastUri); workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo)); - _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vm.getId()); + _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vmId); } AsyncJobExecutionContext.getCurrentExecutionContext().joinJob(workJob.getId()); - return new VmJobVirtualMachineOutcome(workJob, vm.getId()); + return new VmJobVirtualMachineOutcome(workJob, vmId); } public Outcome reconfigureVmThroughJobQueue( @@ -5310,38 +5280,34 @@ public Outcome reconfigureVmThroughJobQueue( final User user = context.getCallingUser(); final Account account = context.getCallingAccount(); - final VMInstanceVO vm = _vmDao.findByUuid(vmUuid); - - final List pendingWorkJobs = _workJobDao.listPendingWorkJobs( - VirtualMachine.Type.Instance, vm.getId(), - VmWorkReconfigure.class.getName()); + VirtualMachine.Type vmType = VirtualMachine.Type.Instance; + String commandName = VmWorkReconfigure.class.getName(); + Pair pendingWorkJob = retrievePendingWorkJob(vmUuid, vmType, commandName); - VmWorkJobVO workJob = null; - if (CollectionUtils.isNotEmpty(pendingWorkJobs)) { - assert pendingWorkJobs.size() == 1; - workJob = pendingWorkJobs.get(0); - } else { + VmWorkJobVO workJob = pendingWorkJob.first(); + Long vmId = pendingWorkJob.second(); + if (workJob == null) { workJob = new VmWorkJobVO(context.getContextId()); workJob.setDispatcher(VmWorkConstants.VM_WORK_JOB_DISPATCHER); - workJob.setCmd(VmWorkReconfigure.class.getName()); + workJob.setCmd(commandName); workJob.setAccountId(account.getId()); workJob.setUserId(user.getId()); - workJob.setVmType(VirtualMachine.Type.Instance); - workJob.setVmInstanceId(vm.getId()); + workJob.setVmType(vmType); + workJob.setVmInstanceId(vmId); workJob.setRelated(AsyncJobExecutionContext.getOriginJobId()); - final VmWorkReconfigure workInfo = new VmWorkReconfigure(user.getId(), account.getId(), vm.getId(), + final VmWorkReconfigure workInfo = new VmWorkReconfigure(user.getId(), account.getId(), vmId, VirtualMachineManagerImpl.VM_WORK_JOB_HANDLER, oldServiceOffering.getId(), newServiceOffering.getId(), customParameters, reconfiguringOnExistingHost); workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo)); - _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vm.getId()); + _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vmId); } AsyncJobExecutionContext.getCurrentExecutionContext().joinJob(workJob.getId()); - return new VmJobVirtualMachineOutcome(workJob, vm.getId()); + return new VmJobVirtualMachineOutcome(workJob, vmId); } @ReflectionUse @@ -5601,24 +5567,21 @@ public Outcome restoreVirtualMachineThroughJobQueue(final long v final User user = context.getCallingUser(); final Account account = context.getCallingAccount(); - final List pendingWorkJobs = _workJobDao.listPendingWorkJobs( - VirtualMachine.Type.Instance, vmId, - VmWorkRestore.class.getName()); + VirtualMachine.Type vmType = VirtualMachine.Type.Instance; + String commandName = VmWorkRestore.class.getName(); + Pair pendingWorkJob = retrievePendingWorkJob(vmId, null, vmType, commandName); - VmWorkJobVO workJob = null; - if (CollectionUtils.isNotEmpty(pendingWorkJobs)) { - assert pendingWorkJobs.size() == 1; - workJob = pendingWorkJobs.get(0); - } else { + VmWorkJobVO workJob = pendingWorkJob.first(); + if (workJob == null) { workJob = new VmWorkJobVO(context.getContextId()); workJob.setDispatcher(VmWorkConstants.VM_WORK_JOB_DISPATCHER); - workJob.setCmd(VmWorkRestore.class.getName()); + workJob.setCmd(commandName); workJob.setAccountId(account.getId()); workJob.setUserId(user.getId()); - workJob.setVmType(VirtualMachine.Type.Instance); + workJob.setVmType(vmType); workJob.setVmInstanceId(vmId); workJob.setRelated(AsyncJobExecutionContext.getOriginJobId()); @@ -5702,36 +5665,34 @@ public Outcome updateDefaultNicForVMThroughJobQueue(final Virtua final User user = context.getCallingUser(); final Account account = context.getCallingAccount(); - final List pendingWorkJobs = _workJobDao.listPendingWorkJobs( - VirtualMachine.Type.Instance, vm.getId(), - VmWorkUpdateDefaultNic.class.getName()); + Long vmId = vm.getId(); + VirtualMachine.Type vmType = VirtualMachine.Type.Instance; + String commandName = VmWorkUpdateDefaultNic.class.getName(); + Pair pendingWorkJob = retrievePendingWorkJob(vmId, null, vmType, commandName); - VmWorkJobVO workJob = null; - if (CollectionUtils.isNotEmpty(pendingWorkJobs)) { - assert pendingWorkJobs.size() == 1; - workJob = pendingWorkJobs.get(0); - } else { + VmWorkJobVO workJob = pendingWorkJob.first(); + if (workJob == null) { workJob = new VmWorkJobVO(context.getContextId()); workJob.setDispatcher(VmWorkConstants.VM_WORK_JOB_DISPATCHER); - workJob.setCmd(VmWorkUpdateDefaultNic.class.getName()); + workJob.setCmd(commandName); workJob.setAccountId(account.getId()); workJob.setUserId(user.getId()); - workJob.setVmType(VirtualMachine.Type.Instance); - workJob.setVmInstanceId(vm.getId()); + workJob.setVmType(vmType); + workJob.setVmInstanceId(vmId); workJob.setRelated(AsyncJobExecutionContext.getOriginJobId()); - final VmWorkUpdateDefaultNic workInfo = new VmWorkUpdateDefaultNic(user.getId(), account.getId(), vm.getId(), + final VmWorkUpdateDefaultNic workInfo = new VmWorkUpdateDefaultNic(user.getId(), account.getId(), vmId, VirtualMachineManagerImpl.VM_WORK_JOB_HANDLER, nic.getId(), defaultNic.getId()); workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo)); - _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vm.getId()); + _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vmId); } AsyncJobExecutionContext.getCurrentExecutionContext().joinJob(workJob.getId()); - return new VmJobVirtualMachineOutcome(workJob, vm.getId()); + return new VmJobVirtualMachineOutcome(workJob, vmId); } @ReflectionUse @@ -5849,4 +5810,35 @@ protected Object retrieveResultFromJobOutcomeAndThrowExceptionIfNeeded(Outcome retrievePendingWorkJob(String vmUuid, VirtualMachine.Type vmType, String commandName) { + return retrievePendingWorkJob(null, vmUuid, vmType, commandName); + } + + protected Pair retrievePendingWorkJob(Long vmId, String vmUuid, VirtualMachine.Type vmType, String commandName) { + if (vmId == null) { + VMInstanceVO vm = _vmDao.findByUuid(vmUuid); + + if (vm == null) { + String message = String.format("Could not find a VM with the uuid [%s]. Unable to continue validations with command [%s] through job queue.", vmUuid, commandName); + s_logger.error(message); + throw new RuntimeException(message); + } + + vmId = vm.getId(); + + if (vmType == null) { + vmType = vm.getType(); + } + } + + List pendingWorkJobs = _workJobDao.listPendingWorkJobs(vmType, vmId, commandName); + + if (CollectionUtils.isNotEmpty(pendingWorkJobs)) { + assert pendingWorkJobs.size() == 1; + return new Pair<>(pendingWorkJobs.get(0), vmId); + } + + return new Pair<>(null, vmId); + } } From abc4b476fb9c5ce3115326744124b642f13872ae Mon Sep 17 00:00:00 2001 From: Daniel Augusto Veronezi Salvador Date: Wed, 14 Apr 2021 15:35:46 -0300 Subject: [PATCH 11/25] Create new constructors --- .../src/main/java/com/cloud/vm/VmWork.java | 7 +++++ .../com/cloud/vm/VmWorkAddVmToNetwork.java | 6 +++++ .../main/java/com/cloud/vm/VmWorkMigrate.java | 27 ++++++++++++------- .../java/com/cloud/vm/VmWorkMigrateAway.java | 5 ++++ .../com/cloud/vm/VmWorkMigrateForScale.java | 5 ++++ .../cloud/vm/VmWorkMigrateWithStorage.java | 7 +++++ .../main/java/com/cloud/vm/VmWorkReboot.java | 5 ++++ .../java/com/cloud/vm/VmWorkReconfigure.java | 8 ++++++ .../com/cloud/vm/VmWorkRemoveNicFromVm.java | 5 ++++ .../cloud/vm/VmWorkRemoveVmFromNetwork.java | 6 +++++ .../main/java/com/cloud/vm/VmWorkRestore.java | 5 ++++ .../main/java/com/cloud/vm/VmWorkStart.java | 4 +++ .../main/java/com/cloud/vm/VmWorkStop.java | 5 ++++ .../com/cloud/vm/VmWorkStorageMigration.java | 7 ++++- .../com/cloud/vm/VmWorkUpdateDefaultNic.java | 6 +++++ 15 files changed, 98 insertions(+), 10 deletions(-) diff --git a/engine/components-api/src/main/java/com/cloud/vm/VmWork.java b/engine/components-api/src/main/java/com/cloud/vm/VmWork.java index ed9f44cb9bbb..a0b5e9795242 100644 --- a/engine/components-api/src/main/java/com/cloud/vm/VmWork.java +++ b/engine/components-api/src/main/java/com/cloud/vm/VmWork.java @@ -34,6 +34,13 @@ public VmWork(long userId, long accountId, long vmId, String handlerName) { this.handlerName = handlerName; } + public VmWork(VmWork vmWork) { + this.userId = vmWork.getUserId(); + this.accountId = vmWork.getAccountId(); + this.vmId = vmWork.getVmId(); + this.handlerName = vmWork.getHandlerName(); + } + public long getUserId() { return userId; } diff --git a/engine/orchestration/src/main/java/com/cloud/vm/VmWorkAddVmToNetwork.java b/engine/orchestration/src/main/java/com/cloud/vm/VmWorkAddVmToNetwork.java index a56259b93eac..4c2a89a4dbed 100644 --- a/engine/orchestration/src/main/java/com/cloud/vm/VmWorkAddVmToNetwork.java +++ b/engine/orchestration/src/main/java/com/cloud/vm/VmWorkAddVmToNetwork.java @@ -30,6 +30,12 @@ public VmWorkAddVmToNetwork(long userId, long accountId, long vmId, String handl requstedNicProfile = requested; } + public VmWorkAddVmToNetwork(VmWork vmWork, long networkId, NicProfile requested) { + super(vmWork); + this.networkId = networkId; + this.requstedNicProfile = requested; + } + public Long getNetworkId() { return networkId; } diff --git a/engine/orchestration/src/main/java/com/cloud/vm/VmWorkMigrate.java b/engine/orchestration/src/main/java/com/cloud/vm/VmWorkMigrate.java index 1d7deca2d9ef..253b750172cc 100644 --- a/engine/orchestration/src/main/java/com/cloud/vm/VmWorkMigrate.java +++ b/engine/orchestration/src/main/java/com/cloud/vm/VmWorkMigrate.java @@ -41,18 +41,27 @@ public class VmWorkMigrate extends VmWork { public VmWorkMigrate(long userId, long accountId, long vmId, String handlerName, long srcHostId, DeployDestination dst) { super(userId, accountId, vmId, handlerName); + setConstructorParams(srcHostId, dst); + } + + public VmWorkMigrate(VmWork vmWork, long srcHostId, DeployDestination dest) { + super(vmWork); + setConstructorParams(srcHostId, dest); + } + + private void setConstructorParams(long srcHostId, DeployDestination dest) { this.srcHostId = srcHostId; - zoneId = dst.getDataCenter() != null ? dst.getDataCenter().getId() : null; - podId = dst.getPod() != null ? dst.getPod().getId() : null; - clusterId = dst.getCluster() != null ? dst.getCluster().getId() : null; - hostId = dst.getHost() != null ? dst.getHost().getId() : null; - if (dst.getStorageForDisks() != null) { - storage = new HashMap(dst.getStorageForDisks().size()); - for (Map.Entry entry : dst.getStorageForDisks().entrySet()) { - storage.put(entry.getKey().getUuid(), entry.getValue().getUuid()); + this.zoneId = dest.getDataCenter() != null ? dest.getDataCenter().getId() : null; + this.podId = dest.getPod() != null ? dest.getPod().getId() : null; + this.clusterId = dest.getCluster() != null ? dest.getCluster().getId() : null; + this.hostId = dest.getHost() != null ? dest.getHost().getId() : null; + if (dest.getStorageForDisks() != null) { + this.storage = new HashMap<>(dest.getStorageForDisks().size()); + for (Map.Entry entry : dest.getStorageForDisks().entrySet()) { + this.storage.put(entry.getKey().getUuid(), entry.getValue().getUuid()); } } else { - storage = null; + this.storage = null; } } diff --git a/engine/orchestration/src/main/java/com/cloud/vm/VmWorkMigrateAway.java b/engine/orchestration/src/main/java/com/cloud/vm/VmWorkMigrateAway.java index 92189edca4d9..ef89129d1dfe 100644 --- a/engine/orchestration/src/main/java/com/cloud/vm/VmWorkMigrateAway.java +++ b/engine/orchestration/src/main/java/com/cloud/vm/VmWorkMigrateAway.java @@ -30,6 +30,11 @@ public VmWorkMigrateAway(long userId, long accountId, long vmId, String handlerN this.srcHostId = srcHostId; } + public VmWorkMigrateAway(VmWork vmWork, long srcHostId) { + super(vmWork); + this.srcHostId = srcHostId; + } + public long getSrcHostId() { return srcHostId; } diff --git a/engine/orchestration/src/main/java/com/cloud/vm/VmWorkMigrateForScale.java b/engine/orchestration/src/main/java/com/cloud/vm/VmWorkMigrateForScale.java index 2147a54a4ca4..35cfbbf758a1 100644 --- a/engine/orchestration/src/main/java/com/cloud/vm/VmWorkMigrateForScale.java +++ b/engine/orchestration/src/main/java/com/cloud/vm/VmWorkMigrateForScale.java @@ -30,6 +30,11 @@ public VmWorkMigrateForScale(long userId, long accountId, long vmId, String hand this.newSvcOfferingId = newSvcOfferingId; } + public VmWorkMigrateForScale(VmWork vmWork, long srcHostId, DeployDestination dest, Long newSvcOfferingId) { + super(vmWork, srcHostId, dest); + this.newSvcOfferingId = newSvcOfferingId; + } + public Long getNewServiceOfferringId() { return newSvcOfferingId; } diff --git a/engine/orchestration/src/main/java/com/cloud/vm/VmWorkMigrateWithStorage.java b/engine/orchestration/src/main/java/com/cloud/vm/VmWorkMigrateWithStorage.java index 52fe9f287711..3097800667a8 100644 --- a/engine/orchestration/src/main/java/com/cloud/vm/VmWorkMigrateWithStorage.java +++ b/engine/orchestration/src/main/java/com/cloud/vm/VmWorkMigrateWithStorage.java @@ -35,6 +35,13 @@ public VmWorkMigrateWithStorage(long userId, long accountId, long vmId, String h this.volumeToPool = volumeToPool; } + public VmWorkMigrateWithStorage(VmWork vmWork, long srcHostId, long destHostId, Map volumeToPool) { + super(vmWork); + this.srcHostId = srcHostId; + this.destHostId = destHostId; + this.volumeToPool = volumeToPool; + } + public long getSrcHostId() { return srcHostId; } diff --git a/engine/orchestration/src/main/java/com/cloud/vm/VmWorkReboot.java b/engine/orchestration/src/main/java/com/cloud/vm/VmWorkReboot.java index 63c7d0049682..6a903b3d781e 100644 --- a/engine/orchestration/src/main/java/com/cloud/vm/VmWorkReboot.java +++ b/engine/orchestration/src/main/java/com/cloud/vm/VmWorkReboot.java @@ -34,6 +34,11 @@ public VmWorkReboot(long userId, long accountId, long vmId, String handlerName, setParams(params); } + public VmWorkReboot(VmWork vmWork, Map params) { + super(vmWork); + setParams(params); + } + public Map getParams() { Map map = new HashMap(); diff --git a/engine/orchestration/src/main/java/com/cloud/vm/VmWorkReconfigure.java b/engine/orchestration/src/main/java/com/cloud/vm/VmWorkReconfigure.java index f96a587dac0f..fdbcb9f6f5e8 100644 --- a/engine/orchestration/src/main/java/com/cloud/vm/VmWorkReconfigure.java +++ b/engine/orchestration/src/main/java/com/cloud/vm/VmWorkReconfigure.java @@ -39,6 +39,14 @@ public VmWorkReconfigure(long userId, long accountId, long vmId, String handlerN this.sameHost = sameHost; } + public VmWorkReconfigure(VmWork vmWork, long oldServiceOfferingId, long newServiceOfferingId, Map customParameters, boolean sameHost) { + super(vmWork); + this.oldServiceOfferingId = oldServiceOfferingId; + this.newServiceOfferingId = newServiceOfferingId; + this.customParameters = customParameters; + this.sameHost = sameHost; + } + public Long getOldServiceOfferingId() { return oldServiceOfferingId; } diff --git a/engine/orchestration/src/main/java/com/cloud/vm/VmWorkRemoveNicFromVm.java b/engine/orchestration/src/main/java/com/cloud/vm/VmWorkRemoveNicFromVm.java index 50f158e421d3..acbdd22702ef 100644 --- a/engine/orchestration/src/main/java/com/cloud/vm/VmWorkRemoveNicFromVm.java +++ b/engine/orchestration/src/main/java/com/cloud/vm/VmWorkRemoveNicFromVm.java @@ -27,6 +27,11 @@ public VmWorkRemoveNicFromVm(long userId, long accountId, long vmId, String hand this.nicId = nicId; } + public VmWorkRemoveNicFromVm(VmWork vmWork, long nicId) { + super(vmWork); + this.nicId = nicId; + } + public Long getNicId() { return nicId; } diff --git a/engine/orchestration/src/main/java/com/cloud/vm/VmWorkRemoveVmFromNetwork.java b/engine/orchestration/src/main/java/com/cloud/vm/VmWorkRemoveVmFromNetwork.java index 535b8d00faab..719c7f9ef1c7 100644 --- a/engine/orchestration/src/main/java/com/cloud/vm/VmWorkRemoveVmFromNetwork.java +++ b/engine/orchestration/src/main/java/com/cloud/vm/VmWorkRemoveVmFromNetwork.java @@ -33,6 +33,12 @@ public VmWorkRemoveVmFromNetwork(long userId, long accountId, long vmId, String this.broadcastUri = broadcastUri; } + public VmWorkRemoveVmFromNetwork(VmWork vmWork, Network network, URI broadcastUri) { + super(vmWork); + this.network = network; + this.broadcastUri = broadcastUri; + } + public Network getNetwork() { return network; } diff --git a/engine/orchestration/src/main/java/com/cloud/vm/VmWorkRestore.java b/engine/orchestration/src/main/java/com/cloud/vm/VmWorkRestore.java index f8162e48ffad..cb3adae27aa3 100644 --- a/engine/orchestration/src/main/java/com/cloud/vm/VmWorkRestore.java +++ b/engine/orchestration/src/main/java/com/cloud/vm/VmWorkRestore.java @@ -27,6 +27,11 @@ public VmWorkRestore(long userId, long accountId, long vmId, String handlerName, this.templateId = templateId; } + public VmWorkRestore(VmWork vmWork, Long templateId) { + super(vmWork); + this.templateId = templateId; + } + public Long getTemplateId() { return templateId; } diff --git a/engine/orchestration/src/main/java/com/cloud/vm/VmWorkStart.java b/engine/orchestration/src/main/java/com/cloud/vm/VmWorkStart.java index 814e78c28301..1b2a7194d172 100644 --- a/engine/orchestration/src/main/java/com/cloud/vm/VmWorkStart.java +++ b/engine/orchestration/src/main/java/com/cloud/vm/VmWorkStart.java @@ -54,6 +54,10 @@ public VmWorkStart(long userId, long accountId, long vmId, String handlerName) { super(userId, accountId, vmId, handlerName); } + public VmWorkStart(VmWork vmWork){ + super(vmWork); + } + public DeploymentPlan getPlan() { if (podId != null || clusterId != null || hostId != null || poolId != null || physicalNetworkId != null || avoids !=null) { diff --git a/engine/orchestration/src/main/java/com/cloud/vm/VmWorkStop.java b/engine/orchestration/src/main/java/com/cloud/vm/VmWorkStop.java index 6d4148000c80..00ff7559cbaa 100644 --- a/engine/orchestration/src/main/java/com/cloud/vm/VmWorkStop.java +++ b/engine/orchestration/src/main/java/com/cloud/vm/VmWorkStop.java @@ -26,6 +26,11 @@ public VmWorkStop(long userId, long accountId, long vmId, String handlerName, bo this.cleanup = cleanup; } + public VmWorkStop(VmWork vmWork, boolean cleanup) { + super(vmWork); + this.cleanup = cleanup; + } + public boolean isCleanup() { return cleanup; } diff --git a/engine/orchestration/src/main/java/com/cloud/vm/VmWorkStorageMigration.java b/engine/orchestration/src/main/java/com/cloud/vm/VmWorkStorageMigration.java index 07e8549d2246..ce3ef9f121fd 100644 --- a/engine/orchestration/src/main/java/com/cloud/vm/VmWorkStorageMigration.java +++ b/engine/orchestration/src/main/java/com/cloud/vm/VmWorkStorageMigration.java @@ -23,7 +23,7 @@ public class VmWorkStorageMigration extends VmWork { Map volumeToPool; - public VmWorkStorageMigration(long userId, long accountId, long vmId, String handlerName, Map volumeToPool) { + public VmWorkStorageMigration(long userId, long accountId, long vmId, String handlerName, Map volumeToPool) { super(userId, accountId, vmId, handlerName); this.volumeToPool = volumeToPool; @@ -32,4 +32,9 @@ public VmWorkStorageMigration(long userId, long accountId, long vmId, String han public Map getVolumeToPool() { return volumeToPool; } + + public VmWorkStorageMigration(VmWork vmWork, Map volumeToPool) { + super(vmWork); + this.volumeToPool = volumeToPool; + } } diff --git a/engine/orchestration/src/main/java/com/cloud/vm/VmWorkUpdateDefaultNic.java b/engine/orchestration/src/main/java/com/cloud/vm/VmWorkUpdateDefaultNic.java index 14f323994493..b1a936d4c70b 100644 --- a/engine/orchestration/src/main/java/com/cloud/vm/VmWorkUpdateDefaultNic.java +++ b/engine/orchestration/src/main/java/com/cloud/vm/VmWorkUpdateDefaultNic.java @@ -29,6 +29,12 @@ public VmWorkUpdateDefaultNic(long userId, long accountId, long vmId, String han this.defaultNicId = defaultNicId; } + public VmWorkUpdateDefaultNic(VmWork vmWork, long nicId, long defaultNicId) { + super(vmWork); + this.nicId = nicId; + this.defaultNicId = defaultNicId; + } + public Long getNicId() { return nicId; } From aecf5f3bf82c0623b29434a9205ded926609e339 Mon Sep 17 00:00:00 2001 From: Daniel Augusto Veronezi Salvador Date: Wed, 14 Apr 2021 15:40:36 -0300 Subject: [PATCH 12/25] Extract work job and info creation to a method --- .../cloud/vm/VirtualMachineManagerImpl.java | 341 +++++------------- 1 file changed, 91 insertions(+), 250 deletions(-) diff --git a/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java b/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java index 69b019a7d9c1..f290f65cd954 100755 --- a/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java +++ b/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java @@ -4832,32 +4832,18 @@ protected VirtualMachine retrieve() { public Outcome startVmThroughJobQueue(final String vmUuid, final Map params, final DeploymentPlan planToDeploy, final DeploymentPlanner planner) { - - final CallContext context = CallContext.current(); - final User callingUser = context.getCallingUser(); - final Account callingAccount = context.getCallingAccount(); - - VirtualMachine.Type vmType = VirtualMachine.Type.Instance; String commandName = VmWorkStart.class.getName(); - Pair pendingWorkJob = retrievePendingWorkJob(vmUuid, vmType, commandName); + Pair pendingWorkJob = retrievePendingWorkJob(vmUuid, commandName); VmWorkJobVO workJob = pendingWorkJob.first(); Long vmId = pendingWorkJob.second(); if (workJob == null) { - workJob = new VmWorkJobVO(context.getContextId()); + Pair newVmWorkJobAndInfo = createWorkJobAndWorkInfo(commandName, VmWorkJobVO.Step.Starting, vmId); - workJob.setDispatcher(VmWorkConstants.VM_WORK_JOB_DISPATCHER); - workJob.setCmd(commandName); + workJob = newVmWorkJobAndInfo.first(); + VmWorkStart workInfo = new VmWorkStart(newVmWorkJobAndInfo.second()); - workJob.setAccountId(callingAccount.getId()); - workJob.setUserId(callingUser.getId()); - workJob.setStep(VmWorkJobVO.Step.Starting); - workJob.setVmType(vmType); - workJob.setVmInstanceId(vmId); - workJob.setRelated(AsyncJobExecutionContext.getOriginJobId()); - - final VmWorkStart workInfo = new VmWorkStart(callingUser.getId(), callingAccount.getId(), vmId, VirtualMachineManagerImpl.VM_WORK_JOB_HANDLER); workInfo.setPlan(planToDeploy); workInfo.setParams(params); if (planner != null) { @@ -4875,30 +4861,18 @@ public Outcome startVmThroughJobQueue(final String vmUuid, } public Outcome stopVmThroughJobQueue(final String vmUuid, final boolean cleanup) { - final CallContext context = CallContext.current(); - final Account account = context.getCallingAccount(); - final User user = context.getCallingUser(); - String commandName = VmWorkStop.class.getName(); - Pair pendingWorkJob = retrievePendingWorkJob(vmUuid, null, commandName); + Pair pendingWorkJob = retrievePendingWorkJob(null, vmUuid, null, commandName); VmWorkJobVO workJob = pendingWorkJob.first(); Long vmId = pendingWorkJob.second(); if (workJob == null) { - workJob = new VmWorkJobVO(context.getContextId()); - - workJob.setDispatcher(VmWorkConstants.VM_WORK_JOB_DISPATCHER); - workJob.setCmd(commandName); + Pair newVmWorkJobAndInfo = createWorkJobAndWorkInfo(commandName, VmWorkJobVO.Step.Prepare, vmId); - workJob.setAccountId(account.getId()); - workJob.setUserId(user.getId()); - workJob.setStep(VmWorkJobVO.Step.Prepare); - workJob.setVmType(VirtualMachine.Type.Instance); - workJob.setVmInstanceId(vmId); - workJob.setRelated(AsyncJobExecutionContext.getOriginJobId()); + workJob = newVmWorkJobAndInfo.first(); + VmWorkStop workInfo = new VmWorkStop(newVmWorkJobAndInfo.second(), cleanup); - final VmWorkStop workInfo = new VmWorkStop(user.getId(), account.getId(), vmId, VirtualMachineManagerImpl.VM_WORK_JOB_HANDLER, cleanup); workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo)); _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vmId); @@ -4912,32 +4886,18 @@ public Outcome stopVmThroughJobQueue(final String vmUuid, final public Outcome rebootVmThroughJobQueue(final String vmUuid, final Map params) { - - final CallContext context = CallContext.current(); - final Account account = context.getCallingAccount(); - final User user = context.getCallingUser(); - - VirtualMachine.Type vmType = VirtualMachine.Type.Instance; String commandName = VmWorkReboot.class.getName(); - Pair pendingWorkJob = retrievePendingWorkJob(vmUuid, vmType, commandName); + Pair pendingWorkJob = retrievePendingWorkJob(vmUuid, commandName); VmWorkJobVO workJob = pendingWorkJob.first(); Long vmId = pendingWorkJob.second(); if (workJob == null) { - workJob = new VmWorkJobVO(context.getContextId()); - - workJob.setDispatcher(VmWorkConstants.VM_WORK_JOB_DISPATCHER); - workJob.setCmd(commandName); + Pair newVmWorkJobAndInfo = createWorkJobAndWorkInfo(commandName, VmWorkJobVO.Step.Prepare, vmId); - workJob.setAccountId(account.getId()); - workJob.setUserId(user.getId()); - workJob.setStep(VmWorkJobVO.Step.Prepare); - workJob.setVmType(vmType); - workJob.setVmInstanceId(vmId); - workJob.setRelated(AsyncJobExecutionContext.getOriginJobId()); + workJob = newVmWorkJobAndInfo.first(); + VmWorkReboot workInfo = new VmWorkReboot(newVmWorkJobAndInfo.second(), params); - final VmWorkReboot workInfo = new VmWorkReboot(user.getId(), account.getId(), vmId, VirtualMachineManagerImpl.VM_WORK_JOB_HANDLER, params); workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo)); _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vmId); @@ -4950,10 +4910,6 @@ public Outcome rebootVmThroughJobQueue(final String vmUuid, } public Outcome migrateVmThroughJobQueue(final String vmUuid, final long srcHostId, final DeployDestination dest) { - final CallContext context = CallContext.current(); - final User user = context.getCallingUser(); - final Account account = context.getCallingAccount(); - Map volumeStorageMap = dest.getStorageForDisks(); if (volumeStorageMap != null) { for (Volume vol : volumeStorageMap.keySet()) { @@ -4964,25 +4920,17 @@ public Outcome migrateVmThroughJobQueue(final String vmUuid, fin VMInstanceVO vm = _vmDao.findByUuid(vmUuid); Long vmId = vm.getId(); - VirtualMachine.Type vmType = VirtualMachine.Type.Instance; String commandName = VmWorkMigrate.class.getName(); - Pair pendingWorkJob = retrievePendingWorkJob(vmId, vmUuid, vmType, commandName); + Pair pendingWorkJob = retrievePendingWorkJob(vmId, vmUuid, VirtualMachine.Type.Instance, commandName); VmWorkJobVO workJob = pendingWorkJob.first(); if (workJob == null) { - workJob = new VmWorkJobVO(context.getContextId()); - - workJob.setDispatcher(VmWorkConstants.VM_WORK_JOB_DISPATCHER); - workJob.setCmd(commandName); + Pair newVmWorkJobAndInfo = createWorkJobAndWorkInfo(commandName, vmId); - workJob.setAccountId(account.getId()); - workJob.setUserId(user.getId()); - workJob.setVmType(vmType); - workJob.setVmInstanceId(vmId); - workJob.setRelated(AsyncJobExecutionContext.getOriginJobId()); + workJob = newVmWorkJobAndInfo.first(); + VmWorkMigrate workInfo = new VmWorkMigrate(newVmWorkJobAndInfo.second(), srcHostId, dest); - final VmWorkMigrate workInfo = new VmWorkMigrate(user.getId(), account.getId(), vmId, VirtualMachineManagerImpl.VM_WORK_JOB_HANDLER, srcHostId, dest); workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo)); _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vmId); @@ -4995,32 +4943,20 @@ public Outcome migrateVmThroughJobQueue(final String vmUuid, fin } public Outcome migrateVmAwayThroughJobQueue(final String vmUuid, final long srcHostId) { - final CallContext context = CallContext.current(); - final User user = context.getCallingUser(); - final Account account = context.getCallingAccount(); - VMInstanceVO vm = _vmDao.findByUuid(vmUuid); Long vmId = vm.getId(); - VirtualMachine.Type vmType = VirtualMachine.Type.Instance; String commandName = VmWorkMigrateAway.class.getName(); - Pair pendingWorkJob = retrievePendingWorkJob(vmId, vmUuid, vmType, commandName); + Pair pendingWorkJob = retrievePendingWorkJob(vmId, vmUuid, VirtualMachine.Type.Instance, commandName); VmWorkJobVO workJob = pendingWorkJob.first(); if (workJob == null) { - workJob = new VmWorkJobVO(context.getContextId()); + Pair newVmWorkJobAndInfo = createWorkJobAndWorkInfo(commandName, vmId); - workJob.setDispatcher(VmWorkConstants.VM_WORK_JOB_DISPATCHER); - workJob.setCmd(commandName); + workJob = newVmWorkJobAndInfo.first(); + VmWorkMigrateAway workInfo = new VmWorkMigrateAway(newVmWorkJobAndInfo.second(), srcHostId); - workJob.setAccountId(account.getId()); - workJob.setUserId(user.getId()); - workJob.setVmType(vmType); - workJob.setVmInstanceId(vmId); - workJob.setRelated(AsyncJobExecutionContext.getOriginJobId()); - - final VmWorkMigrateAway workInfo = new VmWorkMigrateAway(user.getId(), account.getId(), vmId, VirtualMachineManagerImpl.VM_WORK_JOB_HANDLER, srcHostId); workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo)); } @@ -5034,32 +4970,18 @@ public Outcome migrateVmAwayThroughJobQueue(final String vmUuid, public Outcome migrateVmWithStorageThroughJobQueue( final String vmUuid, final long srcHostId, final long destHostId, final Map volumeToPool) { - - final CallContext context = CallContext.current(); - final User user = context.getCallingUser(); - final Account account = context.getCallingAccount(); - - VirtualMachine.Type vmType = VirtualMachine.Type.Instance; String commandName = VmWorkMigrateWithStorage.class.getName(); - Pair pendingWorkJob = retrievePendingWorkJob(vmUuid, vmType, commandName); + Pair pendingWorkJob = retrievePendingWorkJob(vmUuid, commandName); VmWorkJobVO workJob = pendingWorkJob.first(); Long vmId = pendingWorkJob.second(); if (workJob == null) { - workJob = new VmWorkJobVO(context.getContextId()); + Pair newVmWorkJobAndInfo = createWorkJobAndWorkInfo(commandName, vmId); - workJob.setDispatcher(VmWorkConstants.VM_WORK_JOB_DISPATCHER); - workJob.setCmd(commandName); + workJob = newVmWorkJobAndInfo.first(); + VmWorkMigrateWithStorage workInfo = new VmWorkMigrateWithStorage(newVmWorkJobAndInfo.second(), srcHostId, destHostId, volumeToPool); - workJob.setAccountId(account.getId()); - workJob.setUserId(user.getId()); - workJob.setVmType(vmType); - workJob.setVmInstanceId(vmId); - workJob.setRelated(AsyncJobExecutionContext.getOriginJobId()); - - final VmWorkMigrateWithStorage workInfo = new VmWorkMigrateWithStorage(user.getId(), account.getId(), vmId, - VirtualMachineManagerImpl.VM_WORK_JOB_HANDLER, srcHostId, destHostId, volumeToPool); workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo)); _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vmId); @@ -5072,32 +4994,18 @@ public Outcome migrateVmWithStorageThroughJobQueue( public Outcome migrateVmForScaleThroughJobQueue( final String vmUuid, final long srcHostId, final DeployDestination dest, final Long newSvcOfferingId) { - - final CallContext context = CallContext.current(); - final User user = context.getCallingUser(); - final Account account = context.getCallingAccount(); - - VirtualMachine.Type vmType = VirtualMachine.Type.Instance; String commandName = VmWorkMigrateForScale.class.getName(); - Pair pendingWorkJob = retrievePendingWorkJob(vmUuid, vmType, commandName); + Pair pendingWorkJob = retrievePendingWorkJob(vmUuid, commandName); VmWorkJobVO workJob = pendingWorkJob.first(); Long vmId = pendingWorkJob.second(); if (workJob == null) { - workJob = new VmWorkJobVO(context.getContextId()); + Pair newVmWorkJobAndInfo = createWorkJobAndWorkInfo(commandName, vmId); - workJob.setDispatcher(VmWorkConstants.VM_WORK_JOB_DISPATCHER); - workJob.setCmd(commandName); + workJob = newVmWorkJobAndInfo.first(); + VmWorkMigrateForScale workInfo = new VmWorkMigrateForScale(newVmWorkJobAndInfo.second(), srcHostId, dest, newSvcOfferingId); - workJob.setAccountId(account.getId()); - workJob.setUserId(user.getId()); - workJob.setVmType(vmType); - workJob.setVmInstanceId(vmId); - workJob.setRelated(AsyncJobExecutionContext.getOriginJobId()); - - final VmWorkMigrateForScale workInfo = new VmWorkMigrateForScale(user.getId(), account.getId(), vmId, - VirtualMachineManagerImpl.VM_WORK_JOB_HANDLER, srcHostId, dest, newSvcOfferingId); workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo)); _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vmId); @@ -5117,13 +5025,7 @@ private void checkConcurrentJobsPerDatastoreThreshhold(final StoragePool destPoo } } - public Outcome migrateVmStorageThroughJobQueue( - final String vmUuid, final Map volumeToPool) { - - final CallContext context = CallContext.current(); - final User user = context.getCallingUser(); - final Account account = context.getCallingAccount(); - + public Outcome migrateVmStorageThroughJobQueue(final String vmUuid, final Map volumeToPool) { Collection poolIds = volumeToPool.values(); Set uniquePoolIds = new HashSet<>(poolIds); for (Long poolId : uniquePoolIds) { @@ -5131,26 +5033,17 @@ public Outcome migrateVmStorageThroughJobQueue( checkConcurrentJobsPerDatastoreThreshhold(pool); } - VirtualMachine.Type vmType = VirtualMachine.Type.Instance; String commandName = VmWorkStorageMigration.class.getName(); - Pair pendingWorkJob = retrievePendingWorkJob(vmUuid, vmType, commandName); + Pair pendingWorkJob = retrievePendingWorkJob(vmUuid, commandName); VmWorkJobVO workJob = pendingWorkJob.first(); Long vmId = pendingWorkJob.second(); if (workJob == null) { - workJob = new VmWorkJobVO(context.getContextId()); + Pair newVmWorkJobAndInfo = createWorkJobAndWorkInfo(commandName, vmId); - workJob.setDispatcher(VmWorkConstants.VM_WORK_JOB_DISPATCHER); - workJob.setCmd(commandName); - - workJob.setAccountId(account.getId()); - workJob.setUserId(user.getId()); - workJob.setVmType(vmType); - workJob.setVmInstanceId(vmId); - workJob.setRelated(AsyncJobExecutionContext.getOriginJobId()); - - final VmWorkStorageMigration workInfo = new VmWorkStorageMigration(user.getId(), account.getId(), vmId, VirtualMachineManagerImpl.VM_WORK_JOB_HANDLER, volumeToPool); + workJob = newVmWorkJobAndInfo.first(); + VmWorkStorageMigration workInfo = new VmWorkStorageMigration(newVmWorkJobAndInfo.second(), volumeToPool); workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo)); @@ -5163,32 +5056,18 @@ public Outcome migrateVmStorageThroughJobQueue( public Outcome addVmToNetworkThroughJobQueue( final VirtualMachine vm, final Network network, final NicProfile requested) { - - final CallContext context = CallContext.current(); - final User user = context.getCallingUser(); - final Account account = context.getCallingAccount(); - Long vmId = vm.getId(); - VirtualMachine.Type vmType = VirtualMachine.Type.Instance; String commandName = VmWorkAddVmToNetwork.class.getName(); - Pair pendingWorkJob = retrievePendingWorkJob(vmId, null, vmType, commandName); + Pair pendingWorkJob = retrievePendingWorkJob(vmId, commandName); VmWorkJobVO workJob = pendingWorkJob.first(); if (workJob == null) { - workJob = new VmWorkJobVO(context.getContextId()); - - workJob.setDispatcher(VmWorkConstants.VM_WORK_JOB_DISPATCHER); - workJob.setCmd(commandName); + Pair newVmWorkJobAndInfo = createWorkJobAndWorkInfo(commandName, vmId); - workJob.setAccountId(account.getId()); - workJob.setUserId(user.getId()); - workJob.setVmType(vmType); - workJob.setVmInstanceId(vmId); - workJob.setRelated(AsyncJobExecutionContext.getOriginJobId()); + workJob = newVmWorkJobAndInfo.first(); + VmWorkAddVmToNetwork workInfo = new VmWorkAddVmToNetwork(newVmWorkJobAndInfo.second(), network.getId(), requested); - final VmWorkAddVmToNetwork workInfo = new VmWorkAddVmToNetwork(user.getId(), account.getId(), vmId, - VirtualMachineManagerImpl.VM_WORK_JOB_HANDLER, network.getId(), requested); workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo)); _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vmId); @@ -5200,32 +5079,18 @@ public Outcome addVmToNetworkThroughJobQueue( public Outcome removeNicFromVmThroughJobQueue( final VirtualMachine vm, final Nic nic) { - - final CallContext context = CallContext.current(); - final User user = context.getCallingUser(); - final Account account = context.getCallingAccount(); - Long vmId = vm.getId(); - VirtualMachine.Type vmType = VirtualMachine.Type.Instance; String commandName = VmWorkRemoveNicFromVm.class.getName(); - Pair pendingWorkJob = retrievePendingWorkJob(vmId, null, vmType, commandName); + Pair pendingWorkJob = retrievePendingWorkJob(vmId, commandName); VmWorkJobVO workJob = pendingWorkJob.first(); if (workJob == null) { - workJob = new VmWorkJobVO(context.getContextId()); - - workJob.setDispatcher(VmWorkConstants.VM_WORK_JOB_DISPATCHER); - workJob.setCmd(commandName); + Pair newVmWorkJobAndInfo = createWorkJobAndWorkInfo(commandName, vmId); - workJob.setAccountId(account.getId()); - workJob.setUserId(user.getId()); - workJob.setVmType(vmType); - workJob.setVmInstanceId(vmId); - workJob.setRelated(AsyncJobExecutionContext.getOriginJobId()); + workJob = newVmWorkJobAndInfo.first(); + VmWorkRemoveNicFromVm workInfo = new VmWorkRemoveNicFromVm(newVmWorkJobAndInfo.second(), nic.getId()); - final VmWorkRemoveNicFromVm workInfo = new VmWorkRemoveNicFromVm(user.getId(), account.getId(), vmId, - VirtualMachineManagerImpl.VM_WORK_JOB_HANDLER, nic.getId()); workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo)); _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vmId); @@ -5237,32 +5102,18 @@ public Outcome removeNicFromVmThroughJobQueue( public Outcome removeVmFromNetworkThroughJobQueue( final VirtualMachine vm, final Network network, final URI broadcastUri) { - - final CallContext context = CallContext.current(); - final User user = context.getCallingUser(); - final Account account = context.getCallingAccount(); - Long vmId = vm.getId(); - VirtualMachine.Type vmType = VirtualMachine.Type.Instance; String commandName = VmWorkRemoveVmFromNetwork.class.getName(); - Pair pendingWorkJob = retrievePendingWorkJob(vmId, null, vmType, commandName); + Pair pendingWorkJob = retrievePendingWorkJob(vmId, commandName); VmWorkJobVO workJob = pendingWorkJob.first(); if (workJob == null) { - workJob = new VmWorkJobVO(context.getContextId()); - - workJob.setDispatcher(VmWorkConstants.VM_WORK_JOB_DISPATCHER); - workJob.setCmd(commandName); + Pair newVmWorkJobAndInfo = createWorkJobAndWorkInfo(commandName, vmId); - workJob.setAccountId(account.getId()); - workJob.setUserId(user.getId()); - workJob.setVmType(vmType); - workJob.setVmInstanceId(vmId); - workJob.setRelated(AsyncJobExecutionContext.getOriginJobId()); + workJob = newVmWorkJobAndInfo.first(); + VmWorkRemoveVmFromNetwork workInfo = new VmWorkRemoveVmFromNetwork(newVmWorkJobAndInfo.second(), network, broadcastUri); - final VmWorkRemoveVmFromNetwork workInfo = new VmWorkRemoveVmFromNetwork(user.getId(), account.getId(), vmId, - VirtualMachineManagerImpl.VM_WORK_JOB_HANDLER, network, broadcastUri); workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo)); _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vmId); @@ -5275,32 +5126,18 @@ public Outcome removeVmFromNetworkThroughJobQueue( public Outcome reconfigureVmThroughJobQueue( final String vmUuid, final ServiceOffering oldServiceOffering, final ServiceOffering newServiceOffering, Map customParameters, final boolean reconfiguringOnExistingHost) { - - final CallContext context = CallContext.current(); - final User user = context.getCallingUser(); - final Account account = context.getCallingAccount(); - - VirtualMachine.Type vmType = VirtualMachine.Type.Instance; String commandName = VmWorkReconfigure.class.getName(); - Pair pendingWorkJob = retrievePendingWorkJob(vmUuid, vmType, commandName); + Pair pendingWorkJob = retrievePendingWorkJob(vmUuid, commandName); VmWorkJobVO workJob = pendingWorkJob.first(); Long vmId = pendingWorkJob.second(); if (workJob == null) { - workJob = new VmWorkJobVO(context.getContextId()); - - workJob.setDispatcher(VmWorkConstants.VM_WORK_JOB_DISPATCHER); - workJob.setCmd(commandName); + Pair newVmWorkJobAndInfo = createWorkJobAndWorkInfo(commandName, vmId); - workJob.setAccountId(account.getId()); - workJob.setUserId(user.getId()); - workJob.setVmType(vmType); - workJob.setVmInstanceId(vmId); - workJob.setRelated(AsyncJobExecutionContext.getOriginJobId()); + workJob = newVmWorkJobAndInfo.first(); + VmWorkReconfigure workInfo = new VmWorkReconfigure(newVmWorkJobAndInfo.second(), oldServiceOffering.getId(), newServiceOffering.getId(), customParameters, reconfiguringOnExistingHost); - final VmWorkReconfigure workInfo = new VmWorkReconfigure(user.getId(), account.getId(), vmId, - VirtualMachineManagerImpl.VM_WORK_JOB_HANDLER, oldServiceOffering.getId(), newServiceOffering.getId(), customParameters, reconfiguringOnExistingHost); workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo)); _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vmId); @@ -5562,31 +5399,17 @@ private UserVm orchestrateRestoreVirtualMachine(final long vmId, final Long newT } public Outcome restoreVirtualMachineThroughJobQueue(final long vmId, final Long newTemplateId) { - - final CallContext context = CallContext.current(); - final User user = context.getCallingUser(); - final Account account = context.getCallingAccount(); - - VirtualMachine.Type vmType = VirtualMachine.Type.Instance; String commandName = VmWorkRestore.class.getName(); - Pair pendingWorkJob = retrievePendingWorkJob(vmId, null, vmType, commandName); + Pair pendingWorkJob = retrievePendingWorkJob(vmId, commandName); VmWorkJobVO workJob = pendingWorkJob.first(); if (workJob == null) { - workJob = new VmWorkJobVO(context.getContextId()); - - workJob.setDispatcher(VmWorkConstants.VM_WORK_JOB_DISPATCHER); - workJob.setCmd(commandName); + Pair newVmWorkJobAndInfo = createWorkJobAndWorkInfo(commandName, vmId); - workJob.setAccountId(account.getId()); - workJob.setUserId(user.getId()); - workJob.setVmType(vmType); - workJob.setVmInstanceId(vmId); - workJob.setRelated(AsyncJobExecutionContext.getOriginJobId()); + workJob = newVmWorkJobAndInfo.first(); + VmWorkRestore workInfo = new VmWorkRestore(newVmWorkJobAndInfo.second(), newTemplateId); - final VmWorkRestore workInfo = new VmWorkRestore(user.getId(), account.getId(), vmId, - VirtualMachineManagerImpl.VM_WORK_JOB_HANDLER, newTemplateId); workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo)); _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vmId); @@ -5660,32 +5483,18 @@ private Boolean orchestrateUpdateDefaultNicForVM(final VirtualMachine vm, final } public Outcome updateDefaultNicForVMThroughJobQueue(final VirtualMachine vm, final Nic nic, final Nic defaultNic) { - - final CallContext context = CallContext.current(); - final User user = context.getCallingUser(); - final Account account = context.getCallingAccount(); - Long vmId = vm.getId(); - VirtualMachine.Type vmType = VirtualMachine.Type.Instance; String commandName = VmWorkUpdateDefaultNic.class.getName(); - Pair pendingWorkJob = retrievePendingWorkJob(vmId, null, vmType, commandName); + Pair pendingWorkJob = retrievePendingWorkJob(vmId, commandName); VmWorkJobVO workJob = pendingWorkJob.first(); if (workJob == null) { - workJob = new VmWorkJobVO(context.getContextId()); - - workJob.setDispatcher(VmWorkConstants.VM_WORK_JOB_DISPATCHER); - workJob.setCmd(commandName); + Pair newVmWorkJobAndInfo = createWorkJobAndWorkInfo(commandName, vmId); - workJob.setAccountId(account.getId()); - workJob.setUserId(user.getId()); - workJob.setVmType(vmType); - workJob.setVmInstanceId(vmId); - workJob.setRelated(AsyncJobExecutionContext.getOriginJobId()); + workJob = newVmWorkJobAndInfo.first(); + VmWorkUpdateDefaultNic workInfo = new VmWorkUpdateDefaultNic(newVmWorkJobAndInfo.second(), nic.getId(), defaultNic.getId()); - final VmWorkUpdateDefaultNic workInfo = new VmWorkUpdateDefaultNic(user.getId(), account.getId(), vmId, - VirtualMachineManagerImpl.VM_WORK_JOB_HANDLER, nic.getId(), defaultNic.getId()); workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo)); _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vmId); @@ -5811,8 +5620,12 @@ protected Object retrieveResultFromJobOutcomeAndThrowExceptionIfNeeded(Outcome retrievePendingWorkJob(String vmUuid, VirtualMachine.Type vmType, String commandName) { - return retrievePendingWorkJob(null, vmUuid, vmType, commandName); + protected Pair retrievePendingWorkJob(String vmUuid, String commandName) { + return retrievePendingWorkJob(null, vmUuid, VirtualMachine.Type.Instance, commandName); + } + + protected Pair retrievePendingWorkJob(Long id, String commandName) { + return retrievePendingWorkJob(id, null, VirtualMachine.Type.Instance, commandName); } protected Pair retrievePendingWorkJob(Long vmId, String vmUuid, VirtualMachine.Type vmType, String commandName) { @@ -5841,4 +5654,32 @@ protected Pair retrievePendingWorkJob(Long vmId, String vmUui return new Pair<>(null, vmId); } + + protected Pair createWorkJobAndWorkInfo(String commandName, Long vmId) { + return createWorkJobAndWorkInfo(commandName, null, vmId); + } + + protected Pair createWorkJobAndWorkInfo(String commandName, VmWorkJobVO.Step step, Long vmId) { + CallContext context = CallContext.current(); + long userId = context.getCallingUser().getId(); + long accountId = context.getCallingAccount().getId(); + + VmWorkJobVO workJob = new VmWorkJobVO(context.getContextId()); + workJob.setDispatcher(VmWorkConstants.VM_WORK_JOB_DISPATCHER); + workJob.setCmd(commandName); + workJob.setAccountId(accountId); + workJob.setUserId(userId); + + if (step != null) { + workJob.setStep(step); + } + + workJob.setVmType(VirtualMachine.Type.Instance); + workJob.setVmInstanceId(vmId); + workJob.setRelated(AsyncJobExecutionContext.getOriginJobId()); + + VmWork workInfo = new VmWork(userId, accountId, vmId, VirtualMachineManagerImpl.VM_WORK_JOB_HANDLER); + + return new Pair<>(workJob, workInfo); + } } From 6fd2cbbd72bb0143caadf9cab2b2144554b5f651 Mon Sep 17 00:00:00 2001 From: Daniel Augusto Veronezi Salvador Date: Wed, 14 Apr 2021 15:43:45 -0300 Subject: [PATCH 13/25] Extract submit async job to a method --- .../cloud/vm/VirtualMachineManagerImpl.java | 57 ++++++------------- 1 file changed, 18 insertions(+), 39 deletions(-) diff --git a/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java b/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java index f290f65cd954..cf9d35c8bd20 100755 --- a/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java +++ b/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java @@ -4849,9 +4849,7 @@ public Outcome startVmThroughJobQueue(final String vmUuid, if (planner != null) { workInfo.setDeploymentPlanner(planner.getName()); } - workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo)); - - _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vmId); + setCmdInfoAndSubmitAsyncJob(workJob, workInfo, vmId); } AsyncJobExecutionContext.getCurrentExecutionContext().joinJob(workJob.getId()); @@ -4873,9 +4871,7 @@ public Outcome stopVmThroughJobQueue(final String vmUuid, final workJob = newVmWorkJobAndInfo.first(); VmWorkStop workInfo = new VmWorkStop(newVmWorkJobAndInfo.second(), cleanup); - workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo)); - - _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vmId); + setCmdInfoAndSubmitAsyncJob(workJob, workInfo, vmId); } AsyncJobExecutionContext.getCurrentExecutionContext().joinJob(workJob.getId()); @@ -4898,9 +4894,7 @@ public Outcome rebootVmThroughJobQueue(final String vmUuid, workJob = newVmWorkJobAndInfo.first(); VmWorkReboot workInfo = new VmWorkReboot(newVmWorkJobAndInfo.second(), params); - workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo)); - - _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vmId); + setCmdInfoAndSubmitAsyncJob(workJob, workInfo, vmId); } AsyncJobExecutionContext.getCurrentExecutionContext().joinJob(workJob.getId()); @@ -4931,9 +4925,7 @@ public Outcome migrateVmThroughJobQueue(final String vmUuid, fin workJob = newVmWorkJobAndInfo.first(); VmWorkMigrate workInfo = new VmWorkMigrate(newVmWorkJobAndInfo.second(), srcHostId, dest); - workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo)); - - _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vmId); + setCmdInfoAndSubmitAsyncJob(workJob, workInfo, vmId); } AsyncJobExecutionContext.getCurrentExecutionContext().joinJob(workJob.getId()); @@ -4982,9 +4974,7 @@ public Outcome migrateVmWithStorageThroughJobQueue( workJob = newVmWorkJobAndInfo.first(); VmWorkMigrateWithStorage workInfo = new VmWorkMigrateWithStorage(newVmWorkJobAndInfo.second(), srcHostId, destHostId, volumeToPool); - workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo)); - - _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vmId); + setCmdInfoAndSubmitAsyncJob(workJob, workInfo, vmId); } AsyncJobExecutionContext.getCurrentExecutionContext().joinJob(workJob.getId()); @@ -5006,9 +4996,7 @@ public Outcome migrateVmForScaleThroughJobQueue( workJob = newVmWorkJobAndInfo.first(); VmWorkMigrateForScale workInfo = new VmWorkMigrateForScale(newVmWorkJobAndInfo.second(), srcHostId, dest, newSvcOfferingId); - workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo)); - - _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vmId); + setCmdInfoAndSubmitAsyncJob(workJob, workInfo, vmId); } AsyncJobExecutionContext.getCurrentExecutionContext().joinJob(workJob.getId()); @@ -5045,9 +5033,7 @@ public Outcome migrateVmStorageThroughJobQueue(final String vmUu workJob = newVmWorkJobAndInfo.first(); VmWorkStorageMigration workInfo = new VmWorkStorageMigration(newVmWorkJobAndInfo.second(), volumeToPool); - workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo)); - - _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vmId); + setCmdInfoAndSubmitAsyncJob(workJob, workInfo, vmId); } AsyncJobExecutionContext.getCurrentExecutionContext().joinJob(workJob.getId()); @@ -5068,9 +5054,7 @@ public Outcome addVmToNetworkThroughJobQueue( workJob = newVmWorkJobAndInfo.first(); VmWorkAddVmToNetwork workInfo = new VmWorkAddVmToNetwork(newVmWorkJobAndInfo.second(), network.getId(), requested); - workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo)); - - _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vmId); + setCmdInfoAndSubmitAsyncJob(workJob, workInfo, vmId); } AsyncJobExecutionContext.getCurrentExecutionContext().joinJob(workJob.getId()); @@ -5091,9 +5075,7 @@ public Outcome removeNicFromVmThroughJobQueue( workJob = newVmWorkJobAndInfo.first(); VmWorkRemoveNicFromVm workInfo = new VmWorkRemoveNicFromVm(newVmWorkJobAndInfo.second(), nic.getId()); - workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo)); - - _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vmId); + setCmdInfoAndSubmitAsyncJob(workJob, workInfo, vmId); } AsyncJobExecutionContext.getCurrentExecutionContext().joinJob(workJob.getId()); @@ -5114,9 +5096,7 @@ public Outcome removeVmFromNetworkThroughJobQueue( workJob = newVmWorkJobAndInfo.first(); VmWorkRemoveVmFromNetwork workInfo = new VmWorkRemoveVmFromNetwork(newVmWorkJobAndInfo.second(), network, broadcastUri); - workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo)); - - _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vmId); + setCmdInfoAndSubmitAsyncJob(workJob, workInfo, vmId); } AsyncJobExecutionContext.getCurrentExecutionContext().joinJob(workJob.getId()); @@ -5138,9 +5118,7 @@ public Outcome reconfigureVmThroughJobQueue( workJob = newVmWorkJobAndInfo.first(); VmWorkReconfigure workInfo = new VmWorkReconfigure(newVmWorkJobAndInfo.second(), oldServiceOffering.getId(), newServiceOffering.getId(), customParameters, reconfiguringOnExistingHost); - workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo)); - - _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vmId); + setCmdInfoAndSubmitAsyncJob(workJob, workInfo, vmId); } AsyncJobExecutionContext.getCurrentExecutionContext().joinJob(workJob.getId()); @@ -5410,9 +5388,7 @@ public Outcome restoreVirtualMachineThroughJobQueue(final long v workJob = newVmWorkJobAndInfo.first(); VmWorkRestore workInfo = new VmWorkRestore(newVmWorkJobAndInfo.second(), newTemplateId); - workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo)); - - _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vmId); + setCmdInfoAndSubmitAsyncJob(workJob, workInfo, vmId); } AsyncJobExecutionContext.getCurrentExecutionContext().joinJob(workJob.getId()); @@ -5495,9 +5471,7 @@ public Outcome updateDefaultNicForVMThroughJobQueue(final Virtua workJob = newVmWorkJobAndInfo.first(); VmWorkUpdateDefaultNic workInfo = new VmWorkUpdateDefaultNic(newVmWorkJobAndInfo.second(), nic.getId(), defaultNic.getId()); - workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo)); - - _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vmId); + setCmdInfoAndSubmitAsyncJob(workJob, workInfo, vmId); } AsyncJobExecutionContext.getCurrentExecutionContext().joinJob(workJob.getId()); @@ -5682,4 +5656,9 @@ protected Pair createWorkJobAndWorkInfo(String commandName, return new Pair<>(workJob, workInfo); } + + protected void setCmdInfoAndSubmitAsyncJob(VmWorkJobVO workJob, VmWork workInfo, Long vmId) { + workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo)); + _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vmId); + } } From c6fc9914e046162849f60ae9faf7aff5b14cd5e0 Mon Sep 17 00:00:00 2001 From: Daniel Augusto Veronezi Salvador Date: Wed, 14 Apr 2021 15:50:05 -0300 Subject: [PATCH 14/25] Extract find vm by id to a method --- .../cloud/vm/VirtualMachineManagerImpl.java | 90 +++++-------------- 1 file changed, 24 insertions(+), 66 deletions(-) diff --git a/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java b/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java index cf9d35c8bd20..3ba0b519410b 100755 --- a/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java +++ b/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java @@ -5127,11 +5127,7 @@ public Outcome reconfigureVmThroughJobQueue( @ReflectionUse private Pair orchestrateStart(final VmWorkStart work) throws Exception { - final VMInstanceVO vm = _entityMgr.findById(VMInstanceVO.class, work.getVmId()); - if (vm == null) { - s_logger.info("Unable to find vm " + work.getVmId()); - } - assert vm != null; + VMInstanceVO vm = findVmById(work.getVmId()); Boolean enterSetup = (Boolean)work.getParams().get(VirtualMachineProfile.Param.BootIntoSetup); if (s_logger.isTraceEnabled()) { @@ -5164,11 +5160,7 @@ private Pair orchestrateStop(final VmWorkStop work) thro @ReflectionUse private Pair orchestrateMigrate(final VmWorkMigrate work) throws Exception { - final VMInstanceVO vm = _entityMgr.findById(VMInstanceVO.class, work.getVmId()); - if (vm == null) { - s_logger.info("Unable to find vm " + work.getVmId()); - } - assert vm != null; + VMInstanceVO vm = findVmById(work.getVmId()); orchestrateMigrate(vm.getUuid(), work.getSrcHostId(), work.getDeployDestination()); return new Pair<>(JobInfo.Status.SUCCEEDED, null); @@ -5176,11 +5168,7 @@ private Pair orchestrateMigrate(final VmWorkMigrate work @ReflectionUse private Pair orchestrateMigrateAway(final VmWorkMigrateAway work) throws Exception { - final VMInstanceVO vm = _entityMgr.findById(VMInstanceVO.class, work.getVmId()); - if (vm == null) { - s_logger.info("Unable to find vm " + work.getVmId()); - } - assert vm != null; + VMInstanceVO vm = findVmById(work.getVmId()); try { orchestrateMigrateAway(vm.getUuid(), work.getSrcHostId(), null); @@ -5194,11 +5182,7 @@ private Pair orchestrateMigrateAway(final VmWorkMigrateA @ReflectionUse private Pair orchestrateMigrateWithStorage(final VmWorkMigrateWithStorage work) throws Exception { - final VMInstanceVO vm = _entityMgr.findById(VMInstanceVO.class, work.getVmId()); - if (vm == null) { - s_logger.info("Unable to find vm " + work.getVmId()); - } - assert vm != null; + VMInstanceVO vm = findVmById(work.getVmId()); orchestrateMigrateWithStorage(vm.getUuid(), work.getSrcHostId(), work.getDestHostId(), @@ -5208,11 +5192,7 @@ private Pair orchestrateMigrateWithStorage(final VmWorkM @ReflectionUse private Pair orchestrateMigrateForScale(final VmWorkMigrateForScale work) throws Exception { - final VMInstanceVO vm = _entityMgr.findById(VMInstanceVO.class, work.getVmId()); - if (vm == null) { - s_logger.info("Unable to find vm " + work.getVmId()); - } - assert vm != null; + VMInstanceVO vm = findVmById(work.getVmId()); orchestrateMigrateForScale(vm.getUuid(), work.getSrcHostId(), work.getDeployDestination(), @@ -5222,22 +5202,14 @@ private Pair orchestrateMigrateForScale(final VmWorkMigr @ReflectionUse private Pair orchestrateReboot(final VmWorkReboot work) throws Exception { - final VMInstanceVO vm = _entityMgr.findById(VMInstanceVO.class, work.getVmId()); - if (vm == null) { - s_logger.info("Unable to find vm " + work.getVmId()); - } - assert vm != null; + VMInstanceVO vm = findVmById(work.getVmId()); orchestrateReboot(vm.getUuid(), work.getParams()); return new Pair<>(JobInfo.Status.SUCCEEDED, null); } @ReflectionUse private Pair orchestrateAddVmToNetwork(final VmWorkAddVmToNetwork work) throws Exception { - final VMInstanceVO vm = _entityMgr.findById(VMInstanceVO.class, work.getVmId()); - if (vm == null) { - s_logger.info("Unable to find vm " + work.getVmId()); - } - assert vm != null; + VMInstanceVO vm = findVmById(work.getVmId()); final Network network = _networkDao.findById(work.getNetworkId()); final NicProfile nic = orchestrateAddVmToNetwork(vm, network, @@ -5248,11 +5220,7 @@ private Pair orchestrateAddVmToNetwork(final VmWorkAddVm @ReflectionUse private Pair orchestrateRemoveNicFromVm(final VmWorkRemoveNicFromVm work) throws Exception { - final VMInstanceVO vm = _entityMgr.findById(VMInstanceVO.class, work.getVmId()); - if (vm == null) { - s_logger.info("Unable to find vm " + work.getVmId()); - } - assert vm != null; + VMInstanceVO vm = findVmById(work.getVmId()); final NicVO nic = _entityMgr.findById(NicVO.class, work.getNicId()); final boolean result = orchestrateRemoveNicFromVm(vm, nic); return new Pair<>(JobInfo.Status.SUCCEEDED, @@ -5261,11 +5229,7 @@ private Pair orchestrateRemoveNicFromVm(final VmWorkRemo @ReflectionUse private Pair orchestrateRemoveVmFromNetwork(final VmWorkRemoveVmFromNetwork work) throws Exception { - final VMInstanceVO vm = _entityMgr.findById(VMInstanceVO.class, work.getVmId()); - if (vm == null) { - s_logger.info("Unable to find vm " + work.getVmId()); - } - assert vm != null; + VMInstanceVO vm = findVmById(work.getVmId()); final boolean result = orchestrateRemoveVmFromNetwork(vm, work.getNetwork(), work.getBroadcastUri()); return new Pair<>(JobInfo.Status.SUCCEEDED, @@ -5274,11 +5238,7 @@ private Pair orchestrateRemoveVmFromNetwork(final VmWork @ReflectionUse private Pair orchestrateReconfigure(final VmWorkReconfigure work) throws Exception { - final VMInstanceVO vm = _entityMgr.findById(VMInstanceVO.class, work.getVmId()); - if (vm == null) { - s_logger.info("Unable to find vm " + work.getVmId()); - } - assert vm != null; + VMInstanceVO vm = findVmById(work.getVmId()); ServiceOfferingVO oldServiceOffering = _offeringDao.findById(work.getOldServiceOfferingId()); ServiceOfferingVO newServiceOffering = _offeringDao.findById(work.getNewServiceOfferingId()); if (newServiceOffering.isDynamic()) { @@ -5292,13 +5252,8 @@ private Pair orchestrateReconfigure(final VmWorkReconfig @ReflectionUse private Pair orchestrateStorageMigration(final VmWorkStorageMigration work) throws Exception { - final VMInstanceVO vm = _entityMgr.findById(VMInstanceVO.class, work.getVmId()); - if (vm == null) { - s_logger.info("Unable to find vm " + work.getVmId()); - } - assert vm != null; + VMInstanceVO vm = findVmById(work.getVmId()); orchestrateStorageMigration(vm.getUuid(), work.getVolumeToPool()); - return new Pair<>(JobInfo.Status.SUCCEEDED, null); } @@ -5397,11 +5352,7 @@ public Outcome restoreVirtualMachineThroughJobQueue(final long v @ReflectionUse private Pair orchestrateRestoreVirtualMachine(final VmWorkRestore work) throws Exception { - final VMInstanceVO vm = _entityMgr.findById(VMInstanceVO.class, work.getVmId()); - if (vm == null) { - s_logger.info("Unable to find vm " + work.getVmId()); - } - assert vm != null; + VMInstanceVO vm = findVmById(work.getVmId()); UserVm uservm = orchestrateRestoreVirtualMachine(vm.getId(), work.getTemplateId()); HashMap passwordMap = new HashMap<>(); passwordMap.put(uservm.getId(), uservm.getPassword()); @@ -5480,11 +5431,7 @@ public Outcome updateDefaultNicForVMThroughJobQueue(final Virtua @ReflectionUse private Pair orchestrateUpdateDefaultNic(final VmWorkUpdateDefaultNic work) throws Exception { - final VMInstanceVO vm = _entityMgr.findById(VMInstanceVO.class, work.getVmId()); - if (vm == null) { - s_logger.info("Unable to find vm " + work.getVmId()); - } - assert vm != null; + VMInstanceVO vm = findVmById(work.getVmId()); final NicVO nic = _entityMgr.findById(NicVO.class, work.getNicId()); if (nic == null) { throw new CloudRuntimeException("Unable to find nic " + work.getNicId()); @@ -5661,4 +5608,15 @@ protected void setCmdInfoAndSubmitAsyncJob(VmWorkJobVO workJob, VmWork workInfo, workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo)); _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vmId); } + + protected VMInstanceVO findVmById(Long vmId) { + VMInstanceVO vm = _entityMgr.findById(VMInstanceVO.class, vmId); + + if (vm == null) { + s_logger.warn(String.format("Could not find VM [%s].", vmId)); + } + + assert vm != null; + return vm; + } } From 8e7f475c40007c2b459439b350dfa44cc37469ce Mon Sep 17 00:00:00 2001 From: Daniel Augusto Veronezi Salvador Date: Wed, 14 Apr 2021 15:51:06 -0300 Subject: [PATCH 15/25] Change log level from trace to debug --- .../cloud/vm/VirtualMachineManagerImpl.java | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java b/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java index 3ba0b519410b..e656016144c7 100755 --- a/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java +++ b/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java @@ -911,8 +911,8 @@ public void advanceStart(final String vmUuid, final Map":params.get(VirtualMachineProfile.Param.BootIntoSetup)))); } @@ -927,8 +927,8 @@ public void advanceStart(final String vmUuid, final Map":params.get(VirtualMachineProfile.Param.BootIntoSetup)))); } @@ -3245,7 +3245,7 @@ private void orchestrateMigrateAway(final String vmUuid, final long srcHostId, f protected class CleanupTask extends ManagedContextRunnable { @Override protected void runInContext() { - s_logger.trace("VM Operation Thread Running"); + s_logger.debug("VM Operation Thread Running"); try { _workDao.cleanup(VmOpCleanupWait.value()); final Date cutDate = new Date(DateUtil.currentGMTTime().getTime() - VmOpCleanupInterval.value() * 1000); @@ -3287,8 +3287,8 @@ public void advanceReboot(final String vmUuid, final Map":params.get(VirtualMachineProfile.Param.BootIntoSetup)))); } orchestrateReboot(vmUuid, params); @@ -3298,8 +3298,8 @@ public void advanceReboot(final String vmUuid, final Map":params.get(VirtualMachineProfile.Param.BootIntoSetup)))); } final Outcome outcome = rebootVmThroughJobQueue(vmUuid, params); @@ -5130,8 +5130,8 @@ private Pair orchestrateStart(final VmWorkStart work) th VMInstanceVO vm = findVmById(work.getVmId()); Boolean enterSetup = (Boolean)work.getParams().get(VirtualMachineProfile.Param.BootIntoSetup); - if (s_logger.isTraceEnabled()) { - s_logger.trace(String.format("orchestrating VM start for '%s' %s set to %s", vm.getInstanceName(), VirtualMachineProfile.Param.BootIntoSetup, enterSetup)); + if (s_logger.isDebugEnabled()) { + s_logger.debug(String.format("orchestrating VM start for '%s' %s set to %s", vm.getInstanceName(), VirtualMachineProfile.Param.BootIntoSetup, enterSetup)); } try { From 2f7c385a89eee200327fbe44c06e06b967830c04 Mon Sep 17 00:00:00 2001 From: Daniel Augusto Veronezi Salvador Date: Wed, 14 Apr 2021 17:21:29 -0300 Subject: [PATCH 16/25] Remove unnused methods and add logs --- .../com/cloud/hypervisor/HypervisorGuru.java | 14 ----- .../cloud/vm/VirtualMachineManagerImpl.java | 52 +++++++++---------- .../com/cloud/agent/manager/Commands.java | 7 +++ .../cloud/hypervisor/HypervisorGuruBase.java | 10 ---- 4 files changed, 32 insertions(+), 51 deletions(-) diff --git a/api/src/main/java/com/cloud/hypervisor/HypervisorGuru.java b/api/src/main/java/com/cloud/hypervisor/HypervisorGuru.java index 96518ac17693..790071da9310 100644 --- a/api/src/main/java/com/cloud/hypervisor/HypervisorGuru.java +++ b/api/src/main/java/com/cloud/hypervisor/HypervisorGuru.java @@ -70,20 +70,6 @@ public interface HypervisorGuru extends Adapter { */ NicTO toNicTO(NicProfile profile); - /** - * Give hypervisor guru opportunity to decide if certain command needs to be done after expunge VM from DB - * @param vm - * @return a list of Commands - */ - List finalizeExpunge(VirtualMachine vm); - - /** - * Give the hypervisor guru the opportinity to decide if additional clean is - * required for nics before expunging the VM - * - */ - List finalizeExpungeNics(VirtualMachine vm, List nics); - List finalizeExpungeVolumes(VirtualMachine vm); Map getClusterSettings(long vmId); diff --git a/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java b/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java index e656016144c7..4b32f2e25612 100755 --- a/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java +++ b/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java @@ -573,7 +573,6 @@ protected void advanceExpunge(VMInstanceVO vm) throws ResourceUnavailableExcepti List vmNics = profile.getNics(); s_logger.debug(String.format("Cleaning up NICS [%s] of %s.", vmNics.stream().map(nic -> nic.toString()).collect(Collectors.joining(", ")),vm.toString())); - final List nicExpungeCommands = hvGuru.finalizeExpungeNics(vm, vmNics); _networkMgr.cleanupNics(profile); s_logger.debug(String.format("Cleaning up hypervisor data structures (ex. SRs in XenServer) for managed storage. Data from %s.", vm.toString())); @@ -613,39 +612,37 @@ protected void advanceExpunge(VMInstanceVO vm) throws ResourceUnavailableExcepti userVmDeployAsIsDetailsDao.removeDetails(vm.getId()); - final List finalizeExpungeCommands = hvGuru.finalizeExpunge(vm); - if (CollectionUtils.isNotEmpty(finalizeExpungeCommands)) { - if (hostId != null) { - final Commands cmds = new Commands(Command.OnError.Stop); - for (final Command command : finalizeExpungeCommands) { - command.setBypassHostMaintenance(expungeCommandCanBypassHostMaintenance(vm)); - cmds.addCommand(command); - } - if (nicExpungeCommands != null) { - for (final Command command : nicExpungeCommands) { - command.setBypassHostMaintenance(expungeCommandCanBypassHostMaintenance(vm)); - cmds.addCommand(command); - } - } - _agentMgr.send(hostId, cmds); - handleUnsuccessfulCommands(cmds, vm); - } - } - if (s_logger.isDebugEnabled()) { s_logger.debug("Expunged " + vm); } } protected void handleUnsuccessfulCommands(Commands cmds, VMInstanceVO vm) throws CloudRuntimeException { - if (!cmds.isSuccessful()) { - for (Answer answer : cmds.getAnswers()) { - if (!answer.getResult()) { - String message = String.format("Unable to expunge %s due to [%s].", vm.toString(), answer.getDetails()); - s_logger.warn(message); - throw new CloudRuntimeException(message); - } + String cmdsStr = cmds.toString(); + String vmToString = vm.toString(); + + if (cmds.isSuccessful()) { + s_logger.debug(String.format("The commands [%s] to %s were successful.", cmdsStr, vmToString)); + return; + } + + s_logger.info(String.format("The commands [%s] to %s were unsuccessful. Handling answers.", cmdsStr, vmToString)); + + Answer[] answers = cmds.getAnswers(); + if (answers == null) { + s_logger.debug(String.format("There are no answers to commands [%s] to %s.", cmdsStr, vmToString)); + return; + } + + for (Answer answer : answers) { + String details = answer.getDetails(); + if (!answer.getResult()) { + String message = String.format("Unable to expunge %s due to [%s].", vmToString, details); + s_logger.warn(message); + throw new CloudRuntimeException(message); } + + s_logger.debug(String.format("Commands [%s] to %s got answer [%s].", cmdsStr, vmToString, details)); } } @@ -3415,6 +3412,7 @@ private String getControlNicIpForVM(VirtualMachine vm) { case DomainRouter: return vm.getPrivateIpAddress(); default: + s_logger.debug(String.format("% is a [%s], returning null for control Nic IP.", vm.toString(), vm.getType())); return null; } } diff --git a/framework/ipc/src/main/java/com/cloud/agent/manager/Commands.java b/framework/ipc/src/main/java/com/cloud/agent/manager/Commands.java index 50ab85e8b475..208512b5306e 100644 --- a/framework/ipc/src/main/java/com/cloud/agent/manager/Commands.java +++ b/framework/ipc/src/main/java/com/cloud/agent/manager/Commands.java @@ -24,6 +24,7 @@ import com.cloud.agent.api.Command; import com.cloud.agent.api.Command.OnError; import com.cloud.utils.exception.CloudRuntimeException; +import java.util.stream.Collectors; public class Commands implements Iterable { OnError _handler; @@ -150,4 +151,10 @@ public boolean isSuccessful() { public Iterator iterator() { return _cmds.iterator(); } + + @Override + public String toString() { + return _cmds.stream().map(cmd -> cmd.toString()).collect(Collectors.joining(", ")); + } + } diff --git a/server/src/main/java/com/cloud/hypervisor/HypervisorGuruBase.java b/server/src/main/java/com/cloud/hypervisor/HypervisorGuruBase.java index 5965e89773ad..24dddc43daa4 100644 --- a/server/src/main/java/com/cloud/hypervisor/HypervisorGuruBase.java +++ b/server/src/main/java/com/cloud/hypervisor/HypervisorGuruBase.java @@ -285,16 +285,6 @@ public Pair getCommandHostDelegation(long hostId, Command cmd) { return new Pair(Boolean.FALSE, new Long(hostId)); } - @Override - public List finalizeExpunge(VirtualMachine vm) { - return null; - } - - @Override - public List finalizeExpungeNics(VirtualMachine vm, List nics) { - return null; - } - @Override public List finalizeExpungeVolumes(VirtualMachine vm) { return null; From 47ddbc4eb68a63f55701f0da5b9227d43421492a Mon Sep 17 00:00:00 2001 From: Daniel Augusto Veronezi Salvador Date: Thu, 29 Apr 2021 17:34:52 -0300 Subject: [PATCH 17/25] Undo code remotion --- .../java/com/cloud/hypervisor/HypervisorGuru.java | 14 ++++++++++++++ .../com/cloud/hypervisor/HypervisorGuruBase.java | 10 ++++++++++ 2 files changed, 24 insertions(+) diff --git a/api/src/main/java/com/cloud/hypervisor/HypervisorGuru.java b/api/src/main/java/com/cloud/hypervisor/HypervisorGuru.java index 790071da9310..96518ac17693 100644 --- a/api/src/main/java/com/cloud/hypervisor/HypervisorGuru.java +++ b/api/src/main/java/com/cloud/hypervisor/HypervisorGuru.java @@ -70,6 +70,20 @@ public interface HypervisorGuru extends Adapter { */ NicTO toNicTO(NicProfile profile); + /** + * Give hypervisor guru opportunity to decide if certain command needs to be done after expunge VM from DB + * @param vm + * @return a list of Commands + */ + List finalizeExpunge(VirtualMachine vm); + + /** + * Give the hypervisor guru the opportinity to decide if additional clean is + * required for nics before expunging the VM + * + */ + List finalizeExpungeNics(VirtualMachine vm, List nics); + List finalizeExpungeVolumes(VirtualMachine vm); Map getClusterSettings(long vmId); diff --git a/server/src/main/java/com/cloud/hypervisor/HypervisorGuruBase.java b/server/src/main/java/com/cloud/hypervisor/HypervisorGuruBase.java index 24dddc43daa4..5965e89773ad 100644 --- a/server/src/main/java/com/cloud/hypervisor/HypervisorGuruBase.java +++ b/server/src/main/java/com/cloud/hypervisor/HypervisorGuruBase.java @@ -285,6 +285,16 @@ public Pair getCommandHostDelegation(long hostId, Command cmd) { return new Pair(Boolean.FALSE, new Long(hostId)); } + @Override + public List finalizeExpunge(VirtualMachine vm) { + return null; + } + + @Override + public List finalizeExpungeNics(VirtualMachine vm, List nics) { + return null; + } + @Override public List finalizeExpungeVolumes(VirtualMachine vm) { return null; From a1acf58d043a4e85f5b7a2ceabd64e173846e016 Mon Sep 17 00:00:00 2001 From: Daniel Augusto Veronezi Salvador Date: Tue, 4 May 2021 12:29:51 -0300 Subject: [PATCH 18/25] Remove asserts and fix conditionals --- .../java/com/cloud/vm/VirtualMachineManagerImpl.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java b/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java index 4b32f2e25612..a327950e2d66 100755 --- a/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java +++ b/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java @@ -3412,7 +3412,7 @@ private String getControlNicIpForVM(VirtualMachine vm) { case DomainRouter: return vm.getPrivateIpAddress(); default: - s_logger.debug(String.format("% is a [%s], returning null for control Nic IP.", vm.toString(), vm.getType())); + s_logger.debug(String.format("%s is a [%s], returning null for control Nic IP.", vm.toString(), vm.getType())); return null; } } @@ -4793,8 +4793,7 @@ public VmStateSyncOutcome(final AsyncJob job, final PowerState desiredPowerState @Override public boolean checkCondition() { final AsyncJobVO jobVo = _entityMgr.findById(AsyncJobVO.class, job.getId()); - assert jobVo != null; - return jobVo.getStatus() != JobInfo.Status.IN_PROGRESS; + return jobVo == null || jobVo.getStatus() != JobInfo.Status.IN_PROGRESS; } }, Topics.VM_POWER_STATE, AsyncJob.Topics.JOB_STATE); _vmId = vmId; @@ -4814,8 +4813,7 @@ public VmJobVirtualMachineOutcome(final AsyncJob job, final long vmId) { @Override public boolean checkCondition() { final AsyncJobVO jobVo = _entityMgr.findById(AsyncJobVO.class, job.getId()); - assert jobVo != null; - return jobVo.getStatus() != JobInfo.Status.IN_PROGRESS; + return jobVo == null || jobVo.getStatus() != JobInfo.Status.IN_PROGRESS; } }, AsyncJob.Topics.JOB_STATE); _vmId = vmId; @@ -5567,7 +5565,6 @@ protected Pair retrievePendingWorkJob(Long vmId, String vmUui List pendingWorkJobs = _workJobDao.listPendingWorkJobs(vmType, vmId, commandName); if (CollectionUtils.isNotEmpty(pendingWorkJobs)) { - assert pendingWorkJobs.size() == 1; return new Pair<>(pendingWorkJobs.get(0), vmId); } From 0a08ad2c747127bf283c2a342282c638948a6d32 Mon Sep 17 00:00:00 2001 From: GutoVeronezi Date: Fri, 23 Jul 2021 17:08:27 -0300 Subject: [PATCH 19/25] Address @GabrielBrascher reviews --- api/src/main/java/com/cloud/vm/NicProfile.java | 2 +- .../src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/api/src/main/java/com/cloud/vm/NicProfile.java b/api/src/main/java/com/cloud/vm/NicProfile.java index 2b664c5f9d9d..0d69340e854e 100644 --- a/api/src/main/java/com/cloud/vm/NicProfile.java +++ b/api/src/main/java/com/cloud/vm/NicProfile.java @@ -430,6 +430,6 @@ public void deallocate() { @Override public String toString() { - return String.format("NicProfile {\"id\": %s, \"vmId\": %s, \"reservationId\": \"%s\", \"iPv4Address\": \"%s\", \"broadcastUri\": \"%s\"}", id, vmId, reservationId, iPv4Address, broadcastUri); + return String.format("NicProfile {id: %s, vmId: %s, reservationId: \"%s\", iPv4Address: \"%s\", broadcastUri: \"%s\"}", id, vmId, reservationId, iPv4Address, broadcastUri); } } diff --git a/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java b/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java index a327950e2d66..d0ad3cb56c7c 100755 --- a/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java +++ b/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java @@ -638,7 +638,7 @@ protected void handleUnsuccessfulCommands(Commands cmds, VMInstanceVO vm) throws String details = answer.getDetails(); if (!answer.getResult()) { String message = String.format("Unable to expunge %s due to [%s].", vmToString, details); - s_logger.warn(message); + s_logger.error(message); throw new CloudRuntimeException(message); } From 94e4807cfcc3d394202eb8f9f382cef6ab6238d0 Mon Sep 17 00:00:00 2001 From: Daniel Augusto Veronezi Salvador <38945620+GutoVeronezi@users.noreply.github.com> Date: Tue, 27 Jul 2021 09:46:54 -0300 Subject: [PATCH 20/25] Remove double quotes from keys in manual json --- engine/schema/src/main/java/com/cloud/storage/VMTemplateVO.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/engine/schema/src/main/java/com/cloud/storage/VMTemplateVO.java b/engine/schema/src/main/java/com/cloud/storage/VMTemplateVO.java index 49843fbfaebd..2f5a2aa3201f 100644 --- a/engine/schema/src/main/java/com/cloud/storage/VMTemplateVO.java +++ b/engine/schema/src/main/java/com/cloud/storage/VMTemplateVO.java @@ -553,7 +553,7 @@ public int hashCode() { @Override public String toString() { - return String.format("Template {\"id\": %s, \"uniqueName\": \"%s\", \"format\": \"%s\"}", id, uniqueName, format); + return String.format("Template {id: %s, uniqueName: \"%s\", format: \"%s\"}", id, uniqueName, format); } public void setRemoved(Date removed) { From f29e630b9fac9540c92c782952bdeccb47656d83 Mon Sep 17 00:00:00 2001 From: GutoVeronezi Date: Thu, 12 Aug 2021 10:46:53 -0300 Subject: [PATCH 21/25] Undo code remotion --- .../cloud/vm/VirtualMachineManagerImpl.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java b/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java index d0ad3cb56c7c..4a55c80b297b 100755 --- a/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java +++ b/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java @@ -573,6 +573,7 @@ protected void advanceExpunge(VMInstanceVO vm) throws ResourceUnavailableExcepti List vmNics = profile.getNics(); s_logger.debug(String.format("Cleaning up NICS [%s] of %s.", vmNics.stream().map(nic -> nic.toString()).collect(Collectors.joining(", ")),vm.toString())); + final List nicExpungeCommands = hvGuru.finalizeExpungeNics(vm, profile.getNics()); _networkMgr.cleanupNics(profile); s_logger.debug(String.format("Cleaning up hypervisor data structures (ex. SRs in XenServer) for managed storage. Data from %s.", vm.toString())); @@ -612,6 +613,33 @@ protected void advanceExpunge(VMInstanceVO vm) throws ResourceUnavailableExcepti userVmDeployAsIsDetailsDao.removeDetails(vm.getId()); + // send hypervisor-dependent commands before removing + final List finalizeExpungeCommands = hvGuru.finalizeExpunge(vm); + if (finalizeExpungeCommands != null && finalizeExpungeCommands.size() > 0) { + if (hostId != null) { + final Commands cmds = new Commands(Command.OnError.Stop); + for (final Command command : finalizeExpungeCommands) { + command.setBypassHostMaintenance(expungeCommandCanBypassHostMaintenance(vm)); + cmds.addCommand(command); + } + if (nicExpungeCommands != null) { + for (final Command command : nicExpungeCommands) { + command.setBypassHostMaintenance(expungeCommandCanBypassHostMaintenance(vm)); + cmds.addCommand(command); + } + } + _agentMgr.send(hostId, cmds); + if (!cmds.isSuccessful()) { + for (final Answer answer : cmds.getAnswers()) { + if (!answer.getResult()) { + s_logger.warn("Failed to expunge vm due to: " + answer.getDetails()); + throw new CloudRuntimeException("Unable to expunge " + vm + " due to " + answer.getDetails()); + } + } + } + } + } + if (s_logger.isDebugEnabled()) { s_logger.debug("Expunged " + vm); } From 1f9565deb6d178711cbc2ea74c6f206a77865676 Mon Sep 17 00:00:00 2001 From: GutoVeronezi Date: Mon, 23 Aug 2021 13:55:26 -0300 Subject: [PATCH 22/25] Add object to log --- .../src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java b/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java index c4be0cfa6809..38bf32c84282 100755 --- a/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java +++ b/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java @@ -4401,7 +4401,7 @@ public VMInstanceVO reConfigureVm(final String vmUuid, final ServiceOffering old throw new RuntimeException("Unhandled exception", ex); } - throw new RuntimeException("Unexpected job execution result"); + throw new RuntimeException(String.format("Unexpected job execution result [%s]", result)); } } From eecbaec6ee8f916725b89f339b5bf3dad3073c6a Mon Sep 17 00:00:00 2001 From: GutoVeronezi Date: Thu, 26 Aug 2021 10:06:53 -0300 Subject: [PATCH 23/25] Remove statement from try/catch --- .../java/com/cloud/vm/VirtualMachineManagerImpl.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java b/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java index 38bf32c84282..3cd340a15410 100755 --- a/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java +++ b/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java @@ -4393,15 +4393,15 @@ public VMInstanceVO reConfigureVm(final String vmUuid, final ServiceOffering old Object result = null; try { result = retrieveResultFromJobOutcomeAndThrowExceptionIfNeeded(outcome); - - if (result == null) { - return (VMInstanceVO)vm; - } } catch (Exception ex) { throw new RuntimeException("Unhandled exception", ex); } - throw new RuntimeException(String.format("Unexpected job execution result [%s]", result)); + if (result != null) { + throw new RuntimeException(String.format("Unexpected job execution result [%s]", result)); + } + + return (VMInstanceVO)vm; } } From c8cb75aaf5add22b38d8199eb8a2c003269020f3 Mon Sep 17 00:00:00 2001 From: GutoVeronezi Date: Tue, 14 Sep 2021 14:57:23 -0300 Subject: [PATCH 24/25] Implement toString with ReflectionToStringBuilderUtils --- api/src/main/java/com/cloud/vm/NicProfile.java | 3 ++- .../src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java | 4 ++-- .../schema/src/main/java/com/cloud/storage/VMTemplateVO.java | 4 +++- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/api/src/main/java/com/cloud/vm/NicProfile.java b/api/src/main/java/com/cloud/vm/NicProfile.java index 0d69340e854e..162336e344d1 100644 --- a/api/src/main/java/com/cloud/vm/NicProfile.java +++ b/api/src/main/java/com/cloud/vm/NicProfile.java @@ -20,6 +20,7 @@ import java.net.URI; import org.apache.cloudstack.api.InternalIdentity; +import org.apache.cloudstack.utils.reflectiontostringbuilderutils.ReflectionToStringBuilderUtils; import com.cloud.network.Network; import com.cloud.network.Networks.AddressFormat; @@ -430,6 +431,6 @@ public void deallocate() { @Override public String toString() { - return String.format("NicProfile {id: %s, vmId: %s, reservationId: \"%s\", iPv4Address: \"%s\", broadcastUri: \"%s\"}", id, vmId, reservationId, iPv4Address, broadcastUri); + return String.format("NicProfile %s", ReflectionToStringBuilderUtils.reflectOnlySelectedFields(this, "id", "vmId", "reservationId", "iPv4Address", "broadcastUri")); } } diff --git a/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java b/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java index d61561a88fda..c0548128c506 100755 --- a/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java +++ b/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java @@ -482,7 +482,7 @@ public void doInTransactionWithoutResult(final TransactionStatus status) throws volumeMgr.allocateRawVolume(Type.ROOT, rootVolumeName, rootDiskOfferingInfo.getDiskOffering(), rootDiskOfferingInfo.getSize(), rootDiskOfferingInfo.getMinIops(), rootDiskOfferingInfo.getMaxIops(), vmFinal, template, owner, null); } else if (template.getFormat() == ImageFormat.BAREMETAL) { - s_logger.debug(String.format("Template [%s] has format [%s]. Skipping ROOT volume [%s] allocation.", template.toString(), ImageFormat.BAREMETAL, rootVolumeName)); + s_logger.debug(String.format("%s has format [%s]. Skipping ROOT volume [%s] allocation.", template.toString(), ImageFormat.BAREMETAL, rootVolumeName)); } else { volumeMgr.allocateTemplatedVolumes(Type.ROOT, rootVolumeName, rootDiskOfferingInfo.getDiskOffering(), rootDiskOfferingInfo.getSize(), rootDiskOfferingInfo.getMinIops(), rootDiskOfferingInfo.getMaxIops(), template, vmFinal, owner); @@ -2321,7 +2321,7 @@ private void markVolumesInPool(VMInstanceVO vm, Answer[] hypervisorMigrationResu } for (Answer answer : hypervisorMigrationResults) { if (s_logger.isDebugEnabled()) { - s_logger.debug(String.format("received an %s: %s", answer.getClass().getSimpleName(), answer)); + s_logger.debug(String.format("Received an %s: %s", answer.getClass().getSimpleName(), answer)); } if (answer instanceof MigrateVmToPoolAnswer) { relevantAnswer = (MigrateVmToPoolAnswer) answer; diff --git a/engine/schema/src/main/java/com/cloud/storage/VMTemplateVO.java b/engine/schema/src/main/java/com/cloud/storage/VMTemplateVO.java index 2f5a2aa3201f..8f66da052e90 100644 --- a/engine/schema/src/main/java/com/cloud/storage/VMTemplateVO.java +++ b/engine/schema/src/main/java/com/cloud/storage/VMTemplateVO.java @@ -31,6 +31,8 @@ import javax.persistence.TemporalType; import javax.persistence.Transient; +import org.apache.cloudstack.utils.reflectiontostringbuilderutils.ReflectionToStringBuilderUtils; + import com.cloud.hypervisor.Hypervisor.HypervisorType; import com.cloud.storage.Storage.ImageFormat; import com.cloud.storage.Storage.TemplateType; @@ -553,7 +555,7 @@ public int hashCode() { @Override public String toString() { - return String.format("Template {id: %s, uniqueName: \"%s\", format: \"%s\"}", id, uniqueName, format); + return String.format("Template %s", ReflectionToStringBuilderUtils.reflectOnlySelectedFields(this, "id", "uniqueName", "format")); } public void setRemoved(Date removed) { From 732bbcd6d9f72d1c1218315aae41d6cfa8c2bae7 Mon Sep 17 00:00:00 2001 From: GutoVeronezi Date: Fri, 19 Nov 2021 10:02:26 -0300 Subject: [PATCH 25/25] Fix errors related to merge main --- .../src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java | 1 + 1 file changed, 1 insertion(+) diff --git a/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java b/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java index a4435a5b559b..0c8736bbe71e 100755 --- a/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java +++ b/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java @@ -39,6 +39,7 @@ import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; import javax.inject.Inject; import javax.naming.ConfigurationException;