Skip to content

Commit 5e4acc2

Browse files
author
Abhinandan Prateek
committed
bug 12530: introduce initial full sync; cleanup syncronization;
1 parent ac0275b commit 5e4acc2

6 files changed

Lines changed: 148 additions & 80 deletions

File tree

‎api/src/com/cloud/agent/api/ClusterSyncAnswer.java‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,8 @@ public class ClusterSyncAnswer extends Answer {
3131

3232
public static final int FULL_SYNC=0;
3333
public static final int DELTA_SYNC=1;
34-
35-
public ClusterSyncAnswer(long clusterId) {
36-
_clusterId = clusterId;
37-
result = false;
38-
this.details = "Ignore sync as this is not a pool master";
39-
_type = -1;
40-
}
41-
34+
public static final int INITIAL_FULL_SYNC=2;
35+
4236
// this is here because a cron command answer is being sent twice
4337
// AgentAttache.processAnswers
4438
// AgentManagerImpl.notifyAnswersToMonitors
@@ -67,6 +61,14 @@ public ClusterSyncAnswer(long clusterId, HashMap<String, Pair<String, State>> ne
6761
result = true;
6862
}
6963

64+
public ClusterSyncAnswer(long clusterId, HashMap<String, Pair<String, State>> newStates, HashMap<String, Pair<String, State>> allStates, boolean init){
65+
_clusterId = clusterId;
66+
_newStates = newStates;
67+
_allStates = allStates;
68+
_type = INITIAL_FULL_SYNC;
69+
result = true;
70+
}
71+
7072
public long getClusterId() {
7173
return _clusterId;
7274
}

‎api/src/com/cloud/agent/api/ClusterSyncCommand.java‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,15 @@ public class ClusterSyncCommand extends Command implements CronCommand {
2222
private int _interval;
2323
private int _skipSteps; // skip this many steps for full sync
2424
private int _steps;
25-
25+
private boolean _init;
2626
private long _clusterId;
2727

28-
public ClusterSyncCommand() {
29-
}
30-
3128
public ClusterSyncCommand(int interval, int skipSteps, long clusterId){
3229
_interval = interval;
3330
_skipSteps = skipSteps;
3431
_clusterId = clusterId;
3532
_steps=0;
33+
_init=true;
3634
}
3735

3836
@Override
@@ -50,7 +48,7 @@ public void incrStep(){
5048
}
5149

5250
public boolean isRightStep(){
53-
return (_steps==_skipSteps);
51+
return (_steps==0);
5452
}
5553

5654
public long getClusterId() {
@@ -62,4 +60,12 @@ public boolean executeInSequence() {
6260
return false;
6361
}
6462

63+
public boolean isInit(){
64+
return _init;
65+
}
66+
67+
public void unsetInit(){
68+
_init = false;
69+
}
70+
6571
}

‎core/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java‎

Lines changed: 54 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,8 +1081,10 @@ public StartAnswer execute(StartCommand cmd) {
10811081
}
10821082
}
10831083
}
1084-
s_logger.debug("1. The VM " + vmName + " is in Starting state.");
1085-
s_vms.put(_cluster, _name, vmName, State.Starting);
1084+
synchronized (s_vms) {
1085+
s_logger.debug("1. The VM " + vmName + " is in Starting state.");
1086+
s_vms.put(_cluster, _name, vmName, State.Starting);
1087+
}
10861088

10871089
Host host = Host.getByUuid(conn, _host.uuid);
10881090
vm = createVmFromTemplate(conn, vmSpec, host);
@@ -1155,7 +1157,7 @@ public StartAnswer execute(StartCommand cmd) {
11551157
}
11561158
}
11571159

