Skip to content

Commit 6ba07b9

Browse files
Worked around some possible runtime exceptions introduced in PR 780.
1 parent de173bd commit 6ba07b9

File tree

3 files changed

+87
-44
lines changed

3 files changed

+87
-44
lines changed

plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,17 @@
5252
import org.apache.cloudstack.utils.qemu.QemuImg.PhysicalDiskFormat;
5353
import org.apache.commons.io.FileUtils;
5454
import org.apache.commons.io.IOUtils;
55+
import org.apache.commons.lang.ArrayUtils;
56+
import org.apache.commons.lang.math.NumberUtils;
5557
import org.apache.log4j.Logger;
5658
import org.libvirt.Connect;
5759
import org.libvirt.Domain;
5860
import org.libvirt.DomainBlockStats;
5961
import org.libvirt.DomainInfo;
60-
import org.libvirt.MemoryStatistic;
6162
import org.libvirt.DomainInfo.DomainState;
6263
import org.libvirt.DomainInterfaceStats;
6364
import org.libvirt.LibvirtException;
65+
import org.libvirt.MemoryStatistic;
6466
import org.libvirt.NodeInfo;
6567

6668
import com.cloud.agent.api.Answer;
@@ -189,7 +191,6 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv
189191
private String _clusterId;
190192

191193
private long _hvVersion;
192-
private long _kernelVersion;
193194
private int _timeout;
194195
private static final int NUMMEMSTATS =2;
195196

@@ -957,14 +958,6 @@ public boolean configure(final String name, final Map<String, Object> params) th
957958
final KVMStorageProcessor storageProcessor = new KVMStorageProcessor(_storagePoolMgr, this);
958959
storageProcessor.configure(name, params);
959960
storageHandler = new StorageSubsystemCommandHandlerBase(storageProcessor);
960-
961-
final String unameKernelVersion = Script.runSimpleBashScript("uname -r");
962-
final String[] kernelVersions = unameKernelVersion.split("[\\.\\-]");
963-
_kernelVersion = Integer.parseInt(kernelVersions[0]) * 1000 * 1000 + (long)Integer.parseInt(kernelVersions[1]) * 1000 + Integer.parseInt(kernelVersions[2]);
964-
965-
/* Disable this, the code using this is pretty bad and non portable
966-
* getOsVersion();
967-
*/
968961
return true;
969962
}
970963

@@ -2210,7 +2203,7 @@ private void createVif(final LibvirtVMDef vm, final NicTO nic, final String nicA
22102203

22112204
if (s_logger.isDebugEnabled()) {
22122205
s_logger.debug("NIC with MAC " + nic.getMac() + " and BroadcastDomainType " + nic.getBroadcastType() + " in network(" + nic.getGateway() + "/" + nic.getNetmask()
2213-
+ ") is " + nic.getType() + " traffic type. So, vsp-vr-ip " + vrIp + " is set in the metadata");
2206+
+ ") is " + nic.getType() + " traffic type. So, vsp-vr-ip " + vrIp + " is set in the metadata");
22142207
}
22152208
}
22162209

@@ -3020,18 +3013,17 @@ private class VmStats {
30203013
long _ioWrote;
30213014
long _bytesRead;
30223015
long _bytesWrote;
3023-
long _intmemfree;
3024-
long _memory;
3025-
long _maxmemory;
30263016
Calendar _timestamp;
30273017
}
30283018

30293019
public VmStatsEntry getVmStat(final Connect conn, final String vmName) throws LibvirtException {
30303020
Domain dm = null;
30313021
try {
30323022
dm = getDomain(conn, vmName);
3023+
if (dm == null) {
3024+
return null;
3025+
}
30333026
final DomainInfo info = dm.getInfo();
3034-
final MemoryStatistic[] mems = dm.memoryStats(NUMMEMSTATS); //number of memory statistics required.
30353027

30363028
final VmStatsEntry stats = new VmStatsEntry();
30373029

@@ -3040,7 +3032,7 @@ public VmStatsEntry getVmStat(final Connect conn, final String vmName) throws Li
30403032

30413033
stats.setMemoryKBs(info.maxMem);
30423034
stats.setTargetMemoryKBs(info.memory);
3043-
stats.setIntFreeMemoryKBs((double) mems[0].getValue());
3035+
stats.setIntFreeMemoryKBs(getMemoryFreeInKBs(dm));
30443036
/* get cpu utilization */
30453037
VmStats oldStats = null;
30463038

@@ -3125,9 +3117,6 @@ public VmStatsEntry getVmStat(final Connect conn, final String vmName) throws Li
31253117
newStat._bytesRead = bytes_rd;
31263118
newStat._bytesWrote = bytes_wr;
31273119
newStat._timestamp = now;
3128-
newStat._intmemfree = mems[0].getValue();
3129-
newStat._memory = info.memory;
3130-
newStat._maxmemory = info.maxMem;
31313120
_vmStats.put(vmName, newStat);
31323121
return stats;
31333122
} finally {
@@ -3137,6 +3126,23 @@ public VmStatsEntry getVmStat(final Connect conn, final String vmName) throws Li
31373126
}
31383127
}
31393128

