From 77a7f38d9674111660cfeee29599fe577dbad704 Mon Sep 17 00:00:00 2001 From: Brian Hellman <1276518+brhellman@users.noreply.github.com> Date: Tue, 1 Dec 2026 22:38:27 -0500 Subject: [PATCH] agent: reconnect when every management server is in the avoid list An agent whose only configured management server is in its avoid list never tries to reconnect: Agent.reconnect() skips every host in the avoid list, sleeps and picks the same host again, forever, without logging anything (#10915). The avoid list comes with every ping answer from ManagementServerHostDaoImpl.listNonUpStateMsIPs(), the service IPs of all management servers that are not Up. A management server that is not Up can share its service IP with one that is Up, for example after a management server was re-installed with a new msid on the same address, or when management servers share an address. The agents then avoid the management server they are connected to and do not come back after the next disconnect. - listNonUpStateMsIPs() no longer returns an address an Up management server uses. - When every configured management server is in the avoid list, the agent ignores the avoid list for that reconnect, and it logs a host it skips because it is in the avoid list. Fixes #10915 Co-Authored-By: Claude Opus 5.5 --- .../src/main/java/com/cloud/agent/Agent.java | 22 ++++- .../test/java/com/cloud/agent/AgentTest.java | 24 ++++++ .../dao/ManagementServerHostDaoImpl.java | 17 +++- .../dao/ManagementServerHostDaoImplTest.java | 81 +++++++++++++++++++ 4 files changed, 142 insertions(+), 2 deletions(-) create mode 100644 framework/cluster/src/test/java/com/cloud/cluster/dao/ManagementServerHostDaoImplTest.java diff --git a/agent/src/main/java/com/cloud/agent/Agent.java b/agent/src/main/java/com/cloud/agent/Agent.java index 52b1fe392e8d..9180ccb7963b 100644 --- a/agent/src/main/java/com/cloud/agent/Agent.java +++ b/agent/src/main/java/com/cloud/agent/Agent.java @@ -28,6 +28,7 @@ import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -644,7 +645,7 @@ protected void reconnect(final Link link, String preferredMSHost, boolean forTra if (org.apache.commons.lang3.StringUtils.isBlank(host)) { host = shell.getNextHost(); } - List avoidMSHostList = shell.getAvoidHosts(); + List avoidMSHostList = getAvoidMsHostsForReconnect(); do { if (CollectionUtils.isEmpty(avoidMSHostList) || !avoidMSHostList.contains(host)) { connection = new NioClient(getAgentName(), host, shell.getPort(), shell.getWorkers(), shell.getSslHandshakeTimeout(), this); @@ -655,6 +656,8 @@ protected void reconnect(final Link link, String preferredMSHost, boolean forTra logger.info("Attempted to re-connect to the server, but received an unexpected exception, trying again...", e); stopAndCleanupConnection(false); } + } else { + logger.info("Not reconnecting to host: {}, it is in the avoid list {}", host, avoidMSHostList); } shell.getBackoffAlgorithm().waitBeforeRetry(); host = shell.getNextHost(); @@ -663,6 +666,23 @@ protected void reconnect(final Link link, String preferredMSHost, boolean forTra logger.info("Connected to the host: {}", shell.getConnectedHost()); } + /** + * The management servers to skip while reconnecting. When every configured management server is in the + * avoid list, skipping them would mean never trying any of them again, so the avoid list is not used then. + */ + protected List getAvoidMsHostsForReconnect() { + List avoidMSHostList = shell.getAvoidHosts(); + if (CollectionUtils.isEmpty(avoidMSHostList)) { + return avoidMSHostList; + } + List configuredHosts = Arrays.asList(shell.getHosts()); + if (avoidMSHostList.containsAll(configuredHosts)) { + logger.warn("All configured management servers {} are in the avoid list {}, ignoring the avoid list to reconnect", configuredHosts, avoidMSHostList); + return Collections.emptyList(); + } + return avoidMSHostList; + } + protected void closeAndTerminateLink(final Link link) { if (link == null) { return; diff --git a/agent/src/test/java/com/cloud/agent/AgentTest.java b/agent/src/test/java/com/cloud/agent/AgentTest.java index 65dc030ebd76..b671c7507a07 100644 --- a/agent/src/test/java/com/cloud/agent/AgentTest.java +++ b/agent/src/test/java/com/cloud/agent/AgentTest.java @@ -19,6 +19,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.any; @@ -32,6 +33,7 @@ import java.io.IOException; import java.net.InetSocketAddress; +import java.util.List; import javax.naming.ConfigurationException; @@ -254,4 +256,26 @@ public void testStopAndCleanupConnectionValidConnectionWaitForStopWaitsForStartu verify(mockConnection).cleanUp(); verify(mockBackoff, times(3)).waitBeforeRetry(); } + + @Test + public void testGetAvoidMsHostsForReconnectIgnoresAvoidListWhenAllHostsAreInIt() { + when(shell.getAvoidHosts()).thenReturn(List.of("10.1.1.1")); + when(shell.getHosts()).thenReturn(new String[] {"10.1.1.1"}); + List result = agent.getAvoidMsHostsForReconnect(); + assertTrue(result.isEmpty()); + verify(logger).warn(eq("All configured management servers {} are in the avoid list {}, ignoring the avoid list to reconnect"), eq(List.of("10.1.1.1")), eq(List.of("10.1.1.1"))); + } + + @Test + public void testGetAvoidMsHostsForReconnectKeepsAvoidListWhenAnotherHostIsAvailable() { + when(shell.getAvoidHosts()).thenReturn(List.of("10.1.1.1")); + when(shell.getHosts()).thenReturn(new String[] {"10.1.1.1", "10.1.1.2"}); + assertEquals(List.of("10.1.1.1"), agent.getAvoidMsHostsForReconnect()); + } + + @Test + public void testGetAvoidMsHostsForReconnectWithoutAvoidList() { + when(shell.getAvoidHosts()).thenReturn(null); + assertNull(agent.getAvoidMsHostsForReconnect()); + } } diff --git a/framework/cluster/src/main/java/com/cloud/cluster/dao/ManagementServerHostDaoImpl.java b/framework/cluster/src/main/java/com/cloud/cluster/dao/ManagementServerHostDaoImpl.java index ec943a9c26be..5ea1a9cdeb13 100644 --- a/framework/cluster/src/main/java/com/cloud/cluster/dao/ManagementServerHostDaoImpl.java +++ b/framework/cluster/src/main/java/com/cloud/cluster/dao/ManagementServerHostDaoImpl.java @@ -48,6 +48,7 @@ public class ManagementServerHostDaoImpl extends GenericDaoBase InactiveSearch; private final SearchBuilder StateSearch; protected GenericSearchBuilder NonUpStateMsSearch; + protected GenericSearchBuilder UpStateMsIpSearch; @Override public void invalidateRunSession(long id, long runid) { @@ -234,6 +235,11 @@ protected ManagementServerHostDaoImpl() { NonUpStateMsSearch.selectFields(NonUpStateMsSearch.entity().getServiceIP()); NonUpStateMsSearch.and("state", NonUpStateMsSearch.entity().getState(), SearchCriteria.Op.NLIKE); NonUpStateMsSearch.done(); + + UpStateMsIpSearch = createSearchBuilder(String.class); + UpStateMsIpSearch.selectFields(UpStateMsIpSearch.entity().getServiceIP()); + UpStateMsIpSearch.and("state", UpStateMsIpSearch.entity().getState(), SearchCriteria.Op.EQ); + UpStateMsIpSearch.done(); } @Override @@ -271,7 +277,16 @@ public List listBy(ManagementServerHost.State... states) public List listNonUpStateMsIPs() { SearchCriteria sc = NonUpStateMsSearch.create(); sc.addAnd("state", SearchCriteria.Op.NLIKE, State.Up); - return customSearch(sc, null); + List nonUpMsIps = new ArrayList<>(customSearch(sc, null)); + if (nonUpMsIps.isEmpty()) { + return nonUpMsIps; + } + // A management server that is not Up can have the same service IP as one that is Up (for example a + // management server re-installed with a new msid on the same address); agents must not avoid that address. + SearchCriteria upSc = UpStateMsIpSearch.create(); + upSc.setParameters("state", State.Up); + nonUpMsIps.removeAll(customSearch(upSc, null)); + return nonUpMsIps; } @Override diff --git a/framework/cluster/src/test/java/com/cloud/cluster/dao/ManagementServerHostDaoImplTest.java b/framework/cluster/src/test/java/com/cloud/cluster/dao/ManagementServerHostDaoImplTest.java new file mode 100644 index 000000000000..295816b36341 --- /dev/null +++ b/framework/cluster/src/test/java/com/cloud/cluster/dao/ManagementServerHostDaoImplTest.java @@ -0,0 +1,81 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +package com.cloud.cluster.dao; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.Collections; +import java.util.List; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Spy; +import org.mockito.junit.MockitoJUnitRunner; + +import com.cloud.cluster.ManagementServerHostVO; +import com.cloud.utils.db.GenericSearchBuilder; +import com.cloud.utils.db.SearchCriteria; + +@RunWith(MockitoJUnitRunner.class) +public class ManagementServerHostDaoImplTest { + @Spy + ManagementServerHostDaoImpl dao = new ManagementServerHostDaoImpl(); + + private SearchCriteria nonUpSc; + private SearchCriteria upSc; + + @Before + @SuppressWarnings("unchecked") + public void setUp() { + GenericSearchBuilder nonUpSearch = mock(GenericSearchBuilder.class); + GenericSearchBuilder upSearch = mock(GenericSearchBuilder.class); + nonUpSc = mock(SearchCriteria.class); + upSc = mock(SearchCriteria.class); + when(nonUpSearch.create()).thenReturn(nonUpSc); + when(upSearch.create()).thenReturn(upSc); + dao.NonUpStateMsSearch = nonUpSearch; + dao.UpStateMsIpSearch = upSearch; + } + + @Test + public void listNonUpStateMsIPsExcludesAddressesOfUpManagementServers() { + doReturn(List.of("10.1.1.1", "10.1.1.2")).when(dao).customSearch(nonUpSc, null); + doReturn(List.of("10.1.1.1")).when(dao).customSearch(upSc, null); + assertEquals(List.of("10.1.1.2"), dao.listNonUpStateMsIPs()); + } + + @Test + public void listNonUpStateMsIPsKeepsAddressesNoUpManagementServerUses() { + doReturn(List.of("10.1.1.2")).when(dao).customSearch(nonUpSc, null); + doReturn(List.of("10.1.1.1")).when(dao).customSearch(upSc, null); + assertEquals(List.of("10.1.1.2"), dao.listNonUpStateMsIPs()); + } + + @Test + public void listNonUpStateMsIPsDoesNotQueryUpServersWhenAllAreUp() { + doReturn(Collections.emptyList()).when(dao).customSearch(nonUpSc, null); + assertTrue(dao.listNonUpStateMsIPs().isEmpty()); + verify(dao, never()).customSearch(upSc, null); + } +}