Skip to content

Commit c64d1fe

Browse files
mprokopchukPearl Dsilva
authored andcommitted
Handle multiple active entries for same hostname in k8s setup
1 parent 855f611 commit c64d1fe

4 files changed

Lines changed: 18 additions & 32 deletions

File tree

framework/cluster/src/main/java/com/cloud/cluster/ClusterManagerImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,8 +1050,8 @@ public ManagementServerHostVO doInTransaction(final TransactionStatus status) {
10501050

10511051
// Look for duplicate hostname in the case of kubernetes setup where ip/mac changes but hostname is constant.
10521052
if (mshost == null && StringUtils.isNotBlank(currentHostname)) {
1053-
ManagementServerHostVO activeEntry = _mshostDao.findByName(currentHostname);
1054-
if (activeEntry != null) {
1053+
List<ManagementServerHostVO> activeEntries = _mshostDao.findAllByName(currentHostname);
1054+
for (ManagementServerHostVO activeEntry : activeEntries) {
10551055
// Found an active entry with this hostname but different MSID
10561056
// This happens when a pod restarts with a new MAC address (new MSID)
10571057
if (activeEntry.getMsid() != _msId) {

framework/cluster/src/main/java/com/cloud/cluster/dao/ManagementServerHostDao.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public interface ManagementServerHostDao extends GenericDao<ManagementServerHost
3131

3232
ManagementServerHostVO findByMsid(long msid);
3333

34-
ManagementServerHostVO findByName(String name);
34+
List<ManagementServerHostVO> findAllByName(String name);
3535

3636
int increaseAlertCount(long id);
3737

framework/cluster/src/main/java/com/cloud/cluster/dao/ManagementServerHostDaoImpl.java

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727

2828
import org.apache.commons.collections.CollectionUtils;
29+
import org.apache.commons.lang3.StringUtils;
2930

3031
import com.cloud.cluster.ClusterInvalidSessionException;
3132
import org.apache.cloudstack.management.ManagementServerHost;
@@ -77,29 +78,13 @@ public ManagementServerHostVO findByMsid(long msid) {
7778
}
7879

7980
@Override
80-
public ManagementServerHostVO findByName(String name) {
81-
if (name == null || name.trim().isEmpty()) {
82-
return null;
81+
public List<ManagementServerHostVO> findAllByName(String name) {
82+
if (StringUtils.isBlank(name)) {
83+
return List.of();
8384
}
84-
8585
SearchCriteria<ManagementServerHostVO> sc = NameSearch.create();
8686
sc.setParameters("name", name);
87-
// Only search for active (non-removed) entries to avoid non-deterministic results
88-
// when multiple removed entries exist for the same hostname
89-
sc.addAnd("removed", SearchCriteria.Op.NULL);
90-
91-
List<ManagementServerHostVO> l = listBy(sc);
92-
if (l != null && l.size() > 0) {
93-
if (l.size() > 1) {
94-
s_logger.error(String.format(
95-
"Data corruption detected: Found %d active entries for hostname '%s'. " +
96-
"This should never happen and indicates duplicate mshost records.",
97-
l.size(), name));
98-
}
99-
return l.get(0);
100-
}
101-
102-
return null;
87+
return listBy(sc);
10388
}
10489

10590
@Override

framework/cluster/src/test/java/com/cloud/cluster/dao/ManagementServerHostDaoImplTest.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
import org.junit.runner.RunWith;
2323
import org.mockito.junit.MockitoJUnitRunner;
2424

25-
import static org.junit.Assert.assertNull;
25+
import java.util.List;
26+
import static org.junit.Assert.assertEquals;
2627

2728
/**
2829
* Unit tests for ManagementServerHostDaoImpl focusing on the new findByName method added in PR #641.
@@ -39,26 +40,26 @@ public class ManagementServerHostDaoImplTest {
3940

4041
@Test
4142
public void testFindByName_ReturnsNullForNullHostname() {
42-
ManagementServerHostVO result = dao.findByName(null);
43-
assertNull("Result should be null for null hostname", result);
43+
List<ManagementServerHostVO> result = dao.findAllByName(null);
44+
assertEquals(0, result.size());
4445
}
4546

4647
@Test
4748
public void testFindByName_ReturnsNullForEmptyHostname() {
48-
ManagementServerHostVO result = dao.findByName("");
49-
assertNull("Result should be null for empty hostname", result);
49+
List<ManagementServerHostVO> result = dao.findAllByName("");
50+
assertEquals(0, result.size());
5051
}
5152

5253
@Test
5354
public void testFindByName_ReturnsNullForWhitespaceHostname() {
54-
ManagementServerHostVO result = dao.findByName(" ");
55-
assertNull("Result should be null for whitespace-only hostname", result);
55+
List<ManagementServerHostVO> result = dao.findAllByName(" ");
56+
assertEquals(0, result.size());
5657
}
5758

5859
@Test
5960
public void testFindByName_ReturnsNullForTabAndSpaceHostname() {
60-
ManagementServerHostVO result = dao.findByName(" \t ");
61-
assertNull("Result should be null for tab/space-only hostname", result);
61+
List<ManagementServerHostVO> result = dao.findAllByName(" \t ");
62+
assertEquals(0, result.size());
6263
}
6364

6465
/**

0 commit comments

Comments
 (0)