Skip to content
Open
Show file tree
Hide file tree
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 @@ -3559,6 +3559,12 @@ private Volume orchestrateDetachVolumeFromVM(long vmId, long volumeId) {
Volume volume = _volsDao.findById(volumeId);
VMInstanceVO vm = _vmInstanceDao.findById(vmId);

// A queued detach can run after the volume was already detached and attached elsewhere; leave that attachment alone
if (volume.getInstanceId() == null || volume.getInstanceId() != vmId) {
logger.warn("Volume {} is no longer attached to VM {}, skipping detach", volume, vm);
return volume;
}

Comment on lines +3562 to +3567

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does what it should, just one cleanup suggestion; add a shouldDetachBeSkipped(Volume voume, VMInstanceVO vm) and make the comment a javadoc. Then return the volume on true.

String errorMsg = "Failed to detach volume " + volume.getName() + " from VM " + vm.getHostName();
boolean sendCommand = vm.getState() == State.Running;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,30 @@ public void testDetachVolumeFromStoppedXenVm() throws NoSuchFieldException, Ille
volumeApiServiceImpl.detachVolumeFromVM(detachCmd);
}

@Test
public void testDetachVolumeSkippedWhenAttachedToAnotherVm() {
VolumeVO volume = Mockito.mock(VolumeVO.class);
Mockito.when(volume.getInstanceId()).thenReturn(200L);
Mockito.when(volumeDaoMock.findById(100L)).thenReturn(volume);

Volume result = volumeApiServiceImpl.detachVolumeViaDestroyVM(300L, 100L);

Assert.assertEquals(volume, result);
Mockito.verify(volumeDaoMock, Mockito.never()).detachVolume(Mockito.anyLong());
}

@Test
public void testDetachVolumeSkippedWhenAlreadyDetached() {
VolumeVO volume = Mockito.mock(VolumeVO.class);
Mockito.when(volume.getInstanceId()).thenReturn(null);
Mockito.when(volumeDaoMock.findById(100L)).thenReturn(volume);

Volume result = volumeApiServiceImpl.detachVolumeViaDestroyVM(300L, 100L);

Assert.assertEquals(volume, result);
Mockito.verify(volumeDaoMock, Mockito.never()).detachVolume(Mockito.anyLong());
}

/**
* TESTS FOR ATTACH ROOT VOLUME, COUNT=7
*/
Expand Down
Loading