Faster agent connection

This commit is contained in:
Alex Huang 2011-05-10 22:55:14 -07:00
parent 4fd1bb8cfa
commit e467d8b788
1 changed files with 62 additions and 41 deletions

View File

@ -182,6 +182,7 @@ public class AgentManagerImpl implements AgentManager, HandlerFactory {
11);
protected List<Pair<Integer, Listener>> _cmdMonitors = new ArrayList<Pair<Integer, Listener>>(
11);
protected List<Long> _loadingAgents = new ArrayList<Long>();
protected int _monitorId = 0;
protected NioServer _connection;
@ -925,6 +926,13 @@ public class AgentManagerImpl implements AgentManager, HandlerFactory {
if (attache != null) {
disconnect(attache, event, investigate);
} else {
synchronized (_loadingAgents) {
if (_loadingAgents.contains(hostId)) {
s_logger.info("Host " + hostId + " is being loaded so no disconnects needed.");
return;
}
}
HostVO host = _hostDao.findById(hostId);
if (host != null && host.getRemoved() == null) {
_hostDao.updateStatus(host, event, _nodeId);
@ -1221,54 +1229,67 @@ public class AgentManagerImpl implements AgentManager, HandlerFactory {
.getDetails(), null));
}
protected AgentAttache simulateStart(Long id, ServerResource resource,
Map<String, String> details, boolean old, List<String> hostTags)
throws IllegalArgumentException {
protected AgentAttache simulateStart(Long id, ServerResource resource, Map<String, String> details, boolean old, List<String> hostTags) throws IllegalArgumentException {
HostVO host = null;
if (id != null) {
host = _hostDao.findById(id);
if (!_hostDao.directConnect(host, _nodeId, false)) {
s_logger.info("MS " + host.getManagementServerId() + " is loading " + host);
synchronized (_loadingAgents) {
_loadingAgents.add(id);
}
}
AgentAttache attache = null;
StartupCommand[] cmds = null;
try {
if (id != null) {
host = _hostDao.findById(id);
if (!_hostDao.directConnect(host, _nodeId, false)) {
s_logger.info("MS " + host.getManagementServerId() + " is loading " + host);
return null;
}
}
cmds = resource.initialize();
if (cmds == null) {
s_logger.info("Unable to fully initialize the agent because no StartupCommands are returned");
return null;
}
}
StartupCommand[] cmds = resource.initialize();
if (cmds == null) {
s_logger.info("Unable to fully initialize the agent because no StartupCommands are returned");
if (host != null) {
if (!_hostDao.directConnect(host, _nodeId, true)) {
host = _hostDao.findById(id);
s_logger.info("MS " + host.getManagementServerId() + " is loading " + host + " after it has been initialized.");
return null;
}
}
if (s_logger.isDebugEnabled()) {
s_logger.debug("Startup request from directly connected host: " + new Request(0l, -1l, -1l, cmds, true, false).toString());
}
try {
attache = handleDirectConnect(resource, cmds, details, old, hostTags);
} catch (IllegalArgumentException ex) {
s_logger.warn("Unable to connect due to ", ex);
throw ex;
} catch (Exception e) {
s_logger.warn("Unable to connect due to ", e);
}
} finally {
if (id != null) {
_hostDao.updateStatus(host, Event.AgentDisconnected, _nodeId);
synchronized (_loadingAgents) {
_loadingAgents.remove(id);
}
}
if (attache == null) {
if (cmds != null) {
resource.disconnected();
}
if (host != null) {
_hostDao.updateStatus(host, Event.AgentDisconnected, _nodeId);
}
}
}
if (host != null) {
if (!_hostDao.directConnect(host, _nodeId, true)) {
host = _hostDao.findById(id);
s_logger.info("MS " + host.getManagementServerId() + " is loading " + host + " after it has been initialized.");
resource.disconnected();
return null;
}
}
AgentAttache attache = null;
if (s_logger.isDebugEnabled()) {
s_logger.debug("Startup request from directly connected host: "
+ new Request(0, -1, -1, cmds, false).toString());
}
try {
attache = handleDirectConnect(resource, cmds, details, old,
hostTags);
} catch (IllegalArgumentException ex) {
s_logger.warn("Unable to connect due to ", ex);
throw ex;
} catch (Exception e) {
s_logger.warn("Unable to connect due to ", e);
}
if (attache == null) {
resource.disconnected();
return null;
}
return attache;
}
return attache;
}
@Override
public boolean stop() {