This is an automated email from the ASF dual-hosted git repository.

nvazquez pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cloudstack.git


The following commit(s) were added to refs/heads/main by this push:
     new 0efffb326be Remove DB calls from VirtualMachinePowerStateSyncImpl logs 
(#14940)
0efffb326be is described below

commit 0efffb326be2701778916ddfebb97681535761c1
Author: Nicolas Vazquez <[email protected]>
AuthorDate: Thu Dec 3 08:42:44 2026 -0300

    Remove DB calls from VirtualMachinePowerStateSyncImpl logs (#14940)
    
    * Remove DB calls from VirtualMachinePowerStateSyncImpl logs
    
    VirtualMachinePowerStateSyncImpl maintained a lazy cache of host
    id->host and vm id->vm mappings in order to provide host and vm
    objects in method debug/trace logs. On a cache miss, debug logs
    would use HostDao and VMInstanceDao to query the respective objects
    from the primary database. If all HikariCP DB connections were
    exhausted, this call would spin for 30 seconds before timing out
    and throwing a CloudRuntimeException. This meant that simple
    logging blocked agent-handler threads, creating performance
    degradation for the entire management server.
    
    Fix by logging just host ID and VM ID instead of objects, removing
    the need to call the primary DB and maintain a cache. Remove
    corresponding imports, constructors, helper methods, and test
    scaffold.
    
    * Remove extra closing bracket on Power VM state report log
    
    ---------
    
    Co-authored-by: Maximilian Dittgen <[email protected]>
---
 .../cloud/vm/VirtualMachinePowerStateSyncImpl.java | 40 ++++++----------------
 .../vm/VirtualMachinePowerStateSyncImplTest.java   | 11 ------
 2 files changed, 10 insertions(+), 41 deletions(-)

diff --git 
a/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachinePowerStateSyncImpl.java
 
b/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachinePowerStateSyncImpl.java
index 475ed0f37bd..d43125d52bc 100644
--- 
a/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachinePowerStateSyncImpl.java
+++ 
b/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachinePowerStateSyncImpl.java
@@ -27,7 +27,6 @@ import javax.inject.Inject;
 
 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;
@@ -36,8 +35,6 @@ import org.apache.logging.log4j.Logger;
 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;
 
@@ -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);
@@ -65,14 +53,14 @@ public class VirtualMachinePowerStateSyncImpl implements 
VirtualMachinePowerStat
 
     @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);
     }
@@ -90,13 +78,13 @@ public class VirtualMachinePowerStateSyncImpl implements 
VirtualMachinePowerStat
         }
         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);
         }
     }
 
@@ -143,7 +131,7 @@ public class VirtualMachinePowerStateSyncImpl implements 
VirtualMachinePowerStat
                     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(),
@@ -166,16 +154,16 @@ public class VirtualMachinePowerStateSyncImpl implements 
VirtualMachinePowerStat
     }
 
     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) {
@@ -194,12 +182,4 @@ public class VirtualMachinePowerStateSyncImpl implements 
VirtualMachinePowerStat
         }
         return map;
     }
-
-    protected VMInstanceVO getVmFromId(long vmId) {
-        return _instanceDao.findById(vmId);
-    }
-
-    protected HostVO getHostFromId(long hostId) {
-        return hostDao.findById(hostId);
-    }
 }
diff --git 
a/engine/orchestration/src/test/java/com/cloud/vm/VirtualMachinePowerStateSyncImplTest.java
 
b/engine/orchestration/src/test/java/com/cloud/vm/VirtualMachinePowerStateSyncImplTest.java
index 4df14fe22f3..f9c3187dc90 100644
--- 
a/engine/orchestration/src/test/java/com/cloud/vm/VirtualMachinePowerStateSyncImplTest.java
+++ 
b/engine/orchestration/src/test/java/com/cloud/vm/VirtualMachinePowerStateSyncImplTest.java
@@ -22,7 +22,6 @@ import java.util.Map;
 
 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;
@@ -30,8 +29,6 @@ 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)
@@ -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());

Reply via email to