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
11 changes: 11 additions & 0 deletions server/src/main/java/com/cloud/server/StatsCollector.java
Original file line number Diff line number Diff line change
Expand Up @@ -1297,6 +1297,11 @@ protected Point createInfluxDbPoint(Object metricsObject) {
}
}

private boolean isStatsCleanupOwner() {
ManagementServerHostVO msHost = managementServerHostDao.findOneByLongestRuntime();
return msHost != null && msHost.getMsid() == managementServerNodeId;
}

/**
* <p>Previously, the VM stats cleanup process was triggered during the data collection process.
* So, when data collection was disabled, the cleaning process was also disabled.</p>
Expand All @@ -1309,6 +1314,9 @@ class VmStatsCleaner extends ManagedContextRunnable{
@Override
protected void runInContext() {
try {
if (!isStatsCleanupOwner()) {
return;
}
cleanUpVirtualMachineStats();
} catch (RuntimeException e) {
logger.error("Error trying to clean up VM stats", e);
Expand All @@ -1320,6 +1328,9 @@ class VolumeStatsCleaner extends ManagedContextRunnable{
@Override
protected void runInContext() {
try {
if (!isStatsCleanupOwner()) {
return;
}
cleanUpVolumeStats();
} catch (RuntimeException e) {
logger.error("Error trying to clean up Volume stats", e);
Expand Down
67 changes: 67 additions & 0 deletions server/src/test/java/com/cloud/server/StatsCollectorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import java.util.concurrent.TimeUnit;

import com.cloud.utils.DateUtil;
import com.cloud.cluster.ManagementServerHostVO;
import com.cloud.cluster.dao.ManagementServerHostDao;
import com.google.gson.JsonSyntaxException;
import org.apache.cloudstack.framework.config.ConfigKey;
import org.apache.cloudstack.network.RoutedIpv4Manager;
Expand Down Expand Up @@ -134,6 +136,9 @@ public class StatsCollectorTest {
@Mock
private VlanDao vlanDao;

@Mock
private ManagementServerHostDao managementServerHostDao;

private static Gson gson = new Gson();

private Gson msStatsGson;
Expand Down Expand Up @@ -376,6 +381,7 @@ public void cleanUpVolumeStatsTestIsEnabled() {

@Test
public void vmStatsCleanerTestCatchesCloudRuntimeExceptionAndKeepsRunning() {
setStatsCleanupOwner(1L);
Mockito.doThrow(new CloudRuntimeException("Communications link failure")).when(statsCollector).cleanUpVirtualMachineStats();
StatsCollector.VmStatsCleaner vmStatsCleaner = statsCollector.new VmStatsCleaner();

Expand All @@ -386,6 +392,7 @@ public void vmStatsCleanerTestCatchesCloudRuntimeExceptionAndKeepsRunning() {

@Test
public void volumeStatsCleanerTestCatchesCloudRuntimeExceptionAndKeepsRunning() {
setStatsCleanupOwner(1L);
Mockito.doThrow(new CloudRuntimeException("Communications link failure")).when(statsCollector).cleanUpVolumeStats();
StatsCollector.VolumeStatsCleaner volumeStatsCleaner = statsCollector.new VolumeStatsCleaner();

Expand All @@ -394,6 +401,66 @@ public void volumeStatsCleanerTestCatchesCloudRuntimeExceptionAndKeepsRunning()
Mockito.verify(statsCollector).cleanUpVolumeStats();
}

private void setStatsCleanupOwner(long ownerId) {
statsCollector.managementServerNodeId = 1L;
ManagementServerHostVO owner = Mockito.mock(ManagementServerHostVO.class);
Mockito.when(owner.getMsid()).thenReturn(ownerId);
Mockito.when(managementServerHostDao.findOneByLongestRuntime()).thenReturn(owner);
}

@Test
public void statsCleanersRunOnLongestRunningServer() {
setStatsCleanupOwner(1L);
Mockito.doNothing().when(statsCollector).cleanUpVirtualMachineStats();
Mockito.doNothing().when(statsCollector).cleanUpVolumeStats();

statsCollector.new VmStatsCleaner().runInContext();
statsCollector.new VolumeStatsCleaner().runInContext();

Mockito.verify(statsCollector).cleanUpVirtualMachineStats();
Mockito.verify(statsCollector).cleanUpVolumeStats();
}

@Test
public void statsCleanersSkipOtherServers() {
setStatsCleanupOwner(2L);

statsCollector.new VmStatsCleaner().runInContext();
statsCollector.new VolumeStatsCleaner().runInContext();

Mockito.verify(statsCollector, Mockito.never()).cleanUpVirtualMachineStats();
Mockito.verify(statsCollector, Mockito.never()).cleanUpVolumeStats();
}

@Test
public void statsCleanersSkipWhenNoServerIsUp() {
statsCollector.new VmStatsCleaner().runInContext();
statsCollector.new VolumeStatsCleaner().runInContext();

Mockito.verify(statsCollector, Mockito.never()).cleanUpVirtualMachineStats();
Mockito.verify(statsCollector, Mockito.never()).cleanUpVolumeStats();
}

@Test
public void statsCleanersTakeOverWhenOldestServerChanges() {
setStatsCleanupOwner(2L);
StatsCollector.VmStatsCleaner vmCleaner = statsCollector.new VmStatsCleaner();
StatsCollector.VolumeStatsCleaner volumeCleaner = statsCollector.new VolumeStatsCleaner();
vmCleaner.runInContext();
volumeCleaner.runInContext();
Mockito.verify(statsCollector, Mockito.never()).cleanUpVirtualMachineStats();
Mockito.verify(statsCollector, Mockito.never()).cleanUpVolumeStats();

setStatsCleanupOwner(1L);
Mockito.doNothing().when(statsCollector).cleanUpVirtualMachineStats();
Mockito.doNothing().when(statsCollector).cleanUpVolumeStats();
vmCleaner.runInContext();
volumeCleaner.runInContext();

Mockito.verify(statsCollector).cleanUpVirtualMachineStats();
Mockito.verify(statsCollector).cleanUpVolumeStats();
}

@Test
public void persistVirtualMachineStatsTestPersistsSuccessfully() {
statsCollector.msId = 1L;
Expand Down