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
22 changes: 21 additions & 1 deletion agent/src/main/java/com/cloud/agent/Agent.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> avoidMSHostList = shell.getAvoidHosts();
List<String> avoidMSHostList = getAvoidMsHostsForReconnect();
do {
if (CollectionUtils.isEmpty(avoidMSHostList) || !avoidMSHostList.contains(host)) {
connection = new NioClient(getAgentName(), host, shell.getPort(), shell.getWorkers(), shell.getSslHandshakeTimeout(), this);
Expand All @@ -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();
Expand All @@ -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<String> getAvoidMsHostsForReconnect() {
List<String> avoidMSHostList = shell.getAvoidHosts();
if (CollectionUtils.isEmpty(avoidMSHostList)) {
return avoidMSHostList;
}
List<String> 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;
Expand Down
24 changes: 24 additions & 0 deletions agent/src/test/java/com/cloud/agent/AgentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,6 +33,7 @@

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.List;

import javax.naming.ConfigurationException;

Expand Down Expand Up @@ -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<String> 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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class ManagementServerHostDaoImpl extends GenericDaoBase<ManagementServer
private final SearchBuilder<ManagementServerHostVO> InactiveSearch;
private final SearchBuilder<ManagementServerHostVO> StateSearch;
protected GenericSearchBuilder<ManagementServerHostVO, String> NonUpStateMsSearch;
protected GenericSearchBuilder<ManagementServerHostVO, String> UpStateMsIpSearch;

@Override
public void invalidateRunSession(long id, long runid) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -271,7 +277,16 @@ public List<ManagementServerHostVO> listBy(ManagementServerHost.State... states)
public List<String> listNonUpStateMsIPs() {
SearchCriteria<String> sc = NonUpStateMsSearch.create();
sc.addAnd("state", SearchCriteria.Op.NLIKE, State.Up);
return customSearch(sc, null);
List<String> 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<String> upSc = UpStateMsIpSearch.create();
upSc.setParameters("state", State.Up);
nonUpMsIps.removeAll(customSearch(upSc, null));
return nonUpMsIps;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> nonUpSc;
private SearchCriteria<String> upSc;

@Before
@SuppressWarnings("unchecked")
public void setUp() {
GenericSearchBuilder<ManagementServerHostVO, String> nonUpSearch = mock(GenericSearchBuilder.class);
GenericSearchBuilder<ManagementServerHostVO, String> 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);
}
}