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 @@ -27,7 +27,6 @@

import org.apache.cloudstack.framework.messagebus.MessageBus;
import org.apache.cloudstack.framework.messagebus.PublishScope;
import org.apache.cloudstack.utils.cache.LazyCache;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.logging.log4j.LogManager;
Expand All @@ -36,8 +35,6 @@
import com.cloud.agent.api.HostVmStateReportEntry;
import com.cloud.configuration.ManagementServiceConfiguration;
import com.cloud.host.Host;
import com.cloud.host.HostVO;
import com.cloud.host.dao.HostDao;
import com.cloud.utils.DateUtil;
import com.cloud.vm.dao.VMInstanceDao;

Expand All @@ -46,17 +43,8 @@ public class VirtualMachinePowerStateSyncImpl implements VirtualMachinePowerStat

@Inject MessageBus _messageBus;
@Inject VMInstanceDao _instanceDao;
@Inject HostDao hostDao;
@Inject ManagementServiceConfiguration mgmtServiceConf;

private LazyCache<Long, VMInstanceVO> vmCache;
private LazyCache<Long, HostVO> hostCache;

public VirtualMachinePowerStateSyncImpl() {
vmCache = new LazyCache<>(16, 10, this::getVmFromId);
hostCache = new LazyCache<>(16, 10, this::getHostFromId);
}

@Override
public void resetHostSyncState(Host host) {
logger.info("Reset VM power state sync for host: {}", host);
Expand All @@ -65,14 +53,14 @@ public void resetHostSyncState(Host host) {

@Override
public void processHostVmStateReport(long hostId, Map<String, HostVmStateReportEntry> report) {
logger.debug("Process host VM state report. host: {}", hostCache.get(hostId));
logger.debug("Process host VM state report. host id: {}", hostId);
Map<Long, VirtualMachine.PowerState> translatedInfo = convertVmStateReport(report);
processReport(hostId, translatedInfo, false);
}

@Override
public void processHostVmStatePingReport(long hostId, Map<String, HostVmStateReportEntry> report, boolean force) {
logger.debug("Process host VM state report from ping process. host: {}", hostCache.get(hostId));
logger.debug("Process host VM state report from ping process. host id: {}", hostId);
Map<Long, VirtualMachine.PowerState> translatedInfo = convertVmStateReport(report);
processReport(hostId, translatedInfo, force);
}
Expand All @@ -90,13 +78,13 @@ protected void updateAndPublishVmPowerStates(long hostId, Map<Long, VirtualMachi
}
for (Long vmId : vmIds) {
if (MapUtils.isEmpty(notUpdated) || !notUpdated.containsKey(vmId)) {
logger.debug("VM state report is updated. {}, {}, power state: {}",
() -> hostCache.get(hostId), () -> vmCache.get(vmId), () -> instancePowerStates.get(vmId));
logger.debug("VM state report is updated. Host id {}, VM id {}, power state: {}",
hostId, vmId, instancePowerStates.get(vmId));
_messageBus.publish(null, VirtualMachineManager.Topics.VM_POWER_STATE,
PublishScope.GLOBAL, vmId);
continue;
}
logger.trace("VM power state does not change, skip DB writing. {}", () -> vmCache.get(vmId));
logger.trace("VM power state does not change, skip DB writing. VM id {}", vmId);
}
}

Expand Down Expand Up @@ -143,7 +131,7 @@ private void processMissingVmReport(long hostId, Set<Long> vmIds, boolean force)
vmStateUpdateTime = instance.getCreated();
}
}
logger.debug("Detected missing VM. host: {}, vm id: {}({}), power state: {}, last state update: {}",
logger.debug("Detected missing VM. Host id: {}, vm id: {}({}), power state: {}, last state update: {}",
hostId,
instance.getId(),
instance.getUuid(),
Expand All @@ -166,16 +154,16 @@ private void processMissingVmReport(long hostId, Set<Long> vmIds, boolean force)
}

private void processReport(long hostId, Map<Long, VirtualMachine.PowerState> translatedInfo, boolean force) {
logger.debug("Process VM state report. {}, number of records in report: {}. VMs: [{}]",
() -> hostCache.get(hostId),
logger.debug("Process VM state report. Host id {}, number of records in report: {}. VMs: [{}]",
() -> hostId,
translatedInfo::size,
() -> translatedInfo.entrySet().stream().map(entry -> entry.getKey() + ":" + entry.getValue())
.collect(Collectors.joining(", ")) + "]");
.collect(Collectors.joining(", ")));
updateAndPublishVmPowerStates(hostId, translatedInfo, DateUtil.currentGMTTime());

processMissingVmReport(hostId, translatedInfo.keySet(), force);

logger.debug("Done with process of VM state report. host: {}", () -> hostCache.get(hostId));
logger.debug("Done with process of VM state report. Host id: {}", hostId);
}

public Map<Long, VirtualMachine.PowerState> convertVmStateReport(Map<String, HostVmStateReportEntry> states) {
Expand All @@ -194,12 +182,4 @@ public Map<Long, VirtualMachine.PowerState> convertVmStateReport(Map<String, Hos
}
return map;
}

protected VMInstanceVO getVmFromId(long vmId) {
return _instanceDao.findById(vmId);
}

protected HostVO getHostFromId(long hostId) {
return hostDao.findById(hostId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,13 @@

import org.apache.cloudstack.framework.messagebus.MessageBus;
import org.apache.cloudstack.framework.messagebus.PublishScope;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;

import com.cloud.host.HostVO;
import com.cloud.host.dao.HostDao;
import com.cloud.vm.dao.VMInstanceDao;

@RunWith(MockitoJUnitRunner.class)
Expand All @@ -40,18 +37,10 @@ public class VirtualMachinePowerStateSyncImplTest {
MessageBus messageBus;
@Mock
VMInstanceDao instanceDao;
@Mock
HostDao hostDao;

@InjectMocks
VirtualMachinePowerStateSyncImpl virtualMachinePowerStateSync = new VirtualMachinePowerStateSyncImpl();

@Before
public void setup() {
Mockito.lenient().when(instanceDao.findById(Mockito.anyLong())).thenReturn(Mockito.mock(VMInstanceVO.class));
Mockito.lenient().when(hostDao.findById(Mockito.anyLong())).thenReturn(Mockito.mock(HostVO.class));
}

@Test
public void test_updateAndPublishVmPowerStates_emptyStates() {
virtualMachinePowerStateSync.updateAndPublishVmPowerStates(1L, new HashMap<>(), new Date());
Expand Down
Loading