Make job info universally available across management server and resource agents

This commit is contained in:
Kelven Yang 2014-06-23 16:44:59 -07:00
parent 3d9eaf0753
commit f756d4aa33
3 changed files with 59 additions and 25 deletions

View File

@ -43,6 +43,8 @@ import org.apache.log4j.Logger;
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.managed.context.ManagedContextRunnable;
import org.apache.cloudstack.utils.identity.ManagementServerNode;
@ -379,6 +381,18 @@ public class AgentManagerImpl extends ManagerBase implements AgentManager, Handl
return !txn.dbTxnStarted();
}
private static void tagCommand(Command cmd) {
AsyncJobExecutionContext context = AsyncJobExecutionContext.getCurrentExecutionContext();
if (context != null && context.getJob() != null) {
AsyncJob job = context.getJob();
if (job.getRelated() != null && !job.getRelated().isEmpty())
cmd.setContextParam("job", "job-" + job.getRelated() + "/" + "job-" + job.getId());
else
cmd.setContextParam("job", "job-" + job.getId());
}
}
@Override
public Answer[] send(Long hostId, Commands commands, int timeout) throws AgentUnavailableException, OperationTimedoutException {
assert hostId != null : "Who's not checking the agent id before sending? ... (finger wagging)";
@ -408,6 +422,9 @@ public class AgentManagerImpl extends ManagerBase implements AgentManager, Handl
commands.setAnswers(new Answer[0]);
}
for (Command cmd : cmds)
tagCommand(cmd);
final AgentAttache agent = getAttache(hostId);
if (agent == null || agent.isClosed()) {
throw new AgentUnavailableException("agent not logged into this management server", hostId);
@ -466,6 +483,10 @@ public class AgentManagerImpl extends ManagerBase implements AgentManager, Handl
if (cmds.length == 0) {
throw new AgentUnavailableException("Empty command set for agent " + agent.getId(), agent.getId());
}
for (Command cmd : cmds)
tagCommand(cmd);
Request req = new Request(hostId, agent.getName(), _nodeId, cmds, commands.stopOnError(), true);
req.setSequence(agent.getNextSequence());

View File

@ -354,6 +354,20 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa
_gson = GsonHelper.getGsonLogger();
}
private String getCommandLogTitle(Command cmd) {
StringBuffer sb = new StringBuffer();
if (_hostName != null) {
sb.append(_hostName);
}
if (cmd.getContextParam("job") != null) {
sb.append(", ").append(cmd.getContextParam("job"));
}
sb.append(", cmd: ").append(cmd.getClass().getSimpleName());
return sb.toString();
}
@Override
public Answer executeRequest(Command cmd) {
@ -361,7 +375,7 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa
s_logger.trace("Begin executeRequest(), cmd: " + cmd.getClass().getSimpleName());
Answer answer = null;
NDC.push(_hostName != null ? _hostName : _guid + "(" + this.getClass().getPackage().getImplementationVersion() + ")");
NDC.push(getCommandLogTitle(cmd));
try {
long cmdSequence = _cmdSequence++;
Date startTime = DateUtil.currentGMTTime();

View File

@ -19,6 +19,7 @@ package com.cloud.storage.resource;
import java.util.List;
import org.apache.log4j.Logger;
import org.apache.log4j.NDC;
import com.google.gson.Gson;
import com.vmware.vim25.ManagedObjectReference;
@ -77,11 +78,27 @@ public class VmwareSecondaryStorageResourceHandler implements SecondaryStorageRe
storageSubsystemHandler = vmwareStorageSubsystemCommandHandler;
}
private static String getCommandLogTitle(Command cmd) {
StringBuffer sb = new StringBuffer();
if (cmd.getContextParam("job") != null) {
sb.append(cmd.getContextParam("job"));
}
sb.append(", cmd: ").append(cmd.getClass().getSimpleName());
return sb.toString();
}
@Override
public Answer executeRequest(Command cmd) {
try {
Answer answer;
NDC.push(getCommandLogTitle(cmd));
if (s_logger.isDebugEnabled())
s_logger.debug("Executing " + _gson.toJson(cmd));
if (cmd instanceof PrimaryStorageDownloadCommand) {
answer = execute((PrimaryStorageDownloadCommand)cmd);
} else if (cmd instanceof BackupSnapshotCommand) {
@ -115,9 +132,15 @@ public class VmwareSecondaryStorageResourceHandler implements SecondaryStorageRe
answer.setContextParam("checkpoint2", cmd.getContextParam("checkpoint2"));
}
if (s_logger.isDebugEnabled())
s_logger.debug("Command execution answer: " + _gson.toJson(answer));
return answer;
} finally {
if (s_logger.isDebugEnabled())
s_logger.debug("Done executing " + _gson.toJson(cmd));
recycleServiceContext();
NDC.pop();
}
}
@ -127,50 +150,26 @@ public class VmwareSecondaryStorageResourceHandler implements SecondaryStorageRe
}
private Answer execute(PrimaryStorageDownloadCommand cmd) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Executing resource PrimaryStorageDownloadCommand: " + _gson.toJson(cmd));
}
return _storageMgr.execute(this, cmd);
}
private Answer execute(BackupSnapshotCommand cmd) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Executing resource BackupSnapshotCommand: " + _gson.toJson(cmd));
}
return _storageMgr.execute(this, cmd);
}
private Answer execute(CreatePrivateTemplateFromVolumeCommand cmd) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Executing resource CreatePrivateTemplateFromVolumeCommand: " + _gson.toJson(cmd));
}
return _storageMgr.execute(this, cmd);
}
private Answer execute(CreatePrivateTemplateFromSnapshotCommand cmd) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Executing resource CreatePrivateTemplateFromVolumeCommand: " + _gson.toJson(cmd));
}
return _storageMgr.execute(this, cmd);
}
private Answer execute(CopyVolumeCommand cmd) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Executing resource CopyVolumeCommand: " + _gson.toJson(cmd));
}
return _storageMgr.execute(this, cmd);
}
private Answer execute(CreateVolumeFromSnapshotCommand cmd) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Executing resource CreateVolumeFromSnapshotCommand: " + _gson.toJson(cmd));
}
return _storageMgr.execute(this, cmd);
}