cloudstack/agent/src/test/java/com/cloud/agent/AgentShellTest.java
Abhishek Kumar 0b5a5e8043
api,agent,server,engine-schema: scalability improvements (#9840)
* api,agent,server,engine-schema: scalability improvements

Following changes and improvements have been added:

- Improvements in handling of PingRoutingCommand

    1. Added global config - `vm.sync.power.state.transitioning`, default value: true, to control syncing of power states for transitioning VMs. This can be set to false to prevent computation of transitioning state VMs.
    2. Improved VirtualMachinePowerStateSync to allow power state sync for host VMs in a batch
    3. Optimized scanning stalled VMs

- Added option to set worker threads for capacity calculation using config - `capacity.calculate.workers`

- Added caching framework based on Caffeine in-memory caching library, https://github.com/ben-manes/caffeine

- Added caching for account/use role API access with expiration after write can be configured using config - `dynamic.apichecker.cache.period`. If set to zero then there will be no caching. Default is 0.

- Added caching for account/use role API access with expiration after write set to 60 seconds.

- Added caching for some recurring DB retrievals

    1. CapacityManager - listing service offerings - beneficial in host capacity calculation
    2. LibvirtServerDiscoverer existing host for the cluster - beneficial for host joins
    3. DownloadListener - hypervisors for zone - beneficial for host joins
    5. VirtualMachineManagerImpl - VMs in progress- beneficial for processing stalled VMs during PingRoutingCommands

- Optimized MS list retrieval for agent connect

- Optimize finding ready systemvm template for zone

- Database retrieval optimisations - fix and refactor for cases where only IDs or counts are used mainly for hosts and other infra entities. Also similar cases for VMs and other entities related to host concerning background tasks

- Changes in agent-agentmanager connection with NIO client-server classes

    1. Optimized the use of the executor service
    2. Refactore Agent class to better handle connections.
    3. Do SSL handshakes within worker threads
    5. Added global configs to control the behaviour depending on the infra. SSL handshake could be a bottleneck during agent connections. Configs - `agent.ssl.handshake.min.workers` and `agent.ssl.handshake.max.workers` can be used to control number of new connections management server handles at a time. `agent.ssl.handshake.timeout` can be used to set number of seconds after which SSL handshake times out at MS end.
    6. On agent side backoff and sslhandshake timeout can be controlled by agent properties. `backoff.seconds` and `ssl.handshake.timeout` properties can be used.

- Improvements in StatsCollection - minimize DB retrievals.

- Improvements in DeploymentPlanner allow for the retrieval of only desired host fields and fewer retrievals.

- Improvements in hosts connection for a storage pool. Added config - `storage.pool.host.connect.workers` to control the number of worker threads that can be used to connect hosts to a storage pool. Worker thread approach is followed currently only for NFS and ScaleIO pools.

- Minor improvements in resource limit calculations wrt DB retrievals

Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>

Co-authored-by: Abhishek Kumar <abhishek.mrt22@gmail.com>
Co-authored-by: Rohit Yadav <rohit.yadav@shapeblue.com>

* test1, domaindetails, capacitymanager fix

Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>

* test2 - agent tests

Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>

* capacitymanagertest fix

Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>

* change

Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>

* fix missing changes

Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>

* address comments

Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>

* revert marvin/setup.py

Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>

* fix indent

Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>

* use space in sql

Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>

* address duplicate

Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>

* update host logs

Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>

* revert e36c6a5d07

Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>

* fix npe in capacity calculation

Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>

* move schema changes to 4.20.1 upgrade

Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>

* build fix

Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>

* address comments

Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>

* fix build

Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>

* add some more tests

Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>

* checkstyle fix

Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>

* remove unnecessary mocks

Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>

* build fix

Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>

* replace statics

Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>

* engine/orchestration,utils: limit number of concurrent new agent
connections

Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>

* refactor - remove unused

Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>

* unregister closed connections, monitor & cleanup

Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>

* add check for outdated vm filter in power sync

Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>

* agent: synchronize sendRequest wait

Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>

---------

Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>
Co-authored-by: Rohit Yadav <rohit.yadav@shapeblue.com>
2025-02-01 12:28:41 +05:30

373 lines
14 KiB
Java

// 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.agent;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.naming.ConfigurationException;
import com.cloud.agent.properties.AgentProperties;
import com.cloud.agent.properties.AgentPropertiesFileHandler;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import com.cloud.utils.StringUtils;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class AgentShellTest {
@InjectMocks
@Spy
AgentShell agentShellSpy = new AgentShell();
@Mock
AgentProperties agentPropertiesMock;
@Mock
AgentProperties.Property<Integer> propertyIntegerMock;
@Mock
AgentProperties.Property<String> propertyStringMock;
@Mock
UUID uuidMock;
MockedStatic<AgentPropertiesFileHandler> agentPropertiesFileHandlerMocked;
@Before
public void setUp() throws Exception {
agentPropertiesFileHandlerMocked = Mockito.mockStatic(AgentPropertiesFileHandler.class, Mockito.CALLS_REAL_METHODS);
}
@After
public void tearDown() throws Exception {
agentPropertiesFileHandlerMocked.close();
}
@Test
public void parseCommand() throws ConfigurationException {
AgentShell shell = new AgentShell();
UUID anyUuid = UUID.randomUUID();
shell.parseCommand(new String[] {"port=55555", "threads=4", "host=localhost", "pod=pod1", "guid=" + anyUuid, "zone=zone1"});
Assert.assertEquals(55555, shell.getPort());
Assert.assertEquals(4, shell.getWorkers());
Assert.assertEquals("localhost", shell.getNextHost());
Assert.assertEquals(anyUuid.toString(), shell.getGuid());
Assert.assertEquals("pod1", shell.getPod());
Assert.assertEquals("zone1", shell.getZone());
}
@Test
public void loadProperties() throws ConfigurationException {
AgentShell shell = new AgentShell();
shell.loadProperties();
Assert.assertNotNull(shell.getProperties());
Assert.assertFalse(shell.getProperties().entrySet().isEmpty());
}
@Test
public void testGetHost() {
AgentShell shell = new AgentShell();
List<String> hosts = Arrays.asList("10.1.1.1", "20.2.2.2", "30.3.3.3", "2001:db8::1");
shell.setHosts(StringUtils.listToCsvTags(hosts));
for (String host : hosts) {
Assert.assertEquals(host, shell.getNextHost());
}
Assert.assertEquals(shell.getNextHost(), hosts.get(0));
}
@Test
public void isValueStartingAndEndingWithAtSignTestValues() {
Map<String, Boolean> valuesAndExpects = new HashMap<>();
valuesAndExpects.put("@test@", true);
valuesAndExpects.put("test@", false);
valuesAndExpects.put("@test", false);
valuesAndExpects.put("test", false);
valuesAndExpects.put("te@st", false);
valuesAndExpects.forEach((value, expected) -> {
boolean result = agentShellSpy.isValueStartingAndEndingWithAtSign(value);
Assert.assertEquals(String.format("Test with value [%s] does not return as expected.", value), expected, result);
});
}
@Test
public void getPortOrWorkersTestValueIsNullGetFromProperty() {
int expected = 195;
agentPropertiesFileHandlerMocked.when(() -> AgentPropertiesFileHandler.getPropertyValue(Mockito.any())).thenReturn(expected);
int result = agentShellSpy.getPortOrWorkers(null, propertyIntegerMock);
Assert.assertEquals(expected, result);
}
@Test
public void getPortOrWorkersTestValueIsNotAValidIntegerReturnDefaultFromProperty() {
int expected = 42;
Mockito.doReturn(expected).when(propertyIntegerMock).getDefaultValue();
int result = agentShellSpy.getPortOrWorkers("test", propertyIntegerMock);
Assert.assertEquals(expected, result);
agentPropertiesFileHandlerMocked.verify(() -> AgentPropertiesFileHandler.getPropertyValue(Mockito.any()), Mockito.never());
}
@Test
public void getPortOrWorkersTestValueIsAValidIntegerReturnValue() {
int expected = 42;
Mockito.doReturn(79).when(propertyIntegerMock).getDefaultValue();
int result = agentShellSpy.getPortOrWorkers(String.valueOf(expected), propertyIntegerMock);
Assert.assertEquals(expected, result);
agentPropertiesFileHandlerMocked.verify(() -> AgentPropertiesFileHandler.getPropertyValue(Mockito.any()), Mockito.never());
}
@Test
public void getWorkersTestWorkersLessThan0ReturnDefault() {
int expected = 42;
Mockito.doReturn(propertyIntegerMock).when(agentPropertiesMock).getWorkers();
Mockito.doReturn(-1).when(agentShellSpy).getPortOrWorkers(Mockito.any(), Mockito.any());
Mockito.doReturn(expected).when(propertyIntegerMock).getDefaultValue();
int result = agentShellSpy.getWorkers("");
Assert.assertEquals(expected, result);
}
@Test
public void getWorkersTestWorkersEqualTo0ReturnDefault() {
int expected = 42;
Mockito.doReturn(propertyIntegerMock).when(agentPropertiesMock).getWorkers();
Mockito.doReturn(0).when(agentShellSpy).getPortOrWorkers(Mockito.any(), Mockito.any());
Mockito.doReturn(expected).when(propertyIntegerMock).getDefaultValue();
int result = agentShellSpy.getWorkers("");
Assert.assertEquals(expected, result);
}
@Test
public void getWorkersTestWorkersHigherThan0ReturnValue() {
int expected = 1;
Mockito.doReturn(expected).when(agentShellSpy).getPortOrWorkers(Mockito.any(), Mockito.any());
int result = agentShellSpy.getWorkers("");
Assert.assertEquals(expected, result);
}
@Test
public void getZoneOrPodTestValueIsNullAndPropertyStartsAndEndsWithAtSignReturnPropertyDefaultValue() {
String expected = "default";
agentPropertiesFileHandlerMocked.when(() -> AgentPropertiesFileHandler.getPropertyValue(Mockito.any())).thenReturn("test");
Mockito.doReturn(true).when(agentShellSpy).isValueStartingAndEndingWithAtSign(Mockito.any());
Mockito.doReturn(expected).when(propertyStringMock).getDefaultValue();
String result = agentShellSpy.getZoneOrPod(null, propertyStringMock);
Assert.assertEquals(expected, result);
}
@Test
public void getZoneOrPodTestValueIsNullAndPropertyDoesNotStartAndEndWithAtSignReturnPropertyDefaultValue() {
String expected = "test";
agentPropertiesFileHandlerMocked.when(() -> AgentPropertiesFileHandler.getPropertyValue(Mockito.any())).thenReturn(expected);
Mockito.doReturn(false).when(agentShellSpy).isValueStartingAndEndingWithAtSign(Mockito.any());
String result = agentShellSpy.getZoneOrPod(null, propertyStringMock);
Assert.assertEquals(expected, result);
}
@Test
public void getZoneOrPodTestValueIsNotNullAndStartsAndEndsWithAtSignReturnPropertyDefaultValue() {
String expected = "default";
Mockito.doReturn(true).when(agentShellSpy).isValueStartingAndEndingWithAtSign(Mockito.any());
Mockito.doReturn(expected).when(propertyStringMock).getDefaultValue();
String result = agentShellSpy.getZoneOrPod("test", propertyStringMock);
Assert.assertEquals(expected, result);
agentPropertiesFileHandlerMocked.verify(() -> AgentPropertiesFileHandler.getPropertyValue(Mockito.any()), Mockito.never());
}
@Test
public void getZoneOrPodTestValueIsNotNullAndDoesNotStartAndEndWithAtSignReturnPropertyDefaultValue() {
String expected = "test";
Mockito.doReturn(false).when(agentShellSpy).isValueStartingAndEndingWithAtSign(Mockito.any());
String result = agentShellSpy.getZoneOrPod(expected, propertyStringMock);
Assert.assertEquals(expected, result);
agentPropertiesFileHandlerMocked.verify(() -> AgentPropertiesFileHandler.getPropertyValue(Mockito.any()), Mockito.never());
}
@Test
public void getGuidTestGuidNotNullReturnIt() throws ConfigurationException {
String expected = "test";
String result = agentShellSpy.getGuid(expected);
Assert.assertEquals(expected, result);
}
@Test
public void getGuidTestGuidIsNullReturnProperty() throws ConfigurationException {
String expected = "test";
agentPropertiesFileHandlerMocked.when(() -> AgentPropertiesFileHandler.getPropertyValue(Mockito.any())).thenReturn(expected);
String result = agentShellSpy.getGuid(null);
Assert.assertEquals(expected, result);
}
@Test
public void getGuidTestGuidAndPropertyAreNullIsDeveloperGenerateNewUuid() throws ConfigurationException {
String expected = "test";
agentPropertiesFileHandlerMocked.when(() -> AgentPropertiesFileHandler.getPropertyValue(Mockito.any())).thenReturn(null, true);
MockedStatic<UUID> uuidMocked = Mockito.mockStatic(UUID.class);
uuidMocked.when(() -> UUID.randomUUID()).thenReturn(uuidMock);
Mockito.doReturn(expected).when(uuidMock).toString();
String result = agentShellSpy.getGuid(null);
Assert.assertEquals(expected, result);
uuidMocked.close();
}
@Test(expected = ConfigurationException.class)
public void getGuidTestGuidAndPropertyAreNullIsNotDeveloperThrowConfigurationException() throws ConfigurationException {
agentPropertiesFileHandlerMocked.when(() -> AgentPropertiesFileHandler.getPropertyValue(Mockito.any())).thenReturn(null, false);
agentShellSpy.getGuid(null);
}
@Test
public void setHostTestValueIsNotNullAndStartsAndEndsWithAtSignThrowConfigurationException(){
Mockito.doReturn(true).when(agentShellSpy).isValueStartingAndEndingWithAtSign(Mockito.any());
boolean error = false;
try {
agentShellSpy.setHost("test");
} catch (ConfigurationException e) {
error = true;
}
if (!error) {
throw new AssertionError("This test expects a ConfigurationException.");
}
agentPropertiesFileHandlerMocked.verify(() -> AgentPropertiesFileHandler.getPropertyValue(Mockito.any()), Mockito.never());
}
@Test
public void setHostTestValueIsNullPropertyStartsAndEndsWithAtSignThrowConfigurationException(){
Mockito.doReturn(true).when(agentShellSpy).isValueStartingAndEndingWithAtSign(Mockito.any());
agentPropertiesFileHandlerMocked.when(() -> AgentPropertiesFileHandler.getPropertyValue(Mockito.any())).thenReturn("test");
boolean error = false;
try {
agentShellSpy.setHost(null);
} catch (ConfigurationException e) {
error = true;
}
if (!error) {
throw new AssertionError("This test expects a ConfigurationException.");
}
agentPropertiesFileHandlerMocked.verify(() -> AgentPropertiesFileHandler.getPropertyValue(Mockito.any()));
}
@Test
public void setHostTestValueIsNotNullAndDoesNotStartAndEndWithAtSignSetHosts() throws ConfigurationException {
String expected = "test";
Mockito.doReturn(false).when(agentShellSpy).isValueStartingAndEndingWithAtSign(Mockito.any());
agentShellSpy.setHost(expected);
agentPropertiesFileHandlerMocked.verify(() -> AgentPropertiesFileHandler.getPropertyValue(Mockito.any()), Mockito.never());
Mockito.verify(agentShellSpy).setHosts(expected);
}
@Test
public void setHostTestValueIsNullPropertyDoesNotStartAndEndWithAtSignSetHosts() throws ConfigurationException {
String expected = "test";
Mockito.doReturn(false).when(agentShellSpy).isValueStartingAndEndingWithAtSign(Mockito.any());
agentPropertiesFileHandlerMocked.when(() -> AgentPropertiesFileHandler.getPropertyValue(Mockito.any())).thenReturn(expected);
agentShellSpy.setHost(null);
agentPropertiesFileHandlerMocked.verify(() -> AgentPropertiesFileHandler.getPropertyValue(Mockito.any()));
AgentPropertiesFileHandler.getPropertyValue(Mockito.any());
Mockito.verify(agentShellSpy).setHosts(expected);
}
@Test
public void updateAndGetConnectedHost() {
String expected = "test";
AgentShell shell = new AgentShell();
shell.setHosts("test");
shell.getNextHost();
shell.updateConnectedHost();
Assert.assertEquals(expected, shell.getConnectedHost());
}
@Test
public void testGetSslHandshakeTimeout() {
Integer expected = 1;
agentPropertiesFileHandlerMocked.when(() -> AgentPropertiesFileHandler.getPropertyValue(Mockito.eq(AgentProperties.SSL_HANDSHAKE_TIMEOUT))).thenReturn(expected);
Assert.assertEquals(expected, agentShellSpy.getSslHandshakeTimeout());
}
}