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
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,23 @@

public interface PrometheusExporter {

/**
* Update the Prometheus metrics in text format.
*
* NOTE: capacity data is refreshed independently by {@code AlertManagerImpl}'s own
* periodic {@code CapacityChecker} timer. Do NOT force a synchronous
* {@code recalculateCapacity()} call here: it spins up a fresh thread pool per host
* and per storage pool across ALL zones on every single scrape, so with Z zones a
* single Prometheus scrape triggered Z redundant full recalculations. That extra,
* uncoordinated load compounds over time and can lead to {@code scrape_duration_seconds}
* climbing until a management-server restart.
*
* @see PrometheusExporterImpl#updateMetrics()
*/
void updateMetrics();

/**
* @return the latest Prometheus metrics refreshed by {@link #updateMetrics()}.
*/
String getMetrics();
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import org.apache.cloudstack.storage.datastore.db.ImageStoreDao;
import org.apache.commons.lang3.StringUtils;

import com.cloud.alert.AlertManager;
import com.cloud.api.ApiDBUtils;
import com.cloud.api.query.dao.DomainJoinDao;
import com.cloud.api.query.dao.StoragePoolJoinDao;
Expand Down Expand Up @@ -126,8 +125,6 @@ public String toString() {
@Inject
private DomainJoinDao domainDao;
@Inject
private AlertManager alertManager;
@Inject
DedicatedResourceDao _dedicatedDao;
@Inject
private AccountDao _accountDao;
Expand Down Expand Up @@ -497,7 +494,6 @@ public void updateMetrics() {
for (final DataCenterVO dc : dcDao.listAll()) {
final String zoneName = dc.getName();
final String zoneUuid = dc.getUuid();
alertManager.recalculateCapacity();
addHostMetrics(latestMetricsItems, dc.getId(), zoneName, zoneUuid);
addVMMetrics(latestMetricsItems, dc.getId(), zoneName, zoneUuid);
addVolumeMetrics(latestMetricsItems, dc.getId(), zoneName, zoneUuid);
Expand Down
33 changes: 25 additions & 8 deletions server/src/main/java/com/cloud/alert/AlertManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ public class AlertManagerImpl extends ManagerBase implements AlertManager, Confi

private final ExecutorService _executor;

private ExecutorService capacityExecutorService;

protected SMTPMailSender mailSender;
protected String[] recipients = null;
protected String senderAddress = null;
Expand Down Expand Up @@ -249,6 +251,9 @@ public boolean start() {
@Override
public boolean stop() {
_timer.cancel();
if (capacityExecutorService != null) {
capacityExecutorService.shutdown();
}
return true;
}

Expand Down Expand Up @@ -281,6 +286,24 @@ public void sendAlert(AlertType alertType, long dataCenterId, Long podId, String
}
}

/**
* Shared, long-lived pool for capacity recalculation, reused across every
* recalculateHostCapacities()/recalculateStorageCapacities() call instead of creating and
* tearing down a new thread pool per invocation. Repeatedly creating/shutting down pools was
* unnecessary overhead under frequent callers (e.g. the Prometheus exporter used to trigger a
* full recalculation on every scrape, see https://github.com/apache/cloudstack/issues/13586).
* Lazily created so this remains safe for callers that invoke the recalculate methods directly
* without going through configure()/start() (e.g. unit tests).
*/
private synchronized ExecutorService getCapacityExecutorService() {
if (capacityExecutorService == null || capacityExecutorService.isShutdown()) {
capacityExecutorService = Executors.newFixedThreadPool(
Math.max(1, CapacityManager.CapacityCalculateWorkers.value()),
new NamedThreadFactory("Capacity-Calculator"));
}
return capacityExecutorService;
}

/**
* Recalculates the capacities of hosts, including CPU and RAM.
*/
Expand All @@ -290,10 +313,8 @@ protected void recalculateHostCapacities() {
return;
}
ConcurrentHashMap<Long, Future<Void>> futures = new ConcurrentHashMap<>();
ExecutorService executorService = Executors.newFixedThreadPool(Math.max(1,
Math.min(CapacityManager.CapacityCalculateWorkers.value(), hostIds.size())));
for (Long hostId : hostIds) {
futures.put(hostId, executorService.submit(() -> {
futures.put(hostId, getCapacityExecutorService().submit(() -> {
final HostVO host = hostDao.findById(hostId);
_capacityMgr.updateCapacityForHost(host);
return null;
Expand All @@ -307,7 +328,6 @@ protected void recalculateHostCapacities() {
entry.getKey(), e.getMessage()), e);
}
}
executorService.shutdown();
}

protected void recalculateStorageCapacities() {
Expand All @@ -316,10 +336,8 @@ protected void recalculateStorageCapacities() {
return;
}
ConcurrentHashMap<Long, Future<Void>> futures = new ConcurrentHashMap<>();
ExecutorService executorService = Executors.newFixedThreadPool(Math.max(1,
Math.min(CapacityManager.CapacityCalculateWorkers.value(), storagePoolIds.size())));
for (Long poolId: storagePoolIds) {
futures.put(poolId, executorService.submit(() -> {
futures.put(poolId, getCapacityExecutorService().submit(() -> {
Transaction.execute(new TransactionCallbackNoReturn() {
@Override
public void doInTransactionWithoutResult(TransactionStatus status) {
Expand All @@ -343,7 +361,6 @@ public void doInTransactionWithoutResult(TransactionStatus status) {
entry.getKey(), e.getMessage()), e);
}
}
executorService.shutdown();
}

@Override
Expand Down
113 changes: 113 additions & 0 deletions server/src/test/java/com/cloud/alert/AlertManagerImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
package com.cloud.alert;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Timer;
import java.util.concurrent.ExecutorService;

import javax.mail.MessagingException;

Expand Down Expand Up @@ -219,4 +222,114 @@
Mockito.verify(storageManager, Mockito.times(2)).createCapacityEntry(sharedPool, Capacity.CAPACITY_TYPE_STORAGE_ALLOCATED, 10L);
Mockito.verify(storageManager, Mockito.times(1)).createCapacityEntry(nonSharedPool, Capacity.CAPACITY_TYPE_LOCAL_STORAGE, 20L);
}

@Test
public void testRecalculateHostCapacitiesWithEmptyHostList() throws Exception {
Mockito.when(hostDao.listIdsByType(Host.Type.Routing)).thenReturn(List.of());

Check warning on line 228 in server/src/test/java/com/cloud/alert/AlertManagerImplTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "when".

See more on https://sonarcloud.io/project/issues?id=apache_cloudstack&issues=AZ_Mf2V1UG1NZvDOk5hb&open=AZ_Mf2V1UG1NZvDOk5hb&pullRequest=13650
alertManagerImplMock.recalculateHostCapacities();
Mockito.verify(hostDao, Mockito.never()).findById(Mockito.anyLong());

Check warning on line 230 in server/src/test/java/com/cloud/alert/AlertManagerImplTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "never".

See more on https://sonarcloud.io/project/issues?id=apache_cloudstack&issues=AZ_Mf2V1UG1NZvDOk5hd&open=AZ_Mf2V1UG1NZvDOk5hd&pullRequest=13650

Check warning on line 230 in server/src/test/java/com/cloud/alert/AlertManagerImplTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "verify".

See more on https://sonarcloud.io/project/issues?id=apache_cloudstack&issues=AZ_Mf2V1UG1NZvDOk5hc&open=AZ_Mf2V1UG1NZvDOk5hc&pullRequest=13650
Mockito.verify(capacityManager, Mockito.never()).updateCapacityForHost(Mockito.any());

Check warning on line 231 in server/src/test/java/com/cloud/alert/AlertManagerImplTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "never".

See more on https://sonarcloud.io/project/issues?id=apache_cloudstack&issues=AZ_Mf2V1UG1NZvDOk5hf&open=AZ_Mf2V1UG1NZvDOk5hf&pullRequest=13650

Check warning on line 231 in server/src/test/java/com/cloud/alert/AlertManagerImplTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "verify".

See more on https://sonarcloud.io/project/issues?id=apache_cloudstack&issues=AZ_Mf2V1UG1NZvDOk5he&open=AZ_Mf2V1UG1NZvDOk5he&pullRequest=13650
assertNull("executor should never be created when there is nothing to submit", getCapacityExecutorService());
}

@Test
public void testRecalculateStorageCapacitiesWithEmptyPoolList() throws Exception {
Mockito.when(primaryDataStoreDao.listAllIds()).thenReturn(List.of());

Check warning on line 237 in server/src/test/java/com/cloud/alert/AlertManagerImplTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "when".

See more on https://sonarcloud.io/project/issues?id=apache_cloudstack&issues=AZ_Mf2V1UG1NZvDOk5hg&open=AZ_Mf2V1UG1NZvDOk5hg&pullRequest=13650
alertManagerImplMock.recalculateStorageCapacities();
Mockito.verify(primaryDataStoreDao, Mockito.never()).findById(Mockito.anyLong());

Check warning on line 239 in server/src/test/java/com/cloud/alert/AlertManagerImplTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "verify".

See more on https://sonarcloud.io/project/issues?id=apache_cloudstack&issues=AZ_Mf2V1UG1NZvDOk5hh&open=AZ_Mf2V1UG1NZvDOk5hh&pullRequest=13650

Check warning on line 239 in server/src/test/java/com/cloud/alert/AlertManagerImplTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "never".

See more on https://sonarcloud.io/project/issues?id=apache_cloudstack&issues=AZ_Mf2V1UG1NZvDOk5hi&open=AZ_Mf2V1UG1NZvDOk5hi&pullRequest=13650
Mockito.verify(storageManager, Mockito.never()).createCapacityEntry(Mockito.any(), Mockito.anyShort(), Mockito.anyLong());

Check warning on line 240 in server/src/test/java/com/cloud/alert/AlertManagerImplTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "verify".

See more on https://sonarcloud.io/project/issues?id=apache_cloudstack&issues=AZ_Mf2V1UG1NZvDOk5hj&open=AZ_Mf2V1UG1NZvDOk5hj&pullRequest=13650

Check warning on line 240 in server/src/test/java/com/cloud/alert/AlertManagerImplTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "never".

See more on https://sonarcloud.io/project/issues?id=apache_cloudstack&issues=AZ_Mf2V1UG1NZvDOk5hk&open=AZ_Mf2V1UG1NZvDOk5hk&pullRequest=13650
assertNull("executor should never be created when there is nothing to submit", getCapacityExecutorService());
}

@Test
public void testRecalculateHostCapacitiesLogsAndContinuesOnTaskFailure() {
Mockito.when(hostDao.listIdsByType(Host.Type.Routing)).thenReturn(List.of(1L, 2L, 3L));

Check warning on line 246 in server/src/test/java/com/cloud/alert/AlertManagerImplTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "when".

See more on https://sonarcloud.io/project/issues?id=apache_cloudstack&issues=AZ_Mf2V1UG1NZvDOk5hl&open=AZ_Mf2V1UG1NZvDOk5hl&pullRequest=13650
HostVO host1 = Mockito.mock(HostVO.class);

Check warning on line 247 in server/src/test/java/com/cloud/alert/AlertManagerImplTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "mock".

See more on https://sonarcloud.io/project/issues?id=apache_cloudstack&issues=AZ_Mf2V1UG1NZvDOk5hm&open=AZ_Mf2V1UG1NZvDOk5hm&pullRequest=13650
HostVO host2 = Mockito.mock(HostVO.class);

Check warning on line 248 in server/src/test/java/com/cloud/alert/AlertManagerImplTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "mock".

See more on https://sonarcloud.io/project/issues?id=apache_cloudstack&issues=AZ_Mf2V1UG1NZvDOk5hn&open=AZ_Mf2V1UG1NZvDOk5hn&pullRequest=13650
HostVO host3 = Mockito.mock(HostVO.class);

Check warning on line 249 in server/src/test/java/com/cloud/alert/AlertManagerImplTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "mock".

See more on https://sonarcloud.io/project/issues?id=apache_cloudstack&issues=AZ_Mf2V1UG1NZvDOk5ho&open=AZ_Mf2V1UG1NZvDOk5ho&pullRequest=13650
Mockito.when(hostDao.findById(1L)).thenReturn(host1);

Check warning on line 250 in server/src/test/java/com/cloud/alert/AlertManagerImplTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "when".

See more on https://sonarcloud.io/project/issues?id=apache_cloudstack&issues=AZ_Mf2V1UG1NZvDOk5hp&open=AZ_Mf2V1UG1NZvDOk5hp&pullRequest=13650
Mockito.when(hostDao.findById(2L)).thenReturn(host2);

Check warning on line 251 in server/src/test/java/com/cloud/alert/AlertManagerImplTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "when".

See more on https://sonarcloud.io/project/issues?id=apache_cloudstack&issues=AZ_Mf2V1UG1NZvDOk5hq&open=AZ_Mf2V1UG1NZvDOk5hq&pullRequest=13650
Mockito.when(hostDao.findById(3L)).thenReturn(host3);

Check warning on line 252 in server/src/test/java/com/cloud/alert/AlertManagerImplTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "when".

See more on https://sonarcloud.io/project/issues?id=apache_cloudstack&issues=AZ_Mf2V1UG1NZvDOk5hr&open=AZ_Mf2V1UG1NZvDOk5hr&pullRequest=13650
Mockito.doThrow(new RuntimeException("boom")).when(capacityManager).updateCapacityForHost(host2);

Check warning on line 253 in server/src/test/java/com/cloud/alert/AlertManagerImplTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "doThrow".

See more on https://sonarcloud.io/project/issues?id=apache_cloudstack&issues=AZ_Mf2V1UG1NZvDOk5hs&open=AZ_Mf2V1UG1NZvDOk5hs&pullRequest=13650

alertManagerImplMock.recalculateHostCapacities();

Mockito.verify(capacityManager).updateCapacityForHost(host1);

Check warning on line 257 in server/src/test/java/com/cloud/alert/AlertManagerImplTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "verify".

See more on https://sonarcloud.io/project/issues?id=apache_cloudstack&issues=AZ_Mf2V1UG1NZvDOk5ht&open=AZ_Mf2V1UG1NZvDOk5ht&pullRequest=13650
Mockito.verify(capacityManager).updateCapacityForHost(host2);

Check warning on line 258 in server/src/test/java/com/cloud/alert/AlertManagerImplTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "verify".

See more on https://sonarcloud.io/project/issues?id=apache_cloudstack&issues=AZ_Mf2V1UG1NZvDOk5hu&open=AZ_Mf2V1UG1NZvDOk5hu&pullRequest=13650
Mockito.verify(capacityManager).updateCapacityForHost(host3);

Check warning on line 259 in server/src/test/java/com/cloud/alert/AlertManagerImplTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "verify".

See more on https://sonarcloud.io/project/issues?id=apache_cloudstack&issues=AZ_Mf2V1UG1NZvDOk5hv&open=AZ_Mf2V1UG1NZvDOk5hv&pullRequest=13650
Mockito.verify(alertManagerImplMock.logger).error(Mockito.anyString(), Mockito.any(Throwable.class));

Check warning on line 260 in server/src/test/java/com/cloud/alert/AlertManagerImplTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "verify".

See more on https://sonarcloud.io/project/issues?id=apache_cloudstack&issues=AZ_Mf2V1UG1NZvDOk5hw&open=AZ_Mf2V1UG1NZvDOk5hw&pullRequest=13650
}

@Test
public void testRecalculateHostCapacitiesReusesExecutorAcrossCalls() throws Exception {
Mockito.when(hostDao.listIdsByType(Host.Type.Routing)).thenReturn(List.of(1L));
Mockito.when(hostDao.findById(Mockito.anyLong())).thenReturn(Mockito.mock(HostVO.class));

Check warning on line 266 in server/src/test/java/com/cloud/alert/AlertManagerImplTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "mock".

See more on https://sonarcloud.io/project/issues?id=apache_cloudstack&issues=AZ_Mf2V1UG1NZvDOk5hx&open=AZ_Mf2V1UG1NZvDOk5hx&pullRequest=13650

Check warning on line 266 in server/src/test/java/com/cloud/alert/AlertManagerImplTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Extract this mock creation to a local variable.

See more on https://sonarcloud.io/project/issues?id=apache_cloudstack&issues=AZ_Mf2V1UG1NZvDOk5hY&open=AZ_Mf2V1UG1NZvDOk5hY&pullRequest=13650

alertManagerImplMock.recalculateHostCapacities();
ExecutorService firstExecutor = getCapacityExecutorService();
assertNotNull(firstExecutor);

Mockito.when(primaryDataStoreDao.listAllIds()).thenReturn(List.of(101L));

Check warning on line 272 in server/src/test/java/com/cloud/alert/AlertManagerImplTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "when".

See more on https://sonarcloud.io/project/issues?id=apache_cloudstack&issues=AZ_Mf2V1UG1NZvDOk5hy&open=AZ_Mf2V1UG1NZvDOk5hy&pullRequest=13650
StoragePoolVO pool = Mockito.mock(StoragePoolVO.class);

Check warning on line 273 in server/src/test/java/com/cloud/alert/AlertManagerImplTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "mock".

See more on https://sonarcloud.io/project/issues?id=apache_cloudstack&issues=AZ_Mf2V1UG1NZvDOk5hz&open=AZ_Mf2V1UG1NZvDOk5hz&pullRequest=13650
Mockito.when(primaryDataStoreDao.findById(101L)).thenReturn(pool);

Check warning on line 274 in server/src/test/java/com/cloud/alert/AlertManagerImplTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "when".

See more on https://sonarcloud.io/project/issues?id=apache_cloudstack&issues=AZ_Mf2V1UG1NZvDOk5h0&open=AZ_Mf2V1UG1NZvDOk5h0&pullRequest=13650
alertManagerImplMock.recalculateStorageCapacities();

assertEquals("host and storage recalculation should share the same long-lived pool",
firstExecutor, getCapacityExecutorService());
}

@Test
public void testRecalculateHostCapacitiesRecreatesExecutorAfterShutdown() throws Exception {
Mockito.when(hostDao.listIdsByType(Host.Type.Routing)).thenReturn(List.of(1L));

Check warning on line 283 in server/src/test/java/com/cloud/alert/AlertManagerImplTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "when".

See more on https://sonarcloud.io/project/issues?id=apache_cloudstack&issues=AZ_Mf2V1UG1NZvDOk5h1&open=AZ_Mf2V1UG1NZvDOk5h1&pullRequest=13650
Mockito.when(hostDao.findById(Mockito.anyLong())).thenReturn(Mockito.mock(HostVO.class));

Check warning on line 284 in server/src/test/java/com/cloud/alert/AlertManagerImplTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Extract this mock creation to a local variable.

See more on https://sonarcloud.io/project/issues?id=apache_cloudstack&issues=AZ_Mf2V1UG1NZvDOk5hZ&open=AZ_Mf2V1UG1NZvDOk5hZ&pullRequest=13650

Check warning on line 284 in server/src/test/java/com/cloud/alert/AlertManagerImplTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "mock".

See more on https://sonarcloud.io/project/issues?id=apache_cloudstack&issues=AZ_Mf2V1UG1NZvDOk5h3&open=AZ_Mf2V1UG1NZvDOk5h3&pullRequest=13650

Check warning on line 284 in server/src/test/java/com/cloud/alert/AlertManagerImplTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "when".

See more on https://sonarcloud.io/project/issues?id=apache_cloudstack&issues=AZ_Mf2V1UG1NZvDOk5h2&open=AZ_Mf2V1UG1NZvDOk5h2&pullRequest=13650

alertManagerImplMock.recalculateHostCapacities();
ExecutorService firstExecutor = getCapacityExecutorService();
firstExecutor.shutdown();

alertManagerImplMock.recalculateHostCapacities();
ExecutorService secondExecutor = getCapacityExecutorService();

Assert.assertNotEquals("a shut down executor should be replaced rather than reused", firstExecutor, secondExecutor);
Assert.assertFalse(secondExecutor.isShutdown());
}

@Test
public void testStopShutsDownCapacityExecutorServiceWhenPresent() throws Exception {
Timer timerMock = Mockito.mock(Timer.class);

Check warning on line 299 in server/src/test/java/com/cloud/alert/AlertManagerImplTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "mock".

See more on https://sonarcloud.io/project/issues?id=apache_cloudstack&issues=AZ_Mf2V1UG1NZvDOk5h4&open=AZ_Mf2V1UG1NZvDOk5h4&pullRequest=13650
setTimer(timerMock);
Mockito.when(hostDao.listIdsByType(Host.Type.Routing)).thenReturn(List.of(1L));

Check warning on line 301 in server/src/test/java/com/cloud/alert/AlertManagerImplTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "when".

See more on https://sonarcloud.io/project/issues?id=apache_cloudstack&issues=AZ_Mf2V1UG1NZvDOk5h5&open=AZ_Mf2V1UG1NZvDOk5h5&pullRequest=13650
Mockito.when(hostDao.findById(Mockito.anyLong())).thenReturn(Mockito.mock(HostVO.class));

Check warning on line 302 in server/src/test/java/com/cloud/alert/AlertManagerImplTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "mock".

See more on https://sonarcloud.io/project/issues?id=apache_cloudstack&issues=AZ_Mf2V1UG1NZvDOk5h7&open=AZ_Mf2V1UG1NZvDOk5h7&pullRequest=13650

Check warning on line 302 in server/src/test/java/com/cloud/alert/AlertManagerImplTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Extract this mock creation to a local variable.

See more on https://sonarcloud.io/project/issues?id=apache_cloudstack&issues=AZ_Mf2V1UG1NZvDOk5ha&open=AZ_Mf2V1UG1NZvDOk5ha&pullRequest=13650

Check warning on line 302 in server/src/test/java/com/cloud/alert/AlertManagerImplTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "when".

See more on https://sonarcloud.io/project/issues?id=apache_cloudstack&issues=AZ_Mf2V1UG1NZvDOk5h6&open=AZ_Mf2V1UG1NZvDOk5h6&pullRequest=13650
alertManagerImplMock.recalculateHostCapacities();

boolean result = alertManagerImplMock.stop();

Assert.assertTrue(result);
Mockito.verify(timerMock).cancel();

Check warning on line 308 in server/src/test/java/com/cloud/alert/AlertManagerImplTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "verify".

See more on https://sonarcloud.io/project/issues?id=apache_cloudstack&issues=AZ_Mf2V1UG1NZvDOk5h8&open=AZ_Mf2V1UG1NZvDOk5h8&pullRequest=13650
Assert.assertTrue(getCapacityExecutorService().isShutdown());
}

@Test
public void testStopDoesNotThrowWhenCapacityExecutorServiceNeverCreated() throws Exception {
Timer timerMock = Mockito.mock(Timer.class);

Check warning on line 314 in server/src/test/java/com/cloud/alert/AlertManagerImplTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "mock".

See more on https://sonarcloud.io/project/issues?id=apache_cloudstack&issues=AZ_Mf2V1UG1NZvDOk5h9&open=AZ_Mf2V1UG1NZvDOk5h9&pullRequest=13650
setTimer(timerMock);

boolean result = alertManagerImplMock.stop();

Assert.assertTrue(result);
Mockito.verify(timerMock).cancel();

Check warning on line 320 in server/src/test/java/com/cloud/alert/AlertManagerImplTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a static import for "verify".

See more on https://sonarcloud.io/project/issues?id=apache_cloudstack&issues=AZ_Mf2V1UG1NZvDOk5h-&open=AZ_Mf2V1UG1NZvDOk5h-&pullRequest=13650
assertNull(getCapacityExecutorService());
}

private ExecutorService getCapacityExecutorService() throws Exception {
Field field = AlertManagerImpl.class.getDeclaredField("capacityExecutorService");
field.setAccessible(true);
return (ExecutorService) field.get(alertManagerImplMock);
}

private void setTimer(Timer timer) throws Exception {
Field field = AlertManagerImpl.class.getDeclaredField("_timer");
field.setAccessible(true);
field.set(alertManagerImplMock, timer);
}
}
Loading