3129+
/**
3130+
* This method retrieves the memory statistics from the domain given as parameters.
3131+
* If no memory statistic is found, it will return {@link NumberUtils#LONG_ZERO} as the value of free memory in the domain.
3132+
* If it can retrieve the domain memory statistics, it will return the free memory statistic; that means, it returns the value at the first position of the array returned by {@link Domain#memoryStats(int)}.
3133+
*
3134+
* @param dm
3135+
* @return the amount of free memory in KBs
3136+
* @throws LibvirtException
3137+
*/
3138+
protected long getMemoryFreeInKBs(Domain dm) throws LibvirtException {
3139+
final MemoryStatistic[] mems = dm.memoryStats(NUMMEMSTATS);
3140+
if (ArrayUtils.isEmpty(mems)) {
3141+
return NumberUtils.LONG_ZERO;
3142+
}
3143+
return mems[0].getValue();
3144+
}
3145+
31403146
private boolean canBridgeFirewall(final String prvNic) {
31413147
final Script cmd = new Script(_securityGroupPath, _timeout, s_logger);
31423148
cmd.add("can_bridge_firewall");

plugins/hypervisors/kvm/test/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@
6464
import org.libvirt.DomainInfo.DomainState;
6565
import org.libvirt.DomainInterfaceStats;
6666
import org.libvirt.LibvirtException;
67+
import org.libvirt.MemoryStatistic;
6768
import org.libvirt.NodeInfo;
6869
import org.libvirt.StorageVol;
69-
import org.libvirt.MemoryStatistic;
70-
70+
import org.libvirt.jna.virDomainMemoryStats;
7171
import org.mockito.Matchers;
7272
import org.mockito.Mock;
7373
import org.mockito.Mockito;
@@ -717,10 +717,9 @@ public void testGetVmDiskStatsCommand() {
717717
}
718718
}
719719

