From 753b6a00a674280ec7f29de4f09d8ba85de9477f Mon Sep 17 00:00:00 2001 From: Alex Huang Date: Thu, 12 May 2011 16:57:09 -0700 Subject: [PATCH] faster agent connection --- .../cloud/agent/manager/AgentManagerImpl.java | 121 +++++++++++------- 1 file changed, 72 insertions(+), 49 deletions(-) diff --git a/server/src/com/cloud/agent/manager/AgentManagerImpl.java b/server/src/com/cloud/agent/manager/AgentManagerImpl.java index adb1e91e1a3..3c72a29f37a 100755 --- a/server/src/com/cloud/agent/manager/AgentManagerImpl.java +++ b/server/src/com/cloud/agent/manager/AgentManagerImpl.java @@ -200,6 +200,7 @@ public class AgentManagerImpl implements AgentManager, HandlerFactory, ResourceS protected List> _hostMonitors = new ArrayList>(17); protected List> _cmdMonitors = new ArrayList>(17); protected List> _creationMonitors = new ArrayList>(17); + protected List _loadingAgents = new ArrayList(); protected int _monitorId = 0; protected NioServer _connection; @@ -226,7 +227,7 @@ public class AgentManagerImpl implements AgentManager, HandlerFactory, ResourceS @Inject protected StoragePoolDao _storagePoolDao = null; @Inject - protected StoragePoolHostDao _storagePoolHostDao = null; + protected StoragePoolHostDao _storagePoolHostDao = null; @Inject protected GuestOSCategoryDao _guestOSCategoryDao = null; @Inject @@ -1520,6 +1521,13 @@ public class AgentManagerImpl implements AgentManager, HandlerFactory, ResourceS 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) { if (event != null && event.equals(Event.Remove)) { @@ -1810,50 +1818,65 @@ public class AgentManagerImpl implements AgentManager, HandlerFactory, ResourceS protected AgentAttache simulateStart(Long id, ServerResource resource, Map details, boolean old, List hostTags, String allocationState) 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); - return null; + synchronized (_loadingAgents) { + s_logger.debug("Adding to loading agents " + id); + _loadingAgents.add(id); } } - - StartupCommand[] cmds = resource.initialize(); - if (cmds == null) { - s_logger.info("Unable to fully initialize the agent because no StartupCommands are returned"); - if (id != null) { - _hostDao.updateStatus(host, Event.AgentDisconnected, _nodeId); - } - return null; - } - - 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()) { - new Request(0l, -1l, -1l, cmds, true, false, true).log(-1, "Startup request from directly connected host: "); - // s_logger.debug("Startup request from directly connected host: " - // + new Request(0l, -1l, -1l, cmds, true, false, true) - // .toString()); - } + StartupCommand[] cmds = null; try { - attache = handleDirectConnect(resource, cmds, details, old, hostTags, allocationState); - } 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 (id != null) { + host = _hostDao.findById(id); + if (!_hostDao.directConnect(host, _nodeId, false)) { + s_logger.info("MS " + host.getManagementServerId() + " is loading " + host); + return null; + } + } - if (attache == null) { - resource.disconnected(); - return null; + cmds = resource.initialize(); + if (cmds == null) { + s_logger.info("Unable to fully initialize the agent because no StartupCommands are returned"); + return null; + } + + 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()) { + new Request(0l, -1l, -1l, cmds, true, false, true).log(-1, "Startup request from directly connected host: "); + // s_logger.debug("Startup request from directly connected host: " + // + new Request(0l, -1l, -1l, cmds, true, false, true) + // .toString()); + } + try { + attache = handleDirectConnect(resource, cmds, details, old, hostTags, allocationState); + } 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) { + synchronized (_loadingAgents) { + _loadingAgents.remove(id); + } + } + if (attache == null) { + if (cmds != null) { + resource.disconnected(); + } + if (host != null) { + _hostDao.updateStatus(host, Event.AgentDisconnected, _nodeId); + } + } } return attache; } @@ -2473,8 +2496,8 @@ public class AgentManagerImpl implements AgentManager, HandlerFactory, ResourceS } public AgentAttache handleConnect(final Link link, - final StartupCommand[] startup) throws IllegalArgumentException, - ConnectionException { + final StartupCommand[] startup) throws IllegalArgumentException, + ConnectionException { HostVO server = null; boolean handled = notifyCreatorsOfConnection(startup); if (!handled) { @@ -2483,16 +2506,16 @@ public class AgentManagerImpl implements AgentManager, HandlerFactory, ResourceS server = _hostDao.findByGuid(startup[0].getGuid()); } - if (server == null) { - return null; - } - long id = server.getId(); + if (server == null) { + return null; + } + long id = server.getId(); - AgentAttache attache = createAttache(id, server, link); + AgentAttache attache = createAttache(id, server, link); - attache = notifyMonitorsOfConnection(attache, startup); + attache = notifyMonitorsOfConnection(attache, startup); - return attache; + return attache; } public AgentAttache findAgent(long hostId) {