diff --git a/api/src/com/cloud/agent/api/ClusterSyncAnswer.java b/api/src/com/cloud/agent/api/ClusterSyncAnswer.java index ac4434d3152..d3ea9452016 100644 --- a/api/src/com/cloud/agent/api/ClusterSyncAnswer.java +++ b/api/src/com/cloud/agent/api/ClusterSyncAnswer.java @@ -23,9 +23,11 @@ import com.cloud.utils.Pair; import com.cloud.vm.VirtualMachine.State; public class ClusterSyncAnswer extends Answer { - long _clusterId; - HashMap> _newStates; - int _type = -1; // 0 for full, 1 for delta + private long _clusterId; + private HashMap> _newStates; + private int _type = -1; // 0 for full, 1 for delta + private boolean isExecuted=false; // this is to avoidf double execution first time, due to framework ??? + public static final int FULL_SYNC=0; public static final int DELTA_SYNC=1; @@ -59,4 +61,12 @@ public class ClusterSyncAnswer extends Answer { public boolean isDelta(){ return _type==1; } + + public boolean execute(){ + return !isExecuted; + } + + public void setExecuted(){ + isExecuted = true; + } } \ No newline at end of file diff --git a/core/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java b/core/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java index 6046678e380..7be5337c9e5 100755 --- a/core/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java +++ b/core/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java @@ -3895,20 +3895,6 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } StartupRoutingCommand cmd = new StartupRoutingCommand(); fillHostInfo(conn, cmd); - - Pool pool; - try { - pool = Pool.getByUuid(conn, _host.pool); - Pool.Record poolr = pool.getRecord(conn); - - Host.Record hostr = poolr.master.getRecord(conn); - if (_host.uuid.equals(hostr.uuid)) { - s_logger.debug("Doing the initial cluster sync from the pool master " + _host.ip); - fullClusterSync(cmd, conn); - } - } catch (Exception e) { - s_logger.warn("Check for master failed, failing the probbaly initial Cluster sync ", e); - } cmd.setHypervisorType(HypervisorType.XenServer); cmd.setCluster(_cluster); cmd.setPoolSync(false); @@ -6483,7 +6469,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe return new ClusterSyncAnswer(cmd.getClusterId()); } HashMap> newStates; - int sync_type; + int sync_type=-1; if (cmd.isRightStep()){ // do full sync newStates=fullClusterSync(conn); @@ -6500,41 +6486,6 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe cmd.incrStep(); return new ClusterSyncAnswer(cmd.getClusterId(), newStates, sync_type); } - - - protected void fullClusterSync(StartupRoutingCommand cmd, Connection conn) { - try { - final HashMap vmStates = new HashMap(); - Host lhost = Host.getByUuid(conn, _host.uuid); - Map vm_map = VM.getAllRecords(conn); - for (VM.Record record: vm_map.values()) { - if (record.isControlDomain || record.isASnapshot || record.isATemplate) { - continue; // Skip DOM0 - } - String vm_name = record.nameLabel; - VmPowerState ps = record.powerState; - final State state = convertToState(ps); - Host host = record.residentOn; - String host_uuid = null; - if( ! isRefNull(host) ) { - host_uuid = host.getUuid(conn); - VmState vm_state = new StartupRoutingCommand.VmState(state, host_uuid); - vmStates.put(vm_name, vm_state); - s_vms.put(_cluster, host_uuid, vm_name, state); - } - if (s_logger.isTraceEnabled()) { - s_logger.trace("VM " + vm_name + ": powerstate = " + ps + "; vm state=" + state.toString()); - } - } - cmd.setChanges(vmStates); - } catch (final Throwable e) { - String msg = "Unable to get vms through host " + _host.uuid + " due to to " + e.toString(); - s_logger.warn(msg, e); - throw new CloudRuntimeException(msg); - } - } - - protected HashMap> fullClusterSync(Connection conn) { s_vms.clear(_cluster); @@ -6589,6 +6540,13 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe State newState = entry.getValue().second(); String host_uuid = entry.getValue().first(); final Pair oldState = oldStates.remove(vm); + //check if host is changed + if (host_uuid != null){ + if (!host_uuid.equals(oldState.first())){ + changes.put(vm, new Pair(host_uuid, newState)); + s_vms.put(_cluster, host_uuid, vm, newState); + } + } if (newState == State.Stopped && oldState != null && oldState.second() != State.Stopping && oldState.second() != State.Stopped) { newState = getRealPowerState(conn, vm); diff --git a/server/src/com/cloud/hypervisor/XenServerGuru.java b/server/src/com/cloud/hypervisor/XenServerGuru.java index 703ffd89a95..11d77f18b52 100644 --- a/server/src/com/cloud/hypervisor/XenServerGuru.java +++ b/server/src/com/cloud/hypervisor/XenServerGuru.java @@ -61,6 +61,6 @@ public class XenServerGuru extends HypervisorGuruBase implements HypervisorGuru @Override public boolean trackVmHostChange() { - return false; + return true; } } diff --git a/server/src/com/cloud/vm/VirtualMachineManagerImpl.java b/server/src/com/cloud/vm/VirtualMachineManagerImpl.java index f9a143e023c..10e07da8308 100755 --- a/server/src/com/cloud/vm/VirtualMachineManagerImpl.java +++ b/server/src/com/cloud/vm/VirtualMachineManagerImpl.java @@ -1518,7 +1518,7 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene } public Commands deltaSync(Map> newStates) { - Map states = convertDeltaToInfos(newStates); + Map states = convertToInfos(newStates); Commands commands = new Commands(OnError.Continue); for (Map.Entry entry : states.entrySet()) { @@ -1528,8 +1528,12 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene Command command = null; if (vm != null) { + String host_guid = info.getHostUuid(); + Host host = _hostDao.findByGuid(host_guid); + long hId = host.getId(); + HypervisorGuru hvGuru = _hvGuruMgr.getGuru(vm.getHypervisorType()); - command = compareState(vm.hostId, vm, info, false, hvGuru.trackVmHostChange()); + command = compareState(hId, vm, info, false, hvGuru.trackVmHostChange()); } else { if (s_logger.isDebugEnabled()) { s_logger.debug("Cleaning up a VM that is no longer found: " + info.name); @@ -1580,38 +1584,6 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene return commands; } - - protected Map convertDeltaToInfos(final Map> states) { - final HashMap map = new HashMap(); - - if (states == null) { - return map; - } - - Collection> vmGurus = _vmGurus.values(); - - for (Map.Entry> entry : states.entrySet()) { - for (VirtualMachineGuru vmGuru : vmGurus) { - String name = entry.getKey(); - - VMInstanceVO vm = vmGuru.findByName(name); - - if (vm != null) { - map.put(vm.getId(), new AgentVmInfo(entry.getKey(), vmGuru, vm, entry.getValue().second())); - break; - } - - Long id = vmGuru.convertToId(name); - if (id != null) { - map.put(id, new AgentVmInfo(entry.getKey(), vmGuru, null, entry.getValue().second())); - break; - } - } - } - - return map; - } - protected Map convertToInfos(final Map> newStates) { final HashMap map = new HashMap(); @@ -1860,11 +1832,14 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene for (final Answer answer : answers) { if (answer instanceof ClusterSyncAnswer) { ClusterSyncAnswer hs = (ClusterSyncAnswer) answer; - if (hs.isFull()) { - fullSync(hs.getClusterId(), hs.getNewStates()); - } else if (hs.isDelta()) { - deltaSync(hs.getNewStates()); + if (hs.execute()){ + if (hs.isFull()) { + fullSync(hs.getClusterId(), hs.getNewStates()); + } else if (hs.isDelta()) { + deltaSync(hs.getNewStates()); + } } + hs.setExecuted(); } else if (!answer.getResult()) { s_logger.warn("Cleanup failed due to " + answer.getDetails()); } else {