CLOUDSTACK-4636: In a scaled up setup all Vm's in a cluster were stopped and/or 
started after management server restart
Issue happens as there are more than one thread processing connect for a host 
simultaneously. The VM full sync. is not designed to work in this scenario and 
as a result user VMs may get stopped incorrectly.
Direct agent scan task runs at regular intervals (direct.agent.scan.interval 
defaulted to 90 secs) and identifies hosts that needs to be processed for 
connect. In a normal scenario hosts mostly get connected within that interval 
and there are no issues. But if due to some reason the connect process takes 
more time and is not completed by the time next agent scan runs. In this case, 
based on the db. state same hosts may get picked up again. And then there will 
be situations where more than one thread is processing connect for the same 
host.
The fix is to check if there is a thread already processing connect for a host 
and in this case all subsequent threads for that host will simply bail out. 
Also there may be a scenario where one thread already completed processing 
connect but another thread already got scheduled before that and will again 
repeat the same. This is also prevented by putting appropriate checks.


Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/ae181afb
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/ae181afb
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/ae181afb

Branch: refs/heads/kvm-vnc-listen
Commit: ae181afb005bd26af5503665c5c08883759ffecf
Parents: 28af817
Author: Koushik Das <[email protected]>
Authored: Tue Sep 10 17:21:36 2013 +0530
Committer: Koushik Das <[email protected]>
Committed: Tue Sep 10 17:21:36 2013 +0530

----------------------------------------------------------------------
 .../cloud/agent/manager/AgentManagerImpl.java   | 24 ++++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/ae181afb/engine/orchestration/src/com/cloud/agent/manager/AgentManagerImpl.java
----------------------------------------------------------------------
diff --git 
a/engine/orchestration/src/com/cloud/agent/manager/AgentManagerImpl.java 
b/engine/orchestration/src/com/cloud/agent/manager/AgentManagerImpl.java
index 811ff13..6c88f6e 100755
--- a/engine/orchestration/src/com/cloud/agent/manager/AgentManagerImpl.java
+++ b/engine/orchestration/src/com/cloud/agent/manager/AgentManagerImpl.java
@@ -1037,9 +1037,22 @@ public class AgentManagerImpl extends ManagerBase 
implements AgentManager, Handl
                     s_logger.debug("Simulating start for resource " + 
resource.getName() + " id " + id);
                 }
 
-                tapLoadingAgents(id, TapAgentsAction.Add);
-                _resourceMgr.createHostAndAgent(id, resource, details, false, 
null, false);
-                tapLoadingAgents(id, TapAgentsAction.Del);
+                if (tapLoadingAgents(id, TapAgentsAction.Add)) {
+                    try {
+                        AgentAttache agentattache = findAttache(id);
+                        if (agentattache == null) {
+                            s_logger.debug("Creating agent for host " + id);
+                            _resourceMgr.createHostAndAgent(id, resource, 
details, false, null, false);
+                            s_logger.debug("Completed creating agent for host 
" + id);
+                        } else {
+                            s_logger.debug("Agent already created in another 
thread for host " + id + ", ignore this");
+                        }
+                    } finally {
+                        tapLoadingAgents(id, TapAgentsAction.Del);
+                    }
+                } else {
+                    s_logger.debug("Agent creation already getting processed 
in another thread for host " + id + ", ignore this");
+                }
             } catch (Exception e) {
                 s_logger.warn("Unable to simulate start on resource " + id + " 
name " + resource.getName(), e);
             } finally {
@@ -1290,7 +1303,10 @@ public class AgentManagerImpl extends ManagerBase 
implements AgentManager, Handl
     public boolean tapLoadingAgents(Long hostId, TapAgentsAction action) {
         synchronized (_loadingAgents) {
             if (action == TapAgentsAction.Add) {
-                _loadingAgents.add(hostId);
+                if (_loadingAgents.contains(hostId))
+                    return false;
+                else
+                    _loadingAgents.add(hostId);
             } else if (action == TapAgentsAction.Del) {
                 _loadingAgents.remove(hostId);
             } else if (action == TapAgentsAction.Contains) {

Reply via email to