Skip to content

ssvm: prevent sql constraint violation on SVM removal during net outage - #14438

Open
DaanHoogland wants to merge 1 commit into
4.20from
ghi14391-SQLIntegrityConstraintViolation
Open

DaanHoogland wants to merge 1 commit into
4.20from
ghi14391-SQLIntegrityConstraintViolation

Conversation

@DaanHoogland

Copy link
Copy Markdown
Contributor

Description

This PR...

Fixes: #14391

Types of changes

  • Breaking change (fix or feature that would cause existing functionality to change)
  • New feature (non-breaking change which adds functionality)
  • Bug fix (non-breaking change which fixes an issue)
  • Enhancement (improves an existing feature and functionality)
  • Cleanup (Code refactoring and cleanup, that may add test cases)
  • Build/CI
  • Test (unit or integration test code)

Feature/Enhancement Scale or Bug Severity

Feature/Enhancement Scale

  • Major
  • Minor

Bug Severity

  • BLOCKER
  • Critical
  • Major
  • Minor
  • Trivial

Screenshots (if appropriate):

How Has This Been Tested?

How did you try to break this feature and the system with this change?

@DaanHoogland
DaanHoogland requested review from vishesh92 and a balanced review from Copilot October 19, 2026 13:41
@codecov

codecov Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 57.89474% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 16.41%. Comparing base (376c1b4) to head (edc2a39).

Files with missing lines Patch % Lines
.../src/main/java/com/cloud/host/dao/HostDaoImpl.java 0.00% 4 Missing ⚠️
...ne/schema/src/main/java/com/cloud/host/HostVO.java 0.00% 3 Missing ⚠️
...n/java/com/cloud/resource/ResourceManagerImpl.java 91.66% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff            @@
##               4.20   #14438   +/-   ##
=========================================
  Coverage     16.41%   16.41%           
- Complexity    13657    13662    +5     
=========================================
  Files          5669     5669           
  Lines        501628   501647   +19     
  Branches      60942    60944    +2     
=========================================
+ Hits          82336    82365   +29     
+ Misses       410064   410053   -11     
- Partials       9228     9229    +1     
Flag Coverage Δ
uitests 4.16% <ø> (ø)
unittests 17.28% <57.89%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copilot AI left a comment

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.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Copilot review overview

4 open issues
What changed in this PR

Prevents SQL unique-constraint violations when a previously removed System VM/host reconnects by reviving the removed host row (instead of attempting to create a duplicate) and adds coverage for the new behavior.

Changes:

  • Revive an existing removed HostVO with the same GUID during host discovery.
  • Add DAO support to find a host by GUID including removed records.
  • Add unit tests covering the “revive removed host on GUID collision” path.
File Description
server/​src/​main/​java/​com/​cloud/​resource/​ResourceManagerImpl.java Revives removed host records by GUID during getNewHost to avoid duplicate insert/constraint violations.
engine/​schema/​src/​main/​java/​com/​cloud/​host/​dao/​HostDao.java Adds a new DAO API to find a host by GUID including removed rows.
engine/​schema/​src/​main/​java/​com/​cloud/​host/​dao/​HostDaoImpl.java Implements findByGuidIncludingRemoved.
engine/​schema/​src/​main/​java/​com/​cloud/​host/​HostVO.java Adds a setter for the removed field to support “revival.”
server/​src/​test/​java/​com/​cloud/​resource/​ResourceManagerImplTest.java Adds unit tests (via reflection) for the revival/null-return behavior.

Review effort: Lite

}
logger.warn(String.format("Host with guid %s reconnected but was previously removed (id: %s); reviving the removed host record instead of creating a duplicate",
guid, removedHost.getId()));
removedHost.setRemoved(null);
Comment on lines +2293 to +2298
/**
* A host can be marked removed (e.g. a system VM force-stopped and destroyed from the UI) while the
* underlying resource is still alive and later reconnects with the same guid. The `guid` column is
* uniquely constrained regardless of removal, so blindly inserting a new row for it fails with a
* SQLIntegrityConstraintViolationException. Revive the removed row instead of creating a duplicate.
*/
Comment on lines +2300 to +2307
HostVO removedHost = _hostDao.findByGuidIncludingRemoved(guid);
if (removedHost == null || removedHost.getRemoved() == null) {
return null;
}
logger.warn(String.format("Host with guid %s reconnected but was previously removed (id: %s); reviving the removed host record instead of creating a duplicate",
guid, removedHost.getId()));
removedHost.setRemoved(null);
return removedHost;
Comment on lines +688 to +732

private HostVO invokeGetNewHost(StartupCommand[] cmds) throws Exception {
Method method = ResourceManagerImpl.class.getDeclaredMethod("getNewHost", StartupCommand[].class);
method.setAccessible(true);
return (HostVO) method.invoke(resourceManager, (Object) cmds);
}

@Test
public void testGetNewHostRevivesRemovedHostOnGuidCollision() throws Exception {
String guid = "s-14-VM-PremiumSecondaryStorageResource";
String guidPrefix = "s-14-VM";
StartupCommand cmd = Mockito.mock(StartupCommand.class);
when(cmd.getGuid()).thenReturn(guid);
when(cmd.getGuidWithoutResource()).thenReturn(guidPrefix);

Mockito.doReturn(null).when(resourceManager).findHostByGuid(guid);
Mockito.doReturn(null).when(resourceManager).findHostByGuidPrefix(guidPrefix);

HostVO removedHost = Mockito.mock(HostVO.class);
when(removedHost.getId()).thenReturn(99L);
when(removedHost.getRemoved()).thenReturn(new Date());
when(hostDao.findByGuidIncludingRemoved(guid)).thenReturn(removedHost);

HostVO result = invokeGetNewHost(new StartupCommand[]{cmd});

Assert.assertSame(removedHost, result);
Mockito.verify(removedHost).setRemoved(null);
}

@Test
public void testGetNewHostReturnsNullWhenNoHostAndNoRemovedMatch() throws Exception {
String guid = "unknown-guid";
String guidPrefix = "unknown";
StartupCommand cmd = Mockito.mock(StartupCommand.class);
when(cmd.getGuid()).thenReturn(guid);
when(cmd.getGuidWithoutResource()).thenReturn(guidPrefix);

Mockito.doReturn(null).when(resourceManager).findHostByGuid(guid);
Mockito.doReturn(null).when(resourceManager).findHostByGuidPrefix(guidPrefix);
when(hostDao.findByGuidIncludingRemoved(guid)).thenReturn(null);

HostVO result = invokeGetNewHost(new StartupCommand[]{cmd});

Assert.assertNull(result);
}
@sonarqubecloud

Copy link
Copy Markdown

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SQLIntegrityConstraintViolationException for SystemVMs after a network disruption

2 participants