Ravi Nori has uploaded a new change for review. Change subject: engine : Generate CommandContext on engine restart ......................................................................
engine : Generate CommandContext on engine restart After engine restart the command context map in memory is empty and we need to regenerate the CommandContexts for those commands that do not have a constructor with out CommandContext Change-Id: I641802c6e0e05abaa2ecd84d0718f0d95ab59d9e Bug-Url: https://bugzilla.redhat.com/1115127 Signed-off-by: Ravi Nori <[email protected]> --- M backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/CommandBase.java M backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/CommandsFactory.java M backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/tasks/CommandContextsCacheImpl.java M backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/tasks/CommandCoordinatorImpl.java M backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/CommandEntity.java M backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/CommandEntityDaoDbFacadeImpl.java M packaging/dbscripts/command_entities_sp.sql A packaging/dbscripts/upgrade/03_05_0780_add_job_step_ids_to_command_entities.sql 8 files changed, 102 insertions(+), 9 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/35/30535/1 diff --git a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/CommandBase.java b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/CommandBase.java index 5f8b4a2..98bfe48 100644 --- a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/CommandBase.java +++ b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/CommandBase.java @@ -2120,6 +2120,8 @@ TaskManagerUtil.persistCommand( CommandEntity.buildCommandEntity(getCommandId(), parentParameters.getCommandId(), + getExecutionContext().getJob() == null ? Guid.Empty : getExecutionContext().getJob().getId(), + getExecutionContext().getStep() == null ? Guid.Empty : getExecutionContext().getStep().getId(), getActionType(), getParameters(), commandStatus, diff --git a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/CommandsFactory.java b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/CommandsFactory.java index bb12626..94b0b71 100644 --- a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/CommandsFactory.java +++ b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/CommandsFactory.java @@ -142,6 +142,14 @@ return getCommandClass(name, QueryPrefix); } + public static <P extends VdcActionParametersBase> boolean hasConstructor(VdcActionType action, P parameters) { + return ReflectionUtils.findConstructor(getCommandClass(action.name(), CommandSuffix), parameters.getClass()) != null; + } + + public static <P extends VdcActionParametersBase> boolean hasConstructor(VdcActionType action, P parameters, CommandContext cmdContext) { + return ReflectionUtils.findConstructor(getCommandClass(action.name(), CommandSuffix), parameters.getClass(), cmdContext.getClass()) != null; + } + private static Class<CommandBase<? extends VdcActionParametersBase>> getCommandClass(String name, String suffix) { // try the cache first String key = name + suffix; diff --git a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/tasks/CommandContextsCacheImpl.java b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/tasks/CommandContextsCacheImpl.java index 13c5f94..a5fefff 100644 --- a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/tasks/CommandContextsCacheImpl.java +++ b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/tasks/CommandContextsCacheImpl.java @@ -1,20 +1,52 @@ package org.ovirt.engine.core.bll.tasks; import org.ovirt.engine.core.bll.context.CommandContext; +import org.ovirt.engine.core.bll.context.EngineContext; +import org.ovirt.engine.core.bll.job.ExecutionContext; +import org.ovirt.engine.core.bll.job.JobRepositoryFactory; import org.ovirt.engine.core.bll.tasks.interfaces.CommandContextsCache; +import org.ovirt.engine.core.common.businessentities.CommandEntity; import org.ovirt.engine.core.compat.Guid; public class CommandContextsCacheImpl implements CommandContextsCache { private static final String COMMAND_CONTEXT_MAP_NAME = "commandContextMap"; - CacheWrapper<Guid, CommandContext> contextsMap; + private final CommandsCache commandsCache; + private CacheWrapper<Guid, CommandContext> contextsMap; + private volatile boolean cacheInitialized; + private Object LOCK = new Object(); - public CommandContextsCacheImpl() { + public CommandContextsCacheImpl(final CommandsCache commandsCache) { + this.commandsCache = commandsCache; contextsMap = CacheProviderFactory.<Guid, CommandContext> getCacheWrapper(COMMAND_CONTEXT_MAP_NAME); + } + + private void initializeCache() { + if (!cacheInitialized) { + synchronized(LOCK) { + if (!cacheInitialized) { + for (Guid cmdId : commandsCache.keySet()) { + contextsMap.put(cmdId, buildCommandContext(commandsCache.get(cmdId))); + } + cacheInitialized = true; + } + } + } + } + + private CommandContext buildCommandContext(CommandEntity cmdEntity) { + ExecutionContext executionContext = new ExecutionContext(); + if (!Guid.isNullOrEmpty(cmdEntity.getJobId())) { + executionContext.setJob(JobRepositoryFactory.getJobRepository().getJobWithSteps(cmdEntity.getJobId())); + } else if (!Guid.isNullOrEmpty(cmdEntity.getStepId())) { + executionContext.setStep(JobRepositoryFactory.getJobRepository().getStep(cmdEntity.getStepId())); + } + return new CommandContext(new EngineContext()).withExecutionContext(executionContext); } @Override public CommandContext get(Guid commandId) { + initializeCache(); return contextsMap.get(commandId); } diff --git a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/tasks/CommandCoordinatorImpl.java b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/tasks/CommandCoordinatorImpl.java index 247636e..a15137a 100644 --- a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/tasks/CommandCoordinatorImpl.java +++ b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/tasks/CommandCoordinatorImpl.java @@ -11,6 +11,7 @@ import org.ovirt.engine.core.bll.CommandBase; import org.ovirt.engine.core.bll.CommandsFactory; import org.ovirt.engine.core.bll.context.CommandContext; +import org.ovirt.engine.core.bll.context.EngineContext; import org.ovirt.engine.core.bll.interfaces.BackendInternal; import org.ovirt.engine.core.bll.job.ExecutionContext; import org.ovirt.engine.core.bll.tasks.interfaces.CommandContextsCache; @@ -50,7 +51,7 @@ CommandCoordinatorImpl() { commandsCache = new CommandsCacheImpl(); - contextsCache = new CommandContextsCacheImpl(); + contextsCache = new CommandContextsCacheImpl(commandsCache); coCoAsyncTaskHelper = new CoCoAsyncTaskHelper(this); cmdExecutor = new CommandExecutor(this); } @@ -118,7 +119,15 @@ private CommandBase<?> buildCommand(CommandEntity cmdEntity, CommandContext cmdContext) { CommandBase<?> command = null; if (cmdEntity != null) { - command = CommandsFactory.createCommand(cmdEntity.getCommandType(), cmdEntity.getActionParameters(), cmdContext); + if (cmdContext == null) { + cmdContext = new CommandContext(new EngineContext()).withExecutionContext(new ExecutionContext()); + } + if (CommandsFactory.hasConstructor(cmdEntity.getCommandType(), cmdEntity.getActionParameters(), cmdContext)) { + command = CommandsFactory.createCommand(cmdEntity.getCommandType(), cmdEntity.getActionParameters(), cmdContext); + } else { + command = CommandsFactory.createCommand(cmdEntity.getCommandType(), cmdEntity.getActionParameters()); + } + command.setCommandStatus(cmdEntity.getCommandStatus(), false); if (!Guid.isNullOrEmpty(cmdEntity.getRootCommandId()) && ! cmdEntity.getRootCommandId().equals(cmdEntity.getId()) && diff --git a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/CommandEntity.java b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/CommandEntity.java index 005918a..c1ea5bc 100644 --- a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/CommandEntity.java +++ b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/CommandEntity.java @@ -14,6 +14,8 @@ private static final long serialVersionUID = 5293055556971973650L; private Guid commandId; private Guid rootCommandId; + private Guid jobId; + private Guid stepId; private VdcActionType commandType; private VdcActionParametersBase actionParameters; private VdcReturnValueBase returnValue; @@ -114,10 +116,20 @@ this.callBackNotified = callBackNotified; } - public static CommandEntity buildCommandEntity(Guid commandId, Guid rootCommandId, VdcActionType actionType, VdcActionParametersBase params, CommandStatus status, boolean callBackEnabled, VdcReturnValueBase returnValue) { + public static CommandEntity buildCommandEntity(Guid commandId, + Guid rootCommandId, + Guid jobId, + Guid stepId, + VdcActionType actionType, + VdcActionParametersBase params, + CommandStatus status, + boolean callBackEnabled, + VdcReturnValueBase returnValue) { CommandEntity entity = new CommandEntity(); entity.setId(commandId); entity.setRootCommandId(rootCommandId); + entity.setJobId(jobId); + entity.setStepId(stepId); entity.setCommandType(actionType); entity.setActionParameters(params); entity.setCommandStatus(status); @@ -133,4 +145,20 @@ public void setReturnValue(VdcReturnValueBase returnValue) { this.returnValue = returnValue; } + + public Guid getJobId() { + return jobId; + } + + public void setJobId(Guid jobId) { + this.jobId = jobId; + } + + public Guid getStepId() { + return stepId; + } + + public void setStepId(Guid stepId) { + this.stepId = stepId; + } } diff --git a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/CommandEntityDaoDbFacadeImpl.java b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/CommandEntityDaoDbFacadeImpl.java index fa11ed0..44a7430 100644 --- a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/CommandEntityDaoDbFacadeImpl.java +++ b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/CommandEntityDaoDbFacadeImpl.java @@ -26,6 +26,8 @@ public CommandEntity mapRow(ResultSet resultSet, int rowNum) throws SQLException { CommandEntity result = new CommandEntity(); result.setId(Guid.createGuidFromString(resultSet.getString("command_id"))); + result.setJobId(Guid.createGuidFromString(resultSet.getString("job_id"))); + result.setStepId(Guid.createGuidFromString(resultSet.getString("step_id"))); result.setCreatedAt(DbFacadeUtils.fromDate(resultSet.getTimestamp("created_at"))); result.setCommandType(VdcActionType.forValue(resultSet.getInt("command_type"))); result.setRootCommandId(Guid.createGuidFromString(resultSet.getString("root_command_id"))); @@ -56,6 +58,8 @@ return getCustomMapSqlParameterSource().addValue("command_id", Guid.isNullOrEmpty(entity.getId()) ? Guid.Empty : entity.getId()) .addValue("command_type", entity.getCommandType().getValue()) .addValue("root_command_id", Guid.isNullOrEmpty(entity.getRootCommandId()) ? Guid.Empty : entity.getRootCommandId()) + .addValue("job_id", Guid.isNullOrEmpty(entity.getJobId()) ? Guid.Empty : entity.getJobId()) + .addValue("step_id", Guid.isNullOrEmpty(entity.getStepId()) ? Guid.Empty : entity.getStepId()) .addValue("action_parameters", serializeParameters(entity.getActionParameters())) .addValue("action_parameters_class", entity.getActionParameters() == null ? null : entity.getActionParameters().getClass().getName()) .addValue("status", entity.getCommandStatus().toString()) diff --git a/packaging/dbscripts/command_entities_sp.sql b/packaging/dbscripts/command_entities_sp.sql index c57fcb2..1c8fc78 100644 --- a/packaging/dbscripts/command_entities_sp.sql +++ b/packaging/dbscripts/command_entities_sp.sql @@ -1,6 +1,8 @@ CREATE OR REPLACE FUNCTION InsertCommandEntity (v_command_id uuid, v_command_type int, v_root_command_id uuid, + v_job_id uuid, + v_step_id uuid, v_action_parameters text, v_action_parameters_class varchar(256), v_status varchar(20), @@ -10,8 +12,8 @@ RETURNS VOID AS $procedure$ BEGIN - INSERT INTO command_entities(command_id, command_type, root_command_id, action_parameters, action_parameters_class, created_at, status, callback_enabled, return_value, return_value_class) - VALUES(v_command_id, v_command_type, v_root_command_id, v_action_parameters, v_action_parameters_class, NOW(), v_status, v_callback_enabled, v_return_value, v_return_value_class); + INSERT INTO command_entities(command_id, command_type, root_command_id, job_id, step_id, action_parameters, action_parameters_class, created_at, status, callback_enabled, return_value, return_value_class) + VALUES(v_command_id, v_command_type, v_root_command_id, v_job_id, v_step_id, v_action_parameters, v_action_parameters_class, NOW(), v_status, v_callback_enabled, v_return_value, v_return_value_class); END; $procedure$ LANGUAGE plpgsql; @@ -19,6 +21,8 @@ CREATE OR REPLACE FUNCTION UpdateCommandEntity (v_command_id uuid, v_command_type int, v_root_command_id uuid, + v_job_id uuid, + v_step_id uuid, v_action_parameters text, v_action_parameters_class varchar(256), v_status varchar(20), @@ -31,6 +35,8 @@ UPDATE command_entities SET command_type = v_command_type , root_command_id = v_root_command_id, + job_id = v_job_id, + step_id = v_step_id, action_parameters = v_action_parameters, action_parameters_class = v_action_parameters_class, status = v_status, @@ -69,6 +75,8 @@ CREATE OR REPLACE FUNCTION InsertOrUpdateCommandEntity (v_command_id uuid, v_command_type int, v_root_command_id uuid, + v_job_id uuid, + v_step_id uuid, v_action_parameters text, v_action_parameters_class varchar(256), v_status varchar(20), @@ -79,9 +87,9 @@ AS $procedure$ BEGIN IF NOT EXISTS (SELECT 1 from command_entities where command_id = v_command_id) THEN - PERFORM InsertCommandEntity (v_command_id, v_command_type, v_root_command_id, v_action_parameters, v_action_parameters_class, v_status, v_callback_enabled, v_return_value, v_return_value_class); + PERFORM InsertCommandEntity (v_command_id, v_command_type, v_root_command_id, v_job_id, v_step_id, v_action_parameters, v_action_parameters_class, v_status, v_callback_enabled, v_return_value, v_return_value_class); ELSE - PERFORM UpdateCommandEntity (v_command_id, v_command_type, v_root_command_id, v_action_parameters, v_action_parameters_class, v_status, v_callback_enabled, v_return_value, v_return_value_class); + PERFORM UpdateCommandEntity (v_command_id, v_command_type, v_root_command_id, v_job_id, v_step_id, v_action_parameters, v_action_parameters_class, v_status, v_callback_enabled, v_return_value, v_return_value_class); END IF; END; $procedure$ LANGUAGE plpgsql; diff --git a/packaging/dbscripts/upgrade/03_05_0780_add_job_step_ids_to_command_entities.sql b/packaging/dbscripts/upgrade/03_05_0780_add_job_step_ids_to_command_entities.sql new file mode 100644 index 0000000..00691de --- /dev/null +++ b/packaging/dbscripts/upgrade/03_05_0780_add_job_step_ids_to_command_entities.sql @@ -0,0 +1,2 @@ +select fn_db_add_column('command_entities', 'job_id', 'UUID DEFAULT NULL'); +select fn_db_add_column('command_entities', 'step_id', 'UUID DEFAULT NULL'); -- To view, visit http://gerrit.ovirt.org/30535 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I641802c6e0e05abaa2ecd84d0718f0d95ab59d9e Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine Gerrit-Branch: ovirt-engine-3.5 Gerrit-Owner: Ravi Nori <[email protected]> _______________________________________________ Engine-patches mailing list [email protected] http://lists.ovirt.org/mailman/listinfo/engine-patches
