Arik Hadas has uploaded a new change for review. Change subject: core: move to logic in DownVmsHandler to separate command ......................................................................
core: move to logic in DownVmsHandler to separate command The logic of how to handle VM that went down is extracted to separate command, so the invocation of this logic is be in a more standard way. The DownVmsHandler remains, it will later on be used as a listener for notifications from VURTI. Change-Id: I1ff9f5a5f24fffca8117bd87f3df4d2476958f0b Signed-off-by: Arik Hadas <aha...@redhat.com> --- M backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/ClearNonResponsiveVdsVmsCommand.java M backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/DownVmsHandler.java M backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/FenceVdsBaseCommand.java A backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/ProcessDownVmCommand.java M backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/RunVmCommand.java M backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/RunVmCommandBase.java M backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/VdsEventListener.java M backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/action/VdcActionType.java 8 files changed, 180 insertions(+), 130 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/45/28145/1 diff --git a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/ClearNonResponsiveVdsVmsCommand.java b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/ClearNonResponsiveVdsVmsCommand.java index 8f1dc98..67a8613 100644 --- a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/ClearNonResponsiveVdsVmsCommand.java +++ b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/ClearNonResponsiveVdsVmsCommand.java @@ -6,6 +6,8 @@ import org.ovirt.engine.core.bll.job.ExecutionHandler; import org.ovirt.engine.core.common.AuditLogType; +import org.ovirt.engine.core.common.action.IdParameters; +import org.ovirt.engine.core.common.action.VdcActionType; import org.ovirt.engine.core.common.action.VdsActionParameters; import org.ovirt.engine.core.common.businessentities.VDSStatus; import org.ovirt.engine.core.common.businessentities.VM; @@ -59,7 +61,8 @@ LogSettingVmToDown(getVds().getId(), vm.getId()); } - DownVmsHandler.processVmPoolOnStopVm(vm.getId(), + Backend.getInstance().runInternalAction(VdcActionType.ProcessDownVm, + new IdParameters(vm.getId()), ExecutionHandler.createDefaultContexForTasks(getExecutionContext())); } diff --git a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/DownVmsHandler.java b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/DownVmsHandler.java index 415623e..e13b9cbb 100644 --- a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/DownVmsHandler.java +++ b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/DownVmsHandler.java @@ -1,32 +1,13 @@ package org.ovirt.engine.core.bll; -import java.util.Date; -import java.util.List; - import org.ovirt.engine.core.bll.context.CommandContext; -import org.ovirt.engine.core.bll.quota.QuotaManager; -import org.ovirt.engine.core.bll.snapshots.SnapshotsManager; -import org.ovirt.engine.core.common.action.VdcActionType; -import org.ovirt.engine.core.common.action.VmManagementParametersBase; -import org.ovirt.engine.core.common.action.VmOperationParameterBase; -import org.ovirt.engine.core.common.action.VmPoolSimpleUserParameters; -import org.ovirt.engine.core.common.businessentities.DbUser; -import org.ovirt.engine.core.common.businessentities.Snapshot; -import org.ovirt.engine.core.common.businessentities.Snapshot.SnapshotType; -import org.ovirt.engine.core.common.businessentities.VM; -import org.ovirt.engine.core.common.businessentities.VmDevice; -import org.ovirt.engine.core.common.businessentities.VmPayload; -import org.ovirt.engine.core.common.businessentities.VmPool; -import org.ovirt.engine.core.common.businessentities.VmPoolMap; -import org.ovirt.engine.core.common.businessentities.VmPoolType; -import org.ovirt.engine.core.common.businessentities.VmWatchdog; -import org.ovirt.engine.core.common.utils.VmDeviceType; import org.ovirt.engine.core.compat.Guid; -import org.ovirt.engine.core.dal.dbbroker.DbFacade; import org.ovirt.engine.core.utils.log.Log; import org.ovirt.engine.core.utils.log.LogFactory; public class DownVmsHandler { + + private static final Log log = LogFactory.getLog(DownVmsHandler.class); /** * VM should be return to pool after it stopped unless Manual Return VM To Pool chosen. @@ -39,109 +20,5 @@ * solution supplied */ public static void processVmPoolOnStopVm(Guid vmId, CommandContext context) { - VmPoolMap map = DbFacade.getInstance().getVmPoolDao().getVmPoolMapByVmGuid(vmId); - List<DbUser> users = DbFacade.getInstance().getDbUserDao().getAllForVm(vmId); - // Check if this is a Vm from a Vm pool, and is attached to a user - if (map != null && users != null && !users.isEmpty()) { - VmPool pool = DbFacade.getInstance().getVmPoolDao().get(map.getvm_pool_id()); - if (pool != null && pool.getVmPoolType() == VmPoolType.Automatic) { - // should be only one user in the collection - for (DbUser dbUser : users) { - Backend.getInstance().runInternalAction(VdcActionType.DetachUserFromVmFromPool, - new VmPoolSimpleUserParameters(map.getvm_pool_id(), dbUser.getId(), vmId), context); - } - } - } else { - // If we are dealing with a prestarted Vm or a regular Vm - clean stateless images - // Otherwise this was already done in DetachUserFromVmFromPoolCommand - removeVmStatelessImages(vmId, context); - } - - QuotaManager.getInstance().rollbackQuotaByVmId(vmId); - VmHandler.removeStatelessVmUnmanagedDevices(vmId); - - ApplyNextRunConfiguration(vmId); } - - /** - * Update vm configuration with NEXT_RUN configuration, if exists - * @param vmId - */ - private static void ApplyNextRunConfiguration(Guid vmId) { - // Remove snpashot first, in case other update is in progress, it will block this one with exclusive lock - // and any newer update should be preffered to this one. - Snapshot runSnap = DbFacade.getInstance().getSnapshotDao().get(vmId, SnapshotType.NEXT_RUN); - if (runSnap != null) { - DbFacade.getInstance().getSnapshotDao().remove(runSnap.getId()); - VM vm = DbFacade.getInstance().getVmDao().get(vmId); - if (vm != null) { - Date originalCreationDate = vm.getVmCreationDate(); - new SnapshotsManager().updateVmFromConfiguration(vm, runSnap.getVmConfiguration()); - // override creation date because the value in the config is the creation date of the config, not the vm - vm.setVmCreationDate(originalCreationDate); - - VmManagementParametersBase updateVmParams = createUpdateVmParameters(vm); - - Backend.getInstance().runInternalAction(VdcActionType.UpdateVm, updateVmParams); - } - } - } - - private static VmManagementParametersBase createUpdateVmParameters(VM vm) { - // clear non updateable fields got from config - vm.setExportDate(null); - vm.setOvfVersion(null); - - VmManagementParametersBase updateVmParams = new VmManagementParametersBase(vm); - updateVmParams.setUpdateWatchdog(true); - updateVmParams.setSoundDeviceEnabled(false); - updateVmParams.setBalloonEnabled(false); - updateVmParams.setVirtioScsiEnabled(false); - updateVmParams.setClearPayload(true); - - for (VmDevice device : vm.getManagedVmDeviceMap().values()) { - switch (device.getType()) { - case WATCHDOG: - updateVmParams.setWatchdog(new VmWatchdog(device)); - break; - case SOUND: - updateVmParams.setSoundDeviceEnabled(true); - break; - case BALLOON: - updateVmParams.setBalloonEnabled(true); - break; - case CONTROLLER: - if (VmDeviceType.VIRTIOSCSI.getName().equals(device.getDevice())) { - updateVmParams.setVirtioScsiEnabled(true); - } - break; - case DISK: - if (VmPayload.isPayload(device.getSpecParams())) { - updateVmParams.setVmPayload(new VmPayload(VmDeviceType.getByName(device.getDevice()), device.getSpecParams())); - } - break; - case CONSOLE: - updateVmParams.setConsoleEnabled(true); - break; - default: - } - } - - // clear these fields as these are non updatable - vm.getManagedVmDeviceMap().clear(); - vm.getVmUnamagedDeviceList().clear(); - - return updateVmParams; - } - - public static void removeVmStatelessImages(Guid vmId, CommandContext context) { - if (DbFacade.getInstance().getSnapshotDao().exists(vmId, SnapshotType.STATELESS)) { - log.infoFormat("Deleting snapshot for stateless vm {0}", vmId); - Backend.getInstance().runInternalAction(VdcActionType.RestoreStatelessVm, - new VmOperationParameterBase(vmId), - context); - } - } - - private static final Log log = LogFactory.getLog(DownVmsHandler.class); } diff --git a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/FenceVdsBaseCommand.java b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/FenceVdsBaseCommand.java index b19c246..d0bd4bc 100644 --- a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/FenceVdsBaseCommand.java +++ b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/FenceVdsBaseCommand.java @@ -14,6 +14,7 @@ import org.ovirt.engine.core.bll.job.ExecutionHandler; import org.ovirt.engine.core.common.AuditLogType; import org.ovirt.engine.core.common.action.FenceVdsActionParameters; +import org.ovirt.engine.core.common.action.IdParameters; import org.ovirt.engine.core.common.action.VdcActionType; import org.ovirt.engine.core.common.businessentities.FenceActionType; import org.ovirt.engine.core.common.businessentities.FenceAgentOrder; @@ -482,7 +483,8 @@ setVmId(vm.getId()); setVmName(vm.getName()); setVm(vm); - DownVmsHandler.processVmPoolOnStopVm(vm.getId(), + Backend.getInstance().runInternalAction(VdcActionType.ProcessDownVm, + new IdParameters(vm.getId()), ExecutionHandler.createDefaultContexForTasks(getExecutionContext())); // Handle highly available VMs diff --git a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/ProcessDownVmCommand.java b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/ProcessDownVmCommand.java new file mode 100644 index 0000000..f4028b0 --- /dev/null +++ b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/ProcessDownVmCommand.java @@ -0,0 +1,159 @@ +package org.ovirt.engine.core.bll; + +import java.util.Date; +import java.util.List; + +import org.ovirt.engine.core.bll.context.CommandContext; +import org.ovirt.engine.core.bll.job.ExecutionHandler; +import org.ovirt.engine.core.bll.quota.QuotaManager; +import org.ovirt.engine.core.bll.snapshots.SnapshotsManager; +import org.ovirt.engine.core.bll.utils.PermissionSubject; +import org.ovirt.engine.core.common.action.IdParameters; +import org.ovirt.engine.core.common.action.VdcActionType; +import org.ovirt.engine.core.common.action.VmManagementParametersBase; +import org.ovirt.engine.core.common.action.VmOperationParameterBase; +import org.ovirt.engine.core.common.action.VmPoolSimpleUserParameters; +import org.ovirt.engine.core.common.businessentities.DbUser; +import org.ovirt.engine.core.common.businessentities.Snapshot; +import org.ovirt.engine.core.common.businessentities.VM; +import org.ovirt.engine.core.common.businessentities.VmDevice; +import org.ovirt.engine.core.common.businessentities.VmPayload; +import org.ovirt.engine.core.common.businessentities.VmPool; +import org.ovirt.engine.core.common.businessentities.VmPoolMap; +import org.ovirt.engine.core.common.businessentities.VmPoolType; +import org.ovirt.engine.core.common.businessentities.VmWatchdog; +import org.ovirt.engine.core.common.businessentities.Snapshot.SnapshotType; +import org.ovirt.engine.core.common.utils.VmDeviceType; +import org.ovirt.engine.core.compat.Guid; +import org.ovirt.engine.core.dal.dbbroker.DbFacade; +import org.ovirt.engine.core.utils.log.Log; +import org.ovirt.engine.core.utils.log.LogFactory; + +@InternalCommandAttribute +@NonTransactiveCommandAttribute +public class ProcessDownVmCommand<T extends IdParameters> extends CommandBase<T> { + + private static final Log log = LogFactory.getLog(ProcessDownVmCommand.class); + + protected ProcessDownVmCommand(Guid commandId) { + super(commandId); + } + + public ProcessDownVmCommand(T parameters) { + super(parameters); + } + + @Override + protected void executeCommand() { + Guid vmId = getParameters().getId(); + VmPoolMap map = DbFacade.getInstance().getVmPoolDao().getVmPoolMapByVmGuid(vmId); + List<DbUser> users = DbFacade.getInstance().getDbUserDao().getAllForVm(vmId); + // Check if this is a Vm from a Vm pool, and is attached to a user + if (map != null && users != null && !users.isEmpty()) { + VmPool pool = DbFacade.getInstance().getVmPoolDao().get(map.getvm_pool_id()); + if (pool != null && pool.getVmPoolType() == VmPoolType.Automatic) { + // should be only one user in the collection + for (DbUser dbUser : users) { + Backend.getInstance().runInternalAction(VdcActionType.DetachUserFromVmFromPool, + new VmPoolSimpleUserParameters(map.getvm_pool_id(), dbUser.getId(), vmId), + ExecutionHandler.createDefaultContexForTasks(getExecutionContext(), getLock())); + } + } + } else { + // If we are dealing with a prestarted Vm or a regular Vm - clean stateless images + // Otherwise this was already done in DetachUserFromVmFromPoolCommand + removeVmStatelessImages(vmId, + ExecutionHandler.createDefaultContexForTasks(getExecutionContext(), getLock())); + } + + QuotaManager.getInstance().rollbackQuotaByVmId(vmId); + VmHandler.removeStatelessVmUnmanagedDevices(vmId); + + ApplyNextRunConfiguration(vmId); + } + + @Override + public List<PermissionSubject> getPermissionCheckSubjects() { + return null; + } + + /** + * Update vm configuration with NEXT_RUN configuration, if exists + * @param vmId + */ + private static void ApplyNextRunConfiguration(Guid vmId) { + // Remove snpashot first, in case other update is in progress, it will block this one with exclusive lock + // and any newer update should be preffered to this one. + Snapshot runSnap = DbFacade.getInstance().getSnapshotDao().get(vmId, SnapshotType.NEXT_RUN); + if (runSnap != null) { + DbFacade.getInstance().getSnapshotDao().remove(runSnap.getId()); + VM vm = DbFacade.getInstance().getVmDao().get(vmId); + if (vm != null) { + Date originalCreationDate = vm.getVmCreationDate(); + new SnapshotsManager().updateVmFromConfiguration(vm, runSnap.getVmConfiguration()); + // override creation date because the value in the config is the creation date of the config, not the vm + vm.setVmCreationDate(originalCreationDate); + + VmManagementParametersBase updateVmParams = createUpdateVmParameters(vm); + + Backend.getInstance().runInternalAction(VdcActionType.UpdateVm, updateVmParams); + } + } + } + + private static VmManagementParametersBase createUpdateVmParameters(VM vm) { + // clear non updateable fields got from config + vm.setExportDate(null); + vm.setOvfVersion(null); + + VmManagementParametersBase updateVmParams = new VmManagementParametersBase(vm); + updateVmParams.setUpdateWatchdog(true); + updateVmParams.setSoundDeviceEnabled(false); + updateVmParams.setBalloonEnabled(false); + updateVmParams.setVirtioScsiEnabled(false); + updateVmParams.setClearPayload(true); + + for (VmDevice device : vm.getManagedVmDeviceMap().values()) { + switch (device.getType()) { + case WATCHDOG: + updateVmParams.setWatchdog(new VmWatchdog(device)); + break; + case SOUND: + updateVmParams.setSoundDeviceEnabled(true); + break; + case BALLOON: + updateVmParams.setBalloonEnabled(true); + break; + case CONTROLLER: + if (VmDeviceType.VIRTIOSCSI.getName().equals(device.getDevice())) { + updateVmParams.setVirtioScsiEnabled(true); + } + break; + case DISK: + if (VmPayload.isPayload(device.getSpecParams())) { + updateVmParams.setVmPayload(new VmPayload(VmDeviceType.getByName(device.getDevice()), device.getSpecParams())); + } + break; + case CONSOLE: + updateVmParams.setConsoleEnabled(true); + break; + default: + } + } + + // clear these fields as these are non updatable + vm.getManagedVmDeviceMap().clear(); + vm.getVmUnamagedDeviceList().clear(); + + return updateVmParams; + } + + public static void removeVmStatelessImages(Guid vmId, CommandContext context) { + if (DbFacade.getInstance().getSnapshotDao().exists(vmId, SnapshotType.STATELESS)) { + log.infoFormat("Deleting snapshot for stateless vm {0}", vmId); + Backend.getInstance().runInternalAction(VdcActionType.RestoreStatelessVm, + new VmOperationParameterBase(vmId), + context); + } + } +} diff --git a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/RunVmCommand.java b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/RunVmCommand.java index 301c651..c642b33 100644 --- a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/RunVmCommand.java +++ b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/RunVmCommand.java @@ -29,6 +29,7 @@ import org.ovirt.engine.core.common.FeatureSupported; import org.ovirt.engine.core.common.VdcObjectType; import org.ovirt.engine.core.common.action.CreateAllSnapshotsFromVmParameters; +import org.ovirt.engine.core.common.action.IdParameters; import org.ovirt.engine.core.common.action.RunVmParams; import org.ovirt.engine.core.common.action.VdcActionType; import org.ovirt.engine.core.common.action.VdcReturnValueBase; @@ -463,7 +464,9 @@ } private void removeVmStatlessImages() { - DownVmsHandler.processVmPoolOnStopVm(getVm().getId(), new CommandContext(getExecutionContext(), getLock())); + Backend.getInstance().runInternalAction(VdcActionType.ProcessDownVm, + new IdParameters(getVm().getId()), + ExecutionHandler.createDefaultContexForTasks(getExecutionContext(), getLock())); // setting lock to null in order not to release lock twice setLock(null); setSucceeded(true); diff --git a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/RunVmCommandBase.java b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/RunVmCommandBase.java index 78930fc..dc81617 100644 --- a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/RunVmCommandBase.java +++ b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/RunVmCommandBase.java @@ -16,6 +16,7 @@ import org.ovirt.engine.core.bll.scheduling.RunVmDelayer; import org.ovirt.engine.core.bll.snapshots.SnapshotsValidator; import org.ovirt.engine.core.bll.storage.StorageHelperDirector; +import org.ovirt.engine.core.common.action.IdParameters; import org.ovirt.engine.core.common.action.RemoveVmHibernationVolumesParameters; import org.ovirt.engine.core.common.action.VdcActionType; import org.ovirt.engine.core.common.action.VdcReturnValueBase; @@ -145,7 +146,8 @@ ThreadPoolUtil.execute(new Runnable() { @Override public void run() { - DownVmsHandler.processVmPoolOnStopVm(getVm().getId(), + Backend.getInstance().runInternalAction(VdcActionType.ProcessDownVm, + new IdParameters(getVm().getId()), ExecutionHandler.createDefaultContexForTasks(getExecutionContext())); } }); diff --git a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/VdsEventListener.java b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/VdsEventListener.java index 69dee37..aef0360 100644 --- a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/VdsEventListener.java +++ b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/VdsEventListener.java @@ -20,6 +20,7 @@ import org.ovirt.engine.core.common.action.AddVmFromScratchParameters; import org.ovirt.engine.core.common.action.FenceVdsActionParameters; import org.ovirt.engine.core.common.action.HostStoragePoolParametersBase; +import org.ovirt.engine.core.common.action.IdParameters; import org.ovirt.engine.core.common.action.MaintenanceNumberOfVdssParameters; import org.ovirt.engine.core.common.action.MigrateVmToServerParameters; import org.ovirt.engine.core.common.action.ReconstructMasterParameters; @@ -106,7 +107,9 @@ @Override public void processOnVmStop(Guid vmId) { - DownVmsHandler.processVmPoolOnStopVm(vmId, null); + Backend.getInstance().runInternalAction(VdcActionType.ProcessDownVm, + new IdParameters(vmId), + ExecutionHandler.createInternalJobContext()); } @Override diff --git a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/action/VdcActionType.java b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/action/VdcActionType.java index b8d2010..48de7cc 100644 --- a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/action/VdcActionType.java +++ b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/action/VdcActionType.java @@ -54,6 +54,7 @@ CloneVm(53, ActionGroup.CREATE_VM, QuotaDependency.BOTH), ImportVmFromConfiguration(43, ActionGroup.IMPORT_EXPORT_VM, QuotaDependency.NONE), UpdateVmVersion(44, QuotaDependency.NONE), + ProcessDownVm(45, QuotaDependency.NONE), // VdsCommands AddVds(101, ActionGroup.CREATE_HOST, QuotaDependency.NONE), UpdateVds(102, ActionGroup.EDIT_HOST_CONFIGURATION, false, QuotaDependency.NONE), -- To view, visit http://gerrit.ovirt.org/28145 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I1ff9f5a5f24fffca8117bd87f3df4d2476958f0b Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine Gerrit-Branch: master Gerrit-Owner: Arik Hadas <aha...@redhat.com> _______________________________________________ Engine-patches mailing list Engine-patches@ovirt.org http://lists.ovirt.org/mailman/listinfo/engine-patches