bug 11701: cleanup, and vm host sync on delta

This commit is contained in:
Abhinandan Prateek 2011-10-27 16:23:04 +05:30
parent d61a544e68
commit edbce41065
4 changed files with 35 additions and 92 deletions

View File

@ -23,9 +23,11 @@ import com.cloud.utils.Pair;
import com.cloud.vm.VirtualMachine.State;
public class ClusterSyncAnswer extends Answer {
long _clusterId;
HashMap<String, Pair<String, State>> _newStates;
int _type = -1; // 0 for full, 1 for delta
private long _clusterId;
private HashMap<String, Pair<String, State>> _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;
}
}

View File

@ -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<String, Pair<String, State>> 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<String, VmState> vmStates = new HashMap<String, VmState>();
Host lhost = Host.getByUuid(conn, _host.uuid);
Map<VM, VM.Record> 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<String, Pair<String, State>> 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<String, State> oldState = oldStates.remove(vm);
//check if host is changed
if (host_uuid != null){
if (!host_uuid.equals(oldState.first())){
changes.put(vm, new Pair<String, State>(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);

View File

@ -61,6 +61,6 @@ public class XenServerGuru extends HypervisorGuruBase implements HypervisorGuru
@Override
public boolean trackVmHostChange() {
return false;
return true;
}
}

View File

@ -1518,7 +1518,7 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene
}
public Commands deltaSync(Map<String, Pair<String, State>> newStates) {
Map<Long, AgentVmInfo> states = convertDeltaToInfos(newStates);
Map<Long, AgentVmInfo> states = convertToInfos(newStates);
Commands commands = new Commands(OnError.Continue);
for (Map.Entry<Long, AgentVmInfo> 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<Long, AgentVmInfo> convertDeltaToInfos(final Map<String, Pair<String, State>> states) {
final HashMap<Long, AgentVmInfo> map = new HashMap<Long, AgentVmInfo>();
if (states == null) {
return map;
}
Collection<VirtualMachineGuru<? extends VMInstanceVO>> vmGurus = _vmGurus.values();
for (Map.Entry<String, Pair<String, State>> entry : states.entrySet()) {
for (VirtualMachineGuru<? extends VMInstanceVO> 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<Long, AgentVmInfo> convertToInfos(final Map<String, Pair<String, State>> newStates) {
final HashMap<Long, AgentVmInfo> map = new HashMap<Long, AgentVmInfo>();
@ -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 {