diff --git a/server/src/com/cloud/agent/manager/AgentManagerImpl.java b/server/src/com/cloud/agent/manager/AgentManagerImpl.java index 069819251c8..293035903aa 100755 --- a/server/src/com/cloud/agent/manager/AgentManagerImpl.java +++ b/server/src/com/cloud/agent/manager/AgentManagerImpl.java @@ -1274,7 +1274,7 @@ public class AgentManagerImpl implements AgentManager, HandlerFactory, Manager { try { if (id != null) { host = _hostDao.findById(id); - if (!_hostDao.directConnect(host, _nodeId, false)) { + if (!_hostDao.directConnect(host, _nodeId)) { s_logger.info("MS " + host.getManagementServerId() + " is loading " + host); return null; } @@ -1287,7 +1287,7 @@ public class AgentManagerImpl implements AgentManager, HandlerFactory, Manager { } if (host != null) { - if (!_hostDao.directConnect(host, _nodeId, true)) { + if (!_hostDao.directConnect(host, _nodeId)) { host = _hostDao.findById(id); s_logger.info("MS " + host.getManagementServerId() + " is loading " + host + " after it has been initialized."); return null; diff --git a/server/src/com/cloud/agent/manager/ClusteredAgentManagerImpl.java b/server/src/com/cloud/agent/manager/ClusteredAgentManagerImpl.java index e7048c62c1e..160de9d7108 100644 --- a/server/src/com/cloud/agent/manager/ClusteredAgentManagerImpl.java +++ b/server/src/com/cloud/agent/manager/ClusteredAgentManagerImpl.java @@ -143,18 +143,7 @@ public class ClusteredAgentManagerImpl extends AgentManagerImpl implements Clust } private void runDirectAgentScanTimerTask() { - GlobalLock scanLock = GlobalLock.getInternLock("clustermgr.scan"); - try { - if (scanLock.lock(ACQUIRE_GLOBAL_LOCK_TIMEOUT_FOR_COOPERATION)) { - try { - scanDirectAgentToLoad(); - } finally { - scanLock.unlock(); - } - } - } finally { - scanLock.releaseRef(); - } + scanDirectAgentToLoad(); } private void scanDirectAgentToLoad() { @@ -164,22 +153,8 @@ public class ClusteredAgentManagerImpl extends AgentManagerImpl implements Clust // for agents that are self-managed, threshold to be considered as disconnected is 3 ping intervals long cutSeconds = (System.currentTimeMillis() >> 10) - (_pingInterval * 3); - List hosts = _hostDao.findDirectAgentToLoad(cutSeconds, _loadSize); - if (hosts != null && hosts.size() == _loadSize) { - //if list contains more than one cluster, exclude the last cluster from the list - if (hosts.size() > 1 && hosts.get(0).getClusterId().longValue() != hosts.get(hosts.size()-1).getClusterId().longValue()) { - Long clusterId = hosts.get((int) (_loadSize - 1)).getClusterId(); - if (clusterId != null) { - for (int i = (int) (_loadSize - 1); i > 0; i--) { - if (hosts.get(i).getClusterId().longValue() == clusterId.longValue()) { - hosts.remove(i); - } else { - break; - } - } - } - } - } + List hosts = _hostDao.findAndUpdateDirectAgentToLoad(cutSeconds, _loadSize, _nodeId); + if (hosts != null && hosts.size() > 0) { s_logger.debug("Found " + hosts.size() + " unmanaged direct hosts, processing connect for them..."); for (HostVO host : hosts) { diff --git a/server/src/com/cloud/host/dao/HostDao.java b/server/src/com/cloud/host/dao/HostDao.java index e296ff8058b..30d1592346c 100644 --- a/server/src/com/cloud/host/dao/HostDao.java +++ b/server/src/com/cloud/host/dao/HostDao.java @@ -68,7 +68,7 @@ public interface HostDao extends GenericDao { */ List findDirectlyConnectedHosts(); - List findDirectAgentToLoad(long lastPingSecondsAfter, Long limit); + List findAndUpdateDirectAgentToLoad(long lastPingSecondsAfter, Long limit, long managementServerId); /** * Mark the host as disconnected if it is in one of these states. * The management server id is set to null. @@ -165,7 +165,7 @@ public interface HostDao extends GenericDao { List listSecondaryStorageHosts(long dataCenterId); - boolean directConnect(HostVO host, long msId, boolean secondConnect); + boolean directConnect(HostVO host, long msId); List listDirectHostsBy(long msId, Status status); diff --git a/server/src/com/cloud/host/dao/HostDaoImpl.java b/server/src/com/cloud/host/dao/HostDaoImpl.java index c8f6ba83437..e56ee693429 100644 --- a/server/src/com/cloud/host/dao/HostDaoImpl.java +++ b/server/src/com/cloud/host/dao/HostDaoImpl.java @@ -371,12 +371,24 @@ public class HostDaoImpl extends GenericDaoBase implements HostDao return search(sc, null); } - @Override - public List findDirectAgentToLoad(long lastPingSecondsAfter, Long limit) { + @Override @DB + public List findAndUpdateDirectAgentToLoad(long lastPingSecondsAfter, Long limit, long managementServerId) { + Transaction txn = Transaction.currentTxn(); + txn.start(); + SearchCriteria sc = UnmanagedDirectConnectSearch.create(); sc.setParameters("lastPinged", lastPingSecondsAfter); sc.setParameters("statuses", Status.ErrorInMaintenance, Status.Maintenance, Status.PrepareForMaintenance); - return search(sc, new Filter(HostVO.class, "clusterId", true, 0L, limit)); + List hosts = lockRows(sc, new Filter(HostVO.class, "clusterId", true, 0L, limit), true); + + for (HostVO host : hosts) { + host.setManagementServerId(managementServerId); + update(host.getId(), host); + } + + txn.commit(); + + return hosts; } @Override @@ -525,12 +537,10 @@ public class HostDaoImpl extends GenericDaoBase implements HostDao } @Override - public boolean directConnect(HostVO host, long msId, boolean secondConnect) { + public boolean directConnect(HostVO host, long msId) { SearchCriteria sc = DirectConnectSearch.create(); sc.setParameters("id", host.getId()); - if (secondConnect) { - sc.setParameters("server", msId); - } + sc.setParameters("server", msId); host.setManagementServerId(msId); host.setLastPinged(System.currentTimeMillis() >> 10);