HADOOP-13663 Index out of range in SysInfoWindows. Contributed by Inigo Goiri

This commit is contained in:
Steve Loughran 2016-09-29 11:35:00 +01:00
parent 1f1e47e411
commit 2b6b4fd779
2 changed files with 37 additions and 28 deletions

View File

@ -100,36 +100,40 @@ void refreshIfNeeded() {
String sysInfoStr = getSystemInfoInfoFromShell(); String sysInfoStr = getSystemInfoInfoFromShell();
if (sysInfoStr != null) { if (sysInfoStr != null) {
final int sysInfoSplitCount = 11; final int sysInfoSplitCount = 11;
String[] sysInfo = sysInfoStr.substring(0, sysInfoStr.indexOf("\r\n")) int index = sysInfoStr.indexOf("\r\n");
.split(","); if (index >= 0) {
if (sysInfo.length == sysInfoSplitCount) { String[] sysInfo = sysInfoStr.substring(0, index).split(",");
try { if (sysInfo.length == sysInfoSplitCount) {
vmemSize = Long.parseLong(sysInfo[0]); try {
memSize = Long.parseLong(sysInfo[1]); vmemSize = Long.parseLong(sysInfo[0]);
vmemAvailable = Long.parseLong(sysInfo[2]); memSize = Long.parseLong(sysInfo[1]);
memAvailable = Long.parseLong(sysInfo[3]); vmemAvailable = Long.parseLong(sysInfo[2]);
numProcessors = Integer.parseInt(sysInfo[4]); memAvailable = Long.parseLong(sysInfo[3]);
cpuFrequencyKhz = Long.parseLong(sysInfo[5]); numProcessors = Integer.parseInt(sysInfo[4]);
cumulativeCpuTimeMs = Long.parseLong(sysInfo[6]); cpuFrequencyKhz = Long.parseLong(sysInfo[5]);
storageBytesRead = Long.parseLong(sysInfo[7]); cumulativeCpuTimeMs = Long.parseLong(sysInfo[6]);
storageBytesWritten = Long.parseLong(sysInfo[8]); storageBytesRead = Long.parseLong(sysInfo[7]);
netBytesRead = Long.parseLong(sysInfo[9]); storageBytesWritten = Long.parseLong(sysInfo[8]);
netBytesWritten = Long.parseLong(sysInfo[10]); netBytesRead = Long.parseLong(sysInfo[9]);
if (lastCumCpuTimeMs != -1) { netBytesWritten = Long.parseLong(sysInfo[10]);
/** if (lastCumCpuTimeMs != -1) {
* This number will be the aggregated usage across all cores in /**
* [0.0, 100.0]. For example, it will be 400.0 if there are 8 * This number will be the aggregated usage across all cores in
* cores and each of them is running at 50% utilization. * [0.0, 100.0]. For example, it will be 400.0 if there are 8
*/ * cores and each of them is running at 50% utilization.
cpuUsage = (cumulativeCpuTimeMs - lastCumCpuTimeMs) */
* 100F / refreshInterval; cpuUsage = (cumulativeCpuTimeMs - lastCumCpuTimeMs)
* 100F / refreshInterval;
}
} catch (NumberFormatException nfe) {
LOG.warn("Error parsing sysInfo", nfe);
} }
} catch (NumberFormatException nfe) { } else {
LOG.warn("Error parsing sysInfo", nfe); LOG.warn("Expected split length of sysInfo to be "
+ sysInfoSplitCount + ". Got " + sysInfo.length);
} }
} else { } else {
LOG.warn("Expected split length of sysInfo to be " LOG.warn("Wrong output from sysInfo: " + sysInfoStr);
+ sysInfoSplitCount + ". Got " + sysInfo.length);
} }
} }
} }

View File

@ -141,10 +141,15 @@ public void refreshAndCpuUsageMulticore() throws InterruptedException {
@Test(timeout = 10000) @Test(timeout = 10000)
public void errorInGetSystemInfo() { public void errorInGetSystemInfo() {
SysInfoWindowsMock tester = new SysInfoWindowsMock(); SysInfoWindowsMock tester = new SysInfoWindowsMock();
// info str derived from windows shell command has \r\n termination // info str derived from windows shell command is null
tester.setSysinfoString(null); tester.setSysinfoString(null);
// call a method to refresh values // call a method to refresh values
tester.getAvailablePhysicalMemorySize(); tester.getAvailablePhysicalMemorySize();
// info str derived from windows shell command with no \r\n termination
tester.setSysinfoString("");
// call a method to refresh values
tester.getAvailablePhysicalMemorySize();
} }
} }