From 4e0e7410e99c7f57cde934af1fef9b27d0ae1e66 Mon Sep 17 00:00:00 2001 From: Marcus Sorensen Date: Mon, 14 Oct 2013 11:46:01 -0600 Subject: [PATCH] Store agent hostname in attache, print it in logs wherever possible. This was discussed on the mailing list as a useful debugging tool, currently the log prints the DB id of the agent, which makes admins have to look it up to know where the Command was run. --- core/src/com/cloud/agent/transport/Request.java | 16 +++++++++++++++- .../com/cloud/agent/manager/AgentAttache.java | 15 ++++++++++----- .../cloud/agent/manager/AgentManagerImpl.java | 8 ++++---- .../agent/manager/ClusteredAgentAttache.java | 8 ++++---- .../agent/manager/ClusteredAgentManagerImpl.java | 7 ++++--- .../manager/ClusteredDirectAgentAttache.java | 4 ++-- .../agent/manager/ConnectedAgentAttache.java | 6 +++--- .../cloud/agent/manager/DirectAgentAttache.java | 14 +++++++------- .../com/cloud/agent/manager/DummyAttache.java | 4 ++-- 9 files changed, 51 insertions(+), 31 deletions(-) diff --git a/core/src/com/cloud/agent/transport/Request.java b/core/src/com/cloud/agent/transport/Request.java index 31076c075e7..cbeb112fea7 100755 --- a/core/src/com/cloud/agent/transport/Request.java +++ b/core/src/com/cloud/agent/transport/Request.java @@ -108,6 +108,7 @@ public class Request { protected long _agentId; protected Command[] _cmds; protected String _content; + protected String _agentName; protected Request() { } @@ -142,6 +143,11 @@ public class Request { setFromServer(fromServer); } + public Request(long agentId, String agentName, long mgmtId, Command[] cmds, boolean stopOnError, boolean fromServer) { + this(agentId, mgmtId, cmds, stopOnError, fromServer); + setAgentName(agentName); + } + public void setSequence(long seq) { _seq = seq; } @@ -174,6 +180,10 @@ public class Request { _flags |= (stopOnError ? FLAG_STOP_ON_ERROR : 0); } + private final void setAgentName(String agentName) { + _agentName = agentName; + } + private final void setInSequence(boolean inSequence) { _flags |= (inSequence ? FLAG_IN_SEQUENCE : 0); } @@ -422,7 +432,11 @@ public class Request { buf.append(msg); buf.append(" { ").append(getType()); - buf.append(", MgmtId: ").append(_mgmtId).append(", via: ").append(_via); + if (_agentName != null) { + buf.append(", MgmtId: ").append(_mgmtId).append(", via: ").append(_via).append("(" + _agentName + ")"); + } else { + buf.append(", MgmtId: ").append(_mgmtId).append(", via: ").append(_via); + } buf.append(", Ver: ").append(_ver.toString()); buf.append(", Flags: ").append(Integer.toBinaryString(getFlags())).append(", "); buf.append(content); diff --git a/engine/orchestration/src/com/cloud/agent/manager/AgentAttache.java b/engine/orchestration/src/com/cloud/agent/manager/AgentAttache.java index 5b1007b26f0..ff35255c7db 100755 --- a/engine/orchestration/src/com/cloud/agent/manager/AgentAttache.java +++ b/engine/orchestration/src/com/cloud/agent/manager/AgentAttache.java @@ -101,6 +101,7 @@ public abstract class AgentAttache { }; protected final long _id; + protected String _name = null; protected final ConcurrentHashMap _waitForList; protected final LinkedList _requests; protected Long _currentSequence; @@ -121,9 +122,9 @@ public abstract class AgentAttache { Arrays.sort(s_commandsNotAllowedInConnectingMode); } - - protected AgentAttache(AgentManagerImpl agentMgr, final long id, boolean maintenance) { + protected AgentAttache(AgentManagerImpl agentMgr, final long id, final String name, boolean maintenance) { _id = id; + _name = name; _waitForList = new ConcurrentHashMap(); _currentSequence = null; _maintenance = maintenance; @@ -164,7 +165,7 @@ public abstract class AgentAttache { if (_maintenance) { for (final Command cmd : cmds) { if (Arrays.binarySearch(s_commandsAllowedInMaintenanceMode, cmd.getClass().toString()) < 0) { - throw new AgentUnavailableException("Unable to send " + cmd.getClass().toString() + " because agent is in maintenance mode", _id); + throw new AgentUnavailableException("Unable to send " + cmd.getClass().toString() + " because agent " + _name + " is in maintenance mode", _id); } } } @@ -172,7 +173,7 @@ public abstract class AgentAttache { if (_status == Status.Connecting) { for (final Command cmd : cmds) { if (Arrays.binarySearch(s_commandsNotAllowedInConnectingMode, cmd.getClass().toString()) >= 0) { - throw new AgentUnavailableException("Unable to send " + cmd.getClass().toString() + " because agent is in connecting mode", _id); + throw new AgentUnavailableException("Unable to send " + cmd.getClass().toString() + " because agent " + _name + " is in connecting mode", _id); } } } @@ -242,6 +243,10 @@ public abstract class AgentAttache { return _id; } + public String getName() { + return _name; + } + public int getQueueSize() { return _requests.size(); } @@ -350,7 +355,7 @@ public abstract class AgentAttache { synchronized(this) { try { if (isClosed()) { - throw new AgentUnavailableException("The link to the agent has been closed", _id); + throw new AgentUnavailableException("The link to the agent " + _name + " has been closed", _id); } if (req.executeInSequence() && _currentSequence != null) { diff --git a/engine/orchestration/src/com/cloud/agent/manager/AgentManagerImpl.java b/engine/orchestration/src/com/cloud/agent/manager/AgentManagerImpl.java index 42020153448..6804dc1ef68 100755 --- a/engine/orchestration/src/com/cloud/agent/manager/AgentManagerImpl.java +++ b/engine/orchestration/src/com/cloud/agent/manager/AgentManagerImpl.java @@ -384,7 +384,7 @@ public class AgentManagerImpl extends ManagerBase implements AgentManager, Handl throw new AgentUnavailableException("agent not logged into this management server", hostId); } - Request req = new Request(hostId, _nodeId, cmds, commands.stopOnError(), true); + Request req = new Request(hostId, agent.getName(), _nodeId, cmds, commands.stopOnError(), true); req.setSequence(agent.getNextSequence()); Answer[] answers = agent.send(req, timeout); notifyAnswersToMonitors(hostId, req.getSequence(), answers); @@ -437,7 +437,7 @@ public class AgentManagerImpl extends ManagerBase implements AgentManager, Handl if (cmds.length == 0) { throw new AgentUnavailableException("Empty command set for agent " + agent.getId(), agent.getId()); } - Request req = new Request(hostId, _nodeId, cmds, commands.stopOnError(), true); + Request req = new Request(hostId, agent.getName(), _nodeId, cmds, commands.stopOnError(), true); req.setSequence(agent.getNextSequence()); agent.send(req, listener); @@ -679,7 +679,7 @@ public class AgentManagerImpl extends ManagerBase implements AgentManager, Handl // } s_logger.debug("create DirectAgentAttache for " + host.getId()); - DirectAgentAttache attache = new DirectAgentAttache(this, host.getId(), resource, host.isInMaintenanceStates(), this); + DirectAgentAttache attache = new DirectAgentAttache(this, host.getId(), host.getName(), resource, host.isInMaintenanceStates(), this); AgentAttache old = null; synchronized (_agents) { @@ -969,7 +969,7 @@ public class AgentManagerImpl extends ManagerBase implements AgentManager, Handl protected AgentAttache createAttacheForConnect(HostVO host, Link link) throws ConnectionException { s_logger.debug("create ConnectedAgentAttache for " + host.getId()); - AgentAttache attache = new ConnectedAgentAttache(this, host.getId(), link, host.isInMaintenanceStates()); + AgentAttache attache = new ConnectedAgentAttache(this, host.getId(), host.getName(), link, host.isInMaintenanceStates()); link.attach(attache); AgentAttache old = null; diff --git a/engine/orchestration/src/com/cloud/agent/manager/ClusteredAgentAttache.java b/engine/orchestration/src/com/cloud/agent/manager/ClusteredAgentAttache.java index 058a90475fd..5bcde6be712 100755 --- a/engine/orchestration/src/com/cloud/agent/manager/ClusteredAgentAttache.java +++ b/engine/orchestration/src/com/cloud/agent/manager/ClusteredAgentAttache.java @@ -50,14 +50,14 @@ public class ClusteredAgentAttache extends ConnectedAgentAttache implements Rout s_clusteredAgentMgr = agentMgr; } - public ClusteredAgentAttache(AgentManagerImpl agentMgr, long id) { - super(agentMgr, id, null, false); + public ClusteredAgentAttache(AgentManagerImpl agentMgr, long id, String name) { + super(agentMgr, id, name, null, false); _forward = true; _transferRequests = new LinkedList(); } - public ClusteredAgentAttache(AgentManagerImpl agentMgr, long id, Link link, boolean maintenance) { - super(agentMgr, id, link, maintenance); + public ClusteredAgentAttache(AgentManagerImpl agentMgr, long id, String name, Link link, boolean maintenance) { + super(agentMgr, id, name, link, maintenance); _forward = link == null; _transferRequests = new LinkedList(); } diff --git a/engine/orchestration/src/com/cloud/agent/manager/ClusteredAgentManagerImpl.java b/engine/orchestration/src/com/cloud/agent/manager/ClusteredAgentManagerImpl.java index 2b9e5419376..07fa73caae0 100755 --- a/engine/orchestration/src/com/cloud/agent/manager/ClusteredAgentManagerImpl.java +++ b/engine/orchestration/src/com/cloud/agent/manager/ClusteredAgentManagerImpl.java @@ -246,7 +246,8 @@ public class ClusteredAgentManagerImpl extends AgentManagerImpl implements Clust protected AgentAttache createAttache(long id) { s_logger.debug("create forwarding ClusteredAgentAttache for " + id); - final AgentAttache attache = new ClusteredAgentAttache(this, id); + HostVO host = _hostDao.findById(id); + final AgentAttache attache = new ClusteredAgentAttache(this, id, host.getName()); AgentAttache old = null; synchronized (_agents) { old = _agents.get(id); @@ -261,7 +262,7 @@ public class ClusteredAgentManagerImpl extends AgentManagerImpl implements Clust @Override protected AgentAttache createAttacheForConnect(HostVO host, Link link) { s_logger.debug("create ClusteredAgentAttache for " + host.getId()); - final AgentAttache attache = new ClusteredAgentAttache(this, host.getId(), link, host.isInMaintenanceStates()); + final AgentAttache attache = new ClusteredAgentAttache(this, host.getId(), host.getName(), link, host.isInMaintenanceStates()); link.attach(attache); AgentAttache old = null; synchronized (_agents) { @@ -280,7 +281,7 @@ public class ClusteredAgentManagerImpl extends AgentManagerImpl implements Clust // return new DummyAttache(this, host.getId(), false); // } s_logger.debug("create ClusteredDirectAgentAttache for " + host.getId()); - final DirectAgentAttache attache = new ClusteredDirectAgentAttache(this, host.getId(), _nodeId, resource, host.isInMaintenanceStates(), this); + final DirectAgentAttache attache = new ClusteredDirectAgentAttache(this, host.getId(), host.getName(), _nodeId, resource, host.isInMaintenanceStates(), this); AgentAttache old = null; synchronized (_agents) { old = _agents.get(host.getId()); diff --git a/engine/orchestration/src/com/cloud/agent/manager/ClusteredDirectAgentAttache.java b/engine/orchestration/src/com/cloud/agent/manager/ClusteredDirectAgentAttache.java index 9012433b4d7..692f6ad67de 100755 --- a/engine/orchestration/src/com/cloud/agent/manager/ClusteredDirectAgentAttache.java +++ b/engine/orchestration/src/com/cloud/agent/manager/ClusteredDirectAgentAttache.java @@ -28,8 +28,8 @@ public class ClusteredDirectAgentAttache extends DirectAgentAttache implements R private final ClusteredAgentManagerImpl _mgr; private final long _nodeId; - public ClusteredDirectAgentAttache(AgentManagerImpl agentMgr, long id, long mgmtId, ServerResource resource, boolean maintenance, ClusteredAgentManagerImpl mgr) { - super(agentMgr, id, resource, maintenance, mgr); + public ClusteredDirectAgentAttache(AgentManagerImpl agentMgr, long id, String name, long mgmtId, ServerResource resource, boolean maintenance, ClusteredAgentManagerImpl mgr) { + super(agentMgr, id, name, resource, maintenance, mgr); _mgr = mgr; _nodeId = mgmtId; } diff --git a/engine/orchestration/src/com/cloud/agent/manager/ConnectedAgentAttache.java b/engine/orchestration/src/com/cloud/agent/manager/ConnectedAgentAttache.java index e5d2867b96d..8ee47d55c26 100755 --- a/engine/orchestration/src/com/cloud/agent/manager/ConnectedAgentAttache.java +++ b/engine/orchestration/src/com/cloud/agent/manager/ConnectedAgentAttache.java @@ -33,8 +33,8 @@ public class ConnectedAgentAttache extends AgentAttache { protected Link _link; - public ConnectedAgentAttache(AgentManagerImpl agentMgr, final long id, final Link link, boolean maintenance) { - super(agentMgr, id, maintenance); + public ConnectedAgentAttache(AgentManagerImpl agentMgr, final long id, final String name, final Link link, boolean maintenance) { + super(agentMgr, id, name, maintenance); _link = link; } @@ -83,7 +83,7 @@ public class ConnectedAgentAttache extends AgentAttache { assert _link == null : "Duh...Says you....Forgot to call disconnect()!"; synchronized (this) { if (_link != null) { - s_logger.warn("Lost attache " + _id); + s_logger.warn("Lost attache " + _id + "(" + _name + ")"); disconnect(Status.Alert); } } diff --git a/engine/orchestration/src/com/cloud/agent/manager/DirectAgentAttache.java b/engine/orchestration/src/com/cloud/agent/manager/DirectAgentAttache.java index 2808c6af91a..7d3f7659639 100755 --- a/engine/orchestration/src/com/cloud/agent/manager/DirectAgentAttache.java +++ b/engine/orchestration/src/com/cloud/agent/manager/DirectAgentAttache.java @@ -44,8 +44,8 @@ public class DirectAgentAttache extends AgentAttache { AgentManagerImpl _mgr; long _seq = 0; - public DirectAgentAttache(AgentManagerImpl agentMgr, long id, ServerResource resource, boolean maintenance, AgentManagerImpl mgr) { - super(agentMgr, id, maintenance); + public DirectAgentAttache(AgentManagerImpl agentMgr, long id, String name, ServerResource resource, boolean maintenance, AgentManagerImpl mgr) { + super(agentMgr, id, name, maintenance); _resource = resource; _mgr = mgr; } @@ -53,7 +53,7 @@ public class DirectAgentAttache extends AgentAttache { @Override public void disconnect(Status state) { if (s_logger.isDebugEnabled()) { - s_logger.debug("Processing disconnect " + _id); + s_logger.debug("Processing disconnect " + _id + "(" + _name + ")"); } for (ScheduledFuture future : _futures) { @@ -119,7 +119,7 @@ public class DirectAgentAttache extends AgentAttache { assert _resource == null : "Come on now....If you're going to dabble in agent code, you better know how to close out our resources. Ever considered why there's a method called disconnect()?"; synchronized (this) { if (_resource != null) { - s_logger.warn("Lost attache for " + _id); + s_logger.warn("Lost attache for " + _id + "(" + _name + ")"); disconnect(Status.Alert); } } @@ -137,12 +137,12 @@ public class DirectAgentAttache extends AgentAttache { if (resource != null) { PingCommand cmd = resource.getCurrentStatus(_id); if (cmd == null) { - s_logger.warn("Unable to get current status on " + _id); + s_logger.warn("Unable to get current status on " + _id + "(" + _name + ")"); _mgr.disconnectWithInvestigation(DirectAgentAttache.this, Event.AgentDisconnected); return; } if (s_logger.isDebugEnabled()) { - s_logger.debug("Ping from " + _id); + s_logger.debug("Ping from " + _id + "(" + _name + ")"); } long seq = _seq++; @@ -152,7 +152,7 @@ public class DirectAgentAttache extends AgentAttache { _mgr.handleCommands(DirectAgentAttache.this, seq, new Command[]{cmd}); } else { - s_logger.debug("Unable to send ping because agent is disconnected " + _id); + s_logger.debug("Unable to send ping because agent is disconnected " + _id + "(" + _name + ")"); } } catch (Exception e) { s_logger.warn("Unable to complete the ping task", e); diff --git a/engine/orchestration/src/com/cloud/agent/manager/DummyAttache.java b/engine/orchestration/src/com/cloud/agent/manager/DummyAttache.java index 182c1b85d59..2c768480ffe 100755 --- a/engine/orchestration/src/com/cloud/agent/manager/DummyAttache.java +++ b/engine/orchestration/src/com/cloud/agent/manager/DummyAttache.java @@ -23,8 +23,8 @@ import com.cloud.host.Status; public class DummyAttache extends AgentAttache { - public DummyAttache(AgentManagerImpl agentMgr, long id, boolean maintenance) { - super(agentMgr, id, maintenance); + public DummyAttache(AgentManagerImpl agentMgr, long id, String name, boolean maintenance) { + super(agentMgr, id, name, maintenance); }