Skip to content

Commit b7c970f

Browse files
authored
Fix issue with multiple KVM Host entries in host table (#12589)
1 parent 4de8c2b commit b7c970f

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

engine/components-api/src/main/java/com/cloud/resource/ResourceManager.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ public interface ResourceManager extends ResourceService, Configurable {
154154

155155
public HostVO findHostByGuid(String guid);
156156

157+
HostVO findHostByGuidPrefix(String guid);
158+
157159
public HostVO findHostByName(String name);
158160

159161
HostStats getHostStatistics(Host host);

server/src/main/java/com/cloud/resource/ResourceManagerImpl.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2261,15 +2261,26 @@ private boolean checkCIDR(final HostPodVO pod, final String serverPrivateIP, fin
22612261
private HostVO getNewHost(StartupCommand[] startupCommands) {
22622262
StartupCommand startupCommand = startupCommands[0];
22632263

2264-
HostVO host = findHostByGuid(startupCommand.getGuid());
2264+
String fullGuid = startupCommand.getGuid();
2265+
logger.debug(String.format("Trying to find Host by guid %s", fullGuid));
2266+
HostVO host = findHostByGuid(fullGuid);
22652267

22662268
if (host != null) {
2269+
logger.debug(String.format("Found Host by guid %s: %s", fullGuid, host));
22672270
return host;
22682271
}
22692272

2270-
host = findHostByGuid(startupCommand.getGuidWithoutResource());
2273+
String guidPrefix = startupCommand.getGuidWithoutResource();
2274+
logger.debug(String.format("Trying to find Host by guid prefix %s", guidPrefix));
2275+
host = findHostByGuidPrefix(guidPrefix);
22712276

2272-
return host; // even when host == null!
2277+
if (host != null) {
2278+
logger.debug(String.format("Found Host by guid prefix %s: %s", guidPrefix, host));
2279+
return host;
2280+
}
2281+
2282+
logger.debug(String.format("Could not find Host by guid %s", fullGuid));
2283+
return null;
22732284
}
22742285

22752286
protected HostVO createHostVO(final StartupCommand[] cmds, final ServerResource resource, final Map<String, String> details, List<String> hostTags,
@@ -3296,13 +3307,23 @@ public List<HypervisorType> listAvailHypervisorInZone(final Long zoneId) {
32963307
public HostVO findHostByGuid(final String guid) {
32973308
final QueryBuilder<HostVO> sc = QueryBuilder.create(HostVO.class);
32983309
sc.and(sc.entity().getGuid(), Op.EQ, guid);
3310+
sc.and(sc.entity().getRemoved(), Op.NULL);
3311+
return sc.find();
3312+
}
3313+
3314+
@Override
3315+
public HostVO findHostByGuidPrefix(String guid) {
3316+
final QueryBuilder<HostVO> sc = QueryBuilder.create(HostVO.class);
3317+
sc.and(sc.entity().getGuid(), Op.LIKE, guid + "%");
3318+
sc.and(sc.entity().getRemoved(), Op.NULL);
32993319
return sc.find();
33003320
}
33013321

33023322
@Override
33033323
public HostVO findHostByName(final String name) {
33043324
final QueryBuilder<HostVO> sc = QueryBuilder.create(HostVO.class);
33053325
sc.and(sc.entity().getName(), Op.EQ, name);
3326+
sc.and(sc.entity().getRemoved(), Op.NULL);
33063327
return sc.find();
33073328
}
33083329

server/src/test/java/com/cloud/resource/MockResourceManagerImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,11 @@ public HostVO findHostByGuid(final String guid) {
460460
return null;
461461
}
462462

463+
@Override
464+
public HostVO findHostByGuidPrefix(String guid) {
465+
return null;
466+
}
467+
463468
/* (non-Javadoc)
464469
* @see com.cloud.resource.ResourceManager#findHostByName(java.lang.String)
465470
*/

0 commit comments

Comments
 (0)