1158-
state = State.Starting;
1160+
state = State.Running;
11591161
return new StartAnswer(cmd);
11601162
} catch (Exception e) {
11611163
s_logger.warn("Catch Exception: " + e.getClass().toString() + " due to " + e.toString(), e);
@@ -2191,8 +2193,10 @@ protected PrepareForMigrationAnswer execute(PrepareForMigrationCommand cmd) {
21912193
for (NicTO nic : nics) {
21922194
getNetwork(conn, nic);
21932195
}
2194-
s_logger.debug("4. The VM " + vm.getName() + " is in " + State.Migrating + " state");
2195-
s_vms.put(_cluster, _name, vm.getName(), State.Migrating);
2196+
synchronized (s_vms) {
2197+
s_logger.debug("4. The VM " + vm.getName() + " is in " + State.Migrating + " state");
2198+
s_vms.put(_cluster, _name, vm.getName(), State.Migrating);
2199+
}
21962200

21972201
return new PrepareForMigrationAnswer(cmd);
21982202
} catch (Exception e) {
@@ -2428,8 +2432,10 @@ protected MigrateAnswer execute(final MigrateCommand cmd) {
24282432

24292433
state = s_vms.getState(_cluster, vmName);
24302434

2431-
s_logger.debug("5. The VM " + vmName + " is in " + State.Stopping + " state");
2432-
s_vms.put(_cluster, _name, vmName, State.Stopping);
2435+
synchronized (s_vms) {
2436+
s_logger.debug("5. The VM " + vmName + " is in " + State.Stopping + " state");
2437+
s_vms.put(_cluster, _name, vmName, State.Stopping);
2438+
}
24332439
try {
24342440
Set<VM> vms = VM.getByNameLabel(conn, vmName);
24352441

@@ -2495,8 +2501,10 @@ protected MigrateAnswer execute(final MigrateCommand cmd) {
24952501
s_logger.warn(msg, e);
24962502
return new MigrateAnswer(cmd, false, msg, null);
24972503
} finally {
2498-
s_logger.debug("6. The VM " + vmName + " is in " + state + " state");
2499-
s_vms.put(_cluster, _name, vmName, state);
2504+
synchronized (s_vms) {
2505+
s_logger.debug("6. The VM " + vmName + " is in " + state + " state");
2506+
s_vms.put(_cluster, _name, vmName, state);
2507+
}
25002508
}
25012509

25022510
}
@@ -2618,8 +2626,10 @@ record = vm.getRecord(conn);
26182626
@Override
26192627
public RebootAnswer execute(RebootCommand cmd) {
26202628
Connection conn = getConnection();
2621-
s_logger.debug("7. The VM " + cmd.getVmName() + " is in " + State.Starting + " state");
2622-
s_vms.put(_cluster, _name, cmd.getVmName(), State.Starting);
2629+
synchronized (s_vms) {
2630+
s_logger.debug("7. The VM " + cmd.getVmName() + " is in " + State.Starting + " state");
2631+
s_vms.put(_cluster, _name, cmd.getVmName(), State.Starting);
2632+
}
26232633
try {
26242634
Set<VM> vms = null;
26252635
try {
@@ -2642,8 +2652,10 @@ public RebootAnswer execute(RebootCommand cmd) {
26422652
}
26432653
return new RebootAnswer(cmd, "reboot succeeded", null, null);
26442654
} finally {
2645-
s_logger.debug("8. The VM " + cmd.getVmName() + " is in " + State.Running + " state");
2646-
s_vms.put(_cluster, _name, cmd.getVmName(), State.Running);
2655+
synchronized (s_vms) {
2656+
s_logger.debug("8. The VM " + cmd.getVmName() + " is in " + State.Running + " state");
2657+
s_vms.put(_cluster, _name, cmd.getVmName(), State.Running);
2658+
}
26472659
}
26482660
}
26492661

@@ -3113,8 +3125,10 @@ public StopAnswer execute(StopCommand cmd) {
31133125
}
31143126

31153127
if (vms.size() == 0) {
3116-
s_logger.info("VM does not exist on XenServer" + _host.uuid);
3117-
s_vms.remove(_cluster, _name, vmName);
3128+
synchronized (s_vms) {
3129+
s_logger.info("VM does not exist on XenServer" + _host.uuid);
3130+
s_vms.remove(_cluster, _name, vmName);
3131+
}
31183132
return new StopAnswer(cmd, "VM does not exist", 0 , 0L, 0L);
31193133
}
31203134
Long bytesSent = 0L;
@@ -3136,8 +3150,10 @@ public StopAnswer execute(StopCommand cmd) {
31363150

31373151
State state = s_vms.getState(_cluster, vmName);
31383152

3139-
s_logger.debug("9. The VM " + vmName + " is in " + State.Stopping + " state");
3140-
s_vms.put(_cluster, _name, vmName, State.Stopping);
3153+
synchronized (s_vms) {
3154+
s_logger.debug("9. The VM " + vmName + " is in " + State.Stopping + " state");
3155+
s_vms.put(_cluster, _name, vmName, State.Stopping);
3156+
}
31413157

31423158
try {
31433159
if (vmr.powerState == VmPowerState.RUNNING) {
@@ -3198,8 +3214,10 @@ public StopAnswer execute(StopCommand cmd) {
31983214
String msg = "VM destroy failed in Stop " + vmName + " Command due to " + e.getMessage();
31993215
s_logger.warn(msg, e);
32003216
} finally {
3201-
s_logger.debug("10. The VM " + vmName + " is in " + state + " state");
3202-
s_vms.put(_cluster, _name, vmName, state);
3217+
synchronized (s_vms) {
3218+
s_logger.debug("10. The VM " + vmName + " is in " + state + " state");
3219+
s_vms.put(_cluster, _name, vmName, state);
3220+
}
32033221
}
32043222
}
32053223
}
@@ -6563,20 +6581,27 @@ protected Answer execute(final ClusterSyncCommand cmd) {
65636581

65646582
Host.Record hostr = poolr.master.getRecord(conn);
65656583
if (!_host.uuid.equals(hostr.uuid)) {
6566-
return new ClusterSyncAnswer(cmd.getClusterId());
6584+
return new Answer(cmd);
65676585
}
65686586
} catch (Throwable e) {
65696587
s_logger.warn("Check for master failed, failing the Cluster sync command");
6570-
return new ClusterSyncAnswer(cmd.getClusterId());
6588+
return new Answer(cmd);
65716589
}
65726590
HashMap<String, Pair<String, State>> newStates = deltaClusterSync(conn);
6573-
cmd.incrStep();
65746591
if (cmd.isRightStep()){
6592+
cmd.incrStep();
65756593
// do full sync
65766594
HashMap<String, Pair<String, State>> allStates=fullClusterSync(conn);
6577-
return new ClusterSyncAnswer(cmd.getClusterId(), newStates, allStates);
6595+
if (cmd.isInit()){
6596+
cmd.unsetInit();
6597+
return new ClusterSyncAnswer(cmd.getClusterId(), newStates, allStates, true);
6598+
}
6599+
else {
6600+
return new ClusterSyncAnswer(cmd.getClusterId(), newStates, allStates);
6601+
}
65786602
}
65796603
else {
6604+
cmd.incrStep();
65806605
return new ClusterSyncAnswer(cmd.getClusterId(), newStates);
65816606
}
65826607
}
@@ -6613,19 +6638,16 @@ protected HashMap<String, Pair<String, State>> fullClusterSync(Connection conn)
66136638

66146639

66156640
protected HashMap<String, Pair<String, State>> deltaClusterSync(Connection conn) {
6616-
HashMap<String, Pair<String, State>> newStates;
6617-
HashMap<String, Pair<String, State>> oldStates = null;
6618-
66196641
final HashMap<String, Pair<String, State>> changes = new HashMap<String, Pair<String, State>>();
66206642

6621-
newStates = getAllVms(conn);
6622-
if (newStates == null) {
6623-
s_logger.warn("Unable to get the vm states so no state sync at this point.");
6624-
return null;
6625-
}
66266643

66276644
synchronized (s_vms) {
6628-
oldStates = new HashMap<String, Pair<String, State>>(s_vms.size(_cluster));
6645+
HashMap<String, Pair<String, State>> newStates = getAllVms(conn);
6646+
if (newStates == null) {
6647+
s_logger.warn("Unable to get the vm states so no state sync at this point.");
6648+
return null;
6649+
}
6650+
HashMap<String, Pair<String, State>> oldStates = new HashMap<String, Pair<String, State>>(s_vms.size(_cluster));
66296651
oldStates.putAll(s_vms.getClusterVmState(_cluster));
66306652

66316653
for (final Map.Entry<String, Pair<String, State>> entry : newStates.entrySet()) {

‎core/src/com/cloud/hypervisor/xen/resource/XenServerPoolVms.java‎

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package com.cloud.hypervisor.xen.resource;
22

3-
import java.util.ArrayList;
43
import java.util.HashMap;
5-
import java.util.HashSet;
6-
import java.util.Set;
74

85
import org.apache.log4j.Logger;
96

@@ -15,7 +12,6 @@ public class XenServerPoolVms {
1512
private static final Logger s_logger = Logger.getLogger(XenServerPoolVms.class);
1613
private HashMap<String/* clusterId */, HashMap<String/* vm name */, Pair<String/* host uuid */, State/* vm state */>>> _cluster_vms =
1714
new HashMap<String, HashMap<String, Pair<String, State>>>();
18-
private long _last_sync_time=0;
1915

2016
public HashMap<String, Pair<String, State>> getClusterVmState(String clusterId){
2117
HashMap<String, Pair<String, State>> _vms= _cluster_vms.get(clusterId);
@@ -29,9 +25,7 @@ public HashMap<String, Pair<String, State>> getClusterVmState(String clusterId){
2925

3026
public void clear(String clusterId){
3127
HashMap<String, Pair<String, State>> _vms= getClusterVmState(clusterId);
32-
synchronized (_vms) {
33-
_vms.clear();
34-
}
28+
_vms.clear();
3529
}
3630

3731
public State getState(String clusterId, String name){
@@ -42,34 +36,24 @@ public State getState(String clusterId, String name){
4236

4337
public void put(String clusterId, String hostUuid, String name, State state){
4438
HashMap<String, Pair<String, State>> vms= getClusterVmState(clusterId);
45-
synchronized (vms) {
46-
vms.put(name, new Pair<String, State>(hostUuid, state));
47-
}
39+
vms.put(name, new Pair<String, State>(hostUuid, state));
4840
}
4941

5042
public void remove(String clusterId, String hostUuid, String name){
5143
HashMap<String, Pair<String, State>> vms= getClusterVmState(clusterId);
52-
synchronized (vms) {
53-
vms.remove(name);
54-
}
44+
vms.remove(name);
5545
}
5646

5747
public void putAll(String clusterId, HashMap<String, Pair<String, State>> new_vms){
5848
HashMap<String, Pair<String, State>> vms= getClusterVmState(clusterId);
59-
synchronized (vms) {
60-
vms.putAll(new_vms);
61-
}
49+
vms.putAll(new_vms);
6250
}
6351

6452
public int size(String clusterId){
6553
HashMap<String, Pair<String, State>> vms= getClusterVmState(clusterId);
6654
return vms.size();
6755
}
6856

69-
public void initSyncTime(){
70-
_last_sync_time = System.currentTimeMillis();
71-
}
72-
7357
@Override
7458
public String toString(){
7559
StringBuilder sbuf = new StringBuilder("PoolVms=");

‎core/src/com/cloud/vm/VMInstanceVO.java‎

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@
4848
@Table(name="vm_instance")
4949
@Inheritance(strategy=InheritanceType.JOINED)
5050
@DiscriminatorColumn(name="type", discriminatorType=DiscriminatorType.STRING, length=32)
51-
public class VMInstanceVO implements VirtualMachine, FiniteStateObject<State, VirtualMachine.Event> {
52-
@Id
51+
public class VMInstanceVO implements VirtualMachine, FiniteStateObject<State, VirtualMachine.Event> {
52+
@Id
5353
@TableGenerator(name="vm_instance_sq", table="sequence", pkColumnName="name", valueColumnName="value", pkColumnValue="vm_instance_seq", allocationSize=1)
5454
@Column(name="id", updatable=false, nullable = false)
5555
protected long id;
@@ -435,5 +435,29 @@ public String toString() {
435435
toString = new StringBuilder("VM[").append(type.toString()).append("|").append(instanceName).append("]").toString();
436436
}
437437
return toString;
438-
}
438+
}
439+
440+
441+
@Override
442+
public int hashCode() {
443+
final int prime = 31;
444+
int result = 1;
445+
result = prime * result + (int) (id ^ (id >>> 32));
446+
return result;
447+
}
448+
449+
@Override
450+
public boolean equals(Object obj) {
451+
if (this == obj)
452+
return true;
453+
if (obj == null)
454+
return false;
455+
if (getClass() != obj.getClass())
456+
return false;
457+
VMInstanceVO other = (VMInstanceVO) obj;
458+
if (id != other.id)
459+
return false;
460+
return true;
461+
}
462+
439463
}

0 commit comments

Comments
 (0)