720-
@SuppressWarnings("unchecked")
721720
@Test
721+
@SuppressWarnings("unchecked")
722722
public void testGetVmDiskStatsCommandException() {
723-
final Connect conn = Mockito.mock(Connect.class);
724723
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
725724

726725
final String vmName = "Test";
@@ -940,7 +939,6 @@ public void testRebootRouterCommandConnect() {
940939
public void testGetHostStatsCommand() {
941940
// A bit difficult to test due to the logger being passed and the parser itself relying on the connection.
942941
// Have to spend some more time afterwards in order to refactor the wrapper itself.
943-
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
944942
final CPUStat cpuStat = Mockito.mock(CPUStat.class);
945943
final MemStat memStat = Mockito.mock(MemStat.class);
946944

@@ -3521,7 +3519,6 @@ public void testNetworkUsageCommandVpcNoOption() {
35213519
verify(libvirtComputingResource, times(1)).configureVPCNetworkUsage(command.getPrivateIP(), command.getGatewayIP(), command.getOption(), command.getVpcCIDR());
35223520
}
35233521

3524-
@SuppressWarnings("unchecked")
35253522
@Test
35263523
public void testCreatePrivateTemplateFromVolumeCommand() {
35273524
//Simple test used to make sure the flow (LibvirtComputingResource => Request => CommandWrapper) is working.
@@ -5030,4 +5027,40 @@ public void testIsInterface () {
50305027
}
50315028
}
50325029
}
5030+
5031+
@Test
5032+
public void testFetMemoryFreeInKBsDomainReturningNoMemoryStatistics() throws LibvirtException {
5033+
LibvirtComputingResource libvirtComputingResource = new LibvirtComputingResource();
5034+
5035+
Domain domainMock = getDomainConfiguredToReturnMemoryStatistic(null);
5036+
long memoryFreeInKBs = libvirtComputingResource.getMemoryFreeInKBs(domainMock);
5037+
5038+
Assert.assertEquals(0, memoryFreeInKBs);
5039+
}
5040+
5041+
@Test
5042+
public void testFetMemoryFreeInKBsDomainReturningOfSomeMemoryStatistics() throws LibvirtException {
5043+
LibvirtComputingResource libvirtComputingResource = new LibvirtComputingResource();
5044+
5045+
MemoryStatistic[] mem = createMemoryStatisticFreeMemory100();
5046+
Domain domainMock = getDomainConfiguredToReturnMemoryStatistic(mem);
5047+
long memoryFreeInKBs = libvirtComputingResource.getMemoryFreeInKBs(domainMock);
5048+
5049+
Assert.assertEquals(100, memoryFreeInKBs);
5050+
}
5051+
5052+
private MemoryStatistic[] createMemoryStatisticFreeMemory100() {
5053+
virDomainMemoryStats stat = new virDomainMemoryStats();
5054+
stat.val = 100;
5055+
5056+
MemoryStatistic[] mem = new MemoryStatistic[2];
5057+
mem[0] = new MemoryStatistic(stat);
5058+
return mem;
5059+
}
5060+
5061+
private Domain getDomainConfiguredToReturnMemoryStatistic(MemoryStatistic[] mem) throws LibvirtException {
5062+
Domain domainMock = Mockito.mock(Domain.class);
5063+
when(domainMock.memoryStats(2)).thenReturn(mem);
5064+
return domainMock;
5065+
}
50335066
}

plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/CitrixResourceBase.java

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@
167167
@Local(value = ServerResource.class)
168168
public abstract class CitrixResourceBase implements ServerResource, HypervisorResource, VirtualRouterDeployer {
169169

170+
private final static int BASE_TO_CONVERT_BYTES_INTO_KILOBYTES = 1024;
171+
170172
public enum SRType {
171173
EXT, FILE, ISCSI, ISO, LVM, LVMOHBA, LVMOISCSI, NFS;
172174

@@ -2306,7 +2308,7 @@ public SR getIscsiSR(final Connection conn, final String srNameLabel, final Stri
23062308
}
23072309
if (target.equals(dc.get("target")) && targetiqn.equals(dc.get("targetIQN")) && lunid.equals(dc.get("lunid"))) {
23082310
throw new CloudRuntimeException("There is a SR using the same configuration target:" + dc.get("target") + ", targetIQN:" + dc.get("targetIQN")
2309-
+ ", lunid:" + dc.get("lunid") + " for pool " + srNameLabel + "on host:" + _host.getUuid());
2311+
+ ", lunid:" + dc.get("lunid") + " for pool " + srNameLabel + "on host:" + _host.getUuid());
23102312
}
23112313
}
23122314
deviceConfig.put("target", target);
@@ -2617,7 +2619,7 @@ protected XsLocalNetwork getManagementNetwork(final Connection conn) throws XmlR
26172619
final Bond bond = mgmtPifRec.bondSlaveOf;
26182620
if (!isRefNull(bond)) {
26192621
final String msg = "Management interface is on slave(" + mgmtPifRec.uuid + ") of bond(" + bond.getUuid(conn) + ") on host(" + _host.getUuid()
2620-
+ "), please move management interface to bond!";
2622+
+ "), please move management interface to bond!";
26212623
s_logger.warn(msg);
26222624
throw new CloudRuntimeException(msg);
26232625
}
@@ -2837,7 +2839,7 @@ public SR getNfsSR(final Connection conn, final String poolid, final String uuid
28372839

28382840
if (server.equals(dc.get("server")) && serverpath.equals(dc.get("serverpath"))) {
28392841
throw new CloudRuntimeException("There is a SR using the same configuration server:" + dc.get("server") + ", serverpath:" + dc.get("serverpath")
2840-
+ " for pool " + uuid + " on host:" + _host.getUuid());
2842+
+ " for pool " + uuid + " on host:" + _host.getUuid());
28412843
}
28422844

28432845
}
@@ -3324,20 +3326,22 @@ public HashMap<String, VmStatsEntry> getVmStats(final Connection conn, final Get
33243326
if (param.contains("cpu")) {
33253327
vmStatsAnswer.setNumCPUs(vmStatsAnswer.getNumCPUs() + 1);
33263328
vmStatsAnswer.setCPUUtilization(vmStatsAnswer.getCPUUtilization() + getDataAverage(dataNode, col, numRows));
3327-
} else if (param.matches("vif_\\d*_rx")) {
3328-
vmStatsAnswer.setNetworkReadKBs(vmStatsAnswer.getNetworkReadKBs() + getDataAverage(dataNode, col, numRows) / 1000);
3329-
} else if (param.matches("vif_\\d*_tx")) {
3330-
vmStatsAnswer.setNetworkWriteKBs(vmStatsAnswer.getNetworkWriteKBs() + getDataAverage(dataNode, col, numRows) / 1000);
3331-
} else if (param.matches("vbd_.*_read")) {
3332-
vmStatsAnswer.setDiskReadKBs(vmStatsAnswer.getDiskReadKBs() + getDataAverage(dataNode, col, numRows) / 1000);
3333-
} else if (param.matches("vbd_.*_write")) {
3334-
vmStatsAnswer.setDiskWriteKBs(vmStatsAnswer.getDiskWriteKBs() + getDataAverage(dataNode, col, numRows) / 1000);
3335-
} else if (param.contains("memory_internal_free")) {
3336-
vmStatsAnswer.setIntFreeMemoryKBs(vmStatsAnswer.getIntFreeMemoryKBs() + getDataAverage(dataNode, col, numRows) / 1024);
3337-
} else if (param.contains("memory_target")) {
3338-
vmStatsAnswer.setTargetMemoryKBs(vmStatsAnswer.getTargetMemoryKBs() + getDataAverage(dataNode, col, numRows) / 1024);
3339-
} else if (param.contains("memory")) {
3340-
vmStatsAnswer.setMemoryKBs(vmStatsAnswer.getMemoryKBs() + getDataAverage(dataNode, col, numRows) / 1024);
3329+
} else {
3330+
if (param.matches("vif_\\d*_rx")) {
3331+
vmStatsAnswer.setNetworkReadKBs(vmStatsAnswer.getNetworkReadKBs() + getDataAverage(dataNode, col, numRows) / BASE_TO_CONVERT_BYTES_INTO_KILOBYTES);
3332+
} else if (param.matches("vif_\\d*_tx")) {
3333+
vmStatsAnswer.setNetworkWriteKBs(vmStatsAnswer.getNetworkWriteKBs() + getDataAverage(dataNode, col, numRows) / BASE_TO_CONVERT_BYTES_INTO_KILOBYTES);
3334+
} else if (param.matches("vbd_.*_read")) {
3335+
vmStatsAnswer.setDiskReadKBs(vmStatsAnswer.getDiskReadKBs() + getDataAverage(dataNode, col, numRows) / BASE_TO_CONVERT_BYTES_INTO_KILOBYTES);
3336+
} else if (param.matches("vbd_.*_write")) {
3337+
vmStatsAnswer.setDiskWriteKBs(vmStatsAnswer.getDiskWriteKBs() + getDataAverage(dataNode, col, numRows) / BASE_TO_CONVERT_BYTES_INTO_KILOBYTES);
3338+
} else if (param.contains("memory_internal_free")) {
3339+
vmStatsAnswer.setIntFreeMemoryKBs(vmStatsAnswer.getIntFreeMemoryKBs() + getDataAverage(dataNode, col, numRows) / BASE_TO_CONVERT_BYTES_INTO_KILOBYTES);
3340+
} else if (param.contains("memory_target")) {
3341+
vmStatsAnswer.setTargetMemoryKBs(vmStatsAnswer.getTargetMemoryKBs() + getDataAverage(dataNode, col, numRows) / BASE_TO_CONVERT_BYTES_INTO_KILOBYTES);
3342+
} else if (param.contains("memory")) {
3343+
vmStatsAnswer.setMemoryKBs(vmStatsAnswer.getMemoryKBs() + getDataAverage(dataNode, col, numRows) / BASE_TO_CONVERT_BYTES_INTO_KILOBYTES);
3344+
}
33413345
}
33423346

33433347
}
@@ -5080,8 +5084,8 @@ public boolean createVmdataFiles(final String vmName, final List<String[]> vmDat
50805084
if (result && content != null && !content.isEmpty()) {
50815085
File file = new File(folder+"/"+fileName+".txt");
50825086
try (OutputStreamWriter fw = new OutputStreamWriter(new FileOutputStream(file.getAbsoluteFile()),"UTF-8");
5083-
BufferedWriter bw = new BufferedWriter(fw);
5084-
) {
5087+
BufferedWriter bw = new BufferedWriter(fw);
5088+
) {
50855089
bw.write(content);
50865090
s_logger.debug("created file: "+ file + " in folder:"+folder);
50875091
} catch (final IOException ex) {

0 commit comments

Comments
 (0)