Skip to content

Commit 61ea35c

Browse files
weizhouapachedhslove
authored andcommitted
HA: set correct hostId of HA work for vm migration (apache#10591)
1 parent a554edf commit 61ea35c

3 files changed

Lines changed: 24 additions & 4 deletions

File tree

engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5253,10 +5253,9 @@ public Outcome<VirtualMachine> migrateVmAwayThroughJobQueue(final String vmUuid,
52535253
workJob = newVmWorkJobAndInfo.first();
52545254
VmWorkMigrateAway workInfo = new VmWorkMigrateAway(newVmWorkJobAndInfo.second(), srcHostId);
52555255

5256-
workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo));
5256+
setCmdInfoAndSubmitAsyncJob(workJob, workInfo, vmId);
52575257
}
52585258

5259-
_jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vmId);
52605259

52615260
AsyncJobExecutionContext.getCurrentExecutionContext().joinJob(workJob.getId());
52625261

engine/schema/src/main/java/com/cloud/vm/VMInstanceVO.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ public void setRemoved(Date removed) {
506506

507507
@Override
508508
public String toString() {
509-
return String.format("VM instance %s", ReflectionToStringBuilderUtils.reflectOnlySelectedFields(this, "id", "instanceName", "uuid", "type"));
509+
return String.format("VM instance %s", ReflectionToStringBuilderUtils.reflectOnlySelectedFields(this, "id", "instanceName", "uuid", "type", "state"));
510510
}
511511

512512
@Override

server/src/main/java/com/cloud/ha/HighAvailabilityManagerImpl.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,6 @@ public boolean scheduleMigration(final VMInstanceVO vm, ReasonType reasonType) {
388388
if (vm.getHostId() == null) {
389389
return false;
390390
}
391-
392391
if (!VmHaEnabled.valueIn(vm.getDataCenterId())) {
393392
String message = String.format("Unable to schedule migration for the VM %s on host %s, VM high availability manager is disabled.", vm, _hostDao.findById(vm.getHostId()));
394393
if (logger.isDebugEnabled()) {
@@ -398,6 +397,7 @@ public boolean scheduleMigration(final VMInstanceVO vm, ReasonType reasonType) {
398397
return false;
399398
}
400399

400+
Long hostId = VirtualMachine.State.Migrating.equals(vm.getState()) ? vm.getLastHostId() : vm.getHostId();
401401
final HaWorkVO work = new HaWorkVO(vm.getId(), vm.getType(), WorkType.Migration, Step.Scheduled, vm.getHostId(), vm.getState(), 0, vm.getUpdated(), reasonType);
402402
_haDao.persist(work);
403403
logger.info("Scheduled migration work of VM {} from host {} with HAWork {}", vm, _hostDao.findById(vm.getHostId()), work);
@@ -813,6 +813,18 @@ public Long migrate(final HaWorkVO work) {
813813
return null;
814814
}
815815
logger.info("Migration attempt: for VM {}from host {}. Starting attempt: {}/{} times.", vm, srcHost, 1 + work.getTimesTried(), _maxRetries);
816+
817+
if (VirtualMachine.State.Stopped.equals(vm.getState())) {
818+
logger.info(String.format("vm %s is Stopped, skipping migrate.", vm));
819+
return null;
820+
}
821+
if (VirtualMachine.State.Running.equals(vm.getState()) && srcHostId != vm.getHostId()) {
822+
logger.info(String.format("VM %s is running on a different host %s, skipping migration", vm, vm.getHostId()));
823+
return null;
824+
}
825+
logger.info("Migration attempt: for VM " + vm.getUuid() + "from host id " + srcHostId +
826+
". Starting attempt: " + (1 + work.getTimesTried()) + "/" + _maxRetries + " times.");
827+
816828
try {
817829
work.setStep(Step.Migrating);
818830
_haDao.update(work.getId(), work);
@@ -1148,6 +1160,15 @@ public WorkerThread(String name) {
11481160
@Override
11491161
public void run() {
11501162
logger.info("Starting work");
1163+
try {
1164+
synchronized (this) {
1165+
wait(_timeToSleep);
1166+
}
1167+
} catch (final InterruptedException e) {
1168+
s_logger.info("Interrupted");
1169+
}
1170+
logger.info("Starting work");
1171+
11511172
while (!_stopped) {
11521173
_managedContext.runWithContext(new Runnable() {
11531174
@Override

0 commit comments

Comments
 (0)