Merge pull request #16061 from jasontedor/normalize-unavailable-load-average
Normalize unavailable load average Relates #12049, #14741, #15907, #15932, #15934
This commit is contained in:
commit
90284e6f01
|
@ -119,12 +119,15 @@ public class OsProbe {
|
||||||
}
|
}
|
||||||
// fallback
|
// fallback
|
||||||
}
|
}
|
||||||
|
if (Constants.WINDOWS) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
if (getSystemLoadAverage == null) {
|
if (getSystemLoadAverage == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
double oneMinuteLoadAverage = (double) getSystemLoadAverage.invoke(osMxBean);
|
double oneMinuteLoadAverage = (double) getSystemLoadAverage.invoke(osMxBean);
|
||||||
return new double[] { oneMinuteLoadAverage, -1, -1 };
|
return new double[] { oneMinuteLoadAverage >= 0 ? oneMinuteLoadAverage : -1, -1, -1 };
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,6 +28,7 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||||
import org.elasticsearch.common.xcontent.XContentBuilderString;
|
import org.elasticsearch.common.xcontent.XContentBuilderString;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
|
@ -89,7 +90,7 @@ public class OsStats implements Streamable, ToXContent {
|
||||||
if (cpu != null) {
|
if (cpu != null) {
|
||||||
builder.startObject(Fields.CPU);
|
builder.startObject(Fields.CPU);
|
||||||
builder.field(Fields.PERCENT, cpu.getPercent());
|
builder.field(Fields.PERCENT, cpu.getPercent());
|
||||||
if (cpu.getLoadAverage() != null) {
|
if (cpu.getLoadAverage() != null && Arrays.stream(cpu.getLoadAverage()).anyMatch(load -> load != -1)) {
|
||||||
builder.startObject(Fields.LOAD_AVERAGE);
|
builder.startObject(Fields.LOAD_AVERAGE);
|
||||||
if (cpu.getLoadAverage()[0] != -1) {
|
if (cpu.getLoadAverage()[0] != -1) {
|
||||||
builder.field(Fields.LOAD_AVERAGE_1M, cpu.getLoadAverage()[0]);
|
builder.field(Fields.LOAD_AVERAGE_1M, cpu.getLoadAverage()[0]);
|
||||||
|
|
|
@ -29,7 +29,6 @@ import static org.hamcrest.Matchers.equalTo;
|
||||||
import static org.hamcrest.Matchers.greaterThan;
|
import static org.hamcrest.Matchers.greaterThan;
|
||||||
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
|
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
|
||||||
import static org.hamcrest.Matchers.is;
|
import static org.hamcrest.Matchers.is;
|
||||||
import static org.hamcrest.Matchers.lessThan;
|
|
||||||
import static org.hamcrest.Matchers.lessThanOrEqualTo;
|
import static org.hamcrest.Matchers.lessThanOrEqualTo;
|
||||||
|
|
||||||
public class OsProbeTests extends ESTestCase {
|
public class OsProbeTests extends ESTestCase {
|
||||||
|
@ -56,22 +55,29 @@ public class OsProbeTests extends ESTestCase {
|
||||||
}
|
}
|
||||||
if (Constants.WINDOWS) {
|
if (Constants.WINDOWS) {
|
||||||
// load average is unavailable on Windows
|
// load average is unavailable on Windows
|
||||||
if (loadAverage != null) {
|
assertNull(loadAverage);
|
||||||
assertThat(loadAverage[0], equalTo((double) -1));
|
|
||||||
assertThat(loadAverage[1], equalTo((double) -1));
|
|
||||||
assertThat(loadAverage[2], equalTo((double) -1));
|
|
||||||
}
|
|
||||||
} else if (Constants.LINUX) {
|
} else if (Constants.LINUX) {
|
||||||
// we should be able to get the load average
|
// we should be able to get the load average
|
||||||
assertNotNull(loadAverage);
|
assertNotNull(loadAverage);
|
||||||
assertThat(loadAverage[0], greaterThanOrEqualTo((double) 0));
|
assertThat(loadAverage[0], greaterThanOrEqualTo((double) 0));
|
||||||
assertThat(loadAverage[1], greaterThanOrEqualTo((double) 0));
|
assertThat(loadAverage[1], greaterThanOrEqualTo((double) 0));
|
||||||
assertThat(loadAverage[2], greaterThanOrEqualTo((double) 0));
|
assertThat(loadAverage[2], greaterThanOrEqualTo((double) 0));
|
||||||
} else {
|
} else if (Constants.FREE_BSD) {
|
||||||
|
// five- and fifteen-minute load averages not available if linprocfs is not mounted at /compat/linux/proc
|
||||||
|
assertNotNull(loadAverage);
|
||||||
|
assertThat(loadAverage[0], greaterThanOrEqualTo((double) 0));
|
||||||
|
assertThat(loadAverage[1], anyOf(equalTo((double) -1), greaterThanOrEqualTo((double) 0)));
|
||||||
|
assertThat(loadAverage[2], anyOf(equalTo((double) -1), greaterThanOrEqualTo((double) 0)));
|
||||||
|
} else if (Constants.MAC_OS_X) {
|
||||||
// one minute load average is available, but 10-minute and 15-minute load averages are not
|
// one minute load average is available, but 10-minute and 15-minute load averages are not
|
||||||
// load average can be negative if not available or not computed yet, otherwise it should be >= 0
|
assertNotNull(loadAverage);
|
||||||
|
assertThat(loadAverage[0], greaterThanOrEqualTo((double) 0));
|
||||||
|
assertThat(loadAverage[1], equalTo((double) -1));
|
||||||
|
assertThat(loadAverage[2], equalTo((double) -1));
|
||||||
|
} else {
|
||||||
|
// unknown system, but the best case is that we have the one-minute load average
|
||||||
if (loadAverage != null) {
|
if (loadAverage != null) {
|
||||||
assertThat(loadAverage[0], anyOf(lessThan((double) 0), greaterThanOrEqualTo((double) 0)));
|
assertThat(loadAverage[0], anyOf(equalTo((double) -1), greaterThanOrEqualTo((double) 0)));
|
||||||
assertThat(loadAverage[1], equalTo((double) -1));
|
assertThat(loadAverage[1], equalTo((double) -1));
|
||||||
assertThat(loadAverage[2], equalTo((double) -1));
|
assertThat(loadAverage[2], equalTo((double) -1));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue