diff --git a/server/src/com/cloud/agent/manager/AgentManagerImpl.java b/server/src/com/cloud/agent/manager/AgentManagerImpl.java index 8044a4a0e9d..fe2681007e5 100755 --- a/server/src/com/cloud/agent/manager/AgentManagerImpl.java +++ b/server/src/com/cloud/agent/manager/AgentManagerImpl.java @@ -182,6 +182,7 @@ public class AgentManagerImpl implements AgentManager, HandlerFactory { 11); protected List> _cmdMonitors = new ArrayList>( 11); + protected List _loadingAgents = new ArrayList(); 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 details, boolean old, List hostTags) - throws IllegalArgumentException { + protected AgentAttache simulateStart(Long id, ServerResource resource, Map details, boolean old, List 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() {