Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: vnic hot un\plug - avoid race #419

Merged
merged 2 commits into from
Jul 21, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import javax.inject.Inject;

Expand Down Expand Up @@ -54,9 +56,12 @@
import org.ovirt.engine.core.dao.provider.ProviderDao;
import org.ovirt.engine.core.utils.ReplacementUtils;
import org.ovirt.engine.core.utils.StringMapUtils;
import org.ovirt.engine.core.vdsbroker.ResourceManager;
import org.ovirt.engine.core.vdsbroker.monitoring.VmDevicesMonitoring;
import org.ovirt.engine.core.vdsbroker.vdsbroker.VdsProperties;
import org.ovirt.engine.core.vdsbroker.vdsbroker.VmInfoReturn;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Activate or deactivate a virtual network interface of a VM in case it is in a valid status. If the VM is down, simply
Expand All @@ -65,6 +70,7 @@
@NonTransactiveCommandAttribute
public class ActivateDeactivateVmNicCommand<T extends ActivateDeactivateVmNicParameters> extends VmCommand<T> {

private static final Logger log = LoggerFactory.getLogger(ActivateDeactivateVmNicCommand.class);
private static List<VMStatus> ALLOWED_VM_STATES =
Arrays.asList(VMStatus.Up, VMStatus.Down, VMStatus.ImageLocked, VMStatus.PoweringDown, VMStatus.PoweringUp);

Expand Down Expand Up @@ -118,6 +124,9 @@ public class ActivateDeactivateVmNicCommand<T extends ActivateDeactivateVmNicPar
@Inject
private NetworkDao networkDao;

@Inject
private ResourceManager resourceManager;

public ActivateDeactivateVmNicCommand(T parameters, CommandContext commandContext) {
super(parameters, commandContext);
setVmId(parameters.getVmId());
Expand Down Expand Up @@ -248,21 +257,46 @@ public String getInterfaceType() {

@Override
protected void executeVmCommand() {
switch (getParameters().getAction()) {
case PLUG:
plugNic();
break;
case UNPLUG:
unplugNic();
break;
default:
throw new RuntimeException("Coding error: unknown enum value");
boolean hotPlug = getVm().getStatus().isUpOrPaused();
Lock vmDevicesLock = getVmDevicesLock(hotPlug, getParameters().getAction());
vmDevicesLock.lock();
try {
switch (getParameters().getAction()) {
case PLUG:
plugNic();
break;
case UNPLUG:
unplugNic();
break;
default:
throw new RuntimeException("Coding error: unknown enum value");
}

var success = handleFailoverIfNeeded();
// In any case, the device is updated
updateDevice();
setSucceeded(success);
} finally {
vmDevicesLock.unlock();
log.debug("Released lock for {}", getParameters().getAction());
}
}

var success = handleFailoverIfNeeded();
// In any case, the device is updated
updateDevice();
setSucceeded(success);
protected Lock getVmDevicesLock(boolean runningVmChanges, PlugAction action) {
if (!runningVmChanges) {
// dummy lock
log.debug("Acquired dummy lock for {}", action);
return new ReentrantLock() {
@Override
public void lock() {
}
@Override
public void unlock() {
}
};
}
log.debug("Acquired lock for {}", action);
return resourceManager.getVmManager(getVmId()).getVmDevicesLock();
}

private void plugNic() {
Expand Down Expand Up @@ -439,15 +473,8 @@ private NetworkProviderProxy getProviderProxy() {
}

private void updateDevice() {
if (getParameters().getAction() == PlugAction.PLUG
&& hotPlugVmNicRequired(getVm().getStatus())) {
VmDevicesMonitoring.Change change = vmDevicesMonitoring.createChange(getVdsId(), System.nanoTime());
change.updateVm(getVmId(), VmDevicesMonitoring.UPDATE_HASH);
change.flush();
} else {
vmDevice.setPlugged(getParameters().getAction() == PlugAction.PLUG);
vmDeviceDao.update(vmDevice);
}
vmDevice.setPlugged(getParameters().getAction() == PlugAction.PLUG);
vmDeviceDao.update(vmDevice);
}

private boolean handleFailoverIfNeeded() {
Expand Down