From dc3e42855bba633a919aa49ecc6845a42d201fc4 Mon Sep 17 00:00:00 2001 From: Alex Huang Date: Mon, 9 May 2011 17:54:11 -0700 Subject: [PATCH] Propagate fix on 2.2.4 --- core/src/com/cloud/host/dao/HostDao.java | 45 ++++++++++--------- core/src/com/cloud/host/dao/HostDaoImpl.java | 10 ++++- .../cloud/agent/manager/AgentManagerImpl.java | 16 ++++--- 3 files changed, 42 insertions(+), 29 deletions(-) diff --git a/core/src/com/cloud/host/dao/HostDao.java b/core/src/com/cloud/host/dao/HostDao.java index cd968ff75a2..f19520e6a42 100644 --- a/core/src/com/cloud/host/dao/HostDao.java +++ b/core/src/com/cloud/host/dao/HostDao.java @@ -1,8 +1,8 @@ /** * Copyright (C) 2010 Cloud.com, Inc. All rights reserved. - * + * * This software is licensed under the GNU General Public License v3 or later. - * + * * It is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or any later version. @@ -10,10 +10,10 @@ * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public License * along with this program. If not, see . - * + * */ package com.cloud.host.dao; @@ -21,22 +21,22 @@ import java.util.Date; import java.util.List; import com.cloud.host.Host; +import com.cloud.host.Host.Type; import com.cloud.host.HostVO; import com.cloud.host.Status; -import com.cloud.host.Host.Type; import com.cloud.host.Status.Event; import com.cloud.info.RunningHostCountInfo; import com.cloud.utils.db.GenericDao; /** * Data Access Object for server - * + * */ public interface HostDao extends GenericDao { List listBy(Host.Type type, Long clusterId, Long podId, long dcId); - + long countBy(long clusterId, Status... statuses); - + List listByDataCenter(long dcId); List listByHostPod(long podId); List listByStatus(Status... status); @@ -48,27 +48,28 @@ public interface HostDao extends GenericDao { * @return list of Hosts */ List listSecondaryStorageHosts(); - + /** * Mark all hosts in Up or Orphaned state as disconnected. This method * is used at AgentManager startup to reset all of the connections. - * + * * @param msId management server id. * @param statuses states of the host. */ void markHostsAsDisconnected(long msId, Status... states); - + List findLostHosts(long timeout); - + List findHostsLike(String hostName); /** * Find hosts that are directly connected. */ List findDirectlyConnectedHosts(); - + List findDirectAgentToLoad(long msid, long lastPingSecondsAfter, Long limit); - boolean directConnect(HostVO host, long msId); + + boolean directConnect(HostVO host, long msId, boolean secondConnect); /** * Mark the host as disconnected if it is in one of these states. @@ -76,16 +77,16 @@ public interface HostDao extends GenericDao { * The lastPinged timestamp is set to current. * The state is set to the state passed in. * The disconnectedOn timestamp is set to current. - * + * * @param host host to be marked * @param state state to be set to. * @param ifStates only if it is one of these states. * @return true if it's updated; false if not. */ boolean disconnect(HostVO host, Event event, long msId); - + boolean connect(HostVO host, long msId); - + HostVO findByStorageIpAddressInDataCenter(long dcId, String privateIpAddress); HostVO findByPrivateIpAddressInDataCenter(long dcId, String privateIpAddress); @@ -95,7 +96,7 @@ public interface HostDao extends GenericDao { * @return HostVO or null if not found. */ public HostVO findByGuid(String macAddress); - + /** * find all hosts of a certain type in a data center @@ -120,7 +121,7 @@ public interface HostDao extends GenericDao { * @return */ List findLostHosts2(long timeout, Type type); - + /** * update the host and changes the status depending on the Event and * the current status. If the status changed between @@ -130,11 +131,11 @@ public interface HostDao extends GenericDao { * @return true if updated; false if not. */ boolean updateStatus(HostVO host, Event event, long msId); - + List getRunningHostCounts(Date cutTime); - + long getNextSequence(long hostId); - + void loadDetails(HostVO host); void loadHostTags(HostVO host); diff --git a/core/src/com/cloud/host/dao/HostDaoImpl.java b/core/src/com/cloud/host/dao/HostDaoImpl.java index 09a0828aee4..e82d611bc00 100644 --- a/core/src/com/cloud/host/dao/HostDaoImpl.java +++ b/core/src/com/cloud/host/dao/HostDaoImpl.java @@ -47,6 +47,7 @@ import com.cloud.utils.db.Filter; import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; +import com.cloud.utils.db.SearchCriteria.Op; import com.cloud.utils.db.Transaction; import com.cloud.utils.db.UpdateBuilder; @@ -193,9 +194,11 @@ public class HostDaoImpl extends GenericDaoBase implements HostDao UnmanagedDirectConnectSearch.done(); DirectConnectSearch = createSearchBuilder(); - DirectConnectSearch.and("server", DirectConnectSearch.entity().getManagementServerId(), SearchCriteria.Op.NULL); DirectConnectSearch.and("resource", DirectConnectSearch.entity().getResource(), SearchCriteria.Op.NNULL); DirectConnectSearch.and("id", DirectConnectSearch.entity().getId(), SearchCriteria.Op.EQ); + DirectConnectSearch.op(Op.AND, "nullserver", DirectConnectSearch.entity().getManagementServerId(), SearchCriteria.Op.NULL); + DirectConnectSearch.or("server", DirectConnectSearch.entity().getManagementServerId(), SearchCriteria.Op.EQ); + DirectConnectSearch.cp(); DirectConnectSearch.done(); _statusAttr = _allAttributes.get("status"); @@ -218,9 +221,12 @@ public class HostDaoImpl extends GenericDaoBase implements HostDao } @Override - public boolean directConnect(HostVO host, long msId) { + public boolean directConnect(HostVO host, long msId, boolean secondConnect) { SearchCriteria sc = DirectConnectSearch.create(); sc.setParameters("id", host.getId()); + if (secondConnect) { + sc.setParameters("server", msId); + } host.setManagementServerId(msId); host.setLastPinged(System.currentTimeMillis() >> 10); UpdateBuilder ub = getUpdateBuilder(host); diff --git a/server/src/com/cloud/agent/manager/AgentManagerImpl.java b/server/src/com/cloud/agent/manager/AgentManagerImpl.java index 5d9afb84448..5738a5ddf22 100755 --- a/server/src/com/cloud/agent/manager/AgentManagerImpl.java +++ b/server/src/com/cloud/agent/manager/AgentManagerImpl.java @@ -1230,17 +1230,23 @@ public class AgentManagerImpl implements AgentManager, HandlerFactory { HostVO host = null; if (id != null) { host = _hostDao.findById(id); - if (host.getManagementServerId() != null) { + if (!_hostDao.directConnect(host, _nodeId, false)) { s_logger.info("MS " + host.getManagementServerId() + " is loading " + host); return null; } } StartupCommand[] cmds = resource.initialize(); - if (cmds == null) - return null; + 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); + } + } + if (host != null) { - if (!_hostDao.directConnect(host, _nodeId)) { - s_logger.info("Someone else is loading " + host); + 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; }