diff --git a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtGetVolumeStatsCommandWrapper.java b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtGetVolumeStatsCommandWrapper.java index 677af99ed157..aa98cacbc80d 100644 --- a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtGetVolumeStatsCommandWrapper.java +++ b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtGetVolumeStatsCommandWrapper.java @@ -21,12 +21,8 @@ import java.util.HashMap; -import org.libvirt.Connect; -import org.libvirt.LibvirtException; - import com.cloud.agent.api.Answer; import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource; -import com.cloud.hypervisor.kvm.resource.LibvirtConnection; import com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk; import com.cloud.hypervisor.kvm.storage.KVMStoragePool; import com.cloud.resource.CommandWrapper; @@ -42,33 +38,43 @@ public final class LibvirtGetVolumeStatsCommandWrapper extends CommandWrapper statEntry = new HashMap(); - for (String volumeUuid : cmd.getVolumeUuids()) { - VolumeStatsEntry volumeStatsEntry = getVolumeStat(libvirtComputingResource, conn, volumeUuid, storeUuid, poolType); - if (volumeStatsEntry == null) { - String msg = "Can't get disk stats as pool or disk details unavailable for volume: " + volumeUuid + " on the storage pool: " + storeUuid; - return new GetVolumeStatsAnswer(cmd, msg, null); - } + // All volumes of the command are on the same pool, so look it up once instead of once per volume. + sourceKVMPool = libvirtComputingResource.getStoragePoolMgr().getStoragePool(poolType, storeUuid); + } catch (CloudRuntimeException e) { + return new GetVolumeStatsAnswer(cmd, "Can't get vm disk stats: " + e.getMessage(), null); + } + if (sourceKVMPool == null) { + return new GetVolumeStatsAnswer(cmd, "Can't get disk stats as the storage pool " + storeUuid + " is unavailable", null); + } + + HashMap statEntry = new HashMap(); + for (String volumeUuid : cmd.getVolumeUuids()) { + VolumeStatsEntry volumeStatsEntry = getVolumeStat(sourceKVMPool, volumeUuid, storeUuid); + if (volumeStatsEntry != null) { statEntry.put(volumeUuid, volumeStatsEntry); } - return new GetVolumeStatsAnswer(cmd, "", statEntry); - } catch (LibvirtException | CloudRuntimeException e) { - return new GetVolumeStatsAnswer(cmd, "Can't get vm disk stats: " + e.getMessage(), null); } + if (statEntry.isEmpty() && !cmd.getVolumeUuids().isEmpty()) { + return new GetVolumeStatsAnswer(cmd, "Can't get disk stats for any volume on the storage pool: " + storeUuid, null); + } + // A volume that can't be resolved is skipped, so the stats of the other volumes are still reported. + return new GetVolumeStatsAnswer(cmd, "", statEntry); } - private VolumeStatsEntry getVolumeStat(final LibvirtComputingResource libvirtComputingResource, final Connect conn, final String volumeUuid, final String storeUuid, final StoragePoolType poolType) throws LibvirtException { - KVMStoragePool sourceKVMPool = libvirtComputingResource.getStoragePoolMgr().getStoragePool(poolType, storeUuid); - if (sourceKVMPool == null) { + private VolumeStatsEntry getVolumeStat(final KVMStoragePool sourceKVMPool, final String volumeUuid, final String storeUuid) { + KVMPhysicalDisk sourceKVMVolume; + try { + sourceKVMVolume = sourceKVMPool.getPhysicalDisk(volumeUuid); + } catch (CloudRuntimeException e) { + logger.warn("Can't get disk stats for volume {} on the storage pool {}: {}", volumeUuid, storeUuid, e.getMessage()); return null; } - - KVMPhysicalDisk sourceKVMVolume = sourceKVMPool.getPhysicalDisk(volumeUuid); if (sourceKVMVolume == null) { + logger.warn("Can't get disk stats as disk details are unavailable for volume {} on the storage pool {}", volumeUuid, storeUuid); return null; } diff --git a/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtGetVolumeStatsCommandWrapperTest.java b/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtGetVolumeStatsCommandWrapperTest.java new file mode 100644 index 000000000000..45e5800d65af --- /dev/null +++ b/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtGetVolumeStatsCommandWrapperTest.java @@ -0,0 +1,138 @@ +// +// 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.hypervisor.kvm.resource.wrapper; + +import java.util.Arrays; +import java.util.Map; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockedStatic; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; + +import com.cloud.agent.api.GetVolumeStatsAnswer; +import com.cloud.agent.api.GetVolumeStatsCommand; +import com.cloud.agent.api.VolumeStatsEntry; +import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource; +import com.cloud.hypervisor.kvm.resource.LibvirtConnection; +import com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk; +import com.cloud.hypervisor.kvm.storage.KVMStoragePool; +import com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager; +import com.cloud.storage.Storage.StoragePoolType; +import com.cloud.utils.exception.CloudRuntimeException; + +@RunWith(MockitoJUnitRunner.class) +public class LibvirtGetVolumeStatsCommandWrapperTest { + + private static final String POOL_UUID = "pool-uuid"; + private static final StoragePoolType POOL_TYPE = StoragePoolType.RBD; + + @Mock + private LibvirtComputingResource libvirtComputingResource; + @Mock + private KVMStoragePoolManager storagePoolMgr; + @Mock + private KVMStoragePool storagePool; + + private final LibvirtGetVolumeStatsCommandWrapper wrapper = new LibvirtGetVolumeStatsCommandWrapper(); + + // No libvirt in unit tests: getConnection() returns null instead of loading the native library. + private MockedStatic libvirtConnection; + + @Before + public void setUp() { + libvirtConnection = Mockito.mockStatic(LibvirtConnection.class); + Mockito.when(libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr); + } + + @After + public void tearDown() { + libvirtConnection.close(); + } + + private KVMPhysicalDisk disk(long size, long virtualSize) { + KVMPhysicalDisk disk = Mockito.mock(KVMPhysicalDisk.class); + Mockito.when(disk.getSize()).thenReturn(size); + Mockito.when(disk.getVirtualSize()).thenReturn(virtualSize); + return disk; + } + + private Map execute(String... volumeUuids) { + GetVolumeStatsCommand cmd = new GetVolumeStatsCommand(POOL_TYPE, POOL_UUID, Arrays.asList(volumeUuids)); + GetVolumeStatsAnswer answer = (GetVolumeStatsAnswer) wrapper.execute(cmd, libvirtComputingResource); + return answer.getVolumeStats(); + } + + @Test + public void testPoolIsLookedUpOnceForAllVolumes() { + Mockito.when(storagePoolMgr.getStoragePool(POOL_TYPE, POOL_UUID)).thenReturn(storagePool); + KVMPhysicalDisk disk = disk(10L, 20L); + Mockito.when(storagePool.getPhysicalDisk(Mockito.anyString())).thenReturn(disk); + + Map stats = execute("vol-1", "vol-2", "vol-3"); + + Assert.assertEquals(3, stats.size()); + Mockito.verify(storagePoolMgr, Mockito.times(1)).getStoragePool(POOL_TYPE, POOL_UUID); + } + + @Test + public void testVolumeThatCannotBeResolvedDoesNotDropTheOthers() { + Mockito.when(storagePoolMgr.getStoragePool(POOL_TYPE, POOL_UUID)).thenReturn(storagePool); + KVMPhysicalDisk disk = disk(10L, 20L); + Mockito.when(storagePool.getPhysicalDisk("vol-ok")).thenReturn(disk); + Mockito.when(storagePool.getPhysicalDisk("vol-missing")).thenReturn(null); + Mockito.when(storagePool.getPhysicalDisk("vol-error")).thenThrow(new CloudRuntimeException("image not found")); + + Map stats = execute("vol-missing", "vol-ok", "vol-error"); + + Assert.assertEquals(1, stats.size()); + VolumeStatsEntry entry = stats.get("vol-ok"); + Assert.assertEquals(10L, entry.getPhysicalSize()); + Assert.assertEquals(20L, entry.getVirtualSize()); + } + + @Test + public void testUnavailablePoolReturnsNoStats() { + Mockito.when(storagePoolMgr.getStoragePool(POOL_TYPE, POOL_UUID)).thenReturn(null); + + Assert.assertNull(execute("vol-1", "vol-2")); + Mockito.verifyNoInteractions(storagePool); + } + + @Test + public void testPoolLookupErrorReturnsNoStats() { + Mockito.when(storagePoolMgr.getStoragePool(POOL_TYPE, POOL_UUID)).thenThrow(new CloudRuntimeException("pool not found")); + + Assert.assertNull(execute("vol-1")); + } + + @Test + public void testNoStatsWhenNoVolumeCanBeResolved() { + Mockito.when(storagePoolMgr.getStoragePool(POOL_TYPE, POOL_UUID)).thenReturn(storagePool); + Mockito.when(storagePool.getPhysicalDisk(Mockito.anyString())).thenReturn(null); + + Assert.assertNull(execute("vol-1", "vol-2")); + } +}